blob: bfb8898ae518908f8394ef94a6f158b74f1bb232 [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +00008#include <linux/hardirq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include <linux/types.h>
Kiran Divekare86dc1c2010-06-14 22:01:26 +053011#include <net/cfg80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012
Kiran Divekare86dc1c2010-06-14 22:01:26 +053013#include "defs.h"
Holger Schurig9e66e702009-10-16 17:32:16 +020014#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015#include "radiotap.h"
16#include "decl.h"
17#include "dev.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018
19struct eth803hdr {
20 u8 dest_addr[6];
21 u8 src_addr[6];
22 u16 h803_len;
Eric Dumazetba2d3582010-06-02 18:10:09 +000023} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024
25struct rfc1042hdr {
26 u8 llc_dsap;
27 u8 llc_ssap;
28 u8 llc_ctrl;
29 u8 snap_oui[3];
30 u16 snap_type;
Eric Dumazetba2d3582010-06-02 18:10:09 +000031} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020032
33struct rxpackethdr {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020034 struct eth803hdr eth803_hdr;
35 struct rfc1042hdr rfc1042_hdr;
Eric Dumazetba2d3582010-06-02 18:10:09 +000036} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020037
38struct rx80211packethdr {
39 struct rxpd rx_pd;
40 void *eth80211_hdr;
Eric Dumazetba2d3582010-06-02 18:10:09 +000041} __packed;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020042
Holger Schurig69f90322007-11-23 15:43:44 +010043static int process_rxed_802_11_packet(struct lbs_private *priv,
44 struct sk_buff *skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020045
46/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -070047 * lbs_process_rxed_packet - processes received packet and forwards it
48 * to kernel/upper layer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020049 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -070050 * @priv: A pointer to &struct lbs_private
51 * @skb: A pointer to skb which includes the received packet
52 * returns: 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020053 */
Holger Schurig69f90322007-11-23 15:43:44 +010054int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020055{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020056 int ret = 0;
David Woodhouse6f93a8e2007-12-10 00:49:26 -050057 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020058 struct rxpackethdr *p_rx_pkt;
59 struct rxpd *p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020060 int hdrchop;
61 struct ethhdr *p_ethhdr;
Joe Perches482e0392010-11-20 18:38:58 -080062 static const u8 rfc1042_eth_hdr[] = {
63 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
64 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020065
Holger Schurig9012b282007-05-25 11:27:16 -040066 lbs_deb_enter(LBS_DEB_RX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020067
Holger Schurig7919b892008-04-01 14:50:43 +020068 BUG_ON(!skb);
69
David Woodhouse6f93a8e2007-12-10 00:49:26 -050070 skb->ip_summed = CHECKSUM_NONE;
71
Kiran Divekare86dc1c2010-06-14 22:01:26 +053072 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 return process_rxed_802_11_packet(priv, skb);
74
Bing Zhaoe45d8e52009-04-06 15:50:56 -070075 p_rx_pd = (struct rxpd *) skb->data;
76 p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
77 le32_to_cpu(p_rx_pd->pkt_ptr));
Holger Schurige0e42da2009-11-25 13:10:15 +010078
79 dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020080
Holger Schurigece56192007-08-02 11:53:06 -040081 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020082 min_t(unsigned int, skb->len, 100));
83
84 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
Holger Schurig9012b282007-05-25 11:27:16 -040085 lbs_deb_rx("rx err: frame received with bad length\n");
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +000086 dev->stats.rx_length_errors++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087 ret = 0;
Philip Rakityf54930f2009-04-07 12:41:17 -070088 dev_kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020089 goto done;
90 }
91
Bing Zhaoe45d8e52009-04-06 15:50:56 -070092 lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
John W. Linvillea2caba62009-04-14 11:45:57 -040093 skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
94 skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020095
Holger Schurigece56192007-08-02 11:53:06 -040096 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020097 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
Holger Schurigece56192007-08-02 11:53:06 -040098 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020099 sizeof(p_rx_pkt->eth803_hdr.src_addr));
100
101 if (memcmp(&p_rx_pkt->rfc1042_hdr,
102 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
103 /*
104 * Replace the 803 header and rfc1042 header (llc/snap) with an
105 * EthernetII header, keep the src/dst and snap_type (ethertype)
106 *
107 * The firmware only passes up SNAP frames converting
108 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
109 *
110 * To create the Ethernet II, just move the src, dst address right
111 * before the snap_type.
112 */
113 p_ethhdr = (struct ethhdr *)
Stewart Malik819cf152010-03-11 20:28:29 +1030114 ((u8 *) &p_rx_pkt->eth803_hdr
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
116 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
117 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
118 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
119
120 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
121 sizeof(p_ethhdr->h_source));
122 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
123 sizeof(p_ethhdr->h_dest));
124
125 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
126 * that was removed
127 */
Bing Zhaoe45d8e52009-04-06 15:50:56 -0700128 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129 } else {
Holger Schurigece56192007-08-02 11:53:06 -0400130 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
Stewart Malik819cf152010-03-11 20:28:29 +1030131 (u8 *) &p_rx_pkt->rfc1042_hdr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 sizeof(p_rx_pkt->rfc1042_hdr));
133
134 /* Chop off the rxpd */
Bing Zhaoe45d8e52009-04-06 15:50:56 -0700135 hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 }
137
138 /* Chop off the leading header bytes so the skb points to the start of
139 * either the reconstructed EthII frame or the 802.2/llc/snap frame
140 */
141 skb_pull(skb, hdrchop);
142
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530143 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144
Holger Schurig9012b282007-05-25 11:27:16 -0400145 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000146 dev->stats.rx_bytes += skb->len;
147 dev->stats.rx_packets++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148
David Woodhouse6f93a8e2007-12-10 00:49:26 -0500149 skb->protocol = eth_type_trans(skb, dev);
Holger Schurigae3e0fc2008-01-16 15:48:44 +0100150 if (in_interrupt())
151 netif_rx(skb);
152 else
153 netif_rx_ni(skb);
Florin Malita3d4bd242007-05-18 16:04:33 -0400154
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200155 ret = 0;
156done:
Holger Schurig9012b282007-05-25 11:27:16 -0400157 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200158 return ret;
159}
Holger Schurig10078322007-11-15 18:05:47 -0500160EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161
162/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700163 * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
164 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700166 * @rate: Input rate
167 * returns: Output Rate (0 if invalid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168 */
169static u8 convert_mv_rate_to_radiotap(u8 rate)
170{
171 switch (rate) {
172 case 0: /* 1 Mbps */
173 return 2;
174 case 1: /* 2 Mbps */
175 return 4;
176 case 2: /* 5.5 Mbps */
177 return 11;
178 case 3: /* 11 Mbps */
179 return 22;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400180 /* case 4: reserved */
181 case 5: /* 6 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 return 12;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400183 case 6: /* 9 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184 return 18;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400185 case 7: /* 12 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200186 return 24;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400187 case 8: /* 18 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188 return 36;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400189 case 9: /* 24 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200190 return 48;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400191 case 10: /* 36 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 return 72;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400193 case 11: /* 48 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 return 96;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400195 case 12: /* 54 Mbps */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200196 return 108;
197 }
Joe Perches0e4e06a2011-05-02 16:49:14 -0700198 pr_alert("Invalid Marvell WLAN rate %i\n", rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200199 return 0;
200}
201
202/**
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700203 * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
204 * it to kernel/upper layer
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700206 * @priv: A pointer to &struct lbs_private
207 * @skb: A pointer to skb which includes the received packet
208 * returns: 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209 */
Holger Schurig69f90322007-11-23 15:43:44 +0100210static int process_rxed_802_11_packet(struct lbs_private *priv,
211 struct sk_buff *skb)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200212{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213 int ret = 0;
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000214 struct net_device *dev = priv->dev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200215 struct rx80211packethdr *p_rx_pkt;
216 struct rxpd *prxpd;
217 struct rx_radiotap_hdr radiotap_hdr;
218 struct rx_radiotap_hdr *pradiotap_hdr;
219
Holger Schurig9012b282007-05-25 11:27:16 -0400220 lbs_deb_enter(LBS_DEB_RX);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200221
222 p_rx_pkt = (struct rx80211packethdr *) skb->data;
223 prxpd = &p_rx_pkt->rx_pd;
224
Stewart Malik819cf152010-03-11 20:28:29 +1030225 /* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200226
227 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
David Woodhouse7bf02c22007-12-10 00:17:28 -0500228 lbs_deb_rx("rx err: frame received with bad length\n");
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000229 dev->stats.rx_length_errors++;
David Woodhouse7bf02c22007-12-10 00:17:28 -0500230 ret = -EINVAL;
Sergio Luisb700a982008-10-26 23:09:27 -0700231 kfree_skb(skb);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200232 goto done;
233 }
234
Holger Schurig9012b282007-05-25 11:27:16 -0400235 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
237
238 /* create the exported radio header */
David Woodhouse180be752007-12-10 00:05:37 -0500239
240 /* radiotap header */
Prarit Bhargavac6a63682010-05-27 14:41:20 -0400241 memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
242 /* XXX must check radiotap_hdr.hdr.it_pad for pad */
David Woodhouse180be752007-12-10 00:05:37 -0500243 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
244 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
David Woodhouse180be752007-12-10 00:05:37 -0500245 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
246 /* XXX must check no carryout */
247 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
David Woodhouse180be752007-12-10 00:05:37 -0500248
249 /* chop the rxpd */
250 skb_pull(skb, sizeof(struct rxpd));
251
252 /* add space for the new radio header */
253 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
David Woodhouse7bf02c22007-12-10 00:17:28 -0500254 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
Joe Perchesf3a57fd2011-05-02 16:49:15 -0700255 netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
David Woodhouse7bf02c22007-12-10 00:17:28 -0500256 ret = -ENOMEM;
257 kfree_skb(skb);
258 goto done;
Luis Carlos Cobo965f8bb2007-08-02 13:16:55 -0400259 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260
David Woodhouse180be752007-12-10 00:05:37 -0500261 pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
262 memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530264 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200265
Holger Schurig9012b282007-05-25 11:27:16 -0400266 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
Stephen Hemmingerbbfc6b72009-03-20 19:36:36 +0000267 dev->stats.rx_bytes += skb->len;
268 dev->stats.rx_packets++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200269
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530270 skb->protocol = eth_type_trans(skb, priv->dev);
271
272 if (in_interrupt())
273 netif_rx(skb);
274 else
275 netif_rx_ni(skb);
Florin Malita3d4bd242007-05-18 16:04:33 -0400276
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200277 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278
Holger Schurig9012b282007-05-25 11:27:16 -0400279done:
Holger Schurig9012b282007-05-25 11:27:16 -0400280 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
281 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200282}