[PATCH] libertas: split module into two (libertas.ko and usb8xxx.ko)
[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 < adapter->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 >= adapter->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->rxpd_rate = p_rx_pd->rx_rate;
121
122         adapter->SNR[TYPE_RXPD][TYPE_AVG] = wlan_getavgsnr(priv) * AVG_SCALE;
123         adapter->NF[TYPE_RXPD][TYPE_AVG] = wlan_getavgnf(priv) * AVG_SCALE;
124         lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
125                adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
126                adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
127
128         adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
129             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
130                      adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
131
132         adapter->RSSI[TYPE_RXPD][TYPE_AVG] =
133             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
134                      adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
135
136         lbs_deb_leave(LBS_DEB_RX);
137 }
138
139 void libertas_upload_rx_packet(wlan_private * priv, struct sk_buff *skb)
140 {
141         lbs_deb_rx("skb->data %p\n", skb->data);
142
143         if (priv->mesh_dev && IS_MESH_FRAME(skb))
144                 skb->dev = priv->mesh_dev;
145         else
146                 skb->dev = priv->wlan_dev.netdev;
147         skb->protocol = eth_type_trans(skb, priv->wlan_dev.netdev);
148         skb->ip_summed = CHECKSUM_UNNECESSARY;
149
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->debugmode & MRVDRV_DEBUG_RX_PATH)
177                 lbs_dbg_hex("RX packet: ", skb->data,
178                          min_t(unsigned int, skb->len, 100));
179
180         if (priv->adapter->linkmode == WLAN_LINKMODE_802_11)
181                 return process_rxed_802_11_packet(priv, skb);
182
183         p_rx_pkt = (struct rxpackethdr *) skb->data;
184         p_rx_pd = &p_rx_pkt->rx_pd;
185         if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
186                 SET_MESH_FRAME(skb);
187         else
188                 UNSET_MESH_FRAME(skb);
189
190         lbs_dbg_hex("RX Data: Before chop rxpd", skb->data,
191                  min_t(unsigned int, skb->len, 100));
192
193         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
194                 lbs_deb_rx("rx err: frame received with bad length\n");
195                 priv->stats.rx_length_errors++;
196                 ret = 0;
197                 goto done;
198         }
199
200         /*
201          * Check rxpd status and update 802.3 stat,
202          */
203         if (!(p_rx_pd->status & MRVDRV_RXPD_STATUS_OK)) {
204                 lbs_deb_rx("rx err: frame received with bad status\n");
205                 lbs_pr_alert("rxpd not ok\n");
206                 priv->stats.rx_errors++;
207                 ret = 0;
208                 goto done;
209         }
210
211         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
212                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
213
214         lbs_dbg_hex("RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
215                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
216         lbs_dbg_hex("RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
217                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
218
219         if (memcmp(&p_rx_pkt->rfc1042_hdr,
220                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
221                 /*
222                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
223                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
224                  *
225                  *  The firmware only passes up SNAP frames converting
226                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
227                  *
228                  *  To create the Ethernet II, just move the src, dst address right
229                  *    before the snap_type.
230                  */
231                 p_ethhdr = (struct ethhdr *)
232                     ((u8 *) & p_rx_pkt->eth803_hdr
233                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
234                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
235                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
236                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
237
238                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
239                        sizeof(p_ethhdr->h_source));
240                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
241                        sizeof(p_ethhdr->h_dest));
242
243                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
244                  *   that was removed
245                  */
246                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
247         } else {
248                 lbs_dbg_hex("RX Data: LLC/SNAP",
249                         (u8 *) & p_rx_pkt->rfc1042_hdr,
250                         sizeof(p_rx_pkt->rfc1042_hdr));
251
252                 /* Chop off the rxpd */
253                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
254         }
255
256         /* Chop off the leading header bytes so the skb points to the start of
257          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
258          */
259         skb_pull(skb, hdrchop);
260
261         /* Take the data rate from the rxpd structure
262          * only if the rate is auto
263          */
264         if (adapter->is_datarate_auto)
265                 adapter->datarate = libertas_index_to_data_rate(p_rx_pd->rx_rate);
266
267         wlan_compute_rssi(priv, p_rx_pd);
268
269         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
270         priv->stats.rx_bytes += skb->len;
271         priv->stats.rx_packets++;
272
273         libertas_upload_rx_packet(priv, skb);
274
275         ret = 0;
276 done:
277         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
278         return ret;
279 }
280 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
281
282 /**
283  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
284  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
285  *
286  *  @param rate    Input rate
287  *  @return        Output Rate (0 if invalid)
288  */
289 static u8 convert_mv_rate_to_radiotap(u8 rate)
290 {
291         switch (rate) {
292         case 0:         /*   1 Mbps */
293                 return 2;
294         case 1:         /*   2 Mbps */
295                 return 4;
296         case 2:         /* 5.5 Mbps */
297                 return 11;
298         case 3:         /*  11 Mbps */
299                 return 22;
300         case 4:         /*   6 Mbps */
301                 return 12;
302         case 5:         /*   9 Mbps */
303                 return 18;
304         case 6:         /*  12 Mbps */
305                 return 24;
306         case 7:         /*  18 Mbps */
307                 return 36;
308         case 8:         /*  24 Mbps */
309                 return 48;
310         case 9:         /*  36 Mbps */
311                 return 72;
312         case 10:                /*  48 Mbps */
313                 return 96;
314         case 11:                /*  54 Mbps */
315                 return 108;
316         }
317         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
318         return 0;
319 }
320
321 /**
322  *  @brief This function processes a received 802.11 packet and forwards it
323  *  to kernel/upper layer
324  *
325  *  @param priv    A pointer to wlan_private
326  *  @param skb     A pointer to skb which includes the received packet
327  *  @return        0 or -1
328  */
329 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
330 {
331         wlan_adapter *adapter = priv->adapter;
332         int ret = 0;
333
334         struct rx80211packethdr *p_rx_pkt;
335         struct rxpd *prxpd;
336         struct rx_radiotap_hdr radiotap_hdr;
337         struct rx_radiotap_hdr *pradiotap_hdr;
338
339         lbs_deb_enter(LBS_DEB_RX);
340
341         p_rx_pkt = (struct rx80211packethdr *) skb->data;
342         prxpd = &p_rx_pkt->rx_pd;
343
344         // lbs_dbg_hex("RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
345
346         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
347                 lbs_deb_rx("rx err: frame received wit bad length\n");
348                 priv->stats.rx_length_errors++;
349                 ret = 0;
350                 goto done;
351         }
352
353         /*
354          * Check rxpd status and update 802.3 stat,
355          */
356         if (!(prxpd->status & MRVDRV_RXPD_STATUS_OK)) {
357                 //lbs_deb_rx("rx err: frame received with bad status\n");
358                 priv->stats.rx_errors++;
359         }
360
361         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
362                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
363
364         /* create the exported radio header */
365         switch (priv->adapter->radiomode) {
366         case WLAN_RADIOMODE_NONE:
367                 /* no radio header */
368                 /* chop the rxpd */
369                 skb_pull(skb, sizeof(struct rxpd));
370                 break;
371
372         case WLAN_RADIOMODE_RADIOTAP:
373                 /* radiotap header */
374                 radiotap_hdr.hdr.it_version = 0;
375                 /* XXX must check this value for pad */
376                 radiotap_hdr.hdr.it_pad = 0;
377                 radiotap_hdr.hdr.it_len = sizeof(struct rx_radiotap_hdr);
378                 radiotap_hdr.hdr.it_present = RX_RADIOTAP_PRESENT;
379                 /* unknown values */
380                 radiotap_hdr.flags = 0;
381                 radiotap_hdr.chan_freq = 0;
382                 radiotap_hdr.chan_flags = 0;
383                 radiotap_hdr.antenna = 0;
384                 /* known values */
385                 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
386                 /* XXX must check no carryout */
387                 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
388                 radiotap_hdr.rx_flags = 0;
389                 if (!(prxpd->status & MRVDRV_RXPD_STATUS_OK))
390                         radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
391                 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
392
393                 // lbs_dbg_hex1("RX radiomode packet BEF: ", skb->data, min(skb->len, 100));
394
395                 /* chop the rxpd */
396                 skb_pull(skb, sizeof(struct rxpd));
397
398                 /* add space for the new radio header */
399                 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
400                     pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
401                                      GFP_ATOMIC)) {
402                         lbs_pr_alert("%s: couldn't pskb_expand_head\n",
403                                __func__);
404                 }
405
406                 pradiotap_hdr =
407                     (struct rx_radiotap_hdr *)skb_push(skb,
408                                                      sizeof(struct
409                                                             rx_radiotap_hdr));
410                 memcpy(pradiotap_hdr, &radiotap_hdr,
411                        sizeof(struct rx_radiotap_hdr));
412                 //lbs_dbg_hex1("RX radiomode packet AFT: ", skb->data, min(skb->len, 100));
413                 break;
414
415         default:
416                 /* unknown header */
417                 lbs_pr_alert("Unknown radiomode %i\n",
418                        priv->adapter->radiomode);
419                 /* don't export any header */
420                 /* chop the rxpd */
421                 skb_pull(skb, sizeof(struct rxpd));
422                 break;
423         }
424
425         /* Take the data rate from the rxpd structure
426          * only if the rate is auto
427          */
428         if (adapter->is_datarate_auto) {
429                 adapter->datarate = libertas_index_to_data_rate(prxpd->rx_rate);
430         }
431
432         wlan_compute_rssi(priv, prxpd);
433
434         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
435         priv->stats.rx_bytes += skb->len;
436         priv->stats.rx_packets++;
437
438         libertas_upload_rx_packet(priv, skb);
439
440         ret = 0;
441
442 done:
443         skb->protocol = __constant_htons(0x0019);       /* ETH_P_80211_RAW */
444         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
445         return ret;
446 }