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 < adapter->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 >= adapter->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->rxpd_rate = p_rx_pd->rx_rate;
122 adapter->SNR[TYPE_RXPD][TYPE_AVG] = wlan_getavgsnr(priv) * AVG_SCALE;
123 adapter->NF[TYPE_RXPD][TYPE_AVG] = wlan_getavgnf(priv) * AVG_SCALE;
124 lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
125 adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
126 adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
128 adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
129 CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
130 adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
132 adapter->RSSI[TYPE_RXPD][TYPE_AVG] =
133 CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
134 adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
136 lbs_deb_leave(LBS_DEB_RX);
139 void libertas_upload_rx_packet(wlan_private * priv, struct sk_buff *skb)
141 lbs_deb_rx("skb->data %p\n", skb->data);
143 if (priv->mesh_dev && IS_MESH_FRAME(skb))
144 skb->protocol = eth_type_trans(skb, priv->mesh_dev);
146 skb->protocol = eth_type_trans(skb, priv->dev);
147 skb->ip_summed = CHECKSUM_UNNECESSARY;
153 * @brief This function processes received packet and forwards it
154 * to kernel/upper layer
156 * @param priv A pointer to wlan_private
157 * @param skb A pointer to skb which includes the received packet
160 int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
162 wlan_adapter *adapter = priv->adapter;
165 struct rxpackethdr *p_rx_pkt;
166 struct rxpd *p_rx_pd;
169 struct ethhdr *p_ethhdr;
171 const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
173 lbs_deb_enter(LBS_DEB_RX);
175 if (priv->adapter->debugmode & MRVDRV_DEBUG_RX_PATH)
176 lbs_dbg_hex("RX packet: ", skb->data,
177 min_t(unsigned int, skb->len, 100));
179 if (priv->adapter->linkmode == WLAN_LINKMODE_802_11)
180 return process_rxed_802_11_packet(priv, skb);
182 p_rx_pkt = (struct rxpackethdr *) skb->data;
183 p_rx_pd = &p_rx_pkt->rx_pd;
184 if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
187 UNSET_MESH_FRAME(skb);
189 lbs_dbg_hex("RX Data: Before chop rxpd", skb->data,
190 min_t(unsigned int, skb->len, 100));
192 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
193 lbs_deb_rx("rx err: frame received with bad length\n");
194 priv->stats.rx_length_errors++;
200 * Check rxpd status and update 802.3 stat,
202 if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
203 lbs_deb_rx("rx err: frame received with bad status\n");
204 lbs_pr_alert("rxpd not ok\n");
205 priv->stats.rx_errors++;
210 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
211 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
213 lbs_dbg_hex("RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
214 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
215 lbs_dbg_hex("RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
216 sizeof(p_rx_pkt->eth803_hdr.src_addr));
218 if (memcmp(&p_rx_pkt->rfc1042_hdr,
219 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
221 * Replace the 803 header and rfc1042 header (llc/snap) with an
222 * EthernetII header, keep the src/dst and snap_type (ethertype)
224 * The firmware only passes up SNAP frames converting
225 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
227 * To create the Ethernet II, just move the src, dst address right
228 * before the snap_type.
230 p_ethhdr = (struct ethhdr *)
231 ((u8 *) & p_rx_pkt->eth803_hdr
232 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
233 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
234 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
235 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
237 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
238 sizeof(p_ethhdr->h_source));
239 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
240 sizeof(p_ethhdr->h_dest));
242 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
245 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
247 lbs_dbg_hex("RX Data: LLC/SNAP",
248 (u8 *) & p_rx_pkt->rfc1042_hdr,
249 sizeof(p_rx_pkt->rfc1042_hdr));
251 /* Chop off the rxpd */
252 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
255 /* Chop off the leading header bytes so the skb points to the start of
256 * either the reconstructed EthII frame or the 802.2/llc/snap frame
258 skb_pull(skb, hdrchop);
260 /* Take the data rate from the rxpd structure
261 * only if the rate is auto
263 if (adapter->is_datarate_auto)
264 adapter->datarate = libertas_index_to_data_rate(p_rx_pd->rx_rate);
266 wlan_compute_rssi(priv, p_rx_pd);
268 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
269 priv->stats.rx_bytes += skb->len;
270 priv->stats.rx_packets++;
272 libertas_upload_rx_packet(priv, skb);
276 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
279 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
282 * @brief This function converts Tx/Rx rates from the Marvell WLAN format
283 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
285 * @param rate Input rate
286 * @return Output Rate (0 if invalid)
288 static u8 convert_mv_rate_to_radiotap(u8 rate)
295 case 2: /* 5.5 Mbps */
297 case 3: /* 11 Mbps */
303 case 6: /* 12 Mbps */
305 case 7: /* 18 Mbps */
307 case 8: /* 24 Mbps */
309 case 9: /* 36 Mbps */
311 case 10: /* 48 Mbps */
313 case 11: /* 54 Mbps */
316 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
321 * @brief This function processes a received 802.11 packet and forwards it
322 * to kernel/upper layer
324 * @param priv A pointer to wlan_private
325 * @param skb A pointer to skb which includes the received packet
328 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
330 wlan_adapter *adapter = priv->adapter;
333 struct rx80211packethdr *p_rx_pkt;
335 struct rx_radiotap_hdr radiotap_hdr;
336 struct rx_radiotap_hdr *pradiotap_hdr;
338 lbs_deb_enter(LBS_DEB_RX);
340 p_rx_pkt = (struct rx80211packethdr *) skb->data;
341 prxpd = &p_rx_pkt->rx_pd;
343 // lbs_dbg_hex("RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
345 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
346 lbs_deb_rx("rx err: frame received wit bad length\n");
347 priv->stats.rx_length_errors++;
353 * Check rxpd status and update 802.3 stat,
355 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
356 //lbs_deb_rx("rx err: frame received with bad status\n");
357 priv->stats.rx_errors++;
360 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
361 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
363 /* create the exported radio header */
364 switch (priv->adapter->radiomode) {
365 case WLAN_RADIOMODE_NONE:
366 /* no radio header */
368 skb_pull(skb, sizeof(struct rxpd));
371 case WLAN_RADIOMODE_RADIOTAP:
372 /* radiotap header */
373 radiotap_hdr.hdr.it_version = 0;
374 /* XXX must check this value for pad */
375 radiotap_hdr.hdr.it_pad = 0;
376 radiotap_hdr.hdr.it_len = sizeof(struct rx_radiotap_hdr);
377 radiotap_hdr.hdr.it_present = RX_RADIOTAP_PRESENT;
379 radiotap_hdr.flags = 0;
380 radiotap_hdr.chan_freq = 0;
381 radiotap_hdr.chan_flags = 0;
382 radiotap_hdr.antenna = 0;
384 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
385 /* XXX must check no carryout */
386 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
387 radiotap_hdr.rx_flags = 0;
388 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
389 radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
390 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
392 // lbs_dbg_hex1("RX radiomode packet BEF: ", skb->data, min(skb->len, 100));
395 skb_pull(skb, sizeof(struct rxpd));
397 /* add space for the new radio header */
398 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
399 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
401 lbs_pr_alert("%s: couldn't pskb_expand_head\n",
406 (struct rx_radiotap_hdr *)skb_push(skb,
409 memcpy(pradiotap_hdr, &radiotap_hdr,
410 sizeof(struct rx_radiotap_hdr));
411 //lbs_dbg_hex1("RX radiomode packet AFT: ", skb->data, min(skb->len, 100));
416 lbs_pr_alert("Unknown radiomode %i\n",
417 priv->adapter->radiomode);
418 /* don't export any header */
420 skb_pull(skb, sizeof(struct rxpd));
424 /* Take the data rate from the rxpd structure
425 * only if the rate is auto
427 if (adapter->is_datarate_auto) {
428 adapter->datarate = libertas_index_to_data_rate(prxpd->rx_rate);
431 wlan_compute_rssi(priv, prxpd);
433 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
434 priv->stats.rx_bytes += skb->len;
435 priv->stats.rx_packets++;
437 libertas_upload_rx_packet(priv, skb);
442 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);