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