xref: /openbmc/linux/drivers/net/wireless/marvell/mwifiex/uap_txrx.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1828c91f7SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2277b024eSKalle Valo /*
3932183aaSGanapathi Bhat  * NXP Wireless LAN device driver: AP TX and RX data handling
4277b024eSKalle Valo  *
5932183aaSGanapathi Bhat  * Copyright 2011-2020 NXP
6277b024eSKalle Valo  */
7277b024eSKalle Valo 
8277b024eSKalle Valo #include "decl.h"
9277b024eSKalle Valo #include "ioctl.h"
10277b024eSKalle Valo #include "main.h"
11277b024eSKalle Valo #include "wmm.h"
12277b024eSKalle Valo #include "11n_aggr.h"
13277b024eSKalle Valo #include "11n_rxreorder.h"
14277b024eSKalle Valo 
15277b024eSKalle Valo /* This function checks if particular RA list has packets more than low bridge
16277b024eSKalle Valo  * packet threshold and then deletes packet from this RA list.
17277b024eSKalle Valo  * Function deletes packets from such RA list and returns true. If no such list
18277b024eSKalle Valo  * is found, false is returned.
19277b024eSKalle Valo  */
20277b024eSKalle Valo static bool
mwifiex_uap_del_tx_pkts_in_ralist(struct mwifiex_private * priv,struct list_head * ra_list_head,int tid)21277b024eSKalle Valo mwifiex_uap_del_tx_pkts_in_ralist(struct mwifiex_private *priv,
22277b024eSKalle Valo 				  struct list_head *ra_list_head,
23277b024eSKalle Valo 				  int tid)
24277b024eSKalle Valo {
25277b024eSKalle Valo 	struct mwifiex_ra_list_tbl *ra_list;
26277b024eSKalle Valo 	struct sk_buff *skb, *tmp;
27277b024eSKalle Valo 	bool pkt_deleted = false;
28277b024eSKalle Valo 	struct mwifiex_txinfo *tx_info;
29277b024eSKalle Valo 	struct mwifiex_adapter *adapter = priv->adapter;
30277b024eSKalle Valo 
31277b024eSKalle Valo 	list_for_each_entry(ra_list, ra_list_head, list) {
32277b024eSKalle Valo 		if (skb_queue_empty(&ra_list->skb_head))
33277b024eSKalle Valo 			continue;
34277b024eSKalle Valo 
35277b024eSKalle Valo 		skb_queue_walk_safe(&ra_list->skb_head, skb, tmp) {
36277b024eSKalle Valo 			tx_info = MWIFIEX_SKB_TXCB(skb);
37277b024eSKalle Valo 			if (tx_info->flags & MWIFIEX_BUF_FLAG_BRIDGED_PKT) {
38277b024eSKalle Valo 				__skb_unlink(skb, &ra_list->skb_head);
39277b024eSKalle Valo 				mwifiex_write_data_complete(adapter, skb, 0,
40277b024eSKalle Valo 							    -1);
41277b024eSKalle Valo 				if (ra_list->tx_paused)
42277b024eSKalle Valo 					priv->wmm.pkts_paused[tid]--;
43277b024eSKalle Valo 				else
44277b024eSKalle Valo 					atomic_dec(&priv->wmm.tx_pkts_queued);
45277b024eSKalle Valo 				pkt_deleted = true;
46277b024eSKalle Valo 			}
47277b024eSKalle Valo 			if ((atomic_read(&adapter->pending_bridged_pkts) <=
48277b024eSKalle Valo 					     MWIFIEX_BRIDGED_PKTS_THR_LOW))
49277b024eSKalle Valo 				break;
50277b024eSKalle Valo 		}
51277b024eSKalle Valo 	}
52277b024eSKalle Valo 
53277b024eSKalle Valo 	return pkt_deleted;
54277b024eSKalle Valo }
55277b024eSKalle Valo 
56277b024eSKalle Valo /* This function deletes packets from particular RA List. RA list index
57277b024eSKalle Valo  * from which packets are deleted is preserved so that packets from next RA
58277b024eSKalle Valo  * list are deleted upon subsequent call thus maintaining fairness.
59277b024eSKalle Valo  */
mwifiex_uap_cleanup_tx_queues(struct mwifiex_private * priv)60277b024eSKalle Valo static void mwifiex_uap_cleanup_tx_queues(struct mwifiex_private *priv)
61277b024eSKalle Valo {
62277b024eSKalle Valo 	struct list_head *ra_list;
63277b024eSKalle Valo 	int i;
64277b024eSKalle Valo 
658a7f9fd8SBrian Norris 	spin_lock_bh(&priv->wmm.ra_list_spinlock);
66277b024eSKalle Valo 
67277b024eSKalle Valo 	for (i = 0; i < MAX_NUM_TID; i++, priv->del_list_idx++) {
68277b024eSKalle Valo 		if (priv->del_list_idx == MAX_NUM_TID)
69277b024eSKalle Valo 			priv->del_list_idx = 0;
70277b024eSKalle Valo 		ra_list = &priv->wmm.tid_tbl_ptr[priv->del_list_idx].ra_list;
71277b024eSKalle Valo 		if (mwifiex_uap_del_tx_pkts_in_ralist(priv, ra_list, i)) {
72277b024eSKalle Valo 			priv->del_list_idx++;
73277b024eSKalle Valo 			break;
74277b024eSKalle Valo 		}
75277b024eSKalle Valo 	}
76277b024eSKalle Valo 
778a7f9fd8SBrian Norris 	spin_unlock_bh(&priv->wmm.ra_list_spinlock);
78277b024eSKalle Valo }
79277b024eSKalle Valo 
80277b024eSKalle Valo 
mwifiex_uap_queue_bridged_pkt(struct mwifiex_private * priv,struct sk_buff * skb)81277b024eSKalle Valo static void mwifiex_uap_queue_bridged_pkt(struct mwifiex_private *priv,
82277b024eSKalle Valo 					 struct sk_buff *skb)
83277b024eSKalle Valo {
84277b024eSKalle Valo 	struct mwifiex_adapter *adapter = priv->adapter;
85277b024eSKalle Valo 	struct uap_rxpd *uap_rx_pd;
86277b024eSKalle Valo 	struct rx_packet_hdr *rx_pkt_hdr;
87277b024eSKalle Valo 	struct sk_buff *new_skb;
88277b024eSKalle Valo 	struct mwifiex_txinfo *tx_info;
89277b024eSKalle Valo 	int hdr_chop;
90277b024eSKalle Valo 	struct ethhdr *p_ethhdr;
91277b024eSKalle Valo 	struct mwifiex_sta_node *src_node;
92bb28c28eSMarty Faltesek 	int index;
93277b024eSKalle Valo 
94277b024eSKalle Valo 	uap_rx_pd = (struct uap_rxpd *)(skb->data);
95277b024eSKalle Valo 	rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
96277b024eSKalle Valo 
97277b024eSKalle Valo 	if ((atomic_read(&adapter->pending_bridged_pkts) >=
98277b024eSKalle Valo 					     MWIFIEX_BRIDGED_PKTS_THR_HIGH)) {
99277b024eSKalle Valo 		mwifiex_dbg(priv->adapter, ERROR,
100277b024eSKalle Valo 			    "Tx: Bridge packet limit reached. Drop packet!\n");
101277b024eSKalle Valo 		kfree_skb(skb);
102277b024eSKalle Valo 		mwifiex_uap_cleanup_tx_queues(priv);
103277b024eSKalle Valo 		return;
104277b024eSKalle Valo 	}
105277b024eSKalle Valo 
10611958528SPolaris Pi 	if (sizeof(*rx_pkt_hdr) +
10711958528SPolaris Pi 	    le16_to_cpu(uap_rx_pd->rx_pkt_offset) > skb->len) {
10811958528SPolaris Pi 		mwifiex_dbg(adapter, ERROR,
10911958528SPolaris Pi 			    "wrong rx packet offset: len=%d,rx_pkt_offset=%d\n",
11011958528SPolaris Pi 			    skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset));
11111958528SPolaris Pi 		priv->stats.rx_dropped++;
11211958528SPolaris Pi 		dev_kfree_skb_any(skb);
1132785851cSPolaris Pi 		return;
11411958528SPolaris Pi 	}
11511958528SPolaris Pi 
116277b024eSKalle Valo 	if ((!memcmp(&rx_pkt_hdr->rfc1042_hdr, bridge_tunnel_header,
117277b024eSKalle Valo 		     sizeof(bridge_tunnel_header))) ||
118277b024eSKalle Valo 	    (!memcmp(&rx_pkt_hdr->rfc1042_hdr, rfc1042_header,
119277b024eSKalle Valo 		     sizeof(rfc1042_header)) &&
120277b024eSKalle Valo 	     ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_AARP &&
121277b024eSKalle Valo 	     ntohs(rx_pkt_hdr->rfc1042_hdr.snap_type) != ETH_P_IPX)) {
122277b024eSKalle Valo 		/* Replace the 803 header and rfc1042 header (llc/snap) with
123277b024eSKalle Valo 		 * an Ethernet II header, keep the src/dst and snap_type
124277b024eSKalle Valo 		 * (ethertype).
125277b024eSKalle Valo 		 *
126277b024eSKalle Valo 		 * The firmware only passes up SNAP frames converting all RX
127277b024eSKalle Valo 		 * data from 802.11 to 802.2/LLC/SNAP frames.
128277b024eSKalle Valo 		 *
129277b024eSKalle Valo 		 * To create the Ethernet II, just move the src, dst address
130277b024eSKalle Valo 		 * right before the snap_type.
131277b024eSKalle Valo 		 */
132277b024eSKalle Valo 		p_ethhdr = (struct ethhdr *)
133277b024eSKalle Valo 			((u8 *)(&rx_pkt_hdr->eth803_hdr)
134277b024eSKalle Valo 			 + sizeof(rx_pkt_hdr->eth803_hdr)
135277b024eSKalle Valo 			 + sizeof(rx_pkt_hdr->rfc1042_hdr)
136277b024eSKalle Valo 			 - sizeof(rx_pkt_hdr->eth803_hdr.h_dest)
137277b024eSKalle Valo 			 - sizeof(rx_pkt_hdr->eth803_hdr.h_source)
138277b024eSKalle Valo 			 - sizeof(rx_pkt_hdr->rfc1042_hdr.snap_type));
139277b024eSKalle Valo 		memcpy(p_ethhdr->h_source, rx_pkt_hdr->eth803_hdr.h_source,
140277b024eSKalle Valo 		       sizeof(p_ethhdr->h_source));
141277b024eSKalle Valo 		memcpy(p_ethhdr->h_dest, rx_pkt_hdr->eth803_hdr.h_dest,
142277b024eSKalle Valo 		       sizeof(p_ethhdr->h_dest));
143277b024eSKalle Valo 		/* Chop off the rxpd + the excess memory from
144277b024eSKalle Valo 		 * 802.2/llc/snap header that was removed.
145277b024eSKalle Valo 		 */
146277b024eSKalle Valo 		hdr_chop = (u8 *)p_ethhdr - (u8 *)uap_rx_pd;
147277b024eSKalle Valo 	} else {
148277b024eSKalle Valo 		/* Chop off the rxpd */
149277b024eSKalle Valo 		hdr_chop = (u8 *)&rx_pkt_hdr->eth803_hdr - (u8 *)uap_rx_pd;
150277b024eSKalle Valo 	}
151277b024eSKalle Valo 
152277b024eSKalle Valo 	/* Chop off the leading header bytes so that it points
153277b024eSKalle Valo 	 * to the start of either the reconstructed EthII frame
154277b024eSKalle Valo 	 * or the 802.2/llc/snap frame.
155277b024eSKalle Valo 	 */
156277b024eSKalle Valo 	skb_pull(skb, hdr_chop);
157277b024eSKalle Valo 
158277b024eSKalle Valo 	if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
159277b024eSKalle Valo 		mwifiex_dbg(priv->adapter, ERROR,
160277b024eSKalle Valo 			    "data: Tx: insufficient skb headroom %d\n",
161277b024eSKalle Valo 			    skb_headroom(skb));
162277b024eSKalle Valo 		/* Insufficient skb headroom - allocate a new skb */
163277b024eSKalle Valo 		new_skb =
164277b024eSKalle Valo 			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
165277b024eSKalle Valo 		if (unlikely(!new_skb)) {
166277b024eSKalle Valo 			mwifiex_dbg(priv->adapter, ERROR,
167277b024eSKalle Valo 				    "Tx: cannot allocate new_skb\n");
168277b024eSKalle Valo 			kfree_skb(skb);
169277b024eSKalle Valo 			priv->stats.tx_dropped++;
170277b024eSKalle Valo 			return;
171277b024eSKalle Valo 		}
172277b024eSKalle Valo 
173277b024eSKalle Valo 		kfree_skb(skb);
174277b024eSKalle Valo 		skb = new_skb;
175277b024eSKalle Valo 		mwifiex_dbg(priv->adapter, INFO,
176277b024eSKalle Valo 			    "info: new skb headroom %d\n",
177277b024eSKalle Valo 			    skb_headroom(skb));
178277b024eSKalle Valo 	}
179277b024eSKalle Valo 
180277b024eSKalle Valo 	tx_info = MWIFIEX_SKB_TXCB(skb);
181277b024eSKalle Valo 	memset(tx_info, 0, sizeof(*tx_info));
182277b024eSKalle Valo 	tx_info->bss_num = priv->bss_num;
183277b024eSKalle Valo 	tx_info->bss_type = priv->bss_type;
184277b024eSKalle Valo 	tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
185277b024eSKalle Valo 
186277b024eSKalle Valo 	src_node = mwifiex_get_sta_entry(priv, rx_pkt_hdr->eth803_hdr.h_source);
187277b024eSKalle Valo 	if (src_node) {
188277b024eSKalle Valo 		src_node->stats.last_rx = jiffies;
189277b024eSKalle Valo 		src_node->stats.rx_bytes += skb->len;
190277b024eSKalle Valo 		src_node->stats.rx_packets++;
191277b024eSKalle Valo 		src_node->stats.last_tx_rate = uap_rx_pd->rx_rate;
192277b024eSKalle Valo 		src_node->stats.last_tx_htinfo = uap_rx_pd->ht_info;
193277b024eSKalle Valo 	}
194277b024eSKalle Valo 
195277b024eSKalle Valo 	if (is_unicast_ether_addr(rx_pkt_hdr->eth803_hdr.h_dest)) {
196277b024eSKalle Valo 		/* Update bridge packet statistics as the
197277b024eSKalle Valo 		 * packet is not going to kernel/upper layer.
198277b024eSKalle Valo 		 */
199277b024eSKalle Valo 		priv->stats.rx_bytes += skb->len;
200277b024eSKalle Valo 		priv->stats.rx_packets++;
201277b024eSKalle Valo 
202277b024eSKalle Valo 		/* Sending bridge packet to TX queue, so save the packet
203277b024eSKalle Valo 		 * length in TXCB to update statistics in TX complete.
204277b024eSKalle Valo 		 */
205277b024eSKalle Valo 		tx_info->pkt_len = skb->len;
206277b024eSKalle Valo 	}
207277b024eSKalle Valo 
208277b024eSKalle Valo 	__net_timestamp(skb);
209bb28c28eSMarty Faltesek 
210bb28c28eSMarty Faltesek 	index = mwifiex_1d_to_wmm_queue[skb->priority];
211bb28c28eSMarty Faltesek 	atomic_inc(&priv->wmm_tx_pending[index]);
212277b024eSKalle Valo 	mwifiex_wmm_add_buf_txqueue(priv, skb);
213277b024eSKalle Valo 	atomic_inc(&adapter->tx_pending);
214277b024eSKalle Valo 	atomic_inc(&adapter->pending_bridged_pkts);
215277b024eSKalle Valo 
216ad5ca845SXinming Hu 	mwifiex_queue_main_work(priv->adapter);
217ad5ca845SXinming Hu 
218277b024eSKalle Valo 	return;
219277b024eSKalle Valo }
220277b024eSKalle Valo 
221277b024eSKalle Valo /*
222277b024eSKalle Valo  * This function contains logic for AP packet forwarding.
223277b024eSKalle Valo  *
224277b024eSKalle Valo  * If a packet is multicast/broadcast, it is sent to kernel/upper layer
225277b024eSKalle Valo  * as well as queued back to AP TX queue so that it can be sent to other
226277b024eSKalle Valo  * associated stations.
227277b024eSKalle Valo  * If a packet is unicast and RA is present in associated station list,
228277b024eSKalle Valo  * it is again requeued into AP TX queue.
229277b024eSKalle Valo  * If a packet is unicast and RA is not in associated station list,
230277b024eSKalle Valo  * packet is forwarded to kernel to handle routing logic.
231277b024eSKalle Valo  */
mwifiex_handle_uap_rx_forward(struct mwifiex_private * priv,struct sk_buff * skb)232277b024eSKalle Valo int mwifiex_handle_uap_rx_forward(struct mwifiex_private *priv,
233277b024eSKalle Valo 				  struct sk_buff *skb)
234277b024eSKalle Valo {
235277b024eSKalle Valo 	struct mwifiex_adapter *adapter = priv->adapter;
236277b024eSKalle Valo 	struct uap_rxpd *uap_rx_pd;
237277b024eSKalle Valo 	struct rx_packet_hdr *rx_pkt_hdr;
238277b024eSKalle Valo 	u8 ra[ETH_ALEN];
239277b024eSKalle Valo 	struct sk_buff *skb_uap;
240277b024eSKalle Valo 
241277b024eSKalle Valo 	uap_rx_pd = (struct uap_rxpd *)(skb->data);
242277b024eSKalle Valo 	rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
243277b024eSKalle Valo 
244277b024eSKalle Valo 	/* don't do packet forwarding in disconnected state */
245277b024eSKalle Valo 	if (!priv->media_connected) {
246277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
247277b024eSKalle Valo 			    "drop packet in disconnected state.\n");
248277b024eSKalle Valo 		dev_kfree_skb_any(skb);
249277b024eSKalle Valo 		return 0;
250277b024eSKalle Valo 	}
251277b024eSKalle Valo 
252277b024eSKalle Valo 	memcpy(ra, rx_pkt_hdr->eth803_hdr.h_dest, ETH_ALEN);
253277b024eSKalle Valo 
254277b024eSKalle Valo 	if (is_multicast_ether_addr(ra)) {
255277b024eSKalle Valo 		skb_uap = skb_copy(skb, GFP_ATOMIC);
256*35a7a1ceSDmitry Antipov 		if (likely(skb_uap)) {
257277b024eSKalle Valo 			mwifiex_uap_queue_bridged_pkt(priv, skb_uap);
258277b024eSKalle Valo 		} else {
259*35a7a1ceSDmitry Antipov 			mwifiex_dbg(adapter, ERROR,
260*35a7a1ceSDmitry Antipov 				    "failed to copy skb for uAP\n");
261*35a7a1ceSDmitry Antipov 			priv->stats.rx_dropped++;
262*35a7a1ceSDmitry Antipov 			dev_kfree_skb_any(skb);
263*35a7a1ceSDmitry Antipov 			return -1;
264*35a7a1ceSDmitry Antipov 		}
265*35a7a1ceSDmitry Antipov 	} else {
266277b024eSKalle Valo 		if (mwifiex_get_sta_entry(priv, ra)) {
267277b024eSKalle Valo 			/* Requeue Intra-BSS packet */
268277b024eSKalle Valo 			mwifiex_uap_queue_bridged_pkt(priv, skb);
269277b024eSKalle Valo 			return 0;
270277b024eSKalle Valo 		}
271277b024eSKalle Valo 	}
272277b024eSKalle Valo 
273277b024eSKalle Valo 	/* Forward unicat/Inter-BSS packets to kernel. */
274277b024eSKalle Valo 	return mwifiex_process_rx_packet(priv, skb);
275277b024eSKalle Valo }
276277b024eSKalle Valo 
mwifiex_uap_recv_packet(struct mwifiex_private * priv,struct sk_buff * skb)277bf00dc22SXinming Hu int mwifiex_uap_recv_packet(struct mwifiex_private *priv,
278bf00dc22SXinming Hu 			    struct sk_buff *skb)
279bf00dc22SXinming Hu {
2803fdbda44SHeinrich Schuchardt 	struct mwifiex_adapter *adapter = priv->adapter;
281bf00dc22SXinming Hu 	struct mwifiex_sta_node *src_node;
282bf00dc22SXinming Hu 	struct ethhdr *p_ethhdr;
283bf00dc22SXinming Hu 	struct sk_buff *skb_uap;
284bf00dc22SXinming Hu 	struct mwifiex_txinfo *tx_info;
285bf00dc22SXinming Hu 
286bf00dc22SXinming Hu 	if (!skb)
287bf00dc22SXinming Hu 		return -1;
288bf00dc22SXinming Hu 
289bf00dc22SXinming Hu 	p_ethhdr = (void *)skb->data;
290bf00dc22SXinming Hu 	src_node = mwifiex_get_sta_entry(priv, p_ethhdr->h_source);
291bf00dc22SXinming Hu 	if (src_node) {
292bf00dc22SXinming Hu 		src_node->stats.last_rx = jiffies;
293bf00dc22SXinming Hu 		src_node->stats.rx_bytes += skb->len;
294bf00dc22SXinming Hu 		src_node->stats.rx_packets++;
295bf00dc22SXinming Hu 	}
296bf00dc22SXinming Hu 
297bf00dc22SXinming Hu 	if (is_multicast_ether_addr(p_ethhdr->h_dest) ||
298bf00dc22SXinming Hu 	    mwifiex_get_sta_entry(priv, p_ethhdr->h_dest)) {
299bf00dc22SXinming Hu 		if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN)
300bf00dc22SXinming Hu 			skb_uap =
301bf00dc22SXinming Hu 			skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
302bf00dc22SXinming Hu 		else
303bf00dc22SXinming Hu 			skb_uap = skb_copy(skb, GFP_ATOMIC);
304bf00dc22SXinming Hu 
305bf00dc22SXinming Hu 		if (likely(skb_uap)) {
306bf00dc22SXinming Hu 			tx_info = MWIFIEX_SKB_TXCB(skb_uap);
307bf00dc22SXinming Hu 			memset(tx_info, 0, sizeof(*tx_info));
308bf00dc22SXinming Hu 			tx_info->bss_num = priv->bss_num;
309bf00dc22SXinming Hu 			tx_info->bss_type = priv->bss_type;
310bf00dc22SXinming Hu 			tx_info->flags |= MWIFIEX_BUF_FLAG_BRIDGED_PKT;
311bf00dc22SXinming Hu 			__net_timestamp(skb_uap);
312bf00dc22SXinming Hu 			mwifiex_wmm_add_buf_txqueue(priv, skb_uap);
313bf00dc22SXinming Hu 			atomic_inc(&adapter->tx_pending);
314bf00dc22SXinming Hu 			atomic_inc(&adapter->pending_bridged_pkts);
315bf00dc22SXinming Hu 			if ((atomic_read(&adapter->pending_bridged_pkts) >=
316bf00dc22SXinming Hu 					MWIFIEX_BRIDGED_PKTS_THR_HIGH)) {
317bf00dc22SXinming Hu 				mwifiex_dbg(adapter, ERROR,
318bf00dc22SXinming Hu 					    "Tx: Bridge packet limit reached. Drop packet!\n");
319bf00dc22SXinming Hu 				mwifiex_uap_cleanup_tx_queues(priv);
320bf00dc22SXinming Hu 			}
321bf00dc22SXinming Hu 
322bf00dc22SXinming Hu 		} else {
323bf00dc22SXinming Hu 			mwifiex_dbg(adapter, ERROR, "failed to allocate skb_uap");
324bf00dc22SXinming Hu 		}
325bf00dc22SXinming Hu 
326bf00dc22SXinming Hu 		mwifiex_queue_main_work(adapter);
327bf00dc22SXinming Hu 		/* Don't forward Intra-BSS unicast packet to upper layer*/
328bf00dc22SXinming Hu 		if (mwifiex_get_sta_entry(priv, p_ethhdr->h_dest))
329bf00dc22SXinming Hu 			return 0;
330bf00dc22SXinming Hu 	}
331bf00dc22SXinming Hu 
33238013eefSXinming Hu 	skb->dev = priv->netdev;
33338013eefSXinming Hu 	skb->protocol = eth_type_trans(skb, priv->netdev);
33438013eefSXinming Hu 	skb->ip_summed = CHECKSUM_NONE;
33538013eefSXinming Hu 
33638013eefSXinming Hu 	/* This is required only in case of 11n and USB/PCIE as we alloc
33738013eefSXinming Hu 	 * a buffer of 4K only if its 11N (to be able to receive 4K
33838013eefSXinming Hu 	 * AMSDU packets). In case of SD we allocate buffers based
33938013eefSXinming Hu 	 * on the size of packet and hence this is not needed.
34038013eefSXinming Hu 	 *
34138013eefSXinming Hu 	 * Modifying the truesize here as our allocation for each
34238013eefSXinming Hu 	 * skb is 4K but we only receive 2K packets and this cause
34338013eefSXinming Hu 	 * the kernel to start dropping packets in case where
34438013eefSXinming Hu 	 * application has allocated buffer based on 2K size i.e.
34538013eefSXinming Hu 	 * if there a 64K packet received (in IP fragments and
34638013eefSXinming Hu 	 * application allocates 64K to receive this packet but
34738013eefSXinming Hu 	 * this packet would almost double up because we allocate
34838013eefSXinming Hu 	 * each 1.5K fragment in 4K and pass it up. As soon as the
34938013eefSXinming Hu 	 * 64K limit hits kernel will start to drop rest of the
35038013eefSXinming Hu 	 * fragments. Currently we fail the Filesndl-ht.scr script
35138013eefSXinming Hu 	 * for UDP, hence this fix
35238013eefSXinming Hu 	 */
35338013eefSXinming Hu 	if ((adapter->iface_type == MWIFIEX_USB ||
35438013eefSXinming Hu 	     adapter->iface_type == MWIFIEX_PCIE) &&
35538013eefSXinming Hu 	    skb->truesize > MWIFIEX_RX_DATA_BUF_SIZE)
35638013eefSXinming Hu 		skb->truesize += (skb->len - MWIFIEX_RX_DATA_BUF_SIZE);
35738013eefSXinming Hu 
358bf00dc22SXinming Hu 	/* Forward multicast/broadcast packet to upper layer*/
359afb6d39fSSebastian Andrzej Siewior 	netif_rx(skb);
360bf00dc22SXinming Hu 	return 0;
361bf00dc22SXinming Hu }
362bf00dc22SXinming Hu 
363277b024eSKalle Valo /*
364277b024eSKalle Valo  * This function processes the packet received on AP interface.
365277b024eSKalle Valo  *
366277b024eSKalle Valo  * The function looks into the RxPD and performs sanity tests on the
367277b024eSKalle Valo  * received buffer to ensure its a valid packet before processing it
368277b024eSKalle Valo  * further. If the packet is determined to be aggregated, it is
369277b024eSKalle Valo  * de-aggregated accordingly. Then skb is passed to AP packet forwarding logic.
370277b024eSKalle Valo  *
371277b024eSKalle Valo  * The completion callback is called after processing is complete.
372277b024eSKalle Valo  */
mwifiex_process_uap_rx_packet(struct mwifiex_private * priv,struct sk_buff * skb)373277b024eSKalle Valo int mwifiex_process_uap_rx_packet(struct mwifiex_private *priv,
374277b024eSKalle Valo 				  struct sk_buff *skb)
375277b024eSKalle Valo {
376277b024eSKalle Valo 	struct mwifiex_adapter *adapter = priv->adapter;
377277b024eSKalle Valo 	int ret;
378277b024eSKalle Valo 	struct uap_rxpd *uap_rx_pd;
379277b024eSKalle Valo 	struct rx_packet_hdr *rx_pkt_hdr;
380277b024eSKalle Valo 	u16 rx_pkt_type;
381277b024eSKalle Valo 	u8 ta[ETH_ALEN], pkt_type;
382277b024eSKalle Valo 	struct mwifiex_sta_node *node;
383277b024eSKalle Valo 
384277b024eSKalle Valo 	uap_rx_pd = (struct uap_rxpd *)(skb->data);
385277b024eSKalle Valo 	rx_pkt_type = le16_to_cpu(uap_rx_pd->rx_pkt_type);
386277b024eSKalle Valo 	rx_pkt_hdr = (void *)uap_rx_pd + le16_to_cpu(uap_rx_pd->rx_pkt_offset);
387277b024eSKalle Valo 
38811958528SPolaris Pi 	if (le16_to_cpu(uap_rx_pd->rx_pkt_offset) +
38911958528SPolaris Pi 	    sizeof(rx_pkt_hdr->eth803_hdr) > skb->len) {
39011958528SPolaris Pi 		mwifiex_dbg(adapter, ERROR,
39111958528SPolaris Pi 			    "wrong rx packet for struct ethhdr: len=%d, offset=%d\n",
39211958528SPolaris Pi 			    skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset));
39311958528SPolaris Pi 		priv->stats.rx_dropped++;
39411958528SPolaris Pi 		dev_kfree_skb_any(skb);
39511958528SPolaris Pi 		return 0;
39611958528SPolaris Pi 	}
39711958528SPolaris Pi 
398277b024eSKalle Valo 	ether_addr_copy(ta, rx_pkt_hdr->eth803_hdr.h_source);
399277b024eSKalle Valo 
400277b024eSKalle Valo 	if ((le16_to_cpu(uap_rx_pd->rx_pkt_offset) +
401277b024eSKalle Valo 	     le16_to_cpu(uap_rx_pd->rx_pkt_length)) > (u16) skb->len) {
402277b024eSKalle Valo 		mwifiex_dbg(adapter, ERROR,
403277b024eSKalle Valo 			    "wrong rx packet: len=%d, offset=%d, length=%d\n",
404277b024eSKalle Valo 			    skb->len, le16_to_cpu(uap_rx_pd->rx_pkt_offset),
405277b024eSKalle Valo 			    le16_to_cpu(uap_rx_pd->rx_pkt_length));
406277b024eSKalle Valo 		priv->stats.rx_dropped++;
407277b024eSKalle Valo 
408277b024eSKalle Valo 		node = mwifiex_get_sta_entry(priv, ta);
409277b024eSKalle Valo 		if (node)
410277b024eSKalle Valo 			node->stats.tx_failed++;
411277b024eSKalle Valo 
412277b024eSKalle Valo 		dev_kfree_skb_any(skb);
413277b024eSKalle Valo 		return 0;
414277b024eSKalle Valo 	}
415277b024eSKalle Valo 
416277b024eSKalle Valo 	if (rx_pkt_type == PKT_TYPE_MGMT) {
417277b024eSKalle Valo 		ret = mwifiex_process_mgmt_packet(priv, skb);
418277b024eSKalle Valo 		if (ret)
419bd642acfSAmitkumar Karwar 			mwifiex_dbg(adapter, DATA, "Rx of mgmt packet failed");
420277b024eSKalle Valo 		dev_kfree_skb_any(skb);
421277b024eSKalle Valo 		return ret;
422277b024eSKalle Valo 	}
423277b024eSKalle Valo 
424277b024eSKalle Valo 
425277b024eSKalle Valo 	if (rx_pkt_type != PKT_TYPE_BAR && uap_rx_pd->priority < MAX_NUM_TID) {
4268a7f9fd8SBrian Norris 		spin_lock_bh(&priv->sta_list_spinlock);
427277b024eSKalle Valo 		node = mwifiex_get_sta_entry(priv, ta);
428277b024eSKalle Valo 		if (node)
429277b024eSKalle Valo 			node->rx_seq[uap_rx_pd->priority] =
430277b024eSKalle Valo 						le16_to_cpu(uap_rx_pd->seq_num);
4318a7f9fd8SBrian Norris 		spin_unlock_bh(&priv->sta_list_spinlock);
432277b024eSKalle Valo 	}
433277b024eSKalle Valo 
434277b024eSKalle Valo 	if (!priv->ap_11n_enabled ||
435277b024eSKalle Valo 	    (!mwifiex_11n_get_rx_reorder_tbl(priv, uap_rx_pd->priority, ta) &&
436277b024eSKalle Valo 	    (le16_to_cpu(uap_rx_pd->rx_pkt_type) != PKT_TYPE_AMSDU))) {
437277b024eSKalle Valo 		ret = mwifiex_handle_uap_rx_forward(priv, skb);
438277b024eSKalle Valo 		return ret;
439277b024eSKalle Valo 	}
440277b024eSKalle Valo 
441277b024eSKalle Valo 	/* Reorder and send to kernel */
442277b024eSKalle Valo 	pkt_type = (u8)le16_to_cpu(uap_rx_pd->rx_pkt_type);
443277b024eSKalle Valo 	ret = mwifiex_11n_rx_reorder_pkt(priv, le16_to_cpu(uap_rx_pd->seq_num),
444277b024eSKalle Valo 					 uap_rx_pd->priority, ta, pkt_type,
445277b024eSKalle Valo 					 skb);
446277b024eSKalle Valo 
447277b024eSKalle Valo 	if (ret || (rx_pkt_type == PKT_TYPE_BAR))
448277b024eSKalle Valo 		dev_kfree_skb_any(skb);
449277b024eSKalle Valo 
450277b024eSKalle Valo 	if (ret)
451277b024eSKalle Valo 		priv->stats.rx_dropped++;
452277b024eSKalle Valo 
453277b024eSKalle Valo 	return ret;
454277b024eSKalle Valo }
455277b024eSKalle Valo 
456277b024eSKalle Valo /*
457277b024eSKalle Valo  * This function fills the TxPD for AP tx packets.
458277b024eSKalle Valo  *
459277b024eSKalle Valo  * The Tx buffer received by this function should already have the
460277b024eSKalle Valo  * header space allocated for TxPD.
461277b024eSKalle Valo  *
462277b024eSKalle Valo  * This function inserts the TxPD in between interface header and actual
463277b024eSKalle Valo  * data and adjusts the buffer pointers accordingly.
464277b024eSKalle Valo  *
465277b024eSKalle Valo  * The following TxPD fields are set by this function, as required -
466277b024eSKalle Valo  *      - BSS number
467277b024eSKalle Valo  *      - Tx packet length and offset
468277b024eSKalle Valo  *      - Priority
469277b024eSKalle Valo  *      - Packet delay
470277b024eSKalle Valo  *      - Priority specific Tx control
471277b024eSKalle Valo  *      - Flags
472277b024eSKalle Valo  */
mwifiex_process_uap_txpd(struct mwifiex_private * priv,struct sk_buff * skb)47335983875SDmitry Antipov void mwifiex_process_uap_txpd(struct mwifiex_private *priv,
474277b024eSKalle Valo 			      struct sk_buff *skb)
475277b024eSKalle Valo {
476277b024eSKalle Valo 	struct mwifiex_adapter *adapter = priv->adapter;
477277b024eSKalle Valo 	struct uap_txpd *txpd;
478277b024eSKalle Valo 	struct mwifiex_txinfo *tx_info = MWIFIEX_SKB_TXCB(skb);
479277b024eSKalle Valo 	int pad;
480277b024eSKalle Valo 	u16 pkt_type, pkt_offset;
481f4c5d599SXinming Hu 	int hroom = adapter->intf_hdr_len;
482277b024eSKalle Valo 
483277b024eSKalle Valo 	pkt_type = mwifiex_is_skb_mgmt_frame(skb) ? PKT_TYPE_MGMT : 0;
484277b024eSKalle Valo 
485603a1621SArnd Bergmann 	pad = ((uintptr_t)skb->data - (sizeof(*txpd) + hroom)) &
486277b024eSKalle Valo 	       (MWIFIEX_DMA_ALIGN_SZ - 1);
487277b024eSKalle Valo 
488277b024eSKalle Valo 	skb_push(skb, sizeof(*txpd) + pad);
489277b024eSKalle Valo 
490277b024eSKalle Valo 	txpd = (struct uap_txpd *)skb->data;
491277b024eSKalle Valo 	memset(txpd, 0, sizeof(*txpd));
492277b024eSKalle Valo 	txpd->bss_num = priv->bss_num;
493277b024eSKalle Valo 	txpd->bss_type = priv->bss_type;
494277b024eSKalle Valo 	txpd->tx_pkt_length = cpu_to_le16((u16)(skb->len - (sizeof(*txpd) +
495277b024eSKalle Valo 						pad)));
496277b024eSKalle Valo 	txpd->priority = (u8)skb->priority;
497277b024eSKalle Valo 
498277b024eSKalle Valo 	txpd->pkt_delay_2ms = mwifiex_wmm_compute_drv_pkt_delay(priv, skb);
499277b024eSKalle Valo 
500277b024eSKalle Valo 	if (tx_info->flags & MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS ||
501277b024eSKalle Valo 	    tx_info->flags & MWIFIEX_BUF_FLAG_ACTION_TX_STATUS) {
502277b024eSKalle Valo 		txpd->tx_token_id = tx_info->ack_frame_id;
503277b024eSKalle Valo 		txpd->flags |= MWIFIEX_TXPD_FLAGS_REQ_TX_STATUS;
504277b024eSKalle Valo 	}
505277b024eSKalle Valo 
506277b024eSKalle Valo 	if (txpd->priority < ARRAY_SIZE(priv->wmm.user_pri_pkt_tx_ctrl))
507277b024eSKalle Valo 		/*
508277b024eSKalle Valo 		 * Set the priority specific tx_control field, setting of 0 will
509277b024eSKalle Valo 		 * cause the default value to be used later in this function.
510277b024eSKalle Valo 		 */
511277b024eSKalle Valo 		txpd->tx_control =
512277b024eSKalle Valo 		    cpu_to_le32(priv->wmm.user_pri_pkt_tx_ctrl[txpd->priority]);
513277b024eSKalle Valo 
514277b024eSKalle Valo 	/* Offset of actual data */
515277b024eSKalle Valo 	pkt_offset = sizeof(*txpd) + pad;
516277b024eSKalle Valo 	if (pkt_type == PKT_TYPE_MGMT) {
517277b024eSKalle Valo 		/* Set the packet type and add header for management frame */
518277b024eSKalle Valo 		txpd->tx_pkt_type = cpu_to_le16(pkt_type);
519277b024eSKalle Valo 		pkt_offset += MWIFIEX_MGMT_FRAME_HEADER_SIZE;
520277b024eSKalle Valo 	}
521277b024eSKalle Valo 
522277b024eSKalle Valo 	txpd->tx_pkt_offset = cpu_to_le16(pkt_offset);
523277b024eSKalle Valo 
524f4c5d599SXinming Hu 	/* make space for adapter->intf_hdr_len */
525277b024eSKalle Valo 	skb_push(skb, hroom);
526277b024eSKalle Valo 
527277b024eSKalle Valo 	if (!txpd->tx_control)
528277b024eSKalle Valo 		/* TxCtrl set by user or default */
529277b024eSKalle Valo 		txpd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl);
530277b024eSKalle Valo }
531