Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[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(wlan_private * priv, struct sk_buff *skb);
39
40 /**
41  *  @brief This function computes the avgSNR .
42  *
43  *  @param priv    A pointer to wlan_private structure
44  *  @return        avgSNR
45  */
46 static u8 wlan_getavgsnr(wlan_private * priv)
47 {
48         u8 i;
49         u16 temp = 0;
50         wlan_adapter *adapter = priv->adapter;
51         if (adapter->numSNRNF == 0)
52                 return 0;
53         for (i = 0; i < adapter->numSNRNF; i++)
54                 temp += adapter->rawSNR[i];
55         return (u8) (temp / adapter->numSNRNF);
56
57 }
58
59 /**
60  *  @brief This function computes the AvgNF
61  *
62  *  @param priv    A pointer to wlan_private structure
63  *  @return        AvgNF
64  */
65 static u8 wlan_getavgnf(wlan_private * priv)
66 {
67         u8 i;
68         u16 temp = 0;
69         wlan_adapter *adapter = priv->adapter;
70         if (adapter->numSNRNF == 0)
71                 return 0;
72         for (i = 0; i < adapter->numSNRNF; i++)
73                 temp += adapter->rawNF[i];
74         return (u8) (temp / adapter->numSNRNF);
75
76 }
77
78 /**
79  *  @brief This function save the raw SNR/NF to our internel buffer
80  *
81  *  @param priv    A pointer to wlan_private structure
82  *  @param prxpd   A pointer to rxpd structure of received packet
83  *  @return        n/a
84  */
85 static void wlan_save_rawSNRNF(wlan_private * priv, struct rxpd *p_rx_pd)
86 {
87         wlan_adapter *adapter = priv->adapter;
88         if (adapter->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
89                 adapter->numSNRNF++;
90         adapter->rawSNR[adapter->nextSNRNF] = p_rx_pd->snr;
91         adapter->rawNF[adapter->nextSNRNF] = p_rx_pd->nf;
92         adapter->nextSNRNF++;
93         if (adapter->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
94                 adapter->nextSNRNF = 0;
95         return;
96 }
97
98 /**
99  *  @brief This function computes the RSSI in received packet.
100  *
101  *  @param priv    A pointer to wlan_private structure
102  *  @param prxpd   A pointer to rxpd structure of received packet
103  *  @return        n/a
104  */
105 static void wlan_compute_rssi(wlan_private * priv, struct rxpd *p_rx_pd)
106 {
107         wlan_adapter *adapter = priv->adapter;
108
109         lbs_deb_enter(LBS_DEB_RX);
110
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);
115
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);
119
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);
125
126         adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
127             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
128                      adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
129
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);
133
134         lbs_deb_leave(LBS_DEB_RX);
135 }
136
137 void libertas_upload_rx_packet(wlan_private * priv, struct sk_buff *skb)
138 {
139         lbs_deb_rx("skb->data %p\n", skb->data);
140
141         if (priv->adapter->monitormode != WLAN_MONITOR_OFF) {
142                 skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
143         } else {
144                 if (priv->mesh_dev && IS_MESH_FRAME(skb))
145                         skb->protocol = eth_type_trans(skb, priv->mesh_dev);
146                 else
147                         skb->protocol = eth_type_trans(skb, priv->dev);
148         }
149         skb->ip_summed = CHECKSUM_UNNECESSARY;
150         netif_rx(skb);
151 }
152
153 /**
154  *  @brief This function processes received packet and forwards it
155  *  to kernel/upper layer
156  *
157  *  @param priv    A pointer to wlan_private
158  *  @param skb     A pointer to skb which includes the received packet
159  *  @return        0 or -1
160  */
161 int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
162 {
163         wlan_adapter *adapter = priv->adapter;
164         int ret = 0;
165
166         struct rxpackethdr *p_rx_pkt;
167         struct rxpd *p_rx_pd;
168
169         int hdrchop;
170         struct ethhdr *p_ethhdr;
171
172         const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
173
174         lbs_deb_enter(LBS_DEB_RX);
175
176         if (priv->adapter->monitormode != WLAN_MONITOR_OFF)
177                 return process_rxed_802_11_packet(priv, skb);
178
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)
182                 SET_MESH_FRAME(skb);
183         else
184                 UNSET_MESH_FRAME(skb);
185
186         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
187                  min_t(unsigned int, skb->len, 100));
188
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++;
192                 ret = 0;
193                 goto done;
194         }
195
196         /*
197          * Check rxpd status and update 802.3 stat,
198          */
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++;
203                 ret = 0;
204                 goto done;
205         }
206
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));
209
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));
214
215         if (memcmp(&p_rx_pkt->rfc1042_hdr,
216                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
217                 /*
218                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
219                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
220                  *
221                  *  The firmware only passes up SNAP frames converting
222                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
223                  *
224                  *  To create the Ethernet II, just move the src, dst address right
225                  *    before the snap_type.
226                  */
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));
233
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));
238
239                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
240                  *   that was removed
241                  */
242                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
243         } else {
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));
247
248                 /* Chop off the rxpd */
249                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
250         }
251
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
254          */
255         skb_pull(skb, hdrchop);
256
257         /* Take the data rate from the rxpd structure
258          * only if the rate is auto
259          */
260         if (adapter->auto_rate)
261                 adapter->cur_rate = libertas_fw_index_to_data_rate(p_rx_pd->rx_rate);
262
263         wlan_compute_rssi(priv, p_rx_pd);
264
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++;
268
269         libertas_upload_rx_packet(priv, skb);
270
271         ret = 0;
272 done:
273         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
274         return ret;
275 }
276 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
277
278 /**
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)
281  *
282  *  @param rate    Input rate
283  *  @return        Output Rate (0 if invalid)
284  */
285 static u8 convert_mv_rate_to_radiotap(u8 rate)
286 {
287         switch (rate) {
288         case 0:         /*   1 Mbps */
289                 return 2;
290         case 1:         /*   2 Mbps */
291                 return 4;
292         case 2:         /* 5.5 Mbps */
293                 return 11;
294         case 3:         /*  11 Mbps */
295                 return 22;
296         /* case 4: reserved */
297         case 5:         /*   6 Mbps */
298                 return 12;
299         case 6:         /*   9 Mbps */
300                 return 18;
301         case 7:         /*  12 Mbps */
302                 return 24;
303         case 8:         /*  18 Mbps */
304                 return 36;
305         case 9:         /*  24 Mbps */
306                 return 48;
307         case 10:                /*  36 Mbps */
308                 return 72;
309         case 11:                /*  48 Mbps */
310                 return 96;
311         case 12:                /*  54 Mbps */
312                 return 108;
313         }
314         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
315         return 0;
316 }
317
318 /**
319  *  @brief This function processes a received 802.11 packet and forwards it
320  *  to kernel/upper layer
321  *
322  *  @param priv    A pointer to wlan_private
323  *  @param skb     A pointer to skb which includes the received packet
324  *  @return        0 or -1
325  */
326 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
327 {
328         wlan_adapter *adapter = priv->adapter;
329         int ret = 0;
330
331         struct rx80211packethdr *p_rx_pkt;
332         struct rxpd *prxpd;
333         struct rx_radiotap_hdr radiotap_hdr;
334         struct rx_radiotap_hdr *pradiotap_hdr;
335
336         lbs_deb_enter(LBS_DEB_RX);
337
338         p_rx_pkt = (struct rx80211packethdr *) skb->data;
339         prxpd = &p_rx_pkt->rx_pd;
340
341         // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
342
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++;
346                 ret = 0;
347                 goto done;
348         }
349
350         /*
351          * Check rxpd status and update 802.3 stat,
352          */
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++;
356         }
357
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));
360
361         /* create the exported radio header */
362         if(priv->adapter->monitormode == WLAN_MONITOR_OFF) {
363                 /* no radio header */
364                 /* chop the rxpd */
365                 skb_pull(skb, sizeof(struct rxpd));
366         }
367
368         else {
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);
375                 /* unknown values */
376                 radiotap_hdr.flags = 0;
377                 radiotap_hdr.chan_freq = 0;
378                 radiotap_hdr.chan_flags = 0;
379                 radiotap_hdr.antenna = 0;
380                 /* known values */
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);
388
389                 /* chop the rxpd */
390                 skb_pull(skb, sizeof(struct rxpd));
391
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,
395                                      GFP_ATOMIC)) {
396                         lbs_pr_alert("%s: couldn't pskb_expand_head\n",
397                                __func__);
398                 }
399
400                 pradiotap_hdr =
401                     (struct rx_radiotap_hdr *)skb_push(skb,
402                                                      sizeof(struct
403                                                             rx_radiotap_hdr));
404                 memcpy(pradiotap_hdr, &radiotap_hdr,
405                        sizeof(struct rx_radiotap_hdr));
406         }
407
408         /* Take the data rate from the rxpd structure
409          * only if the rate is auto
410          */
411         if (adapter->auto_rate)
412                 adapter->cur_rate = libertas_fw_index_to_data_rate(prxpd->rx_rate);
413
414         wlan_compute_rssi(priv, prxpd);
415
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++;
419
420         libertas_upload_rx_packet(priv, skb);
421
422         ret = 0;
423
424 done:
425         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
426         return ret;
427 }