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