libertas: support mesh for various firmware versions
[pandora-kernel.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/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         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_pkt = (struct rxpackethdr *) skb->data;
162         p_rx_pd = &p_rx_pkt->rx_pd;
163         if (priv->mesh_dev) {
164                 if (priv->mesh_fw_ver == MESH_FW_OLD) {
165                         if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
166                                 dev = priv->mesh_dev;
167                 } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
168                         if (p_rx_pd->u.bss.bss_num == MESH_IFACE_ID)
169                                 dev = priv->mesh_dev;
170                 }
171         }
172
173         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
174                  min_t(unsigned int, skb->len, 100));
175
176         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
177                 lbs_deb_rx("rx err: frame received with bad length\n");
178                 dev->stats.rx_length_errors++;
179                 ret = 0;
180                 dev_kfree_skb(skb);
181                 goto done;
182         }
183
184         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
185                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
186
187         lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
188                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
189         lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
190                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
191
192         if (memcmp(&p_rx_pkt->rfc1042_hdr,
193                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
194                 /*
195                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
196                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
197                  *
198                  *  The firmware only passes up SNAP frames converting
199                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
200                  *
201                  *  To create the Ethernet II, just move the src, dst address right
202                  *    before the snap_type.
203                  */
204                 p_ethhdr = (struct ethhdr *)
205                     ((u8 *) & p_rx_pkt->eth803_hdr
206                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
207                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
208                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
209                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
210
211                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
212                        sizeof(p_ethhdr->h_source));
213                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
214                        sizeof(p_ethhdr->h_dest));
215
216                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
217                  *   that was removed
218                  */
219                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
220         } else {
221                 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
222                         (u8 *) & p_rx_pkt->rfc1042_hdr,
223                         sizeof(p_rx_pkt->rfc1042_hdr));
224
225                 /* Chop off the rxpd */
226                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
227         }
228
229         /* Chop off the leading header bytes so the skb points to the start of
230          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
231          */
232         skb_pull(skb, hdrchop);
233
234         /* Take the data rate from the rxpd structure
235          * only if the rate is auto
236          */
237         if (priv->enablehwauto)
238                 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
239
240         lbs_compute_rssi(priv, p_rx_pd);
241
242         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
243         dev->stats.rx_bytes += skb->len;
244         dev->stats.rx_packets++;
245
246         skb->protocol = eth_type_trans(skb, dev);
247         if (in_interrupt())
248                 netif_rx(skb);
249         else
250                 netif_rx_ni(skb);
251
252         ret = 0;
253 done:
254         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
255         return ret;
256 }
257 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
258
259 /**
260  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
261  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
262  *
263  *  @param rate    Input rate
264  *  @return        Output Rate (0 if invalid)
265  */
266 static u8 convert_mv_rate_to_radiotap(u8 rate)
267 {
268         switch (rate) {
269         case 0:         /*   1 Mbps */
270                 return 2;
271         case 1:         /*   2 Mbps */
272                 return 4;
273         case 2:         /* 5.5 Mbps */
274                 return 11;
275         case 3:         /*  11 Mbps */
276                 return 22;
277         /* case 4: reserved */
278         case 5:         /*   6 Mbps */
279                 return 12;
280         case 6:         /*   9 Mbps */
281                 return 18;
282         case 7:         /*  12 Mbps */
283                 return 24;
284         case 8:         /*  18 Mbps */
285                 return 36;
286         case 9:         /*  24 Mbps */
287                 return 48;
288         case 10:                /*  36 Mbps */
289                 return 72;
290         case 11:                /*  48 Mbps */
291                 return 96;
292         case 12:                /*  54 Mbps */
293                 return 108;
294         }
295         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
296         return 0;
297 }
298
299 /**
300  *  @brief This function processes a received 802.11 packet and forwards it
301  *  to kernel/upper layer
302  *
303  *  @param priv    A pointer to struct lbs_private
304  *  @param skb     A pointer to skb which includes the received packet
305  *  @return        0 or -1
306  */
307 static int process_rxed_802_11_packet(struct lbs_private *priv,
308         struct sk_buff *skb)
309 {
310         int ret = 0;
311         struct net_device *dev = priv->dev;
312         struct rx80211packethdr *p_rx_pkt;
313         struct rxpd *prxpd;
314         struct rx_radiotap_hdr radiotap_hdr;
315         struct rx_radiotap_hdr *pradiotap_hdr;
316
317         lbs_deb_enter(LBS_DEB_RX);
318
319         p_rx_pkt = (struct rx80211packethdr *) skb->data;
320         prxpd = &p_rx_pkt->rx_pd;
321
322         // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
323
324         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
325                 lbs_deb_rx("rx err: frame received with bad length\n");
326                 dev->stats.rx_length_errors++;
327                 ret = -EINVAL;
328                 kfree_skb(skb);
329                 goto done;
330         }
331
332         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
333                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
334
335         /* create the exported radio header */
336
337         /* radiotap header */
338         radiotap_hdr.hdr.it_version = 0;
339         /* XXX must check this value for pad */
340         radiotap_hdr.hdr.it_pad = 0;
341         radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
342         radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
343         radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
344         /* XXX must check no carryout */
345         radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
346
347         /* chop the rxpd */
348         skb_pull(skb, sizeof(struct rxpd));
349
350         /* add space for the new radio header */
351         if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
352             pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
353                 lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
354                 ret = -ENOMEM;
355                 kfree_skb(skb);
356                 goto done;
357         }
358
359         pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
360         memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
361
362         /* Take the data rate from the rxpd structure
363          * only if the rate is auto
364          */
365         if (priv->enablehwauto)
366                 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
367
368         lbs_compute_rssi(priv, prxpd);
369
370         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
371         dev->stats.rx_bytes += skb->len;
372         dev->stats.rx_packets++;
373
374         skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
375         netif_rx(skb);
376
377         ret = 0;
378
379 done:
380         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
381         return ret;
382 }