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