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
->dev
= priv
->mesh_dev
;
146 skb
->dev
= priv
->dev
;
147 skb
->protocol
= eth_type_trans(skb
, priv
->dev
);
148 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
->debugmode
& MRVDRV_DEBUG_RX_PATH
)
177 lbs_dbg_hex("RX packet: ", skb
->data
,
178 min_t(unsigned int, skb
->len
, 100));
180 if (priv
->adapter
->linkmode
== WLAN_LINKMODE_802_11
)
181 return process_rxed_802_11_packet(priv
, skb
);
183 p_rx_pkt
= (struct rxpackethdr
*) skb
->data
;
184 p_rx_pd
= &p_rx_pkt
->rx_pd
;
185 if (p_rx_pd
->rx_control
& RxPD_MESH_FRAME
)
188 UNSET_MESH_FRAME(skb
);
190 lbs_dbg_hex("RX Data: Before chop rxpd", skb
->data
,
191 min_t(unsigned int, skb
->len
, 100));
193 if (skb
->len
< (ETH_HLEN
+ 8 + sizeof(struct rxpd
))) {
194 lbs_deb_rx("rx err: frame received with bad length\n");
195 priv
->stats
.rx_length_errors
++;
201 * Check rxpd status and update 802.3 stat,
203 if (!(p_rx_pd
->status
& cpu_to_le16(MRVDRV_RXPD_STATUS_OK
))) {
204 lbs_deb_rx("rx err: frame received with bad status\n");
205 lbs_pr_alert("rxpd not ok\n");
206 priv
->stats
.rx_errors
++;
211 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
212 skb
->len
, sizeof(struct rxpd
), skb
->len
- sizeof(struct rxpd
));
214 lbs_dbg_hex("RX Data: Dest", p_rx_pkt
->eth803_hdr
.dest_addr
,
215 sizeof(p_rx_pkt
->eth803_hdr
.dest_addr
));
216 lbs_dbg_hex("RX Data: Src", p_rx_pkt
->eth803_hdr
.src_addr
,
217 sizeof(p_rx_pkt
->eth803_hdr
.src_addr
));
219 if (memcmp(&p_rx_pkt
->rfc1042_hdr
,
220 rfc1042_eth_hdr
, sizeof(rfc1042_eth_hdr
)) == 0) {
222 * Replace the 803 header and rfc1042 header (llc/snap) with an
223 * EthernetII header, keep the src/dst and snap_type (ethertype)
225 * The firmware only passes up SNAP frames converting
226 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
228 * To create the Ethernet II, just move the src, dst address right
229 * before the snap_type.
231 p_ethhdr
= (struct ethhdr
*)
232 ((u8
*) & p_rx_pkt
->eth803_hdr
233 + sizeof(p_rx_pkt
->eth803_hdr
) + sizeof(p_rx_pkt
->rfc1042_hdr
)
234 - sizeof(p_rx_pkt
->eth803_hdr
.dest_addr
)
235 - sizeof(p_rx_pkt
->eth803_hdr
.src_addr
)
236 - sizeof(p_rx_pkt
->rfc1042_hdr
.snap_type
));
238 memcpy(p_ethhdr
->h_source
, p_rx_pkt
->eth803_hdr
.src_addr
,
239 sizeof(p_ethhdr
->h_source
));
240 memcpy(p_ethhdr
->h_dest
, p_rx_pkt
->eth803_hdr
.dest_addr
,
241 sizeof(p_ethhdr
->h_dest
));
243 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
246 hdrchop
= (u8
*) p_ethhdr
- (u8
*) p_rx_pkt
;
248 lbs_dbg_hex("RX Data: LLC/SNAP",
249 (u8
*) & p_rx_pkt
->rfc1042_hdr
,
250 sizeof(p_rx_pkt
->rfc1042_hdr
));
252 /* Chop off the rxpd */
253 hdrchop
= (u8
*) & p_rx_pkt
->eth803_hdr
- (u8
*) p_rx_pkt
;
256 /* Chop off the leading header bytes so the skb points to the start of
257 * either the reconstructed EthII frame or the 802.2/llc/snap frame
259 skb_pull(skb
, hdrchop
);
261 /* Take the data rate from the rxpd structure
262 * only if the rate is auto
264 if (adapter
->is_datarate_auto
)
265 adapter
->datarate
= libertas_index_to_data_rate(p_rx_pd
->rx_rate
);
267 wlan_compute_rssi(priv
, p_rx_pd
);
269 lbs_deb_rx("rx data: size of actual packet %d\n", skb
->len
);
270 priv
->stats
.rx_bytes
+= skb
->len
;
271 priv
->stats
.rx_packets
++;
273 libertas_upload_rx_packet(priv
, skb
);
277 lbs_deb_leave_args(LBS_DEB_RX
, "ret %d", ret
);
280 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet
);
283 * @brief This function converts Tx/Rx rates from the Marvell WLAN format
284 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
286 * @param rate Input rate
287 * @return Output Rate (0 if invalid)
289 static u8
convert_mv_rate_to_radiotap(u8 rate
)
296 case 2: /* 5.5 Mbps */
298 case 3: /* 11 Mbps */
304 case 6: /* 12 Mbps */
306 case 7: /* 18 Mbps */
308 case 8: /* 24 Mbps */
310 case 9: /* 36 Mbps */
312 case 10: /* 48 Mbps */
314 case 11: /* 54 Mbps */
317 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate
);
322 * @brief This function processes a received 802.11 packet and forwards it
323 * to kernel/upper layer
325 * @param priv A pointer to wlan_private
326 * @param skb A pointer to skb which includes the received packet
329 static int process_rxed_802_11_packet(wlan_private
* priv
, struct sk_buff
*skb
)
331 wlan_adapter
*adapter
= priv
->adapter
;
334 struct rx80211packethdr
*p_rx_pkt
;
336 struct rx_radiotap_hdr radiotap_hdr
;
337 struct rx_radiotap_hdr
*pradiotap_hdr
;
339 lbs_deb_enter(LBS_DEB_RX
);
341 p_rx_pkt
= (struct rx80211packethdr
*) skb
->data
;
342 prxpd
= &p_rx_pkt
->rx_pd
;
344 // lbs_dbg_hex("RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
346 if (skb
->len
< (ETH_HLEN
+ 8 + sizeof(struct rxpd
))) {
347 lbs_deb_rx("rx err: frame received wit bad length\n");
348 priv
->stats
.rx_length_errors
++;
354 * Check rxpd status and update 802.3 stat,
356 if (!(prxpd
->status
& cpu_to_le16(MRVDRV_RXPD_STATUS_OK
))) {
357 //lbs_deb_rx("rx err: frame received with bad status\n");
358 priv
->stats
.rx_errors
++;
361 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
362 skb
->len
, sizeof(struct rxpd
), skb
->len
- sizeof(struct rxpd
));
364 /* create the exported radio header */
365 switch (priv
->adapter
->radiomode
) {
366 case WLAN_RADIOMODE_NONE
:
367 /* no radio header */
369 skb_pull(skb
, sizeof(struct rxpd
));
372 case WLAN_RADIOMODE_RADIOTAP
:
373 /* radiotap header */
374 radiotap_hdr
.hdr
.it_version
= 0;
375 /* XXX must check this value for pad */
376 radiotap_hdr
.hdr
.it_pad
= 0;
377 radiotap_hdr
.hdr
.it_len
= sizeof(struct rx_radiotap_hdr
);
378 radiotap_hdr
.hdr
.it_present
= RX_RADIOTAP_PRESENT
;
380 radiotap_hdr
.flags
= 0;
381 radiotap_hdr
.chan_freq
= 0;
382 radiotap_hdr
.chan_flags
= 0;
383 radiotap_hdr
.antenna
= 0;
385 radiotap_hdr
.rate
= convert_mv_rate_to_radiotap(prxpd
->rx_rate
);
386 /* XXX must check no carryout */
387 radiotap_hdr
.antsignal
= prxpd
->snr
+ prxpd
->nf
;
388 radiotap_hdr
.rx_flags
= 0;
389 if (!(prxpd
->status
& cpu_to_le16(MRVDRV_RXPD_STATUS_OK
)))
390 radiotap_hdr
.rx_flags
|= IEEE80211_RADIOTAP_F_RX_BADFCS
;
391 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
393 // lbs_dbg_hex1("RX radiomode packet BEF: ", skb->data, min(skb->len, 100));
396 skb_pull(skb
, sizeof(struct rxpd
));
398 /* add space for the new radio header */
399 if ((skb_headroom(skb
) < sizeof(struct rx_radiotap_hdr
)) &&
400 pskb_expand_head(skb
, sizeof(struct rx_radiotap_hdr
), 0,
402 lbs_pr_alert("%s: couldn't pskb_expand_head\n",
407 (struct rx_radiotap_hdr
*)skb_push(skb
,
410 memcpy(pradiotap_hdr
, &radiotap_hdr
,
411 sizeof(struct rx_radiotap_hdr
));
412 //lbs_dbg_hex1("RX radiomode packet AFT: ", skb->data, min(skb->len, 100));
417 lbs_pr_alert("Unknown radiomode %i\n",
418 priv
->adapter
->radiomode
);
419 /* don't export any header */
421 skb_pull(skb
, sizeof(struct rxpd
));
425 /* Take the data rate from the rxpd structure
426 * only if the rate is auto
428 if (adapter
->is_datarate_auto
) {
429 adapter
->datarate
= libertas_index_to_data_rate(prxpd
->rx_rate
);
432 wlan_compute_rssi(priv
, prxpd
);
434 lbs_deb_rx("rx data: size of actual packet %d\n", skb
->len
);
435 priv
->stats
.rx_bytes
+= skb
->len
;
436 priv
->stats
.rx_packets
++;
438 libertas_upload_rx_packet(priv
, skb
);
443 skb
->protocol
= __constant_htons(0x0019); /* ETH_P_80211_RAW */
444 lbs_deb_leave_args(LBS_DEB_RX
, "ret %d", ret
);