xref: /openbmc/linux/drivers/net/wireless/marvell/libertas/rx.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f988d640SKalle Valo /*
3f988d640SKalle Valo  * This file contains the handling of RX in wlan driver.
4f988d640SKalle Valo  */
5f988d640SKalle Valo 
6f988d640SKalle Valo #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7f988d640SKalle Valo 
8f988d640SKalle Valo #include <linux/etherdevice.h>
9f988d640SKalle Valo #include <linux/hardirq.h>
10f988d640SKalle Valo #include <linux/slab.h>
11f988d640SKalle Valo #include <linux/types.h>
12f988d640SKalle Valo #include <linux/export.h>
13f988d640SKalle Valo #include <net/cfg80211.h>
14f988d640SKalle Valo 
15f988d640SKalle Valo #include "defs.h"
16f988d640SKalle Valo #include "host.h"
17f988d640SKalle Valo #include "radiotap.h"
18f988d640SKalle Valo #include "decl.h"
19f988d640SKalle Valo #include "dev.h"
20f988d640SKalle Valo #include "mesh.h"
21f988d640SKalle Valo 
22f988d640SKalle Valo struct eth803hdr {
23f988d640SKalle Valo 	u8 dest_addr[6];
24f988d640SKalle Valo 	u8 src_addr[6];
25f988d640SKalle Valo 	u16 h803_len;
26f988d640SKalle Valo } __packed;
27f988d640SKalle Valo 
28f988d640SKalle Valo struct rfc1042hdr {
29f988d640SKalle Valo 	u8 llc_dsap;
30f988d640SKalle Valo 	u8 llc_ssap;
31f988d640SKalle Valo 	u8 llc_ctrl;
32f988d640SKalle Valo 	u8 snap_oui[3];
33f988d640SKalle Valo 	u16 snap_type;
34f988d640SKalle Valo } __packed;
35f988d640SKalle Valo 
36f988d640SKalle Valo struct rxpackethdr {
37f988d640SKalle Valo 	struct eth803hdr eth803_hdr;
38f988d640SKalle Valo 	struct rfc1042hdr rfc1042_hdr;
39f988d640SKalle Valo } __packed;
40f988d640SKalle Valo 
41f988d640SKalle Valo struct rx80211packethdr {
42f988d640SKalle Valo 	struct rxpd rx_pd;
43f988d640SKalle Valo 	void *eth80211_hdr;
44f988d640SKalle Valo } __packed;
45f988d640SKalle Valo 
46f988d640SKalle Valo static int process_rxed_802_11_packet(struct lbs_private *priv,
47f988d640SKalle Valo 	struct sk_buff *skb);
48f988d640SKalle Valo 
49f988d640SKalle Valo /**
50f988d640SKalle Valo  * lbs_process_rxed_packet - processes received packet and forwards it
51f988d640SKalle Valo  * to kernel/upper layer
52f988d640SKalle Valo  *
53f988d640SKalle Valo  * @priv:	A pointer to &struct lbs_private
54f988d640SKalle Valo  * @skb:	A pointer to skb which includes the received packet
55f988d640SKalle Valo  * returns:	0 or -1
56f988d640SKalle Valo  */
lbs_process_rxed_packet(struct lbs_private * priv,struct sk_buff * skb)57f988d640SKalle Valo int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
58f988d640SKalle Valo {
59f988d640SKalle Valo 	int ret = 0;
60f988d640SKalle Valo 	struct net_device *dev = priv->dev;
61f988d640SKalle Valo 	struct rxpackethdr *p_rx_pkt;
62f988d640SKalle Valo 	struct rxpd *p_rx_pd;
63f988d640SKalle Valo 	int hdrchop;
64f988d640SKalle Valo 	struct ethhdr *p_ethhdr;
65f988d640SKalle Valo 
66f988d640SKalle Valo 	BUG_ON(!skb);
67f988d640SKalle Valo 
68f988d640SKalle Valo 	skb->ip_summed = CHECKSUM_NONE;
69f988d640SKalle Valo 
70f988d640SKalle Valo 	if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
71f988d640SKalle Valo 		ret = process_rxed_802_11_packet(priv, skb);
72f988d640SKalle Valo 		goto done;
73f988d640SKalle Valo 	}
74f988d640SKalle Valo 
75f988d640SKalle Valo 	p_rx_pd = (struct rxpd *) skb->data;
76f988d640SKalle Valo 	p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
77f988d640SKalle Valo 		le32_to_cpu(p_rx_pd->pkt_ptr));
78f988d640SKalle Valo 
79f988d640SKalle Valo 	dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
80f988d640SKalle Valo 
81f988d640SKalle Valo 	lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
82f988d640SKalle Valo 		 min_t(unsigned int, skb->len, 100));
83f988d640SKalle Valo 
84f988d640SKalle Valo 	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
85f988d640SKalle Valo 		lbs_deb_rx("rx err: frame received with bad length\n");
86f988d640SKalle Valo 		dev->stats.rx_length_errors++;
87f988d640SKalle Valo 		ret = -EINVAL;
88f988d640SKalle Valo 		dev_kfree_skb(skb);
89f988d640SKalle Valo 		goto done;
90f988d640SKalle Valo 	}
91f988d640SKalle Valo 
92f988d640SKalle Valo 	lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
93f988d640SKalle Valo 		skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
94f988d640SKalle Valo 		skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
95f988d640SKalle Valo 
96f988d640SKalle Valo 	lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
97f988d640SKalle Valo 		sizeof(p_rx_pkt->eth803_hdr.dest_addr));
98f988d640SKalle Valo 	lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
99f988d640SKalle Valo 		sizeof(p_rx_pkt->eth803_hdr.src_addr));
100f988d640SKalle Valo 
101f988d640SKalle Valo 	if (memcmp(&p_rx_pkt->rfc1042_hdr,
102729ef6b6SPascal Terjan 		   rfc1042_header, sizeof(rfc1042_header)) == 0) {
103f988d640SKalle Valo 		/*
104f988d640SKalle Valo 		 *  Replace the 803 header and rfc1042 header (llc/snap) with an
105f988d640SKalle Valo 		 *    EthernetII header, keep the src/dst and snap_type (ethertype)
106f988d640SKalle Valo 		 *
107f988d640SKalle Valo 		 *  The firmware only passes up SNAP frames converting
108f988d640SKalle Valo 		 *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
109f988d640SKalle Valo 		 *
110f988d640SKalle Valo 		 *  To create the Ethernet II, just move the src, dst address right
111f988d640SKalle Valo 		 *    before the snap_type.
112f988d640SKalle Valo 		 */
113f988d640SKalle Valo 		p_ethhdr = (struct ethhdr *)
114f988d640SKalle Valo 		    ((u8 *) &p_rx_pkt->eth803_hdr
115f988d640SKalle Valo 		     + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
116f988d640SKalle Valo 		     - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
117f988d640SKalle Valo 		     - sizeof(p_rx_pkt->eth803_hdr.src_addr)
118f988d640SKalle Valo 		     - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
119f988d640SKalle Valo 
120f988d640SKalle Valo 		memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
121f988d640SKalle Valo 		       sizeof(p_ethhdr->h_source));
122f988d640SKalle Valo 		memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
123f988d640SKalle Valo 		       sizeof(p_ethhdr->h_dest));
124f988d640SKalle Valo 
125f988d640SKalle Valo 		/* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
126f988d640SKalle Valo 		 *   that was removed
127f988d640SKalle Valo 		 */
128f988d640SKalle Valo 		hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
129f988d640SKalle Valo 	} else {
130f988d640SKalle Valo 		lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
131f988d640SKalle Valo 			(u8 *) &p_rx_pkt->rfc1042_hdr,
132f988d640SKalle Valo 			sizeof(p_rx_pkt->rfc1042_hdr));
133f988d640SKalle Valo 
134f988d640SKalle Valo 		/* Chop off the rxpd */
135f988d640SKalle Valo 		hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
136f988d640SKalle Valo 	}
137f988d640SKalle Valo 
138f988d640SKalle Valo 	/* Chop off the leading header bytes so the skb points to the start of
139f988d640SKalle Valo 	 *   either the reconstructed EthII frame or the 802.2/llc/snap frame
140f988d640SKalle Valo 	 */
141f988d640SKalle Valo 	skb_pull(skb, hdrchop);
142f988d640SKalle Valo 
143f988d640SKalle Valo 	priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
144f988d640SKalle Valo 
145f988d640SKalle Valo 	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
146f988d640SKalle Valo 	dev->stats.rx_bytes += skb->len;
147f988d640SKalle Valo 	dev->stats.rx_packets++;
148f988d640SKalle Valo 
149f988d640SKalle Valo 	skb->protocol = eth_type_trans(skb, dev);
150*afb6d39fSSebastian Andrzej Siewior 	netif_rx(skb);
151f988d640SKalle Valo 
152f988d640SKalle Valo 	ret = 0;
153f988d640SKalle Valo done:
154f988d640SKalle Valo 	return ret;
155f988d640SKalle Valo }
156f988d640SKalle Valo EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
157f988d640SKalle Valo 
158f988d640SKalle Valo /**
159f988d640SKalle Valo  * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
160f988d640SKalle Valo  * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
161f988d640SKalle Valo  *
162f988d640SKalle Valo  * @rate:	Input rate
163f988d640SKalle Valo  * returns:	Output Rate (0 if invalid)
164f988d640SKalle Valo  */
convert_mv_rate_to_radiotap(u8 rate)165f988d640SKalle Valo static u8 convert_mv_rate_to_radiotap(u8 rate)
166f988d640SKalle Valo {
167f988d640SKalle Valo 	switch (rate) {
168f988d640SKalle Valo 	case 0:		/*   1 Mbps */
169f988d640SKalle Valo 		return 2;
170f988d640SKalle Valo 	case 1:		/*   2 Mbps */
171f988d640SKalle Valo 		return 4;
172f988d640SKalle Valo 	case 2:		/* 5.5 Mbps */
173f988d640SKalle Valo 		return 11;
174f988d640SKalle Valo 	case 3:		/*  11 Mbps */
175f988d640SKalle Valo 		return 22;
176f988d640SKalle Valo 	/* case 4: reserved */
177f988d640SKalle Valo 	case 5:		/*   6 Mbps */
178f988d640SKalle Valo 		return 12;
179f988d640SKalle Valo 	case 6:		/*   9 Mbps */
180f988d640SKalle Valo 		return 18;
181f988d640SKalle Valo 	case 7:		/*  12 Mbps */
182f988d640SKalle Valo 		return 24;
183f988d640SKalle Valo 	case 8:		/*  18 Mbps */
184f988d640SKalle Valo 		return 36;
185f988d640SKalle Valo 	case 9:		/*  24 Mbps */
186f988d640SKalle Valo 		return 48;
187f988d640SKalle Valo 	case 10:		/*  36 Mbps */
188f988d640SKalle Valo 		return 72;
189f988d640SKalle Valo 	case 11:		/*  48 Mbps */
190f988d640SKalle Valo 		return 96;
191f988d640SKalle Valo 	case 12:		/*  54 Mbps */
192f988d640SKalle Valo 		return 108;
193f988d640SKalle Valo 	}
194f988d640SKalle Valo 	pr_alert("Invalid Marvell WLAN rate %i\n", rate);
195f988d640SKalle Valo 	return 0;
196f988d640SKalle Valo }
197f988d640SKalle Valo 
198f988d640SKalle Valo /**
199f988d640SKalle Valo  * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
200f988d640SKalle Valo  * it to kernel/upper layer
201f988d640SKalle Valo  *
202f988d640SKalle Valo  * @priv:	A pointer to &struct lbs_private
203f988d640SKalle Valo  * @skb:	A pointer to skb which includes the received packet
204f988d640SKalle Valo  * returns:	0 or -1
205f988d640SKalle Valo  */
process_rxed_802_11_packet(struct lbs_private * priv,struct sk_buff * skb)206f988d640SKalle Valo static int process_rxed_802_11_packet(struct lbs_private *priv,
207f988d640SKalle Valo 	struct sk_buff *skb)
208f988d640SKalle Valo {
209f988d640SKalle Valo 	int ret = 0;
210f988d640SKalle Valo 	struct net_device *dev = priv->dev;
211f988d640SKalle Valo 	struct rx80211packethdr *p_rx_pkt;
212f988d640SKalle Valo 	struct rxpd *prxpd;
213f988d640SKalle Valo 	struct rx_radiotap_hdr radiotap_hdr;
214f988d640SKalle Valo 	struct rx_radiotap_hdr *pradiotap_hdr;
215f988d640SKalle Valo 
216f988d640SKalle Valo 	p_rx_pkt = (struct rx80211packethdr *) skb->data;
217f988d640SKalle Valo 	prxpd = &p_rx_pkt->rx_pd;
218f988d640SKalle Valo 
219f988d640SKalle Valo 	/* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
220f988d640SKalle Valo 
221f988d640SKalle Valo 	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
222f988d640SKalle Valo 		lbs_deb_rx("rx err: frame received with bad length\n");
223f988d640SKalle Valo 		dev->stats.rx_length_errors++;
224f988d640SKalle Valo 		ret = -EINVAL;
225f988d640SKalle Valo 		kfree_skb(skb);
226f988d640SKalle Valo 		goto done;
227f988d640SKalle Valo 	}
228f988d640SKalle Valo 
229f988d640SKalle Valo 	lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
230f988d640SKalle Valo 	       skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
231f988d640SKalle Valo 
232f988d640SKalle Valo 	/* create the exported radio header */
233f988d640SKalle Valo 
234f988d640SKalle Valo 	/* radiotap header */
235f988d640SKalle Valo 	memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
236f988d640SKalle Valo 	/* XXX must check radiotap_hdr.hdr.it_pad for pad */
237f988d640SKalle Valo 	radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
238f988d640SKalle Valo 	radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
239f988d640SKalle Valo 	radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
240f988d640SKalle Valo 	/* XXX must check no carryout */
241f988d640SKalle Valo 	radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
242f988d640SKalle Valo 
243f988d640SKalle Valo 	/* chop the rxpd */
244f988d640SKalle Valo 	skb_pull(skb, sizeof(struct rxpd));
245f988d640SKalle Valo 
246f988d640SKalle Valo 	/* add space for the new radio header */
247f988d640SKalle Valo 	if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
248f988d640SKalle Valo 	    pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
249f988d640SKalle Valo 		netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
250f988d640SKalle Valo 		ret = -ENOMEM;
251f988d640SKalle Valo 		kfree_skb(skb);
252f988d640SKalle Valo 		goto done;
253f988d640SKalle Valo 	}
254f988d640SKalle Valo 
255d58ff351SJohannes Berg 	pradiotap_hdr = skb_push(skb, sizeof(struct rx_radiotap_hdr));
256f988d640SKalle Valo 	memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
257f988d640SKalle Valo 
258f988d640SKalle Valo 	priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
259f988d640SKalle Valo 
260f988d640SKalle Valo 	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
261f988d640SKalle Valo 	dev->stats.rx_bytes += skb->len;
262f988d640SKalle Valo 	dev->stats.rx_packets++;
263f988d640SKalle Valo 
264f988d640SKalle Valo 	skb->protocol = eth_type_trans(skb, priv->dev);
265*afb6d39fSSebastian Andrzej Siewior 	netif_rx(skb);
266f988d640SKalle Valo 
267f988d640SKalle Valo 	ret = 0;
268f988d640SKalle Valo 
269f988d640SKalle Valo done:
270f988d640SKalle Valo 	return ret;
271f988d640SKalle Valo }
272