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->adapter->monitormode != WLAN_MONITOR_OFF) {
142 skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
144 if (priv->mesh_dev && IS_MESH_FRAME(skb))
145 skb->protocol = eth_type_trans(skb, priv->mesh_dev);
147 skb->protocol = eth_type_trans(skb, priv->dev);
149 skb->ip_summed = CHECKSUM_UNNECESSARY;
154 * @brief This function processes received packet and forwards it
155 * to kernel/upper layer
157 * @param priv A pointer to wlan_private
158 * @param skb A pointer to skb which includes the received packet
161 int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
163 wlan_adapter *adapter = priv->adapter;
166 struct rxpackethdr *p_rx_pkt;
167 struct rxpd *p_rx_pd;
170 struct ethhdr *p_ethhdr;
172 const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
174 lbs_deb_enter(LBS_DEB_RX);
176 if (priv->adapter->monitormode != WLAN_MONITOR_OFF)
177 return process_rxed_802_11_packet(priv, skb);
179 p_rx_pkt = (struct rxpackethdr *) skb->data;
180 p_rx_pd = &p_rx_pkt->rx_pd;
181 if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
184 UNSET_MESH_FRAME(skb);
186 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
187 min_t(unsigned int, skb->len, 100));
189 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
190 lbs_deb_rx("rx err: frame received with bad length\n");
191 priv->stats.rx_length_errors++;
197 * Check rxpd status and update 802.3 stat,
199 if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
200 lbs_deb_rx("rx err: frame received with bad status\n");
201 lbs_pr_alert("rxpd not ok\n");
202 priv->stats.rx_errors++;
207 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
208 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
210 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
211 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
212 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
213 sizeof(p_rx_pkt->eth803_hdr.src_addr));
215 if (memcmp(&p_rx_pkt->rfc1042_hdr,
216 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
218 * Replace the 803 header and rfc1042 header (llc/snap) with an
219 * EthernetII header, keep the src/dst and snap_type (ethertype)
221 * The firmware only passes up SNAP frames converting
222 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
224 * To create the Ethernet II, just move the src, dst address right
225 * before the snap_type.
227 p_ethhdr = (struct ethhdr *)
228 ((u8 *) & p_rx_pkt->eth803_hdr
229 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
230 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
231 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
232 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
234 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
235 sizeof(p_ethhdr->h_source));
236 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
237 sizeof(p_ethhdr->h_dest));
239 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
242 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
244 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
245 (u8 *) & p_rx_pkt->rfc1042_hdr,
246 sizeof(p_rx_pkt->rfc1042_hdr));
248 /* Chop off the rxpd */
249 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
252 /* Chop off the leading header bytes so the skb points to the start of
253 * either the reconstructed EthII frame or the 802.2/llc/snap frame
255 skb_pull(skb, hdrchop);
257 /* Take the data rate from the rxpd structure
258 * only if the rate is auto
260 if (adapter->auto_rate)
261 adapter->cur_rate = libertas_fw_index_to_data_rate(p_rx_pd->rx_rate);
263 wlan_compute_rssi(priv, p_rx_pd);
265 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
266 priv->stats.rx_bytes += skb->len;
267 priv->stats.rx_packets++;
269 libertas_upload_rx_packet(priv, skb);
273 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
276 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
279 * @brief This function converts Tx/Rx rates from the Marvell WLAN format
280 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
282 * @param rate Input rate
283 * @return Output Rate (0 if invalid)
285 static u8 convert_mv_rate_to_radiotap(u8 rate)
292 case 2: /* 5.5 Mbps */
294 case 3: /* 11 Mbps */
296 /* case 4: reserved */
301 case 7: /* 12 Mbps */
303 case 8: /* 18 Mbps */
305 case 9: /* 24 Mbps */
307 case 10: /* 36 Mbps */
309 case 11: /* 48 Mbps */
311 case 12: /* 54 Mbps */
314 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
319 * @brief This function processes a received 802.11 packet and forwards it
320 * to kernel/upper layer
322 * @param priv A pointer to wlan_private
323 * @param skb A pointer to skb which includes the received packet
326 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
328 wlan_adapter *adapter = priv->adapter;
331 struct rx80211packethdr *p_rx_pkt;
333 struct rx_radiotap_hdr radiotap_hdr;
334 struct rx_radiotap_hdr *pradiotap_hdr;
336 lbs_deb_enter(LBS_DEB_RX);
338 p_rx_pkt = (struct rx80211packethdr *) skb->data;
339 prxpd = &p_rx_pkt->rx_pd;
341 // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
343 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
344 lbs_deb_rx("rx err: frame received wit bad length\n");
345 priv->stats.rx_length_errors++;
351 * Check rxpd status and update 802.3 stat,
353 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
354 //lbs_deb_rx("rx err: frame received with bad status\n");
355 priv->stats.rx_errors++;
358 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
359 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
361 /* create the exported radio header */
362 if(priv->adapter->monitormode == WLAN_MONITOR_OFF) {
363 /* no radio header */
365 skb_pull(skb, sizeof(struct rxpd));
369 /* radiotap header */
370 radiotap_hdr.hdr.it_version = 0;
371 /* XXX must check this value for pad */
372 radiotap_hdr.hdr.it_pad = 0;
373 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
374 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
376 radiotap_hdr.flags = 0;
377 radiotap_hdr.chan_freq = 0;
378 radiotap_hdr.chan_flags = 0;
379 radiotap_hdr.antenna = 0;
381 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
382 /* XXX must check no carryout */
383 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
384 radiotap_hdr.rx_flags = 0;
385 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
386 radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
387 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
390 skb_pull(skb, sizeof(struct rxpd));
392 /* add space for the new radio header */
393 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
394 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
396 lbs_pr_alert("%s: couldn't pskb_expand_head\n",
401 (struct rx_radiotap_hdr *)skb_push(skb,
404 memcpy(pradiotap_hdr, &radiotap_hdr,
405 sizeof(struct rx_radiotap_hdr));
408 /* Take the data rate from the rxpd structure
409 * only if the rate is auto
411 if (adapter->auto_rate)
412 adapter->cur_rate = libertas_fw_index_to_data_rate(prxpd->rx_rate);
414 wlan_compute_rssi(priv, prxpd);
416 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
417 priv->stats.rx_bytes += skb->len;
418 priv->stats.rx_packets++;
420 libertas_upload_rx_packet(priv, skb);
425 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);