blob: a4d0bca9ef2c8aeac99f629b955d8056eaaa4c66 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * This file contains the handling of RX in wlan driver.
3 */
4#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006#include <linux/types.h>
Kiran Divekare86dc1c2010-06-14 22:01:26 +05307#include <net/cfg80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008
Kiran Divekare86dc1c2010-06-14 22:01:26 +05309#include "defs.h"
Holger Schurig9e66e702009-10-16 17:32:16 +020010#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "radiotap.h"
12#include "decl.h"
13#include "dev.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
15struct eth803hdr {
16 u8 dest_addr[6];
17 u8 src_addr[6];
18 u16 h803_len;
Eric Dumazetba2d3582010-06-02 18:10:09 +000019} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020
21struct rfc1042hdr {
22 u8 llc_dsap;
23 u8 llc_ssap;
24 u8 llc_ctrl;
25 u8 snap_oui[3];
26 u16 snap_type;
Eric Dumazetba2d3582010-06-02 18:10:09 +000027} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020028
29struct rxpackethdr {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020030 struct eth803hdr eth803_hdr;
31 struct rfc1042hdr rfc1042_hdr;
Eric Dumazetba2d3582010-06-02 18:10:09 +000032} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020033
34struct rx80211packethdr {
35 struct rxpd rx_pd;
36 void *eth80211_hdr;
Eric Dumazetba2d3582010-06-02 18:10:09 +000037} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020038
Holger Schurig69f90322007-11-23 15:43:44 +010039static int process_rxed_802_11_packet(struct lbs_private *priv,
40 struct sk_buff *skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020041
42/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020043 * @brief This function processes received packet and forwards it
44 * to kernel/upper layer
45 *
Stewart Malik819cf152010-03-11 20:28:29 +103046 * @param priv A pointer to struct lbs_private
47 * @param skb A pointer to skb which includes the received packet
48 * @return 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020049 */
Holger Schurig69f90322007-11-23 15:43:44 +010050int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020051{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020052 int ret = 0;
David Woodhouse6f93a8e2007-12-10 00:49:26 -050053 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020054 struct rxpackethdr *p_rx_pkt;
55 struct rxpd *p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020056 int hdrchop;
57 struct ethhdr *p_ethhdr;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020058 const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
59
Holger Schurig9012b282007-05-25 11:27:16 -040060 lbs_deb_enter(LBS_DEB_RX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020061
Holger Schurig7919b892008-04-01 14:50:43 +020062 BUG_ON(!skb);
63
David Woodhouse6f93a8e2007-12-10 00:49:26 -050064 skb->ip_summed = CHECKSUM_NONE;
65
Kiran Divekare86dc1c2010-06-14 22:01:26 +053066 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020067 return process_rxed_802_11_packet(priv, skb);
68
Bing Zhaoe45d8e52009-04-06 15:50:56 -070069 p_rx_pd = (struct rxpd *) skb->data;
70 p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
71 le32_to_cpu(p_rx_pd->pkt_ptr));
Holger Schurige0e42da2009-11-25 13:10:15 +010072
73 dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074
Holger Schurigece56192007-08-02 11:53:06 -040075 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020076 min_t(unsigned int, skb->len, 100));
77
78 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
Holger Schurig9012b282007-05-25 11:27:16 -040079 lbs_deb_rx("rx err: frame received with bad length\n");
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +000080 dev->stats.rx_length_errors++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020081 ret = 0;
Philip Rakityf54930f2009-04-07 12:41:17 -070082 dev_kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020083 goto done;
84 }
85
Bing Zhaoe45d8e52009-04-06 15:50:56 -070086 lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
John W. Linvillea2caba62009-04-14 11:45:57 -040087 skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
88 skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020089
Holger Schurigece56192007-08-02 11:53:06 -040090 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020091 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
Holger Schurigece56192007-08-02 11:53:06 -040092 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020093 sizeof(p_rx_pkt->eth803_hdr.src_addr));
94
95 if (memcmp(&p_rx_pkt->rfc1042_hdr,
96 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
97 /*
98 * Replace the 803 header and rfc1042 header (llc/snap) with an
99 * EthernetII header, keep the src/dst and snap_type (ethertype)
100 *
101 * The firmware only passes up SNAP frames converting
102 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
103 *
104 * To create the Ethernet II, just move the src, dst address right
105 * before the snap_type.
106 */
107 p_ethhdr = (struct ethhdr *)
Stewart Malik819cf152010-03-11 20:28:29 +1030108 ((u8 *) &p_rx_pkt->eth803_hdr
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
110 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
111 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
112 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
113
114 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
115 sizeof(p_ethhdr->h_source));
116 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
117 sizeof(p_ethhdr->h_dest));
118
119 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
120 * that was removed
121 */
Bing Zhaoe45d8e52009-04-06 15:50:56 -0700122 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200123 } else {
Holger Schurigece56192007-08-02 11:53:06 -0400124 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
Stewart Malik819cf152010-03-11 20:28:29 +1030125 (u8 *) &p_rx_pkt->rfc1042_hdr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200126 sizeof(p_rx_pkt->rfc1042_hdr));
127
128 /* Chop off the rxpd */
Bing Zhaoe45d8e52009-04-06 15:50:56 -0700129 hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130 }
131
132 /* Chop off the leading header bytes so the skb points to the start of
133 * either the reconstructed EthII frame or the 802.2/llc/snap frame
134 */
135 skb_pull(skb, hdrchop);
136
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530137 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200138
Holger Schurig9012b282007-05-25 11:27:16 -0400139 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000140 dev->stats.rx_bytes += skb->len;
141 dev->stats.rx_packets++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142
David Woodhouse6f93a8e2007-12-10 00:49:26 -0500143 skb->protocol = eth_type_trans(skb, dev);
Holger Schurigae3e0fc2008-01-16 15:48:44 +0100144 if (in_interrupt())
145 netif_rx(skb);
146 else
147 netif_rx_ni(skb);
Florin Malita3d4bd242007-05-18 16:04:33 -0400148
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200149 ret = 0;
150done:
Holger Schurig9012b282007-05-25 11:27:16 -0400151 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152 return ret;
153}
Holger Schurig10078322007-11-15 18:05:47 -0500154EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200155
156/**
157 * @brief This function converts Tx/Rx rates from the Marvell WLAN format
158 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
159 *
Stewart Malik819cf152010-03-11 20:28:29 +1030160 * @param rate Input rate
161 * @return Output Rate (0 if invalid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162 */
163static u8 convert_mv_rate_to_radiotap(u8 rate)
164{
165 switch (rate) {
166 case 0: /* 1 Mbps */
167 return 2;
168 case 1: /* 2 Mbps */
169 return 4;
170 case 2: /* 5.5 Mbps */
171 return 11;
172 case 3: /* 11 Mbps */
173 return 22;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400174 /* case 4: reserved */
175 case 5: /* 6 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200176 return 12;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400177 case 6: /* 9 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 return 18;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400179 case 7: /* 12 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200180 return 24;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400181 case 8: /* 18 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 return 36;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400183 case 9: /* 24 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184 return 48;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400185 case 10: /* 36 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200186 return 72;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400187 case 11: /* 48 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188 return 96;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400189 case 12: /* 54 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200190 return 108;
191 }
Holger Schurig9012b282007-05-25 11:27:16 -0400192 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200193 return 0;
194}
195
196/**
197 * @brief This function processes a received 802.11 packet and forwards it
198 * to kernel/upper layer
199 *
Stewart Malik819cf152010-03-11 20:28:29 +1030200 * @param priv A pointer to struct lbs_private
201 * @param skb A pointer to skb which includes the received packet
202 * @return 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200203 */
Holger Schurig69f90322007-11-23 15:43:44 +0100204static int process_rxed_802_11_packet(struct lbs_private *priv,
205 struct sk_buff *skb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200206{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200207 int ret = 0;
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000208 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209 struct rx80211packethdr *p_rx_pkt;
210 struct rxpd *prxpd;
211 struct rx_radiotap_hdr radiotap_hdr;
212 struct rx_radiotap_hdr *pradiotap_hdr;
213
Holger Schurig9012b282007-05-25 11:27:16 -0400214 lbs_deb_enter(LBS_DEB_RX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200215
216 p_rx_pkt = (struct rx80211packethdr *) skb->data;
217 prxpd = &p_rx_pkt->rx_pd;
218
Stewart Malik819cf152010-03-11 20:28:29 +1030219 /* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200220
221 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
David Woodhouse7bf02c22007-12-10 00:17:28 -0500222 lbs_deb_rx("rx err: frame received with bad length\n");
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000223 dev->stats.rx_length_errors++;
David Woodhouse7bf02c22007-12-10 00:17:28 -0500224 ret = -EINVAL;
Sergio Luisb700a982008-10-26 23:09:27 -0700225 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200226 goto done;
227 }
228
Holger Schurig9012b282007-05-25 11:27:16 -0400229 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200230 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
231
232 /* create the exported radio header */
David Woodhouse180be752007-12-10 00:05:37 -0500233
234 /* radiotap header */
Prarit Bhargavac6a63682010-05-27 14:41:20 -0400235 memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
236 /* XXX must check radiotap_hdr.hdr.it_pad for pad */
David Woodhouse180be752007-12-10 00:05:37 -0500237 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
238 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
David Woodhouse180be752007-12-10 00:05:37 -0500239 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
240 /* XXX must check no carryout */
241 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
David Woodhouse180be752007-12-10 00:05:37 -0500242
243 /* chop the rxpd */
244 skb_pull(skb, sizeof(struct rxpd));
245
246 /* add space for the new radio header */
247 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
David Woodhouse7bf02c22007-12-10 00:17:28 -0500248 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
249 lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
250 ret = -ENOMEM;
251 kfree_skb(skb);
252 goto done;
Luis Carlos Cobo965f8bbc2007-08-02 13:16:55 -0400253 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254
David Woodhouse180be752007-12-10 00:05:37 -0500255 pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
256 memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530258 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259
Holger Schurig9012b282007-05-25 11:27:16 -0400260 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000261 dev->stats.rx_bytes += skb->len;
262 dev->stats.rx_packets++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530264 skb->protocol = eth_type_trans(skb, priv->dev);
265
266 if (in_interrupt())
267 netif_rx(skb);
268 else
269 netif_rx_ni(skb);
Florin Malita3d4bd242007-05-18 16:04:33 -0400270
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200272
Holger Schurig9012b282007-05-25 11:27:16 -0400273done:
Holger Schurig9012b282007-05-25 11:27:16 -0400274 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
275 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200276}