blob: 24a12daa7c67053ec77d6e782b7103d5fd440f70 [file] [log] [blame]
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001/*
2 * This file contains the handling of RX in wlan driver.
3 */
Joe Perches0e4e06a2011-05-02 16:49:14 -07004
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02007#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include <linux/types.h>
Kiran Divekare86dc1c2010-06-14 22:01:26 +053010#include <net/cfg80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011
Kiran Divekare86dc1c2010-06-14 22:01:26 +053012#include "defs.h"
Holger Schurig9e66e702009-10-16 17:32:16 +020013#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014#include "radiotap.h"
15#include "decl.h"
16#include "dev.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020017
18struct eth803hdr {
19 u8 dest_addr[6];
20 u8 src_addr[6];
21 u16 h803_len;
Eric Dumazetba2d3582010-06-02 18:10:09 +000022} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023
24struct rfc1042hdr {
25 u8 llc_dsap;
26 u8 llc_ssap;
27 u8 llc_ctrl;
28 u8 snap_oui[3];
29 u16 snap_type;
Eric Dumazetba2d3582010-06-02 18:10:09 +000030} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020031
32struct rxpackethdr {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020033 struct eth803hdr eth803_hdr;
34 struct rfc1042hdr rfc1042_hdr;
Eric Dumazetba2d3582010-06-02 18:10:09 +000035} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020036
37struct rx80211packethdr {
38 struct rxpd rx_pd;
39 void *eth80211_hdr;
Eric Dumazetba2d3582010-06-02 18:10:09 +000040} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020041
Holger Schurig69f90322007-11-23 15:43:44 +010042static int process_rxed_802_11_packet(struct lbs_private *priv,
43 struct sk_buff *skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020044
45/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070046 * lbs_process_rxed_packet - processes received packet and forwards it
47 * to kernel/upper layer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020048 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070049 * @priv: A pointer to &struct lbs_private
50 * @skb: A pointer to skb which includes the received packet
51 * returns: 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020052 */
Holger Schurig69f90322007-11-23 15:43:44 +010053int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020055 int ret = 0;
David Woodhouse6f93a8e2007-12-10 00:49:26 -050056 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020057 struct rxpackethdr *p_rx_pkt;
58 struct rxpd *p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020059 int hdrchop;
60 struct ethhdr *p_ethhdr;
Joe Perches482e0392010-11-20 18:38:58 -080061 static const u8 rfc1042_eth_hdr[] = {
62 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
63 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064
Holger Schurig9012b282007-05-25 11:27:16 -040065 lbs_deb_enter(LBS_DEB_RX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066
Holger Schurig7919b892008-04-01 14:50:43 +020067 BUG_ON(!skb);
68
David Woodhouse6f93a8e2007-12-10 00:49:26 -050069 skb->ip_summed = CHECKSUM_NONE;
70
Kiran Divekare86dc1c2010-06-14 22:01:26 +053071 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020072 return process_rxed_802_11_packet(priv, skb);
73
Bing Zhaoe45d8e52009-04-06 15:50:56 -070074 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));
Holger Schurige0e42da2009-11-25 13:10:15 +010077
78 dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020079
Holger Schurigece56192007-08-02 11:53:06 -040080 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020081 min_t(unsigned int, skb->len, 100));
82
83 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
Holger Schurig9012b282007-05-25 11:27:16 -040084 lbs_deb_rx("rx err: frame received with bad length\n");
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +000085 dev->stats.rx_length_errors++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020086 ret = 0;
Philip Rakityf54930f2009-04-07 12:41:17 -070087 dev_kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088 goto done;
89 }
90
Bing Zhaoe45d8e52009-04-06 15:50:56 -070091 lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
John W. Linvillea2caba62009-04-14 11:45:57 -040092 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));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020094
Holger Schurigece56192007-08-02 11:53:06 -040095 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020096 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
Holger Schurigece56192007-08-02 11:53:06 -040097 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020098 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 *)
Stewart Malik819cf152010-03-11 20:28:29 +1030113 ((u8 *) &p_rx_pkt->eth803_hdr
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200114 + 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 */
Bing Zhaoe45d8e52009-04-06 15:50:56 -0700127 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128 } else {
Holger Schurigece56192007-08-02 11:53:06 -0400129 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
Stewart Malik819cf152010-03-11 20:28:29 +1030130 (u8 *) &p_rx_pkt->rfc1042_hdr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200131 sizeof(p_rx_pkt->rfc1042_hdr));
132
133 /* Chop off the rxpd */
Bing Zhaoe45d8e52009-04-06 15:50:56 -0700134 hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 }
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
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530142 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143
Holger Schurig9012b282007-05-25 11:27:16 -0400144 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000145 dev->stats.rx_bytes += skb->len;
146 dev->stats.rx_packets++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147
David Woodhouse6f93a8e2007-12-10 00:49:26 -0500148 skb->protocol = eth_type_trans(skb, dev);
Holger Schurigae3e0fc2008-01-16 15:48:44 +0100149 if (in_interrupt())
150 netif_rx(skb);
151 else
152 netif_rx_ni(skb);
Florin Malita3d4bd242007-05-18 16:04:33 -0400153
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154 ret = 0;
155done:
Holger Schurig9012b282007-05-25 11:27:16 -0400156 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200157 return ret;
158}
Holger Schurig10078322007-11-15 18:05:47 -0500159EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200160
161/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700162 * 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)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700165 * @rate: Input rate
166 * returns: Output Rate (0 if invalid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167 */
168static 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;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400179 /* case 4: reserved */
180 case 5: /* 6 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181 return 12;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400182 case 6: /* 9 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 return 18;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400184 case 7: /* 12 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185 return 24;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400186 case 8: /* 18 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200187 return 36;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400188 case 9: /* 24 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200189 return 48;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400190 case 10: /* 36 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200191 return 72;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400192 case 11: /* 48 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200193 return 96;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400194 case 12: /* 54 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 return 108;
196 }
Joe Perches0e4e06a2011-05-02 16:49:14 -0700197 pr_alert("Invalid Marvell WLAN rate %i\n", rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200198 return 0;
199}
200
201/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700202 * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
203 * it to kernel/upper layer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700205 * @priv: A pointer to &struct lbs_private
206 * @skb: A pointer to skb which includes the received packet
207 * returns: 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200208 */
Holger Schurig69f90322007-11-23 15:43:44 +0100209static int process_rxed_802_11_packet(struct lbs_private *priv,
210 struct sk_buff *skb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200211{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200212 int ret = 0;
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000213 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200214 struct rx80211packethdr *p_rx_pkt;
215 struct rxpd *prxpd;
216 struct rx_radiotap_hdr radiotap_hdr;
217 struct rx_radiotap_hdr *pradiotap_hdr;
218
Holger Schurig9012b282007-05-25 11:27:16 -0400219 lbs_deb_enter(LBS_DEB_RX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200220
221 p_rx_pkt = (struct rx80211packethdr *) skb->data;
222 prxpd = &p_rx_pkt->rx_pd;
223
Stewart Malik819cf152010-03-11 20:28:29 +1030224 /* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200225
226 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
David Woodhouse7bf02c22007-12-10 00:17:28 -0500227 lbs_deb_rx("rx err: frame received with bad length\n");
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000228 dev->stats.rx_length_errors++;
David Woodhouse7bf02c22007-12-10 00:17:28 -0500229 ret = -EINVAL;
Sergio Luisb700a982008-10-26 23:09:27 -0700230 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200231 goto done;
232 }
233
Holger Schurig9012b282007-05-25 11:27:16 -0400234 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
236
237 /* create the exported radio header */
David Woodhouse180be752007-12-10 00:05:37 -0500238
239 /* radiotap header */
Prarit Bhargavac6a63682010-05-27 14:41:20 -0400240 memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
241 /* XXX must check radiotap_hdr.hdr.it_pad for pad */
David Woodhouse180be752007-12-10 00:05:37 -0500242 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);
David Woodhouse180be752007-12-10 00:05:37 -0500244 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;
David Woodhouse180be752007-12-10 00:05:37 -0500247
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)) &&
David Woodhouse7bf02c22007-12-10 00:17:28 -0500253 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700254 pr_alert("%s: couldn't pskb_expand_head\n", __func__);
David Woodhouse7bf02c22007-12-10 00:17:28 -0500255 ret = -ENOMEM;
256 kfree_skb(skb);
257 goto done;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400258 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259
David Woodhouse180be752007-12-10 00:05:37 -0500260 pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
261 memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530263 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200264
Holger Schurig9012b282007-05-25 11:27:16 -0400265 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000266 dev->stats.rx_bytes += skb->len;
267 dev->stats.rx_packets++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530269 skb->protocol = eth_type_trans(skb, priv->dev);
270
271 if (in_interrupt())
272 netif_rx(skb);
273 else
274 netif_rx_ni(skb);
Florin Malita3d4bd242007-05-18 16:04:33 -0400275
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200277
Holger Schurig9012b282007-05-25 11:27:16 -0400278done:
Holger Schurig9012b282007-05-25 11:27:16 -0400279 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
280 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281}