]> err.no Git - linux-2.6/blob - drivers/net/wireless/libertas/rx.c
fa467dfef003fc992c134f3cd762083be3dc3610
[linux-2.6] / drivers / net / wireless / libertas / rx.c
1 /**
2   * This file contains the handling of RX in wlan driver.
3   */
4 #include <linux/etherdevice.h>
5 #include <linux/types.h>
6
7 #include "hostcmd.h"
8 #include "radiotap.h"
9 #include "decl.h"
10 #include "dev.h"
11 #include "wext.h"
12
13 struct eth803hdr {
14         u8 dest_addr[6];
15         u8 src_addr[6];
16         u16 h803_len;
17 } __attribute__ ((packed));
18
19 struct rfc1042hdr {
20         u8 llc_dsap;
21         u8 llc_ssap;
22         u8 llc_ctrl;
23         u8 snap_oui[3];
24         u16 snap_type;
25 } __attribute__ ((packed));
26
27 struct rxpackethdr {
28         struct rxpd rx_pd;
29         struct eth803hdr eth803_hdr;
30         struct rfc1042hdr rfc1042_hdr;
31 } __attribute__ ((packed));
32
33 struct rx80211packethdr {
34         struct rxpd rx_pd;
35         void *eth80211_hdr;
36 } __attribute__ ((packed));
37
38 static int process_rxed_802_11_packet(struct lbs_private *priv,
39         struct sk_buff *skb);
40
41 /**
42  *  @brief This function computes the avgSNR .
43  *
44  *  @param priv    A pointer to struct lbs_private structure
45  *  @return        avgSNR
46  */
47 static u8 lbs_getavgsnr(struct lbs_private *priv)
48 {
49         u8 i;
50         u16 temp = 0;
51         struct lbs_adapter *adapter = priv->adapter;
52         if (adapter->numSNRNF == 0)
53                 return 0;
54         for (i = 0; i < adapter->numSNRNF; i++)
55                 temp += adapter->rawSNR[i];
56         return (u8) (temp / adapter->numSNRNF);
57
58 }
59
60 /**
61  *  @brief This function computes the AvgNF
62  *
63  *  @param priv    A pointer to struct lbs_private structure
64  *  @return        AvgNF
65  */
66 static u8 lbs_getavgnf(struct lbs_private *priv)
67 {
68         u8 i;
69         u16 temp = 0;
70         struct lbs_adapter *adapter = priv->adapter;
71         if (adapter->numSNRNF == 0)
72                 return 0;
73         for (i = 0; i < adapter->numSNRNF; i++)
74                 temp += adapter->rawNF[i];
75         return (u8) (temp / adapter->numSNRNF);
76
77 }
78
79 /**
80  *  @brief This function save the raw SNR/NF to our internel buffer
81  *
82  *  @param priv    A pointer to struct lbs_private structure
83  *  @param prxpd   A pointer to rxpd structure of received packet
84  *  @return        n/a
85  */
86 static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
87 {
88         struct lbs_adapter *adapter = priv->adapter;
89         if (adapter->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
90                 adapter->numSNRNF++;
91         adapter->rawSNR[adapter->nextSNRNF] = p_rx_pd->snr;
92         adapter->rawNF[adapter->nextSNRNF] = p_rx_pd->nf;
93         adapter->nextSNRNF++;
94         if (adapter->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
95                 adapter->nextSNRNF = 0;
96         return;
97 }
98
99 /**
100  *  @brief This function computes the RSSI in received packet.
101  *
102  *  @param priv    A pointer to struct lbs_private structure
103  *  @param prxpd   A pointer to rxpd structure of received packet
104  *  @return        n/a
105  */
106 static void lbs_compute_rssi(struct lbs_private *priv, struct rxpd *p_rx_pd)
107 {
108         struct lbs_adapter *adapter = priv->adapter;
109
110         lbs_deb_enter(LBS_DEB_RX);
111
112         lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
113         lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
114                adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
115                adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
116
117         adapter->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
118         adapter->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
119         lbs_save_rawSNRNF(priv, p_rx_pd);
120
121         adapter->SNR[TYPE_RXPD][TYPE_AVG] = lbs_getavgsnr(priv) * AVG_SCALE;
122         adapter->NF[TYPE_RXPD][TYPE_AVG] = lbs_getavgnf(priv) * AVG_SCALE;
123         lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
124                adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
125                adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
126
127         adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
128             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
129                      adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
130
131         adapter->RSSI[TYPE_RXPD][TYPE_AVG] =
132             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
133                      adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
134
135         lbs_deb_leave(LBS_DEB_RX);
136 }
137
138 void lbs_upload_rx_packet(struct lbs_private *priv, struct sk_buff *skb)
139 {
140         lbs_deb_rx("skb->data %p\n", skb->data);
141
142         if (priv->adapter->monitormode != LBS_MONITOR_OFF) {
143                 skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
144         } else {
145                 if (priv->mesh_dev && IS_MESH_FRAME(skb))
146                         skb->protocol = eth_type_trans(skb, priv->mesh_dev);
147                 else
148                         skb->protocol = eth_type_trans(skb, priv->dev);
149         }
150         skb->ip_summed = CHECKSUM_UNNECESSARY;
151         netif_rx(skb);
152 }
153
154 /**
155  *  @brief This function processes received packet and forwards it
156  *  to kernel/upper layer
157  *
158  *  @param priv    A pointer to struct lbs_private
159  *  @param skb     A pointer to skb which includes the received packet
160  *  @return        0 or -1
161  */
162 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
163 {
164         struct lbs_adapter *adapter = priv->adapter;
165         int ret = 0;
166
167         struct rxpackethdr *p_rx_pkt;
168         struct rxpd *p_rx_pd;
169
170         int hdrchop;
171         struct ethhdr *p_ethhdr;
172
173         const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
174
175         lbs_deb_enter(LBS_DEB_RX);
176
177         if (priv->adapter->monitormode != LBS_MONITOR_OFF)
178                 return process_rxed_802_11_packet(priv, skb);
179
180         p_rx_pkt = (struct rxpackethdr *) skb->data;
181         p_rx_pd = &p_rx_pkt->rx_pd;
182         if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
183                 SET_MESH_FRAME(skb);
184         else
185                 UNSET_MESH_FRAME(skb);
186
187         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
188                  min_t(unsigned int, skb->len, 100));
189
190         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
191                 lbs_deb_rx("rx err: frame received with bad length\n");
192                 priv->stats.rx_length_errors++;
193                 ret = 0;
194                 goto done;
195         }
196
197         /*
198          * Check rxpd status and update 802.3 stat,
199          */
200         if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
201                 lbs_deb_rx("rx err: frame received with bad status\n");
202                 lbs_pr_alert("rxpd not ok\n");
203                 priv->stats.rx_errors++;
204                 ret = 0;
205                 goto done;
206         }
207
208         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
209                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
210
211         lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
212                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
213         lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
214                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
215
216         if (memcmp(&p_rx_pkt->rfc1042_hdr,
217                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
218                 /*
219                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
220                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
221                  *
222                  *  The firmware only passes up SNAP frames converting
223                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
224                  *
225                  *  To create the Ethernet II, just move the src, dst address right
226                  *    before the snap_type.
227                  */
228                 p_ethhdr = (struct ethhdr *)
229                     ((u8 *) & p_rx_pkt->eth803_hdr
230                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
231                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
232                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
233                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
234
235                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
236                        sizeof(p_ethhdr->h_source));
237                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
238                        sizeof(p_ethhdr->h_dest));
239
240                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
241                  *   that was removed
242                  */
243                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
244         } else {
245                 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
246                         (u8 *) & p_rx_pkt->rfc1042_hdr,
247                         sizeof(p_rx_pkt->rfc1042_hdr));
248
249                 /* Chop off the rxpd */
250                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
251         }
252
253         /* Chop off the leading header bytes so the skb points to the start of
254          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
255          */
256         skb_pull(skb, hdrchop);
257
258         /* Take the data rate from the rxpd structure
259          * only if the rate is auto
260          */
261         if (adapter->auto_rate)
262                 adapter->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
263
264         lbs_compute_rssi(priv, p_rx_pd);
265
266         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
267         priv->stats.rx_bytes += skb->len;
268         priv->stats.rx_packets++;
269
270         lbs_upload_rx_packet(priv, skb);
271
272         ret = 0;
273 done:
274         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
275         return ret;
276 }
277 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
278
279 /**
280  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
281  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
282  *
283  *  @param rate    Input rate
284  *  @return        Output Rate (0 if invalid)
285  */
286 static u8 convert_mv_rate_to_radiotap(u8 rate)
287 {
288         switch (rate) {
289         case 0:         /*   1 Mbps */
290                 return 2;
291         case 1:         /*   2 Mbps */
292                 return 4;
293         case 2:         /* 5.5 Mbps */
294                 return 11;
295         case 3:         /*  11 Mbps */
296                 return 22;
297         /* case 4: reserved */
298         case 5:         /*   6 Mbps */
299                 return 12;
300         case 6:         /*   9 Mbps */
301                 return 18;
302         case 7:         /*  12 Mbps */
303                 return 24;
304         case 8:         /*  18 Mbps */
305                 return 36;
306         case 9:         /*  24 Mbps */
307                 return 48;
308         case 10:                /*  36 Mbps */
309                 return 72;
310         case 11:                /*  48 Mbps */
311                 return 96;
312         case 12:                /*  54 Mbps */
313                 return 108;
314         }
315         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
316         return 0;
317 }
318
319 /**
320  *  @brief This function processes a received 802.11 packet and forwards it
321  *  to kernel/upper layer
322  *
323  *  @param priv    A pointer to struct lbs_private
324  *  @param skb     A pointer to skb which includes the received packet
325  *  @return        0 or -1
326  */
327 static int process_rxed_802_11_packet(struct lbs_private *priv,
328         struct sk_buff *skb)
329 {
330         struct lbs_adapter *adapter = priv->adapter;
331         int ret = 0;
332
333         struct rx80211packethdr *p_rx_pkt;
334         struct rxpd *prxpd;
335         struct rx_radiotap_hdr radiotap_hdr;
336         struct rx_radiotap_hdr *pradiotap_hdr;
337
338         lbs_deb_enter(LBS_DEB_RX);
339
340         p_rx_pkt = (struct rx80211packethdr *) skb->data;
341         prxpd = &p_rx_pkt->rx_pd;
342
343         // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
344
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++;
348                 ret = 0;
349                 goto done;
350         }
351
352         /*
353          * Check rxpd status and update 802.3 stat,
354          */
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++;
358         }
359
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));
362
363         /* create the exported radio header */
364         if (priv->adapter->monitormode == LBS_MONITOR_OFF) {
365                 /* no radio header */
366                 /* chop the rxpd */
367                 skb_pull(skb, sizeof(struct rxpd));
368         }
369
370         else {
371                 /* radiotap header */
372                 radiotap_hdr.hdr.it_version = 0;
373                 /* XXX must check this value for pad */
374                 radiotap_hdr.hdr.it_pad = 0;
375                 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
376                 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
377                 /* unknown values */
378                 radiotap_hdr.flags = 0;
379                 radiotap_hdr.chan_freq = 0;
380                 radiotap_hdr.chan_flags = 0;
381                 radiotap_hdr.antenna = 0;
382                 /* known values */
383                 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
384                 /* XXX must check no carryout */
385                 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
386                 radiotap_hdr.rx_flags = 0;
387                 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
388                         radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
389                 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
390
391                 /* chop the rxpd */
392                 skb_pull(skb, sizeof(struct rxpd));
393
394                 /* add space for the new radio header */
395                 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
396                     pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
397                                      GFP_ATOMIC)) {
398                         lbs_pr_alert("%s: couldn't pskb_expand_head\n",
399                                __func__);
400                 }
401
402                 pradiotap_hdr =
403                     (struct rx_radiotap_hdr *)skb_push(skb,
404                                                      sizeof(struct
405                                                             rx_radiotap_hdr));
406                 memcpy(pradiotap_hdr, &radiotap_hdr,
407                        sizeof(struct rx_radiotap_hdr));
408         }
409
410         /* Take the data rate from the rxpd structure
411          * only if the rate is auto
412          */
413         if (adapter->auto_rate)
414                 adapter->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
415
416         lbs_compute_rssi(priv, prxpd);
417
418         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
419         priv->stats.rx_bytes += skb->len;
420         priv->stats.rx_packets++;
421
422         lbs_upload_rx_packet(priv, skb);
423
424         ret = 0;
425
426 done:
427         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
428         return ret;
429 }