libertas: kill struct lbs_adapter
[safe/jmp/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         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 void lbs_upload_rx_packet(struct lbs_private *priv, struct sk_buff *skb)
135 {
136         lbs_deb_rx("skb->data %p\n", skb->data);
137
138         if (priv->monitormode != LBS_MONITOR_OFF) {
139                 skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
140         } else {
141                 if (priv->mesh_dev && IS_MESH_FRAME(skb))
142                         skb->protocol = eth_type_trans(skb, priv->mesh_dev);
143                 else
144                         skb->protocol = eth_type_trans(skb, priv->dev);
145         }
146         skb->ip_summed = CHECKSUM_NONE;
147         netif_rx(skb);
148 }
149
150 /**
151  *  @brief This function processes received packet and forwards it
152  *  to kernel/upper layer
153  *
154  *  @param priv    A pointer to struct lbs_private
155  *  @param skb     A pointer to skb which includes the received packet
156  *  @return        0 or -1
157  */
158 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
159 {
160         int ret = 0;
161
162         struct rxpackethdr *p_rx_pkt;
163         struct rxpd *p_rx_pd;
164
165         int hdrchop;
166         struct ethhdr *p_ethhdr;
167
168         const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
169
170         lbs_deb_enter(LBS_DEB_RX);
171
172         if (priv->monitormode != LBS_MONITOR_OFF)
173                 return process_rxed_802_11_packet(priv, skb);
174
175         p_rx_pkt = (struct rxpackethdr *) skb->data;
176         p_rx_pd = &p_rx_pkt->rx_pd;
177         if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
178                 SET_MESH_FRAME(skb);
179         else
180                 UNSET_MESH_FRAME(skb);
181
182         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
183                  min_t(unsigned int, skb->len, 100));
184
185         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
186                 lbs_deb_rx("rx err: frame received with bad length\n");
187                 priv->stats.rx_length_errors++;
188                 ret = 0;
189                 goto done;
190         }
191
192         /*
193          * Check rxpd status and update 802.3 stat,
194          */
195         if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
196                 lbs_deb_rx("rx err: frame received with bad status\n");
197                 lbs_pr_alert("rxpd not ok\n");
198                 priv->stats.rx_errors++;
199                 ret = 0;
200                 goto done;
201         }
202
203         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
204                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
205
206         lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
207                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
208         lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
209                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
210
211         if (memcmp(&p_rx_pkt->rfc1042_hdr,
212                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
213                 /*
214                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
215                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
216                  *
217                  *  The firmware only passes up SNAP frames converting
218                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
219                  *
220                  *  To create the Ethernet II, just move the src, dst address right
221                  *    before the snap_type.
222                  */
223                 p_ethhdr = (struct ethhdr *)
224                     ((u8 *) & p_rx_pkt->eth803_hdr
225                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
226                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
227                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
228                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
229
230                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
231                        sizeof(p_ethhdr->h_source));
232                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
233                        sizeof(p_ethhdr->h_dest));
234
235                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
236                  *   that was removed
237                  */
238                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
239         } else {
240                 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
241                         (u8 *) & p_rx_pkt->rfc1042_hdr,
242                         sizeof(p_rx_pkt->rfc1042_hdr));
243
244                 /* Chop off the rxpd */
245                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
246         }
247
248         /* Chop off the leading header bytes so the skb points to the start of
249          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
250          */
251         skb_pull(skb, hdrchop);
252
253         /* Take the data rate from the rxpd structure
254          * only if the rate is auto
255          */
256         if (priv->auto_rate)
257                 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
258
259         lbs_compute_rssi(priv, p_rx_pd);
260
261         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
262         priv->stats.rx_bytes += skb->len;
263         priv->stats.rx_packets++;
264
265         lbs_upload_rx_packet(priv, skb);
266
267         ret = 0;
268 done:
269         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
270         return ret;
271 }
272 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
273
274 /**
275  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
276  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
277  *
278  *  @param rate    Input rate
279  *  @return        Output Rate (0 if invalid)
280  */
281 static u8 convert_mv_rate_to_radiotap(u8 rate)
282 {
283         switch (rate) {
284         case 0:         /*   1 Mbps */
285                 return 2;
286         case 1:         /*   2 Mbps */
287                 return 4;
288         case 2:         /* 5.5 Mbps */
289                 return 11;
290         case 3:         /*  11 Mbps */
291                 return 22;
292         /* case 4: reserved */
293         case 5:         /*   6 Mbps */
294                 return 12;
295         case 6:         /*   9 Mbps */
296                 return 18;
297         case 7:         /*  12 Mbps */
298                 return 24;
299         case 8:         /*  18 Mbps */
300                 return 36;
301         case 9:         /*  24 Mbps */
302                 return 48;
303         case 10:                /*  36 Mbps */
304                 return 72;
305         case 11:                /*  48 Mbps */
306                 return 96;
307         case 12:                /*  54 Mbps */
308                 return 108;
309         }
310         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
311         return 0;
312 }
313
314 /**
315  *  @brief This function processes a received 802.11 packet and forwards it
316  *  to kernel/upper layer
317  *
318  *  @param priv    A pointer to struct lbs_private
319  *  @param skb     A pointer to skb which includes the received packet
320  *  @return        0 or -1
321  */
322 static int process_rxed_802_11_packet(struct lbs_private *priv,
323         struct sk_buff *skb)
324 {
325         int ret = 0;
326
327         struct rx80211packethdr *p_rx_pkt;
328         struct rxpd *prxpd;
329         struct rx_radiotap_hdr radiotap_hdr;
330         struct rx_radiotap_hdr *pradiotap_hdr;
331
332         lbs_deb_enter(LBS_DEB_RX);
333
334         p_rx_pkt = (struct rx80211packethdr *) skb->data;
335         prxpd = &p_rx_pkt->rx_pd;
336
337         // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
338
339         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
340                 lbs_deb_rx("rx err: frame received wit bad length\n");
341                 priv->stats.rx_length_errors++;
342                 ret = 0;
343                 goto done;
344         }
345
346         /*
347          * Check rxpd status and update 802.3 stat,
348          */
349         if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
350                 //lbs_deb_rx("rx err: frame received with bad status\n");
351                 priv->stats.rx_errors++;
352         }
353
354         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
355                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
356
357         /* create the exported radio header */
358         if (priv->monitormode == LBS_MONITOR_OFF) {
359                 /* no radio header */
360                 /* chop the rxpd */
361                 skb_pull(skb, sizeof(struct rxpd));
362         }
363
364         else {
365                 /* radiotap header */
366                 radiotap_hdr.hdr.it_version = 0;
367                 /* XXX must check this value for pad */
368                 radiotap_hdr.hdr.it_pad = 0;
369                 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
370                 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
371                 /* unknown values */
372                 radiotap_hdr.flags = 0;
373                 radiotap_hdr.chan_freq = 0;
374                 radiotap_hdr.chan_flags = 0;
375                 radiotap_hdr.antenna = 0;
376                 /* known values */
377                 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
378                 /* XXX must check no carryout */
379                 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
380                 radiotap_hdr.rx_flags = 0;
381                 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
382                         radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
383                 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
384
385                 /* chop the rxpd */
386                 skb_pull(skb, sizeof(struct rxpd));
387
388                 /* add space for the new radio header */
389                 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
390                     pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
391                                      GFP_ATOMIC)) {
392                         lbs_pr_alert("%s: couldn't pskb_expand_head\n",
393                                __func__);
394                 }
395
396                 pradiotap_hdr =
397                     (struct rx_radiotap_hdr *)skb_push(skb,
398                                                      sizeof(struct
399                                                             rx_radiotap_hdr));
400                 memcpy(pradiotap_hdr, &radiotap_hdr,
401                        sizeof(struct rx_radiotap_hdr));
402         }
403
404         /* Take the data rate from the rxpd structure
405          * only if the rate is auto
406          */
407         if (priv->auto_rate)
408                 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
409
410         lbs_compute_rssi(priv, prxpd);
411
412         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
413         priv->stats.rx_bytes += skb->len;
414         priv->stats.rx_packets++;
415
416         lbs_upload_rx_packet(priv, skb);
417
418         ret = 0;
419
420 done:
421         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
422         return ret;
423 }