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