Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / net / wireless / libertas / rx.c
1 /*
2  * This file contains the handling of RX in wlan driver.
3  */
4
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <linux/types.h>
10 #include <net/cfg80211.h>
11
12 #include "defs.h"
13 #include "host.h"
14 #include "radiotap.h"
15 #include "decl.h"
16 #include "dev.h"
17
18 struct eth803hdr {
19         u8 dest_addr[6];
20         u8 src_addr[6];
21         u16 h803_len;
22 } __packed;
23
24 struct rfc1042hdr {
25         u8 llc_dsap;
26         u8 llc_ssap;
27         u8 llc_ctrl;
28         u8 snap_oui[3];
29         u16 snap_type;
30 } __packed;
31
32 struct rxpackethdr {
33         struct eth803hdr eth803_hdr;
34         struct rfc1042hdr rfc1042_hdr;
35 } __packed;
36
37 struct rx80211packethdr {
38         struct rxpd rx_pd;
39         void *eth80211_hdr;
40 } __packed;
41
42 static int process_rxed_802_11_packet(struct lbs_private *priv,
43         struct sk_buff *skb);
44
45 /**
46  * lbs_process_rxed_packet - processes received packet and forwards it
47  * to kernel/upper layer
48  *
49  * @priv:       A pointer to &struct lbs_private
50  * @skb:        A pointer to skb which includes the received packet
51  * returns:     0 or -1
52  */
53 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
54 {
55         int ret = 0;
56         struct net_device *dev = priv->dev;
57         struct rxpackethdr *p_rx_pkt;
58         struct rxpd *p_rx_pd;
59         int hdrchop;
60         struct ethhdr *p_ethhdr;
61         static const u8 rfc1042_eth_hdr[] = {
62                 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
63         };
64
65         lbs_deb_enter(LBS_DEB_RX);
66
67         BUG_ON(!skb);
68
69         skb->ip_summed = CHECKSUM_NONE;
70
71         if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
72                 return process_rxed_802_11_packet(priv, skb);
73
74         p_rx_pd = (struct rxpd *) skb->data;
75         p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
76                 le32_to_cpu(p_rx_pd->pkt_ptr));
77
78         dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
79
80         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
81                  min_t(unsigned int, skb->len, 100));
82
83         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
84                 lbs_deb_rx("rx err: frame received with bad length\n");
85                 dev->stats.rx_length_errors++;
86                 ret = 0;
87                 dev_kfree_skb(skb);
88                 goto done;
89         }
90
91         lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
92                 skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
93                 skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
94
95         lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
96                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
97         lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
98                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
99
100         if (memcmp(&p_rx_pkt->rfc1042_hdr,
101                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
102                 /*
103                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
104                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
105                  *
106                  *  The firmware only passes up SNAP frames converting
107                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
108                  *
109                  *  To create the Ethernet II, just move the src, dst address right
110                  *    before the snap_type.
111                  */
112                 p_ethhdr = (struct ethhdr *)
113                     ((u8 *) &p_rx_pkt->eth803_hdr
114                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
115                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
116                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
117                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
118
119                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
120                        sizeof(p_ethhdr->h_source));
121                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
122                        sizeof(p_ethhdr->h_dest));
123
124                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
125                  *   that was removed
126                  */
127                 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
128         } else {
129                 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
130                         (u8 *) &p_rx_pkt->rfc1042_hdr,
131                         sizeof(p_rx_pkt->rfc1042_hdr));
132
133                 /* Chop off the rxpd */
134                 hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
135         }
136
137         /* Chop off the leading header bytes so the skb points to the start of
138          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
139          */
140         skb_pull(skb, hdrchop);
141
142         priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
143
144         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
145         dev->stats.rx_bytes += skb->len;
146         dev->stats.rx_packets++;
147
148         skb->protocol = eth_type_trans(skb, dev);
149         if (in_interrupt())
150                 netif_rx(skb);
151         else
152                 netif_rx_ni(skb);
153
154         ret = 0;
155 done:
156         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
157         return ret;
158 }
159 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
160
161 /**
162  * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
163  * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
164  *
165  * @rate:       Input rate
166  * returns:     Output Rate (0 if invalid)
167  */
168 static u8 convert_mv_rate_to_radiotap(u8 rate)
169 {
170         switch (rate) {
171         case 0:         /*   1 Mbps */
172                 return 2;
173         case 1:         /*   2 Mbps */
174                 return 4;
175         case 2:         /* 5.5 Mbps */
176                 return 11;
177         case 3:         /*  11 Mbps */
178                 return 22;
179         /* case 4: reserved */
180         case 5:         /*   6 Mbps */
181                 return 12;
182         case 6:         /*   9 Mbps */
183                 return 18;
184         case 7:         /*  12 Mbps */
185                 return 24;
186         case 8:         /*  18 Mbps */
187                 return 36;
188         case 9:         /*  24 Mbps */
189                 return 48;
190         case 10:                /*  36 Mbps */
191                 return 72;
192         case 11:                /*  48 Mbps */
193                 return 96;
194         case 12:                /*  54 Mbps */
195                 return 108;
196         }
197         pr_alert("Invalid Marvell WLAN rate %i\n", rate);
198         return 0;
199 }
200
201 /**
202  * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
203  * it to kernel/upper layer
204  *
205  * @priv:       A pointer to &struct lbs_private
206  * @skb:        A pointer to skb which includes the received packet
207  * returns:     0 or -1
208  */
209 static int process_rxed_802_11_packet(struct lbs_private *priv,
210         struct sk_buff *skb)
211 {
212         int ret = 0;
213         struct net_device *dev = priv->dev;
214         struct rx80211packethdr *p_rx_pkt;
215         struct rxpd *prxpd;
216         struct rx_radiotap_hdr radiotap_hdr;
217         struct rx_radiotap_hdr *pradiotap_hdr;
218
219         lbs_deb_enter(LBS_DEB_RX);
220
221         p_rx_pkt = (struct rx80211packethdr *) skb->data;
222         prxpd = &p_rx_pkt->rx_pd;
223
224         /* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
225
226         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
227                 lbs_deb_rx("rx err: frame received with bad length\n");
228                 dev->stats.rx_length_errors++;
229                 ret = -EINVAL;
230                 kfree_skb(skb);
231                 goto done;
232         }
233
234         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
235                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
236
237         /* create the exported radio header */
238
239         /* radiotap header */
240         memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
241         /* XXX must check radiotap_hdr.hdr.it_pad for pad */
242         radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
243         radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
244         radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
245         /* XXX must check no carryout */
246         radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
247
248         /* chop the rxpd */
249         skb_pull(skb, sizeof(struct rxpd));
250
251         /* add space for the new radio header */
252         if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
253             pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
254                 netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
255                 ret = -ENOMEM;
256                 kfree_skb(skb);
257                 goto done;
258         }
259
260         pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
261         memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
262
263         priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
264
265         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
266         dev->stats.rx_bytes += skb->len;
267         dev->stats.rx_packets++;
268
269         skb->protocol = eth_type_trans(skb, priv->dev);
270
271         if (in_interrupt())
272                 netif_rx(skb);
273         else
274                 netif_rx_ni(skb);
275
276         ret = 0;
277
278 done:
279         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
280         return ret;
281 }