2 * Copyright (c) 2004-2011 Atheros Communications Inc.
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.
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.
20 static u8 ath6kl_ibss_map_epid(struct sk_buff *skb, struct net_device *dev,
23 struct ath6kl *ar = ath6kl_priv(dev);
24 struct ethhdr *eth_hdr;
30 eth_hdr = (struct ethhdr *) (datap + sizeof(struct wmi_data_hdr));
32 if (is_multicast_ether_addr(eth_hdr->h_dest))
35 for (i = 0; i < ar->node_num; i++) {
36 if (memcmp(eth_hdr->h_dest, ar->node_map[i].mac_addr,
39 ar->node_map[i].tx_pend++;
40 return ar->node_map[i].ep_id;
43 if ((ep_map == -1) && !ar->node_map[i].tx_pend)
48 ep_map = ar->node_num;
50 if (ar->node_num > MAX_NODE_NUM)
51 return ENDPOINT_UNUSED;
54 memcpy(ar->node_map[ep_map].mac_addr, eth_hdr->h_dest, ETH_ALEN);
56 for (i = ENDPOINT_2; i <= ENDPOINT_5; i++) {
57 if (!ar->tx_pending[i]) {
58 ar->node_map[ep_map].ep_id = i;
63 * No free endpoint is available, start redistribution on
64 * the inuse endpoints.
66 if (i == ENDPOINT_5) {
67 ar->node_map[ep_map].ep_id = ar->next_ep_id;
69 if (ar->next_ep_id > ENDPOINT_5)
70 ar->next_ep_id = ENDPOINT_2;
75 ar->node_map[ep_map].tx_pend++;
77 return ar->node_map[ep_map].ep_id;
80 static bool ath6kl_powersave_ap(struct ath6kl *ar, struct sk_buff *skb,
83 struct ethhdr *datap = (struct ethhdr *) skb->data;
84 struct ath6kl_sta *conn = NULL;
85 bool ps_queued = false, is_psq_empty = false;
87 if (is_multicast_ether_addr(datap->h_dest)) {
91 for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
92 if (ar->sta_list[ctr].sta_flags & STA_PS_SLEEP) {
100 * If this transmit is not because of a Dtim Expiry
103 if (!test_bit(DTIM_EXPIRED, &ar->flag)) {
104 bool is_mcastq_empty = false;
106 spin_lock_bh(&ar->mcastpsq_lock);
108 skb_queue_empty(&ar->mcastpsq);
109 skb_queue_tail(&ar->mcastpsq, skb);
110 spin_unlock_bh(&ar->mcastpsq_lock);
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.
118 ath6kl_wmi_set_pvb_cmd(ar->wmi,
124 * This transmit is because of Dtim expiry.
125 * Determine if MoreData bit has to be set.
127 spin_lock_bh(&ar->mcastpsq_lock);
128 if (!skb_queue_empty(&ar->mcastpsq))
130 spin_unlock_bh(&ar->mcastpsq_lock);
134 conn = ath6kl_find_sta(ar, datap->h_dest);
138 /* Inform the caller that the skb is consumed */
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);
151 * If this is the first pkt getting queued
152 * for this STA, update the PVB for this
156 ath6kl_wmi_set_pvb_cmd(ar->wmi,
162 * This tx is because of a PsPoll.
163 * Determine if MoreData bit has to be set.
165 spin_lock_bh(&conn->psq_lock);
166 if (!skb_queue_empty(&conn->psq))
168 spin_unlock_bh(&conn->psq_lock);
178 int ath6kl_control_tx(void *devt, struct sk_buff *skb,
179 enum htc_endpoint_id eid)
181 struct ath6kl *ar = devt;
183 struct ath6kl_cookie *cookie = NULL;
185 spin_lock_bh(&ar->lock);
187 ath6kl_dbg(ATH6KL_DBG_WLAN_TX,
188 "%s: skb=0x%p, len=0x%x eid =%d\n", __func__,
191 if (test_bit(WMI_CTRL_EP_FULL, &ar->flag) && (eid == ar->ctrl_ep)) {
193 * Control endpoint is full, don't allocate resources, we
194 * are just going to drop this packet.
197 ath6kl_err("wmi ctrl ep full, dropping pkt : 0x%p, len:%d\n",
200 cookie = ath6kl_alloc_cookie(ar);
202 if (cookie == NULL) {
203 spin_unlock_bh(&ar->lock);
208 ar->tx_pending[eid]++;
210 if (eid != ar->ctrl_ep)
211 ar->total_tx_data_pend++;
213 spin_unlock_bh(&ar->lock);
217 set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
218 eid, ATH6KL_CONTROL_PKT_TAG);
221 * This interface is asynchronous, if there is an error, cleanup
222 * will happen in the TX completion callback.
224 ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
233 int ath6kl_data_tx(struct sk_buff *skb, struct net_device *dev)
235 struct ath6kl *ar = ath6kl_priv(dev);
236 struct ath6kl_cookie *cookie = NULL;
237 enum htc_endpoint_id eid = ENDPOINT_UNUSED;
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;
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);
249 /* If target is not associated */
250 if (!test_bit(CONNECTED, &ar->flag)) {
255 if (!test_bit(WMI_READY, &ar->flag))
258 /* AP mode Power saving processing */
259 if (ar->nw_type == AP_NETWORK) {
260 if (ath6kl_powersave_ap(ar, skb, &more_data))
264 if (test_bit(WMI_ENABLED, &ar->flag)) {
265 memset(&meta_v2, 0, sizeof(meta_v2));
267 if (skb_headroom(skb) < dev->needed_headroom) {
272 if (ath6kl_wmi_dix_2_dot3(ar->wmi, skb)) {
273 ath6kl_err("ath6kl_wmi_dix_2_dot3 failed\n");
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");
283 if ((ar->nw_type == ADHOC_NETWORK) &&
284 ar->ibss_ps_enable && test_bit(CONNECTED, &ar->flag))
285 chk_adhoc_ps_mapping = true;
287 /* get the stream mapping */
288 ret = ath6kl_wmi_implicit_create_pstream(ar->wmi, skb,
289 0, test_bit(WMM_ENABLED, &ar->flag), &ac);
296 spin_lock_bh(&ar->lock);
298 if (chk_adhoc_ps_mapping)
299 eid = ath6kl_ibss_map_epid(skb, dev, &map_no);
301 eid = ar->ac2ep_map[ac];
303 if (eid == 0 || eid == ENDPOINT_UNUSED) {
304 ath6kl_err("eid %d is not mapped!\n", eid);
305 spin_unlock_bh(&ar->lock);
309 /* allocate resource for this packet */
310 cookie = ath6kl_alloc_cookie(ar);
313 spin_unlock_bh(&ar->lock);
317 /* update counts while the lock is held */
318 ar->tx_pending[eid]++;
319 ar->total_tx_data_pend++;
321 spin_unlock_bh(&ar->lock);
324 cookie->map_no = map_no;
325 set_htc_pkt_info(&cookie->htc_pkt, cookie, skb->data, skb->len,
328 ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, skb->data, skb->len);
331 * HTC interface is asynchronous, if this fails, cleanup will
332 * happen in the ath6kl_tx_complete callback.
334 ath6kl_htc_tx(ar->htc_target, &cookie->htc_pkt);
341 ar->net_stats.tx_dropped++;
342 ar->net_stats.tx_aborted_errors++;
347 /* indicate tx activity or inactivity on a WMI stream */
348 void ath6kl_indicate_tx_activity(void *devt, u8 traffic_class, bool active)
350 struct ath6kl *ar = devt;
351 enum htc_endpoint_id eid;
354 eid = ar->ac2ep_map[traffic_class];
356 if (!test_bit(WMI_ENABLED, &ar->flag))
359 spin_lock_bh(&ar->lock);
361 ar->ac_stream_active[traffic_class] = active;
365 * Keep track of the active stream with the highest
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];
376 * We may have to search for the next active stream
377 * that is the highest priority.
379 if (ar->hiac_stream_active_pri ==
380 ar->ac_stream_pri_map[traffic_class]) {
382 * The highest priority stream just went inactive
383 * reset and search for the "next" highest "active"
386 ar->hiac_stream_active_pri = 0;
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))
393 * Set the new highest active
396 ar->hiac_stream_active_pri =
397 ar->ac_stream_pri_map[i];
402 spin_unlock_bh(&ar->lock);
405 /* notify HTC, this may cause credit distribution changes */
406 ath6kl_htc_indicate_activity_change(ar->htc_target, eid, active);
409 enum htc_send_full_action ath6kl_tx_queue_full(struct htc_target *target,
410 struct htc_packet *packet)
412 struct ath6kl *ar = target->dev->ar;
413 enum htc_endpoint_id endpoint = packet->endpoint;
415 if (endpoint == ar->ctrl_ep) {
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.
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;
429 if (packet->info.tx.tag == ATH6KL_CONTROL_PKT_TAG)
430 return HTC_SEND_FULL_KEEP;
432 if (ar->nw_type == ADHOC_NETWORK)
434 * In adhoc mode, we cannot differentiate traffic
435 * priorities so there is no need to continue, however we
436 * should stop the network.
438 goto stop_net_queues;
441 * The last MAX_HI_COOKIE_NUM "batch" of cookies are reserved for
442 * the highest active stream.
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)
448 * Give preference to the highest priority stream by
449 * dropping the packets which overflowed.
451 return HTC_SEND_FULL_DROP;
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);
459 return HTC_SEND_FULL_KEEP;
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)
468 if (ar->nw_type != ADHOC_NETWORK)
471 if (!ar->ibss_ps_enable)
474 if (eid == ar->ctrl_ep)
481 ar->node_map[map_no].tx_pend--;
483 if (ar->node_map[map_no].tx_pend)
486 if (map_no != (ar->node_num - 1))
489 for (i = ar->node_num; i > 0; i--) {
490 if (ar->node_map[i - 1].tx_pend)
493 memset(&ar->node_map[i - 1], 0,
494 sizeof(struct ath6kl_node_mapping));
499 void ath6kl_tx_complete(void *context, struct list_head *packet_queue)
501 struct ath6kl *ar = context;
502 struct sk_buff_head skb_queue;
503 struct htc_packet *packet;
505 struct ath6kl_cookie *ath6kl_cookie;
508 enum htc_endpoint_id eid;
509 bool wake_event = false;
510 bool flushing = false;
512 skb_queue_head_init(&skb_queue);
514 /* lock the driver as we update internal state */
515 spin_lock_bh(&ar->lock);
517 /* reap completed packets */
518 while (!list_empty(packet_queue)) {
520 packet = list_first_entry(packet_queue, struct htc_packet,
522 list_del(&packet->list);
524 ath6kl_cookie = (struct ath6kl_cookie *)packet->pkt_cntxt;
528 status = packet->status;
529 skb = ath6kl_cookie->skb;
530 eid = packet->endpoint;
531 map_no = ath6kl_cookie->map_no;
533 if (!skb || !skb->data)
536 packet->buf = skb->data;
538 __skb_queue_tail(&skb_queue, skb);
540 if (!status && (packet->act_len != skb->len))
543 ar->tx_pending[eid]--;
545 if (eid != ar->ctrl_ep)
546 ar->total_tx_data_pend--;
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);
552 if (ar->tx_pending[eid] == 0)
557 if (status == -ECANCELED)
558 /* a packet was flushed */
561 ar->net_stats.tx_errors++;
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,
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,
576 ar->net_stats.tx_packets++;
577 ar->net_stats.tx_bytes += skb->len;
580 ath6kl_tx_clear_node_map(ar, eid, map_no);
582 ath6kl_free_cookie(ar, ath6kl_cookie);
584 if (test_bit(NETQ_STOPPED, &ar->flag))
585 clear_bit(NETQ_STOPPED, &ar->flag);
588 spin_unlock_bh(&ar->lock);
590 __skb_queue_purge(&skb_queue);
592 if (test_bit(CONNECTED, &ar->flag)) {
594 netif_wake_queue(ar->net_dev);
598 wake_up(&ar->event_wq);
604 spin_unlock_bh(&ar->lock);
608 void ath6kl_tx_data_cleanup(struct ath6kl *ar)
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);
620 static void ath6kl_deliver_frames_to_nw_stack(struct net_device *dev,
628 if (!(skb->dev->flags & IFF_UP)) {
633 skb->protocol = eth_type_trans(skb, skb->dev);
638 static void ath6kl_alloc_netbufs(struct sk_buff_head *q, u16 num)
643 skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
645 ath6kl_err("netbuf allocation failed\n");
648 skb_queue_tail(q, skb);
653 static struct sk_buff *aggr_get_free_skb(struct aggr_info *p_aggr)
655 struct sk_buff *skb = NULL;
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);
660 skb = skb_dequeue(&p_aggr->free_q);
665 void ath6kl_rx_refill(struct htc_target *target, enum htc_endpoint_id endpoint)
667 struct ath6kl *ar = target->dev->ar;
671 struct htc_packet *packet;
672 struct list_head queue;
674 n_buf_refill = ATH6KL_MAX_RX_BUFFERS -
675 ath6kl_htc_get_rxbuf_num(ar->htc_target, endpoint);
677 if (n_buf_refill <= 0)
680 INIT_LIST_HEAD(&queue);
682 ath6kl_dbg(ATH6KL_DBG_WLAN_RX,
683 "%s: providing htc with %d buffers at eid=%d\n",
684 __func__, n_buf_refill, endpoint);
686 for (rx_buf = 0; rx_buf < n_buf_refill; rx_buf++) {
687 skb = ath6kl_buf_alloc(ATH6KL_BUFFER_SIZE);
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);
697 if (!list_empty(&queue))
698 ath6kl_htc_add_rxbuf_multiple(ar->htc_target, &queue);
701 void ath6kl_refill_amsdu_rxbufs(struct ath6kl *ar, int count)
703 struct htc_packet *packet;
707 skb = ath6kl_buf_alloc(ATH6KL_AMSDU_BUFFER_SIZE);
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);
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).
725 struct htc_packet *ath6kl_alloc_amsdu_rxbuf(struct htc_target *target,
726 enum htc_endpoint_id endpoint,
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;
734 ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: eid=%d, len:%d\n",
735 __func__, endpoint, len);
737 if ((len <= ATH6KL_BUFFER_SIZE) ||
738 (len > ATH6KL_AMSDU_BUFFER_SIZE))
741 spin_lock_bh(&ar->lock);
743 if (list_empty(&ar->amsdu_rx_buffer_queue)) {
744 spin_unlock_bh(&ar->lock);
745 refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS;
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)
755 refill_cnt = ATH6KL_MAX_AMSDU_RX_BUFFERS - depth;
756 spin_unlock_bh(&ar->lock);
758 /* set actual endpoint ID */
759 packet->endpoint = endpoint;
762 if (refill_cnt >= ATH6KL_AMSDU_REFILL_THRESHOLD)
763 ath6kl_refill_amsdu_rxbufs(ar, refill_cnt);
768 static void aggr_slice_amsdu(struct aggr_info *p_aggr,
769 struct rxtid *rxtid, struct sk_buff *skb)
771 struct sk_buff *new_skb;
773 u16 frame_8023_len, payload_8023_len, mac_hdr_len, amsdu_len;
776 mac_hdr_len = sizeof(struct ethhdr);
777 framep = skb->data + mac_hdr_len;
778 amsdu_len = skb->len - mac_hdr_len;
780 while (amsdu_len > mac_hdr_len) {
781 hdr = (struct ethhdr *) framep;
782 payload_8023_len = ntohs(hdr->h_proto);
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",
791 frame_8023_len = payload_8023_len + mac_hdr_len;
792 new_skb = aggr_get_free_skb(p_aggr);
794 ath6kl_err("no buffer available\n");
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);
806 skb_queue_tail(&rxtid->q, new_skb);
808 /* Is this the last subframe within this aggregate ? */
809 if ((amsdu_len - frame_8023_len) == 0)
812 /* Add the length of A-MSDU subframe padding bytes -
813 * Round to nearest word.
815 frame_8023_len = ALIGN(frame_8023_len + 3, 3);
817 framep += frame_8023_len;
818 amsdu_len -= frame_8023_len;
824 static void aggr_deque_frms(struct aggr_info *p_aggr, u8 tid,
825 u16 seq_no, u8 order)
829 struct skb_hold_q *node;
830 u16 idx, idx_end, seq_end;
831 struct rxtid_stats *stats;
836 rxtid = &p_aggr->rx_tid[tid];
837 stats = &p_aggr->stat[tid];
839 idx = AGGR_WIN_IDX(rxtid->seq_next, rxtid->hold_q_sz);
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.
854 seq_end = seq_no ? seq_no : rxtid->seq_next;
855 idx_end = AGGR_WIN_IDX(seq_end, rxtid->hold_q_sz);
857 spin_lock_bh(&rxtid->lock);
860 node = &rxtid->hold_q[idx];
861 if ((order == 1) && (!node->skb))
866 aggr_slice_amsdu(p_aggr, rxtid, node->skb);
868 skb_queue_tail(&rxtid->q, node->skb);
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);
877 spin_unlock_bh(&rxtid->lock);
879 stats->num_delivered += skb_queue_len(&rxtid->q);
881 while ((skb = skb_dequeue(&rxtid->q)))
882 ath6kl_deliver_frames_to_nw_stack(p_aggr->dev, skb);
885 static bool aggr_process_recv_frm(struct aggr_info *agg_info, u8 tid,
887 bool is_amsdu, struct sk_buff *frame)
890 struct rxtid_stats *stats;
892 struct skb_hold_q *node;
893 u16 idx, st, cur, end;
894 bool is_queued = false;
897 rxtid = &agg_info->rx_tid[tid];
898 stats = &agg_info->stat[tid];
900 stats->num_into_aggr++;
904 aggr_slice_amsdu(agg_info, rxtid, frame);
907 while ((skb = skb_dequeue(&rxtid->q)))
908 ath6kl_deliver_frames_to_nw_stack(agg_info->dev,
914 /* Check the incoming sequence no, if it's in the window */
915 st = rxtid->seq_next;
917 end = (st + rxtid->hold_q_sz-1) & ATH6KL_MAX_SEQ_NO;
919 if (((st < end) && (cur < st || cur > end)) ||
920 ((st > end) && (cur > end) && (cur < st))) {
921 extended_end = (end + rxtid->hold_q_sz - 1) &
924 if (((end < extended_end) &&
925 (cur < end || cur > extended_end)) ||
926 ((end > extended_end) && (cur > extended_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);
932 rxtid->seq_next = ATH6KL_MAX_SEQ_NO -
933 (rxtid->hold_q_sz - 2 - cur);
936 * Dequeue only those frames that are outside the
937 * new shifted window.
939 if (cur >= rxtid->hold_q_sz - 1)
940 st = cur - (rxtid->hold_q_sz - 1);
942 st = ATH6KL_MAX_SEQ_NO -
943 (rxtid->hold_q_sz - 2 - cur);
945 aggr_deque_frms(agg_info, tid, st, 0);
951 idx = AGGR_WIN_IDX(seq_no, rxtid->hold_q_sz);
953 node = &rxtid->hold_q[idx];
955 spin_lock_bh(&rxtid->lock);
958 * Is the cur frame duplicate or something beyond our window(hold_q
959 * -> which is 2x, already)?
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.
969 dev_kfree_skb(node->skb);
974 node->is_amsdu = is_amsdu;
975 node->seq_no = seq_no;
982 spin_unlock_bh(&rxtid->lock);
984 aggr_deque_frms(agg_info, tid, 0, 1);
986 if (agg_info->timer_scheduled)
987 rxtid->progress = true;
989 for (idx = 0 ; idx < rxtid->hold_q_sz; idx++) {
990 if (rxtid->hold_q[idx].skb) {
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
997 agg_info->timer_scheduled = true;
998 mod_timer(&agg_info->timer,
1000 HZ * (AGGR_RX_TIMEOUT) / 1000));
1001 rxtid->progress = false;
1002 rxtid->timer_mon = true;
1010 void ath6kl_rx(struct htc_target *target, struct htc_packet *packet)
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;
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;
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);
1032 if (status || !(skb->data + HTC_HDR_LENGTH)) {
1033 ar->net_stats.rx_errors++;
1039 * Take lock to protect buffer counts and adaptive power throughput
1042 spin_lock_bh(&ar->lock);
1044 ar->net_stats.rx_packets++;
1045 ar->net_stats.rx_bytes += packet->act_len;
1047 spin_unlock_bh(&ar->lock);
1049 skb_put(skb, packet->act_len + HTC_HDR_LENGTH);
1050 skb_pull(skb, HTC_HDR_LENGTH);
1052 ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, __func__, skb->data, skb->len);
1054 skb->dev = ar->net_dev;
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);
1063 if (ept == ar->ctrl_ep) {
1064 ath6kl_wmi_control_rx(ar->wmi, skb);
1068 min_hdr_len = sizeof(struct ethhdr);
1069 min_hdr_len += sizeof(struct wmi_data_hdr) +
1070 sizeof(struct ath6kl_llc_snap_hdr);
1072 dhdr = (struct wmi_data_hdr *) skb->data;
1075 * In the case of AP mode we may receive NULL data frames
1076 * that do not have LLC hdr. They are 16 bytes in size.
1077 * Allow these frames in the AP mode.
1079 if (ar->nw_type != AP_NETWORK &&
1080 ((packet->act_len < min_hdr_len) ||
1081 (packet->act_len > WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH))) {
1082 ath6kl_info("frame len is too short or too long\n");
1083 ar->net_stats.rx_errors++;
1084 ar->net_stats.rx_length_errors++;
1089 /* Get the Power save state of the STA */
1090 if (ar->nw_type == AP_NETWORK) {
1091 meta_type = wmi_data_hdr_get_meta(dhdr);
1093 ps_state = !!((dhdr->info >> WMI_DATA_HDR_PS_SHIFT) &
1094 WMI_DATA_HDR_PS_MASK);
1096 offset = sizeof(struct wmi_data_hdr);
1098 switch (meta_type) {
1101 case WMI_META_VERSION_1:
1102 offset += sizeof(struct wmi_rx_meta_v1);
1104 case WMI_META_VERSION_2:
1105 offset += sizeof(struct wmi_rx_meta_v2);
1111 datap = (struct ethhdr *) (skb->data + offset);
1112 conn = ath6kl_find_sta(ar, datap->h_source);
1120 * If there is a change in PS state of the STA,
1121 * take appropriate steps:
1123 * 1. If Sleep-->Awake, flush the psq for the STA
1124 * Clear the PVB for the STA.
1125 * 2. If Awake-->Sleep, Starting queueing frames
1128 prev_ps = !!(conn->sta_flags & STA_PS_SLEEP);
1131 conn->sta_flags |= STA_PS_SLEEP;
1133 conn->sta_flags &= ~STA_PS_SLEEP;
1135 if (prev_ps ^ !!(conn->sta_flags & STA_PS_SLEEP)) {
1136 if (!(conn->sta_flags & STA_PS_SLEEP)) {
1137 struct sk_buff *skbuff = NULL;
1139 spin_lock_bh(&conn->psq_lock);
1140 while ((skbuff = skb_dequeue(&conn->psq))
1142 spin_unlock_bh(&conn->psq_lock);
1143 ath6kl_data_tx(skbuff, ar->net_dev);
1144 spin_lock_bh(&conn->psq_lock);
1146 spin_unlock_bh(&conn->psq_lock);
1147 /* Clear the PVB for this STA */
1148 ath6kl_wmi_set_pvb_cmd(ar->wmi, conn->aid, 0);
1152 /* drop NULL data frames here */
1153 if ((packet->act_len < min_hdr_len) ||
1155 WMI_MAX_AMSDU_RX_DATA_FRAME_LENGTH)) {
1161 is_amsdu = wmi_data_hdr_is_amsdu(dhdr) ? true : false;
1162 tid = wmi_data_hdr_get_up(dhdr);
1163 seq_no = wmi_data_hdr_get_seqno(dhdr);
1164 meta_type = wmi_data_hdr_get_meta(dhdr);
1165 dot11_hdr = wmi_data_hdr_get_dot11(dhdr);
1167 ath6kl_wmi_data_hdr_remove(ar->wmi, skb);
1169 switch (meta_type) {
1170 case WMI_META_VERSION_1:
1171 skb_pull(skb, sizeof(struct wmi_rx_meta_v1));
1173 case WMI_META_VERSION_2:
1174 meta = (struct wmi_rx_meta_v2 *) skb->data;
1175 if (meta->csum_flags & 0x1) {
1176 skb->ip_summed = CHECKSUM_COMPLETE;
1177 skb->csum = (__force __wsum) meta->csum;
1179 skb_pull(skb, sizeof(struct wmi_rx_meta_v2));
1186 status = ath6kl_wmi_dot11_hdr_remove(ar->wmi, skb);
1188 status = ath6kl_wmi_dot3_2_dix(skb);
1192 * Drop frames that could not be processed (lack of
1199 if (!(ar->net_dev->flags & IFF_UP)) {
1204 if (ar->nw_type == AP_NETWORK) {
1205 datap = (struct ethhdr *) skb->data;
1206 if (is_multicast_ether_addr(datap->h_dest))
1208 * Bcast/Mcast frames should be sent to the
1209 * OS stack as well as on the air.
1211 skb1 = skb_copy(skb, GFP_ATOMIC);
1214 * Search for a connected STA with dstMac
1215 * as the Mac address. If found send the
1216 * frame to it on the air else send the
1217 * frame up the stack.
1219 struct ath6kl_sta *conn = NULL;
1220 conn = ath6kl_find_sta(ar, datap->h_dest);
1222 if (conn && ar->intra_bss) {
1225 } else if (conn && !ar->intra_bss) {
1231 ath6kl_data_tx(skb1, ar->net_dev);
1234 if (!aggr_process_recv_frm(ar->aggr_cntxt, tid, seq_no,
1236 ath6kl_deliver_frames_to_nw_stack(ar->net_dev, skb);
1239 static void aggr_timeout(unsigned long arg)
1242 struct aggr_info *p_aggr = (struct aggr_info *) arg;
1243 struct rxtid *rxtid;
1244 struct rxtid_stats *stats;
1246 for (i = 0; i < NUM_OF_TIDS; i++) {
1247 rxtid = &p_aggr->rx_tid[i];
1248 stats = &p_aggr->stat[i];
1250 if (!rxtid->aggr || !rxtid->timer_mon || rxtid->progress)
1254 * FIXME: these timeouts happen quite fruently, something
1255 * line once within 60 seconds. Investigate why.
1257 stats->num_timeouts++;
1258 ath6kl_dbg(ATH6KL_DBG_AGGR,
1259 "aggr timeout (st %d end %d)\n",
1261 ((rxtid->seq_next + rxtid->hold_q_sz-1) &
1262 ATH6KL_MAX_SEQ_NO));
1263 aggr_deque_frms(p_aggr, i, 0, 0);
1266 p_aggr->timer_scheduled = false;
1268 for (i = 0; i < NUM_OF_TIDS; i++) {
1269 rxtid = &p_aggr->rx_tid[i];
1271 if (rxtid->aggr && rxtid->hold_q) {
1272 for (j = 0; j < rxtid->hold_q_sz; j++) {
1273 if (rxtid->hold_q[j].skb) {
1274 p_aggr->timer_scheduled = true;
1275 rxtid->timer_mon = true;
1276 rxtid->progress = false;
1281 if (j >= rxtid->hold_q_sz)
1282 rxtid->timer_mon = false;
1286 if (p_aggr->timer_scheduled)
1287 mod_timer(&p_aggr->timer,
1288 jiffies + msecs_to_jiffies(AGGR_RX_TIMEOUT));
1291 static void aggr_delete_tid_state(struct aggr_info *p_aggr, u8 tid)
1293 struct rxtid *rxtid;
1294 struct rxtid_stats *stats;
1296 if (!p_aggr || tid >= NUM_OF_TIDS)
1299 rxtid = &p_aggr->rx_tid[tid];
1300 stats = &p_aggr->stat[tid];
1303 aggr_deque_frms(p_aggr, tid, 0, 0);
1305 rxtid->aggr = false;
1306 rxtid->progress = false;
1307 rxtid->timer_mon = false;
1309 rxtid->seq_next = 0;
1310 rxtid->hold_q_sz = 0;
1312 kfree(rxtid->hold_q);
1313 rxtid->hold_q = NULL;
1315 memset(stats, 0, sizeof(struct rxtid_stats));
1318 void aggr_recv_addba_req_evt(struct ath6kl *ar, u8 tid, u16 seq_no, u8 win_sz)
1320 struct aggr_info *p_aggr = ar->aggr_cntxt;
1321 struct rxtid *rxtid;
1322 struct rxtid_stats *stats;
1328 rxtid = &p_aggr->rx_tid[tid];
1329 stats = &p_aggr->stat[tid];
1331 if (win_sz < AGGR_WIN_SZ_MIN || win_sz > AGGR_WIN_SZ_MAX)
1332 ath6kl_dbg(ATH6KL_DBG_WLAN_RX, "%s: win_sz %d, tid %d\n",
1333 __func__, win_sz, tid);
1336 aggr_delete_tid_state(p_aggr, tid);
1338 rxtid->seq_next = seq_no;
1339 hold_q_size = TID_WINDOW_SZ(win_sz) * sizeof(struct skb_hold_q);
1340 rxtid->hold_q = kzalloc(hold_q_size, GFP_KERNEL);
1344 rxtid->win_sz = win_sz;
1345 rxtid->hold_q_sz = TID_WINDOW_SZ(win_sz);
1346 if (!skb_queue_empty(&rxtid->q))
1352 struct aggr_info *aggr_init(struct net_device *dev)
1354 struct aggr_info *p_aggr = NULL;
1355 struct rxtid *rxtid;
1358 p_aggr = kzalloc(sizeof(struct aggr_info), GFP_KERNEL);
1360 ath6kl_err("failed to alloc memory for aggr_node\n");
1364 p_aggr->aggr_sz = AGGR_SZ_DEFAULT;
1366 init_timer(&p_aggr->timer);
1367 p_aggr->timer.function = aggr_timeout;
1368 p_aggr->timer.data = (unsigned long) p_aggr;
1370 p_aggr->timer_scheduled = false;
1371 skb_queue_head_init(&p_aggr->free_q);
1373 ath6kl_alloc_netbufs(&p_aggr->free_q, AGGR_NUM_OF_FREE_NETBUFS);
1375 for (i = 0; i < NUM_OF_TIDS; i++) {
1376 rxtid = &p_aggr->rx_tid[i];
1377 rxtid->aggr = false;
1378 rxtid->progress = false;
1379 rxtid->timer_mon = false;
1380 skb_queue_head_init(&rxtid->q);
1381 spin_lock_init(&rxtid->lock);
1387 void aggr_recv_delba_req_evt(struct ath6kl *ar, u8 tid)
1389 struct aggr_info *p_aggr = ar->aggr_cntxt;
1390 struct rxtid *rxtid;
1395 rxtid = &p_aggr->rx_tid[tid];
1398 aggr_delete_tid_state(p_aggr, tid);
1401 void aggr_reset_state(struct aggr_info *aggr_info)
1405 for (tid = 0; tid < NUM_OF_TIDS; tid++)
1406 aggr_delete_tid_state(aggr_info, tid);
1409 /* clean up our amsdu buffer list */
1410 void ath6kl_cleanup_amsdu_rxbufs(struct ath6kl *ar)
1412 struct htc_packet *packet, *tmp_pkt;
1414 spin_lock_bh(&ar->lock);
1415 if (list_empty(&ar->amsdu_rx_buffer_queue)) {
1416 spin_unlock_bh(&ar->lock);
1420 list_for_each_entry_safe(packet, tmp_pkt, &ar->amsdu_rx_buffer_queue,
1422 list_del(&packet->list);
1423 spin_unlock_bh(&ar->lock);
1424 dev_kfree_skb(packet->pkt_cntxt);
1425 spin_lock_bh(&ar->lock);
1428 spin_unlock_bh(&ar->lock);
1431 void aggr_module_destroy(struct aggr_info *aggr_info)
1433 struct rxtid *rxtid;
1439 if (aggr_info->timer_scheduled) {
1440 del_timer(&aggr_info->timer);
1441 aggr_info->timer_scheduled = false;
1444 for (i = 0; i < NUM_OF_TIDS; i++) {
1445 rxtid = &aggr_info->rx_tid[i];
1446 if (rxtid->hold_q) {
1447 for (k = 0; k < rxtid->hold_q_sz; k++)
1448 dev_kfree_skb(rxtid->hold_q[k].skb);
1449 kfree(rxtid->hold_q);
1452 skb_queue_purge(&rxtid->q);
1455 skb_queue_purge(&aggr_info->free_q);