ath6kl: Release ar->lock right afer updating net_stats in ath6kl_rx()
[pandora-kernel.git] / drivers / net / wireless / ath / ath6kl / txrx.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "core.h"
18 #include "debug.h"
19
20 static u8 ath6kl_ibss_map_epid(struct sk_buff *skb, struct net_device *dev,
21                                u32 *map_no)
22 {
23         struct ath6kl *ar = ath6kl_priv(dev);
24         struct ethhdr *eth_hdr;
25         u32 i, ep_map = -1;
26         u8 *datap;
27
28         *map_no = 0;
29         datap = skb->data;
30         eth_hdr = (struct ethhdr *) (datap + sizeof(struct wmi_data_hdr));
31
32         if (is_multicast_ether_addr(eth_hdr->h_dest))
33                 return ENDPOINT_2;
34
35         for (i = 0; i < ar->node_num; i++) {
36                 if (memcmp(eth_hdr->h_dest, ar->node_map[i].mac_addr,
37                            ETH_ALEN) == 0) {
38                         *map_no = i + 1;
39                         ar->node_map[i].tx_pend++;
40                         return ar->node_map[i].ep_id;
41                 }
42
43                 if ((ep_map == -1) && !ar->node_map[i].tx_pend)
44                         ep_map = i;
45         }
46
47         if (ep_map == -1) {
48                 ep_map = ar->node_num;
49                 ar->node_num++;
50                 if (ar->node_num > MAX_NODE_NUM)
51                         return ENDPOINT_UNUSED;
52         }
53
54         memcpy(ar->node_map[ep_map].mac_addr, eth_hdr->h_dest, ETH_ALEN);
55
56         for (i = ENDPOINT_2; i <= ENDPOINT_5; i++) {
57                 if (!ar->tx_pending[i]) {
58                         ar->node_map[ep_map].ep_id = i;
59                         break;
60                 }
61
62                 /*
63                  * No free endpoint is available, start redistribution on
64                  * the inuse endpoints.
65                  */
66                 if (i == ENDPOINT_5) {
67                         ar->node_map[ep_map].ep_id = ar->next_ep_id;
68                         ar->next_ep_id++;
69                         if (ar->next_ep_id > ENDPOINT_5)
70                                 ar->next_ep_id = ENDPOINT_2;
71                 }
72         }
73
74         *map_no = ep_map + 1;
75         ar->node_map[ep_map].tx_pend++;
76
77         return ar->node_map[ep_map].ep_id;
78 }
79
80 static bool ath6kl_powersave_ap(struct ath6kl *ar, struct sk_buff *skb,
81                                 bool *more_data)
82 {
83         struct ethhdr *datap = (struct ethhdr *) skb->data;
84         struct ath6kl_sta *conn = NULL;
85         bool ps_queued = false, is_psq_empty = false;
86
87         if (is_multicast_ether_addr(datap->h_dest)) {
88                 u8 ctr = 0;
89                 bool q_mcast = false;
90
91                 for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
92                         if (ar->sta_list[ctr].sta_flags & STA_PS_SLEEP) {
93                                 q_mcast = true;
94                                 break;
95                         }
96                 }
97
98                 if (q_mcast) {
99                         /*
100                          * If this transmit is not because of a Dtim Expiry
101                          * q it.
102                          */
103                         if (!test_bit(DTIM_EXPIRED, &ar->flag)) {
104                                 bool is_mcastq_empty = false;
105
106                                 spin_lock_bh(&ar->mcastpsq_lock);
107                                 is_mcastq_empty =
108                                         skb_queue_empty(&ar->mcastpsq);
109                                 skb_queue_tail(&ar->mcastpsq, skb);
110                                 spin_unlock_bh(&ar->mcastpsq_lock);
111
112                                 /*
113                                  * If this is the first Mcast pkt getting
114                                  * queued indicate to the target to set the
115                                  * BitmapControl LSB of the TIM IE.
116                                  */
117                                 if (is_mcastq_empty)
118                                         ath6kl_wmi_set_pvb_cmd(ar->wmi,
119                                                                MCAST_AID, 1);
120
121                                 ps_queued = true;
122                         } else {
123                                 /*
124                                  * This transmit is because of Dtim expiry.
125                                  * Determine if MoreData bit has to be set.
126                                  */
127                                 spin_lock_bh(&ar->mcastpsq_lock);
128                                 if (!skb_queue_empty(&ar->mcastpsq))
129                                         *more_data = true;
130                                 spin_unlock_bh(&ar->mcastpsq_lock);
131                         }
132                 }
133         } else {
134                 conn = ath6kl_find_sta(ar, datap->h_dest);
135                 if (!conn) {
136                         dev_kfree_skb(skb);
137
138                         /* Inform the caller that the skb is consumed */
139                         return true;
140                 }
141
142                 if (conn->sta_flags & STA_PS_SLEEP) {
143                         if (!(conn->sta_flags & STA_PS_POLLED)) {
144                                 /* Queue the frames if the STA is sleeping */
145                                 spin_lock_bh(&conn->psq_lock);
146                                 is_psq_empty = skb_queue_empty(&conn->psq);
147                                 skb_queue_tail(&conn->psq, skb);
148                                 spin_unlock_bh(&conn->psq_lock);
149
150                                 /*
151                                  * If this is the first pkt getting queued
152                                  * for this STA, update the PVB for this
153                                  * STA.
154                                  */
155                                 if (is_psq_empty)
156                                         ath6kl_wmi_set_pvb_cmd(ar->wmi,
157                                                                conn->aid, 1);
158
159                                 ps_queued = true;
160                         } else {
161                                 /*
162                                  * This tx is because of a PsPoll.
163                                  * Determine if MoreData bit has to be set.
164                                  */
165                                 spin_lock_bh(&conn->psq_lock);
166                                 if (!skb_queue_empty(&conn->psq))
167                                         *more_data = true;
168                                 spin_unlock_bh(&conn->psq_lock);
169                         }
170                 }
171         }
172
173         return ps_queued;
174 }
175
176 /* Tx functions */
177
178 int ath6kl_control_tx(void *devt, struct sk_buff *skb,
179                       enum htc_endpoint_id eid)
180 {
181         struct ath6kl *ar = devt;
182         int status = 0;
183         struct ath6kl_cookie *cookie = NULL;
184
185         spin_lock_bh(&ar->lock);
186
187         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
188                    "%s: skb=0x%p, len=0x%x eid =%d\n", __func__,
189                    skb, skb->len, eid);
190
191         if (test_bit(WMI_CTRL_EP_FULL, &ar->flag) && (eid == ar->ctrl_ep)) {
192                 /*
193                  * Control endpoint is full, don't allocate resources, we
194                  * are just going to drop this packet.
195                  */
196                 cookie = NULL;
197                 ath6kl_err("wmi ctrl ep full, dropping pkt : 0x%p, len:%d\n",
198                            skb, skb->len);
199         } else
200                 cookie = ath6kl_alloc_cookie(ar);
201
202         if (cookie == NULL) {
203                 spin_unlock_bh(&ar->lock);
204                 status = -ENOMEM;
205                 goto fail_ctrl_tx;
206         }
207
208         ar->tx_pending[eid]++;
209
210         if (eid != ar->ctrl_ep)
211                 ar->total_tx_data_pend++;
212
213         spin_unlock_bh(&ar->lock);
214
215         cookie->skb = skb;
216         cookie->map_no = 0;
217         set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
218                          eid, ATH6KL_CONTROL_PKT_TAG);
219
220         /*
221          * This interface is asynchronous, if there is an error, cleanup
222          * will happen in the TX completion callback.
223          */
224         ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
225
226         return 0;
227
228 fail_ctrl_tx:
229         dev_kfree_skb(skb);
230         return status;
231 }
232
233 int ath6kl_data_tx(struct sk_buff *skb, struct net_device *dev)
234 {
235         struct ath6kl *ar = ath6kl_priv(dev);
236         struct ath6kl_cookie *cookie = NULL;
237         enum htc_endpoint_id eid = ENDPOINT_UNUSED;
238         u32 map_no = 0;
239         u16 htc_tag = ATH6KL_DATA_PKT_TAG;
240         u8 ac = 99 ; /* initialize to unmapped ac */
241         bool chk_adhoc_ps_mapping = false, more_data = false;
242         struct wmi_tx_meta_v2 meta_v2;
243         int ret;
244
245         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
246                    "%s: skb=0x%p, data=0x%p, len=0x%x\n", __func__,
247                    skb, skb->data, skb->len);
248
249         /* If target is not associated */
250         if (!test_bit(CONNECTED, &ar->flag)) {
251                 dev_kfree_skb(skb);
252                 return 0;
253         }
254
255         if (!test_bit(WMI_READY, &ar->flag))
256                 goto fail_tx;
257
258         /* AP mode Power saving processing */
259         if (ar->nw_type == AP_NETWORK) {
260                 if (ath6kl_powersave_ap(ar, skb, &more_data))
261                         return 0;
262         }
263
264         if (test_bit(WMI_ENABLED, &ar->flag)) {
265                 memset(&meta_v2, 0, sizeof(meta_v2));
266
267                 if (skb_headroom(skb) < dev->needed_headroom) {
268                         WARN_ON(1);
269                         goto fail_tx;
270                 }
271
272                 if (ath6kl_wmi_dix_2_dot3(ar->wmi, skb)) {
273                         ath6kl_err("ath6kl_wmi_dix_2_dot3 failed\n");
274                         goto fail_tx;
275                 }
276
277                 if (ath6kl_wmi_data_hdr_add(ar->wmi, skb, DATA_MSGTYPE,
278                                             more_data, 0, 0, NULL)) {
279                         ath6kl_err("wmi_data_hdr_add failed\n");
280                         goto fail_tx;
281                 }
282
283                 if ((ar->nw_type == ADHOC_NETWORK) &&
284                      ar->ibss_ps_enable && test_bit(CONNECTED, &ar->flag))
285                         chk_adhoc_ps_mapping = true;
286                 else {
287                         /* get the stream mapping */
288                         ret = ath6kl_wmi_implicit_create_pstream(ar->wmi, skb,
289                                     0, test_bit(WMM_ENABLED, &ar->flag), &ac);
290                         if (ret)
291                                 goto fail_tx;
292                 }
293         } else
294                 goto fail_tx;
295
296         spin_lock_bh(&ar->lock);
297
298         if (chk_adhoc_ps_mapping)
299                 eid = ath6kl_ibss_map_epid(skb, dev, &map_no);
300         else
301                 eid = ar->ac2ep_map[ac];
302
303         if (eid == 0 || eid == ENDPOINT_UNUSED) {
304                 ath6kl_err("eid %d is not mapped!\n", eid);
305                 spin_unlock_bh(&ar->lock);
306                 goto fail_tx;
307         }
308
309         /* allocate resource for this packet */
310         cookie = ath6kl_alloc_cookie(ar);
311
312         if (!cookie) {
313                 spin_unlock_bh(&ar->lock);
314                 goto fail_tx;
315         }
316
317         /* update counts while the lock is held */
318         ar->tx_pending[eid]++;
319         ar->total_tx_data_pend++;
320
321         spin_unlock_bh(&ar->lock);
322
323         cookie->skb = skb;
324         cookie->map_no = map_no;
325         set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
326                          eid, htc_tag);
327
328         ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, skb->data, skb->len);
329
330         /*
331          * HTC interface is asynchronous, if this fails, cleanup will
332          * happen in the ath6kl_tx_complete callback.
333          */
334         ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
335
336         return 0;
337
338 fail_tx:
339         dev_kfree_skb(skb);
340
341         ar->net_stats.tx_dropped++;
342         ar->net_stats.tx_aborted_errors++;
343
344         return 0;
345 }
346
347 /* indicate tx activity or inactivity on a WMI stream */
348 void ath6kl_indicate_tx_activity(void *devt, u8 traffic_class, bool active)
349 {
350         struct ath6kl *ar = devt;
351         enum htc_endpoint_id eid;
352         int i;
353
354         eid = ar->ac2ep_map[traffic_class];
355
356         if (!test_bit(WMI_ENABLED, &ar->flag))
357                 goto notify_htc;
358
359         spin_lock_bh(&ar->lock);
360
361         ar->ac_stream_active[traffic_class] = active;
362
363         if (active) {
364                 /*
365                  * Keep track of the active stream with the highest
366                  * priority.
367                  */
368                 if (ar->ac_stream_pri_map[traffic_class] >
369                     ar->hiac_stream_active_pri)
370                         /* set the new highest active priority */
371                         ar->hiac_stream_active_pri =
372                                         ar->ac_stream_pri_map[traffic_class];
373
374         } else {
375                 /*
376                  * We may have to search for the next active stream
377                  * that is the highest priority.
378                  */
379                 if (ar->hiac_stream_active_pri ==
380                         ar->ac_stream_pri_map[traffic_class]) {
381                         /*
382                          * The highest priority stream just went inactive
383                          * reset and search for the "next" highest "active"
384                          * priority stream.
385                          */
386                         ar->hiac_stream_active_pri = 0;
387
388                         for (i = 0; i < WMM_NUM_AC; i++) {
389                                 if (ar->ac_stream_active[i] &&
390                                     (ar->ac_stream_pri_map[i] >
391                                      ar->hiac_stream_active_pri))
392                                         /*
393                                          * Set the new highest active
394                                          * priority.
395                                          */
396                                         ar->hiac_stream_active_pri =
397                                                 ar->ac_stream_pri_map[i];
398                         }
399                 }
400         }
401
402         spin_unlock_bh(&ar->lock);
403
404 notify_htc:
405         /* notify HTC, this may cause credit distribution changes */
406         ath6kl_htc_indicate_activity_change(ar->htc_target, eid, active);
407 }
408
409 enum htc_send_full_action ath6kl_tx_queue_full(struct htc_target *target,
410                                                struct htc_packet *packet)
411 {
412         struct ath6kl *ar = target->dev->ar;
413         enum htc_endpoint_id endpoint = packet->endpoint;
414
415         if (endpoint == ar->ctrl_ep) {
416                 /*
417                  * Under normal WMI if this is getting full, then something
418                  * is running rampant the host should not be exhausting the
419                  * WMI queue with too many commands the only exception to
420                  * this is during testing using endpointping.
421                  */
422                 spin_lock_bh(&ar->lock);
423                 set_bit(WMI_CTRL_EP_FULL, &ar->flag);
424                 spin_unlock_bh(&ar->lock);
425                 ath6kl_err("wmi ctrl ep is full\n");
426                 return HTC_SEND_FULL_KEEP;
427         }
428
429         if (packet->info.tx.tag == ATH6KL_CONTROL_PKT_TAG)
430                 return HTC_SEND_FULL_KEEP;
431
432         if (ar->nw_type == ADHOC_NETWORK)
433                 /*
434                  * In adhoc mode, we cannot differentiate traffic
435                  * priorities so there is no need to continue, however we
436                  * should stop the network.
437                  */
438                 goto stop_net_queues;
439
440         /*
441          * The last MAX_HI_COOKIE_NUM "batch" of cookies are reserved for
442          * the highest active stream.
443          */
444         if (ar->ac_stream_pri_map[ar->ep2ac_map[endpoint]] <
445             ar->hiac_stream_active_pri &&
446             ar->cookie_count <= MAX_HI_COOKIE_NUM)
447                 /*
448                  * Give preference to the highest priority stream by
449                  * dropping the packets which overflowed.
450                  */
451                 return HTC_SEND_FULL_DROP;
452
453 stop_net_queues:
454         spin_lock_bh(&ar->lock);
455         set_bit(NETQ_STOPPED, &ar->flag);
456         spin_unlock_bh(&ar->lock);
457         netif_stop_queue(ar->net_dev);
458
459         return HTC_SEND_FULL_KEEP;
460 }
461
462 /* TODO this needs to be looked at */
463 static void ath6kl_tx_clear_node_map(struct ath6kl *ar,
464                                      enum htc_endpoint_id eid, u32 map_no)
465 {
466         u32 i;
467
468         if (ar->nw_type != ADHOC_NETWORK)
469                 return;
470
471         if (!ar->ibss_ps_enable)
472                 return;
473
474         if (eid == ar->ctrl_ep)
475                 return;
476
477         if (map_no == 0)
478                 return;
479
480         map_no--;
481         ar->node_map[map_no].tx_pend--;
482
483         if (ar->node_map[map_no].tx_pend)
484                 return;
485
486         if (map_no != (ar->node_num - 1))
487                 return;
488
489         for (i = ar->node_num; i > 0; i--) {
490                 if (ar->node_map[i - 1].tx_pend)
491                         break;
492
493                 memset(&ar->node_map[i - 1], 0,
494                        sizeof(struct ath6kl_node_mapping));
495                 ar->node_num--;
496         }
497 }
498
499 void ath6kl_tx_complete(void *context, struct list_head *packet_queue)
500 {
501         struct ath6kl *ar = context;
502         struct sk_buff_head skb_queue;
503         struct htc_packet *packet;
504         struct sk_buff *skb;
505         struct ath6kl_cookie *ath6kl_cookie;
506         u32 map_no = 0;
507         int status;
508         enum htc_endpoint_id eid;
509         bool wake_event = false;
510         bool flushing = false;
511
512         skb_queue_head_init(&skb_queue);
513
514         /* lock the driver as we update internal state */
515         spin_lock_bh(&ar->lock);
516
517         /* reap completed packets */
518         while (!list_empty(packet_queue)) {
519
520                 packet = list_first_entry(packet_queue, struct htc_packet,
521                                           list);
522                 list_del(&packet->list);
523
524                 ath6kl_cookie = (struct ath6kl_cookie *)packet->pkt_cntxt;
525                 if (!ath6kl_cookie)
526                         goto fatal;
527
528                 status = packet->status;
529                 skb = ath6kl_cookie->skb;
530                 eid = packet->endpoint;
531                 map_no = ath6kl_cookie->map_no;
532
533                 if (!skb || !skb->data)
534                         goto fatal;
535
536                 packet->buf = skb->data;
537
538                 __skb_queue_tail(&skb_queue, skb);
539
540                 if (!status && (packet->act_len != skb->len))
541                         goto fatal;
542
543                 ar->tx_pending[eid]--;
544
545                 if (eid != ar->ctrl_ep)
546                         ar->total_tx_data_pend--;
547
548                 if (eid == ar->ctrl_ep) {
549                         if (test_bit(WMI_CTRL_EP_FULL, &ar->flag))
550                                 clear_bit(WMI_CTRL_EP_FULL, &ar->flag);
551
552                         if (ar->tx_pending[eid] == 0)
553                                 wake_event = true;
554                 }
555
556                 if (status) {
557                         if (status == -ECANCELED)
558                                 /* a packet was flushed  */
559                                 flushing = true;
560
561                         ar->net_stats.tx_errors++;
562
563                         if (status != -ENOSPC)
564                                 ath6kl_err("tx error, status: 0x%x\n", status);
565                         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
566                                    "%s: skb=0x%p data=0x%p len=0x%x eid=%d %s\n",
567                                    __func__, skb, packet->buf, packet->act_len,
568                                    eid, "error!");
569                 } else {
570                         ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
571                                    "%s: skb=0x%p data=0x%p len=0x%x eid=%d %s\n",
572                                    __func__, skb, packet->buf, packet->act_len,
573                                    eid, "OK");
574
575                         flushing = false;
576                         ar->net_stats.tx_packets++;
577                         ar->net_stats.tx_bytes += skb->len;
578                 }
579
580                 ath6kl_tx_clear_node_map(ar, eid, map_no);
581
582                 ath6kl_free_cookie(ar, ath6kl_cookie);
583
584                 if (test_bit(NETQ_STOPPED, &ar->flag))
585                         clear_bit(NETQ_STOPPED, &ar->flag);
586         }
587
588         spin_unlock_bh(&ar->lock);
589
590         __skb_queue_purge(&skb_queue);
591
592         if (test_bit(CONNECTED, &ar->flag)) {
593                 if (!flushing)
594                         netif_wake_queue(ar->net_dev);
595         }
596
597         if (wake_event)
598                 wake_up(&ar->event_wq);
599
600         return;
601
602 fatal:
603         WARN_ON(1);
604         spin_unlock_bh(&ar->lock);
605         return;
606 }
607
608 void ath6kl_tx_data_cleanup(struct ath6kl *ar)
609 {
610         int i;
611
612         /* flush all the data (non-control) streams */
613         for (i = 0; i < WMM_NUM_AC; i++)
614                 ath6kl_htc_flush_txep(ar->htc_target, ar->ac2ep_map[i],
615                                       ATH6KL_DATA_PKT_TAG);
616 }
617
618 /* Rx functions */
619
620 static void ath6kl_deliver_frames_to_nw_stack(struct net_device *dev,
621                                               struct sk_buff *skb)
622 {
623         if (!skb)
624                 return;
625
626         skb->dev = dev;
627
628         if (!(skb->dev->flags & IFF_UP)) {
629                 dev_kfree_skb(skb);
630                 return;
631         }
632
633         skb->protocol = eth_type_trans(skb, skb->dev);
634
635         netif_rx_ni(skb);
636 }
637
638 static void ath6kl_alloc_netbufs(struct sk_buff_head *q, u16 num)
639 {
640         struct sk_buff *skb;
641
642         while (num) {
643                 skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
644                 if (!skb) {
645                         ath6kl_err("netbuf allocation failed\n");
646                         return;
647                 }
648                 skb_queue_tail(q, skb);
649                 num--;
650         }
651 }
652
653 static struct sk_buff *aggr_get_free_skb(struct aggr_info *p_aggr)
654 {
655         struct sk_buff *skb = NULL;
656
657         if (skb_queue_len(&p_aggr->free_q) < (AGGR_NUM_OF_FREE_NETBUFS >> 2))
658                 ath6kl_alloc_netbufs(&p_aggr->free_q, AGGR_NUM_OF_FREE_NETBUFS);
659
660         skb = skb_dequeue(&p_aggr->free_q);
661
662         return skb;
663 }
664
665 void ath6kl_rx_refill(struct htc_target *target, enum htc_endpoint_id endpoint)
666 {
667         struct ath6kl *ar = target->dev->ar;
668         struct sk_buff *skb;
669         int rx_buf;
670         int n_buf_refill;
671         struct htc_packet *packet;
672         struct list_head queue;
673
674         n_buf_refill = ATH6KL_MAX_RX_BUFFERS -
675                           ath6kl_htc_get_rxbuf_num(ar->htc_target, endpoint);
676
677         if (n_buf_refill <= 0)
678                 return;
679
680         INIT_LIST_HEAD(&queue);
681
682         ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
683                    "%s: providing htc with %d buffers at eid=%d\n",
684                    __func__, n_buf_refill, endpoint);
685
686         for (rx_buf = 0; rx_buf < n_buf_refill; rx_buf++) {
687                 skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
688                 if (!skb)
689                         break;
690
691                 packet = (struct htc_packet *) skb->head;
692                 set_htc_rxpkt_info(packet, skb, skb->data,
693                                 ATH6KL_BUFFER_SIZE, endpoint);
694                 list_add_tail(&packet->list, &queue);
695         }
696
697         if (!list_empty(&queue))
698                 ath6kl_htc_add_rxbuf_multiple(ar->htc_target, &queue);
699 }
700
701 void ath6kl_refill_amsdu_rxbufs(struct ath6kl *ar, int count)
702 {
703         struct htc_packet *packet;
704         struct sk_buff *skb;
705
706         while (count) {
707                 skb = ath6kl_buf_alloc(ATH6KL_AMSDU_BUFFER_SIZE);
708                 if (!skb)
709                         return;
710
711                 packet = (struct htc_packet *) skb->head;
712                 set_htc_rxpkt_info(packet, skb, skb->data,
713                                    ATH6KL_AMSDU_BUFFER_SIZE, 0);
714                 spin_lock_bh(&ar->lock);
715                 list_add_tail(&packet->list, &ar->amsdu_rx_buffer_queue);
716                 spin_unlock_bh(&ar->lock);
717                 count--;
718         }
719 }
720
721 /*
722  * Callback to allocate a receive buffer for a pending packet. We use a
723  * pre-allocated list of buffers of maximum AMSDU size (4K).
724  */
725 struct htc_packet *ath6kl_alloc_amsdu_rxbuf(struct htc_target *target,
726                                             enum htc_endpoint_id endpoint,
727                                             int len)
728 {
729         struct ath6kl *ar = target->dev->ar;
730         struct htc_packet *packet = NULL;
731         struct list_head *pkt_pos;
732         int refill_cnt = 0, depth = 0;
733
734         ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: eid=%d, len:%d\n",
735                    __func__, endpoint, len);
736
737         if ((len <= ATH6KL_BUFFER_SIZE) ||
738             (len > ATH6KL_AMSDU_BUFFER_SIZE))
739                 return NULL;
740
741         spin_lock_bh(&ar->lock);
742
743         if (list_empty(&ar->amsdu_rx_buffer_queue)) {
744                 spin_unlock_bh(&ar->lock);
745                 refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS;
746                 goto refill_buf;
747         }
748
749         packet = list_first_entry(&ar->amsdu_rx_buffer_queue,
750                                   struct htc_packet, list);
751         list_del(&packet->list);
752         list_for_each(pkt_pos, &ar->amsdu_rx_buffer_queue)
753                 depth++;
754
755         refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS - depth;
756         spin_unlock_bh(&ar->lock);
757
758         /* set actual endpoint ID */
759         packet->endpoint = endpoint;
760
761 refill_buf:
762         if (refill_cnt >= ATH6KL_AMSDU_REFILL_THRESHOLD)
763                 ath6kl_refill_amsdu_rxbufs(ar, refill_cnt);
764
765         return packet;
766 }
767
768 static void aggr_slice_amsdu(struct aggr_info *p_aggr,
769                              struct rxtid *rxtid, struct sk_buff *skb)
770 {
771         struct sk_buff *new_skb;
772         struct ethhdr *hdr;
773         u16 frame_8023_len, payload_8023_len, mac_hdr_len, amsdu_len;
774         u8 *framep;
775
776         mac_hdr_len = sizeof(struct ethhdr);
777         framep = skb->data + mac_hdr_len;
778         amsdu_len = skb->len - mac_hdr_len;
779
780         while (amsdu_len > mac_hdr_len) {
781                 hdr = (struct ethhdr *) framep;
782                 payload_8023_len = ntohs(hdr->h_proto);
783
784                 if (payload_8023_len < MIN_MSDU_SUBFRAME_PAYLOAD_LEN ||
785                     payload_8023_len > MAX_MSDU_SUBFRAME_PAYLOAD_LEN) {
786                         ath6kl_err("802.3 AMSDU frame bound check failed. len %d\n",
787                                    payload_8023_len);
788                         break;
789                 }
790
791                 frame_8023_len = payload_8023_len + mac_hdr_len;
792                 new_skb = aggr_get_free_skb(p_aggr);
793                 if (!new_skb) {
794                         ath6kl_err("no buffer available\n");
795                         break;
796                 }
797
798                 memcpy(new_skb->data, framep, frame_8023_len);
799                 skb_put(new_skb, frame_8023_len);
800                 if (ath6kl_wmi_dot3_2_dix(new_skb)) {
801                         ath6kl_err("dot3_2_dix error\n");
802                         dev_kfree_skb(new_skb);
803                         break;
804                 }
805
806                 skb_queue_tail(&rxtid->q, new_skb);
807
808                 /* Is this the last subframe within this aggregate ? */
809                 if ((amsdu_len - frame_8023_len) == 0)
810                         break;
811
812                 /* Add the length of A-MSDU subframe padding bytes -
813                  * Round to nearest word.
814                  */
815                 frame_8023_len = ALIGN(frame_8023_len + 3, 3);
816
817                 framep += frame_8023_len;
818                 amsdu_len -= frame_8023_len;
819         }
820
821         dev_kfree_skb(skb);
822 }
823
824 static void aggr_deque_frms(struct aggr_info *p_aggr, u8 tid,
825                             u16 seq_no, u8 order)
826 {
827         struct sk_buff *skb;
828         struct rxtid *rxtid;
829         struct skb_hold_q *node;
830         u16 idx, idx_end, seq_end;
831         struct rxtid_stats *stats;
832
833         if (!p_aggr)
834                 return;
835
836         rxtid = &p_aggr->rx_tid[tid];
837         stats = &p_aggr->stat[tid];
838
839         idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);
840
841         /*
842          * idx_end is typically the last possible frame in the window,
843          * but changes to 'the' seq_no, when BAR comes. If seq_no
844          * is non-zero, we will go up to that and stop.
845          * Note: last seq no in current window will occupy the same
846          * index position as index that is just previous to start.
847          * An imp point : if win_sz is 7, for seq_no space of 4095,
848          * then, there would be holes when sequence wrap around occurs.
849          * Target should judiciously choose the win_sz, based on
850          * this condition. For 4095, (TID_WINDOW_SZ = 2 x win_sz
851          * 2, 4, 8, 16 win_sz works fine).
852          * We must deque from "idx" to "idx_end", including both.
853          */
854         seq_end = seq_no ? seq_no : rxtid->seq_next;
855         idx_end = AGGR_WIN_IDX(seq_end, rxtid->hold_q_sz);
856
857         spin_lock_bh(&rxtid->lock);
858
859         do {
860                 node = &rxtid->hold_q[idx];
861                 if ((order == 1) && (!node->skb))
862                         break;
863
864                 if (node->skb) {
865                         if (node->is_amsdu)
866                                 aggr_slice_amsdu(p_aggr, rxtid, node->skb);
867                         else
868                                 skb_queue_tail(&rxtid->q, node->skb);
869                         node->skb = NULL;
870                 } else
871                         stats->num_hole++;
872
873                 rxtid->seq_next = ATH6KL_NEXT_SEQ_NO(rxtid->seq_next);
874                 idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);
875         } while (idx != idx_end);
876
877         spin_unlock_bh(&rxtid->lock);
878
879         stats->num_delivered += skb_queue_len(&rxtid->q);
880
881         while ((skb = skb_dequeue(&rxtid->q)))
882                 ath6kl_deliver_frames_to_nw_stack(p_aggr->dev, skb);
883 }
884
885 static bool aggr_process_recv_frm(struct aggr_info *agg_info, u8 tid,
886                                   u16 seq_no,
887                                   bool is_amsdu, struct sk_buff *frame)
888 {
889         struct rxtid *rxtid;
890         struct rxtid_stats *stats;
891         struct sk_buff *skb;
892         struct skb_hold_q *node;
893         u16 idx, st, cur, end;
894         bool is_queued = false;
895         u16 extended_end;
896
897         rxtid = &agg_info->rx_tid[tid];
898         stats = &agg_info->stat[tid];
899
900         stats->num_into_aggr++;
901
902         if (!rxtid->aggr) {
903                 if (is_amsdu) {
904                         aggr_slice_amsdu(agg_info, rxtid, frame);
905                         is_queued = true;
906                         stats->num_amsdu++;
907                         while ((skb = skb_dequeue(&rxtid->q)))
908                                 ath6kl_deliver_frames_to_nw_stack(agg_info->dev,
909                                                                   skb);
910                 }
911                 return is_queued;
912         }
913
914         /* Check the incoming sequence no, if it's in the window */
915         st = rxtid->seq_next;
916         cur = seq_no;
917         end = (st + rxtid->hold_q_sz-1) & ATH6KL_MAX_SEQ_NO;
918
919         if (((st < end) && (cur < st || cur > end)) ||
920             ((st > end) && (cur > end) && (cur < st))) {
921                 extended_end = (end + rxtid->hold_q_sz - 1) &
922                         ATH6KL_MAX_SEQ_NO;
923
924                 if (((end < extended_end) &&
925                      (cur < end || cur > extended_end)) ||
926                     ((end > extended_end) && (cur > extended_end) &&
927                      (cur < end))) {
928                         aggr_deque_frms(agg_info, tid, 0, 0);
929                         if (cur >= rxtid->hold_q_sz - 1)
930                                 rxtid->seq_next = cur - (rxtid->hold_q_sz - 1);
931                         else
932                                 rxtid->seq_next = ATH6KL_MAX_SEQ_NO -
933                                                   (rxtid->hold_q_sz - 2 - cur);
934                 } else {
935                         /*
936                          * Dequeue only those frames that are outside the
937                          * new shifted window.
938                          */
939                         if (cur >= rxtid->hold_q_sz - 1)
940                                 st = cur - (rxtid->hold_q_sz - 1);
941                         else
942                                 st = ATH6KL_MAX_SEQ_NO -
943                                         (rxtid->hold_q_sz - 2 - cur);
944
945                         aggr_deque_frms(agg_info, tid, st, 0);
946                 }
947
948                 stats->num_oow++;
949         }
950
951         idx = AGGR_WIN_IDX(seq_no, rxtid->hold_q_sz);
952
953         node = &rxtid->hold_q[idx];
954
955         spin_lock_bh(&rxtid->lock);
956
957         /*
958          * Is the cur frame duplicate or something beyond our window(hold_q
959          * -> which is 2x, already)?
960          *
961          * 1. Duplicate is easy - drop incoming frame.
962          * 2. Not falling in current sliding window.
963          *  2a. is the frame_seq_no preceding current tid_seq_no?
964          *      -> drop the frame. perhaps sender did not get our ACK.
965          *         this is taken care of above.
966          *  2b. is the frame_seq_no beyond window(st, TID_WINDOW_SZ);
967          *      -> Taken care of it above, by moving window forward.
968          */
969         dev_kfree_skb(node->skb);
970         stats->num_dups++;
971
972         node->skb = frame;
973         is_queued = true;
974         node->is_amsdu = is_amsdu;
975         node->seq_no = seq_no;
976
977         if (node->is_amsdu)
978                 stats->num_amsdu++;
979         else
980                 stats->num_mpdu++;
981
982         spin_unlock_bh(&rxtid->lock);
983
984         aggr_deque_frms(agg_info, tid, 0, 1);
985
986         if (agg_info->timer_scheduled)
987                 rxtid->progress = true;
988         else
989                 for (idx = 0 ; idx < rxtid->hold_q_sz; idx++) {
990                         if (rxtid->hold_q[idx].skb) {
991                                 /*
992                                  * There is a frame in the queue and no
993                                  * timer so start a timer to ensure that
994                                  * the frame doesn't remain stuck
995                                  * forever.
996                                  */
997                                 agg_info->timer_scheduled = true;
998                                 mod_timer(&agg_info->timer,
999                                           (jiffies +
1000                                            HZ * (AGGR_RX_TIMEOUT) / 1000));
1001                                 rxtid->progress = false;
1002                                 rxtid->timer_mon = true;
1003                                 break;
1004                         }
1005                 }
1006
1007         return is_queued;
1008 }
1009
1010 void ath6kl_rx(struct htc_target *target, struct htc_packet *packet)
1011 {
1012         struct ath6kl *ar = target->dev->ar;
1013         struct sk_buff *skb = packet->pkt_cntxt;
1014         struct wmi_rx_meta_v2 *meta;
1015         struct wmi_data_hdr *dhdr;
1016         int min_hdr_len;
1017         u8 meta_type, dot11_hdr = 0;
1018         int status = packet->status;
1019         enum htc_endpoint_id ept = packet->endpoint;
1020         bool is_amsdu, prev_ps, ps_state = false;
1021         struct ath6kl_sta *conn = NULL;
1022         struct sk_buff *skb1 = NULL;
1023         struct ethhdr *datap = NULL;
1024         u16 seq_no, offset;
1025         u8 tid;
1026
1027         ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
1028                    "%s: ar=0x%p eid=%d, skb=0x%p, data=0x%p, len=0x%x status:%d",
1029                    __func__, ar, ept, skb, packet->buf,
1030                    packet->act_len, status);
1031
1032         if (status || !(skb->data + HTC_HDR_LENGTH)) {
1033                 ar->net_stats.rx_errors++;
1034                 dev_kfree_skb(skb);
1035                 return;
1036         }
1037
1038         /*
1039          * Take lock to protect buffer counts and adaptive power throughput
1040          * state.
1041          */
1042         spin_lock_bh(&ar->lock);
1043
1044         ar->net_stats.rx_packets++;
1045         ar->net_stats.rx_bytes += packet->act_len;
1046
1047         spin_unlock_bh(&ar->lock);
1048
1049         skb_put(skb, packet->act_len + HTC_HDR_LENGTH);
1050         skb_pull(skb, HTC_HDR_LENGTH);
1051
1052         ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, skb->data, skb->len);
1053
1054         skb->dev = ar->net_dev;
1055
1056         if (!test_bit(WMI_ENABLED, &ar->flag)) {
1057                 if (EPPING_ALIGNMENT_PAD > 0)
1058                         skb_pull(skb, EPPING_ALIGNMENT_PAD);
1059                 ath6kl_deliver_frames_to_nw_stack(ar->net_dev, skb);
1060                 return;
1061         }
1062
1063         if (ept == ar->ctrl_ep) {
1064                 ath6kl_wmi_control_rx(ar->wmi, skb);
1065                 return;
1066         }
1067
1068         min_hdr_len = sizeof(struct ethhdr);
1069         min_hdr_len += sizeof(struct wmi_data_hdr) +
1070                        sizeof(struct ath6kl_llc_snap_hdr);
1071
1072         dhdr = (struct wmi_data_hdr *) skb->data;
1073
1074         /*
1075          * In the case of AP mode we may receive NULL data frames
1076          * that do not have LLC hdr. They are 16 bytes in size.
1077          * Allow these frames in the AP mode.
1078          */
1079         if (ar->nw_type != AP_NETWORK &&
1080             ((packet->act_len < min_hdr_len) ||
1081              (packet->act_len > WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH))) {
1082                 ath6kl_info("frame len is too short or too long\n");
1083                 ar->net_stats.rx_errors++;
1084                 ar->net_stats.rx_length_errors++;
1085                 dev_kfree_skb(skb);
1086                 return;
1087         }
1088
1089         /* Get the Power save state of the STA */
1090         if (ar->nw_type == AP_NETWORK) {
1091                 meta_type = wmi_data_hdr_get_meta(dhdr);
1092
1093                 ps_state = !!((dhdr->info >> WMI_DATA_HDR_PS_SHIFT) &
1094                               WMI_DATA_HDR_PS_MASK);
1095
1096                 offset = sizeof(struct wmi_data_hdr);
1097
1098                 switch (meta_type) {
1099                 case 0:
1100                         break;
1101                 case WMI_META_VERSION_1:
1102                         offset += sizeof(struct wmi_rx_meta_v1);
1103                         break;
1104                 case WMI_META_VERSION_2:
1105                         offset += sizeof(struct wmi_rx_meta_v2);
1106                         break;
1107                 default:
1108                         break;
1109                 }
1110
1111                 datap = (struct ethhdr *) (skb->data + offset);
1112                 conn = ath6kl_find_sta(ar, datap->h_source);
1113
1114                 if (!conn) {
1115                         dev_kfree_skb(skb);
1116                         return;
1117                 }
1118
1119                 /*
1120                  * If there is a change in PS state of the STA,
1121                  * take appropriate steps:
1122                  *
1123                  * 1. If Sleep-->Awake, flush the psq for the STA
1124                  *    Clear the PVB for the STA.
1125                  * 2. If Awake-->Sleep, Starting queueing frames
1126                  *    the STA.
1127                  */
1128                 prev_ps = !!(conn->sta_flags & STA_PS_SLEEP);
1129
1130                 if (ps_state)
1131                         conn->sta_flags |= STA_PS_SLEEP;
1132                 else
1133                         conn->sta_flags &= ~STA_PS_SLEEP;
1134
1135                 if (prev_ps ^ !!(conn->sta_flags & STA_PS_SLEEP)) {
1136                         if (!(conn->sta_flags & STA_PS_SLEEP)) {
1137                                 struct sk_buff *skbuff = NULL;
1138
1139                                 spin_lock_bh(&conn->psq_lock);
1140                                 while ((skbuff = skb_dequeue(&conn->psq))
1141                                        != NULL) {
1142                                         spin_unlock_bh(&conn->psq_lock);
1143                                         ath6kl_data_tx(skbuff, ar->net_dev);
1144                                         spin_lock_bh(&conn->psq_lock);
1145                                 }
1146                                 spin_unlock_bh(&conn->psq_lock);
1147                                 /* Clear the PVB for this STA */
1148                                 ath6kl_wmi_set_pvb_cmd(ar->wmi, conn->aid, 0);
1149                         }
1150                 }
1151
1152                 /* drop NULL data frames here */
1153                 if ((packet->act_len < min_hdr_len) ||
1154                     (packet->act_len >
1155                      WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH)) {
1156                         dev_kfree_skb(skb);
1157                         return;
1158                 }
1159         }
1160
1161         is_amsdu = wmi_data_hdr_is_amsdu(dhdr) ? true : false;
1162         tid = wmi_data_hdr_get_up(dhdr);
1163         seq_no = wmi_data_hdr_get_seqno(dhdr);
1164         meta_type = wmi_data_hdr_get_meta(dhdr);
1165         dot11_hdr = wmi_data_hdr_get_dot11(dhdr);
1166
1167         ath6kl_wmi_data_hdr_remove(ar->wmi, skb);
1168
1169         switch (meta_type) {
1170         case WMI_META_VERSION_1:
1171                 skb_pull(skb, sizeof(struct wmi_rx_meta_v1));
1172                 break;
1173         case WMI_META_VERSION_2:
1174                 meta = (struct wmi_rx_meta_v2 *) skb->data;
1175                 if (meta->csum_flags & 0x1) {
1176                         skb->ip_summed = CHECKSUM_COMPLETE;
1177                         skb->csum = (__force __wsum) meta->csum;
1178                 }
1179                 skb_pull(skb, sizeof(struct wmi_rx_meta_v2));
1180                 break;
1181         default:
1182                 break;
1183         }
1184
1185         if (dot11_hdr)
1186                 status = ath6kl_wmi_dot11_hdr_remove(ar->wmi, skb);
1187         else if (!is_amsdu)
1188                 status = ath6kl_wmi_dot3_2_dix(skb);
1189
1190         if (status) {
1191                 /*
1192                  * Drop frames that could not be processed (lack of
1193                  * memory, etc.)
1194                  */
1195                 dev_kfree_skb(skb);
1196                 return;
1197         }
1198
1199         if (!(ar->net_dev->flags & IFF_UP)) {
1200                 dev_kfree_skb(skb);
1201                 return;
1202         }
1203
1204         if (ar->nw_type == AP_NETWORK) {
1205                 datap = (struct ethhdr *) skb->data;
1206                 if (is_multicast_ether_addr(datap->h_dest))
1207                         /*
1208                          * Bcast/Mcast frames should be sent to the
1209                          * OS stack as well as on the air.
1210                          */
1211                         skb1 = skb_copy(skb, GFP_ATOMIC);
1212                 else {
1213                         /*
1214                          * Search for a connected STA with dstMac
1215                          * as the Mac address. If found send the
1216                          * frame to it on the air else send the
1217                          * frame up the stack.
1218                          */
1219                         struct ath6kl_sta *conn = NULL;
1220                         conn = ath6kl_find_sta(ar, datap->h_dest);
1221
1222                         if (conn && ar->intra_bss) {
1223                                 skb1 = skb;
1224                                 skb = NULL;
1225                         } else if (conn && !ar->intra_bss) {
1226                                 dev_kfree_skb(skb);
1227                                 skb = NULL;
1228                         }
1229                 }
1230                 if (skb1)
1231                         ath6kl_data_tx(skb1, ar->net_dev);
1232         }
1233
1234         if (!aggr_process_recv_frm(ar->aggr_cntxt, tid, seq_no,
1235                                    is_amsdu, skb))
1236                 ath6kl_deliver_frames_to_nw_stack(ar->net_dev, skb);
1237 }
1238
1239 static void aggr_timeout(unsigned long arg)
1240 {
1241         u8 i, j;
1242         struct aggr_info *p_aggr = (struct aggr_info *) arg;
1243         struct rxtid *rxtid;
1244         struct rxtid_stats *stats;
1245
1246         for (i = 0; i < NUM_OF_TIDS; i++) {
1247                 rxtid = &p_aggr->rx_tid[i];
1248                 stats = &p_aggr->stat[i];
1249
1250                 if (!rxtid->aggr || !rxtid->timer_mon || rxtid->progress)
1251                         continue;
1252
1253                 /*
1254                  * FIXME: these timeouts happen quite fruently, something
1255                  * line once within 60 seconds. Investigate why.
1256                  */
1257                 stats->num_timeouts++;
1258                 ath6kl_dbg(ATH6KL_DBG_AGGR,
1259                            "aggr timeout (st %d end %d)\n",
1260                            rxtid->seq_next,
1261                            ((rxtid->seq_next + rxtid->hold_q_sz-1) &
1262                             ATH6KL_MAX_SEQ_NO));
1263                 aggr_deque_frms(p_aggr, i, 0, 0);
1264         }
1265
1266         p_aggr->timer_scheduled = false;
1267
1268         for (i = 0; i < NUM_OF_TIDS; i++) {
1269                 rxtid = &p_aggr->rx_tid[i];
1270
1271                 if (rxtid->aggr && rxtid->hold_q) {
1272                         for (j = 0; j < rxtid->hold_q_sz; j++) {
1273                                 if (rxtid->hold_q[j].skb) {
1274                                         p_aggr->timer_scheduled = true;
1275                                         rxtid->timer_mon = true;
1276                                         rxtid->progress = false;
1277                                         break;
1278                                 }
1279                         }
1280
1281                         if (j >= rxtid->hold_q_sz)
1282                                 rxtid->timer_mon = false;
1283                 }
1284         }
1285
1286         if (p_aggr->timer_scheduled)
1287                 mod_timer(&p_aggr->timer,
1288                           jiffies + msecs_to_jiffies(AGGR_RX_TIMEOUT));
1289 }
1290
1291 static void aggr_delete_tid_state(struct aggr_info *p_aggr, u8 tid)
1292 {
1293         struct rxtid *rxtid;
1294         struct rxtid_stats *stats;
1295
1296         if (!p_aggr || tid >= NUM_OF_TIDS)
1297                 return;
1298
1299         rxtid = &p_aggr->rx_tid[tid];
1300         stats = &p_aggr->stat[tid];
1301
1302         if (rxtid->aggr)
1303                 aggr_deque_frms(p_aggr, tid, 0, 0);
1304
1305         rxtid->aggr = false;
1306         rxtid->progress = false;
1307         rxtid->timer_mon = false;
1308         rxtid->win_sz = 0;
1309         rxtid->seq_next = 0;
1310         rxtid->hold_q_sz = 0;
1311
1312         kfree(rxtid->hold_q);
1313         rxtid->hold_q = NULL;
1314
1315         memset(stats, 0, sizeof(struct rxtid_stats));
1316 }
1317
1318 void aggr_recv_addba_req_evt(struct ath6kl *ar, u8 tid, u16 seq_no, u8 win_sz)
1319 {
1320         struct aggr_info *p_aggr = ar->aggr_cntxt;
1321         struct rxtid *rxtid;
1322         struct rxtid_stats *stats;
1323         u16 hold_q_size;
1324
1325         if (!p_aggr)
1326                 return;
1327
1328         rxtid = &p_aggr->rx_tid[tid];
1329         stats = &p_aggr->stat[tid];
1330
1331         if (win_sz < AGGR_WIN_SZ_MIN || win_sz > AGGR_WIN_SZ_MAX)
1332                 ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: win_sz %d, tid %d\n",
1333                            __func__, win_sz, tid);
1334
1335         if (rxtid->aggr)
1336                 aggr_delete_tid_state(p_aggr, tid);
1337
1338         rxtid->seq_next = seq_no;
1339         hold_q_size = TID_WINDOW_SZ(win_sz) * sizeof(struct skb_hold_q);
1340         rxtid->hold_q = kzalloc(hold_q_size, GFP_KERNEL);
1341         if (!rxtid->hold_q)
1342                 return;
1343
1344         rxtid->win_sz = win_sz;
1345         rxtid->hold_q_sz = TID_WINDOW_SZ(win_sz);
1346         if (!skb_queue_empty(&rxtid->q))
1347                 return;
1348
1349         rxtid->aggr = true;
1350 }
1351
1352 struct aggr_info *aggr_init(struct net_device *dev)
1353 {
1354         struct aggr_info *p_aggr = NULL;
1355         struct rxtid *rxtid;
1356         u8 i;
1357
1358         p_aggr = kzalloc(sizeof(struct aggr_info), GFP_KERNEL);
1359         if (!p_aggr) {
1360                 ath6kl_err("failed to alloc memory for aggr_node\n");
1361                 return NULL;
1362         }
1363
1364         p_aggr->aggr_sz = AGGR_SZ_DEFAULT;
1365         p_aggr->dev = dev;
1366         init_timer(&p_aggr->timer);
1367         p_aggr->timer.function = aggr_timeout;
1368         p_aggr->timer.data = (unsigned long) p_aggr;
1369
1370         p_aggr->timer_scheduled = false;
1371         skb_queue_head_init(&p_aggr->free_q);
1372
1373         ath6kl_alloc_netbufs(&p_aggr->free_q, AGGR_NUM_OF_FREE_NETBUFS);
1374
1375         for (i = 0; i < NUM_OF_TIDS; i++) {
1376                 rxtid = &p_aggr->rx_tid[i];
1377                 rxtid->aggr = false;
1378                 rxtid->progress = false;
1379                 rxtid->timer_mon = false;
1380                 skb_queue_head_init(&rxtid->q);
1381                 spin_lock_init(&rxtid->lock);
1382         }
1383
1384         return p_aggr;
1385 }
1386
1387 void aggr_recv_delba_req_evt(struct ath6kl *ar, u8 tid)
1388 {
1389         struct aggr_info *p_aggr = ar->aggr_cntxt;
1390         struct rxtid *rxtid;
1391
1392         if (!p_aggr)
1393                 return;
1394
1395         rxtid = &p_aggr->rx_tid[tid];
1396
1397         if (rxtid->aggr)
1398                 aggr_delete_tid_state(p_aggr, tid);
1399 }
1400
1401 void aggr_reset_state(struct aggr_info *aggr_info)
1402 {
1403         u8 tid;
1404
1405         for (tid = 0; tid < NUM_OF_TIDS; tid++)
1406                 aggr_delete_tid_state(aggr_info, tid);
1407 }
1408
1409 /* clean up our amsdu buffer list */
1410 void ath6kl_cleanup_amsdu_rxbufs(struct ath6kl *ar)
1411 {
1412         struct htc_packet *packet, *tmp_pkt;
1413
1414         spin_lock_bh(&ar->lock);
1415         if (list_empty(&ar->amsdu_rx_buffer_queue)) {
1416                 spin_unlock_bh(&ar->lock);
1417                 return;
1418         }
1419
1420         list_for_each_entry_safe(packet, tmp_pkt, &ar->amsdu_rx_buffer_queue,
1421                                  list) {
1422                 list_del(&packet->list);
1423                 spin_unlock_bh(&ar->lock);
1424                 dev_kfree_skb(packet->pkt_cntxt);
1425                 spin_lock_bh(&ar->lock);
1426         }
1427
1428         spin_unlock_bh(&ar->lock);
1429 }
1430
1431 void aggr_module_destroy(struct aggr_info *aggr_info)
1432 {
1433         struct rxtid *rxtid;
1434         u8 i, k;
1435
1436         if (!aggr_info)
1437                 return;
1438
1439         if (aggr_info->timer_scheduled) {
1440                 del_timer(&aggr_info->timer);
1441                 aggr_info->timer_scheduled = false;
1442         }
1443
1444         for (i = 0; i < NUM_OF_TIDS; i++) {
1445                 rxtid = &aggr_info->rx_tid[i];
1446                 if (rxtid->hold_q) {
1447                         for (k = 0; k < rxtid->hold_q_sz; k++)
1448                                 dev_kfree_skb(rxtid->hold_q[k].skb);
1449                         kfree(rxtid->hold_q);
1450                 }
1451
1452                 skb_queue_purge(&rxtid->q);
1453         }
1454
1455         skb_queue_purge(&aggr_info->free_q);
1456         kfree(aggr_info);
1457 }