[PATCH] libertas: remove adapter->listeninterval
[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->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         skb->ip_summed = CHECKSUM_UNNECESSARY;
146
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 wlan_private
155  *  @param skb     A pointer to skb which includes the received packet
156  *  @return        0 or -1
157  */
158 int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
159 {
160         wlan_adapter *adapter = priv->adapter;
161         int ret = 0;
162
163         struct rxpackethdr *p_rx_pkt;
164         struct rxpd *p_rx_pd;
165
166         int hdrchop;
167         struct ethhdr *p_ethhdr;
168
169         const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
170
171         lbs_deb_enter(LBS_DEB_RX);
172
173         if (priv->adapter->linkmode == WLAN_LINKMODE_802_11)
174                 return process_rxed_802_11_packet(priv, skb);
175
176         p_rx_pkt = (struct rxpackethdr *) skb->data;
177         p_rx_pd = &p_rx_pkt->rx_pd;
178         if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
179                 SET_MESH_FRAME(skb);
180         else
181                 UNSET_MESH_FRAME(skb);
182
183         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
184                  min_t(unsigned int, skb->len, 100));
185
186         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
187                 lbs_deb_rx("rx err: frame received with bad length\n");
188                 priv->stats.rx_length_errors++;
189                 ret = 0;
190                 goto done;
191         }
192
193         /*
194          * Check rxpd status and update 802.3 stat,
195          */
196         if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
197                 lbs_deb_rx("rx err: frame received with bad status\n");
198                 lbs_pr_alert("rxpd not ok\n");
199                 priv->stats.rx_errors++;
200                 ret = 0;
201                 goto done;
202         }
203
204         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
205                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
206
207         lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
208                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
209         lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
210                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
211
212         if (memcmp(&p_rx_pkt->rfc1042_hdr,
213                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
214                 /*
215                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
216                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
217                  *
218                  *  The firmware only passes up SNAP frames converting
219                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
220                  *
221                  *  To create the Ethernet II, just move the src, dst address right
222                  *    before the snap_type.
223                  */
224                 p_ethhdr = (struct ethhdr *)
225                     ((u8 *) & p_rx_pkt->eth803_hdr
226                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
227                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
228                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
229                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
230
231                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
232                        sizeof(p_ethhdr->h_source));
233                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
234                        sizeof(p_ethhdr->h_dest));
235
236                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
237                  *   that was removed
238                  */
239                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
240         } else {
241                 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
242                         (u8 *) & p_rx_pkt->rfc1042_hdr,
243                         sizeof(p_rx_pkt->rfc1042_hdr));
244
245                 /* Chop off the rxpd */
246                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
247         }
248
249         /* Chop off the leading header bytes so the skb points to the start of
250          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
251          */
252         skb_pull(skb, hdrchop);
253
254         /* Take the data rate from the rxpd structure
255          * only if the rate is auto
256          */
257         if (adapter->auto_rate)
258                 adapter->cur_rate = libertas_fw_index_to_data_rate(p_rx_pd->rx_rate);
259
260         wlan_compute_rssi(priv, p_rx_pd);
261
262         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
263         priv->stats.rx_bytes += skb->len;
264         priv->stats.rx_packets++;
265
266         libertas_upload_rx_packet(priv, skb);
267
268         ret = 0;
269 done:
270         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
271         return ret;
272 }
273 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
274
275 /**
276  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
277  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
278  *
279  *  @param rate    Input rate
280  *  @return        Output Rate (0 if invalid)
281  */
282 static u8 convert_mv_rate_to_radiotap(u8 rate)
283 {
284         switch (rate) {
285         case 0:         /*   1 Mbps */
286                 return 2;
287         case 1:         /*   2 Mbps */
288                 return 4;
289         case 2:         /* 5.5 Mbps */
290                 return 11;
291         case 3:         /*  11 Mbps */
292                 return 22;
293         case 4:         /*   6 Mbps */
294                 return 12;
295         case 5:         /*   9 Mbps */
296                 return 18;
297         case 6:         /*  12 Mbps */
298                 return 24;
299         case 7:         /*  18 Mbps */
300                 return 36;
301         case 8:         /*  24 Mbps */
302                 return 48;
303         case 9:         /*  36 Mbps */
304                 return 72;
305         case 10:                /*  48 Mbps */
306                 return 96;
307         case 11:                /*  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 wlan_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(wlan_private * priv, struct sk_buff *skb)
323 {
324         wlan_adapter *adapter = priv->adapter;
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         switch (priv->adapter->radiomode) {
359         case WLAN_RADIOMODE_NONE:
360                 /* no radio header */
361                 /* chop the rxpd */
362                 skb_pull(skb, sizeof(struct rxpd));
363                 break;
364
365         case WLAN_RADIOMODE_RADIOTAP:
366                 /* radiotap header */
367                 radiotap_hdr.hdr.it_version = 0;
368                 /* XXX must check this value for pad */
369                 radiotap_hdr.hdr.it_pad = 0;
370                 radiotap_hdr.hdr.it_len = sizeof(struct rx_radiotap_hdr);
371                 radiotap_hdr.hdr.it_present = RX_RADIOTAP_PRESENT;
372                 /* unknown values */
373                 radiotap_hdr.flags = 0;
374                 radiotap_hdr.chan_freq = 0;
375                 radiotap_hdr.chan_flags = 0;
376                 radiotap_hdr.antenna = 0;
377                 /* known values */
378                 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
379                 /* XXX must check no carryout */
380                 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
381                 radiotap_hdr.rx_flags = 0;
382                 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
383                         radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
384                 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
385
386                 /* chop the rxpd */
387                 skb_pull(skb, sizeof(struct rxpd));
388
389                 /* add space for the new radio header */
390                 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
391                     pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
392                                      GFP_ATOMIC)) {
393                         lbs_pr_alert("%s: couldn't pskb_expand_head\n",
394                                __func__);
395                 }
396
397                 pradiotap_hdr =
398                     (struct rx_radiotap_hdr *)skb_push(skb,
399                                                      sizeof(struct
400                                                             rx_radiotap_hdr));
401                 memcpy(pradiotap_hdr, &radiotap_hdr,
402                        sizeof(struct rx_radiotap_hdr));
403                 break;
404
405         default:
406                 /* unknown header */
407                 lbs_pr_alert("Unknown radiomode %i\n",
408                        priv->adapter->radiomode);
409                 /* don't export any header */
410                 /* chop the rxpd */
411                 skb_pull(skb, sizeof(struct rxpd));
412                 break;
413         }
414
415         /* Take the data rate from the rxpd structure
416          * only if the rate is auto
417          */
418         if (adapter->auto_rate)
419                 adapter->cur_rate = libertas_fw_index_to_data_rate(prxpd->rx_rate);
420
421         wlan_compute_rssi(priv, prxpd);
422
423         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
424         priv->stats.rx_bytes += skb->len;
425         priv->stats.rx_packets++;
426
427         libertas_upload_rx_packet(priv, skb);
428
429         ret = 0;
430
431 done:
432         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
433         return ret;
434 }