162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * NXP Wireless LAN device driver: AP TX and RX data handling
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2011-2020 NXP
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "decl.h"
962306a36Sopenharmony_ci#include "ioctl.h"
1062306a36Sopenharmony_ci#include "main.h"
1162306a36Sopenharmony_ci#include "wmm.h"
1262306a36Sopenharmony_ci#include "11n_aggr.h"
1362306a36Sopenharmony_ci#include "11n_rxreorder.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/* This function checks if particular RA list has packets more than low bridge
1662306a36Sopenharmony_ci * packet threshold and then deletes packet from this RA list.
1762306a36Sopenharmony_ci * Function deletes packets from such RA list and returns true. If no such list
1862306a36Sopenharmony_ci * is found, false is returned.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_cistatic bool
2162306a36Sopenharmony_cimwifiex_uap_del_tx_pkts_in_ralist(struct mwifiex_private *priv,
2262306a36Sopenharmony_ci				  struct list_head *ra_list_head,
2362306a36Sopenharmony_ci				  int tid)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	struct mwifiex_ra_list_tbl *ra_list;
2662306a36Sopenharmony_ci	struct sk_buff *skb, *tmp;
2762306a36Sopenharmony_ci	bool pkt_deleted = false;
2862306a36Sopenharmony_ci	struct mwifiex_txinfo *tx_info;
2962306a36Sopenharmony_ci	struct mwifiex_adapter *adapter = priv->adapter;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	list_for_each_entry(ra_list, ra_list_head, list) {
3262306a36Sopenharmony_ci		if (skb_queue_empty(&ra_list->skb_head))
3362306a36Sopenharmony_ci			continue;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci		skb_queue_walk_safe(&ra_list->skb_head, skb, tmp) {
3662306a36Sopenharmony_ci			tx_info = MWIFIEX_SKB_TXCB(skb);
3762306a36Sopenharmony_ci			if (tx_info->flags & MWIFIEX_BUF_FLAG_BRIDGED_PKT) {
3862306a36Sopenharmony_ci				__skb_unlink(skb, &ra_list->skb_head);
3962306a36Sopenharmony_ci				mwifiex_write_data_complete(adapter, skb, 0,
4062306a36Sopenharmony_ci							    -1);
4162306a36Sopenharmony_ci				if (ra_list->tx_paused)
4262306a36Sopenharmony_ci					priv->wmm.pkts_paused[tid]--;
4362306a36Sopenharmony_ci				else
4462306a36Sopenharmony_ci					atomic_dec(&priv->wmm.tx_pkts_queued);
4562306a36Sopenharmony_ci				pkt_deleted = true;
4662306a36Sopenharmony_ci			}
4762306a36Sopenharmony_ci			if ((atomic_read(&adapter->pending_bridged_pkts) <=
4862306a36Sopenharmony_ci					     MWIFIEX_BRIDGED_PKTS_THR_LOW))
4962306a36Sopenharmony_ci				break;
5062306a36Sopenharmony_ci		}
5162306a36Sopenharmony_ci	}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	return pkt_deleted;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* This function deletes packets from particular RA List. RA list index
5762306a36Sopenharmony_ci * from which packets are deleted is preserved so that packets from next RA
5862306a36Sopenharmony_ci * list are deleted upon subsequent call thus maintaining fairness.
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_cistatic void mwifiex_uap_cleanup_tx_queues(struct mwifiex_private *priv)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	struct list_head *ra_list;
6362306a36Sopenharmony_ci	int i;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	spin_lock_bh(&priv->wmm.ra_list_spinlock);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	for (i = 0; i < MAX_NUM_TID; i++, priv->del_list_idx++) {
6862306a36Sopenharmony_ci		if (priv->del_list_idx == MAX_NUM_TID)
6962306a36Sopenharmony_ci			priv->del_list_idx = 0;
7062306a36Sopenharmony_ci		ra_list = &priv->wmm.tid_tbl_ptr[priv->del_list_idx].ra_list;
7162306a36Sopenharmony_ci		if (mwifiex_uap_del_tx_pkts_in_ralist(priv, ra_list, i)) {
7262306a36Sopenharmony_ci			priv->del_list_idx++;
7362306a36Sopenharmony_ci			break;
7462306a36Sopenharmony_ci		}
7562306a36Sopenharmony_ci	}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cistatic void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv,
8262306a36Sopenharmony_ci					 struct sk_buff *skb)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	struct mwifiex_adapter *adapter = priv->adapter;
8562306a36Sopenharmony_ci	struct uap_rxpd *uap_rx_pd;
8662306a36Sopenharmony_ci	struct rx_packet_hdr *rx_pkt_hdr;
8762306a36Sopenharmony_ci	struct sk_buff *new_skb;
8862306a36Sopenharmony_ci	struct mwifiex_txinfo *tx_info;
8962306a36Sopenharmony_ci	int hdr_chop;
9062306a36Sopenharmony_ci	struct ethhdr *p_ethhdr;
9162306a36Sopenharmony_ci	struct mwifiex_sta_node *src_node;
9262306a36Sopenharmony_ci	int index;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	uap_rx_pd = (struct uap_rxpd *)(skb->data);
9562306a36Sopenharmony_ci	rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	if ((atomic_read(&adapter->pending_bridged_pkts) >=
9862306a36Sopenharmony_ci					     MWIFIEX_BRIDGED_PKTS_THR_HIGH)) {
9962306a36Sopenharmony_ci		mwifiex_dbg(priv->adapter, ERROR,
10062306a36Sopenharmony_ci			    "Tx: Bridge packet limit reached. Drop packet!\n");
10162306a36Sopenharmony_ci		kfree_skb(skb);
10262306a36Sopenharmony_ci		mwifiex_uap_cleanup_tx_queues(priv);
10362306a36Sopenharmony_ci		return;
10462306a36Sopenharmony_ci	}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	if (sizeof(*rx_pkt_hdr) +
10762306a36Sopenharmony_ci	    le16_to_cpu(uap_rx_pd->rx_pkt_offset) > skb->len) {
10862306a36Sopenharmony_ci		mwifiex_dbg(adapter, ERROR,
10962306a36Sopenharmony_ci			    "wrong rx packet offset: len=%d,rx_pkt_offset=%d\n",
11062306a36Sopenharmony_ci			    skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset));
11162306a36Sopenharmony_ci		priv->stats.rx_dropped++;
11262306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
11362306a36Sopenharmony_ci		return;
11462306a36Sopenharmony_ci	}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	if ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header,
11762306a36Sopenharmony_ci		     sizeof(bridge_tunnel_header))) ||
11862306a36Sopenharmony_ci	    (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header,
11962306a36Sopenharmony_ci		     sizeof(rfc1042_header)) &&
12062306a36Sopenharmony_ci	     ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_AARP &&
12162306a36Sopenharmony_ci	     ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_IPX)) {
12262306a36Sopenharmony_ci		/* Replace the 803 header and rfc1042 header (llc/snap) with
12362306a36Sopenharmony_ci		 * an Ethernet II header, keep the src/dst and snap_type
12462306a36Sopenharmony_ci		 * (ethertype).
12562306a36Sopenharmony_ci		 *
12662306a36Sopenharmony_ci		 * The firmware only passes up SNAP frames converting all RX
12762306a36Sopenharmony_ci		 * data from 802.11 to 802.2/LLC/SNAP frames.
12862306a36Sopenharmony_ci		 *
12962306a36Sopenharmony_ci		 * To create the Ethernet II, just move the src, dst address
13062306a36Sopenharmony_ci		 * right before the snap_type.
13162306a36Sopenharmony_ci		 */
13262306a36Sopenharmony_ci		p_ethhdr = (struct ethhdr *)
13362306a36Sopenharmony_ci			((u8 *)(&rx_pkt_hdr->eth803_hdr)
13462306a36Sopenharmony_ci			 + sizeof(rx_pkt_hdr->eth803_hdr)
13562306a36Sopenharmony_ci			 + sizeof(rx_pkt_hdr->rfc1042_hdr)
13662306a36Sopenharmony_ci			 - sizeof(rx_pkt_hdr->eth803_hdr.h_dest)
13762306a36Sopenharmony_ci			 - sizeof(rx_pkt_hdr->eth803_hdr.h_source)
13862306a36Sopenharmony_ci			 - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type));
13962306a36Sopenharmony_ci		memcpy(p_ethhdr->h_source, rx_pkt_hdr->eth803_hdr.h_source,
14062306a36Sopenharmony_ci		       sizeof(p_ethhdr->h_source));
14162306a36Sopenharmony_ci		memcpy(p_ethhdr->h_dest, rx_pkt_hdr->eth803_hdr.h_dest,
14262306a36Sopenharmony_ci		       sizeof(p_ethhdr->h_dest));
14362306a36Sopenharmony_ci		/* Chop off the rxpd + the excess memory from
14462306a36Sopenharmony_ci		 * 802.2/llc/snap header that was removed.
14562306a36Sopenharmony_ci		 */
14662306a36Sopenharmony_ci		hdr_chop = (u8 *)p_ethhdr - (u8 *)uap_rx_pd;
14762306a36Sopenharmony_ci	} else {
14862306a36Sopenharmony_ci		/* Chop off the rxpd */
14962306a36Sopenharmony_ci		hdr_chop = (u8 *)&rx_pkt_hdr->eth803_hdr - (u8 *)uap_rx_pd;
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/* Chop off the leading header bytes so that it points
15362306a36Sopenharmony_ci	 * to the start of either the reconstructed EthII frame
15462306a36Sopenharmony_ci	 * or the 802.2/llc/snap frame.
15562306a36Sopenharmony_ci	 */
15662306a36Sopenharmony_ci	skb_pull(skb, hdr_chop);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
15962306a36Sopenharmony_ci		mwifiex_dbg(priv->adapter, ERROR,
16062306a36Sopenharmony_ci			    "data: Tx: insufficient skb headroom %d\n",
16162306a36Sopenharmony_ci			    skb_headroom(skb));
16262306a36Sopenharmony_ci		/* Insufficient skb headroom - allocate a new skb */
16362306a36Sopenharmony_ci		new_skb =
16462306a36Sopenharmony_ci			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
16562306a36Sopenharmony_ci		if (unlikely(!new_skb)) {
16662306a36Sopenharmony_ci			mwifiex_dbg(priv->adapter, ERROR,
16762306a36Sopenharmony_ci				    "Tx: cannot allocate new_skb\n");
16862306a36Sopenharmony_ci			kfree_skb(skb);
16962306a36Sopenharmony_ci			priv->stats.tx_dropped++;
17062306a36Sopenharmony_ci			return;
17162306a36Sopenharmony_ci		}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci		kfree_skb(skb);
17462306a36Sopenharmony_ci		skb = new_skb;
17562306a36Sopenharmony_ci		mwifiex_dbg(priv->adapter, INFO,
17662306a36Sopenharmony_ci			    "info: new skb headroom %d\n",
17762306a36Sopenharmony_ci			    skb_headroom(skb));
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	tx_info = MWIFIEX_SKB_TXCB(skb);
18162306a36Sopenharmony_ci	memset(tx_info, 0, sizeof(*tx_info));
18262306a36Sopenharmony_ci	tx_info->bss_num = priv->bss_num;
18362306a36Sopenharmony_ci	tx_info->bss_type = priv->bss_type;
18462306a36Sopenharmony_ci	tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	src_node = mwifiex_get_sta_entry(priv, rx_pkt_hdr->eth803_hdr.h_source);
18762306a36Sopenharmony_ci	if (src_node) {
18862306a36Sopenharmony_ci		src_node->stats.last_rx = jiffies;
18962306a36Sopenharmony_ci		src_node->stats.rx_bytes += skb->len;
19062306a36Sopenharmony_ci		src_node->stats.rx_packets++;
19162306a36Sopenharmony_ci		src_node->stats.last_tx_rate = uap_rx_pd->rx_rate;
19262306a36Sopenharmony_ci		src_node->stats.last_tx_htinfo = uap_rx_pd->ht_info;
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	if (is_unicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest)) {
19662306a36Sopenharmony_ci		/* Update bridge packet statistics as the
19762306a36Sopenharmony_ci		 * packet is not going to kernel/upper layer.
19862306a36Sopenharmony_ci		 */
19962306a36Sopenharmony_ci		priv->stats.rx_bytes += skb->len;
20062306a36Sopenharmony_ci		priv->stats.rx_packets++;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci		/* Sending bridge packet to TX queue, so save the packet
20362306a36Sopenharmony_ci		 * length in TXCB to update statistics in TX complete.
20462306a36Sopenharmony_ci		 */
20562306a36Sopenharmony_ci		tx_info->pkt_len = skb->len;
20662306a36Sopenharmony_ci	}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	__net_timestamp(skb);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	index = mwifiex_1d_to_wmm_queue[skb->priority];
21162306a36Sopenharmony_ci	atomic_inc(&priv->wmm_tx_pending[index]);
21262306a36Sopenharmony_ci	mwifiex_wmm_add_buf_txqueue(priv, skb);
21362306a36Sopenharmony_ci	atomic_inc(&adapter->tx_pending);
21462306a36Sopenharmony_ci	atomic_inc(&adapter->pending_bridged_pkts);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	mwifiex_queue_main_work(priv->adapter);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	return;
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci/*
22262306a36Sopenharmony_ci * This function contains logic for AP packet forwarding.
22362306a36Sopenharmony_ci *
22462306a36Sopenharmony_ci * If a packet is multicast/broadcast, it is sent to kernel/upper layer
22562306a36Sopenharmony_ci * as well as queued back to AP TX queue so that it can be sent to other
22662306a36Sopenharmony_ci * associated stations.
22762306a36Sopenharmony_ci * If a packet is unicast and RA is present in associated station list,
22862306a36Sopenharmony_ci * it is again requeued into AP TX queue.
22962306a36Sopenharmony_ci * If a packet is unicast and RA is not in associated station list,
23062306a36Sopenharmony_ci * packet is forwarded to kernel to handle routing logic.
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_ciint mwifiex_handle_uap_rx_forward(struct mwifiex_private *priv,
23362306a36Sopenharmony_ci				  struct sk_buff *skb)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	struct mwifiex_adapter *adapter = priv->adapter;
23662306a36Sopenharmony_ci	struct uap_rxpd *uap_rx_pd;
23762306a36Sopenharmony_ci	struct rx_packet_hdr *rx_pkt_hdr;
23862306a36Sopenharmony_ci	u8 ra[ETH_ALEN];
23962306a36Sopenharmony_ci	struct sk_buff *skb_uap;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	uap_rx_pd = (struct uap_rxpd *)(skb->data);
24262306a36Sopenharmony_ci	rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	/* don't do packet forwarding in disconnected state */
24562306a36Sopenharmony_ci	if (!priv->media_connected) {
24662306a36Sopenharmony_ci		mwifiex_dbg(adapter, ERROR,
24762306a36Sopenharmony_ci			    "drop packet in disconnected state.\n");
24862306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
24962306a36Sopenharmony_ci		return 0;
25062306a36Sopenharmony_ci	}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	memcpy(ra, rx_pkt_hdr->eth803_hdr.h_dest, ETH_ALEN);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	if (is_multicast_ether_addr(ra)) {
25562306a36Sopenharmony_ci		skb_uap = skb_copy(skb, GFP_ATOMIC);
25662306a36Sopenharmony_ci		if (likely(skb_uap)) {
25762306a36Sopenharmony_ci			mwifiex_uap_queue_bridged_pkt(priv, skb_uap);
25862306a36Sopenharmony_ci		} else {
25962306a36Sopenharmony_ci			mwifiex_dbg(adapter, ERROR,
26062306a36Sopenharmony_ci				    "failed to copy skb for uAP\n");
26162306a36Sopenharmony_ci			priv->stats.rx_dropped++;
26262306a36Sopenharmony_ci			dev_kfree_skb_any(skb);
26362306a36Sopenharmony_ci			return -1;
26462306a36Sopenharmony_ci		}
26562306a36Sopenharmony_ci	} else {
26662306a36Sopenharmony_ci		if (mwifiex_get_sta_entry(priv, ra)) {
26762306a36Sopenharmony_ci			/* Requeue Intra-BSS packet */
26862306a36Sopenharmony_ci			mwifiex_uap_queue_bridged_pkt(priv, skb);
26962306a36Sopenharmony_ci			return 0;
27062306a36Sopenharmony_ci		}
27162306a36Sopenharmony_ci	}
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* Forward unicat/Inter-BSS packets to kernel. */
27462306a36Sopenharmony_ci	return mwifiex_process_rx_packet(priv, skb);
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ciint mwifiex_uap_recv_packet(struct mwifiex_private *priv,
27862306a36Sopenharmony_ci			    struct sk_buff *skb)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	struct mwifiex_adapter *adapter = priv->adapter;
28162306a36Sopenharmony_ci	struct mwifiex_sta_node *src_node;
28262306a36Sopenharmony_ci	struct ethhdr *p_ethhdr;
28362306a36Sopenharmony_ci	struct sk_buff *skb_uap;
28462306a36Sopenharmony_ci	struct mwifiex_txinfo *tx_info;
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci	if (!skb)
28762306a36Sopenharmony_ci		return -1;
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	p_ethhdr = (void *)skb->data;
29062306a36Sopenharmony_ci	src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
29162306a36Sopenharmony_ci	if (src_node) {
29262306a36Sopenharmony_ci		src_node->stats.last_rx = jiffies;
29362306a36Sopenharmony_ci		src_node->stats.rx_bytes += skb->len;
29462306a36Sopenharmony_ci		src_node->stats.rx_packets++;
29562306a36Sopenharmony_ci	}
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	if (is_multicast_ether_addr(p_ethhdr->h_dest) ||
29862306a36Sopenharmony_ci	    mwifiex_get_sta_entry(priv, p_ethhdr->h_dest)) {
29962306a36Sopenharmony_ci		if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN)
30062306a36Sopenharmony_ci			skb_uap =
30162306a36Sopenharmony_ci			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
30262306a36Sopenharmony_ci		else
30362306a36Sopenharmony_ci			skb_uap = skb_copy(skb, GFP_ATOMIC);
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci		if (likely(skb_uap)) {
30662306a36Sopenharmony_ci			tx_info = MWIFIEX_SKB_TXCB(skb_uap);
30762306a36Sopenharmony_ci			memset(tx_info, 0, sizeof(*tx_info));
30862306a36Sopenharmony_ci			tx_info->bss_num = priv->bss_num;
30962306a36Sopenharmony_ci			tx_info->bss_type = priv->bss_type;
31062306a36Sopenharmony_ci			tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
31162306a36Sopenharmony_ci			__net_timestamp(skb_uap);
31262306a36Sopenharmony_ci			mwifiex_wmm_add_buf_txqueue(priv, skb_uap);
31362306a36Sopenharmony_ci			atomic_inc(&adapter->tx_pending);
31462306a36Sopenharmony_ci			atomic_inc(&adapter->pending_bridged_pkts);
31562306a36Sopenharmony_ci			if ((atomic_read(&adapter->pending_bridged_pkts) >=
31662306a36Sopenharmony_ci					MWIFIEX_BRIDGED_PKTS_THR_HIGH)) {
31762306a36Sopenharmony_ci				mwifiex_dbg(adapter, ERROR,
31862306a36Sopenharmony_ci					    "Tx: Bridge packet limit reached. Drop packet!\n");
31962306a36Sopenharmony_ci				mwifiex_uap_cleanup_tx_queues(priv);
32062306a36Sopenharmony_ci			}
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci		} else {
32362306a36Sopenharmony_ci			mwifiex_dbg(adapter, ERROR, "failed to allocate skb_uap");
32462306a36Sopenharmony_ci		}
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci		mwifiex_queue_main_work(adapter);
32762306a36Sopenharmony_ci		/* Don't forward Intra-BSS unicast packet to upper layer*/
32862306a36Sopenharmony_ci		if (mwifiex_get_sta_entry(priv, p_ethhdr->h_dest))
32962306a36Sopenharmony_ci			return 0;
33062306a36Sopenharmony_ci	}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	skb->dev = priv->netdev;
33362306a36Sopenharmony_ci	skb->protocol = eth_type_trans(skb, priv->netdev);
33462306a36Sopenharmony_ci	skb->ip_summed = CHECKSUM_NONE;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	/* This is required only in case of 11n and USB/PCIE as we alloc
33762306a36Sopenharmony_ci	 * a buffer of 4K only if its 11N (to be able to receive 4K
33862306a36Sopenharmony_ci	 * AMSDU packets). In case of SD we allocate buffers based
33962306a36Sopenharmony_ci	 * on the size of packet and hence this is not needed.
34062306a36Sopenharmony_ci	 *
34162306a36Sopenharmony_ci	 * Modifying the truesize here as our allocation for each
34262306a36Sopenharmony_ci	 * skb is 4K but we only receive 2K packets and this cause
34362306a36Sopenharmony_ci	 * the kernel to start dropping packets in case where
34462306a36Sopenharmony_ci	 * application has allocated buffer based on 2K size i.e.
34562306a36Sopenharmony_ci	 * if there a 64K packet received (in IP fragments and
34662306a36Sopenharmony_ci	 * application allocates 64K to receive this packet but
34762306a36Sopenharmony_ci	 * this packet would almost double up because we allocate
34862306a36Sopenharmony_ci	 * each 1.5K fragment in 4K and pass it up. As soon as the
34962306a36Sopenharmony_ci	 * 64K limit hits kernel will start to drop rest of the
35062306a36Sopenharmony_ci	 * fragments. Currently we fail the Filesndl-ht.scr script
35162306a36Sopenharmony_ci	 * for UDP, hence this fix
35262306a36Sopenharmony_ci	 */
35362306a36Sopenharmony_ci	if ((adapter->iface_type == MWIFIEX_USB ||
35462306a36Sopenharmony_ci	     adapter->iface_type == MWIFIEX_PCIE) &&
35562306a36Sopenharmony_ci	    skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE)
35662306a36Sopenharmony_ci		skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	/* Forward multicast/broadcast packet to upper layer*/
35962306a36Sopenharmony_ci	netif_rx(skb);
36062306a36Sopenharmony_ci	return 0;
36162306a36Sopenharmony_ci}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci/*
36462306a36Sopenharmony_ci * This function processes the packet received on AP interface.
36562306a36Sopenharmony_ci *
36662306a36Sopenharmony_ci * The function looks into the RxPD and performs sanity tests on the
36762306a36Sopenharmony_ci * received buffer to ensure its a valid packet before processing it
36862306a36Sopenharmony_ci * further. If the packet is determined to be aggregated, it is
36962306a36Sopenharmony_ci * de-aggregated accordingly. Then skb is passed to AP packet forwarding logic.
37062306a36Sopenharmony_ci *
37162306a36Sopenharmony_ci * The completion callback is called after processing is complete.
37262306a36Sopenharmony_ci */
37362306a36Sopenharmony_ciint mwifiex_process_uap_rx_packet(struct mwifiex_private *priv,
37462306a36Sopenharmony_ci				  struct sk_buff *skb)
37562306a36Sopenharmony_ci{
37662306a36Sopenharmony_ci	struct mwifiex_adapter *adapter = priv->adapter;
37762306a36Sopenharmony_ci	int ret;
37862306a36Sopenharmony_ci	struct uap_rxpd *uap_rx_pd;
37962306a36Sopenharmony_ci	struct rx_packet_hdr *rx_pkt_hdr;
38062306a36Sopenharmony_ci	u16 rx_pkt_type;
38162306a36Sopenharmony_ci	u8 ta[ETH_ALEN], pkt_type;
38262306a36Sopenharmony_ci	struct mwifiex_sta_node *node;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	uap_rx_pd = (struct uap_rxpd *)(skb->data);
38562306a36Sopenharmony_ci	rx_pkt_type = le16_to_cpu(uap_rx_pd->rx_pkt_type);
38662306a36Sopenharmony_ci	rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	if (le16_to_cpu(uap_rx_pd->rx_pkt_offset) +
38962306a36Sopenharmony_ci	    sizeof(rx_pkt_hdr->eth803_hdr) > skb->len) {
39062306a36Sopenharmony_ci		mwifiex_dbg(adapter, ERROR,
39162306a36Sopenharmony_ci			    "wrong rx packet for struct ethhdr: len=%d, offset=%d\n",
39262306a36Sopenharmony_ci			    skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset));
39362306a36Sopenharmony_ci		priv->stats.rx_dropped++;
39462306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
39562306a36Sopenharmony_ci		return 0;
39662306a36Sopenharmony_ci	}
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ci	ether_addr_copy(ta, rx_pkt_hdr->eth803_hdr.h_source);
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci	if ((le16_to_cpu(uap_rx_pd->rx_pkt_offset) +
40162306a36Sopenharmony_ci	     le16_to_cpu(uap_rx_pd->rx_pkt_length)) > (u16) skb->len) {
40262306a36Sopenharmony_ci		mwifiex_dbg(adapter, ERROR,
40362306a36Sopenharmony_ci			    "wrong rx packet: len=%d, offset=%d, length=%d\n",
40462306a36Sopenharmony_ci			    skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset),
40562306a36Sopenharmony_ci			    le16_to_cpu(uap_rx_pd->rx_pkt_length));
40662306a36Sopenharmony_ci		priv->stats.rx_dropped++;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci		node = mwifiex_get_sta_entry(priv, ta);
40962306a36Sopenharmony_ci		if (node)
41062306a36Sopenharmony_ci			node->stats.tx_failed++;
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
41362306a36Sopenharmony_ci		return 0;
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	if (rx_pkt_type == PKT_TYPE_MGMT) {
41762306a36Sopenharmony_ci		ret = mwifiex_process_mgmt_packet(priv, skb);
41862306a36Sopenharmony_ci		if (ret)
41962306a36Sopenharmony_ci			mwifiex_dbg(adapter, DATA, "Rx of mgmt packet failed");
42062306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
42162306a36Sopenharmony_ci		return ret;
42262306a36Sopenharmony_ci	}
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	if (rx_pkt_type != PKT_TYPE_BAR && uap_rx_pd->priority < MAX_NUM_TID) {
42662306a36Sopenharmony_ci		spin_lock_bh(&priv->sta_list_spinlock);
42762306a36Sopenharmony_ci		node = mwifiex_get_sta_entry(priv, ta);
42862306a36Sopenharmony_ci		if (node)
42962306a36Sopenharmony_ci			node->rx_seq[uap_rx_pd->priority] =
43062306a36Sopenharmony_ci						le16_to_cpu(uap_rx_pd->seq_num);
43162306a36Sopenharmony_ci		spin_unlock_bh(&priv->sta_list_spinlock);
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	if (!priv->ap_11n_enabled ||
43562306a36Sopenharmony_ci	    (!mwifiex_11n_get_rx_reorder_tbl(priv, uap_rx_pd->priority, ta) &&
43662306a36Sopenharmony_ci	    (le16_to_cpu(uap_rx_pd->rx_pkt_type) != PKT_TYPE_AMSDU))) {
43762306a36Sopenharmony_ci		ret = mwifiex_handle_uap_rx_forward(priv, skb);
43862306a36Sopenharmony_ci		return ret;
43962306a36Sopenharmony_ci	}
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	/* Reorder and send to kernel */
44262306a36Sopenharmony_ci	pkt_type = (u8)le16_to_cpu(uap_rx_pd->rx_pkt_type);
44362306a36Sopenharmony_ci	ret = mwifiex_11n_rx_reorder_pkt(priv, le16_to_cpu(uap_rx_pd->seq_num),
44462306a36Sopenharmony_ci					 uap_rx_pd->priority, ta, pkt_type,
44562306a36Sopenharmony_ci					 skb);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	if (ret || (rx_pkt_type == PKT_TYPE_BAR))
44862306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	if (ret)
45162306a36Sopenharmony_ci		priv->stats.rx_dropped++;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	return ret;
45462306a36Sopenharmony_ci}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci/*
45762306a36Sopenharmony_ci * This function fills the TxPD for AP tx packets.
45862306a36Sopenharmony_ci *
45962306a36Sopenharmony_ci * The Tx buffer received by this function should already have the
46062306a36Sopenharmony_ci * header space allocated for TxPD.
46162306a36Sopenharmony_ci *
46262306a36Sopenharmony_ci * This function inserts the TxPD in between interface header and actual
46362306a36Sopenharmony_ci * data and adjusts the buffer pointers accordingly.
46462306a36Sopenharmony_ci *
46562306a36Sopenharmony_ci * The following TxPD fields are set by this function, as required -
46662306a36Sopenharmony_ci *      - BSS number
46762306a36Sopenharmony_ci *      - Tx packet length and offset
46862306a36Sopenharmony_ci *      - Priority
46962306a36Sopenharmony_ci *      - Packet delay
47062306a36Sopenharmony_ci *      - Priority specific Tx control
47162306a36Sopenharmony_ci *      - Flags
47262306a36Sopenharmony_ci */
47362306a36Sopenharmony_civoid mwifiex_process_uap_txpd(struct mwifiex_private *priv,
47462306a36Sopenharmony_ci			      struct sk_buff *skb)
47562306a36Sopenharmony_ci{
47662306a36Sopenharmony_ci	struct mwifiex_adapter *adapter = priv->adapter;
47762306a36Sopenharmony_ci	struct uap_txpd *txpd;
47862306a36Sopenharmony_ci	struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
47962306a36Sopenharmony_ci	int pad;
48062306a36Sopenharmony_ci	u16 pkt_type, pkt_offset;
48162306a36Sopenharmony_ci	int hroom = adapter->intf_hdr_len;
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci	pkt_type = mwifiex_is_skb_mgmt_frame(skb) ? PKT_TYPE_MGMT : 0;
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	pad = ((uintptr_t)skb->data - (sizeof(*txpd) + hroom)) &
48662306a36Sopenharmony_ci	       (MWIFIEX_DMA_ALIGN_SZ - 1);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	skb_push(skb, sizeof(*txpd) + pad);
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	txpd = (struct uap_txpd *)skb->data;
49162306a36Sopenharmony_ci	memset(txpd, 0, sizeof(*txpd));
49262306a36Sopenharmony_ci	txpd->bss_num = priv->bss_num;
49362306a36Sopenharmony_ci	txpd->bss_type = priv->bss_type;
49462306a36Sopenharmony_ci	txpd->tx_pkt_length = cpu_to_le16((u16)(skb->len - (sizeof(*txpd) +
49562306a36Sopenharmony_ci						pad)));
49662306a36Sopenharmony_ci	txpd->priority = (u8)skb->priority;
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	txpd->pkt_delay_2ms = mwifiex_wmm_compute_drv_pkt_delay(priv, skb);
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	if (tx_info->flags & MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS ||
50162306a36Sopenharmony_ci	    tx_info->flags & MWIFIEX_BUF_FLAG_ACTION_TX_STATUS) {
50262306a36Sopenharmony_ci		txpd->tx_token_id = tx_info->ack_frame_id;
50362306a36Sopenharmony_ci		txpd->flags |= MWIFIEX_TXPD_FLAGS_REQ_TX_STATUS;
50462306a36Sopenharmony_ci	}
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	if (txpd->priority < ARRAY_SIZE(priv->wmm.user_pri_pkt_tx_ctrl))
50762306a36Sopenharmony_ci		/*
50862306a36Sopenharmony_ci		 * Set the priority specific tx_control field, setting of 0 will
50962306a36Sopenharmony_ci		 * cause the default value to be used later in this function.
51062306a36Sopenharmony_ci		 */
51162306a36Sopenharmony_ci		txpd->tx_control =
51262306a36Sopenharmony_ci		    cpu_to_le32(priv->wmm.user_pri_pkt_tx_ctrl[txpd->priority]);
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci	/* Offset of actual data */
51562306a36Sopenharmony_ci	pkt_offset = sizeof(*txpd) + pad;
51662306a36Sopenharmony_ci	if (pkt_type == PKT_TYPE_MGMT) {
51762306a36Sopenharmony_ci		/* Set the packet type and add header for management frame */
51862306a36Sopenharmony_ci		txpd->tx_pkt_type = cpu_to_le16(pkt_type);
51962306a36Sopenharmony_ci		pkt_offset += MWIFIEX_MGMT_FRAME_HEADER_SIZE;
52062306a36Sopenharmony_ci	}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	txpd->tx_pkt_offset = cpu_to_le16(pkt_offset);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	/* make space for adapter->intf_hdr_len */
52562306a36Sopenharmony_ci	skb_push(skb, hroom);
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci	if (!txpd->tx_control)
52862306a36Sopenharmony_ci		/* TxCtrl set by user or default */
52962306a36Sopenharmony_ci		txpd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl);
53062306a36Sopenharmony_ci}
531