rx.c 10.0 KB
Newer Older
1 2 3 4 5 6
/**
  * This file contains the handling of RX in wlan driver.
  */
#include <linux/etherdevice.h>
#include <linux/types.h>

7
#include "host.h"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "radiotap.h"
#include "decl.h"
#include "dev.h"
#include "wext.h"

struct eth803hdr {
	u8 dest_addr[6];
	u8 src_addr[6];
	u16 h803_len;
} __attribute__ ((packed));

struct rfc1042hdr {
	u8 llc_dsap;
	u8 llc_ssap;
	u8 llc_ctrl;
	u8 snap_oui[3];
	u16 snap_type;
} __attribute__ ((packed));

struct rxpackethdr {
	struct eth803hdr eth803_hdr;
	struct rfc1042hdr rfc1042_hdr;
} __attribute__ ((packed));

struct rx80211packethdr {
	struct rxpd rx_pd;
	void *eth80211_hdr;
} __attribute__ ((packed));

37 38
static int process_rxed_802_11_packet(struct lbs_private *priv,
	struct sk_buff *skb);
39 40

/**
41
 *  @brief	This function computes the avgSNR .
42
 *
43 44
 *  @param	priv	A pointer to struct lbs_private structure
 *  @return	avgSNR
45
 */
46
static u8 lbs_getavgsnr(struct lbs_private *priv)
47 48 49
{
	u8 i;
	u16 temp = 0;
50
	if (priv->numSNRNF == 0)
51
		return 0;
52 53 54
	for (i = 0; i < priv->numSNRNF; i++)
		temp += priv->rawSNR[i];
	return (u8) (temp / priv->numSNRNF);
55 56 57 58

}

/**
59
 *  @brief	This function computes the AvgNF
60
 *
61 62
 *  @param	priv	A pointer to struct lbs_private structure
 *  @return	AvgNF
63
 */
64
static u8 lbs_getavgnf(struct lbs_private *priv)
65 66 67
{
	u8 i;
	u16 temp = 0;
68
	if (priv->numSNRNF == 0)
69
		return 0;
70 71 72
	for (i = 0; i < priv->numSNRNF; i++)
		temp += priv->rawNF[i];
	return (u8) (temp / priv->numSNRNF);
73 74 75 76

}

/**
77
 *  @brief	This function save the raw SNR/NF to our internel buffer
78
 *
79 80 81
 *  @param	priv	A pointer to struct lbs_private structure
 *  @param	prxpd	A pointer to rxpd structure of received packet
 *  @return	n/a
82
 */
83
static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
84
{
85 86 87 88 89 90 91
	if (priv->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
		priv->numSNRNF++;
	priv->rawSNR[priv->nextSNRNF] = p_rx_pd->snr;
	priv->rawNF[priv->nextSNRNF] = p_rx_pd->nf;
	priv->nextSNRNF++;
	if (priv->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
		priv->nextSNRNF = 0;
92 93 94 95
	return;
}

/**
96
 *  @brief	This function computes the RSSI in received packet.
97
 *
98 99 100
 *  @param	priv	A pointer to struct lbs_private structure
 *  @param	prxpd	A pointer to rxpd structure of received packet
 *  @return	n/a
101
 */
102
static void lbs_compute_rssi(struct lbs_private *priv, struct rxpd *p_rx_pd)
103 104
{

105
	lbs_deb_enter(LBS_DEB_RX);
106

107 108
	lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
	lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
109 110
	       priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
	       priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
111

112 113
	priv->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
	priv->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
114
	lbs_save_rawSNRNF(priv, p_rx_pd);
115

116 117
	priv->SNR[TYPE_RXPD][TYPE_AVG] = lbs_getavgsnr(priv) * AVG_SCALE;
	priv->NF[TYPE_RXPD][TYPE_AVG] = lbs_getavgnf(priv) * AVG_SCALE;
118
	lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
119 120
	       priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
	       priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
121

122 123 124
	priv->RSSI[TYPE_RXPD][TYPE_NOAVG] =
	    CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_NOAVG],
		     priv->NF[TYPE_RXPD][TYPE_NOAVG]);
125

126 127 128
	priv->RSSI[TYPE_RXPD][TYPE_AVG] =
	    CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
		     priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
129

130
	lbs_deb_leave(LBS_DEB_RX);
131 132 133 134 135 136
}

/**
 *  @brief This function processes received packet and forwards it
 *  to kernel/upper layer
 *
137 138 139
 *  @param	priv	A pointer to struct lbs_private
 *  @param	skb		A pointer to skb which includes the received packet
 *  @return	0 or -1
140
 */
141
int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
142 143
{
	int ret = 0;
144
	struct net_device *dev = priv->dev;
145 146 147 148 149 150
	struct rxpackethdr *p_rx_pkt;
	struct rxpd *p_rx_pd;
	int hdrchop;
	struct ethhdr *p_ethhdr;
	const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };

151
	lbs_deb_enter(LBS_DEB_RX);
152

153 154
	BUG_ON(!skb);

155 156
	skb->ip_summed = CHECKSUM_NONE;

157
	if (priv->monitormode)
158 159
		return process_rxed_802_11_packet(priv, skb);

160 161 162
	p_rx_pd = (struct rxpd *) skb->data;
	p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
		le32_to_cpu(p_rx_pd->pkt_ptr));
163 164

	dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
165

166
	lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
167 168 169
		 min_t(unsigned int, skb->len, 100));

	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
170
		lbs_deb_rx("rx err: frame received with bad length\n");
171
		dev->stats.rx_length_errors++;
172
		ret = 0;
173
		dev_kfree_skb(skb);
174 175 176
		goto done;
	}

177
	lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
178 179
		skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
		skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
180

181
	lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
182
		sizeof(p_rx_pkt->eth803_hdr.dest_addr));
183
	lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		sizeof(p_rx_pkt->eth803_hdr.src_addr));

	if (memcmp(&p_rx_pkt->rfc1042_hdr,
		   rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
		/*
		 *  Replace the 803 header and rfc1042 header (llc/snap) with an
		 *    EthernetII header, keep the src/dst and snap_type (ethertype)
		 *
		 *  The firmware only passes up SNAP frames converting
		 *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
		 *
		 *  To create the Ethernet II, just move the src, dst address right
		 *    before the snap_type.
		 */
		p_ethhdr = (struct ethhdr *)
199
		    ((u8 *) &p_rx_pkt->eth803_hdr
200 201 202 203 204 205 206 207 208 209 210 211 212
		     + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
		     - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
		     - sizeof(p_rx_pkt->eth803_hdr.src_addr)
		     - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));

		memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
		       sizeof(p_ethhdr->h_source));
		memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
		       sizeof(p_ethhdr->h_dest));

		/* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
		 *   that was removed
		 */
213
		hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
214
	} else {
215
		lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
216
			(u8 *) &p_rx_pkt->rfc1042_hdr,
217 218 219
			sizeof(p_rx_pkt->rfc1042_hdr));

		/* Chop off the rxpd */
220
		hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
221 222 223 224 225 226 227 228 229 230
	}

	/* Chop off the leading header bytes so the skb points to the start of
	 *   either the reconstructed EthII frame or the 802.2/llc/snap frame
	 */
	skb_pull(skb, hdrchop);

	/* Take the data rate from the rxpd structure
	 * only if the rate is auto
	 */
231
	if (priv->enablehwauto)
232
		priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
233

234
	lbs_compute_rssi(priv, p_rx_pd);
235

236
	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
237 238
	dev->stats.rx_bytes += skb->len;
	dev->stats.rx_packets++;
239

240
	skb->protocol = eth_type_trans(skb, dev);
241 242 243 244
	if (in_interrupt())
		netif_rx(skb);
	else
		netif_rx_ni(skb);
245

246 247
	ret = 0;
done:
248
	lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
249 250
	return ret;
}
251
EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
252 253 254 255 256

/**
 *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
 *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
 *
257 258
 *  @param	rate	Input rate
 *  @return	Output Rate (0 if invalid)
259 260 261 262 263 264 265 266 267 268 269 270
 */
static u8 convert_mv_rate_to_radiotap(u8 rate)
{
	switch (rate) {
	case 0:		/*   1 Mbps */
		return 2;
	case 1:		/*   2 Mbps */
		return 4;
	case 2:		/* 5.5 Mbps */
		return 11;
	case 3:		/*  11 Mbps */
		return 22;
271 272
	/* case 4: reserved */
	case 5:		/*   6 Mbps */
273
		return 12;
274
	case 6:		/*   9 Mbps */
275
		return 18;
276
	case 7:		/*  12 Mbps */
277
		return 24;
278
	case 8:		/*  18 Mbps */
279
		return 36;
280
	case 9:		/*  24 Mbps */
281
		return 48;
282
	case 10:		/*  36 Mbps */
283
		return 72;
284
	case 11:		/*  48 Mbps */
285
		return 96;
286
	case 12:		/*  54 Mbps */
287 288
		return 108;
	}
289
	lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
290 291 292 293 294 295 296
	return 0;
}

/**
 *  @brief This function processes a received 802.11 packet and forwards it
 *  to kernel/upper layer
 *
297 298 299
 *  @param	priv	A pointer to struct lbs_private
 *  @param	skb		A pointer to skb which includes the received packet
 *  @return	0 or -1
300
 */
301 302
static int process_rxed_802_11_packet(struct lbs_private *priv,
	struct sk_buff *skb)
303 304
{
	int ret = 0;
305
	struct net_device *dev = priv->dev;
306 307 308 309 310
	struct rx80211packethdr *p_rx_pkt;
	struct rxpd *prxpd;
	struct rx_radiotap_hdr radiotap_hdr;
	struct rx_radiotap_hdr *pradiotap_hdr;

311
	lbs_deb_enter(LBS_DEB_RX);
312 313 314 315

	p_rx_pkt = (struct rx80211packethdr *) skb->data;
	prxpd = &p_rx_pkt->rx_pd;

316
	/* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
317 318

	if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
319
		lbs_deb_rx("rx err: frame received with bad length\n");
320
		dev->stats.rx_length_errors++;
321
		ret = -EINVAL;
322
		kfree_skb(skb);
323 324 325
		goto done;
	}

326
	lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
327 328 329 330
	       skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));

	/* create the exported radio header */

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	/* radiotap header */
	radiotap_hdr.hdr.it_version = 0;
	/* XXX must check this value for pad */
	radiotap_hdr.hdr.it_pad = 0;
	radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
	radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
	radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
	/* XXX must check no carryout */
	radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;

	/* chop the rxpd */
	skb_pull(skb, sizeof(struct rxpd));

	/* add space for the new radio header */
	if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
346 347 348 349 350
	    pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
		lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
		ret = -ENOMEM;
		kfree_skb(skb);
		goto done;
351 352
	}

353 354 355
	pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
	memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));

356 357 358
	/* Take the data rate from the rxpd structure
	 * only if the rate is auto
	 */
359
	if (priv->enablehwauto)
360
		priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
361

362
	lbs_compute_rssi(priv, prxpd);
363

364
	lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
365 366
	dev->stats.rx_bytes += skb->len;
	dev->stats.rx_packets++;
367

368 369
	skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
	netif_rx(skb);
370

371 372
	ret = 0;

373 374 375
done:
	lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
	return ret;
376
}