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