ath6kl: add pointer to the skb in htc_packet
[cascardo/linux.git] / drivers / net / wireless / ath / ath6kl / txrx.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include "core.h"
19 #include "debug.h"
20
21 /*
22  * tid - tid_mux0..tid_mux3
23  * aid - tid_mux4..tid_mux7
24  */
25 #define ATH6KL_TID_MASK 0xf
26 #define ATH6KL_AID_SHIFT 4
27
28 static inline u8 ath6kl_get_tid(u8 tid_mux)
29 {
30         return tid_mux & ATH6KL_TID_MASK;
31 }
32
33 static inline u8 ath6kl_get_aid(u8 tid_mux)
34 {
35         return tid_mux >> ATH6KL_AID_SHIFT;
36 }
37
38 static u8 ath6kl_ibss_map_epid(struct sk_buff *skb, struct net_device *dev,
39                                u32 *map_no)
40 {
41         struct ath6kl *ar = ath6kl_priv(dev);
42         struct ethhdr *eth_hdr;
43         u32 i, ep_map = -1;
44         u8 *datap;
45
46         *map_no = 0;
47         datap = skb->data;
48         eth_hdr = (struct ethhdr *) (datap + sizeof(struct wmi_data_hdr));
49
50         if (is_multicast_ether_addr(eth_hdr->h_dest))
51                 return ENDPOINT_2;
52
53         for (i = 0; i < ar->node_num; i++) {
54                 if (memcmp(eth_hdr->h_dest, ar->node_map[i].mac_addr,
55                            ETH_ALEN) == 0) {
56                         *map_no = i + 1;
57                         ar->node_map[i].tx_pend++;
58                         return ar->node_map[i].ep_id;
59                 }
60
61                 if ((ep_map == -1) && !ar->node_map[i].tx_pend)
62                         ep_map = i;
63         }
64
65         if (ep_map == -1) {
66                 ep_map = ar->node_num;
67                 ar->node_num++;
68                 if (ar->node_num > MAX_NODE_NUM)
69                         return ENDPOINT_UNUSED;
70         }
71
72         memcpy(ar->node_map[ep_map].mac_addr, eth_hdr->h_dest, ETH_ALEN);
73
74         for (i = ENDPOINT_2; i <= ENDPOINT_5; i++) {
75                 if (!ar->tx_pending[i]) {
76                         ar->node_map[ep_map].ep_id = i;
77                         break;
78                 }
79
80                 /*
81                  * No free endpoint is available, start redistribution on
82                  * the inuse endpoints.
83                  */
84                 if (i == ENDPOINT_5) {
85                         ar->node_map[ep_map].ep_id = ar->next_ep_id;
86                         ar->next_ep_id++;
87                         if (ar->next_ep_id > ENDPOINT_5)
88                                 ar->next_ep_id = ENDPOINT_2;
89                 }
90         }
91
92         *map_no = ep_map + 1;
93         ar->node_map[ep_map].tx_pend++;
94
95         return ar->node_map[ep_map].ep_id;
96 }
97
98 static bool ath6kl_process_uapsdq(struct ath6kl_sta *conn,
99                                 struct ath6kl_vif *vif,
100                                 struct sk_buff *skb,
101                                 u32 *flags)
102 {
103         struct ath6kl *ar = vif->ar;
104         bool is_apsdq_empty = false;
105         struct ethhdr *datap = (struct ethhdr *) skb->data;
106         u8 up = 0, traffic_class, *ip_hdr;
107         u16 ether_type;
108         struct ath6kl_llc_snap_hdr *llc_hdr;
109
110         if (conn->sta_flags & STA_PS_APSD_TRIGGER) {
111                 /*
112                  * This tx is because of a uAPSD trigger, determine
113                  * more and EOSP bit. Set EOSP if queue is empty
114                  * or sufficient frames are delivered for this trigger.
115                  */
116                 spin_lock_bh(&conn->psq_lock);
117                 if (!skb_queue_empty(&conn->apsdq))
118                         *flags |= WMI_DATA_HDR_FLAGS_MORE;
119                 else if (conn->sta_flags & STA_PS_APSD_EOSP)
120                         *flags |= WMI_DATA_HDR_FLAGS_EOSP;
121                 *flags |= WMI_DATA_HDR_FLAGS_UAPSD;
122                 spin_unlock_bh(&conn->psq_lock);
123                 return false;
124         } else if (!conn->apsd_info)
125                 return false;
126
127         if (test_bit(WMM_ENABLED, &vif->flags)) {
128                 ether_type = be16_to_cpu(datap->h_proto);
129                 if (is_ethertype(ether_type)) {
130                         /* packet is in DIX format  */
131                         ip_hdr = (u8 *)(datap + 1);
132                 } else {
133                         /* packet is in 802.3 format */
134                         llc_hdr = (struct ath6kl_llc_snap_hdr *)
135                                                         (datap + 1);
136                         ether_type = be16_to_cpu(llc_hdr->eth_type);
137                         ip_hdr = (u8 *)(llc_hdr + 1);
138                 }
139
140                 if (ether_type == IP_ETHERTYPE)
141                         up = ath6kl_wmi_determine_user_priority(
142                                                         ip_hdr, 0);
143         }
144
145         traffic_class = ath6kl_wmi_get_traffic_class(up);
146
147         if ((conn->apsd_info & (1 << traffic_class)) == 0)
148                 return false;
149
150         /* Queue the frames if the STA is sleeping */
151         spin_lock_bh(&conn->psq_lock);
152         is_apsdq_empty = skb_queue_empty(&conn->apsdq);
153         skb_queue_tail(&conn->apsdq, skb);
154         spin_unlock_bh(&conn->psq_lock);
155
156         /*
157          * If this is the first pkt getting queued
158          * for this STA, update the PVB for this STA
159          */
160         if (is_apsdq_empty) {
161                 ath6kl_wmi_set_apsd_bfrd_traf(ar->wmi,
162                                               vif->fw_vif_idx,
163                                               conn->aid, 1, 0);
164         }
165         *flags |= WMI_DATA_HDR_FLAGS_UAPSD;
166
167         return true;
168 }
169
170 static bool ath6kl_process_psq(struct ath6kl_sta *conn,
171                                 struct ath6kl_vif *vif,
172                                 struct sk_buff *skb,
173                                 u32 *flags)
174 {
175         bool is_psq_empty = false;
176         struct ath6kl *ar = vif->ar;
177
178         if (conn->sta_flags & STA_PS_POLLED) {
179                 spin_lock_bh(&conn->psq_lock);
180                 if (!skb_queue_empty(&conn->psq))
181                         *flags |= WMI_DATA_HDR_FLAGS_MORE;
182                 spin_unlock_bh(&conn->psq_lock);
183                 return false;
184         }
185
186         /* Queue the frames if the STA is sleeping */
187         spin_lock_bh(&conn->psq_lock);
188         is_psq_empty = skb_queue_empty(&conn->psq);
189         skb_queue_tail(&conn->psq, skb);
190         spin_unlock_bh(&conn->psq_lock);
191
192         /*
193          * If this is the first pkt getting queued
194          * for this STA, update the PVB for this
195          * STA.
196          */
197         if (is_psq_empty)
198                 ath6kl_wmi_set_pvb_cmd(ar->wmi,
199                                        vif->fw_vif_idx,
200                                        conn->aid, 1);
201         return true;
202 }
203
204 static bool ath6kl_powersave_ap(struct ath6kl_vif *vif, struct sk_buff *skb,
205                                 u32 *flags)
206 {
207         struct ethhdr *datap = (struct ethhdr *) skb->data;
208         struct ath6kl_sta *conn = NULL;
209         bool ps_queued = false;
210         struct ath6kl *ar = vif->ar;
211
212         if (is_multicast_ether_addr(datap->h_dest)) {
213                 u8 ctr = 0;
214                 bool q_mcast = false;
215
216                 for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
217                         if (ar->sta_list[ctr].sta_flags & STA_PS_SLEEP) {
218                                 q_mcast = true;
219                                 break;
220                         }
221                 }
222
223                 if (q_mcast) {
224                         /*
225                          * If this transmit is not because of a Dtim Expiry
226                          * q it.
227                          */
228                         if (!test_bit(DTIM_EXPIRED, &vif->flags)) {
229                                 bool is_mcastq_empty = false;
230
231                                 spin_lock_bh(&ar->mcastpsq_lock);
232                                 is_mcastq_empty =
233                                         skb_queue_empty(&ar->mcastpsq);
234                                 skb_queue_tail(&ar->mcastpsq, skb);
235                                 spin_unlock_bh(&ar->mcastpsq_lock);
236
237                                 /*
238                                  * If this is the first Mcast pkt getting
239                                  * queued indicate to the target to set the
240                                  * BitmapControl LSB of the TIM IE.
241                                  */
242                                 if (is_mcastq_empty)
243                                         ath6kl_wmi_set_pvb_cmd(ar->wmi,
244                                                                vif->fw_vif_idx,
245                                                                MCAST_AID, 1);
246
247                                 ps_queued = true;
248                         } else {
249                                 /*
250                                  * This transmit is because of Dtim expiry.
251                                  * Determine if MoreData bit has to be set.
252                                  */
253                                 spin_lock_bh(&ar->mcastpsq_lock);
254                                 if (!skb_queue_empty(&ar->mcastpsq))
255                                         *flags |= WMI_DATA_HDR_FLAGS_MORE;
256                                 spin_unlock_bh(&ar->mcastpsq_lock);
257                         }
258                 }
259         } else {
260                 conn = ath6kl_find_sta(vif, datap->h_dest);
261                 if (!conn) {
262                         dev_kfree_skb(skb);
263
264                         /* Inform the caller that the skb is consumed */
265                         return true;
266                 }
267
268                 if (conn->sta_flags & STA_PS_SLEEP) {
269                         ps_queued = ath6kl_process_uapsdq(conn,
270                                                 vif, skb, flags);
271                         if (!(*flags & WMI_DATA_HDR_FLAGS_UAPSD))
272                                 ps_queued = ath6kl_process_psq(conn,
273                                                 vif, skb, flags);
274                 }
275         }
276         return ps_queued;
277 }
278
279 /* Tx functions */
280
281 int ath6kl_control_tx(void *devt, struct sk_buff *skb,
282                       enum htc_endpoint_id eid)
283 {
284         struct ath6kl *ar = devt;
285         int status = 0;
286         struct ath6kl_cookie *cookie = NULL;
287
288         if (WARN_ON_ONCE(ar->state == ATH6KL_STATE_WOW))
289                 return -EACCES;
290
291         spin_lock_bh(&ar->lock);
292
293         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
294                    "%s: skb=0x%p, len=0x%x eid =%d\n", __func__,
295                    skb, skb->len, eid);
296
297         if (test_bit(WMI_CTRL_EP_FULL, &ar->flag) && (eid == ar->ctrl_ep)) {
298                 /*
299                  * Control endpoint is full, don't allocate resources, we
300                  * are just going to drop this packet.
301                  */
302                 cookie = NULL;
303                 ath6kl_err("wmi ctrl ep full, dropping pkt : 0x%p, len:%d\n",
304                            skb, skb->len);
305         } else
306                 cookie = ath6kl_alloc_cookie(ar);
307
308         if (cookie == NULL) {
309                 spin_unlock_bh(&ar->lock);
310                 status = -ENOMEM;
311                 goto fail_ctrl_tx;
312         }
313
314         ar->tx_pending[eid]++;
315
316         if (eid != ar->ctrl_ep)
317                 ar->total_tx_data_pend++;
318
319         spin_unlock_bh(&ar->lock);
320
321         cookie->skb = skb;
322         cookie->map_no = 0;
323         set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
324                          eid, ATH6KL_CONTROL_PKT_TAG);
325         cookie->htc_pkt.skb = skb;
326
327         /*
328          * This interface is asynchronous, if there is an error, cleanup
329          * will happen in the TX completion callback.
330          */
331         ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
332
333         return 0;
334
335 fail_ctrl_tx:
336         dev_kfree_skb(skb);
337         return status;
338 }
339
340 int ath6kl_data_tx(struct sk_buff *skb, struct net_device *dev)
341 {
342         struct ath6kl *ar = ath6kl_priv(dev);
343         struct ath6kl_cookie *cookie = NULL;
344         enum htc_endpoint_id eid = ENDPOINT_UNUSED;
345         struct ath6kl_vif *vif = netdev_priv(dev);
346         u32 map_no = 0;
347         u16 htc_tag = ATH6KL_DATA_PKT_TAG;
348         u8 ac = 99 ; /* initialize to unmapped ac */
349         bool chk_adhoc_ps_mapping = false;
350         int ret;
351         struct wmi_tx_meta_v2 meta_v2;
352         void *meta;
353         u8 csum_start = 0, csum_dest = 0, csum = skb->ip_summed;
354         u8 meta_ver = 0;
355         u32 flags = 0;
356
357         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
358                    "%s: skb=0x%p, data=0x%p, len=0x%x\n", __func__,
359                    skb, skb->data, skb->len);
360
361         /* If target is not associated */
362         if (!test_bit(CONNECTED, &vif->flags)) {
363                 dev_kfree_skb(skb);
364                 return 0;
365         }
366
367         if (WARN_ON_ONCE(ar->state != ATH6KL_STATE_ON)) {
368                 dev_kfree_skb(skb);
369                 return 0;
370         }
371
372         if (!test_bit(WMI_READY, &ar->flag))
373                 goto fail_tx;
374
375         /* AP mode Power saving processing */
376         if (vif->nw_type == AP_NETWORK) {
377                 if (ath6kl_powersave_ap(vif, skb, &flags))
378                         return 0;
379         }
380
381         if (test_bit(WMI_ENABLED, &ar->flag)) {
382                 if ((dev->features & NETIF_F_IP_CSUM) &&
383                     (csum == CHECKSUM_PARTIAL)) {
384                         csum_start = skb->csum_start -
385                                         (skb_network_header(skb) - skb->head) +
386                                         sizeof(struct ath6kl_llc_snap_hdr);
387                         csum_dest = skb->csum_offset + csum_start;
388                 }
389
390                 if (skb_headroom(skb) < dev->needed_headroom) {
391                         struct sk_buff *tmp_skb = skb;
392
393                         skb = skb_realloc_headroom(skb, dev->needed_headroom);
394                         kfree_skb(tmp_skb);
395                         if (skb == NULL) {
396                                 vif->net_stats.tx_dropped++;
397                                 return 0;
398                         }
399                 }
400
401                 if (ath6kl_wmi_dix_2_dot3(ar->wmi, skb)) {
402                         ath6kl_err("ath6kl_wmi_dix_2_dot3 failed\n");
403                         goto fail_tx;
404                 }
405
406                 if ((dev->features & NETIF_F_IP_CSUM) &&
407                     (csum == CHECKSUM_PARTIAL)) {
408                         meta_v2.csum_start = csum_start;
409                         meta_v2.csum_dest = csum_dest;
410
411                         /* instruct target to calculate checksum */
412                         meta_v2.csum_flags = WMI_META_V2_FLAG_CSUM_OFFLOAD;
413                         meta_ver = WMI_META_VERSION_2;
414                         meta = &meta_v2;
415                 } else {
416                         meta_ver = 0;
417                         meta = NULL;
418                 }
419
420                 ret = ath6kl_wmi_data_hdr_add(ar->wmi, skb,
421                                 DATA_MSGTYPE, flags, 0,
422                                 meta_ver,
423                                 meta, vif->fw_vif_idx);
424
425                 if (ret) {
426                         ath6kl_warn("failed to add wmi data header:%d\n"
427                                 , ret);
428                         goto fail_tx;
429                 }
430
431                 if ((vif->nw_type == ADHOC_NETWORK) &&
432                     ar->ibss_ps_enable && test_bit(CONNECTED, &vif->flags))
433                         chk_adhoc_ps_mapping = true;
434                 else {
435                         /* get the stream mapping */
436                         ret = ath6kl_wmi_implicit_create_pstream(ar->wmi,
437                                     vif->fw_vif_idx, skb,
438                                     0, test_bit(WMM_ENABLED, &vif->flags), &ac);
439                         if (ret)
440                                 goto fail_tx;
441                 }
442         } else
443                 goto fail_tx;
444
445         spin_lock_bh(&ar->lock);
446
447         if (chk_adhoc_ps_mapping)
448                 eid = ath6kl_ibss_map_epid(skb, dev, &map_no);
449         else
450                 eid = ar->ac2ep_map[ac];
451
452         if (eid == 0 || eid == ENDPOINT_UNUSED) {
453                 ath6kl_err("eid %d is not mapped!\n", eid);
454                 spin_unlock_bh(&ar->lock);
455                 goto fail_tx;
456         }
457
458         /* allocate resource for this packet */
459         cookie = ath6kl_alloc_cookie(ar);
460
461         if (!cookie) {
462                 spin_unlock_bh(&ar->lock);
463                 goto fail_tx;
464         }
465
466         /* update counts while the lock is held */
467         ar->tx_pending[eid]++;
468         ar->total_tx_data_pend++;
469
470         spin_unlock_bh(&ar->lock);
471
472         if (!IS_ALIGNED((unsigned long) skb->data - HTC_HDR_LENGTH, 4) &&
473             skb_cloned(skb)) {
474                 /*
475                  * We will touch (move the buffer data to align it. Since the
476                  * skb buffer is cloned and not only the header is changed, we
477                  * have to copy it to allow the changes. Since we are copying
478                  * the data here, we may as well align it by reserving suitable
479                  * headroom to avoid the memmove in ath6kl_htc_tx_buf_align().
480                  */
481                 struct sk_buff *nskb;
482
483                 nskb = skb_copy_expand(skb, HTC_HDR_LENGTH, 0, GFP_ATOMIC);
484                 if (nskb == NULL)
485                         goto fail_tx;
486                 kfree_skb(skb);
487                 skb = nskb;
488         }
489
490         cookie->skb = skb;
491         cookie->map_no = map_no;
492         set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
493                          eid, htc_tag);
494         cookie->htc_pkt.skb = skb;
495
496         ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, "tx ",
497                         skb->data, skb->len);
498
499         /*
500          * HTC interface is asynchronous, if this fails, cleanup will
501          * happen in the ath6kl_tx_complete callback.
502          */
503         ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
504
505         return 0;
506
507 fail_tx:
508         dev_kfree_skb(skb);
509
510         vif->net_stats.tx_dropped++;
511         vif->net_stats.tx_aborted_errors++;
512
513         return 0;
514 }
515
516 /* indicate tx activity or inactivity on a WMI stream */
517 void ath6kl_indicate_tx_activity(void *devt, u8 traffic_class, bool active)
518 {
519         struct ath6kl *ar = devt;
520         enum htc_endpoint_id eid;
521         int i;
522
523         eid = ar->ac2ep_map[traffic_class];
524
525         if (!test_bit(WMI_ENABLED, &ar->flag))
526                 goto notify_htc;
527
528         spin_lock_bh(&ar->lock);
529
530         ar->ac_stream_active[traffic_class] = active;
531
532         if (active) {
533                 /*
534                  * Keep track of the active stream with the highest
535                  * priority.
536                  */
537                 if (ar->ac_stream_pri_map[traffic_class] >
538                     ar->hiac_stream_active_pri)
539                         /* set the new highest active priority */
540                         ar->hiac_stream_active_pri =
541                                         ar->ac_stream_pri_map[traffic_class];
542
543         } else {
544                 /*
545                  * We may have to search for the next active stream
546                  * that is the highest priority.
547                  */
548                 if (ar->hiac_stream_active_pri ==
549                         ar->ac_stream_pri_map[traffic_class]) {
550                         /*
551                          * The highest priority stream just went inactive
552                          * reset and search for the "next" highest "active"
553                          * priority stream.
554                          */
555                         ar->hiac_stream_active_pri = 0;
556
557                         for (i = 0; i < WMM_NUM_AC; i++) {
558                                 if (ar->ac_stream_active[i] &&
559                                     (ar->ac_stream_pri_map[i] >
560                                      ar->hiac_stream_active_pri))
561                                         /*
562                                          * Set the new highest active
563                                          * priority.
564                                          */
565                                         ar->hiac_stream_active_pri =
566                                                 ar->ac_stream_pri_map[i];
567                         }
568                 }
569         }
570
571         spin_unlock_bh(&ar->lock);
572
573 notify_htc:
574         /* notify HTC, this may cause credit distribution changes */
575         ath6kl_htc_indicate_activity_change(ar->htc_target, eid, active);
576 }
577
578 enum htc_send_full_action ath6kl_tx_queue_full(struct htc_target *target,
579                                                struct htc_packet *packet)
580 {
581         struct ath6kl *ar = target->dev->ar;
582         struct ath6kl_vif *vif;
583         enum htc_endpoint_id endpoint = packet->endpoint;
584         enum htc_send_full_action action = HTC_SEND_FULL_KEEP;
585
586         if (endpoint == ar->ctrl_ep) {
587                 /*
588                  * Under normal WMI if this is getting full, then something
589                  * is running rampant the host should not be exhausting the
590                  * WMI queue with too many commands the only exception to
591                  * this is during testing using endpointping.
592                  */
593                 set_bit(WMI_CTRL_EP_FULL, &ar->flag);
594                 ath6kl_err("wmi ctrl ep is full\n");
595                 return action;
596         }
597
598         if (packet->info.tx.tag == ATH6KL_CONTROL_PKT_TAG)
599                 return action;
600
601         /*
602          * The last MAX_HI_COOKIE_NUM "batch" of cookies are reserved for
603          * the highest active stream.
604          */
605         if (ar->ac_stream_pri_map[ar->ep2ac_map[endpoint]] <
606             ar->hiac_stream_active_pri &&
607             ar->cookie_count <=
608                         target->endpoint[endpoint].tx_drop_packet_threshold)
609                 /*
610                  * Give preference to the highest priority stream by
611                  * dropping the packets which overflowed.
612                  */
613                 action = HTC_SEND_FULL_DROP;
614
615         /* FIXME: Locking */
616         spin_lock_bh(&ar->list_lock);
617         list_for_each_entry(vif, &ar->vif_list, list) {
618                 if (vif->nw_type == ADHOC_NETWORK ||
619                     action != HTC_SEND_FULL_DROP) {
620                         spin_unlock_bh(&ar->list_lock);
621
622                         set_bit(NETQ_STOPPED, &vif->flags);
623                         netif_stop_queue(vif->ndev);
624
625                         return action;
626                 }
627         }
628         spin_unlock_bh(&ar->list_lock);
629
630         return action;
631 }
632
633 /* TODO this needs to be looked at */
634 static void ath6kl_tx_clear_node_map(struct ath6kl_vif *vif,
635                                      enum htc_endpoint_id eid, u32 map_no)
636 {
637         struct ath6kl *ar = vif->ar;
638         u32 i;
639
640         if (vif->nw_type != ADHOC_NETWORK)
641                 return;
642
643         if (!ar->ibss_ps_enable)
644                 return;
645
646         if (eid == ar->ctrl_ep)
647                 return;
648
649         if (map_no == 0)
650                 return;
651
652         map_no--;
653         ar->node_map[map_no].tx_pend--;
654
655         if (ar->node_map[map_no].tx_pend)
656                 return;
657
658         if (map_no != (ar->node_num - 1))
659                 return;
660
661         for (i = ar->node_num; i > 0; i--) {
662                 if (ar->node_map[i - 1].tx_pend)
663                         break;
664
665                 memset(&ar->node_map[i - 1], 0,
666                        sizeof(struct ath6kl_node_mapping));
667                 ar->node_num--;
668         }
669 }
670
671 void ath6kl_tx_complete(struct htc_target *target,
672                         struct list_head *packet_queue)
673 {
674         struct ath6kl *ar = target->dev->ar;
675         struct sk_buff_head skb_queue;
676         struct htc_packet *packet;
677         struct sk_buff *skb;
678         struct ath6kl_cookie *ath6kl_cookie;
679         u32 map_no = 0;
680         int status;
681         enum htc_endpoint_id eid;
682         bool wake_event = false;
683         bool flushing[ATH6KL_VIF_MAX] = {false};
684         u8 if_idx;
685         struct ath6kl_vif *vif;
686
687         skb_queue_head_init(&skb_queue);
688
689         /* lock the driver as we update internal state */
690         spin_lock_bh(&ar->lock);
691
692         /* reap completed packets */
693         while (!list_empty(packet_queue)) {
694
695                 packet = list_first_entry(packet_queue, struct htc_packet,
696                                           list);
697                 list_del(&packet->list);
698
699                 ath6kl_cookie = (struct ath6kl_cookie *)packet->pkt_cntxt;
700                 if (!ath6kl_cookie)
701                         goto fatal;
702
703                 status = packet->status;
704                 skb = ath6kl_cookie->skb;
705                 eid = packet->endpoint;
706                 map_no = ath6kl_cookie->map_no;
707
708                 if (!skb || !skb->data)
709                         goto fatal;
710
711                 __skb_queue_tail(&skb_queue, skb);
712
713                 if (!status && (packet->act_len != skb->len))
714                         goto fatal;
715
716                 ar->tx_pending[eid]--;
717
718                 if (eid != ar->ctrl_ep)
719                         ar->total_tx_data_pend--;
720
721                 if (eid == ar->ctrl_ep) {
722                         if (test_bit(WMI_CTRL_EP_FULL, &ar->flag))
723                                 clear_bit(WMI_CTRL_EP_FULL, &ar->flag);
724
725                         if (ar->tx_pending[eid] == 0)
726                                 wake_event = true;
727                 }
728
729                 if (eid == ar->ctrl_ep) {
730                         if_idx = wmi_cmd_hdr_get_if_idx(
731                                 (struct wmi_cmd_hdr *) packet->buf);
732                 } else {
733                         if_idx = wmi_data_hdr_get_if_idx(
734                                 (struct wmi_data_hdr *) packet->buf);
735                 }
736
737                 vif = ath6kl_get_vif_by_index(ar, if_idx);
738                 if (!vif) {
739                         ath6kl_free_cookie(ar, ath6kl_cookie);
740                         continue;
741                 }
742
743                 if (status) {
744                         if (status == -ECANCELED)
745                                 /* a packet was flushed  */
746                                 flushing[if_idx] = true;
747
748                         vif->net_stats.tx_errors++;
749
750                         if (status != -ENOSPC && status != -ECANCELED)
751                                 ath6kl_warn("tx complete error: %d\n", status);
752
753                         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
754                                    "%s: skb=0x%p data=0x%p len=0x%x eid=%d %s\n",
755                                    __func__, skb, packet->buf, packet->act_len,
756                                    eid, "error!");
757                 } else {
758                         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
759                                    "%s: skb=0x%p data=0x%p len=0x%x eid=%d %s\n",
760                                    __func__, skb, packet->buf, packet->act_len,
761                                    eid, "OK");
762
763                         flushing[if_idx] = false;
764                         vif->net_stats.tx_packets++;
765                         vif->net_stats.tx_bytes += skb->len;
766                 }
767
768                 ath6kl_tx_clear_node_map(vif, eid, map_no);
769
770                 ath6kl_free_cookie(ar, ath6kl_cookie);
771
772                 if (test_bit(NETQ_STOPPED, &vif->flags))
773                         clear_bit(NETQ_STOPPED, &vif->flags);
774         }
775
776         spin_unlock_bh(&ar->lock);
777
778         __skb_queue_purge(&skb_queue);
779
780         /* FIXME: Locking */
781         spin_lock_bh(&ar->list_lock);
782         list_for_each_entry(vif, &ar->vif_list, list) {
783                 if (test_bit(CONNECTED, &vif->flags) &&
784                     !flushing[vif->fw_vif_idx]) {
785                         spin_unlock_bh(&ar->list_lock);
786                         netif_wake_queue(vif->ndev);
787                         spin_lock_bh(&ar->list_lock);
788                 }
789         }
790         spin_unlock_bh(&ar->list_lock);
791
792         if (wake_event)
793                 wake_up(&ar->event_wq);
794
795         return;
796
797 fatal:
798         WARN_ON(1);
799         spin_unlock_bh(&ar->lock);
800         return;
801 }
802
803 void ath6kl_tx_data_cleanup(struct ath6kl *ar)
804 {
805         int i;
806
807         /* flush all the data (non-control) streams */
808         for (i = 0; i < WMM_NUM_AC; i++)
809                 ath6kl_htc_flush_txep(ar->htc_target, ar->ac2ep_map[i],
810                                       ATH6KL_DATA_PKT_TAG);
811 }
812
813 /* Rx functions */
814
815 static void ath6kl_deliver_frames_to_nw_stack(struct net_device *dev,
816                                               struct sk_buff *skb)
817 {
818         if (!skb)
819                 return;
820
821         skb->dev = dev;
822
823         if (!(skb->dev->flags & IFF_UP)) {
824                 dev_kfree_skb(skb);
825                 return;
826         }
827
828         skb->protocol = eth_type_trans(skb, skb->dev);
829
830         netif_rx_ni(skb);
831 }
832
833 static void ath6kl_alloc_netbufs(struct sk_buff_head *q, u16 num)
834 {
835         struct sk_buff *skb;
836
837         while (num) {
838                 skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
839                 if (!skb) {
840                         ath6kl_err("netbuf allocation failed\n");
841                         return;
842                 }
843                 skb_queue_tail(q, skb);
844                 num--;
845         }
846 }
847
848 static struct sk_buff *aggr_get_free_skb(struct aggr_info *p_aggr)
849 {
850         struct sk_buff *skb = NULL;
851
852         if (skb_queue_len(&p_aggr->rx_amsdu_freeq) <
853             (AGGR_NUM_OF_FREE_NETBUFS >> 2))
854                 ath6kl_alloc_netbufs(&p_aggr->rx_amsdu_freeq,
855                                      AGGR_NUM_OF_FREE_NETBUFS);
856
857         skb = skb_dequeue(&p_aggr->rx_amsdu_freeq);
858
859         return skb;
860 }
861
862 void ath6kl_rx_refill(struct htc_target *target, enum htc_endpoint_id endpoint)
863 {
864         struct ath6kl *ar = target->dev->ar;
865         struct sk_buff *skb;
866         int rx_buf;
867         int n_buf_refill;
868         struct htc_packet *packet;
869         struct list_head queue;
870
871         n_buf_refill = ATH6KL_MAX_RX_BUFFERS -
872                           ath6kl_htc_get_rxbuf_num(ar->htc_target, endpoint);
873
874         if (n_buf_refill <= 0)
875                 return;
876
877         INIT_LIST_HEAD(&queue);
878
879         ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
880                    "%s: providing htc with %d buffers at eid=%d\n",
881                    __func__, n_buf_refill, endpoint);
882
883         for (rx_buf = 0; rx_buf < n_buf_refill; rx_buf++) {
884                 skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
885                 if (!skb)
886                         break;
887
888                 packet = (struct htc_packet *) skb->head;
889                 if (!IS_ALIGNED((unsigned long) skb->data, 4))
890                         skb->data = PTR_ALIGN(skb->data - 4, 4);
891                 set_htc_rxpkt_info(packet, skb, skb->data,
892                                    ATH6KL_BUFFER_SIZE, endpoint);
893                 packet->skb = skb;
894                 list_add_tail(&packet->list, &queue);
895         }
896
897         if (!list_empty(&queue))
898                 ath6kl_htc_add_rxbuf_multiple(ar->htc_target, &queue);
899 }
900
901 void ath6kl_refill_amsdu_rxbufs(struct ath6kl *ar, int count)
902 {
903         struct htc_packet *packet;
904         struct sk_buff *skb;
905
906         while (count) {
907                 skb = ath6kl_buf_alloc(ATH6KL_AMSDU_BUFFER_SIZE);
908                 if (!skb)
909                         return;
910
911                 packet = (struct htc_packet *) skb->head;
912                 if (!IS_ALIGNED((unsigned long) skb->data, 4))
913                         skb->data = PTR_ALIGN(skb->data - 4, 4);
914                 set_htc_rxpkt_info(packet, skb, skb->data,
915                                    ATH6KL_AMSDU_BUFFER_SIZE, 0);
916                 packet->skb = skb;
917
918                 spin_lock_bh(&ar->lock);
919                 list_add_tail(&packet->list, &ar->amsdu_rx_buffer_queue);
920                 spin_unlock_bh(&ar->lock);
921                 count--;
922         }
923 }
924
925 /*
926  * Callback to allocate a receive buffer for a pending packet. We use a
927  * pre-allocated list of buffers of maximum AMSDU size (4K).
928  */
929 struct htc_packet *ath6kl_alloc_amsdu_rxbuf(struct htc_target *target,
930                                             enum htc_endpoint_id endpoint,
931                                             int len)
932 {
933         struct ath6kl *ar = target->dev->ar;
934         struct htc_packet *packet = NULL;
935         struct list_head *pkt_pos;
936         int refill_cnt = 0, depth = 0;
937
938         ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: eid=%d, len:%d\n",
939                    __func__, endpoint, len);
940
941         if ((len <= ATH6KL_BUFFER_SIZE) ||
942             (len > ATH6KL_AMSDU_BUFFER_SIZE))
943                 return NULL;
944
945         spin_lock_bh(&ar->lock);
946
947         if (list_empty(&ar->amsdu_rx_buffer_queue)) {
948                 spin_unlock_bh(&ar->lock);
949                 refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS;
950                 goto refill_buf;
951         }
952
953         packet = list_first_entry(&ar->amsdu_rx_buffer_queue,
954                                   struct htc_packet, list);
955         list_del(&packet->list);
956         list_for_each(pkt_pos, &ar->amsdu_rx_buffer_queue)
957                 depth++;
958
959         refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS - depth;
960         spin_unlock_bh(&ar->lock);
961
962         /* set actual endpoint ID */
963         packet->endpoint = endpoint;
964
965 refill_buf:
966         if (refill_cnt >= ATH6KL_AMSDU_REFILL_THRESHOLD)
967                 ath6kl_refill_amsdu_rxbufs(ar, refill_cnt);
968
969         return packet;
970 }
971
972 static void aggr_slice_amsdu(struct aggr_info *p_aggr,
973                              struct rxtid *rxtid, struct sk_buff *skb)
974 {
975         struct sk_buff *new_skb;
976         struct ethhdr *hdr;
977         u16 frame_8023_len, payload_8023_len, mac_hdr_len, amsdu_len;
978         u8 *framep;
979
980         mac_hdr_len = sizeof(struct ethhdr);
981         framep = skb->data + mac_hdr_len;
982         amsdu_len = skb->len - mac_hdr_len;
983
984         while (amsdu_len > mac_hdr_len) {
985                 hdr = (struct ethhdr *) framep;
986                 payload_8023_len = ntohs(hdr->h_proto);
987
988                 if (payload_8023_len < MIN_MSDU_SUBFRAME_PAYLOAD_LEN ||
989                     payload_8023_len > MAX_MSDU_SUBFRAME_PAYLOAD_LEN) {
990                         ath6kl_err("802.3 AMSDU frame bound check failed. len %d\n",
991                                    payload_8023_len);
992                         break;
993                 }
994
995                 frame_8023_len = payload_8023_len + mac_hdr_len;
996                 new_skb = aggr_get_free_skb(p_aggr);
997                 if (!new_skb) {
998                         ath6kl_err("no buffer available\n");
999                         break;
1000                 }
1001
1002                 memcpy(new_skb->data, framep, frame_8023_len);
1003                 skb_put(new_skb, frame_8023_len);
1004                 if (ath6kl_wmi_dot3_2_dix(new_skb)) {
1005                         ath6kl_err("dot3_2_dix error\n");
1006                         dev_kfree_skb(new_skb);
1007                         break;
1008                 }
1009
1010                 skb_queue_tail(&rxtid->q, new_skb);
1011
1012                 /* Is this the last subframe within this aggregate ? */
1013                 if ((amsdu_len - frame_8023_len) == 0)
1014                         break;
1015
1016                 /* Add the length of A-MSDU subframe padding bytes -
1017                  * Round to nearest word.
1018                  */
1019                 frame_8023_len = ALIGN(frame_8023_len, 4);
1020
1021                 framep += frame_8023_len;
1022                 amsdu_len -= frame_8023_len;
1023         }
1024
1025         dev_kfree_skb(skb);
1026 }
1027
1028 static void aggr_deque_frms(struct aggr_info_conn *agg_conn, u8 tid,
1029                             u16 seq_no, u8 order)
1030 {
1031         struct sk_buff *skb;
1032         struct rxtid *rxtid;
1033         struct skb_hold_q *node;
1034         u16 idx, idx_end, seq_end;
1035         struct rxtid_stats *stats;
1036
1037         rxtid = &agg_conn->rx_tid[tid];
1038         stats = &agg_conn->stat[tid];
1039
1040         idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);
1041
1042         /*
1043          * idx_end is typically the last possible frame in the window,
1044          * but changes to 'the' seq_no, when BAR comes. If seq_no
1045          * is non-zero, we will go up to that and stop.
1046          * Note: last seq no in current window will occupy the same
1047          * index position as index that is just previous to start.
1048          * An imp point : if win_sz is 7, for seq_no space of 4095,
1049          * then, there would be holes when sequence wrap around occurs.
1050          * Target should judiciously choose the win_sz, based on
1051          * this condition. For 4095, (TID_WINDOW_SZ = 2 x win_sz
1052          * 2, 4, 8, 16 win_sz works fine).
1053          * We must deque from "idx" to "idx_end", including both.
1054          */
1055         seq_end = seq_no ? seq_no : rxtid->seq_next;
1056         idx_end = AGGR_WIN_IDX(seq_end, rxtid->hold_q_sz);
1057
1058         spin_lock_bh(&rxtid->lock);
1059
1060         do {
1061                 node = &rxtid->hold_q[idx];
1062                 if ((order == 1) && (!node->skb))
1063                         break;
1064
1065                 if (node->skb) {
1066                         if (node->is_amsdu)
1067                                 aggr_slice_amsdu(agg_conn->aggr_info, rxtid,
1068                                                  node->skb);
1069                         else
1070                                 skb_queue_tail(&rxtid->q, node->skb);
1071                         node->skb = NULL;
1072                 } else
1073                         stats->num_hole++;
1074
1075                 rxtid->seq_next = ATH6KL_NEXT_SEQ_NO(rxtid->seq_next);
1076                 idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);
1077         } while (idx != idx_end);
1078
1079         spin_unlock_bh(&rxtid->lock);
1080
1081         stats->num_delivered += skb_queue_len(&rxtid->q);
1082
1083         while ((skb = skb_dequeue(&rxtid->q)))
1084                 ath6kl_deliver_frames_to_nw_stack(agg_conn->dev, skb);
1085 }
1086
1087 static bool aggr_process_recv_frm(struct aggr_info_conn *agg_conn, u8 tid,
1088                                   u16 seq_no,
1089                                   bool is_amsdu, struct sk_buff *frame)
1090 {
1091         struct rxtid *rxtid;
1092         struct rxtid_stats *stats;
1093         struct sk_buff *skb;
1094         struct skb_hold_q *node;
1095         u16 idx, st, cur, end;
1096         bool is_queued = false;
1097         u16 extended_end;
1098
1099         rxtid = &agg_conn->rx_tid[tid];
1100         stats = &agg_conn->stat[tid];
1101
1102         stats->num_into_aggr++;
1103
1104         if (!rxtid->aggr) {
1105                 if (is_amsdu) {
1106                         aggr_slice_amsdu(agg_conn->aggr_info, rxtid, frame);
1107                         is_queued = true;
1108                         stats->num_amsdu++;
1109                         while ((skb = skb_dequeue(&rxtid->q)))
1110                                 ath6kl_deliver_frames_to_nw_stack(agg_conn->dev,
1111                                                                   skb);
1112                 }
1113                 return is_queued;
1114         }
1115
1116         /* Check the incoming sequence no, if it's in the window */
1117         st = rxtid->seq_next;
1118         cur = seq_no;
1119         end = (st + rxtid->hold_q_sz-1) & ATH6KL_MAX_SEQ_NO;
1120
1121         if (((st < end) && (cur < st || cur > end)) ||
1122             ((st > end) && (cur > end) && (cur < st))) {
1123                 extended_end = (end + rxtid->hold_q_sz - 1) &
1124                         ATH6KL_MAX_SEQ_NO;
1125
1126                 if (((end < extended_end) &&
1127                      (cur < end || cur > extended_end)) ||
1128                     ((end > extended_end) && (cur > extended_end) &&
1129                      (cur < end))) {
1130                         aggr_deque_frms(agg_conn, tid, 0, 0);
1131                         if (cur >= rxtid->hold_q_sz - 1)
1132                                 rxtid->seq_next = cur - (rxtid->hold_q_sz - 1);
1133                         else
1134                                 rxtid->seq_next = ATH6KL_MAX_SEQ_NO -
1135                                                   (rxtid->hold_q_sz - 2 - cur);
1136                 } else {
1137                         /*
1138                          * Dequeue only those frames that are outside the
1139                          * new shifted window.
1140                          */
1141                         if (cur >= rxtid->hold_q_sz - 1)
1142                                 st = cur - (rxtid->hold_q_sz - 1);
1143                         else
1144                                 st = ATH6KL_MAX_SEQ_NO -
1145                                         (rxtid->hold_q_sz - 2 - cur);
1146
1147                         aggr_deque_frms(agg_conn, tid, st, 0);
1148                 }
1149
1150                 stats->num_oow++;
1151         }
1152
1153         idx = AGGR_WIN_IDX(seq_no, rxtid->hold_q_sz);
1154
1155         node = &rxtid->hold_q[idx];
1156
1157         spin_lock_bh(&rxtid->lock);
1158
1159         /*
1160          * Is the cur frame duplicate or something beyond our window(hold_q
1161          * -> which is 2x, already)?
1162          *
1163          * 1. Duplicate is easy - drop incoming frame.
1164          * 2. Not falling in current sliding window.
1165          *  2a. is the frame_seq_no preceding current tid_seq_no?
1166          *      -> drop the frame. perhaps sender did not get our ACK.
1167          *         this is taken care of above.
1168          *  2b. is the frame_seq_no beyond window(st, TID_WINDOW_SZ);
1169          *      -> Taken care of it above, by moving window forward.
1170          */
1171         dev_kfree_skb(node->skb);
1172         stats->num_dups++;
1173
1174         node->skb = frame;
1175         is_queued = true;
1176         node->is_amsdu = is_amsdu;
1177         node->seq_no = seq_no;
1178
1179         if (node->is_amsdu)
1180                 stats->num_amsdu++;
1181         else
1182                 stats->num_mpdu++;
1183
1184         spin_unlock_bh(&rxtid->lock);
1185
1186         aggr_deque_frms(agg_conn, tid, 0, 1);
1187
1188         if (agg_conn->timer_scheduled)
1189                 rxtid->progress = true;
1190         else
1191                 for (idx = 0 ; idx < rxtid->hold_q_sz; idx++) {
1192                         if (rxtid->hold_q[idx].skb) {
1193                                 /*
1194                                  * There is a frame in the queue and no
1195                                  * timer so start a timer to ensure that
1196                                  * the frame doesn't remain stuck
1197                                  * forever.
1198                                  */
1199                                 agg_conn->timer_scheduled = true;
1200                                 mod_timer(&agg_conn->timer,
1201                                           (jiffies +
1202                                            HZ * (AGGR_RX_TIMEOUT) / 1000));
1203                                 rxtid->progress = false;
1204                                 rxtid->timer_mon = true;
1205                                 break;
1206                         }
1207                 }
1208
1209         return is_queued;
1210 }
1211
1212 static void ath6kl_uapsd_trigger_frame_rx(struct ath6kl_vif *vif,
1213                                                  struct ath6kl_sta *conn)
1214 {
1215         struct ath6kl *ar = vif->ar;
1216         bool is_apsdq_empty, is_apsdq_empty_at_start;
1217         u32 num_frames_to_deliver, flags;
1218         struct sk_buff *skb = NULL;
1219
1220         /*
1221          * If the APSD q for this STA is not empty, dequeue and
1222          * send a pkt from the head of the q. Also update the
1223          * More data bit in the WMI_DATA_HDR if there are
1224          * more pkts for this STA in the APSD q.
1225          * If there are no more pkts for this STA,
1226          * update the APSD bitmap for this STA.
1227          */
1228
1229         num_frames_to_deliver = (conn->apsd_info >> ATH6KL_APSD_NUM_OF_AC) &
1230                                                     ATH6KL_APSD_FRAME_MASK;
1231         /*
1232          * Number of frames to send in a service period is
1233          * indicated by the station
1234          * in the QOS_INFO of the association request
1235          * If it is zero, send all frames
1236          */
1237         if (!num_frames_to_deliver)
1238                 num_frames_to_deliver = ATH6KL_APSD_ALL_FRAME;
1239
1240         spin_lock_bh(&conn->psq_lock);
1241         is_apsdq_empty = skb_queue_empty(&conn->apsdq);
1242         spin_unlock_bh(&conn->psq_lock);
1243         is_apsdq_empty_at_start = is_apsdq_empty;
1244
1245         while ((!is_apsdq_empty) && (num_frames_to_deliver)) {
1246
1247                 spin_lock_bh(&conn->psq_lock);
1248                 skb = skb_dequeue(&conn->apsdq);
1249                 is_apsdq_empty = skb_queue_empty(&conn->apsdq);
1250                 spin_unlock_bh(&conn->psq_lock);
1251
1252                 /*
1253                  * Set the STA flag to Trigger delivery,
1254                  * so that the frame will go out
1255                  */
1256                 conn->sta_flags |= STA_PS_APSD_TRIGGER;
1257                 num_frames_to_deliver--;
1258
1259                 /* Last frame in the service period, set EOSP or queue empty */
1260                 if ((is_apsdq_empty) || (!num_frames_to_deliver))
1261                         conn->sta_flags |= STA_PS_APSD_EOSP;
1262
1263                 ath6kl_data_tx(skb, vif->ndev);
1264                 conn->sta_flags &= ~(STA_PS_APSD_TRIGGER);
1265                 conn->sta_flags &= ~(STA_PS_APSD_EOSP);
1266         }
1267
1268         if (is_apsdq_empty) {
1269                 if (is_apsdq_empty_at_start)
1270                         flags = WMI_AP_APSD_NO_DELIVERY_FRAMES;
1271                 else
1272                         flags = 0;
1273
1274                 ath6kl_wmi_set_apsd_bfrd_traf(ar->wmi,
1275                                               vif->fw_vif_idx,
1276                                               conn->aid, 0, flags);
1277         }
1278
1279         return;
1280 }
1281
1282 void ath6kl_rx(struct htc_target *target, struct htc_packet *packet)
1283 {
1284         struct ath6kl *ar = target->dev->ar;
1285         struct sk_buff *skb = packet->pkt_cntxt;
1286         struct wmi_rx_meta_v2 *meta;
1287         struct wmi_data_hdr *dhdr;
1288         int min_hdr_len;
1289         u8 meta_type, dot11_hdr = 0;
1290         int status = packet->status;
1291         enum htc_endpoint_id ept = packet->endpoint;
1292         bool is_amsdu, prev_ps, ps_state = false;
1293         bool trig_state = false;
1294         struct ath6kl_sta *conn = NULL;
1295         struct sk_buff *skb1 = NULL;
1296         struct ethhdr *datap = NULL;
1297         struct ath6kl_vif *vif;
1298         struct aggr_info_conn *aggr_conn;
1299         u16 seq_no, offset;
1300         u8 tid, if_idx;
1301
1302         ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
1303                    "%s: ar=0x%p eid=%d, skb=0x%p, data=0x%p, len=0x%x status:%d",
1304                    __func__, ar, ept, skb, packet->buf,
1305                    packet->act_len, status);
1306
1307         if (status || !(skb->data + HTC_HDR_LENGTH)) {
1308                 dev_kfree_skb(skb);
1309                 return;
1310         }
1311
1312         skb_put(skb, packet->act_len + HTC_HDR_LENGTH);
1313         skb_pull(skb, HTC_HDR_LENGTH);
1314
1315         ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, "rx ",
1316                         skb->data, skb->len);
1317
1318         if (ept == ar->ctrl_ep) {
1319                 if (test_bit(WMI_ENABLED, &ar->flag)) {
1320                         ath6kl_check_wow_status(ar);
1321                         ath6kl_wmi_control_rx(ar->wmi, skb);
1322                         return;
1323                 }
1324                 if_idx =
1325                 wmi_cmd_hdr_get_if_idx((struct wmi_cmd_hdr *) skb->data);
1326         } else {
1327                 if_idx =
1328                 wmi_data_hdr_get_if_idx((struct wmi_data_hdr *) skb->data);
1329         }
1330
1331         vif = ath6kl_get_vif_by_index(ar, if_idx);
1332         if (!vif) {
1333                 dev_kfree_skb(skb);
1334                 return;
1335         }
1336
1337         /*
1338          * Take lock to protect buffer counts and adaptive power throughput
1339          * state.
1340          */
1341         spin_lock_bh(&vif->if_lock);
1342
1343         vif->net_stats.rx_packets++;
1344         vif->net_stats.rx_bytes += packet->act_len;
1345
1346         spin_unlock_bh(&vif->if_lock);
1347
1348         skb->dev = vif->ndev;
1349
1350         if (!test_bit(WMI_ENABLED, &ar->flag)) {
1351                 if (EPPING_ALIGNMENT_PAD > 0)
1352                         skb_pull(skb, EPPING_ALIGNMENT_PAD);
1353                 ath6kl_deliver_frames_to_nw_stack(vif->ndev, skb);
1354                 return;
1355         }
1356
1357         ath6kl_check_wow_status(ar);
1358
1359         min_hdr_len = sizeof(struct ethhdr) + sizeof(struct wmi_data_hdr) +
1360                       sizeof(struct ath6kl_llc_snap_hdr);
1361
1362         dhdr = (struct wmi_data_hdr *) skb->data;
1363
1364         /*
1365          * In the case of AP mode we may receive NULL data frames
1366          * that do not have LLC hdr. They are 16 bytes in size.
1367          * Allow these frames in the AP mode.
1368          */
1369         if (vif->nw_type != AP_NETWORK &&
1370             ((packet->act_len < min_hdr_len) ||
1371              (packet->act_len > WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH))) {
1372                 ath6kl_info("frame len is too short or too long\n");
1373                 vif->net_stats.rx_errors++;
1374                 vif->net_stats.rx_length_errors++;
1375                 dev_kfree_skb(skb);
1376                 return;
1377         }
1378
1379         /* Get the Power save state of the STA */
1380         if (vif->nw_type == AP_NETWORK) {
1381                 meta_type = wmi_data_hdr_get_meta(dhdr);
1382
1383                 ps_state = !!((dhdr->info >> WMI_DATA_HDR_PS_SHIFT) &
1384                               WMI_DATA_HDR_PS_MASK);
1385
1386                 offset = sizeof(struct wmi_data_hdr);
1387                 trig_state = !!(le16_to_cpu(dhdr->info3) & WMI_DATA_HDR_TRIG);
1388
1389                 switch (meta_type) {
1390                 case 0:
1391                         break;
1392                 case WMI_META_VERSION_1:
1393                         offset += sizeof(struct wmi_rx_meta_v1);
1394                         break;
1395                 case WMI_META_VERSION_2:
1396                         offset += sizeof(struct wmi_rx_meta_v2);
1397                         break;
1398                 default:
1399                         break;
1400                 }
1401
1402                 datap = (struct ethhdr *) (skb->data + offset);
1403                 conn = ath6kl_find_sta(vif, datap->h_source);
1404
1405                 if (!conn) {
1406                         dev_kfree_skb(skb);
1407                         return;
1408                 }
1409
1410                 /*
1411                  * If there is a change in PS state of the STA,
1412                  * take appropriate steps:
1413                  *
1414                  * 1. If Sleep-->Awake, flush the psq for the STA
1415                  *    Clear the PVB for the STA.
1416                  * 2. If Awake-->Sleep, Starting queueing frames
1417                  *    the STA.
1418                  */
1419                 prev_ps = !!(conn->sta_flags & STA_PS_SLEEP);
1420
1421                 if (ps_state)
1422                         conn->sta_flags |= STA_PS_SLEEP;
1423                 else
1424                         conn->sta_flags &= ~STA_PS_SLEEP;
1425
1426                 /* Accept trigger only when the station is in sleep */
1427                 if ((conn->sta_flags & STA_PS_SLEEP) && trig_state)
1428                         ath6kl_uapsd_trigger_frame_rx(vif, conn);
1429
1430                 if (prev_ps ^ !!(conn->sta_flags & STA_PS_SLEEP)) {
1431                         if (!(conn->sta_flags & STA_PS_SLEEP)) {
1432                                 struct sk_buff *skbuff = NULL;
1433                                 bool is_apsdq_empty;
1434                                 struct ath6kl_mgmt_buff *mgmt;
1435                                 u8 idx;
1436
1437                                 spin_lock_bh(&conn->psq_lock);
1438                                 while (conn->mgmt_psq_len > 0) {
1439                                         mgmt = list_first_entry(
1440                                                         &conn->mgmt_psq,
1441                                                         struct ath6kl_mgmt_buff,
1442                                                         list);
1443                                         list_del(&mgmt->list);
1444                                         conn->mgmt_psq_len--;
1445                                         spin_unlock_bh(&conn->psq_lock);
1446                                         idx = vif->fw_vif_idx;
1447
1448                                         ath6kl_wmi_send_mgmt_cmd(ar->wmi,
1449                                                                  idx,
1450                                                                  mgmt->id,
1451                                                                  mgmt->freq,
1452                                                                  mgmt->wait,
1453                                                                  mgmt->buf,
1454                                                                  mgmt->len,
1455                                                                  mgmt->no_cck);
1456
1457                                         kfree(mgmt);
1458                                         spin_lock_bh(&conn->psq_lock);
1459                                 }
1460                                 conn->mgmt_psq_len = 0;
1461                                 while ((skbuff = skb_dequeue(&conn->psq))) {
1462                                         spin_unlock_bh(&conn->psq_lock);
1463                                         ath6kl_data_tx(skbuff, vif->ndev);
1464                                         spin_lock_bh(&conn->psq_lock);
1465                                 }
1466
1467                                 is_apsdq_empty = skb_queue_empty(&conn->apsdq);
1468                                 while ((skbuff = skb_dequeue(&conn->apsdq))) {
1469                                         spin_unlock_bh(&conn->psq_lock);
1470                                         ath6kl_data_tx(skbuff, vif->ndev);
1471                                         spin_lock_bh(&conn->psq_lock);
1472                                 }
1473                                 spin_unlock_bh(&conn->psq_lock);
1474
1475                                 if (!is_apsdq_empty)
1476                                         ath6kl_wmi_set_apsd_bfrd_traf(
1477                                                         ar->wmi,
1478                                                         vif->fw_vif_idx,
1479                                                         conn->aid, 0, 0);
1480
1481                                 /* Clear the PVB for this STA */
1482                                 ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx,
1483                                                        conn->aid, 0);
1484                         }
1485                 }
1486
1487                 /* drop NULL data frames here */
1488                 if ((packet->act_len < min_hdr_len) ||
1489                     (packet->act_len >
1490                      WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH)) {
1491                         dev_kfree_skb(skb);
1492                         return;
1493                 }
1494         }
1495
1496         is_amsdu = wmi_data_hdr_is_amsdu(dhdr) ? true : false;
1497         tid = wmi_data_hdr_get_up(dhdr);
1498         seq_no = wmi_data_hdr_get_seqno(dhdr);
1499         meta_type = wmi_data_hdr_get_meta(dhdr);
1500         dot11_hdr = wmi_data_hdr_get_dot11(dhdr);
1501         skb_pull(skb, sizeof(struct wmi_data_hdr));
1502
1503         switch (meta_type) {
1504         case WMI_META_VERSION_1:
1505                 skb_pull(skb, sizeof(struct wmi_rx_meta_v1));
1506                 break;
1507         case WMI_META_VERSION_2:
1508                 meta = (struct wmi_rx_meta_v2 *) skb->data;
1509                 if (meta->csum_flags & 0x1) {
1510                         skb->ip_summed = CHECKSUM_COMPLETE;
1511                         skb->csum = (__force __wsum) meta->csum;
1512                 }
1513                 skb_pull(skb, sizeof(struct wmi_rx_meta_v2));
1514                 break;
1515         default:
1516                 break;
1517         }
1518
1519         if (dot11_hdr)
1520                 status = ath6kl_wmi_dot11_hdr_remove(ar->wmi, skb);
1521         else if (!is_amsdu)
1522                 status = ath6kl_wmi_dot3_2_dix(skb);
1523
1524         if (status) {
1525                 /*
1526                  * Drop frames that could not be processed (lack of
1527                  * memory, etc.)
1528                  */
1529                 dev_kfree_skb(skb);
1530                 return;
1531         }
1532
1533         if (!(vif->ndev->flags & IFF_UP)) {
1534                 dev_kfree_skb(skb);
1535                 return;
1536         }
1537
1538         if (vif->nw_type == AP_NETWORK) {
1539                 datap = (struct ethhdr *) skb->data;
1540                 if (is_multicast_ether_addr(datap->h_dest))
1541                         /*
1542                          * Bcast/Mcast frames should be sent to the
1543                          * OS stack as well as on the air.
1544                          */
1545                         skb1 = skb_copy(skb, GFP_ATOMIC);
1546                 else {
1547                         /*
1548                          * Search for a connected STA with dstMac
1549                          * as the Mac address. If found send the
1550                          * frame to it on the air else send the
1551                          * frame up the stack.
1552                          */
1553                         conn = ath6kl_find_sta(vif, datap->h_dest);
1554
1555                         if (conn && ar->intra_bss) {
1556                                 skb1 = skb;
1557                                 skb = NULL;
1558                         } else if (conn && !ar->intra_bss) {
1559                                 dev_kfree_skb(skb);
1560                                 skb = NULL;
1561                         }
1562                 }
1563                 if (skb1)
1564                         ath6kl_data_tx(skb1, vif->ndev);
1565
1566                 if (skb == NULL) {
1567                         /* nothing to deliver up the stack */
1568                         return;
1569                 }
1570         }
1571
1572         datap = (struct ethhdr *) skb->data;
1573
1574         if (is_unicast_ether_addr(datap->h_dest)) {
1575                 if (vif->nw_type == AP_NETWORK) {
1576                         conn = ath6kl_find_sta(vif, datap->h_source);
1577                         if (!conn)
1578                                 return;
1579                         aggr_conn = conn->aggr_conn;
1580                 } else
1581                         aggr_conn = vif->aggr_cntxt->aggr_conn;
1582
1583                 if (aggr_process_recv_frm(aggr_conn, tid, seq_no,
1584                                           is_amsdu, skb)) {
1585                         /* aggregation code will handle the skb */
1586                         return;
1587                 }
1588         }
1589
1590         ath6kl_deliver_frames_to_nw_stack(vif->ndev, skb);
1591 }
1592
1593 static void aggr_timeout(unsigned long arg)
1594 {
1595         u8 i, j;
1596         struct aggr_info_conn *aggr_conn = (struct aggr_info_conn *) arg;
1597         struct rxtid *rxtid;
1598         struct rxtid_stats *stats;
1599
1600         for (i = 0; i < NUM_OF_TIDS; i++) {
1601                 rxtid = &aggr_conn->rx_tid[i];
1602                 stats = &aggr_conn->stat[i];
1603
1604                 if (!rxtid->aggr || !rxtid->timer_mon || rxtid->progress)
1605                         continue;
1606
1607                 stats->num_timeouts++;
1608                 ath6kl_dbg(ATH6KL_DBG_AGGR,
1609                            "aggr timeout (st %d end %d)\n",
1610                            rxtid->seq_next,
1611                            ((rxtid->seq_next + rxtid->hold_q_sz-1) &
1612                             ATH6KL_MAX_SEQ_NO));
1613                 aggr_deque_frms(aggr_conn, i, 0, 0);
1614         }
1615
1616         aggr_conn->timer_scheduled = false;
1617
1618         for (i = 0; i < NUM_OF_TIDS; i++) {
1619                 rxtid = &aggr_conn->rx_tid[i];
1620
1621                 if (rxtid->aggr && rxtid->hold_q) {
1622                         for (j = 0; j < rxtid->hold_q_sz; j++) {
1623                                 if (rxtid->hold_q[j].skb) {
1624                                         aggr_conn->timer_scheduled = true;
1625                                         rxtid->timer_mon = true;
1626                                         rxtid->progress = false;
1627                                         break;
1628                                 }
1629                         }
1630
1631                         if (j >= rxtid->hold_q_sz)
1632                                 rxtid->timer_mon = false;
1633                 }
1634         }
1635
1636         if (aggr_conn->timer_scheduled)
1637                 mod_timer(&aggr_conn->timer,
1638                           jiffies + msecs_to_jiffies(AGGR_RX_TIMEOUT));
1639 }
1640
1641 static void aggr_delete_tid_state(struct aggr_info_conn *aggr_conn, u8 tid)
1642 {
1643         struct rxtid *rxtid;
1644         struct rxtid_stats *stats;
1645
1646         if (!aggr_conn || tid >= NUM_OF_TIDS)
1647                 return;
1648
1649         rxtid = &aggr_conn->rx_tid[tid];
1650         stats = &aggr_conn->stat[tid];
1651
1652         if (rxtid->aggr)
1653                 aggr_deque_frms(aggr_conn, tid, 0, 0);
1654
1655         rxtid->aggr = false;
1656         rxtid->progress = false;
1657         rxtid->timer_mon = false;
1658         rxtid->win_sz = 0;
1659         rxtid->seq_next = 0;
1660         rxtid->hold_q_sz = 0;
1661
1662         kfree(rxtid->hold_q);
1663         rxtid->hold_q = NULL;
1664
1665         memset(stats, 0, sizeof(struct rxtid_stats));
1666 }
1667
1668 void aggr_recv_addba_req_evt(struct ath6kl_vif *vif, u8 tid_mux, u16 seq_no,
1669                              u8 win_sz)
1670 {
1671         struct ath6kl_sta *sta;
1672         struct aggr_info_conn *aggr_conn = NULL;
1673         struct rxtid *rxtid;
1674         struct rxtid_stats *stats;
1675         u16 hold_q_size;
1676         u8 tid, aid;
1677
1678         if (vif->nw_type == AP_NETWORK) {
1679                 aid = ath6kl_get_aid(tid_mux);
1680                 sta = ath6kl_find_sta_by_aid(vif->ar, aid);
1681                 if (sta)
1682                         aggr_conn = sta->aggr_conn;
1683         } else
1684                 aggr_conn = vif->aggr_cntxt->aggr_conn;
1685
1686         if (!aggr_conn)
1687                 return;
1688
1689         tid = ath6kl_get_tid(tid_mux);
1690         if (tid >= NUM_OF_TIDS)
1691                 return;
1692
1693         rxtid = &aggr_conn->rx_tid[tid];
1694         stats = &aggr_conn->stat[tid];
1695
1696         if (win_sz < AGGR_WIN_SZ_MIN || win_sz > AGGR_WIN_SZ_MAX)
1697                 ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: win_sz %d, tid %d\n",
1698                            __func__, win_sz, tid);
1699
1700         if (rxtid->aggr)
1701                 aggr_delete_tid_state(aggr_conn, tid);
1702
1703         rxtid->seq_next = seq_no;
1704         hold_q_size = TID_WINDOW_SZ(win_sz) * sizeof(struct skb_hold_q);
1705         rxtid->hold_q = kzalloc(hold_q_size, GFP_KERNEL);
1706         if (!rxtid->hold_q)
1707                 return;
1708
1709         rxtid->win_sz = win_sz;
1710         rxtid->hold_q_sz = TID_WINDOW_SZ(win_sz);
1711         if (!skb_queue_empty(&rxtid->q))
1712                 return;
1713
1714         rxtid->aggr = true;
1715 }
1716
1717 void aggr_conn_init(struct ath6kl_vif *vif, struct aggr_info *aggr_info,
1718                     struct aggr_info_conn *aggr_conn)
1719 {
1720         struct rxtid *rxtid;
1721         u8 i;
1722
1723         aggr_conn->aggr_sz = AGGR_SZ_DEFAULT;
1724         aggr_conn->dev = vif->ndev;
1725         init_timer(&aggr_conn->timer);
1726         aggr_conn->timer.function = aggr_timeout;
1727         aggr_conn->timer.data = (unsigned long) aggr_conn;
1728         aggr_conn->aggr_info = aggr_info;
1729
1730         aggr_conn->timer_scheduled = false;
1731
1732         for (i = 0; i < NUM_OF_TIDS; i++) {
1733                 rxtid = &aggr_conn->rx_tid[i];
1734                 rxtid->aggr = false;
1735                 rxtid->progress = false;
1736                 rxtid->timer_mon = false;
1737                 skb_queue_head_init(&rxtid->q);
1738                 spin_lock_init(&rxtid->lock);
1739         }
1740
1741 }
1742
1743 struct aggr_info *aggr_init(struct ath6kl_vif *vif)
1744 {
1745         struct aggr_info *p_aggr = NULL;
1746
1747         p_aggr = kzalloc(sizeof(struct aggr_info), GFP_KERNEL);
1748         if (!p_aggr) {
1749                 ath6kl_err("failed to alloc memory for aggr_node\n");
1750                 return NULL;
1751         }
1752
1753         p_aggr->aggr_conn = kzalloc(sizeof(struct aggr_info_conn), GFP_KERNEL);
1754         if (!p_aggr->aggr_conn) {
1755                 ath6kl_err("failed to alloc memory for connection specific aggr info\n");
1756                 kfree(p_aggr);
1757                 return NULL;
1758         }
1759
1760         aggr_conn_init(vif, p_aggr, p_aggr->aggr_conn);
1761
1762         skb_queue_head_init(&p_aggr->rx_amsdu_freeq);
1763         ath6kl_alloc_netbufs(&p_aggr->rx_amsdu_freeq, AGGR_NUM_OF_FREE_NETBUFS);
1764
1765         return p_aggr;
1766 }
1767
1768 void aggr_recv_delba_req_evt(struct ath6kl_vif *vif, u8 tid_mux)
1769 {
1770         struct ath6kl_sta *sta;
1771         struct rxtid *rxtid;
1772         struct aggr_info_conn *aggr_conn = NULL;
1773         u8 tid, aid;
1774
1775         if (vif->nw_type == AP_NETWORK) {
1776                 aid = ath6kl_get_aid(tid_mux);
1777                 sta = ath6kl_find_sta_by_aid(vif->ar, aid);
1778                 if (sta)
1779                         aggr_conn = sta->aggr_conn;
1780         } else
1781                 aggr_conn = vif->aggr_cntxt->aggr_conn;
1782
1783         if (!aggr_conn)
1784                 return;
1785
1786         tid = ath6kl_get_tid(tid_mux);
1787         if (tid >= NUM_OF_TIDS)
1788                 return;
1789
1790         rxtid = &aggr_conn->rx_tid[tid];
1791
1792         if (rxtid->aggr)
1793                 aggr_delete_tid_state(aggr_conn, tid);
1794 }
1795
1796 void aggr_reset_state(struct aggr_info_conn *aggr_conn)
1797 {
1798         u8 tid;
1799
1800         if (!aggr_conn)
1801                 return;
1802
1803         if (aggr_conn->timer_scheduled) {
1804                 del_timer(&aggr_conn->timer);
1805                 aggr_conn->timer_scheduled = false;
1806         }
1807
1808         for (tid = 0; tid < NUM_OF_TIDS; tid++)
1809                 aggr_delete_tid_state(aggr_conn, tid);
1810 }
1811
1812 /* clean up our amsdu buffer list */
1813 void ath6kl_cleanup_amsdu_rxbufs(struct ath6kl *ar)
1814 {
1815         struct htc_packet *packet, *tmp_pkt;
1816
1817         spin_lock_bh(&ar->lock);
1818         if (list_empty(&ar->amsdu_rx_buffer_queue)) {
1819                 spin_unlock_bh(&ar->lock);
1820                 return;
1821         }
1822
1823         list_for_each_entry_safe(packet, tmp_pkt, &ar->amsdu_rx_buffer_queue,
1824                                  list) {
1825                 list_del(&packet->list);
1826                 spin_unlock_bh(&ar->lock);
1827                 dev_kfree_skb(packet->pkt_cntxt);
1828                 spin_lock_bh(&ar->lock);
1829         }
1830
1831         spin_unlock_bh(&ar->lock);
1832 }
1833
1834 void aggr_module_destroy(struct aggr_info *aggr_info)
1835 {
1836         if (!aggr_info)
1837                 return;
1838
1839         aggr_reset_state(aggr_info->aggr_conn);
1840         skb_queue_purge(&aggr_info->rx_amsdu_freeq);
1841         kfree(aggr_info->aggr_conn);
1842         kfree(aggr_info);
1843 }