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