]> Pileus Git - ~andy/linux/blob - drivers/staging/csr/unifi_pdu_processing.c
staging: csr: remove the CsrTime typedef altogether
[~andy/linux] / drivers / staging / csr / unifi_pdu_processing.c
1 /*
2  * ---------------------------------------------------------------------------
3  * FILE:     unifi_pdu_processing.c
4  *
5  * PURPOSE:
6  *      This file provides the PDU handling functionality before it gets sent to unfi and after
7  *      receiving a PDU from unifi
8  *
9  * Copyright (C) 2010 by Cambridge Silicon Radio Ltd.
10  *
11  * Refer to LICENSE.txt included with this source code for details on
12  * the license terms.
13  *
14  * ---------------------------------------------------------------------------
15  */
16
17 #include <linux/types.h>
18 #include <linux/etherdevice.h>
19 #include <linux/vmalloc.h>
20
21 #include "csr_wifi_hip_unifi.h"
22 #include "csr_wifi_hip_conversions.h"
23 #include "csr_time.h"
24 #include "unifi_priv.h"
25 #include <net/pkt_sched.h>
26
27 #ifdef CSR_SUPPORT_SME
28 static void _update_buffered_pkt_params_after_alignment(unifi_priv_t *priv, bulk_data_param_t *bulkdata,
29                                                         tx_buffered_packets_t* buffered_pkt)
30 {
31     struct sk_buff *skb ;
32     u32 align_offset;
33
34     if (priv == NULL || bulkdata == NULL || buffered_pkt == NULL){
35         return;
36     }
37
38     skb = (struct sk_buff*)bulkdata->d[0].os_net_buf_ptr;
39     align_offset = (u32)(long)(bulkdata->d[0].os_data_ptr) & (CSR_WIFI_ALIGN_BYTES-1);
40     if(align_offset){
41         skb_pull(skb,align_offset);
42     }
43
44     buffered_pkt->bulkdata.os_data_ptr = bulkdata->d[0].os_data_ptr;
45     buffered_pkt->bulkdata.data_length = bulkdata->d[0].data_length;
46     buffered_pkt->bulkdata.os_net_buf_ptr = bulkdata->d[0].os_net_buf_ptr;
47     buffered_pkt->bulkdata.net_buf_length = bulkdata->d[0].net_buf_length;
48 }
49 #endif
50
51 void
52 unifi_frame_ma_packet_req(unifi_priv_t *priv, CSR_PRIORITY priority,
53                           CSR_RATE TransmitRate, CSR_CLIENT_TAG hostTag,
54                           u16 interfaceTag, CSR_TRANSMISSION_CONTROL transmissionControl,
55                           CSR_PROCESS_ID leSenderProcessId, u8 *peerMacAddress,
56                           CSR_SIGNAL *signal)
57 {
58
59     CSR_MA_PACKET_REQUEST *req = &signal->u.MaPacketRequest;
60     netInterface_priv_t *interfacePriv;
61     u8 ba_session_idx = 0;
62     ba_session_tx_struct *ba_session = NULL;
63     u8 *ba_addr = NULL;
64
65     interfacePriv = priv->interfacePriv[interfaceTag];
66
67         unifi_trace(priv, UDBG5,
68                 "In unifi_frame_ma_packet_req, Frame for Peer: %pMF\n",
69                 peerMacAddress);
70     signal->SignalPrimitiveHeader.SignalId = CSR_MA_PACKET_REQUEST_ID;
71     signal->SignalPrimitiveHeader.ReceiverProcessId = 0;
72     signal->SignalPrimitiveHeader.SenderProcessId = leSenderProcessId;
73
74     /* Fill the MA-PACKET.req */
75     req->Priority = priority;
76     unifi_trace(priv, UDBG3, "Tx Frame with Priority: 0x%x\n", req->Priority);
77
78     /* A value of 0 is used for auto selection of rates. But for P2P GO case
79      * for action frames the rate is governed by SME. Hence instead of 0,
80      * the rate is filled in with the value passed here
81      */
82     req->TransmitRate = TransmitRate;
83
84     /* packets from netdev then no confirm required but packets from
85      * Nme/Sme eapol data frames requires the confirmation
86      */
87     req->TransmissionControl = transmissionControl;
88     req->VirtualInterfaceIdentifier =
89            uf_get_vif_identifier(interfacePriv->interfaceMode,interfaceTag);
90     memcpy(req->Ra.x, peerMacAddress, ETH_ALEN);
91
92     if (hostTag == 0xffffffff) {
93         req->HostTag = interfacePriv->tag++;
94         req->HostTag |= 0x40000000;
95         unifi_trace(priv, UDBG3, "new host tag assigned = 0x%x\n", req->HostTag);
96         interfacePriv->tag &= 0x0fffffff;
97     } else {
98         req->HostTag = hostTag;
99         unifi_trace(priv, UDBG3, "host tag got from SME  = 0x%x\n", req->HostTag);
100     }
101     /* check if BA session exists for the peer MAC address on same tID */
102     if(interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_AP ||
103        interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO){
104         ba_addr = peerMacAddress;
105     }else{
106         ba_addr = interfacePriv->bssid.a;
107     }
108     for (ba_session_idx=0; ba_session_idx < MAX_SUPPORTED_BA_SESSIONS_TX; ba_session_idx++){
109         ba_session = interfacePriv->ba_session_tx[ba_session_idx];
110         if (ba_session){
111            if ((!memcmp(ba_session->macAddress.a, ba_addr, ETH_ALEN)) && (ba_session->tID == priority)){
112                 req->TransmissionControl |= CSR_ALLOW_BA;
113                 break;
114             }
115         }
116     }
117
118     unifi_trace(priv, UDBG5, "leaving unifi_frame_ma_packet_req\n");
119 }
120
121 #ifdef CSR_SUPPORT_SME
122
123 #define TRANSMISSION_CONTROL_TRIGGER_MASK 0x0001
124 #define TRANSMISSION_CONTROL_EOSP_MASK 0x0002
125
126 static
127 int frame_and_send_queued_pdu(unifi_priv_t* priv,tx_buffered_packets_t* buffered_pkt,
128             CsrWifiRouterCtrlStaInfo_t *staRecord,u8 moreData , u8 eosp)
129 {
130
131     CSR_SIGNAL signal;
132     bulk_data_param_t bulkdata;
133     int result;
134     u8 toDs, fromDs, macHeaderLengthInBytes = MAC_HEADER_SIZE;
135     u8 *qc;
136     u16 *fc = (u16*)(buffered_pkt->bulkdata.os_data_ptr);
137     unsigned long lock_flags;
138     unifi_trace(priv, UDBG3, "frame_and_send_queued_pdu with moreData: %d , EOSP: %d\n",moreData,eosp);
139     unifi_frame_ma_packet_req(priv, buffered_pkt->priority, buffered_pkt->rate, buffered_pkt->hostTag,
140                buffered_pkt->interfaceTag, buffered_pkt->transmissionControl,
141                buffered_pkt->leSenderProcessId, buffered_pkt->peerMacAddress.a, &signal);
142     bulkdata.d[0].os_data_ptr = buffered_pkt->bulkdata.os_data_ptr;
143     bulkdata.d[0].data_length = buffered_pkt->bulkdata.data_length;
144     bulkdata.d[0].os_net_buf_ptr = buffered_pkt->bulkdata.os_net_buf_ptr;
145     bulkdata.d[0].net_buf_length = buffered_pkt->bulkdata.net_buf_length;
146     bulkdata.d[1].os_data_ptr = NULL;
147     bulkdata.d[1].data_length = 0;
148     bulkdata.d[1].os_net_buf_ptr =0;
149     bulkdata.d[1].net_buf_length =0;
150
151     if(moreData) {
152         *fc |= cpu_to_le16(IEEE802_11_FC_MOREDATA_MASK);
153     } else {
154         *fc &= cpu_to_le16(~IEEE802_11_FC_MOREDATA_MASK);
155     }
156
157     if((staRecord != NULL)&& (staRecord->wmmOrQosEnabled == TRUE))
158     {
159         unifi_trace(priv, UDBG3, "frame_and_send_queued_pdu WMM Enabled: %d \n",staRecord->wmmOrQosEnabled);
160
161         toDs = (*fc & cpu_to_le16(IEEE802_11_FC_TO_DS_MASK))?1 : 0;
162         fromDs = (*fc & cpu_to_le16(IEEE802_11_FC_FROM_DS_MASK))? 1: 0;
163
164         switch(le16_to_cpu(*fc) & IEEE80211_FC_SUBTYPE_MASK)
165         {
166             case IEEE802_11_FC_TYPE_QOS_DATA & IEEE80211_FC_SUBTYPE_MASK:
167             case IEEE802_11_FC_TYPE_QOS_NULL & IEEE80211_FC_SUBTYPE_MASK:
168                 /* If both are set then the Address4 exists (only for AP) */
169                 if (fromDs && toDs) {
170                     /* 6 is the size of Address4 field */
171                     macHeaderLengthInBytes += (QOS_CONTROL_HEADER_SIZE + 6);
172                 } else {
173                     macHeaderLengthInBytes += QOS_CONTROL_HEADER_SIZE;
174                 }
175
176                 /* If order bit set then HT control field is the part of MAC header */
177                 if (*fc & cpu_to_le16(IEEE80211_FC_ORDER_MASK)) {
178                     macHeaderLengthInBytes += HT_CONTROL_HEADER_SIZE;
179                     qc = (u8*)(buffered_pkt->bulkdata.os_data_ptr + (macHeaderLengthInBytes-6));
180                 } else {
181                     qc = (u8*)(buffered_pkt->bulkdata.os_data_ptr + (macHeaderLengthInBytes-2));
182                 }
183                 *qc = eosp ? *qc | (1 << 4) : *qc & (~(1 << 4));
184                 break;
185             default:
186                 if (fromDs && toDs)
187                     macHeaderLengthInBytes += 6;
188         }
189
190     }
191     result = ul_send_signal_unpacked(priv, &signal, &bulkdata);
192     if(result){
193         _update_buffered_pkt_params_after_alignment(priv, &bulkdata,buffered_pkt);
194     }
195
196  /* Decrement the packet counts queued in driver */
197     if (result != -ENOSPC) {
198         /* protect entire counter updation by disabling preemption */
199         if (!priv->noOfPktQueuedInDriver) {
200             unifi_error(priv, "packets queued in driver 0 still decrementing\n");
201         } else {
202             spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
203             priv->noOfPktQueuedInDriver--;
204             spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
205         }
206         /* Sta Record is available for all unicast (except genericMgt Frames) & in other case its NULL */
207         if (staRecord) {
208             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
209             if (!staRecord->noOfPktQueued) {
210                 unifi_error(priv, "packets queued in driver per station is 0 still decrementing\n");
211             } else {
212                 staRecord->noOfPktQueued--;
213             }
214             /* if the STA alive probe frame has failed then reset the saved host tag */
215             if (result){
216                 if (staRecord->nullDataHostTag == buffered_pkt->hostTag){
217                     staRecord->nullDataHostTag = INVALID_HOST_TAG;
218                 }
219             }
220             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
221         }
222
223     }
224     return result;
225 }
226 #ifdef CSR_SUPPORT_SME
227 static
228 void set_eosp_transmit_ctrl(unifi_priv_t *priv, struct list_head *txList)
229 {
230     /* dequeue the tx data packets from the appropriate queue */
231     tx_buffered_packets_t *tx_q_item = NULL;
232     struct list_head *listHead;
233     struct list_head *placeHolder;
234     unsigned long lock_flags;
235
236
237     unifi_trace(priv, UDBG5, "entering set_eosp_transmit_ctrl\n");
238     /* check for list empty */
239     if (list_empty(txList)) {
240         unifi_warning(priv, "In set_eosp_transmit_ctrl, the list is empty\n");
241         return;
242     }
243
244     /* return the last node , and modify it. */
245
246     spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
247     list_for_each_prev_safe(listHead, placeHolder, txList) {
248         tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
249         tx_q_item->transmissionControl |= TRANSMISSION_CONTROL_EOSP_MASK;
250         tx_q_item->transmissionControl = (tx_q_item->transmissionControl & ~(CSR_NO_CONFIRM_REQUIRED));
251         unifi_trace(priv, UDBG1,
252                 "set_eosp_transmit_ctrl Transmission Control = 0x%x hostTag = 0x%x \n",tx_q_item->transmissionControl,tx_q_item->hostTag);
253         unifi_trace(priv,UDBG3,"in set_eosp_transmit_ctrl no.of buffered frames %d\n",priv->noOfPktQueuedInDriver);
254         break;
255     }
256     spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
257     unifi_trace(priv, UDBG1,"List Empty %d\n",list_empty(txList));
258     unifi_trace(priv, UDBG5, "leaving set_eosp_transmit_ctrl\n");
259     return;
260 }
261
262 static
263 void send_vif_availibility_rsp(unifi_priv_t *priv,CSR_VIF_IDENTIFIER vif,CSR_RESULT_CODE resultCode)
264 {
265     CSR_SIGNAL signal;
266     CSR_MA_VIF_AVAILABILITY_RESPONSE *rsp;
267     bulk_data_param_t *bulkdata = NULL;
268     int r;
269
270     unifi_trace(priv, UDBG3, "send_vif_availibility_rsp : invoked with resultCode = %d \n", resultCode);
271
272     memset(&signal,0,sizeof(CSR_SIGNAL));
273     rsp = &signal.u.MaVifAvailabilityResponse;
274     rsp->VirtualInterfaceIdentifier = vif;
275     rsp->ResultCode = resultCode;
276     signal.SignalPrimitiveHeader.SignalId = CSR_MA_VIF_AVAILABILITY_RESPONSE_ID;
277     signal.SignalPrimitiveHeader.ReceiverProcessId = 0;
278     signal.SignalPrimitiveHeader.SenderProcessId = priv->netdev_client->sender_id;
279
280     /* Send the signal to UniFi */
281     r = ul_send_signal_unpacked(priv, &signal, bulkdata);
282     if(r) {
283         unifi_error(priv,"Availibility response sending failed %x status %d\n",vif,r);
284     }
285     else {
286         unifi_trace(priv, UDBG3, "send_vif_availibility_rsp : status = %d \n", r);
287     }
288 }
289 #endif
290
291 static
292 void verify_and_accomodate_tx_packet(unifi_priv_t *priv)
293 {
294     tx_buffered_packets_t *tx_q_item;
295     unsigned long lock_flags;
296     struct list_head *listHead, *list;
297     struct list_head *placeHolder;
298     u8 i, j,eospFramedeleted=0;
299     u8 thresholdExcedeDueToBroadcast = TRUE;
300     /* it will be made it interface Specific in the future when multi interfaces are supported ,
301     right now interface 0 is considered */
302     netInterface_priv_t *interfacePriv = priv->interfacePriv[0];
303     CsrWifiRouterCtrlStaInfo_t *staInfo = NULL;
304
305     unifi_trace(priv, UDBG3, "entering verify_and_accomodate_tx_packet\n");
306
307     for(i = 0; i < UNIFI_MAX_CONNECTIONS; i++) {
308         staInfo = interfacePriv->staInfo[i];
309             if (staInfo && (staInfo->noOfPktQueued >= CSR_WIFI_DRIVER_MAX_PKT_QUEUING_THRESHOLD_PER_PEER)) {
310             /* remove the first(oldest) packet from the all the access catogory, since data
311              * packets for station record crossed the threshold limit (64 for AP supporting
312              * 8 peers)
313              */
314             unifi_trace(priv,UDBG3,"number of station pkts queued=  %d for sta id = %d\n", staInfo->noOfPktQueued, staInfo->aid);
315             for(j = 0; j < MAX_ACCESS_CATOGORY; j++) {
316                 list = &staInfo->dataPdu[j];
317                 spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
318                 list_for_each_safe(listHead, placeHolder, list) {
319                     tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
320                     list_del(listHead);
321                     thresholdExcedeDueToBroadcast = FALSE;
322                     unifi_net_data_free(priv, &tx_q_item->bulkdata);
323                     kfree(tx_q_item);
324                     tx_q_item = NULL;
325                     if (!priv->noOfPktQueuedInDriver) {
326                         unifi_error(priv, "packets queued in driver 0 still decrementing in %s\n", __FUNCTION__);
327                     } else {
328                         /* protection provided by spinlock */
329                         priv->noOfPktQueuedInDriver--;
330
331                     }
332                     /* Sta Record is available for all unicast (except genericMgt Frames) & in other case its NULL */
333                     if (!staInfo->noOfPktQueued) {
334                         unifi_error(priv, "packets queued in driver per station is 0 still decrementing in %s\n", __FUNCTION__);
335                     } else {
336                         spin_lock(&priv->staRecord_lock);
337                         staInfo->noOfPktQueued--;
338                         spin_unlock(&priv->staRecord_lock);
339                     }
340                     break;
341                 }
342                 spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
343             }
344         }
345     }
346     if (thresholdExcedeDueToBroadcast &&  interfacePriv->noOfbroadcastPktQueued > CSR_WIFI_DRIVER_MINIMUM_BROADCAST_PKT_THRESHOLD ) {
347         /* Remove the packets from genericMulticastOrBroadCastFrames queue
348          * (the max packets in driver is reached due to broadcast/multicast frames)
349          */
350         spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
351         list_for_each_safe(listHead, placeHolder, &interfacePriv->genericMulticastOrBroadCastFrames) {
352             tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
353             if(eospFramedeleted){
354                 tx_q_item->transmissionControl |= TRANSMISSION_CONTROL_EOSP_MASK;
355                 tx_q_item->transmissionControl = (tx_q_item->transmissionControl & ~(CSR_NO_CONFIRM_REQUIRED));
356                 unifi_trace(priv, UDBG1,"updating eosp for next packet hostTag:= 0x%x ",tx_q_item->hostTag);
357                 eospFramedeleted =0;
358                 break;
359             }
360
361             if(tx_q_item->transmissionControl & TRANSMISSION_CONTROL_EOSP_MASK ){
362                eospFramedeleted = 1;
363             }
364             unifi_trace(priv,UDBG1, "freeing of multicast packets ToC = 0x%x hostTag = 0x%x \n",tx_q_item->transmissionControl,tx_q_item->hostTag);
365             list_del(listHead);
366             unifi_net_data_free(priv, &tx_q_item->bulkdata);
367             kfree(tx_q_item);
368             priv->noOfPktQueuedInDriver--;
369             spin_lock(&priv->staRecord_lock);
370             interfacePriv->noOfbroadcastPktQueued--;
371             spin_unlock(&priv->staRecord_lock);
372             if(!eospFramedeleted){
373                 break;
374             }
375         }
376         spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
377     }
378     unifi_trace(priv, UDBG3, "leaving verify_and_accomodate_tx_packet\n");
379 }
380
381 static
382 CsrResult enque_tx_data_pdu(unifi_priv_t *priv, bulk_data_param_t *bulkdata,
383                             struct list_head *list, CSR_SIGNAL *signal,
384                             u8 requeueOnSamePos)
385 {
386
387     /* queue the tx data packets on to appropriate queue */
388     CSR_MA_PACKET_REQUEST *req = &signal->u.MaPacketRequest;
389     tx_buffered_packets_t *tx_q_item;
390     unsigned long lock_flags;
391
392     unifi_trace(priv, UDBG5, "entering enque_tx_data_pdu\n");
393     if(!list) {
394        unifi_error(priv,"List is not specified\n");
395        return CSR_RESULT_FAILURE;
396     }
397
398     /* Removes aged packets & adds the incoming packet */
399     if (priv->noOfPktQueuedInDriver >= CSR_WIFI_DRIVER_SUPPORT_FOR_MAX_PKT_QUEUEING) {
400         unifi_trace(priv,UDBG3,"number of pkts queued=  %d \n", priv->noOfPktQueuedInDriver);
401         verify_and_accomodate_tx_packet(priv);
402     }
403
404
405
406     tx_q_item = (tx_buffered_packets_t *)kmalloc(sizeof(tx_buffered_packets_t), GFP_ATOMIC);
407     if (tx_q_item == NULL) {
408         unifi_error(priv,
409                 "Failed to allocate %d bytes for tx packet record\n",
410                 sizeof(tx_buffered_packets_t));
411         func_exit();
412         return CSR_RESULT_FAILURE;
413     }
414
415     /* disable the preemption */
416     spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
417     INIT_LIST_HEAD(&tx_q_item->q);
418     /* fill the tx_q structure members */
419     tx_q_item->bulkdata.os_data_ptr = bulkdata->d[0].os_data_ptr;
420     tx_q_item->bulkdata.data_length = bulkdata->d[0].data_length;
421     tx_q_item->bulkdata.os_net_buf_ptr = bulkdata->d[0].os_net_buf_ptr;
422     tx_q_item->bulkdata.net_buf_length = bulkdata->d[0].net_buf_length;
423     tx_q_item->interfaceTag = req->VirtualInterfaceIdentifier & 0xff;
424     tx_q_item->hostTag = req->HostTag;
425     tx_q_item->leSenderProcessId = signal->SignalPrimitiveHeader.SenderProcessId;
426     tx_q_item->transmissionControl = req->TransmissionControl;
427     tx_q_item->priority = req->Priority;
428     tx_q_item->rate = req->TransmitRate;
429     memcpy(tx_q_item->peerMacAddress.a, req->Ra.x, ETH_ALEN);
430
431
432
433     if (requeueOnSamePos) {
434         list_add(&tx_q_item->q, list);
435     } else {
436         list_add_tail(&tx_q_item->q, list);
437     }
438
439     /* Count of packet queued in driver */
440     priv->noOfPktQueuedInDriver++;
441     spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
442     unifi_trace(priv, UDBG5, "leaving enque_tx_data_pdu\n");
443     return CSR_RESULT_SUCCESS;
444 }
445
446 #ifdef CSR_WIFI_REQUEUE_PACKET_TO_HAL
447 CsrResult unifi_reque_ma_packet_request (void *ospriv, u32 host_tag,
448                                          u16 txStatus, bulk_data_desc_t *bulkDataDesc)
449 {
450     CsrResult status = CSR_RESULT_SUCCESS;
451     unifi_priv_t *priv = (unifi_priv_t*)ospriv;
452     netInterface_priv_t *interfacePriv;
453     struct list_head *list = NULL;
454     CsrWifiRouterCtrlStaInfo_t *staRecord = NULL;
455     bulk_data_param_t bulkData;
456     CSR_SIGNAL signal;
457     CSR_PRIORITY priority = 0;
458     u16 interfaceTag = 0;
459     unifi_TrafficQueue priority_q;
460     u16 frameControl = 0, frameType = 0;
461     unsigned long lock_flags;
462
463     interfacePriv = priv->interfacePriv[interfaceTag];
464
465     /* If the current mode is not AP or P2PGO then just return failure
466      * to clear the hip slot
467      */
468     if(!((interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_AP) ||
469         (interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO))) {
470         return CSR_RESULT_FAILURE;
471     }
472
473     unifi_trace(priv, UDBG6, "unifi_reque_ma_packet_request: host_tag = 0x%x\n", host_tag);
474
475     staRecord = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv,
476                                                                     (((u8 *) bulkDataDesc->os_data_ptr) + 4),
477                                                                     interfaceTag);
478     if (NULL == staRecord) {
479         unifi_trace(priv, UDBG5, "unifi_reque_ma_packet_request: Invalid STA record \n");
480         return CSR_RESULT_FAILURE;
481     }
482
483     /* Update TIM if MA-PACKET.cfm fails with status as Tx-retry-limit or No-BSS and then just return failure
484      * to clear the hip slot associated with the Packet
485      */
486     if (CSR_TX_RETRY_LIMIT == txStatus || CSR_TX_NO_BSS == txStatus) {
487         if (staRecord->timSet == CSR_WIFI_TIM_RESET || staRecord->timSet == CSR_WIFI_TIM_RESETTING)
488         {
489             unifi_trace(priv, UDBG2, "unifi_reque_ma_packet_request: CFM failed with Retry Limit or No BSS-->update TIM\n");
490             if (!staRecord->timRequestPendingFlag) {
491                 update_tim(priv, staRecord->aid, 1, interfaceTag, staRecord->assignedHandle);
492             }
493             else {
494                 /* Cache the TimSet value so that it will processed immidiatly after
495                  * completing the current setTim Request
496                  */
497                 staRecord->updateTimReqQueued = 1;
498                 unifi_trace(priv, UDBG6, "unifi_reque_ma_packet_request: One more UpdateTim Request(:%d)Queued for AID %x\n",
499                                          staRecord->updateTimReqQueued, staRecord->aid);
500             }
501         }
502         return CSR_RESULT_FAILURE;
503     }
504     else if ((CSR_TX_LIFETIME == txStatus) ||  (CSR_TX_BLOCK_ACK_TIMEOUT == txStatus) ||
505              (CSR_TX_FAIL_TRANSMISSION_VIF_INTERRUPTED == txStatus) ||
506              (CSR_TX_REJECTED_PEER_STATION_SLEEPING == txStatus)    ||
507              (CSR_TX_REJECTED_DTIM_STARTED == txStatus)) {
508         /* Extract the Frame control and the frame type */
509         frameControl = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(bulkDataDesc->os_data_ptr);
510         frameType =  ((frameControl & IEEE80211_FC_TYPE_MASK) >> FRAME_CONTROL_TYPE_FIELD_OFFSET);
511
512         /* Mgmt frames will not be re-queued for Tx
513          * so just return failure to clear the hip slot
514          */
515         if (IEEE802_11_FRAMETYPE_MANAGEMENT == frameType) {
516             return CSR_RESULT_FAILURE;
517         }
518         else if (IEEE802_11_FRAMETYPE_DATA == frameType) {
519             /* QOS NULL and DATA NULL frames will not be re-queued for Tx
520              * so just return failure to clear the hip slot
521              */
522             if ((((frameControl & IEEE80211_FC_SUBTYPE_MASK) >> FRAME_CONTROL_SUBTYPE_FIELD_OFFSET) == QOS_DATA_NULL) ||
523                 (((frameControl & IEEE80211_FC_SUBTYPE_MASK) >> FRAME_CONTROL_SUBTYPE_FIELD_OFFSET)== DATA_NULL )) {
524                 return CSR_RESULT_FAILURE;
525             }
526         }
527
528         /* Extract the Packet priority */
529         if (TRUE == staRecord->wmmOrQosEnabled) {
530             u16 qosControl = 0;
531             u8  dataFrameType = 0;
532
533             dataFrameType =((frameControl & IEEE80211_FC_SUBTYPE_MASK) >> 4);
534
535             if (dataFrameType == QOS_DATA) {
536                 /* QoS control field is offset from frame control by 2 (frame control)
537                  * + 2 (duration/ID) + 2 (sequence control) + 3*ETH_ALEN or 4*ETH_ALEN
538                  */
539                 if((frameControl & IEEE802_11_FC_TO_DS_MASK) && (frameControl & IEEE802_11_FC_FROM_DS_MASK)) {
540                     qosControl= CSR_GET_UINT16_FROM_LITTLE_ENDIAN(bulkDataDesc->os_data_ptr + 30);
541                 }
542                 else {
543                     qosControl = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(bulkDataDesc->os_data_ptr + 24);
544                 }
545             }
546
547             priority = (CSR_PRIORITY)(qosControl & IEEE802_11_QC_TID_MASK);
548
549             if (priority < CSR_QOS_UP0 || priority > CSR_QOS_UP7) {
550                 unifi_trace(priv, UDBG5, "unifi_reque_ma_packet_request: Invalid priority:%x \n", priority);
551                 return CSR_RESULT_FAILURE;
552             }
553         }
554         else {
555             priority = CSR_CONTENTION;
556         }
557
558         /* Frame Bulk data to requeue it back to HAL Queues */
559         bulkData.d[0].os_data_ptr    = bulkDataDesc->os_data_ptr;
560         bulkData.d[0].data_length    = bulkDataDesc->data_length;
561         bulkData.d[0].os_net_buf_ptr = bulkDataDesc->os_net_buf_ptr;
562         bulkData.d[0].net_buf_length = bulkDataDesc->net_buf_length;
563
564         bulkData.d[1].os_data_ptr    = NULL;
565         bulkData.d[1].os_net_buf_ptr = NULL;
566         bulkData.d[1].data_length    = bulkData.d[1].net_buf_length = 0;
567
568         /* Initialize signal to zero */
569         memset(&signal, 0, sizeof(CSR_SIGNAL));
570
571         /* Frame MA Packet Req */
572         unifi_frame_ma_packet_req(priv, priority, 0, host_tag,
573                               interfaceTag, CSR_NO_CONFIRM_REQUIRED,
574                               priv->netdev_client->sender_id,
575                               staRecord->peerMacAddress.a, &signal);
576
577         /* Find the Q-Priority */
578         priority_q = unifi_frame_priority_to_queue(priority);
579         list = &staRecord->dataPdu[priority_q];
580
581         /* Place the Packet on to HAL Queue */
582         status = enque_tx_data_pdu(priv, &bulkData, list, &signal, TRUE);
583
584         /* Update the Per-station queued packet counter */
585         if (!status) {
586             spin_lock_irqsave(&priv->staRecord_lock, lock_flags);
587             staRecord->noOfPktQueued++;
588             spin_unlock_irqrestore(&priv->staRecord_lock, lock_flags);
589         }
590     }
591     else {
592         /* Packet will not be re-queued for any of the other MA Packet Tx failure
593          * reasons so just return failure to clear the hip slot
594          */
595         return CSR_RESULT_FAILURE;
596     }
597
598     return status;
599 }
600 #endif
601
602 static void is_all_ac_deliver_enabled_and_moredata(CsrWifiRouterCtrlStaInfo_t *staRecord, u8 *allDeliveryEnabled, u8 *dataAvailable)
603 {
604     u8 i;
605     *allDeliveryEnabled = TRUE;
606     for (i = 0 ;i < MAX_ACCESS_CATOGORY; i++) {
607         if (!IS_DELIVERY_ENABLED(staRecord->powersaveMode[i])) {
608             /* One is is not Delivery Enabled */
609             *allDeliveryEnabled = FALSE;
610             break;
611         }
612     }
613     if (*allDeliveryEnabled) {
614         *dataAvailable = (!list_empty(&staRecord->dataPdu[0]) || !list_empty(&staRecord->dataPdu[1])
615                           ||!list_empty(&staRecord->dataPdu[2]) ||!list_empty(&staRecord->dataPdu[3])
616                           ||!list_empty(&staRecord->mgtFrames));
617     }
618 }
619
620 /*
621  * ---------------------------------------------------------------------------
622  *  uf_handle_tim_cfm
623  *
624  *
625  *      This function updates tim status in host depending confirm status from firmware
626  *
627  *  Arguments:
628  *      priv            Pointer to device private context struct
629  *      cfm             CSR_MLME_SET_TIM_CONFIRM
630  *      receiverProcessId SenderProcessID to fetch handle & timSet status
631  *
632  * ---------------------------------------------------------------------------
633  */
634 void uf_handle_tim_cfm(unifi_priv_t *priv, CSR_MLME_SET_TIM_CONFIRM *cfm, u16 receiverProcessId)
635 {
636     u8 handle = CSR_WIFI_GET_STATION_HANDLE_FROM_RECEIVER_ID(receiverProcessId);
637     u8 timSetStatus = CSR_WIFI_GET_TIMSET_STATE_FROM_RECEIVER_ID(receiverProcessId);
638     u16 interfaceTag = (cfm->VirtualInterfaceIdentifier & 0xff);
639     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
640     CsrWifiRouterCtrlStaInfo_t *staRecord = NULL;
641     /* This variable holds what TIM value we wanted to set in firmware */
642     u16 timSetValue = 0;
643     /* Irrespective of interface the count maintained */
644     static u8 retryCount = 0;
645     unsigned long lock_flags;
646     unifi_trace(priv, UDBG3, "entering %s, handle = %x, timSetStatus = %x\n", __FUNCTION__, handle, timSetStatus);
647
648     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
649         unifi_warning(priv, "bad interfaceTag = %x\n", interfaceTag);
650         return;
651     }
652
653     if ((handle != CSR_WIFI_BROADCAST_OR_MULTICAST_HANDLE) && (handle >= UNIFI_MAX_CONNECTIONS)) {
654         unifi_warning(priv, "bad station Handle = %x\n", handle);
655         return;
656     }
657
658     if (handle != CSR_WIFI_BROADCAST_OR_MULTICAST_HANDLE) {
659         spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
660         if ((staRecord = ((CsrWifiRouterCtrlStaInfo_t *) (interfacePriv->staInfo[handle]))) == NULL) {
661             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
662             unifi_warning(priv, "uf_handle_tim_cfm: station record is NULL  handle = %x\n", handle);
663             return;
664         }
665        spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
666     }
667     switch(timSetStatus)
668     {
669         case CSR_WIFI_TIM_SETTING:
670             timSetValue = CSR_WIFI_TIM_SET;
671             break;
672         case CSR_WIFI_TIM_RESETTING:
673             timSetValue = CSR_WIFI_TIM_RESET;
674             break;
675         default:
676             unifi_warning(priv, "timSet state is %x: Debug\n", timSetStatus);
677             return;
678     }
679
680     /* check TIM confirm for success/failures */
681     switch(cfm->ResultCode)
682     {
683         case CSR_RC_SUCCESS:
684             if (handle != CSR_WIFI_BROADCAST_OR_MULTICAST_HANDLE) {
685                 /* Unicast frame & station record available */
686                 if (timSetStatus == staRecord->timSet) {
687                     staRecord->timSet = timSetValue;
688                     /* fh_cmd_q can also be full at some point of time!,
689                      * resetting count as queue is cleaned by firmware at this point
690                      */
691                     retryCount = 0;
692                     unifi_trace(priv, UDBG2, "tim (%s) successfully in firmware\n", (timSetValue)?"SET":"RESET");
693                 } else {
694                     unifi_trace(priv, UDBG3, "receiver processID = %x, success: request & confirm states are not matching in TIM cfm: Debug status = %x, staRecord->timSet = %x, handle = %x\n",
695                                  receiverProcessId, timSetStatus, staRecord->timSet, handle);
696                 }
697
698                 /* Reset TIM pending flag to send next TIM request */
699                 staRecord->timRequestPendingFlag = FALSE;
700
701                 /* Make sure that one more UpdateTim request is queued, if Queued its value
702                  * should be CSR_WIFI_TIM_SET or CSR_WIFI_TIM_RESET
703                  */
704                 if (0xFF != staRecord->updateTimReqQueued)
705                 {
706                     /* Process the UpdateTim Request which is queued while previous UpdateTim was in progress */
707                     if (staRecord->timSet != staRecord->updateTimReqQueued)
708                     {
709                        unifi_trace(priv, UDBG2, "uf_handle_tim_cfm : Processing Queued UpdateTimReq \n");
710
711                        update_tim(priv, staRecord->aid, staRecord->updateTimReqQueued, interfaceTag, handle);
712
713                        staRecord->updateTimReqQueued = 0xFF;
714                     }
715                 }
716             } else {
717
718                 interfacePriv->bcTimSet = timSetValue;
719                 /* fh_cmd_q can also be full at some point of time!,
720                  * resetting count as queue is cleaned by firmware at this point
721                  */
722                 retryCount = 0;
723                 unifi_trace(priv, UDBG3, "tim (%s) successfully for broadcast frame in firmware\n", (timSetValue)?"SET":"RESET");
724
725                 /* Reset DTIM pending flag to send next DTIM request */
726                 interfacePriv->bcTimSetReqPendingFlag = FALSE;
727
728                 /* Make sure that one more UpdateDTim request is queued, if Queued its value
729                  * should be CSR_WIFI_TIM_SET or CSR_WIFI_TIM_RESET
730                  */
731                 if (0xFF != interfacePriv->bcTimSetReqQueued)
732                 {
733                     /* Process the UpdateTim Request which is queued while previous UpdateTim was in progress */
734                     if (interfacePriv->bcTimSet != interfacePriv->bcTimSetReqQueued)
735                     {
736                         unifi_trace(priv, UDBG2, "uf_handle_tim_cfm : Processing Queued UpdateDTimReq \n");
737
738                         update_tim(priv, 0, interfacePriv->bcTimSetReqQueued, interfaceTag, 0xFFFFFFFF);
739
740                         interfacePriv->bcTimSetReqQueued = 0xFF;
741                     }
742                 }
743
744             }
745             break;
746         case CSR_RC_INVALID_PARAMETERS:
747         case CSR_RC_INSUFFICIENT_RESOURCE:
748             /* check for max retry limit & send again
749              * MAX_RETRY_LIMIT is not maintained for each set of transactions..Its generic
750              * If failure crosses this Limit, we have to take a call to FIX
751              */
752             if (retryCount > UNIFI_MAX_RETRY_LIMIT) {
753                 u8 moreData = FALSE;
754                 retryCount = 0;
755                 /* Because of continuos traffic in fh_cmd_q the tim set request is failing (exceeding retry limit)
756                  * but if we didn't synchronize our timSet varible state with firmware then it can cause below issues
757                  * cond 1. We want to SET tim in firmware if its fails & max retry limit reached
758                  *   -> If host set's the timSet to 1, we wont try to send(as max retry reached) update tim but
759                  *   firmware is not updated with queue(TIM) status so it wont set TIM in beacon finally host start piling
760                  *    up data & wont try to set tim in firmware (This can cause worser performance)
761                  * cond 2. We want to reset tim in firmware it fails & reaches max retry limit
762                  *   -> If host sets the timSet to Zero, it wont try to set a TIM request unless we wont have any packets
763                  *   to be queued, so beacon unnecessarily advertizes the TIM
764                  */
765
766                 if(staRecord) {
767                     if(!staRecord->wmmOrQosEnabled) {
768                         moreData = (!list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]) ||
769                                 !list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_VO]) ||
770                                 !list_empty(&staRecord->mgtFrames));
771                     } else {
772                         /* Peer is QSTA */
773                         u8 allDeliveryEnabled = 0, dataAvailable = 0;
774                         /* Check if all AC's are Delivery Enabled */
775                         is_all_ac_deliver_enabled_and_moredata(staRecord, &allDeliveryEnabled, &dataAvailable);
776                         /*check for more data in non-delivery enabled queues*/
777                         moreData = (uf_is_more_data_for_non_delivery_ac(staRecord) || (allDeliveryEnabled && dataAvailable));
778
779                     }
780                     /* To avoid cond 1 & 2, check internal Queues status, if we have more Data then set RESET the timSet(0),
781                      *  so we are trying to be in sync with firmware & next packets before queuing atleast try to
782                      *  set TIM in firmware otherwise it SET timSet(1)
783                      */
784                     if (moreData) {
785                         staRecord->timSet = CSR_WIFI_TIM_RESET;
786                     } else {
787                         staRecord->timSet = CSR_WIFI_TIM_SET;
788                     }
789                 } else {
790                     /* Its a broadcast frames */
791                     moreData = (!list_empty(&interfacePriv->genericMulticastOrBroadCastMgtFrames) ||
792                                !list_empty(&interfacePriv->genericMulticastOrBroadCastFrames));
793                     if (moreData) {
794                         update_tim(priv, 0, CSR_WIFI_TIM_SET, interfaceTag, 0xFFFFFFFF);
795                     } else {
796                         update_tim(priv, 0, CSR_WIFI_TIM_RESET, interfaceTag, 0xFFFFFFFF);
797                     }
798                 }
799
800                 unifi_error(priv, "no of error's for TIM setting crossed the Limit: verify\n");
801                 return;
802             }
803             retryCount++;
804
805             if (handle != CSR_WIFI_BROADCAST_OR_MULTICAST_HANDLE) {
806                 if (timSetStatus == staRecord->timSet) {
807                     unifi_warning(priv, "tim request failed, retry for AID = %x\n", staRecord->aid);
808                     update_tim(priv, staRecord->aid, timSetValue, interfaceTag, handle);
809                 } else {
810                     unifi_trace(priv, UDBG1, "failure: request & confirm states are not matching in TIM cfm: Debug status = %x, staRecord->timSet = %x\n",
811                                   timSetStatus, staRecord->timSet);
812                 }
813             } else {
814                 unifi_warning(priv, "tim request failed, retry for broadcast frames\n");
815                 update_tim(priv, 0, timSetValue, interfaceTag, 0xFFFFFFFF);
816             }
817             break;
818         default:
819             unifi_warning(priv, "tim update request failed resultcode = %x\n", cfm->ResultCode);
820     }
821
822     unifi_trace(priv, UDBG2, "leaving %s\n", __FUNCTION__);
823 }
824
825 /*
826  * ---------------------------------------------------------------------------
827  *  update_tim
828  *
829  *
830  *      This function updates tim status in firmware for AID[1 to UNIFI_MAX_CONNECTIONS] or
831  *       AID[0] for broadcast/multicast packets.
832  *
833  *      NOTE: The LSB (least significant BYTE) of senderId while sending this MLME premitive
834  *       has been modified(utilized) as below
835  *
836  *       SenderID in signal's SignalPrimitiveHeader is 2 byte the lowe byte bitmap is below
837  *
838  *       station handle(6 bits)      timSet Status (2 bits)
839  *       ---------------------       ----------------------
840  *       0  0  0  0  0  0        |       0  0
841  *
842  * timSet Status can be one of below:
843  *
844  * CSR_WIFI_TIM_RESET
845  * CSR_WIFI_TIM_RESETTING
846  * CSR_WIFI_TIM_SET
847  * CSR_WIFI_TIM_SETTING
848  *
849  *  Arguments:
850  *      priv            Pointer to device private context struct
851  *      aid             can be 1 t0 UNIFI_MAX_CONNECTIONS & 0 means multicast/broadcast
852  *      setTim          value SET(1) / RESET(0)
853  *      interfaceTag    the interfaceID on which activity going on
854  *      handle          from  (0 <= handle < UNIFI_MAX_CONNECTIONS)
855  *
856  * ---------------------------------------------------------------------------
857  */
858 void update_tim(unifi_priv_t * priv, u16 aid, u8 setTim, u16 interfaceTag, u32 handle)
859 {
860     CSR_SIGNAL signal;
861     s32 r;
862     CSR_MLME_SET_TIM_REQUEST *req = &signal.u.MlmeSetTimRequest;
863     bulk_data_param_t *bulkdata = NULL;
864     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
865     u8 senderIdLsb = 0;
866     CsrWifiRouterCtrlStaInfo_t *staRecord = NULL;
867     u32 oldTimSetStatus = 0, timSetStatus = 0;
868
869     unifi_trace(priv, UDBG5, "entering the update_tim routine\n");
870
871
872     if (handle == 0xFFFFFFFF) {
873         handle &= CSR_WIFI_BROADCAST_OR_MULTICAST_HANDLE;
874         if (setTim == interfacePriv->bcTimSet)
875         {
876             unifi_trace(priv, UDBG3, "update_tim, Drop:Hdl=%x, timval=%d, globalTim=%d\n", handle, setTim, interfacePriv->bcTimSet);
877             return;
878         }
879     } else if ((handle != 0xFFFFFFFF) && (handle >= UNIFI_MAX_CONNECTIONS)) {
880         unifi_warning(priv, "bad station Handle = %x\n", handle);
881         return;
882     }
883
884     if (setTim) {
885         timSetStatus =  CSR_WIFI_TIM_SETTING;
886     } else {
887         timSetStatus =  CSR_WIFI_TIM_RESETTING;
888     }
889
890     if (handle != CSR_WIFI_BROADCAST_OR_MULTICAST_HANDLE) {
891         if ((staRecord = ((CsrWifiRouterCtrlStaInfo_t *) (interfacePriv->staInfo[handle]))) == NULL) {
892             unifi_warning(priv, "station record is NULL in  update_tim: handle = %x :debug\n", handle);
893             return;
894         }
895         /* In case of signal sending failed, revert back to old state */
896         oldTimSetStatus = staRecord->timSet;
897         staRecord->timSet = timSetStatus;
898     }
899
900     /* pack senderID LSB */
901     senderIdLsb = CSR_WIFI_PACK_SENDER_ID_LSB_FOR_TIM_REQ(handle,  timSetStatus);
902
903     /* initialize signal to zero */
904     memset(&signal, 0, sizeof(CSR_SIGNAL));
905
906     /* Frame the MLME-SET-TIM request */
907     signal.SignalPrimitiveHeader.SignalId = CSR_MLME_SET_TIM_REQUEST_ID;
908     signal.SignalPrimitiveHeader.ReceiverProcessId = 0;
909     CSR_COPY_UINT16_TO_LITTLE_ENDIAN(((priv->netdev_client->sender_id & 0xff00) | senderIdLsb),
910                    (u8*)&signal.SignalPrimitiveHeader.SenderProcessId);
911
912     /* set The virtual interfaceIdentifier, aid, tim value */
913     req->VirtualInterfaceIdentifier = uf_get_vif_identifier(interfacePriv->interfaceMode,interfaceTag);
914     req->AssociationId = aid;
915     req->TimValue = setTim;
916
917
918     unifi_trace(priv, UDBG2, "update_tim:AID %x,senderIdLsb = 0x%x, handle = 0x%x, timSetStatus = %x, sender proceesID = %x \n",
919                 aid,senderIdLsb, handle, timSetStatus, signal.SignalPrimitiveHeader.SenderProcessId);
920
921     /* Send the signal to UniFi */
922     r = ul_send_signal_unpacked(priv, &signal, bulkdata);
923     if (r) {
924         /* No need to free bulk data, as TIM request doesn't carries any data */
925         unifi_error(priv, "Error queueing CSR_MLME_SET_TIM_REQUEST signal\n");
926         if (staRecord) {
927             staRecord->timSet = oldTimSetStatus ;
928         }
929         else
930         {
931             /* MLME_SET_TIM.req sending failed here for AID0, so revert back our bcTimSet status */
932             interfacePriv->bcTimSet = !setTim;
933         }
934     }
935     else {
936         /* Update tim request pending flag and ensure no more TIM set requests are send
937            for the same station until TIM confirm is received */
938         if (staRecord) {
939             staRecord->timRequestPendingFlag = TRUE;
940         }
941         else
942         {
943             /* Update tim request (for AID 0) pending flag and ensure no more DTIM set requests are send
944              * for the same station until TIM confirm is received
945              */
946             interfacePriv->bcTimSetReqPendingFlag = TRUE;
947         }
948     }
949     unifi_trace(priv, UDBG5, "leaving the update_tim routine\n");
950 }
951
952 static
953 void process_peer_active_transition(unifi_priv_t * priv,
954                                     CsrWifiRouterCtrlStaInfo_t *staRecord,
955                                     u16 interfaceTag)
956 {
957     int r,i;
958     u8 spaceAvail[4] = {TRUE,TRUE,TRUE,TRUE};
959     tx_buffered_packets_t * buffered_pkt = NULL;
960     unsigned long lock_flags;
961     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
962
963     unifi_trace(priv, UDBG5, "entering process_peer_active_transition\n");
964
965     if(IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag)) {
966         /* giving more priority to multicast packets so delaying unicast packets*/
967         unifi_trace(priv,UDBG2, "Multicast transmission is going on so resume unicast transmission after DTIM over\n");
968
969         /* As station is active now, even though AP is not able to send frames to it
970          * because of DTIM, it needs to reset the TIM here
971          */
972         if (!staRecord->timRequestPendingFlag){
973             if((staRecord->timSet == CSR_WIFI_TIM_SET) || (staRecord->timSet == CSR_WIFI_TIM_SETTING)){
974                 update_tim(priv, staRecord->aid, 0, interfaceTag, staRecord->assignedHandle);
975             }
976         }
977         else
978         {
979             /* Cache the TimSet value so that it will processed immidiatly after
980              * completing the current setTim Request
981              */
982             staRecord->updateTimReqQueued = 0;
983             unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
984                         staRecord->aid);
985         }
986         return;
987     }
988     while((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->mgtFrames))) {
989         buffered_pkt->transmissionControl &=
990                      ~(TRANSMISSION_CONTROL_TRIGGER_MASK|TRANSMISSION_CONTROL_EOSP_MASK);
991         if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,0,FALSE)) == -ENOSPC) {
992             unifi_trace(priv, UDBG2, "p_p_a_t:(ENOSPC) Mgt Frame queueing \n");
993             /* Enqueue at the head of the queue */
994             spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
995             list_add(&buffered_pkt->q, &staRecord->mgtFrames);
996             spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
997             priv->pausedStaHandle[3]=(u8)(staRecord->assignedHandle);
998             spaceAvail[3] = FALSE;
999             break;
1000         } else {
1001             if(r){
1002                 unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
1003                 /* the PDU failed where we can't do any thing so free the storage */
1004                 unifi_net_data_free(priv, &buffered_pkt->bulkdata);
1005             }
1006             kfree(buffered_pkt);
1007         }
1008     }
1009     if (!staRecord->timRequestPendingFlag) {
1010         if (staRecord->txSuspend) {
1011             if(staRecord->timSet == CSR_WIFI_TIM_SET) {
1012                 update_tim(priv,staRecord->aid,0,interfaceTag, staRecord->assignedHandle);
1013             }
1014             return;
1015         }
1016     }
1017     else
1018     {
1019         /* Cache the TimSet value so that it will processed immidiatly after
1020          * completing the current setTim Request
1021          */
1022         staRecord->updateTimReqQueued = 0;
1023         unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
1024                     staRecord->aid);
1025     }
1026     for(i=3;i>=0;i--) {
1027         if(!spaceAvail[i])
1028             continue;
1029         unifi_trace(priv, UDBG6, "p_p_a_t:data pkt sending for AC %d \n",i);
1030         while((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->dataPdu[i]))) {
1031            buffered_pkt->transmissionControl &=
1032                       ~(TRANSMISSION_CONTROL_TRIGGER_MASK|TRANSMISSION_CONTROL_EOSP_MASK);
1033            if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,0,FALSE)) == -ENOSPC) {
1034                /* Clear the trigger bit transmission control*/
1035                /* Enqueue at the head of the queue */
1036                spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
1037                list_add(&buffered_pkt->q, &staRecord->dataPdu[i]);
1038                spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
1039                priv->pausedStaHandle[i]=(u8)(staRecord->assignedHandle);
1040                break;
1041            } else {
1042               if(r){
1043                   unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
1044                   /* the PDU failed where we can't do any thing so free the storage */
1045                   unifi_net_data_free(priv, &buffered_pkt->bulkdata);
1046                }
1047               kfree(buffered_pkt);
1048            }
1049         }
1050     }
1051     if (!staRecord->timRequestPendingFlag){
1052         if((staRecord->timSet  == CSR_WIFI_TIM_SET) || (staRecord->timSet  == CSR_WIFI_TIM_SETTING)) {
1053             unifi_trace(priv, UDBG3, "p_p_a_t:resetting tim .....\n");
1054             update_tim(priv,staRecord->aid,0,interfaceTag, staRecord->assignedHandle);
1055         }
1056     }
1057     else
1058     {
1059         /* Cache the TimSet value so that it will processed immidiatly after
1060          * completing the current setTim Request
1061          */
1062         staRecord->updateTimReqQueued = 0;
1063         unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
1064                     staRecord->aid);
1065     }
1066     unifi_trace(priv, UDBG5, "leaving process_peer_active_transition\n");
1067 }
1068
1069
1070
1071 void uf_process_ma_pkt_cfm_for_ap(unifi_priv_t *priv,u16 interfaceTag, const CSR_MA_PACKET_CONFIRM *pkt_cfm)
1072 {
1073     netInterface_priv_t *interfacePriv;
1074     u8 i;
1075     CsrWifiRouterCtrlStaInfo_t *staRecord = NULL;
1076     interfacePriv = priv->interfacePriv[interfaceTag];
1077
1078
1079     if(pkt_cfm->HostTag == interfacePriv->multicastPduHostTag) {
1080          unifi_trace(priv,UDBG2,"CFM for marked Multicast Tag = %x\n",interfacePriv->multicastPduHostTag);
1081          interfacePriv->multicastPduHostTag = 0xffffffff;
1082          resume_suspended_uapsd(priv,interfaceTag);
1083          resume_unicast_buffered_frames(priv,interfaceTag);
1084          if(list_empty(&interfacePriv->genericMulticastOrBroadCastMgtFrames) &&
1085               list_empty(&interfacePriv->genericMulticastOrBroadCastFrames)) {
1086             unifi_trace(priv,UDBG1,"Resetting multicastTIM");
1087             if (!interfacePriv->bcTimSetReqPendingFlag)
1088             {
1089                 update_tim(priv,0,CSR_WIFI_TIM_RESET,interfaceTag, 0xFFFFFFFF);
1090             }
1091             else
1092             {
1093                 /* Cache the DTimSet value so that it will processed immidiatly after
1094                  * completing the current setDTim Request
1095                  */
1096                  interfacePriv->bcTimSetReqQueued = CSR_WIFI_TIM_RESET;
1097                  unifi_trace(priv, UDBG2, "uf_process_ma_pkt_cfm_for_ap : One more UpdateDTim Request(%d) Queued \n",
1098                              interfacePriv->bcTimSetReqQueued);
1099             }
1100
1101         }
1102         return;
1103     }
1104
1105     /* Check if it is a Confirm for null data frame used
1106      * for probing station activity
1107      */
1108     for(i =0; i < UNIFI_MAX_CONNECTIONS; i++) {
1109         staRecord = (CsrWifiRouterCtrlStaInfo_t *) (interfacePriv->staInfo[i]);
1110         if (staRecord && (staRecord->nullDataHostTag == pkt_cfm->HostTag)) {
1111
1112             unifi_trace(priv, UDBG1, "CFM for Inactive probe Null frame (tag = %x, status = %d)\n",
1113                                     pkt_cfm->HostTag,
1114                                     pkt_cfm->TransmissionStatus
1115                                     );
1116             staRecord->nullDataHostTag = INVALID_HOST_TAG;
1117
1118             if(pkt_cfm->TransmissionStatus == CSR_TX_RETRY_LIMIT){
1119                 u32 now;
1120                 u32 inactive_time;
1121
1122                 unifi_trace(priv, UDBG1, "Nulldata to probe STA ALIVE Failed with retry limit\n");
1123                 /* Recheck if there is some activity after null data is sent.
1124                 *
1125                 * If still there is no activity then send a disconnected indication
1126                 * to SME to delete the station record.
1127                 */
1128                 if (staRecord->activity_flag){
1129                     return;
1130                 }
1131                 now = CsrTimeGet(NULL);
1132
1133                 if (staRecord->lastActivity > now)
1134                 {
1135                     /* simple timer wrap (for 1 wrap) */
1136                     inactive_time = CsrTimeAdd((u32)CsrTimeSub(CSR_SCHED_TIME_MAX, staRecord->lastActivity),
1137                                                now);
1138                 }
1139                 else
1140                 {
1141                     inactive_time = (u32)CsrTimeSub(now, staRecord->lastActivity);
1142                 }
1143
1144                 if (inactive_time >= STA_INACTIVE_TIMEOUT_VAL)
1145                 {
1146                     struct list_head send_cfm_list;
1147                     u8 j;
1148
1149                     /* The SME/NME may be waiting for confirmation for requested frames to this station.
1150                      * Though this is --VERY UNLIKELY-- in case of station in active mode. But still as a
1151                      * a defensive check, it loops through buffered frames for this station and if confirmation
1152                      * is requested, send auto confirmation with failure status. Also flush the frames so
1153                      * that these are not processed again in PEER_DEL_REQ handler.
1154                      */
1155                     INIT_LIST_HEAD(&send_cfm_list);
1156
1157                     uf_prepare_send_cfm_list_for_queued_pkts(priv,
1158                                                              &send_cfm_list,
1159                                                              &(staRecord->mgtFrames));
1160
1161                     uf_flush_list(priv, &(staRecord->mgtFrames));
1162
1163                     for(j = 0; j < MAX_ACCESS_CATOGORY; j++){
1164                         uf_prepare_send_cfm_list_for_queued_pkts(priv,
1165                                                                  &send_cfm_list,
1166                                                                  &(staRecord->dataPdu[j]));
1167
1168                         uf_flush_list(priv,&(staRecord->dataPdu[j]));
1169                     }
1170
1171                     send_auto_ma_packet_confirm(priv, staRecord->interfacePriv, &send_cfm_list);
1172
1173
1174
1175                     unifi_warning(priv, "uf_process_ma_pkt_cfm_for_ap: Router Disconnected IND Peer (%x-%x-%x-%x-%x-%x)\n",
1176                                              staRecord->peerMacAddress.a[0],
1177                                              staRecord->peerMacAddress.a[1],
1178                                              staRecord->peerMacAddress.a[2],
1179                                              staRecord->peerMacAddress.a[3],
1180                                              staRecord->peerMacAddress.a[4],
1181                                              staRecord->peerMacAddress.a[5]);
1182
1183                     CsrWifiRouterCtrlConnectedIndSend(priv->CSR_WIFI_SME_IFACEQUEUE,
1184                                                       0,
1185                                                       staRecord->interfacePriv->InterfaceTag,
1186                                                       staRecord->peerMacAddress,
1187                                                       CSR_WIFI_ROUTER_CTRL_PEER_DISCONNECTED);
1188                 }
1189
1190             }
1191             else if (pkt_cfm->TransmissionStatus == CSR_TX_SUCCESSFUL)
1192             {
1193                  staRecord->activity_flag = TRUE;
1194             }
1195         }
1196     }
1197 }
1198
1199 #endif
1200 u16 uf_get_vif_identifier (CsrWifiRouterCtrlMode mode, u16 tag)
1201 {
1202     switch(mode)
1203     {
1204         case CSR_WIFI_ROUTER_CTRL_MODE_STA:
1205         case CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI:
1206             return (0x02<<8|tag);
1207
1208         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
1209         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
1210             return (0x03<<8|tag);
1211
1212         case CSR_WIFI_ROUTER_CTRL_MODE_IBSS:
1213             return (0x01<<8|tag);
1214
1215         case CSR_WIFI_ROUTER_CTRL_MODE_MONITOR:
1216             return (0x04<<8|tag);
1217         case CSR_WIFI_ROUTER_CTRL_MODE_AMP:
1218             return (0x05<<8|tag);
1219         default:
1220             return tag;
1221     }
1222 }
1223
1224 #ifdef CSR_SUPPORT_SME
1225
1226 /*
1227  * ---------------------------------------------------------------------------
1228  *  update_macheader
1229  *
1230  *
1231  *      These functions updates mac header for intra BSS packet
1232  *      routing.
1233  *      NOTE: This function always has to be called in rx context which
1234  *      is in bh thread context since GFP_KERNEL is used. In soft IRQ/ Interrupt
1235  *      context shouldn't be used
1236  *
1237  *  Arguments:
1238  *      priv            Pointer to device private context struct
1239  *      skb             Socket buffer containing data packet to transmit
1240  *      newSkb          Socket buffer containing data packet + Mac header if no sufficient headroom in skb
1241  *      priority        to append QOS control header in Mac header
1242  *      bulkdata        if newSkb allocated then bulkdata updated to send to unifi
1243  *      interfaceTag    the interfaceID on which activity going on
1244  *      macHeaderLengthInBytes no. of bytes of mac header in received frame
1245  *      qosDestination  used to append Qos control field
1246  *
1247  *  Returns:
1248  *      Zero on success or -1 on error.
1249  * ---------------------------------------------------------------------------
1250  */
1251
1252 static int update_macheader(unifi_priv_t *priv, struct sk_buff *skb,
1253                             struct sk_buff *newSkb, CSR_PRIORITY *priority,
1254                             bulk_data_param_t *bulkdata, u16 interfaceTag,
1255                             u8 macHeaderLengthInBytes,
1256                             u8 qosDestination)
1257 {
1258
1259     u16 *fc = NULL;
1260     u8 direction = 0, toDs, fromDs;
1261     u8 *bufPtr = NULL;
1262     u8 sa[ETH_ALEN], da[ETH_ALEN];
1263     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
1264     int headroom;
1265     u8 macHeaderBuf[IEEE802_11_DATA_FRAME_MAC_HEADER_SIZE] = {0};
1266
1267     unifi_trace(priv, UDBG5, "entering the update_macheader function\n");
1268
1269     /* temporary buffer for the Mac header storage */
1270     memcpy(macHeaderBuf, skb->data, macHeaderLengthInBytes);
1271
1272     /* remove the Macheader from the skb */
1273     skb_pull(skb, macHeaderLengthInBytes);
1274
1275     /* get the skb headroom for skb_push check */
1276     headroom = skb_headroom(skb);
1277
1278     /*  pointer to frame control field */
1279     fc = (u16*) macHeaderBuf;
1280
1281     toDs = (*fc & cpu_to_le16(IEEE802_11_FC_TO_DS_MASK))?1 : 0;
1282     fromDs = (*fc & cpu_to_le16(IEEE802_11_FC_FROM_DS_MASK))? 1: 0;
1283     unifi_trace(priv, UDBG5, "In update_macheader function, fromDs = %x, toDs = %x\n", fromDs, toDs);
1284     direction = ((fromDs | (toDs << 1)) & 0x3);
1285
1286     /* Address1 or 3 from the macheader */
1287     memcpy(da, macHeaderBuf+4+toDs*12, ETH_ALEN);
1288     /* Address2, 3 or 4 from the mac header */
1289     memcpy(sa, macHeaderBuf+10+fromDs*(6+toDs*8), ETH_ALEN);
1290
1291     unifi_trace(priv, UDBG3, "update_macheader:direction = %x\n", direction);
1292     /* update the toDs, fromDs & address fields in Mac header */
1293     switch(direction)
1294     {
1295         case 2:
1296             /* toDs = 1 & fromDs = 0 , toAp when frames received from peer
1297              * while sending this packet to Destination the Mac header changed
1298              * as fromDs = 1 & toDs = 0, fromAp
1299              */
1300             *fc &= cpu_to_le16(~IEEE802_11_FC_TO_DS_MASK);
1301             *fc |= cpu_to_le16(IEEE802_11_FC_FROM_DS_MASK);
1302             /* Address1: MAC address of the actual destination (4 = 2+2) */
1303             memcpy(macHeaderBuf + 4, da, ETH_ALEN);
1304             /* Address2: The MAC address of the AP (10 = 2+2+6) */
1305             memcpy(macHeaderBuf + 10, &interfacePriv->bssid, ETH_ALEN);
1306             /* Address3: MAC address of the actual source from mac header (16 = 2+2+6+6) */
1307             memcpy(macHeaderBuf + 16, sa, ETH_ALEN);
1308             break;
1309         case 3:
1310             unifi_trace(priv, UDBG3, "when both the toDs & fromDS set, NOT SUPPORTED\n");
1311             break;
1312         default:
1313             unifi_trace(priv, UDBG3, "problem in decoding packet in update_macheader \n");
1314             return -1;
1315     }
1316
1317     /* frameType is Data always, Validation is done before calling this function */
1318
1319     /* check for the souce station type */
1320     switch(le16_to_cpu(*fc) & IEEE80211_FC_SUBTYPE_MASK)
1321     {
1322         case IEEE802_11_FC_TYPE_QOS_DATA & IEEE80211_FC_SUBTYPE_MASK:
1323             /* No need to modify the qos control field */
1324             if (!qosDestination) {
1325
1326                 /* If source Sta is QOS enabled & if this bit set, then HTC is supported by
1327                  * peer station & htc field present in macHeader
1328                  */
1329                 if (*fc & cpu_to_le16(IEEE80211_FC_ORDER_MASK)) {
1330                     /* HT control field present in Mac header
1331                      * 6 = sizeof(qosControl) + sizeof(htc)
1332                      */
1333                     macHeaderLengthInBytes -= 6;
1334                 } else {
1335                     macHeaderLengthInBytes -= 2;
1336                 }
1337                 /* Destination STA is non qos so change subtype to DATA */
1338                 *fc &= cpu_to_le16(~IEEE80211_FC_SUBTYPE_MASK);
1339                 *fc |= cpu_to_le16(IEEE802_11_FC_TYPE_DATA);
1340                 /* remove the qos control field & HTC(if present). new macHeaderLengthInBytes is less than old
1341                  * macHeaderLengthInBytes so no need to verify skb headroom
1342                  */
1343                 if (headroom < macHeaderLengthInBytes) {
1344                     unifi_trace(priv, UDBG1, " sufficient headroom not there to push updated mac header \n");
1345                     return -1;
1346                 }
1347                 bufPtr = (u8 *) skb_push(skb, macHeaderLengthInBytes);
1348
1349                 /*  update bulk data os_data_ptr */
1350                 bulkdata->d[0].os_data_ptr = skb->data;
1351                 bulkdata->d[0].os_net_buf_ptr = (unsigned char*)skb;
1352                 bulkdata->d[0].data_length = skb->len;
1353
1354             } else {
1355                 /* pointing to QOS control field */
1356                 u8 qc;
1357                 if (*fc & cpu_to_le16(IEEE80211_FC_ORDER_MASK)) {
1358                     qc = *((u8*)(macHeaderBuf + (macHeaderLengthInBytes - 4 - 2)));
1359                 } else {
1360                     qc = *((u8*)(macHeaderBuf + (macHeaderLengthInBytes - 2)));
1361                 }
1362
1363                 if ((qc & IEEE802_11_QC_TID_MASK) > 7) {
1364                     *priority = 7;
1365                 } else {
1366                     *priority = qc & IEEE802_11_QC_TID_MASK;
1367                 }
1368
1369                 unifi_trace(priv, UDBG1, "Incoming packet priority from QSTA is %x\n", *priority);
1370
1371                 if (headroom < macHeaderLengthInBytes) {
1372                     unifi_trace(priv, UDBG3, " sufficient headroom not there to push updated mac header \n");
1373                     return -1;
1374                 }
1375                 bufPtr = (u8 *) skb_push(skb, macHeaderLengthInBytes);
1376             }
1377             break;
1378         default:
1379             {
1380                 bulk_data_param_t data_ptrs;
1381                 CsrResult csrResult;
1382                 unifi_trace(priv, UDBG5, "normal Data packet, NO QOS \n");
1383
1384                 if (qosDestination) {
1385                     u8 qc = 0;
1386                     unifi_trace(priv, UDBG3, "destination is QOS station \n");
1387
1388                     /* Set Ma-Packet.req UP to UP0 */
1389                     *priority = CSR_QOS_UP0;
1390
1391                     /* prepare the qos control field */
1392                     qc |= CSR_QOS_UP0;
1393                     /* no Amsdu is in ap buffer so eosp is left 0 */
1394                     if (da[0] & 0x1) {
1395                         /* multicast/broadcast frames, no acknowledgement needed */
1396                         qc |= 1 << 5;
1397                     }
1398
1399                     /* update new Mac header Length with 2 = sizeof(qos control) */
1400                     macHeaderLengthInBytes += 2;
1401
1402                     /* received DATA frame but destiantion is QOS station so update subtype to QOS*/
1403                     *fc &= cpu_to_le16(~IEEE80211_FC_SUBTYPE_MASK);
1404                     *fc |= cpu_to_le16(IEEE802_11_FC_TYPE_QOS_DATA);
1405
1406                     /* appendQosControlOffset = macHeaderLengthInBytes - 2, since source sta is not QOS */
1407                     macHeaderBuf[macHeaderLengthInBytes - 2] = qc;
1408                     /* txopLimit is 0 */
1409                     macHeaderBuf[macHeaderLengthInBytes - 1] = 0;
1410                     if (headroom < macHeaderLengthInBytes) {
1411                         csrResult = unifi_net_data_malloc(priv, &data_ptrs.d[0], skb->len + macHeaderLengthInBytes);
1412
1413                         if (csrResult != CSR_RESULT_SUCCESS) {
1414                             unifi_error(priv, " failed to allocate request_data. in update_macheader func\n");
1415                             return -1;
1416                         }
1417                         newSkb = (struct sk_buff *)(data_ptrs.d[0].os_net_buf_ptr);
1418                         newSkb->len = skb->len + macHeaderLengthInBytes;
1419
1420                         memcpy((void*)data_ptrs.d[0].os_data_ptr + macHeaderLengthInBytes,
1421                                 skb->data, skb->len);
1422
1423                         bulkdata->d[0].os_data_ptr = newSkb->data;
1424                         bulkdata->d[0].os_net_buf_ptr = (unsigned char*)newSkb;
1425                         bulkdata->d[0].data_length = newSkb->len;
1426
1427                         bufPtr = (u8*)data_ptrs.d[0].os_data_ptr;
1428
1429                         /* The old skb will not be used again */
1430                         kfree_skb(skb);
1431                     } else {
1432                         /* skb headroom is sufficient to append Macheader */
1433                         bufPtr = (u8*)skb_push(skb, macHeaderLengthInBytes);
1434                         bulkdata->d[0].os_data_ptr = skb->data;
1435                         bulkdata->d[0].os_net_buf_ptr = (unsigned char*)skb;
1436                         bulkdata->d[0].data_length = skb->len;
1437                     }
1438                 } else {
1439                     unifi_trace(priv, UDBG3, "destination is not a QSTA\n");
1440                     if (headroom < macHeaderLengthInBytes) {
1441                         csrResult = unifi_net_data_malloc(priv, &data_ptrs.d[0], skb->len + macHeaderLengthInBytes);
1442
1443                         if (csrResult != CSR_RESULT_SUCCESS) {
1444                             unifi_error(priv, " failed to allocate request_data. in update_macheader func\n");
1445                             return -1;
1446                         }
1447                         newSkb = (struct sk_buff *)(data_ptrs.d[0].os_net_buf_ptr);
1448                         newSkb->len = skb->len + macHeaderLengthInBytes;
1449
1450                         memcpy((void*)data_ptrs.d[0].os_data_ptr + macHeaderLengthInBytes,
1451                                 skb->data, skb->len);
1452
1453                         bulkdata->d[0].os_data_ptr = newSkb->data;
1454                         bulkdata->d[0].os_net_buf_ptr = (unsigned char*)newSkb;
1455                         bulkdata->d[0].data_length = newSkb->len;
1456
1457                         bufPtr = (u8*)data_ptrs.d[0].os_data_ptr;
1458
1459                         /* The old skb will not be used again */
1460                         kfree_skb(skb);
1461                     } else {
1462                         /* skb headroom is sufficient to append Macheader */
1463                         bufPtr = (u8*)skb_push(skb, macHeaderLengthInBytes);
1464                         bulkdata->d[0].os_data_ptr = skb->data;
1465                         bulkdata->d[0].os_net_buf_ptr = (unsigned char*)skb;
1466                         bulkdata->d[0].data_length = skb->len;
1467                     }
1468                 }
1469             }
1470     }
1471
1472     /* prepare the complete skb, by pushing the MAC header to the begining of the skb->data */
1473     unifi_trace(priv, UDBG5, "updated Mac Header: %d \n",macHeaderLengthInBytes);
1474     memcpy(bufPtr, macHeaderBuf, macHeaderLengthInBytes);
1475
1476     unifi_trace(priv, UDBG5, "leaving the update_macheader function\n");
1477     return 0;
1478 }
1479 /*
1480  * ---------------------------------------------------------------------------
1481  *  uf_ap_process_data_pdu
1482  *
1483  *
1484  *      Takes care of intra BSS admission control & routing packets within BSS
1485  *
1486  *  Arguments:
1487  *      priv            Pointer to device private context struct
1488  *      skb             Socket buffer containing data packet to transmit
1489  *      ehdr            ethernet header to fetch priority of packet
1490  *      srcStaInfo      source stations record for connection verification
1491  *      packed_signal
1492  *      signal_len
1493  *      signal          MA-PACKET.indication signal
1494  *      bulkdata        if newSkb allocated then bulkdata updated to send to unifi
1495  *      macHeaderLengthInBytes no. of bytes of mac header in received frame
1496  *
1497  *  Returns:
1498  *      Zero on success(ap processing complete) or -1 if packet also have to be sent to NETDEV.
1499  * ---------------------------------------------------------------------------
1500  */
1501 int
1502 uf_ap_process_data_pdu(unifi_priv_t *priv, struct sk_buff *skb,
1503                        struct ethhdr *ehdr, CsrWifiRouterCtrlStaInfo_t * srcStaInfo,
1504                        const CSR_SIGNAL *signal,
1505                        bulk_data_param_t *bulkdata,
1506                        u8 macHeaderLengthInBytes)
1507 {
1508     const CSR_MA_PACKET_INDICATION *ind = &(signal->u.MaPacketIndication);
1509     u16 interfaceTag = (ind->VirtualInterfaceIdentifier & 0x00ff);
1510     struct sk_buff *newSkb = NULL;
1511     /* pointer to skb or private skb created using skb_copy() */
1512     struct sk_buff *skbPtr = skb;
1513     u8 sendToNetdev = FALSE;
1514     u8 qosDestination = FALSE;
1515     CSR_PRIORITY priority = CSR_CONTENTION;
1516     CsrWifiRouterCtrlStaInfo_t *dstStaInfo = NULL;
1517     netInterface_priv_t *interfacePriv;
1518
1519     unifi_trace(priv, UDBG5, "entering  uf_ap_process_data_pdu %d\n",macHeaderLengthInBytes);
1520     /* InterfaceTag validation from MA_PACKET.indication */
1521     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
1522         unifi_trace(priv, UDBG1, "Interface Tag is Invalid in uf_ap_process_data_pdu\n");
1523         unifi_net_data_free(priv, &bulkdata->d[0]);
1524         return 0;
1525     }
1526     interfacePriv = priv->interfacePriv[interfaceTag];
1527
1528     if((interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO) &&
1529        (interfacePriv->intraBssEnabled == FALSE)) {
1530         unifi_trace(priv, UDBG2, "uf_ap_process_data_pdu:P2P GO intrabssEnabled?= %d\n", interfacePriv->intraBssEnabled);
1531
1532         /*In P2P GO case, if intraBSS distribution Disabled then don't do IntraBSS routing */
1533         /* If destination in our BSS then drop otherwise give packet to netdev */
1534         dstStaInfo = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv, ehdr->h_dest, interfaceTag);
1535         if (dstStaInfo) {
1536             unifi_net_data_free(priv, &bulkdata->d[0]);
1537             return 0;
1538         }
1539         /* May be associated P2PCLI trying to send the packets on backbone (Netdev) */
1540         return -1;
1541     }
1542
1543     if(!memcmp(ehdr->h_dest, interfacePriv->bssid.a, ETH_ALEN)) {
1544         /* This packet will be given to the TCP/IP stack since this packet is for us(AP)
1545          * No routing needed */
1546         unifi_trace(priv, UDBG4, "destination address is csr_ap\n");
1547         return -1;
1548     }
1549
1550     /* fetch the destination record from staion record database */
1551     dstStaInfo = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv, ehdr->h_dest, interfaceTag);
1552
1553     /* AP mode processing, & if packet is unicast */
1554     if(!dstStaInfo) {
1555         if (!(ehdr->h_dest[0] & 0x1)) {
1556             /* destination not in station record & its a unicast packet, so pass the packet to network stack */
1557             unifi_trace(priv, UDBG3, "unicast frame & destination record not exist, send to netdev proto = %x\n", htons(skb->protocol));
1558             return -1;
1559         } else {
1560             /* packet is multicast/broadcast */
1561             /* copy the skb to skbPtr, send skb to netdev & skbPtr to multicast/broad cast list */
1562             unifi_trace(priv, UDBG5, "skb_copy, in  uf_ap_process_data_pdu, protocol = %x\n", htons(skb->protocol));
1563             skbPtr = skb_copy(skb, GFP_KERNEL);
1564             if(skbPtr == NULL) {
1565                 /* We don't have memory to don't send the frame in BSS*/
1566                 unifi_notice(priv, "broacast/multicast frame can't be sent in BSS No memeory: proto = %x\n", htons(skb->protocol));
1567                 return -1;
1568             }
1569             sendToNetdev = TRUE;
1570         }
1571     } else {
1572
1573         /* validate the Peer & Destination Station record */
1574         if (uf_process_station_records_for_sending_data(priv, interfaceTag, srcStaInfo, dstStaInfo)) {
1575             unifi_notice(priv, "uf_ap_process_data_pdu: station record validation failed \n");
1576             interfacePriv->stats.rx_errors++;
1577             unifi_net_data_free(priv, &bulkdata->d[0]);
1578             return 0;
1579         }
1580     }
1581
1582     /* BroadCast packet received and it's been sent as non QOS packets.
1583      * Since WMM spec not mandates broadcast/multicast to be sent as QOS data only,
1584      * if all Peers are QSTA
1585      */
1586     if(sendToNetdev) {
1587        /* BroadCast packet and it's been sent as non QOS packets */
1588         qosDestination = FALSE;
1589     } else if(dstStaInfo && (dstStaInfo->wmmOrQosEnabled == TRUE)) {
1590           qosDestination = TRUE;
1591     }
1592
1593     unifi_trace(priv, UDBG3, "uf_ap_process_data_pdu QoS destination  = %s\n", (qosDestination)? "TRUE": "FALSE");
1594
1595     /* packet is allowed to send to unifi, update the Mac header */
1596     if (update_macheader(priv, skbPtr, newSkb, &priority, bulkdata, interfaceTag, macHeaderLengthInBytes, qosDestination)) {
1597         interfacePriv->stats.rx_errors++;
1598         unifi_notice(priv, "(Packet Drop) failed to update the Mac header in uf_ap_process_data_pdu\n");
1599         if (sendToNetdev) {
1600             /*  Free's the skb_copy(skbPtr) data since packet processing failed */
1601             bulkdata->d[0].os_data_ptr = skbPtr->data;
1602             bulkdata->d[0].os_net_buf_ptr = (unsigned char*)skbPtr;
1603             bulkdata->d[0].data_length = skbPtr->len;
1604             unifi_net_data_free(priv, &bulkdata->d[0]);
1605         }
1606         return -1;
1607     }
1608
1609     unifi_trace(priv, UDBG3, "Mac Header updated...calling uf_process_ma_packet_req \n");
1610
1611     /* Packet is ready to send to unifi ,transmissionControl = 0x0004, confirmation is not needed for data packets */
1612     if (uf_process_ma_packet_req(priv,  ehdr->h_dest, 0xffffffff, interfaceTag, CSR_NO_CONFIRM_REQUIRED, (CSR_RATE)0,priority, priv->netdev_client->sender_id, bulkdata)) {
1613         if (sendToNetdev) {
1614             unifi_trace(priv, UDBG1, "In uf_ap_process_data_pdu, (Packet Drop) uf_process_ma_packet_req failed. freeing skb_copy data (original data sent to Netdev)\n");
1615             /*  Free's the skb_copy(skbPtr) data since packet processing failed */
1616             bulkdata->d[0].os_data_ptr = skbPtr->data;
1617             bulkdata->d[0].os_net_buf_ptr = (unsigned char*)skbPtr;
1618             bulkdata->d[0].data_length = skbPtr->len;
1619             unifi_net_data_free(priv, &bulkdata->d[0]);
1620         } else {
1621             /* This free's the skb data */
1622             unifi_trace(priv, UDBG1, "In uf_ap_process_data_pdu, (Packet Drop). Unicast data so freeing original skb \n");
1623             unifi_net_data_free(priv, &bulkdata->d[0]);
1624         }
1625     }
1626     unifi_trace(priv, UDBG5, "leaving  uf_ap_process_data_pdu\n");
1627
1628     if (sendToNetdev) {
1629         /* The packet is multicast/broadcast, so after AP processing packet has to
1630          * be sent to netdev, if peer port state is open
1631         */
1632         unifi_trace(priv, UDBG4, "Packet will be routed to NetDev\n");
1633         return -1;
1634     }
1635     /* Ap handled the packet & its a unicast packet, no need to send to netdev */
1636     return 0;
1637 }
1638
1639 #endif
1640
1641 CsrResult uf_process_ma_packet_req(unifi_priv_t *priv,
1642                                    u8 *peerMacAddress,
1643                                    CSR_CLIENT_TAG hostTag,
1644                                    u16 interfaceTag,
1645                                    CSR_TRANSMISSION_CONTROL transmissionControl,
1646                                    CSR_RATE TransmitRate,
1647                                    CSR_PRIORITY priority,
1648                                    CSR_PROCESS_ID leSenderProcessId,
1649                                    bulk_data_param_t *bulkdata)
1650 {
1651     CsrResult status = CSR_RESULT_SUCCESS;
1652     CSR_SIGNAL signal;
1653     int result;
1654 #ifdef CSR_SUPPORT_SME
1655    CsrWifiRouterCtrlStaInfo_t *staRecord = NULL;
1656     const u8 *macHdrLocation =  bulkdata->d[0].os_data_ptr;
1657     CsrWifiPacketType pktType;
1658     int frameType = 0;
1659     u8 queuePacketDozing = FALSE;
1660     u32 priority_q;
1661     u16 frmCtrl;
1662     struct list_head * list = NULL; /* List to which buffered PDUs are to be enqueued*/
1663     u8 setBcTim=FALSE;
1664     netInterface_priv_t *interfacePriv;
1665     u8 requeueOnSamePos = FALSE;
1666     u32 handle = 0xFFFFFFFF;
1667     unsigned long lock_flags;
1668
1669         unifi_trace(priv, UDBG5,
1670                 "entering uf_process_ma_packet_req, peer: %pMF\n",
1671                 peerMacAddress);
1672
1673     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
1674         unifi_error(priv, "interfaceTag >= CSR_WIFI_NUM_INTERFACES, interfacetag = %d\n", interfaceTag);
1675         return CSR_RESULT_FAILURE;
1676     }
1677     interfacePriv = priv->interfacePriv[interfaceTag];
1678
1679
1680     /* fetch the station record for corresponding peer mac address */
1681     if ((staRecord = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv, peerMacAddress, interfaceTag))) {
1682         handle = staRecord->assignedHandle;
1683     }
1684
1685     /* Frame ma-packet.req, this is saved/transmitted depend on queue state */
1686     unifi_frame_ma_packet_req(priv, priority, TransmitRate, hostTag,
1687                               interfaceTag, transmissionControl, leSenderProcessId,
1688                               peerMacAddress, &signal);
1689
1690    /* Since it's common path between STA & AP mode, in case of STA packet
1691      * need not to be queued but in AP case we have to queue PDU's in
1692      * different scenarios
1693      */
1694     switch(interfacePriv->interfaceMode)
1695     {
1696         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
1697         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
1698             /* For this mode processing done below */
1699             break;
1700         default:
1701             /* In case of STA/IBSS/P2PCLI/AMP, no checks needed send the packet down & return */
1702             unifi_trace(priv, UDBG5, "In %s, interface mode is %x \n", __FUNCTION__, interfacePriv->interfaceMode);
1703             if (interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_NONE) {
1704                 unifi_warning(priv, "In %s, interface mode NONE \n", __FUNCTION__);
1705             }
1706             if ((result = ul_send_signal_unpacked(priv, &signal, bulkdata))) {
1707                 status = CSR_RESULT_FAILURE;
1708             }
1709             return status;
1710     }
1711
1712     /* -----Only AP/P2pGO mode handling falls below----- */
1713
1714     /* convert priority to queue */
1715     priority_q = unifi_frame_priority_to_queue((CSR_PRIORITY) priority);
1716
1717     /* check the powersave status of the peer */
1718     if (staRecord && (staRecord->currentPeerState ==
1719                      CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE)) {
1720         /* Peer is dozing & packet have to be delivered, so buffer the packet &
1721          * update the TIM
1722          */
1723         queuePacketDozing = TRUE;
1724     }
1725
1726     /* find the type of frame unicast or mulicast/broadcast */
1727     if (*peerMacAddress & 0x1) {
1728         /* Multicast/broadCast data are always triggered by vif_availability.ind
1729          * at the DTIM
1730          */
1731         pktType = CSR_WIFI_MULTICAST_PDU;
1732     } else {
1733         pktType = CSR_WIFI_UNICAST_PDU;
1734     }
1735
1736     /* Fetch the frame control field from mac header & check for frame type */
1737     frmCtrl = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(macHdrLocation);
1738
1739     /* Processing done according to Frame/Packet type */
1740     frameType =  ((frmCtrl & 0x000c) >> FRAME_CONTROL_TYPE_FIELD_OFFSET);
1741     switch(frameType)
1742     {
1743         case IEEE802_11_FRAMETYPE_MANAGEMENT:
1744
1745             switch(pktType)
1746             {
1747                 case CSR_WIFI_UNICAST_PDU:
1748                     unifi_trace(priv, UDBG5, "management unicast PDU in uf_process_ma_packet_req \n");
1749                     /* push the packet in to the queue with appropriate mgt list */
1750                     if (!staRecord) {
1751                         /* push the packet to the unifi if list is empty (if packet lost how to re-enque) */
1752                         if (list_empty(&interfacePriv->genericMgtFrames)) {
1753 #ifdef CSR_SUPPORT_SME
1754                             if(!(IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag))) {
1755 #endif
1756
1757                             unifi_trace(priv, UDBG3, "genericMgtFrames list is empty uf_process_ma_packet_req \n");
1758                             result = ul_send_signal_unpacked(priv, &signal, bulkdata);
1759                             /*  reque only on ENOSPC */
1760                             if(result == -ENOSPC) {
1761                                 /* requeue the failed packet to genericMgtFrame with same position */
1762                                 unifi_trace(priv, UDBG1, "(ENOSPC) Sending genericMgtFrames Failed so buffering\n");
1763                                 list = &interfacePriv->genericMgtFrames;
1764                                 requeueOnSamePos = TRUE;
1765                             }
1766 #ifdef CSR_SUPPORT_SME
1767                             }else{
1768                                 list = &interfacePriv->genericMgtFrames;
1769                                 unifi_trace(priv, UDBG3, "genericMgtFrames queue empty and dtim started\n hosttag is 0x%x,\n",signal.u.MaPacketRequest.HostTag);
1770                                 update_eosp_to_head_of_broadcast_list_head(priv,interfaceTag);
1771                            }
1772 #endif
1773                         } else {
1774                             /* Queue the packet to genericMgtFrame of unifi_priv_t data structure */
1775                             list = &interfacePriv->genericMgtFrames;
1776                             unifi_trace(priv, UDBG2, "genericMgtFrames queue not empty\n");
1777                         }
1778                     } else {
1779                         /* check peer power state */
1780                         if (queuePacketDozing || !list_empty(&staRecord->mgtFrames) || IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag)) {
1781                             /* peer is in dozing mode, so queue packet in mgt frame list of station record */
1782                            /*if multicast traffic is going on, buffer the unicast packets*/
1783                             list = &staRecord->mgtFrames;
1784
1785                             unifi_trace(priv, UDBG1, "staRecord->MgtFrames list empty? = %s, handle = %d, queuePacketDozing = %d\n",
1786                                         (list_empty(&staRecord->mgtFrames))? "YES": "NO", staRecord->assignedHandle, queuePacketDozing);
1787                             if(IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag)){
1788                                 update_eosp_to_head_of_broadcast_list_head(priv,interfaceTag);
1789                             }
1790
1791                         } else {
1792                             unifi_trace(priv, UDBG5, "staRecord->mgtFrames list is empty uf_process_ma_packet_req \n");
1793                             result = ul_send_signal_unpacked(priv, &signal, bulkdata);
1794                             if(result == -ENOSPC) {
1795                                 /* requeue the failed packet to staRecord->mgtFrames with same position */
1796                                 list = &staRecord->mgtFrames;
1797                                 requeueOnSamePos = TRUE;
1798                                 unifi_trace(priv, UDBG1, "(ENOSPC) Sending MgtFrames Failed handle = %d so buffering\n",staRecord->assignedHandle);
1799                                 priv->pausedStaHandle[0]=(u8)(staRecord->assignedHandle);
1800                             } else if (result) {
1801                                 status = CSR_RESULT_FAILURE;
1802                             }
1803                         }
1804                     }
1805                     break;
1806                 case CSR_WIFI_MULTICAST_PDU:
1807                     unifi_trace(priv, UDBG5, "management multicast/broadcast PDU in uf_process_ma_packet_req 'QUEUE it' \n");
1808                     /* Queue the packet to genericMulticastOrBroadCastMgtFrames of unifi_priv_t data structure
1809                      * will be sent when we receive VIF AVAILABILITY from firmware as part of DTIM
1810                      */
1811
1812                     list = &interfacePriv->genericMulticastOrBroadCastMgtFrames;
1813                     if((interfacePriv->interfaceMode != CSR_WIFI_ROUTER_CTRL_MODE_IBSS) &&
1814                             (list_empty(&interfacePriv->genericMulticastOrBroadCastMgtFrames))) {
1815                         setBcTim=TRUE;
1816                     }
1817                     break;
1818                 default:
1819                     unifi_error(priv, "condition never meets: packet type unrecognized\n");
1820             }
1821             break;
1822         case IEEE802_11_FRAMETYPE_DATA:
1823             switch(pktType)
1824             {
1825                 case CSR_WIFI_UNICAST_PDU:
1826                     unifi_trace(priv, UDBG5, "data unicast PDU in uf_process_ma_packet_req \n");
1827                     /* check peer power state, list status & peer port status */
1828                     if(!staRecord) {
1829                         unifi_error(priv, "In %s unicast but staRecord = NULL\n", __FUNCTION__);
1830                         return CSR_RESULT_FAILURE;
1831                     } else if (queuePacketDozing || isRouterBufferEnabled(priv,priority_q)|| !list_empty(&staRecord->dataPdu[priority_q]) || IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag)) {
1832                         /* peer is in dozing mode, so queue packet in mgt frame list of station record */
1833                         /* if multicast traffic is going on, buffet the unicast packets */
1834                         unifi_trace(priv, UDBG2, "Enqueued to staRecord->dataPdu[%d] queuePacketDozing=%d,\
1835                                 Buffering enabled = %d \n", priority_q,queuePacketDozing,isRouterBufferEnabled(priv,priority_q));
1836                         list = &staRecord->dataPdu[priority_q];
1837                     } else {
1838                         unifi_trace(priv, UDBG5, "staRecord->dataPdu[%d] list is empty uf_process_ma_packet_req \n", priority_q);
1839                         /* Pdu allowed to send to unifi */
1840                         result = ul_send_signal_unpacked(priv, &signal, bulkdata);
1841                         if(result == -ENOSPC) {
1842                             /* requeue the failed packet to staRecord->dataPdu[priority_q] with same position */
1843                             unifi_trace(priv, UDBG1, "(ENOSPC) Sending Unicast DataPDU to queue %d Failed so buffering\n",priority_q);
1844                             requeueOnSamePos = TRUE;
1845                             list = &staRecord->dataPdu[priority_q];
1846                             priv->pausedStaHandle[priority_q]=(u8)(staRecord->assignedHandle);
1847                             if(!isRouterBufferEnabled(priv,priority_q)) {
1848                                 unifi_error(priv,"Buffering Not enabled for queue %d \n",priority_q);
1849                             }
1850                         } else if (result) {
1851                             status = CSR_RESULT_FAILURE;
1852                         }
1853                     }
1854                     break;
1855                 case CSR_WIFI_MULTICAST_PDU:
1856                     unifi_trace(priv, UDBG5, "data multicast/broadcast PDU in uf_process_ma_packet_req \n");
1857                     /* Queue the packet to genericMulticastOrBroadCastFrames list of unifi_priv_t data structure
1858                      * will be sent when we receive VIF AVAILABILITY from firmware as part of DTIM
1859                      */
1860                     list = &interfacePriv->genericMulticastOrBroadCastFrames;
1861                     if(list_empty(&interfacePriv->genericMulticastOrBroadCastFrames)) {
1862                         setBcTim = TRUE;
1863                     }
1864                     break;
1865                 default:
1866                     unifi_error(priv, "condition never meets: packet type un recognized\n");
1867             }
1868             break;
1869         default:
1870             unifi_error(priv, "unrecognized frame type\n");
1871     }
1872     if(list) {
1873         status = enque_tx_data_pdu(priv, bulkdata,list, &signal,requeueOnSamePos);
1874         /* Record no. of packet queued for each peer */
1875         if (staRecord && (pktType == CSR_WIFI_UNICAST_PDU) && (!status)) {
1876             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
1877             staRecord->noOfPktQueued++;
1878             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
1879         }
1880         else if ((pktType == CSR_WIFI_MULTICAST_PDU) && (!status))
1881         {
1882             /* If broadcast Tim is set && queuing is successfull, then only update TIM */
1883             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
1884             interfacePriv->noOfbroadcastPktQueued++;
1885             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
1886         }
1887     }
1888     /* If broadcast Tim is set && queuing is successfull, then only update TIM */
1889     if(setBcTim && !status) {
1890         unifi_trace(priv, UDBG3, "tim set due to broadcast pkt\n");
1891         if (!interfacePriv->bcTimSetReqPendingFlag)
1892         {
1893             update_tim(priv,0,CSR_WIFI_TIM_SET,interfaceTag, handle);
1894         }
1895         else
1896         {
1897             /* Cache the TimSet value so that it will processed immidiatly after
1898             * completing the current setTim Request
1899             */
1900             interfacePriv->bcTimSetReqQueued = CSR_WIFI_TIM_SET;
1901             unifi_trace(priv, UDBG2, "uf_process_ma_packet_req : One more UpdateDTim Request(:%d) Queued \n",
1902                         interfacePriv->bcTimSetReqQueued);
1903         }
1904     } else if(staRecord && staRecord->currentPeerState ==
1905                             CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE) {
1906         if(staRecord->timSet == CSR_WIFI_TIM_RESET || staRecord->timSet == CSR_WIFI_TIM_RESETTING) {
1907             if(!staRecord->wmmOrQosEnabled) {
1908                 if(!list_empty(&staRecord->mgtFrames) ||
1909                    !list_empty(&staRecord->dataPdu[3]) ||
1910                    !list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION])) {
1911                     unifi_trace(priv, UDBG3, "tim set due to unicast pkt & peer in powersave\n");
1912                     if (!staRecord->timRequestPendingFlag){
1913                         update_tim(priv,staRecord->aid,1,interfaceTag, handle);
1914                     }
1915                     else
1916                     {
1917                         /* Cache the TimSet value so that it will processed immidiatly after
1918                          * completing the current setTim Request
1919                          */
1920                         staRecord->updateTimReqQueued = 1;
1921                         unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
1922                                     staRecord->aid);
1923                     }
1924                 }
1925             } else {
1926                 /* Check for non delivery enable(i.e trigger enable), all delivery enable & legacy AC for TIM update in firmware */
1927                 u8 allDeliveryEnabled = 0, dataAvailable = 0;
1928                 /* Check if all AC's are Delivery Enabled */
1929                 is_all_ac_deliver_enabled_and_moredata(staRecord, &allDeliveryEnabled, &dataAvailable);
1930                 if (uf_is_more_data_for_non_delivery_ac(staRecord) || (allDeliveryEnabled && dataAvailable)
1931                     || (!list_empty(&staRecord->mgtFrames))) {
1932                     if (!staRecord->timRequestPendingFlag) {
1933                         update_tim(priv,staRecord->aid,1,interfaceTag, handle);
1934                     }
1935                     else
1936                     {
1937                         /* Cache the TimSet value so that it will processed immidiatly after
1938                          * completing the current setTim Request
1939                          */
1940                         staRecord->updateTimReqQueued = 1;
1941                         unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
1942                                     staRecord->aid);
1943                     }
1944                 }
1945             }
1946         }
1947     }
1948
1949     if((list) && (pktType == CSR_WIFI_UNICAST_PDU && !queuePacketDozing) && !(isRouterBufferEnabled(priv,priority_q)) && !(IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag))) {
1950         unifi_trace(priv, UDBG2, "buffering cleared for queue = %d So resending buffered frames\n",priority_q);
1951         uf_send_buffered_frames(priv, priority_q);
1952     }
1953     unifi_trace(priv, UDBG5, "leaving uf_process_ma_packet_req \n");
1954     return status;
1955 #else
1956 #ifdef CSR_NATIVE_LINUX
1957     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
1958         unifi_error(priv, "interfaceTag >= CSR_WIFI_NUM_INTERFACES, interfacetag = %d\n", interfaceTag);
1959         return CSR_RESULT_FAILURE;
1960     }
1961     /* Frame ma-packet.req, this is saved/transmitted depend on queue state */
1962     unifi_frame_ma_packet_req(priv, priority, TransmitRate, hostTag, interfaceTag,
1963             transmissionControl, leSenderProcessId,
1964             peerMacAddress, &signal);
1965     result = ul_send_signal_unpacked(priv, &signal, bulkdata);
1966     if (result) {
1967         return CSR_RESULT_FAILURE;
1968     }
1969 #endif
1970     return status;
1971 #endif
1972 }
1973
1974 #ifdef CSR_SUPPORT_SME
1975 s8 uf_get_protection_bit_from_interfacemode(unifi_priv_t *priv, u16 interfaceTag, const u8 *daddr)
1976 {
1977     s8 protection = 0;
1978     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
1979
1980     switch(interfacePriv->interfaceMode)
1981     {
1982         case CSR_WIFI_ROUTER_CTRL_MODE_STA:
1983         case CSR_WIFI_ROUTER_CTRL_MODE_P2PCLI:
1984         case CSR_WIFI_ROUTER_CTRL_MODE_AMP:
1985         case CSR_WIFI_ROUTER_CTRL_MODE_IBSS:
1986             protection = interfacePriv->protect;
1987             break;
1988         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
1989         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
1990             {
1991                 CsrWifiRouterCtrlStaInfo_t *dstStaInfo = NULL;
1992                 if (daddr[0] & 0x1) {
1993                     unifi_trace(priv, UDBG3, "broadcast/multicast packet in send_ma_pkt_request\n");
1994                     /* In this mode, the protect member of priv structure has an information of how
1995                      * AP/P2PGO has started, & the member updated in set mode request for AP/P2PGO
1996                      */
1997                     protection = interfacePriv->protect;
1998                 } else {
1999                     /* fetch the destination record from staion record database */
2000                     dstStaInfo = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv, daddr, interfaceTag);
2001                     if (!dstStaInfo) {
2002                         unifi_trace(priv, UDBG3, "peer not found in station record in send_ma_pkt_request\n");
2003                         return -1;
2004                     }
2005                     protection = dstStaInfo->protection;
2006                 }
2007             }
2008             break;
2009         default:
2010             unifi_trace(priv, UDBG2, "mode unknown in send_ma_pkt_request\n");
2011     }
2012     return protection;
2013 }
2014 #endif
2015 #ifdef CSR_SUPPORT_SME
2016 u8 send_multicast_frames(unifi_priv_t *priv, u16 interfaceTag)
2017 {
2018     int r;
2019     tx_buffered_packets_t * buffered_pkt = NULL;
2020     u8 moreData = FALSE;
2021     u8 pduSent =0;
2022     unsigned long lock_flags;
2023     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2024     u32 hostTag = 0xffffffff;
2025
2026     func_enter();
2027     if(!isRouterBufferEnabled(priv,UNIFI_TRAFFIC_Q_VO)) {
2028         while((interfacePriv->dtimActive)&& (buffered_pkt=dequeue_tx_data_pdu(priv,&interfacePriv->genericMulticastOrBroadCastMgtFrames))) {
2029             buffered_pkt->transmissionControl |= (TRANSMISSION_CONTROL_TRIGGER_MASK);
2030             moreData = (buffered_pkt->transmissionControl & TRANSMISSION_CONTROL_EOSP_MASK)?FALSE:TRUE;
2031
2032
2033             unifi_trace(priv,UDBG2,"DTIM Occurred for interface:sending Mgt packet %d\n",interfaceTag);
2034
2035             if((r=frame_and_send_queued_pdu(priv,buffered_pkt,NULL,moreData,FALSE)) == -ENOSPC) {
2036                unifi_trace(priv,UDBG1,"frame_and_send_queued_pdu failed with ENOSPC for host tag = %x\n", buffered_pkt->hostTag);
2037                /* Enqueue at the head of the queue */
2038                spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
2039                list_add(&buffered_pkt->q, &interfacePriv->genericMulticastOrBroadCastMgtFrames);
2040                spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
2041                break;
2042             } else {
2043                 unifi_trace(priv,UDBG1,"send_multicast_frames: Send genericMulticastOrBroadCastMgtFrames (%x, %x)\n",
2044                                         buffered_pkt->hostTag,
2045                                         r);
2046                 if(r) {
2047                    unifi_net_data_free(priv, &buffered_pkt->bulkdata);
2048                 }
2049                 if(!moreData) {
2050
2051                     interfacePriv->dtimActive = FALSE;
2052                     if(!r) {
2053                         hostTag = buffered_pkt->hostTag;
2054                         pduSent++;
2055                     } else {
2056                         send_vif_availibility_rsp(priv,uf_get_vif_identifier(interfacePriv->interfaceMode,interfaceTag),CSR_RC_UNSPECIFIED_FAILURE);
2057                     }
2058                 }
2059                 /* Buffered frame sent successfully */
2060                 spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2061                 interfacePriv->noOfbroadcastPktQueued--;
2062                 spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2063                 kfree(buffered_pkt);
2064            }
2065
2066         }
2067     }
2068     if(!isRouterBufferEnabled(priv,UNIFI_TRAFFIC_Q_CONTENTION)) {
2069         while((interfacePriv->dtimActive)&& (buffered_pkt=dequeue_tx_data_pdu(priv,&interfacePriv->genericMulticastOrBroadCastFrames))) {
2070             buffered_pkt->transmissionControl |= TRANSMISSION_CONTROL_TRIGGER_MASK;
2071             moreData = (buffered_pkt->transmissionControl & TRANSMISSION_CONTROL_EOSP_MASK)?FALSE:TRUE;
2072
2073
2074             if((r=frame_and_send_queued_pdu(priv,buffered_pkt,NULL,moreData,FALSE)) == -ENOSPC) {
2075                 /* Clear the trigger bit transmission control*/
2076                 buffered_pkt->transmissionControl &= ~(TRANSMISSION_CONTROL_TRIGGER_MASK);
2077                 /* Enqueue at the head of the queue */
2078                 spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
2079                 list_add(&buffered_pkt->q, &interfacePriv->genericMulticastOrBroadCastFrames);
2080                 spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
2081                 break;
2082             } else {
2083                 if(r) {
2084                     unifi_trace(priv,UDBG1,"send_multicast_frames: Send genericMulticastOrBroadCastFrame failed (%x, %x)\n",
2085                                             buffered_pkt->hostTag,
2086                                             r);
2087                     unifi_net_data_free(priv, &buffered_pkt->bulkdata);
2088                 }
2089                 if(!moreData) {
2090                     interfacePriv->dtimActive = FALSE;
2091                     if(!r) {
2092                         pduSent ++;
2093                         hostTag = buffered_pkt->hostTag;
2094                     } else {
2095                         send_vif_availibility_rsp(priv,uf_get_vif_identifier(interfacePriv->interfaceMode,interfaceTag),CSR_RC_UNSPECIFIED_FAILURE);
2096                     }
2097                 }
2098                 /* Buffered frame sent successfully */
2099                 spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2100                 interfacePriv->noOfbroadcastPktQueued--;
2101                 spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2102                 kfree(buffered_pkt);
2103             }
2104         }
2105     }
2106     if((interfacePriv->dtimActive == FALSE)) {
2107         /* Record the host Tag*/
2108         unifi_trace(priv,UDBG2,"send_multicast_frames: Recorded hostTag of EOSP packet: = 0x%x\n",hostTag);
2109         interfacePriv->multicastPduHostTag = hostTag;
2110     }
2111     return pduSent;
2112 }
2113 #endif
2114 void uf_process_ma_vif_availibility_ind(unifi_priv_t *priv,u8 *sigdata,
2115                                         u32 siglen)
2116 {
2117 #ifdef CSR_SUPPORT_SME
2118     CSR_SIGNAL signal;
2119     CSR_MA_VIF_AVAILABILITY_INDICATION *ind;
2120     int r;
2121     u16 interfaceTag;
2122     u8 pduSent =0;
2123     CSR_RESULT_CODE resultCode = CSR_RC_SUCCESS;
2124     netInterface_priv_t *interfacePriv;
2125
2126     func_enter();
2127     unifi_trace(priv, UDBG3,
2128             "uf_process_ma_vif_availibility_ind: Process signal 0x%.4X\n",
2129             *((u16*)sigdata));
2130
2131     r = read_unpack_signal(sigdata, &signal);
2132     if (r) {
2133         unifi_error(priv,
2134                     "uf_process_ma_vif_availibility_ind: Received unknown signal 0x%.4X.\n",
2135                     CSR_GET_UINT16_FROM_LITTLE_ENDIAN(sigdata));
2136         func_exit();
2137         return;
2138     }
2139     ind = &signal.u.MaVifAvailabilityIndication;
2140     interfaceTag=ind->VirtualInterfaceIdentifier & 0xff;
2141
2142     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
2143         unifi_error(priv, "in vif_availability_ind interfaceTag is wrong\n");
2144         return;
2145     }
2146
2147     interfacePriv = priv->interfacePriv[interfaceTag];
2148
2149     if(ind->Multicast) {
2150         if(list_empty(&interfacePriv->genericMulticastOrBroadCastFrames) &&
2151             list_empty(&interfacePriv->genericMulticastOrBroadCastMgtFrames)) {
2152             /* This condition can occur because of a potential race where the
2153                TIM is not yet reset as host is waiting for confirm but it is sent
2154                by firmware and DTIM occurs*/
2155             unifi_notice(priv,"ma_vif_availibility_ind recevied for multicast but queues are empty%d\n",interfaceTag);
2156             send_vif_availibility_rsp(priv,ind->VirtualInterfaceIdentifier,CSR_RC_NO_BUFFERED_BROADCAST_MULTICAST_FRAMES);
2157             interfacePriv->dtimActive = FALSE;
2158             if(interfacePriv->multicastPduHostTag == 0xffffffff) {
2159                 unifi_notice(priv,"ma_vif_availibility_ind recevied for multicast but queues are empty%d\n",interfaceTag);
2160                 /* This may be an extra request in very rare race conditions but it is fine as it would atleast remove the potential lock up */
2161                 if (!interfacePriv->bcTimSetReqPendingFlag)
2162                 {
2163                     update_tim(priv,0,CSR_WIFI_TIM_RESET,interfaceTag, 0xFFFFFFFF);
2164                 }
2165                 else
2166                 {
2167                     /* Cache the TimSet value so that it will processed immidiatly after
2168                      * completing the current setTim Request
2169                      */
2170                     interfacePriv->bcTimSetReqQueued = CSR_WIFI_TIM_RESET;
2171                     unifi_trace(priv, UDBG2, "uf_process_ma_vif_availibility_ind : One more UpdateDTim Request(%d) Queued \n",
2172                                 interfacePriv->bcTimSetReqQueued);
2173                 }
2174             }
2175             return;
2176         }
2177         if(interfacePriv->dtimActive) {
2178             unifi_trace(priv,UDBG2,"DTIM Occurred for already active DTIM interface %d\n",interfaceTag);
2179             return;
2180         } else {
2181             unifi_trace(priv,UDBG2,"DTIM Occurred for interface %d\n",interfaceTag);
2182             if(list_empty(&interfacePriv->genericMulticastOrBroadCastFrames)) {
2183                 set_eosp_transmit_ctrl(priv,&interfacePriv->genericMulticastOrBroadCastMgtFrames);
2184             } else {
2185                 set_eosp_transmit_ctrl(priv,&interfacePriv->genericMulticastOrBroadCastFrames);
2186             }
2187         }
2188         interfacePriv->dtimActive = TRUE;
2189         pduSent = send_multicast_frames(priv,interfaceTag);
2190     }
2191     else {
2192         unifi_error(priv,"Interface switching is not supported %d\n",interfaceTag);
2193         resultCode = CSR_RC_NOT_SUPPORTED;
2194         send_vif_availibility_rsp(priv,ind->VirtualInterfaceIdentifier,CSR_RC_NOT_SUPPORTED);
2195     }
2196 #endif
2197 }
2198 #ifdef CSR_SUPPORT_SME
2199
2200 #define  GET_ACTIVE_INTERFACE_TAG(priv) 0
2201
2202 static u8 uf_is_more_data_for_delivery_ac(unifi_priv_t *priv, CsrWifiRouterCtrlStaInfo_t *staRecord)
2203 {
2204     s8 i;
2205
2206     for(i=UNIFI_TRAFFIC_Q_VO; i >= UNIFI_TRAFFIC_Q_BK; i--)
2207     {
2208         if(((staRecord->powersaveMode[i]==CSR_WIFI_AC_DELIVERY_ONLY_ENABLE)
2209              ||(staRecord->powersaveMode[i]==CSR_WIFI_AC_TRIGGER_AND_DELIVERY_ENABLED))
2210              &&(!list_empty(&staRecord->dataPdu[i]))) {
2211             unifi_trace(priv,UDBG2,"uf_is_more_data_for_delivery_ac: Data Available AC = %d\n", i);
2212             return TRUE;
2213         }
2214     }
2215
2216     unifi_trace(priv,UDBG2,"uf_is_more_data_for_delivery_ac: Data NOT Available \n");
2217     return FALSE;
2218 }
2219
2220 static u8 uf_is_more_data_for_usp_delivery(unifi_priv_t *priv, CsrWifiRouterCtrlStaInfo_t *staRecord, unifi_TrafficQueue queue)
2221 {
2222     s8 i;
2223
2224     for(i = queue; i >= UNIFI_TRAFFIC_Q_BK; i--)
2225     {
2226         if(((staRecord->powersaveMode[i]==CSR_WIFI_AC_DELIVERY_ONLY_ENABLE)
2227              ||(staRecord->powersaveMode[i]==CSR_WIFI_AC_TRIGGER_AND_DELIVERY_ENABLED))
2228              &&(!list_empty(&staRecord->dataPdu[i]))) {
2229             unifi_trace(priv,UDBG2,"uf_is_more_data_for_usp_delivery: Data Available AC = %d\n", i);
2230             return TRUE;
2231         }
2232     }
2233
2234     unifi_trace(priv,UDBG2,"uf_is_more_data_for_usp_delivery: Data NOT Available \n");
2235     return FALSE;
2236 }
2237
2238 /*
2239  * ---------------------------------------------------------------------------
2240  *  uf_send_buffered_data_from_delivery_ac
2241  *
2242  *      This function takes care of
2243  *      -> Parsing the delivery enabled queue & sending frame down to HIP
2244  *      -> Setting EOSP=1 when USP to be terminated
2245  *      -> Depending on MAX SP length services the USP
2246  *
2247  * NOTE:This function always called from uf_handle_uspframes_delivery(), Dont
2248  *      call this function from any other location in code
2249  *
2250  *  Arguments:
2251  *      priv        Pointer to device private context struct
2252  *      vif         interface specific HIP vif instance
2253  *      staInfo     peer for which UAPSD to be scheduled
2254  *      queue       AC from which Data to be sent in USP
2255  *      txList      access category for processing list
2256  * ---------------------------------------------------------------------------
2257  */
2258 void uf_send_buffered_data_from_delivery_ac(unifi_priv_t *priv,
2259                                             CsrWifiRouterCtrlStaInfo_t * staInfo,
2260                                             u8 queue,
2261                                             struct list_head *txList)
2262 {
2263
2264     u16 interfaceTag = GET_ACTIVE_INTERFACE_TAG(priv);
2265     tx_buffered_packets_t * buffered_pkt = NULL;
2266     unsigned long lock_flags;
2267     u8 eosp=FALSE;
2268     s8 r =0;
2269     u8 moreData = FALSE;
2270     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2271
2272     unifi_trace(priv, UDBG2, "++uf_send_buffered_data_from_delivery_ac, active=%x\n", staInfo->uapsdActive);
2273
2274     if (queue > UNIFI_TRAFFIC_Q_VO)
2275     {
2276         return;
2277     }
2278     while((buffered_pkt=dequeue_tx_data_pdu(priv, txList))) {
2279         if((IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag))) {
2280             unifi_trace(priv, UDBG2, "uf_send_buffered_data_from_delivery_ac: DTIM Active, suspend UAPSD, staId: 0x%x\n",
2281                         staInfo->aid);
2282
2283             /* Once resume called, the U-APSD delivery operation will resume */
2284             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2285             staInfo->uspSuspend = TRUE;
2286             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2287             /* re-queueing the packet as DTIM started */
2288             spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
2289             list_add(&buffered_pkt->q,txList);
2290             spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
2291             break;
2292         }
2293
2294         buffered_pkt->transmissionControl &=
2295                  ~(TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
2296
2297
2298         if((staInfo->wmmOrQosEnabled == TRUE)&&(staInfo->uapsdActive == TRUE)) {
2299
2300              buffered_pkt->transmissionControl = TRANSMISSION_CONTROL_TRIGGER_MASK;
2301
2302              /* Check All delivery enables Ac for more data, because caller of this
2303               * function not aware about last packet
2304               * (First check in moreData fetching helps in draining out Mgt frames Q)
2305               */
2306               moreData = (!list_empty(txList) || uf_is_more_data_for_usp_delivery(priv, staInfo, queue));
2307
2308               if(staInfo->noOfSpFramesSent == (staInfo->maxSpLength - 1)) {
2309                   moreData = FALSE;
2310               }
2311
2312               if(moreData == FALSE) {
2313                   eosp = TRUE;
2314                   buffered_pkt->transmissionControl =
2315                       (TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
2316               }
2317         } else {
2318             /* Non QoS and non U-APSD */
2319             unifi_warning(priv, "uf_send_buffered_data_from_delivery_ac: non U-APSD !!! \n");
2320         }
2321
2322         unifi_trace(priv,UDBG2,"uf_send_buffered_data_from_delivery_ac : MoreData:%d, EOSP:%d\n",moreData,eosp);
2323
2324         if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staInfo,moreData,eosp)) == -ENOSPC) {
2325
2326             unifi_trace(priv, UDBG2, "uf_send_buffered_data_from_delivery_ac: UASPD suspended, ENOSPC in hipQ=%x\n", queue);
2327
2328             /* Once resume called, the U-APSD delivery operation will resume */
2329             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2330             staInfo->uspSuspend = TRUE;
2331             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2332
2333             spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
2334             list_add(&buffered_pkt->q,txList);
2335             spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
2336             priv->pausedStaHandle[queue]=(u8)(staInfo->assignedHandle);
2337             break;
2338         } else {
2339             if(r){
2340                 /* the PDU failed where we can't do any thing so free the storage */
2341                 unifi_net_data_free(priv, &buffered_pkt->bulkdata);
2342             }
2343             kfree(buffered_pkt);
2344             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2345             staInfo->noOfSpFramesSent++;
2346             if((!moreData) || (staInfo->noOfSpFramesSent == staInfo->maxSpLength)) {
2347                 unifi_trace(priv, UDBG2, "uf_send_buffered_data_from_delivery_ac: Terminating USP\n");
2348                 staInfo->uapsdActive = FALSE;
2349                 staInfo->uspSuspend = FALSE;
2350                 staInfo->noOfSpFramesSent = 0;
2351                 spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2352                 break;
2353             }
2354             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2355         }
2356     }
2357     unifi_trace(priv, UDBG2, "--uf_send_buffered_data_from_delivery_ac, active=%x\n", staInfo->uapsdActive);
2358 }
2359
2360 void uf_send_buffered_data_from_ac(unifi_priv_t *priv,
2361                                    CsrWifiRouterCtrlStaInfo_t * staInfo,
2362                                    u8 queue,
2363                                    struct list_head *txList)
2364 {
2365     tx_buffered_packets_t * buffered_pkt = NULL;
2366     unsigned long lock_flags;
2367     u8 eosp=FALSE;
2368     u8 moreData = FALSE;
2369     s8 r =0;
2370
2371     func_enter();
2372
2373     unifi_trace(priv,UDBG2,"uf_send_buffered_data_from_ac :\n");
2374
2375     while(!isRouterBufferEnabled(priv,queue) &&
2376                     ((buffered_pkt=dequeue_tx_data_pdu(priv, txList))!=NULL)){
2377
2378         buffered_pkt->transmissionControl &=
2379                  ~(TRANSMISSION_CONTROL_TRIGGER_MASK|TRANSMISSION_CONTROL_EOSP_MASK);
2380
2381         unifi_trace(priv,UDBG3,"uf_send_buffered_data_from_ac : MoreData:%d, EOSP:%d\n",moreData,eosp);
2382
2383         if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staInfo,moreData,eosp)) == -ENOSPC) {
2384            /* Enqueue at the head of the queue */
2385            spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
2386            list_add(&buffered_pkt->q,txList);
2387            spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
2388            if(staInfo != NULL){
2389               priv->pausedStaHandle[queue]=(u8)(staInfo->assignedHandle);
2390            }
2391            unifi_trace(priv,UDBG3," uf_send_buffered_data_from_ac: PDU sending failed .. no space for queue %d \n",queue);
2392            } else {
2393             if(r){
2394                 /* the PDU failed where we can't do any thing so free the storage */
2395                 unifi_net_data_free(priv, &buffered_pkt->bulkdata);
2396             }
2397             kfree(buffered_pkt);
2398       }
2399   }
2400
2401   func_exit();
2402
2403 }
2404
2405 void uf_send_buffered_frames(unifi_priv_t *priv,unifi_TrafficQueue q)
2406 {
2407     u16 interfaceTag = GET_ACTIVE_INTERFACE_TAG(priv);
2408     u32 startIndex=0,endIndex=0;
2409     CsrWifiRouterCtrlStaInfo_t * staInfo = NULL;
2410     u8 queue;
2411     u8 moreData = FALSE;
2412
2413     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2414
2415     if(!((interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_AP) ||
2416         (interfacePriv->interfaceMode == CSR_WIFI_ROUTER_CTRL_MODE_P2PGO)))
2417         return;
2418     func_enter();
2419
2420     queue = (q<=3)?q:0;
2421
2422     if(interfacePriv->dtimActive) {
2423         /* this function updates dtimActive*/
2424         send_multicast_frames(priv,interfaceTag);
2425         if(!interfacePriv->dtimActive) {
2426             moreData = (!list_empty(&interfacePriv->genericMulticastOrBroadCastMgtFrames) ||
2427              !list_empty(&interfacePriv->genericMulticastOrBroadCastFrames));
2428             if(!moreData) {
2429                 if (!interfacePriv->bcTimSetReqPendingFlag)
2430                 {
2431                     update_tim(priv,0,CSR_WIFI_TIM_RESET,interfaceTag, 0XFFFFFFFF);
2432                 }
2433                 else
2434                 {
2435                     /* Cache the TimSet value so that it will processed immidiatly after
2436                      * completing the current setTim Request
2437                      */
2438                     interfacePriv->bcTimSetReqQueued = CSR_WIFI_TIM_RESET;
2439                     unifi_trace(priv, UDBG2, "uf_send_buffered_frames : One more UpdateDTim Request(%d) Queued \n",
2440                                 interfacePriv->bcTimSetReqQueued);
2441                 }
2442             }
2443         } else {
2444             moreData = (!list_empty(&interfacePriv->genericMulticastOrBroadCastMgtFrames) ||
2445                         !list_empty(&interfacePriv->genericMulticastOrBroadCastFrames));
2446            if(!moreData) {
2447                /* This should never happen but if it happens, we need a way out */
2448                unifi_error(priv,"ERROR: No More Data but DTIM is active sending Response\n");
2449                send_vif_availibility_rsp(priv,uf_get_vif_identifier(interfacePriv->interfaceMode,interfaceTag),CSR_RC_NO_BUFFERED_BROADCAST_MULTICAST_FRAMES);
2450                interfacePriv->dtimActive = FALSE;
2451            }
2452         }
2453         func_exit();
2454         return;
2455     }
2456     if(priv->pausedStaHandle[queue] > 7) {
2457         priv->pausedStaHandle[queue] = 0;
2458     }
2459
2460     if(queue == UNIFI_TRAFFIC_Q_VO) {
2461
2462
2463         unifi_trace(priv,UDBG2,"uf_send_buffered_frames : trying mgt from queue=%d\n",queue);
2464         for(startIndex= 0; startIndex < UNIFI_MAX_CONNECTIONS;startIndex++) {
2465             staInfo =  CsrWifiRouterCtrlGetStationRecordFromHandle(priv,startIndex,interfaceTag);
2466             if(!staInfo ) {
2467                 continue;
2468             } else if((staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE)
2469                        &&(staInfo->uapsdActive == FALSE) ) {
2470                 continue;
2471             }
2472
2473             if((staInfo != NULL)&&(staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE)
2474                                &&(staInfo->uapsdActive == FALSE)){
2475                             /*Non-UAPSD case push the management frames out*/
2476                if(!list_empty(&staInfo->mgtFrames)){
2477                     uf_send_buffered_data_from_ac(priv,staInfo, UNIFI_TRAFFIC_Q_VO, &staInfo->mgtFrames);
2478                 }
2479             }
2480
2481             if(isRouterBufferEnabled(priv,queue)) {
2482                 unifi_notice(priv,"uf_send_buffered_frames : No space Left for queue = %d\n",queue);
2483                 break;
2484             }
2485         }
2486         /*push generic management frames out*/
2487         if(!list_empty(&interfacePriv->genericMgtFrames)) {
2488             unifi_trace(priv,UDBG2,"uf_send_buffered_frames : trying generic mgt from queue=%d\n",queue);
2489             uf_send_buffered_data_from_ac(priv,staInfo, UNIFI_TRAFFIC_Q_VO, &interfacePriv->genericMgtFrames);
2490         }
2491     }
2492
2493
2494     unifi_trace(priv,UDBG2,"uf_send_buffered_frames : Resume called for Queue=%d\n",queue);
2495     unifi_trace(priv,UDBG2,"uf_send_buffered_frames : start=%d end=%d\n",startIndex,endIndex);
2496
2497     startIndex = priv->pausedStaHandle[queue];
2498     endIndex = (startIndex + UNIFI_MAX_CONNECTIONS -1) % UNIFI_MAX_CONNECTIONS;
2499
2500     while(startIndex != endIndex) {
2501         staInfo =  CsrWifiRouterCtrlGetStationRecordFromHandle(priv,startIndex,interfaceTag);
2502         if(!staInfo) {
2503             startIndex ++;
2504             if(startIndex >= UNIFI_MAX_CONNECTIONS) {
2505                 startIndex = 0;
2506             }
2507             continue;
2508         } else if((staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE)
2509                    &&(staInfo->uapsdActive == FALSE)) {
2510             startIndex ++;
2511             if(startIndex >= UNIFI_MAX_CONNECTIONS) {
2512                 startIndex = 0;
2513             }
2514             continue;
2515         }
2516         /* Peer is active or U-APSD is active so send PDUs to the peer */
2517         unifi_trace(priv,UDBG2,"uf_send_buffered_frames : trying data from queue=%d\n",queue);
2518
2519
2520         if((staInfo != NULL)&&(staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE)
2521                            &&(staInfo->uapsdActive == FALSE)) {
2522            if(!list_empty(&staInfo->dataPdu[queue])) {
2523
2524                /*Non-UAPSD case push the AC frames out*/
2525                uf_send_buffered_data_from_ac(priv, staInfo, queue, (&staInfo->dataPdu[queue]));
2526            }
2527         }
2528         startIndex ++;
2529         if(startIndex >= UNIFI_MAX_CONNECTIONS) {
2530            startIndex = 0;
2531         }
2532     }
2533     if(isRouterBufferEnabled(priv,queue)) {
2534         priv->pausedStaHandle[queue] = endIndex;
2535     } else {
2536         priv->pausedStaHandle[queue] = 0;
2537     }
2538
2539     /* U-APSD might have stopped because of ENOSPC in lib_hip (pause activity).
2540      * So restart it if U-APSD was active with any of the station
2541      */
2542     unifi_trace(priv, UDBG4, "csrWifiHipSendBufferedFrames: UAPSD Resume Q=%x\n", queue);
2543     resume_suspended_uapsd(priv, interfaceTag);
2544     func_exit();
2545 }
2546
2547
2548 u8 uf_is_more_data_for_non_delivery_ac(CsrWifiRouterCtrlStaInfo_t *staRecord)
2549 {
2550     u8 i;
2551
2552     for(i=0;i<=3;i++)
2553     {
2554         if(((staRecord->powersaveMode[i]==CSR_WIFI_AC_TRIGGER_ONLY_ENABLED)
2555                 ||(staRecord->powersaveMode[i]==CSR_WIFI_AC_LEGACY_POWER_SAVE))
2556                 &&(!list_empty(&staRecord->dataPdu[i]))){
2557
2558          return TRUE;
2559         }
2560     }
2561
2562     if(((staRecord->powersaveMode[UNIFI_TRAFFIC_Q_VO]==CSR_WIFI_AC_TRIGGER_ONLY_ENABLED)
2563             ||(staRecord->powersaveMode[UNIFI_TRAFFIC_Q_VO]==CSR_WIFI_AC_LEGACY_POWER_SAVE))
2564             &&(!list_empty(&staRecord->mgtFrames))){
2565
2566      return TRUE;
2567     }
2568
2569
2570
2571     return FALSE;
2572 }
2573
2574
2575 int uf_process_station_records_for_sending_data(unifi_priv_t *priv,u16 interfaceTag,
2576                                                  CsrWifiRouterCtrlStaInfo_t *srcStaInfo,
2577                                                  CsrWifiRouterCtrlStaInfo_t *dstStaInfo)
2578 {
2579     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2580
2581     unifi_trace(priv, UDBG5, "entering uf_process_station_records_for_sending_data\n");
2582
2583     if (srcStaInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_DISCONNECTED) {
2584         unifi_error(priv, "Peer State not connected AID = %x, handle = %x, control port state = %x\n",
2585                     srcStaInfo->aid, srcStaInfo->assignedHandle, srcStaInfo->peerControlledPort->port_action);
2586         return -1;
2587     }
2588     switch (interfacePriv->interfaceMode)
2589     {
2590         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
2591         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
2592             unifi_trace(priv, UDBG5, "mode is AP/P2PGO\n");
2593             break;
2594         default:
2595             unifi_warning(priv, "mode is nor AP neither P2PGO, packet cant be xmit\n");
2596             return -1;
2597     }
2598
2599     switch(dstStaInfo->peerControlledPort->port_action)
2600     {
2601         case CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_CLOSED_DISCARD:
2602         case CSR_WIFI_ROUTER_CTRL_PORT_ACTION_8021X_PORT_CLOSED_BLOCK:
2603             unifi_trace(priv, UDBG5, "destination port is closed/blocked, discarding the packet\n");
2604             return -1;
2605         default:
2606             unifi_trace(priv, UDBG5, "destination port state is open\n");
2607     }
2608
2609     /* port state is open, destination station record is valid, Power save state is
2610      * validated in uf_process_ma_packet_req function
2611      */
2612     unifi_trace(priv, UDBG5, "leaving uf_process_station_records_for_sending_data\n");
2613     return 0;
2614 }
2615
2616
2617 /*
2618  * ---------------------------------------------------------------------------
2619  *  uf_handle_uspframes_delivery
2620  *
2621  *      This function takes care of handling USP session for peer, when
2622  *      -> trigger frame from peer
2623  *      -> suspended USP to be processed (resumed)
2624  *
2625  *      NOTE: uf_send_buffered_data_from_delivery_ac() always called from this function, Dont
2626  *      make a direct call to uf_send_buffered_data_from_delivery_ac() from any other part of
2627  *      code
2628  *
2629  *  Arguments:
2630  *      priv            Pointer to device private context struct
2631  *      staInfo         peer for which UAPSD to be scheduled
2632  *      interfaceTag    virtual interface tag
2633  * ---------------------------------------------------------------------------
2634  */
2635 static void uf_handle_uspframes_delivery(unifi_priv_t * priv, CsrWifiRouterCtrlStaInfo_t *staInfo, u16 interfaceTag)
2636 {
2637
2638     s8 i;
2639     u8 allDeliveryEnabled = 0, dataAvailable = 0;
2640     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2641     unsigned long lock_flags;
2642
2643     unifi_trace(priv, UDBG2, " ++ uf_handle_uspframes_delivery, uapsd active=%x, suspended?=%x\n",
2644                 staInfo->uapsdActive, staInfo->uspSuspend);
2645
2646     /* Check for Buffered frames according to priority order & deliver it
2647      *  1. AC_VO delivery enable & Mgt frames available
2648      *  2. Process remaining Ac's from order AC_VO to AC_BK
2649      */
2650
2651     /* USP initiated by WMMPS enabled peer  & SET the status flag to TRUE */
2652     if (!staInfo->uspSuspend && staInfo->uapsdActive)
2653     {
2654         unifi_notice(priv, "uf_handle_uspframes_delivery: U-APSD already active! STA=%x:%x:%x:%x:%x:%x\n",
2655                 staInfo->peerMacAddress.a[0], staInfo->peerMacAddress.a[1],
2656                 staInfo->peerMacAddress.a[2], staInfo->peerMacAddress.a[3],
2657                 staInfo->peerMacAddress.a[4], staInfo->peerMacAddress.a[5]);
2658         return;
2659     }
2660
2661     spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2662     staInfo->uapsdActive = TRUE;
2663     staInfo->uspSuspend = FALSE;
2664     spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2665
2666     if(((staInfo->powersaveMode[UNIFI_TRAFFIC_Q_VO]==CSR_WIFI_AC_TRIGGER_AND_DELIVERY_ENABLED)||
2667         (staInfo->powersaveMode[UNIFI_TRAFFIC_Q_VO]==CSR_WIFI_AC_DELIVERY_ONLY_ENABLE))
2668         && (!list_empty(&staInfo->mgtFrames))) {
2669
2670          /* Management queue has data &&  UNIFI_TRAFFIC_Q_VO is delivery enable */
2671         unifi_trace(priv, UDBG4, "uf_handle_uspframes_delivery: Sending buffered management frames\n");
2672         uf_send_buffered_data_from_delivery_ac(priv, staInfo, UNIFI_TRAFFIC_Q_VO, &staInfo->mgtFrames);
2673     }
2674
2675     if (!uf_is_more_data_for_delivery_ac(priv, staInfo)) {
2676         /* All delivery enable AC's are empty, so QNULL to be sent to terminate the USP
2677          * NOTE: If we have sent Mgt frame also, we must send QNULL followed to terminate USP
2678          */
2679         if (!staInfo->uspSuspend) {
2680             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2681             staInfo->uapsdActive = FALSE;
2682             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2683
2684             unifi_trace(priv, UDBG2, "uf_handle_uspframes_delivery: sending QNull for trigger\n");
2685             uf_send_qos_null(priv, interfaceTag, staInfo->peerMacAddress.a, (CSR_PRIORITY) staInfo->triggerFramePriority, staInfo);
2686             staInfo->triggerFramePriority = CSR_QOS_UP0;
2687         } else {
2688             unifi_trace(priv, UDBG2, "uf_handle_uspframes_delivery: MgtQ xfer suspended\n");
2689         }
2690     } else {
2691         for(i = UNIFI_TRAFFIC_Q_VO; i >= UNIFI_TRAFFIC_Q_BK; i--) {
2692             if(((staInfo->powersaveMode[i]==CSR_WIFI_AC_DELIVERY_ONLY_ENABLE)
2693                 ||(staInfo->powersaveMode[i]==CSR_WIFI_AC_TRIGGER_AND_DELIVERY_ENABLED))
2694                 && (!list_empty(&staInfo->dataPdu[i]))) {
2695                 /* Deliver Data according to AC priority (from VO to BK) as part of USP */
2696                 unifi_trace(priv, UDBG4, "uf_handle_uspframes_delivery: Buffered data frames from Queue (%d) for USP\n", i);
2697                 uf_send_buffered_data_from_delivery_ac(priv, staInfo, i, &staInfo->dataPdu[i]);
2698             }
2699
2700             if ((!staInfo->uapsdActive) ||
2701                     (staInfo->uspSuspend && IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag))) {
2702                 /* If DTIM active found on one AC, No need to parse the remaining AC's
2703                  * as USP suspended. Break out of loop
2704                  */
2705                 unifi_trace(priv, UDBG2, "uf_handle_uspframes_delivery: suspend=%x,  DTIM=%x, USP terminated=%s\n",
2706                            staInfo->uspSuspend, IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag),
2707                            staInfo->uapsdActive?"NO":"YES");
2708                 break;
2709             }
2710         }
2711     }
2712
2713     /* Depending on the USP status, update the TIM accordingly for delivery enabled AC only
2714      * (since we are not manipulating any Non-delivery list(AC))
2715      */
2716     is_all_ac_deliver_enabled_and_moredata(staInfo, &allDeliveryEnabled, &dataAvailable);
2717     if ((allDeliveryEnabled && !dataAvailable)) {
2718         if ((staInfo->timSet != CSR_WIFI_TIM_RESET) || (staInfo->timSet != CSR_WIFI_TIM_RESETTING)) {
2719             staInfo->updateTimReqQueued = (u8) CSR_WIFI_TIM_RESET;
2720             unifi_trace(priv, UDBG4, " --uf_handle_uspframes_delivery, UAPSD timset\n");
2721             if (!staInfo->timRequestPendingFlag) {
2722                 update_tim(priv, staInfo->aid, 0, interfaceTag, staInfo->assignedHandle);
2723             }
2724         }
2725     }
2726     unifi_trace(priv, UDBG2, " --uf_handle_uspframes_delivery, uapsd active=%x, suspend?=%x\n",
2727                 staInfo->uapsdActive, staInfo->uspSuspend);
2728 }
2729
2730 void uf_process_wmm_deliver_ac_uapsd(unifi_priv_t * priv,
2731                                      CsrWifiRouterCtrlStaInfo_t * srcStaInfo,
2732                                      u16 qosControl,
2733                                      u16 interfaceTag)
2734 {
2735     CSR_PRIORITY priority;
2736     unifi_TrafficQueue priority_q;
2737     unsigned long lock_flags;
2738
2739     unifi_trace(priv, UDBG2, "++uf_process_wmm_deliver_ac_uapsd: uapsdactive?=%x\n", srcStaInfo->uapsdActive);
2740     /* If recceived Frames trigger Frame and Devlivery enabled AC has data
2741      * then transmit from High priorty delivery enabled AC
2742      */
2743     priority = (CSR_PRIORITY)(qosControl & IEEE802_11_QC_TID_MASK);
2744     priority_q = unifi_frame_priority_to_queue((CSR_PRIORITY) priority);
2745
2746     if((srcStaInfo->powersaveMode[priority_q]==CSR_WIFI_AC_TRIGGER_ONLY_ENABLED)
2747         ||(srcStaInfo->powersaveMode[priority_q]==CSR_WIFI_AC_TRIGGER_AND_DELIVERY_ENABLED)) {
2748         spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2749         srcStaInfo->triggerFramePriority = priority;
2750         spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2751         unifi_trace(priv, UDBG2, "uf_process_wmm_deliver_ac_uapsd: trigger frame, Begin U-APSD, triggerQ=%x\n", priority_q);
2752         uf_handle_uspframes_delivery(priv, srcStaInfo, interfaceTag);
2753     }
2754     unifi_trace(priv, UDBG2, "--uf_process_wmm_deliver_ac_uapsd: uapsdactive?=%x\n", srcStaInfo->uapsdActive);
2755 }
2756
2757
2758 void uf_send_qos_null(unifi_priv_t * priv,u16 interfaceTag, const u8 *da,CSR_PRIORITY priority,CsrWifiRouterCtrlStaInfo_t * srcStaInfo)
2759 {
2760     bulk_data_param_t bulkdata;
2761     CsrResult csrResult;
2762     struct sk_buff *skb, *newSkb = NULL;
2763     CsrWifiMacAddress peerAddress;
2764     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2765     CSR_TRANSMISSION_CONTROL transmissionControl = (TRANSMISSION_CONTROL_EOSP_MASK | TRANSMISSION_CONTROL_TRIGGER_MASK);
2766     int r;
2767     CSR_SIGNAL signal;
2768     u32 priority_q;
2769     CSR_RATE transmitRate = 0;
2770
2771
2772     func_enter();
2773     /* Send a Null Frame to Peer,
2774      * 32= size of mac header  */
2775     csrResult = unifi_net_data_malloc(priv, &bulkdata.d[0], MAC_HEADER_SIZE + QOS_CONTROL_HEADER_SIZE);
2776
2777     if (csrResult != CSR_RESULT_SUCCESS) {
2778         unifi_error(priv, " failed to allocate request_data. in uf_send_qos_null func\n");
2779         return ;
2780     }
2781     skb = (struct sk_buff *)(bulkdata.d[0].os_net_buf_ptr);
2782     skb->len = 0;
2783     bulkdata.d[0].os_data_ptr = skb->data;
2784     bulkdata.d[0].os_net_buf_ptr = (unsigned char*)skb;
2785     bulkdata.d[0].net_buf_length = bulkdata.d[0].data_length = skb->len;
2786     bulkdata.d[1].os_data_ptr = NULL;
2787     bulkdata.d[1].os_net_buf_ptr = NULL;
2788     bulkdata.d[1].net_buf_length = bulkdata.d[1].data_length = 0;
2789
2790     /* For null frames protection bit should not be set in MAC header, so passing value 0 below for protection field */
2791
2792     if (prepare_and_add_macheader(priv, skb, newSkb, priority, &bulkdata, interfaceTag, da, interfacePriv->bssid.a, 0)) {
2793         unifi_error(priv, "failed to create MAC header\n");
2794         unifi_net_data_free(priv, &bulkdata.d[0]);
2795         return;
2796     }
2797     memcpy(peerAddress.a, ((u8 *) bulkdata.d[0].os_data_ptr) + 4, ETH_ALEN);
2798     /* convert priority to queue */
2799     priority_q = unifi_frame_priority_to_queue((CSR_PRIORITY) priority);
2800
2801     /* Frame ma-packet.req, this is saved/transmitted depend on queue state
2802      * send the null frame at data rate of 1 Mb/s for AP or 6 Mb/s for P2PGO
2803      */
2804     switch (interfacePriv->interfaceMode)
2805     {
2806         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
2807             transmitRate = 2;
2808             break;
2809         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
2810             transmitRate = 12;
2811             break;
2812         default:
2813             transmitRate = 0;
2814     }
2815     unifi_frame_ma_packet_req(priv, priority, transmitRate, 0xffffffff, interfaceTag,
2816                               transmissionControl, priv->netdev_client->sender_id,
2817                               peerAddress.a, &signal);
2818
2819     r = ul_send_signal_unpacked(priv, &signal, &bulkdata);
2820     if(r) {
2821         unifi_error(priv, "failed to send QOS data null packet result: %d\n",r);
2822         unifi_net_data_free(priv, &bulkdata.d[0]);
2823     }
2824
2825     func_exit();
2826     return;
2827
2828 }
2829 void uf_send_nulldata(unifi_priv_t * priv,u16 interfaceTag, const u8 *da,CSR_PRIORITY priority,CsrWifiRouterCtrlStaInfo_t * srcStaInfo)
2830 {
2831     bulk_data_param_t bulkdata;
2832     CsrResult csrResult;
2833     struct sk_buff *skb, *newSkb = NULL;
2834     CsrWifiMacAddress peerAddress;
2835     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
2836     CSR_TRANSMISSION_CONTROL transmissionControl = 0;
2837     int r;
2838     CSR_SIGNAL signal;
2839     u32 priority_q;
2840     CSR_RATE transmitRate = 0;
2841     CSR_MA_PACKET_REQUEST *req = &signal.u.MaPacketRequest;
2842     unsigned long lock_flags;
2843
2844     func_enter();
2845     /* Send a Null Frame to Peer, size = 24 for MAC header */
2846     csrResult = unifi_net_data_malloc(priv, &bulkdata.d[0], MAC_HEADER_SIZE);
2847
2848     if (csrResult != CSR_RESULT_SUCCESS) {
2849         unifi_error(priv, "uf_send_nulldata: Failed to allocate memory for NULL frame\n");
2850         return ;
2851     }
2852     skb = (struct sk_buff *)(bulkdata.d[0].os_net_buf_ptr);
2853     skb->len = 0;
2854     bulkdata.d[0].os_data_ptr = skb->data;
2855     bulkdata.d[0].os_net_buf_ptr = (unsigned char*)skb;
2856     bulkdata.d[0].net_buf_length = bulkdata.d[0].data_length = skb->len;
2857     bulkdata.d[1].os_data_ptr = NULL;
2858     bulkdata.d[1].os_net_buf_ptr = NULL;
2859     bulkdata.d[1].net_buf_length = bulkdata.d[1].data_length = 0;
2860
2861     /* For null frames protection bit should not be set in MAC header, so passing value 0 below for protection field */
2862     if (prepare_and_add_macheader(priv, skb, newSkb, priority, &bulkdata, interfaceTag, da, interfacePriv->bssid.a, 0)) {
2863         unifi_error(priv, "uf_send_nulldata: Failed to create MAC header\n");
2864         unifi_net_data_free(priv, &bulkdata.d[0]);
2865         return;
2866     }
2867     memcpy(peerAddress.a, ((u8 *) bulkdata.d[0].os_data_ptr) + 4, ETH_ALEN);
2868     /* convert priority to queue */
2869     priority_q = unifi_frame_priority_to_queue((CSR_PRIORITY) priority);
2870     transmissionControl &= ~(CSR_NO_CONFIRM_REQUIRED);
2871
2872     /* Frame ma-packet.req, this is saved/transmitted depend on queue state
2873      * send the null frame at data rate of 1 Mb/s for AP or 6 Mb/s for P2PGO
2874      */
2875     switch (interfacePriv->interfaceMode)
2876     {
2877         case CSR_WIFI_ROUTER_CTRL_MODE_AP:
2878             transmitRate = 2;
2879             break;
2880         case CSR_WIFI_ROUTER_CTRL_MODE_P2PGO:
2881             transmitRate = 12;
2882             break;
2883         default:
2884             transmitRate = 0;
2885     }
2886     unifi_frame_ma_packet_req(priv, priority, transmitRate, INVALID_HOST_TAG, interfaceTag,
2887                               transmissionControl, priv->netdev_client->sender_id,
2888                               peerAddress.a, &signal);
2889
2890     /* Save host tag to check the status on reception of MA packet confirm */
2891     srcStaInfo->nullDataHostTag = req->HostTag;
2892     unifi_trace(priv, UDBG1, "uf_send_nulldata: STA AID = %d hostTag = %x\n", srcStaInfo->aid, req->HostTag);
2893
2894     r = ul_send_signal_unpacked(priv, &signal, &bulkdata);
2895
2896     if(r == -ENOSPC) {
2897         unifi_trace(priv, UDBG1, "uf_send_nulldata: ENOSPC Requeue the Null frame\n");
2898         enque_tx_data_pdu(priv, &bulkdata, &srcStaInfo->dataPdu[priority_q], &signal, 1);
2899         spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2900         srcStaInfo->noOfPktQueued++;
2901         spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2902
2903
2904     }
2905     if(r && r != -ENOSPC){
2906         unifi_error(priv, "uf_send_nulldata: Failed to send Null frame Error = %d\n",r);
2907         unifi_net_data_free(priv, &bulkdata.d[0]);
2908         srcStaInfo->nullDataHostTag = INVALID_HOST_TAG;
2909     }
2910
2911     func_exit();
2912     return;
2913 }
2914
2915 u8 uf_check_broadcast_bssid(unifi_priv_t *priv, const bulk_data_param_t *bulkdata)
2916 {
2917     u8 *bssid = NULL;
2918     static const CsrWifiMacAddress broadcast_address = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
2919     u8 toDs, fromDs;
2920
2921     toDs = (((bulkdata->d[0].os_data_ptr)[1]) & 0x01) ? 1 : 0;
2922     fromDs =(((bulkdata->d[0].os_data_ptr)[1]) & 0x02) ? 1 : 0;
2923
2924      if (toDs && fromDs)
2925     {
2926         unifi_trace(priv, UDBG6, "Address 4 present, Don't try to find BSSID\n");
2927         bssid = NULL;
2928     }
2929     else if((toDs == 0) && (fromDs ==0))
2930     {
2931         /* BSSID is Address 3 */
2932         bssid = (u8 *) (bulkdata->d[0].os_data_ptr + 4 + (2 * ETH_ALEN));
2933     }
2934     else if(toDs)
2935     {
2936         /* BSSID is Address 1 */
2937         bssid = (u8 *) (bulkdata->d[0].os_data_ptr + 4);
2938     }
2939     else if(fromDs)
2940     {
2941         /* BSSID is Address 2 */
2942         bssid = (u8 *) (bulkdata->d[0].os_data_ptr + 4 + ETH_ALEN);
2943     }
2944
2945     if (memcmp(broadcast_address.a, bssid, ETH_ALEN)== 0)
2946     {
2947         return TRUE;
2948     }
2949     else
2950     {
2951         return FALSE;
2952     }
2953 }
2954
2955
2956 u8 uf_process_pm_bit_for_peer(unifi_priv_t * priv, CsrWifiRouterCtrlStaInfo_t * srcStaInfo,
2957                                 u8 pmBit,u16 interfaceTag)
2958 {
2959     u8 moreData = FALSE;
2960     u8 powerSaveChanged = FALSE;
2961     unsigned long lock_flags;
2962
2963     unifi_trace(priv, UDBG3, "entering uf_process_pm_bit_for_peer\n");
2964     if (pmBit) {
2965         priv->allPeerDozing |= (0x01 << (srcStaInfo->assignedHandle));
2966     } else {
2967         priv->allPeerDozing &= ~(0x01 << (srcStaInfo->assignedHandle));
2968     }
2969     if(pmBit) {
2970         if(srcStaInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE) {
2971
2972             /* disable the preemption */
2973             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2974             srcStaInfo->currentPeerState =CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE;
2975             powerSaveChanged = TRUE;
2976             /* enable the preemption */
2977             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2978         } else {
2979             return powerSaveChanged;
2980         }
2981     } else {
2982         if(srcStaInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE) {
2983             /* disable the preemption */
2984             spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
2985             srcStaInfo->currentPeerState = CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE;
2986             powerSaveChanged = TRUE;
2987             /* enable the preemption */
2988             spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
2989         }else {
2990             return powerSaveChanged;
2991         }
2992     }
2993
2994
2995     if(srcStaInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE) {
2996         unifi_trace(priv,UDBG3, "Peer with AID = %d is active now\n",srcStaInfo->aid);
2997         process_peer_active_transition(priv,srcStaInfo,interfaceTag);
2998     } else {
2999         unifi_trace(priv,UDBG3, "Peer with AID = %d is in PS Now\n",srcStaInfo->aid);
3000         /* Set TIM if needed */
3001         if(!srcStaInfo->wmmOrQosEnabled) {
3002             moreData = (!list_empty(&srcStaInfo->mgtFrames) ||
3003                         !list_empty(&srcStaInfo->dataPdu[UNIFI_TRAFFIC_Q_VO])||
3004                         !list_empty(&srcStaInfo->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]));
3005             if(moreData && (srcStaInfo->timSet == CSR_WIFI_TIM_RESET)) {
3006                 unifi_trace(priv, UDBG3, "This condition should not occur\n");
3007                 if (!srcStaInfo->timRequestPendingFlag){
3008                     update_tim(priv,srcStaInfo->aid,1,interfaceTag, srcStaInfo->assignedHandle);
3009                 }
3010                 else
3011                 {
3012                     /* Cache the TimSet value so that it will processed immidiatly after
3013                      * completing the current setTim Request
3014                      */
3015                     srcStaInfo->updateTimReqQueued = 1;
3016                     unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", srcStaInfo->updateTimReqQueued,
3017                                 srcStaInfo->aid);
3018                 }
3019
3020             }
3021         } else {
3022             u8 allDeliveryEnabled = 0, dataAvailable = 0;
3023             unifi_trace(priv, UDBG5, "Qos in AP Mode\n");
3024             /* Check if all AC's are Delivery Enabled */
3025             is_all_ac_deliver_enabled_and_moredata(srcStaInfo, &allDeliveryEnabled, &dataAvailable);
3026             /*check for more data in non-delivery enabled queues*/
3027             moreData = (uf_is_more_data_for_non_delivery_ac(srcStaInfo) || (allDeliveryEnabled && dataAvailable));
3028
3029             if(moreData && (srcStaInfo->timSet == CSR_WIFI_TIM_RESET)) {
3030                 if (!srcStaInfo->timRequestPendingFlag){
3031                     update_tim(priv,srcStaInfo->aid,1,interfaceTag, srcStaInfo->assignedHandle);
3032                 }
3033                 else
3034                 {
3035                     /* Cache the TimSet value so that it will processed immidiatly after
3036                      * completing the current setTim Request
3037                      */
3038                     srcStaInfo->updateTimReqQueued = 1;
3039                     unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", srcStaInfo->updateTimReqQueued,
3040                                 srcStaInfo->aid);
3041                 }
3042             }
3043         }
3044     }
3045     unifi_trace(priv, UDBG3, "leaving uf_process_pm_bit_for_peer\n");
3046     return powerSaveChanged;
3047 }
3048
3049
3050
3051 void uf_process_ps_poll(unifi_priv_t *priv,u8* sa,u8* da,u8 pmBit,u16 interfaceTag)
3052 {
3053     CsrWifiRouterCtrlStaInfo_t *staRecord =
3054     CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv, sa, interfaceTag);
3055     tx_buffered_packets_t * buffered_pkt = NULL;
3056     CsrWifiMacAddress peerMacAddress;
3057     unsigned long lock_flags;
3058     s8 r =0;
3059     u8 moreData = FALSE;
3060     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
3061
3062     unifi_trace(priv, UDBG3, "entering uf_process_ps_poll\n");
3063     if(!staRecord) {
3064         memcpy(peerMacAddress.a,sa,ETH_ALEN);
3065         unifi_trace(priv, UDBG3, "In uf_process_ps_poll, sta record not found:unexpected frame addr = %x:%x:%x:%x:%x:%x\n",
3066                 sa[0], sa[1],sa[2], sa[3], sa[4],sa[5]);
3067         CsrWifiRouterCtrlUnexpectedFrameIndSend(priv->CSR_WIFI_SME_IFACEQUEUE,0,interfaceTag,peerMacAddress);
3068         return;
3069     }
3070
3071     uf_process_pm_bit_for_peer(priv,staRecord,pmBit,interfaceTag);
3072
3073     /* Update station last activity time */
3074     staRecord->activity_flag = TRUE;
3075
3076     /* This should not change the PM bit as PS-POLL has PM bit always set */
3077     if(!pmBit) {
3078         unifi_notice (priv," PM bit reset in PS-POLL\n");
3079         return;
3080     }
3081
3082     if(IS_DTIM_ACTIVE(interfacePriv->dtimActive,interfacePriv->multicastPduHostTag)) {
3083         /* giving more priority to multicast packets so dropping ps-poll*/
3084         unifi_notice (priv," multicast transmission is going on so don't take action on PS-POLL\n");
3085         return;
3086     }
3087
3088     if(!staRecord->wmmOrQosEnabled) {
3089         if((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->mgtFrames))) {
3090             buffered_pkt->transmissionControl |= TRANSMISSION_CONTROL_TRIGGER_MASK;
3091             moreData = (!list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]) ||
3092                         !list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_VO]) ||
3093                         !list_empty(&staRecord->mgtFrames));
3094
3095             buffered_pkt->transmissionControl |= (TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3096             if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,moreData,FALSE)) == -ENOSPC) {
3097                 /* Clear the trigger bit transmission control*/
3098                 buffered_pkt->transmissionControl &= ~(TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3099                 /* Enqueue at the head of the queue */
3100                 spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3101                 list_add(&buffered_pkt->q, &staRecord->mgtFrames);
3102                 spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3103                 unifi_trace(priv, UDBG1, "(ENOSPC) PS-POLL received : PDU sending failed \n");
3104                 priv->pausedStaHandle[3]=(u8)(staRecord->assignedHandle);
3105             } else {
3106                 if(r){
3107                     unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3108                     /* the PDU failed where we can't do any thing so free the storage */
3109                     unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3110                 }
3111                 kfree(buffered_pkt);
3112             }
3113         } else if((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->dataPdu[UNIFI_TRAFFIC_Q_VO]))) {
3114             buffered_pkt->transmissionControl |= TRANSMISSION_CONTROL_TRIGGER_MASK;
3115             moreData = (!list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]) ||
3116                         !list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_VO]));
3117
3118             buffered_pkt->transmissionControl |= (TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3119             if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,moreData,FALSE)) == -ENOSPC) {
3120                 /* Clear the trigger bit transmission control*/
3121                 buffered_pkt->transmissionControl &= ~(TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3122                 /* Enqueue at the head of the queue */
3123                 spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3124                 list_add(&buffered_pkt->q, &staRecord->dataPdu[UNIFI_TRAFFIC_Q_VO]);
3125                 spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3126                 priv->pausedStaHandle[3]=(u8)(staRecord->assignedHandle);
3127                 unifi_trace(priv, UDBG1, "(ENOSPC) PS-POLL received : PDU sending failed \n");
3128             } else {
3129                 if(r){
3130                     unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3131                     /* the PDU failed where we can't do any thing so free the storage */
3132                     unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3133                 }
3134                 kfree(buffered_pkt);
3135             }
3136         } else  if((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]))) {
3137             buffered_pkt->transmissionControl |= TRANSMISSION_CONTROL_TRIGGER_MASK;
3138             moreData = !list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]);
3139
3140             buffered_pkt->transmissionControl |= (TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3141             if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,moreData,FALSE)) == -ENOSPC) {
3142                 /* Clear the trigger bit transmission control*/
3143                 buffered_pkt->transmissionControl &= ~(TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3144                 /* Enqueue at the head of the queue */
3145                 spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3146                 list_add(&buffered_pkt->q, &staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]);
3147                 spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3148                 priv->pausedStaHandle[0]=(u8)(staRecord->assignedHandle);
3149                 unifi_trace(priv, UDBG1, "(ENOSPC) PS-POLL received : PDU sending failed \n");
3150             } else {
3151                 if(r){
3152                     unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3153                     /* the PDU failed where we can't do any thing so free the storage */
3154                     unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3155                 }
3156                 kfree(buffered_pkt);
3157             }
3158         } else {
3159          /* Actually since we have sent an ACK, there
3160          * there is no need to send a NULL frame*/
3161         }
3162         moreData = (!list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_VO]) ||
3163            !list_empty(&staRecord->dataPdu[UNIFI_TRAFFIC_Q_CONTENTION]) ||
3164             !list_empty(&staRecord->mgtFrames));
3165         if(!moreData && (staRecord->timSet == CSR_WIFI_TIM_SET)) {
3166             unifi_trace(priv, UDBG3, "more data = NULL, set tim to 0 in uf_process_ps_poll\n");
3167             if (!staRecord->timRequestPendingFlag){
3168                 update_tim(priv,staRecord->aid,0,interfaceTag, staRecord->assignedHandle);
3169             }
3170             else
3171             {
3172                 /* Cache the TimSet value so that it will processed immidiatly after
3173                  * completing the current setTim Request
3174                  */
3175                 staRecord->updateTimReqQueued = 0;
3176                 unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
3177                             staRecord->aid);
3178             }
3179         }
3180     } else {
3181
3182         u8 allDeliveryEnabled = 0, dataAvailable = 0;
3183         unifi_trace(priv, UDBG3,"Qos Support station.Processing PS-Poll\n");
3184
3185         /*Send Data From Management Frames*/
3186         /* Priority orders for delivering the buffered packets are
3187          * 1. Deliver the Management frames if there
3188          * 2. Other access catagory frames which are non deliver enable including UNIFI_TRAFFIC_Q_VO
3189          * priority is from VO->BK
3190          */
3191
3192         /* Check if all AC's are Delivery Enabled */
3193         is_all_ac_deliver_enabled_and_moredata(staRecord, &allDeliveryEnabled, &dataAvailable);
3194
3195         if (allDeliveryEnabled) {
3196             unifi_trace(priv, UDBG3, "uf_process_ps_poll: All ACs are delivery enable so Sending QOS Null in response of Ps-poll\n");
3197             uf_send_qos_null(priv,interfaceTag,sa,CSR_QOS_UP0,staRecord);
3198             return;
3199         }
3200
3201         if (!list_empty(&staRecord->mgtFrames)) {
3202              if ((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->mgtFrames))) {
3203                     /* We dont have packets in non delivery enabled UNIFI_TRAFFIC_Q_VO, So we are looking in management
3204                      * queue of the station record
3205                      */
3206                     moreData = uf_is_more_data_for_non_delivery_ac(staRecord);
3207                     buffered_pkt->transmissionControl |= (TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3208
3209                     /* Last parameter is EOSP & its false always for PS-POLL processing */
3210                     if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,moreData,FALSE)) == -ENOSPC) {
3211                         /* Clear the trigger bit transmission control*/
3212                         buffered_pkt->transmissionControl &= ~(TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3213                         /* Enqueue at the head of the queue */
3214                         spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3215                         list_add(&buffered_pkt->q, &staRecord->mgtFrames);
3216                         spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3217                         priv->pausedStaHandle[0]=(u8)(staRecord->assignedHandle);
3218                         unifi_trace(priv, UDBG1, "(ENOSPC) PS-POLL received : PDU sending failed \n");
3219                     } else {
3220                         if(r){
3221                             unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3222                             /* the PDU failed where we can't do any thing so free the storage */
3223                             unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3224                         }
3225                         kfree(buffered_pkt);
3226                     }
3227                 } else {
3228                     unifi_error(priv, "uf_process_ps_poll: Mgt frame list empty!! \n");
3229                 }
3230
3231         } else {
3232             s8 i;
3233             /* We dont have buffered packet in mangement frame queue (1 failed), So proceed with condition 2
3234              * UNIFI_TRAFFIC_Q_VO -> VI -> BE -> BK
3235              */
3236             for(i= 3; i>=0; i--) {
3237                 if (!IS_DELIVERY_ENABLED(staRecord->powersaveMode[i])) {
3238                     /* Send One packet, if queue is NULL then continue */
3239                     if((buffered_pkt=dequeue_tx_data_pdu(priv, &staRecord->dataPdu[i]))) {
3240                         moreData = uf_is_more_data_for_non_delivery_ac(staRecord);
3241
3242                         buffered_pkt->transmissionControl |= (TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3243
3244                         /* Last parameter is EOSP & its false always for PS-POLL processing */
3245                         if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staRecord,moreData,FALSE)) == -ENOSPC) {
3246                             /* Clear the trigger bit transmission control*/
3247                             buffered_pkt->transmissionControl &= ~(TRANSMISSION_CONTROL_TRIGGER_MASK | TRANSMISSION_CONTROL_EOSP_MASK);
3248                             /* Enqueue at the head of the queue */
3249                             spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3250                             list_add(&buffered_pkt->q, &staRecord->dataPdu[i]);
3251                             spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3252                             priv->pausedStaHandle[0]=(u8)(staRecord->assignedHandle);
3253                             unifi_trace(priv, UDBG1, "(ENOSPC) PS-POLL received : PDU sending failed \n");
3254                         } else {
3255                             if(r) {
3256                                 unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3257                                 /* the PDU failed where we can't do any thing so free the storage */
3258                                 unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3259                             }
3260                             kfree(buffered_pkt);
3261                         }
3262                         break;
3263                     }
3264                 }
3265             }
3266         }
3267         /* Check if all AC's are Delivery Enabled */
3268         is_all_ac_deliver_enabled_and_moredata(staRecord, &allDeliveryEnabled, &dataAvailable);
3269         /*check for more data in non-delivery enabled queues*/
3270         moreData = (uf_is_more_data_for_non_delivery_ac(staRecord) || (allDeliveryEnabled && dataAvailable));
3271         if(!moreData && (staRecord->timSet == CSR_WIFI_TIM_SET)) {
3272             unifi_trace(priv, UDBG3, "more data = NULL, set tim to 0 in uf_process_ps_poll\n");
3273             if (!staRecord->timRequestPendingFlag){
3274                 update_tim(priv,staRecord->aid,0,interfaceTag, staRecord->assignedHandle);
3275             }
3276             else
3277             {
3278                 /* Cache the TimSet value so that it will processed immidiatly after
3279                  * completing the current setTim Request
3280                  */
3281                 staRecord->updateTimReqQueued = 0;
3282                 unifi_trace(priv, UDBG6, "update_tim : One more UpdateTim Request (Tim value:%d) Queued for AID %x\n", staRecord->updateTimReqQueued,
3283                             staRecord->aid);
3284             }
3285
3286         }
3287     }
3288
3289         unifi_trace(priv, UDBG3, "leaving uf_process_ps_poll\n");
3290 }
3291
3292
3293
3294 void add_to_send_cfm_list(unifi_priv_t * priv,
3295                           tx_buffered_packets_t *tx_q_item,
3296                           struct list_head *frames_need_cfm_list)
3297 {
3298     tx_buffered_packets_t *send_cfm_list_item = NULL;
3299
3300     send_cfm_list_item = (tx_buffered_packets_t *) kmalloc(sizeof(tx_buffered_packets_t), GFP_ATOMIC);
3301
3302     if(send_cfm_list_item == NULL){
3303         unifi_warning(priv, "%s: Failed to allocate memory for new list item \n");
3304         return;
3305     }
3306
3307     INIT_LIST_HEAD(&send_cfm_list_item->q);
3308
3309     send_cfm_list_item->hostTag = tx_q_item->hostTag;
3310     send_cfm_list_item->interfaceTag = tx_q_item->interfaceTag;
3311     send_cfm_list_item->transmissionControl = tx_q_item->transmissionControl;
3312     send_cfm_list_item->leSenderProcessId = tx_q_item->leSenderProcessId;
3313     send_cfm_list_item->rate = tx_q_item->rate;
3314     memcpy(send_cfm_list_item->peerMacAddress.a, tx_q_item->peerMacAddress.a, ETH_ALEN);
3315     send_cfm_list_item->priority = tx_q_item->priority;
3316
3317     list_add_tail(&send_cfm_list_item->q, frames_need_cfm_list);
3318 }
3319
3320 void uf_prepare_send_cfm_list_for_queued_pkts(unifi_priv_t * priv,
3321                                                  struct list_head *frames_need_cfm_list,
3322                                                  struct list_head * list)
3323 {
3324     tx_buffered_packets_t *tx_q_item = NULL;
3325     struct list_head *listHead;
3326     struct list_head *placeHolder;
3327     unsigned long lock_flags;
3328
3329     func_enter();
3330
3331     spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3332
3333     /* Search through the list and if confirmation required for any frames,
3334     add it to the send_cfm list */
3335     list_for_each_safe(listHead, placeHolder, list) {
3336         tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
3337
3338         if(!tx_q_item) {
3339             unifi_error(priv, "Entry should exist, otherwise it is a (BUG)\n");
3340             continue;
3341         }
3342
3343         /* check if confirmation is requested and if the sender ID
3344         is not netdevice client then save the entry in the list for need cfms */
3345         if (!(tx_q_item->transmissionControl & CSR_NO_CONFIRM_REQUIRED) &&
3346             (tx_q_item->leSenderProcessId != priv->netdev_client->sender_id)){
3347              unifi_trace(priv, UDBG1, "%s: SenderProcessID=%x host tag=%x transmission control=%x\n",
3348                 __FUNCTION__,
3349                 tx_q_item->leSenderProcessId,
3350                 tx_q_item->hostTag,
3351                 tx_q_item->transmissionControl);
3352
3353              add_to_send_cfm_list(priv, tx_q_item, frames_need_cfm_list);
3354         }
3355     }
3356
3357     spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3358
3359     func_exit();
3360 }
3361
3362
3363
3364 void uf_flush_list(unifi_priv_t * priv, struct list_head * list)
3365 {
3366     tx_buffered_packets_t *tx_q_item;
3367     struct list_head *listHead;
3368     struct list_head *placeHolder;
3369     unsigned long lock_flags;
3370
3371     unifi_trace(priv, UDBG5, "entering the uf_flush_list \n");
3372
3373     spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3374     /* go through list, delete & free memory */
3375     list_for_each_safe(listHead, placeHolder, list) {
3376         tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
3377
3378         if(!tx_q_item) {
3379             unifi_error(priv, "entry should exists, otherwise crashes (bug)\n");
3380         }
3381         unifi_trace(priv, UDBG5,
3382                 "proccess_tx:  in uf_flush_list peerMacAddress=%02X%02X%02X%02X%02X%02X senderProcessId=%x\n",
3383                 tx_q_item->peerMacAddress.a[0], tx_q_item->peerMacAddress.a[1],
3384                 tx_q_item->peerMacAddress.a[2], tx_q_item->peerMacAddress.a[3],
3385                 tx_q_item->peerMacAddress.a[4], tx_q_item->peerMacAddress.a[5],
3386                 tx_q_item->leSenderProcessId);
3387
3388         list_del(listHead);
3389         /* free the allocated memory */
3390         unifi_net_data_free(priv, &tx_q_item->bulkdata);
3391         kfree(tx_q_item);
3392         tx_q_item = NULL;
3393         if (!priv->noOfPktQueuedInDriver) {
3394             unifi_error(priv, "packets queued in driver 0 still decrementing in %s\n", __FUNCTION__);
3395         } else {
3396             priv->noOfPktQueuedInDriver--;
3397         }
3398     }
3399     spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3400 }
3401
3402 tx_buffered_packets_t *dequeue_tx_data_pdu(unifi_priv_t *priv, struct list_head *txList)
3403 {
3404     /* dequeue the tx data packets from the appropriate queue */
3405     tx_buffered_packets_t *tx_q_item = NULL;
3406     struct list_head *listHead;
3407     struct list_head *placeHolder;
3408     unsigned long lock_flags;
3409
3410     unifi_trace(priv, UDBG5, "entering dequeue_tx_data_pdu\n");
3411     /* check for list empty */
3412     if (list_empty(txList)) {
3413         unifi_trace(priv, UDBG5, "In dequeue_tx_data_pdu, the list is empty\n");
3414         return NULL;
3415     }
3416
3417     /* Verification, if packet count is negetive */
3418     if (priv->noOfPktQueuedInDriver == 0xFFFF) {
3419         unifi_warning(priv, "no packet available in queue: debug");
3420         return NULL;
3421     }
3422
3423     /* return first node after header, & delete from the list  && atleast one item exist */
3424     spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3425     list_for_each_safe(listHead, placeHolder, txList) {
3426         tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
3427         list_del(listHead);
3428         break;
3429     }
3430     spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3431
3432     if (tx_q_item) {
3433         unifi_trace(priv, UDBG5,
3434                 "proccess_tx:  In dequeue_tx_data_pdu peerMacAddress=%02X%02X%02X%02X%02X%02X senderProcessId=%x\n",
3435                 tx_q_item->peerMacAddress.a[0], tx_q_item->peerMacAddress.a[1],
3436                 tx_q_item->peerMacAddress.a[2], tx_q_item->peerMacAddress.a[3],
3437                 tx_q_item->peerMacAddress.a[4], tx_q_item->peerMacAddress.a[5],
3438                 tx_q_item->leSenderProcessId);
3439     }
3440
3441     unifi_trace(priv, UDBG5, "leaving dequeue_tx_data_pdu\n");
3442     return tx_q_item;
3443 }
3444 /* generic function to get the station record handler */
3445 CsrWifiRouterCtrlStaInfo_t *CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(unifi_priv_t *priv,
3446         const u8 *peerMacAddress,
3447         u16 interfaceTag)
3448 {
3449     u8 i;
3450     netInterface_priv_t *interfacePriv;
3451     unsigned long lock_flags;
3452
3453     if (interfaceTag >= CSR_WIFI_NUM_INTERFACES) {
3454         unifi_error(priv, "interfaceTag is not proper, interfaceTag = %d\n", interfaceTag);
3455         return NULL;
3456     }
3457
3458     interfacePriv = priv->interfacePriv[interfaceTag];
3459
3460     /* disable the preemption untill station record is fetched */
3461     spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
3462
3463     for (i = 0; i < UNIFI_MAX_CONNECTIONS; i++) {
3464         if (interfacePriv->staInfo[i]!= NULL) {
3465             if (!memcmp(((CsrWifiRouterCtrlStaInfo_t *) (interfacePriv->staInfo[i]))->peerMacAddress.a, peerMacAddress, ETH_ALEN)) {
3466                 /* enable the preemption as station record is fetched */
3467                 spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
3468                 unifi_trace(priv, UDBG5, "peer entry found in station record\n");
3469                 return ((CsrWifiRouterCtrlStaInfo_t *) (interfacePriv->staInfo[i]));
3470             }
3471         }
3472     }
3473     /* enable the preemption as station record is fetched */
3474     spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
3475     unifi_trace(priv, UDBG5, "peer entry not found in station record\n");
3476     return NULL;
3477 }
3478 /* generic function to get the station record handler from the handle */
3479 CsrWifiRouterCtrlStaInfo_t * CsrWifiRouterCtrlGetStationRecordFromHandle(unifi_priv_t *priv,
3480                                                                  u32 handle,
3481                                                                  u16 interfaceTag)
3482 {
3483     netInterface_priv_t *interfacePriv;
3484
3485     if ((handle >= UNIFI_MAX_CONNECTIONS) || (interfaceTag >= CSR_WIFI_NUM_INTERFACES)) {
3486         unifi_error(priv, "handle/interfaceTag is not proper, handle = %d, interfaceTag = %d\n", handle, interfaceTag);
3487         return NULL;
3488     }
3489     interfacePriv = priv->interfacePriv[interfaceTag];
3490     return ((CsrWifiRouterCtrlStaInfo_t *) (interfacePriv->staInfo[handle]));
3491 }
3492
3493 /* Function to do inactivity */
3494 void uf_check_inactivity(unifi_priv_t *priv, u16 interfaceTag, u32 currentTime)
3495 {
3496     u32 i;
3497     CsrWifiRouterCtrlStaInfo_t *staInfo;
3498     u32 elapsedTime;    /* Time in microseconds */
3499     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
3500     CsrWifiMacAddress peerMacAddress;
3501     unsigned long lock_flags;
3502
3503     if (interfacePriv == NULL) {
3504         unifi_trace(priv, UDBG3, "uf_check_inactivity: Interface priv is NULL \n");
3505         return;
3506     }
3507
3508     spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
3509     /* Go through the list of stations to check for inactivity */
3510     for(i = 0; i < UNIFI_MAX_CONNECTIONS; i++) {
3511         staInfo =  CsrWifiRouterCtrlGetStationRecordFromHandle(priv, i, interfaceTag);
3512         if(!staInfo ) {
3513             continue;
3514         }
3515
3516         unifi_trace(priv, UDBG3, "Running Inactivity handler Time %xus station's last activity %xus\n",
3517                 currentTime, staInfo->lastActivity);
3518
3519
3520         elapsedTime = (currentTime >= staInfo->lastActivity)?
3521                 (currentTime - staInfo->lastActivity):
3522                 (~((u32)0) - staInfo->lastActivity + currentTime);
3523         spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
3524
3525         if (elapsedTime > MAX_INACTIVITY_INTERVAL) {
3526             memcpy((u8*)&peerMacAddress, (u8*)&staInfo->peerMacAddress, sizeof(CsrWifiMacAddress));
3527
3528             /* Indicate inactivity for the station */
3529             unifi_trace(priv, UDBG3, "Station %x:%x:%x:%x:%x:%x inactive since %xus\n sending Inactive Ind\n",
3530                         peerMacAddress.a[0], peerMacAddress.a[1],
3531                         peerMacAddress.a[2], peerMacAddress.a[3],
3532                         peerMacAddress.a[4], peerMacAddress.a[5],
3533                         elapsedTime);
3534
3535             CsrWifiRouterCtrlStaInactiveIndSend(priv->CSR_WIFI_SME_IFACEQUEUE, 0, interfaceTag, peerMacAddress);
3536         }
3537     }
3538
3539     interfacePriv->last_inactivity_check = currentTime;
3540 }
3541
3542 /* Function to update activity of a station */
3543 void uf_update_sta_activity(unifi_priv_t *priv, u16 interfaceTag, const u8 *peerMacAddress)
3544 {
3545     u32 elapsedTime, currentTime;    /* Time in microseconds */
3546     u32 timeHi;         /* Not used - Time in microseconds */
3547     CsrWifiRouterCtrlStaInfo_t *staInfo;
3548     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
3549     unsigned long lock_flags;
3550
3551     if (interfacePriv == NULL) {
3552         unifi_trace(priv, UDBG3, "uf_check_inactivity: Interface priv is NULL \n");
3553         return;
3554     }
3555
3556     currentTime = CsrTimeGet(&timeHi);
3557
3558
3559     staInfo = CsrWifiRouterCtrlGetStationRecordFromPeerMacAddress(priv, peerMacAddress, interfaceTag);
3560
3561     if (staInfo == NULL) {
3562         unifi_trace(priv, UDBG4, "Sta does not exist yet");
3563         return;
3564     }
3565
3566     spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
3567     /* Update activity */
3568     staInfo->lastActivity = currentTime;
3569
3570     /* See if inactivity handler needs to be run
3571      * Here it is theoretically possible that the counter may have wrapped around. But
3572      * since we just want to know when to run the inactivity handler it does not really matter.
3573      * Especially since this is data path it makes sense in keeping it simple and avoiding
3574      * 64 bit handling */
3575     elapsedTime = (currentTime >= interfacePriv->last_inactivity_check)?
3576                     (currentTime - interfacePriv->last_inactivity_check):
3577                     (~((u32)0) - interfacePriv->last_inactivity_check + currentTime);
3578
3579     spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
3580
3581     /* Check if it is time to run the inactivity handler */
3582     if (elapsedTime > INACTIVITY_CHECK_INTERVAL) {
3583         uf_check_inactivity(priv, interfaceTag, currentTime);
3584     }
3585 }
3586 void resume_unicast_buffered_frames(unifi_priv_t *priv, u16 interfaceTag)
3587 {
3588
3589    netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
3590    u8 i;
3591    int j;
3592    tx_buffered_packets_t * buffered_pkt = NULL;
3593    u8 hipslotFree[4] = {TRUE,TRUE,TRUE,TRUE};
3594    int r;
3595    unsigned long lock_flags;
3596
3597    func_enter();
3598    while(!isRouterBufferEnabled(priv,3) &&
3599                             ((buffered_pkt=dequeue_tx_data_pdu(priv,&interfacePriv->genericMgtFrames))!=NULL)) {
3600         buffered_pkt->transmissionControl &=
3601                      ~(TRANSMISSION_CONTROL_TRIGGER_MASK|TRANSMISSION_CONTROL_EOSP_MASK);
3602         if((r=frame_and_send_queued_pdu(priv,buffered_pkt,NULL,0,FALSE)) == -ENOSPC) {
3603             /* Enqueue at the head of the queue */
3604             spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3605             list_add(&buffered_pkt->q, &interfacePriv->genericMgtFrames);
3606             spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3607             hipslotFree[3]=FALSE;
3608             break;
3609         }else {
3610             if(r){
3611                 unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3612                 /* the PDU failed where we can't do any thing so free the storage */
3613                 unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3614             }
3615             kfree(buffered_pkt);
3616         }
3617    }
3618    for(i = 0; i < UNIFI_MAX_CONNECTIONS; i++) {
3619         CsrWifiRouterCtrlStaInfo_t *staInfo = interfacePriv->staInfo[i];
3620         if(!hipslotFree[0] && !hipslotFree[1] && !hipslotFree[2] && !hipslotFree[3]) {
3621             unifi_trace(priv, UDBG3, "(ENOSPC) in resume_unicast_buffered_frames:: hip slots are full \n");
3622             break;
3623         }
3624         if (staInfo && (staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE)) {
3625           while((( TRUE == hipslotFree[3] ) && (buffered_pkt=dequeue_tx_data_pdu(priv, &staInfo->mgtFrames)))) {
3626               buffered_pkt->transmissionControl &=
3627                            ~(TRANSMISSION_CONTROL_TRIGGER_MASK|TRANSMISSION_CONTROL_EOSP_MASK);
3628               if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staInfo,0,FALSE)) == -ENOSPC) {
3629                   unifi_trace(priv, UDBG3, "(ENOSPC) in resume_unicast_buffered_frames:: hip slots are full for voice queue\n");
3630                   /* Enqueue at the head of the queue */
3631                   spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3632                   list_add(&buffered_pkt->q, &staInfo->mgtFrames);
3633                   spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3634                   priv->pausedStaHandle[3]=(u8)(staInfo->assignedHandle);
3635                   hipslotFree[3] = FALSE;
3636                   break;
3637               } else {
3638                   if(r){
3639                       unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3640                       /* the PDU failed where we can't do any thing so free the storage */
3641                       unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3642                   }
3643                   kfree(buffered_pkt);
3644               }
3645           }
3646
3647           for(j=3;j>=0;j--) {
3648               if(!hipslotFree[j])
3649                   continue;
3650
3651               while((buffered_pkt=dequeue_tx_data_pdu(priv, &staInfo->dataPdu[j]))) {
3652                  buffered_pkt->transmissionControl &=
3653                             ~(TRANSMISSION_CONTROL_TRIGGER_MASK|TRANSMISSION_CONTROL_EOSP_MASK);
3654                  if((r=frame_and_send_queued_pdu(priv,buffered_pkt,staInfo,0,FALSE)) == -ENOSPC) {
3655                      /* Enqueue at the head of the queue */
3656                      spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3657                      list_add(&buffered_pkt->q, &staInfo->dataPdu[j]);
3658                      spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3659                      priv->pausedStaHandle[j]=(u8)(staInfo->assignedHandle);
3660                      hipslotFree[j]=FALSE;
3661                      break;
3662                  } else {
3663                     if(r){
3664                         unifi_trace (priv, UDBG1, " HIP validation failure : PDU sending failed \n");
3665                         /* the PDU failed where we can't do any thing so free the storage */
3666                         unifi_net_data_free(priv, &buffered_pkt->bulkdata);
3667                      }
3668                     kfree(buffered_pkt);
3669                  }
3670               }
3671           }
3672        }
3673     }
3674     func_exit();
3675 }
3676 void update_eosp_to_head_of_broadcast_list_head(unifi_priv_t *priv,u16 interfaceTag)
3677 {
3678
3679     netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
3680     unsigned long lock_flags;
3681     struct list_head *listHead;
3682     struct list_head *placeHolder;
3683     tx_buffered_packets_t *tx_q_item;
3684
3685     func_enter();
3686     if (interfacePriv->noOfbroadcastPktQueued) {
3687
3688         /* Update the EOSP to the HEAD of b/c list
3689          * beacuse we have received any mgmt packet so it should not hold for long time
3690          * peer may time out.
3691          */
3692         spin_lock_irqsave(&priv->tx_q_lock,lock_flags);
3693         list_for_each_safe(listHead, placeHolder, &interfacePriv->genericMulticastOrBroadCastFrames) {
3694             tx_q_item = list_entry(listHead, tx_buffered_packets_t, q);
3695             tx_q_item->transmissionControl |= TRANSMISSION_CONTROL_EOSP_MASK;
3696             tx_q_item->transmissionControl = (tx_q_item->transmissionControl & ~(CSR_NO_CONFIRM_REQUIRED));
3697             unifi_trace(priv, UDBG1,"updating eosp for list Head hostTag:= 0x%x ",tx_q_item->hostTag);
3698             break;
3699         }
3700         spin_unlock_irqrestore(&priv->tx_q_lock,lock_flags);
3701     }
3702     func_exit();
3703 }
3704
3705 /*
3706  * ---------------------------------------------------------------------------
3707  *  resume_suspended_uapsd
3708  *
3709  *      This function takes care processing packets of Unscheduled Service Period,
3710  *      which been suspended earlier due to DTIM/HIP ENOSPC scenarios
3711  *
3712  *  Arguments:
3713  *      priv            Pointer to device private context struct
3714  *      interfaceTag    For which resume should happen
3715  * ---------------------------------------------------------------------------
3716  */
3717 void resume_suspended_uapsd(unifi_priv_t* priv,u16 interfaceTag)
3718 {
3719
3720    u8 startIndex;
3721    CsrWifiRouterCtrlStaInfo_t * staInfo = NULL;
3722     unsigned long lock_flags;
3723
3724     unifi_trace(priv, UDBG2, "++resume_suspended_uapsd: \n");
3725     for(startIndex= 0; startIndex < UNIFI_MAX_CONNECTIONS;startIndex++) {
3726         staInfo =  CsrWifiRouterCtrlGetStationRecordFromHandle(priv,startIndex,interfaceTag);
3727
3728         if(!staInfo || !staInfo->wmmOrQosEnabled) {
3729             continue;
3730         } else if((staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_POWER_SAVE)
3731                    &&staInfo->uapsdActive && staInfo->uspSuspend) {
3732             /* U-APSD Still active & previously suspended either ENOSPC of FH queues OR
3733              * due to DTIM activity
3734              */
3735             uf_handle_uspframes_delivery(priv, staInfo, interfaceTag);
3736         } else {
3737             unifi_trace(priv, UDBG2, "resume_suspended_uapsd: PS state=%x, uapsdActive?=%x, suspend?=%x\n",
3738                         staInfo->currentPeerState, staInfo->uapsdActive, staInfo->uspSuspend);
3739             if (staInfo->currentPeerState == CSR_WIFI_ROUTER_CTRL_PEER_CONNECTED_ACTIVE)
3740             {
3741                 spin_lock_irqsave(&priv->staRecord_lock,lock_flags);
3742                 staInfo->uapsdActive = FALSE;
3743                 staInfo->uspSuspend = FALSE;
3744                 spin_unlock_irqrestore(&priv->staRecord_lock,lock_flags);
3745             }
3746         }
3747     }
3748     unifi_trace(priv, UDBG2, "--resume_suspended_uapsd:\n");
3749 }
3750
3751 #endif