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