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