2 * This file contains the handling of RX in wlan driver.
4 #include <linux/etherdevice.h>
5 #include <linux/types.h>
17 } __attribute__ ((packed));
25 } __attribute__ ((packed));
29 struct eth803hdr eth803_hdr;
30 struct rfc1042hdr rfc1042_hdr;
31 } __attribute__ ((packed));
33 struct rx80211packethdr {
36 } __attribute__ ((packed));
38 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb);
41 * @brief This function computes the avgSNR .
43 * @param priv A pointer to wlan_private structure
46 static u8 wlan_getavgsnr(wlan_private * priv)
50 wlan_adapter *adapter = priv->adapter;
51 if (adapter->numSNRNF == 0)
53 for (i = 0; i < adapter->numSNRNF; i++)
54 temp += adapter->rawSNR[i];
55 return (u8) (temp / adapter->numSNRNF);
60 * @brief This function computes the AvgNF
62 * @param priv A pointer to wlan_private structure
65 static u8 wlan_getavgnf(wlan_private * priv)
69 wlan_adapter *adapter = priv->adapter;
70 if (adapter->numSNRNF == 0)
72 for (i = 0; i < adapter->numSNRNF; i++)
73 temp += adapter->rawNF[i];
74 return (u8) (temp / adapter->numSNRNF);
79 * @brief This function save the raw SNR/NF to our internel buffer
81 * @param priv A pointer to wlan_private structure
82 * @param prxpd A pointer to rxpd structure of received packet
85 static void wlan_save_rawSNRNF(wlan_private * priv, struct rxpd *p_rx_pd)
87 wlan_adapter *adapter = priv->adapter;
88 if (adapter->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
90 adapter->rawSNR[adapter->nextSNRNF] = p_rx_pd->snr;
91 adapter->rawNF[adapter->nextSNRNF] = p_rx_pd->nf;
93 if (adapter->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
94 adapter->nextSNRNF = 0;
99 * @brief This function computes the RSSI in received packet.
101 * @param priv A pointer to wlan_private structure
102 * @param prxpd A pointer to rxpd structure of received packet
105 static void wlan_compute_rssi(wlan_private * priv, struct rxpd *p_rx_pd)
107 wlan_adapter *adapter = priv->adapter;
109 lbs_deb_enter(LBS_DEB_RX);
111 lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
112 lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
113 adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
114 adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
116 adapter->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
117 adapter->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
118 wlan_save_rawSNRNF(priv, p_rx_pd);
120 adapter->SNR[TYPE_RXPD][TYPE_AVG] = wlan_getavgsnr(priv) * AVG_SCALE;
121 adapter->NF[TYPE_RXPD][TYPE_AVG] = wlan_getavgnf(priv) * AVG_SCALE;
122 lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
123 adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
124 adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
126 adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
127 CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
128 adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
130 adapter->RSSI[TYPE_RXPD][TYPE_AVG] =
131 CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
132 adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
134 lbs_deb_leave(LBS_DEB_RX);
137 void libertas_upload_rx_packet(wlan_private * priv, struct sk_buff *skb)
139 lbs_deb_rx("skb->data %p\n", skb->data);
141 if (priv->mesh_dev && IS_MESH_FRAME(skb))
142 skb->protocol = eth_type_trans(skb, priv->mesh_dev);
144 skb->protocol = eth_type_trans(skb, priv->dev);
145 skb->ip_summed = CHECKSUM_UNNECESSARY;
151 * @brief This function processes received packet and forwards it
152 * to kernel/upper layer
154 * @param priv A pointer to wlan_private
155 * @param skb A pointer to skb which includes the received packet
158 int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
160 wlan_adapter *adapter = priv->adapter;
163 struct rxpackethdr *p_rx_pkt;
164 struct rxpd *p_rx_pd;
167 struct ethhdr *p_ethhdr;
169 const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
171 lbs_deb_enter(LBS_DEB_RX);
173 if (priv->adapter->linkmode == WLAN_LINKMODE_802_11)
174 return process_rxed_802_11_packet(priv, skb);
176 p_rx_pkt = (struct rxpackethdr *) skb->data;
177 p_rx_pd = &p_rx_pkt->rx_pd;
178 if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
181 UNSET_MESH_FRAME(skb);
183 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
184 min_t(unsigned int, skb->len, 100));
186 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
187 lbs_deb_rx("rx err: frame received with bad length\n");
188 priv->stats.rx_length_errors++;
194 * Check rxpd status and update 802.3 stat,
196 if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
197 lbs_deb_rx("rx err: frame received with bad status\n");
198 lbs_pr_alert("rxpd not ok\n");
199 priv->stats.rx_errors++;
204 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
205 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
207 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
208 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
209 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
210 sizeof(p_rx_pkt->eth803_hdr.src_addr));
212 if (memcmp(&p_rx_pkt->rfc1042_hdr,
213 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
215 * Replace the 803 header and rfc1042 header (llc/snap) with an
216 * EthernetII header, keep the src/dst and snap_type (ethertype)
218 * The firmware only passes up SNAP frames converting
219 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
221 * To create the Ethernet II, just move the src, dst address right
222 * before the snap_type.
224 p_ethhdr = (struct ethhdr *)
225 ((u8 *) & p_rx_pkt->eth803_hdr
226 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
227 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
228 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
229 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
231 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
232 sizeof(p_ethhdr->h_source));
233 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
234 sizeof(p_ethhdr->h_dest));
236 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
239 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
241 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
242 (u8 *) & p_rx_pkt->rfc1042_hdr,
243 sizeof(p_rx_pkt->rfc1042_hdr));
245 /* Chop off the rxpd */
246 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
249 /* Chop off the leading header bytes so the skb points to the start of
250 * either the reconstructed EthII frame or the 802.2/llc/snap frame
252 skb_pull(skb, hdrchop);
254 /* Take the data rate from the rxpd structure
255 * only if the rate is auto
257 if (adapter->auto_rate)
258 adapter->cur_rate = libertas_fw_index_to_data_rate(p_rx_pd->rx_rate);
260 wlan_compute_rssi(priv, p_rx_pd);
262 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
263 priv->stats.rx_bytes += skb->len;
264 priv->stats.rx_packets++;
266 libertas_upload_rx_packet(priv, skb);
270 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
273 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
276 * @brief This function converts Tx/Rx rates from the Marvell WLAN format
277 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
279 * @param rate Input rate
280 * @return Output Rate (0 if invalid)
282 static u8 convert_mv_rate_to_radiotap(u8 rate)
289 case 2: /* 5.5 Mbps */
291 case 3: /* 11 Mbps */
297 case 6: /* 12 Mbps */
299 case 7: /* 18 Mbps */
301 case 8: /* 24 Mbps */
303 case 9: /* 36 Mbps */
305 case 10: /* 48 Mbps */
307 case 11: /* 54 Mbps */
310 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
315 * @brief This function processes a received 802.11 packet and forwards it
316 * to kernel/upper layer
318 * @param priv A pointer to wlan_private
319 * @param skb A pointer to skb which includes the received packet
322 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
324 wlan_adapter *adapter = priv->adapter;
327 struct rx80211packethdr *p_rx_pkt;
329 struct rx_radiotap_hdr radiotap_hdr;
330 struct rx_radiotap_hdr *pradiotap_hdr;
332 lbs_deb_enter(LBS_DEB_RX);
334 p_rx_pkt = (struct rx80211packethdr *) skb->data;
335 prxpd = &p_rx_pkt->rx_pd;
337 // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
339 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
340 lbs_deb_rx("rx err: frame received wit bad length\n");
341 priv->stats.rx_length_errors++;
347 * Check rxpd status and update 802.3 stat,
349 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
350 //lbs_deb_rx("rx err: frame received with bad status\n");
351 priv->stats.rx_errors++;
354 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
355 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
357 /* create the exported radio header */
358 switch (priv->adapter->radiomode) {
359 case WLAN_RADIOMODE_NONE:
360 /* no radio header */
362 skb_pull(skb, sizeof(struct rxpd));
365 case WLAN_RADIOMODE_RADIOTAP:
366 /* radiotap header */
367 radiotap_hdr.hdr.it_version = 0;
368 /* XXX must check this value for pad */
369 radiotap_hdr.hdr.it_pad = 0;
370 radiotap_hdr.hdr.it_len = sizeof(struct rx_radiotap_hdr);
371 radiotap_hdr.hdr.it_present = RX_RADIOTAP_PRESENT;
373 radiotap_hdr.flags = 0;
374 radiotap_hdr.chan_freq = 0;
375 radiotap_hdr.chan_flags = 0;
376 radiotap_hdr.antenna = 0;
378 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
379 /* XXX must check no carryout */
380 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
381 radiotap_hdr.rx_flags = 0;
382 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
383 radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
384 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
387 skb_pull(skb, sizeof(struct rxpd));
389 /* add space for the new radio header */
390 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
391 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
393 lbs_pr_alert("%s: couldn't pskb_expand_head\n",
398 (struct rx_radiotap_hdr *)skb_push(skb,
401 memcpy(pradiotap_hdr, &radiotap_hdr,
402 sizeof(struct rx_radiotap_hdr));
407 lbs_pr_alert("Unknown radiomode %i\n",
408 priv->adapter->radiomode);
409 /* don't export any header */
411 skb_pull(skb, sizeof(struct rxpd));
415 /* Take the data rate from the rxpd structure
416 * only if the rate is auto
418 if (adapter->auto_rate)
419 adapter->cur_rate = libertas_fw_index_to_data_rate(prxpd->rx_rate);
421 wlan_compute_rssi(priv, prxpd);
423 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
424 priv->stats.rx_bytes += skb->len;
425 priv->stats.rx_packets++;
427 libertas_upload_rx_packet(priv, skb);
432 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);