Merge tag 'pwm/for-4.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[deliverable/linux.git] / drivers / staging / rtl8192e / rtllib_tx.c
CommitLineData
ecdfa446
GKH
1/******************************************************************************
2
3 Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of version 2 of the GNU General Public License as
7 published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 more details.
13
ecdfa446
GKH
14 The full GNU General Public License is included in this distribution in the
15 file called LICENSE.
16
17 Contact Information:
18 James P. Ketrenos <ipw2100-admin@linux.intel.com>
19 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
20
21******************************************************************************
22
23 Few modifications for Realtek's Wi-Fi drivers by
559a4c31 24 Andrea Merello <andrea.merello@gmail.com>
ecdfa446
GKH
25
26 A special thanks goes to Realtek for their support !
27
28******************************************************************************/
29
30#include <linux/compiler.h>
ecdfa446
GKH
31#include <linux/errno.h>
32#include <linux/if_arp.h>
33#include <linux/in6.h>
34#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/netdevice.h>
39#include <linux/pci.h>
40#include <linux/proc_fs.h>
41#include <linux/skbuff.h>
42#include <linux/slab.h>
43#include <linux/tcp.h>
44#include <linux/types.h>
ecdfa446
GKH
45#include <linux/wireless.h>
46#include <linux/etherdevice.h>
f38d223b 47#include <linux/uaccess.h>
ecdfa446
GKH
48#include <linux/if_vlan.h>
49
94a79942 50#include "rtllib.h"
ecdfa446 51
14b40d92
MK
52/* 802.11 Data Frame
53 *
54 *
55 * 802.11 frame_control for data frames - 2 bytes
35e33b04
MK
56 * ,--------------------------------------------------------------------.
57 * bits | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e |
58 * |---|---|---|---|---|---|---|---|---|----|----|-----|-----|-----|----|
59 * val | 0 | 0 | 0 | 1 | x | 0 | 0 | 0 | 1 | 0 | x | x | x | x | x |
60 * |---|---|---|---|---|---|---|---|---|----|----|-----|-----|-----|----|
61 * desc | ver | type | ^-subtype-^ |to |from|more|retry| pwr |more |wep |
62 * | | | x=0 data |DS | DS |frag| | mgm |data | |
63 * | | | x=1 data+ack | | | | | | | |
64 * '--------------------------------------------------------------------'
65 * /\
66 * |
67 * 802.11 Data Frame |
68 * ,--------- 'ctrl' expands to >---'
14b40d92
MK
69 * |
70 * ,--'---,-------------------------------------------------------------.
71 * Bytes | 2 | 2 | 6 | 6 | 6 | 2 | 0..2312 | 4 |
72 * |------|------|---------|---------|---------|------|---------|------|
73 * Desc. | ctrl | dura | DA/RA | TA | SA | Sequ | Frame | fcs |
74 * | | tion | (BSSID) | | | ence | data | |
75 * `--------------------------------------------------| |------'
76 * Total: 28 non-data bytes `----.----'
77 * |
78 * .- 'Frame data' expands to <---------------------------'
79 * |
80 * V
81 * ,---------------------------------------------------.
82 * Bytes | 1 | 1 | 1 | 3 | 2 | 0-2304 |
83 * |------|------|---------|----------|------|---------|
84 * Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP |
85 * | DSAP | SSAP | | | | Packet |
86 * | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8| | |
87 * `-----------------------------------------| |
88 * Total: 8 non-data bytes `----.----'
89 * |
90 * .- 'IP Packet' expands, if WEP enabled, to <--'
91 * |
92 * V
93 * ,-----------------------.
94 * Bytes | 4 | 0-2296 | 4 |
95 * |-----|-----------|-----|
96 * Desc. | IV | Encrypted | ICV |
97 * | | IP Packet | |
98 * `-----------------------'
99 * Total: 8 non-data bytes
100 *
101 *
102 * 802.3 Ethernet Data Frame
103 *
104 * ,-----------------------------------------.
105 * Bytes | 6 | 6 | 2 | Variable | 4 |
106 * |-------|-------|------|-----------|------|
107 * Desc. | Dest. | Source| Type | IP Packet | fcs |
108 * | MAC | MAC | | | |
109 * `-----------------------------------------'
110 * Total: 18 non-data bytes
111 *
35e33b04
MK
112 * In the event that fragmentation is required, the incoming payload is split
113 * into N parts of size ieee->fts. The first fragment contains the SNAP header
114 * and the remaining packets are just data.
14b40d92 115 *
35e33b04
MK
116 * If encryption is enabled, each fragment payload size is reduced by enough
117 * space to add the prefix and postfix (IV and ICV totalling 8 bytes in
118 * the case of WEP) So if you have 1500 bytes of payload with ieee->fts set to
119 * 500 without encryption it will take 3 frames. With WEP it will take 4 frames
120 * as the payload of each frame is reduced to 492 bytes.
14b40d92
MK
121 *
122 * SKB visualization
123 *
124 * ,- skb->data
125 * |
126 * | ETHERNET HEADER ,-<-- PAYLOAD
127 * | | 14 bytes from skb->data
128 * | 2 bytes for Type --> ,T. | (sizeof ethhdr)
129 * | | | |
130 * |,-Dest.--. ,--Src.---. | | |
131 * | 6 bytes| | 6 bytes | | | |
132 * v | | | | | |
133 * 0 | v 1 | v | v 2
134 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
135 * ^ | ^ | ^ |
136 * | | | | | |
137 * | | | | `T' <---- 2 bytes for Type
138 * | | | |
139 * | | '---SNAP--' <-------- 6 bytes for SNAP
140 * | |
141 * `-IV--' <-------------------- 4 bytes for IV (WEP)
142 *
143 * SNAP HEADER
144 *
145 */
ecdfa446
GKH
146
147static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
148static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
149
511998ea 150static int rtllib_put_snap(u8 *data, u16 h_proto)
ecdfa446 151{
94a79942 152 struct rtllib_snap_hdr *snap;
ecdfa446
GKH
153 u8 *oui;
154
94a79942 155 snap = (struct rtllib_snap_hdr *)data;
ecdfa446
GKH
156 snap->dsap = 0xaa;
157 snap->ssap = 0xaa;
158 snap->ctrl = 0x03;
159
160 if (h_proto == 0x8137 || h_proto == 0x80f3)
161 oui = P802_1H_OUI;
162 else
163 oui = RFC1042_OUI;
164 snap->oui[0] = oui[0];
165 snap->oui[1] = oui[1];
166 snap->oui[2] = oui[2];
167
9326c5ca 168 *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
ecdfa446
GKH
169
170 return SNAP_SIZE + sizeof(u16);
171}
172
f38d223b
LF
173int rtllib_encrypt_fragment(struct rtllib_device *ieee, struct sk_buff *frag,
174 int hdr_len)
ecdfa446 175{
32c44cb5 176 struct lib80211_crypt_data *crypt = NULL;
ecdfa446
GKH
177 int res;
178
0ddcf5fd 179 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
94a79942 180
f38d223b 181 if (!(crypt && crypt->ops)) {
d69d2054
MK
182 netdev_info(ieee->dev, "=========>%s(), crypt is null\n",
183 __func__);
ecdfa446
GKH
184 return -1;
185 }
ecdfa446 186 /* To encrypt, frame format is:
14b40d92
MK
187 * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes)
188 */
ecdfa446 189
ecdfa446 190 /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
14b40d92
MK
191 * call both MSDU and MPDU encryption functions from here.
192 */
ecdfa446
GKH
193 atomic_inc(&crypt->refcnt);
194 res = 0;
195 if (crypt->ops->encrypt_msdu)
196 res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
197 if (res == 0 && crypt->ops->encrypt_mpdu)
198 res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
199
200 atomic_dec(&crypt->refcnt);
201 if (res < 0) {
d69d2054
MK
202 netdev_info(ieee->dev, "%s: Encryption failed: len=%d.\n",
203 ieee->dev->name, frag->len);
ecdfa446
GKH
204 return -1;
205 }
206
207 return 0;
208}
209
210
f38d223b
LF
211void rtllib_txb_free(struct rtllib_txb *txb)
212{
ecdfa446
GKH
213 if (unlikely(!txb))
214 return;
ecdfa446
GKH
215 kfree(txb);
216}
217
ec0dc6be
LF
218static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
219 gfp_t gfp_mask)
ecdfa446 220{
94a79942 221 struct rtllib_txb *txb;
ecdfa446 222 int i;
3a6b70c3 223
f38d223b
LF
224 txb = kmalloc(sizeof(struct rtllib_txb) + (sizeof(u8 *) * nr_frags),
225 gfp_mask);
ecdfa446
GKH
226 if (!txb)
227 return NULL;
228
94a79942 229 memset(txb, 0, sizeof(struct rtllib_txb));
ecdfa446 230 txb->nr_frags = nr_frags;
198e0d17 231 txb->frag_size = cpu_to_le16(txb_size);
ecdfa446
GKH
232
233 for (i = 0; i < nr_frags; i++) {
234 txb->fragments[i] = dev_alloc_skb(txb_size);
235 if (unlikely(!txb->fragments[i])) {
236 i--;
237 break;
238 }
239 memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
240 }
241 if (unlikely(i != nr_frags)) {
242 while (i >= 0)
243 dev_kfree_skb_any(txb->fragments[i--]);
244 kfree(txb);
245 return NULL;
246 }
247 return txb;
248}
249
ec0dc6be 250static int rtllib_classify(struct sk_buff *skb, u8 bIsAmsdu)
ecdfa446
GKH
251{
252 struct ethhdr *eth;
253 struct iphdr *ip;
94a79942 254
ecdfa446
GKH
255 eth = (struct ethhdr *)skb->data;
256 if (eth->h_proto != htons(ETH_P_IP))
257 return 0;
258
72321415
MK
259#ifdef VERBOSE_DEBUG
260 print_hex_dump_bytes("rtllib_classify(): ", DUMP_PREFIX_NONE, skb->data,
261 skb->len);
262#endif
ecdfa446 263 ip = ip_hdr(skb);
ecdfa446 264 switch (ip->tos & 0xfc) {
94a79942
LF
265 case 0x20:
266 return 2;
267 case 0x40:
268 return 1;
269 case 0x60:
270 return 3;
271 case 0x80:
272 return 4;
273 case 0xa0:
274 return 5;
275 case 0xc0:
276 return 6;
277 case 0xe0:
278 return 7;
279 default:
280 return 0;
ecdfa446
GKH
281 }
282}
283
ec0dc6be
LF
284static void rtllib_tx_query_agg_cap(struct rtllib_device *ieee,
285 struct sk_buff *skb,
286 struct cb_desc *tcb_desc)
ecdfa446 287{
7796d93e 288 struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
60554f2b 289 struct tx_ts_record *pTxTs = NULL;
4d0c8ee8 290 struct rtllib_hdr_1addr *hdr = (struct rtllib_hdr_1addr *)skb->data;
94a79942 291
f38d223b 292 if (rtllib_act_scanning(ieee, false))
94a79942 293 return;
ecdfa446 294
f38d223b 295 if (!pHTInfo->bCurrentHTSupport || !pHTInfo->bEnableHT)
ecdfa446
GKH
296 return;
297 if (!IsQoSDataFrame(skb->data))
298 return;
14fc4235 299 if (is_multicast_ether_addr(hdr->addr1))
ecdfa446 300 return;
65a43784 301
f38d223b 302 if (tcb_desc->bdhcp || ieee->CntAfterLink < 2)
94a79942 303 return;
65a43784 304
94a79942
LF
305 if (pHTInfo->IOTAction & HT_IOT_ACT_TX_NO_AGGREGATION)
306 return;
65a43784 307
94a79942 308 if (!ieee->GetNmodeSupportBySecCfg(ieee->dev))
ecdfa446 309 return;
f38d223b
LF
310 if (pHTInfo->bCurrentAMPDUEnable) {
311 if (!GetTs(ieee, (struct ts_common_info **)(&pTxTs), hdr->addr1,
312 skb->priority, TX_DIR, true)) {
d69d2054 313 netdev_info(ieee->dev, "%s: can't get TS\n", __func__);
ecdfa446
GKH
314 return;
315 }
f38d223b
LF
316 if (pTxTs->TxAdmittedBARecord.bValid == false) {
317 if (ieee->wpa_ie_len && (ieee->pairwise_key_type ==
318 KEY_TYPE_NA)) {
94a79942 319 ;
f38d223b 320 } else if (tcb_desc->bdhcp == 1) {
94a79942 321 ;
f38d223b 322 } else if (!pTxTs->bDisable_AddBa) {
94a79942
LF
323 TsStartAddBaProcess(ieee, pTxTs);
324 }
ecdfa446 325 goto FORCED_AGG_SETTING;
94a79942 326 } else if (pTxTs->bUsingBa == false) {
f38d223b
LF
327 if (SN_LESS(pTxTs->TxAdmittedBARecord.BaStartSeqCtrl.field.SeqNum,
328 (pTxTs->TxCurSeq+1)%4096))
ecdfa446
GKH
329 pTxTs->bUsingBa = true;
330 else
331 goto FORCED_AGG_SETTING;
332 }
94a79942 333 if (ieee->iw_mode == IW_MODE_INFRA) {
ecdfa446
GKH
334 tcb_desc->bAMPDUEnable = true;
335 tcb_desc->ampdu_factor = pHTInfo->CurrentAMPDUFactor;
336 tcb_desc->ampdu_density = pHTInfo->CurrentMPDUDensity;
337 }
338 }
339FORCED_AGG_SETTING:
94a79942 340 switch (pHTInfo->ForcedAMPDUMode) {
f38d223b
LF
341 case HT_AGG_AUTO:
342 break;
343
344 case HT_AGG_FORCE_ENABLE:
345 tcb_desc->bAMPDUEnable = true;
346 tcb_desc->ampdu_density = pHTInfo->ForcedMPDUDensity;
347 tcb_desc->ampdu_factor = pHTInfo->ForcedAMPDUFactor;
348 break;
349
350 case HT_AGG_FORCE_DISABLE:
351 tcb_desc->bAMPDUEnable = false;
352 tcb_desc->ampdu_density = 0;
353 tcb_desc->ampdu_factor = 0;
354 break;
ecdfa446 355 }
ecdfa446
GKH
356}
357
ec0dc6be 358static void rtllib_qurey_ShortPreambleMode(struct rtllib_device *ieee,
f38d223b 359 struct cb_desc *tcb_desc)
ecdfa446
GKH
360{
361 tcb_desc->bUseShortPreamble = false;
362 if (tcb_desc->data_rate == 2)
ecdfa446 363 return;
f38d223b
LF
364 else if (ieee->current_network.capability &
365 WLAN_CAPABILITY_SHORT_PREAMBLE)
ecdfa446 366 tcb_desc->bUseShortPreamble = true;
ecdfa446 367}
94a79942 368
ec0dc6be 369static void rtllib_query_HTCapShortGI(struct rtllib_device *ieee,
f38d223b 370 struct cb_desc *tcb_desc)
ecdfa446 371{
7796d93e 372 struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
ecdfa446 373
94a79942 374 tcb_desc->bUseShortGI = false;
ecdfa446 375
f38d223b 376 if (!pHTInfo->bCurrentHTSupport || !pHTInfo->bEnableHT)
ecdfa446
GKH
377 return;
378
f38d223b 379 if (pHTInfo->bForcedShortGI) {
ecdfa446
GKH
380 tcb_desc->bUseShortGI = true;
381 return;
382 }
383
f38d223b 384 if ((pHTInfo->bCurBW40MHz == true) && pHTInfo->bCurShortGI40MHz)
ecdfa446 385 tcb_desc->bUseShortGI = true;
f38d223b 386 else if ((pHTInfo->bCurBW40MHz == false) && pHTInfo->bCurShortGI20MHz)
ecdfa446
GKH
387 tcb_desc->bUseShortGI = true;
388}
389
ec0dc6be
LF
390static void rtllib_query_BandwidthMode(struct rtllib_device *ieee,
391 struct cb_desc *tcb_desc)
ecdfa446 392{
7796d93e 393 struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
ecdfa446
GKH
394
395 tcb_desc->bPacketBW = false;
396
f38d223b 397 if (!pHTInfo->bCurrentHTSupport || !pHTInfo->bEnableHT)
ecdfa446
GKH
398 return;
399
94a79942 400 if (tcb_desc->bMulticast || tcb_desc->bBroadcast)
ecdfa446
GKH
401 return;
402
f38d223b 403 if ((tcb_desc->data_rate & 0x80) == 0)
ecdfa446 404 return;
f38d223b
LF
405 if (pHTInfo->bCurBW40MHz && pHTInfo->bCurTxBW40MHz &&
406 !ieee->bandwidth_auto_switch.bforced_tx20Mhz)
ecdfa446 407 tcb_desc->bPacketBW = true;
ecdfa446 408}
94a79942 409
ec0dc6be
LF
410static void rtllib_query_protectionmode(struct rtllib_device *ieee,
411 struct cb_desc *tcb_desc,
412 struct sk_buff *skb)
ecdfa446 413{
285b7c00
MK
414 struct rt_hi_throughput *pHTInfo;
415
ecdfa446 416 tcb_desc->bRTSSTBC = false;
94a79942
LF
417 tcb_desc->bRTSUseShortGI = false;
418 tcb_desc->bCTSEnable = false;
419 tcb_desc->RTSSC = 0;
420 tcb_desc->bRTSBW = false;
ecdfa446 421
94a79942 422 if (tcb_desc->bBroadcast || tcb_desc->bMulticast)
ecdfa446
GKH
423 return;
424
94a79942 425 if (is_broadcast_ether_addr(skb->data+16))
ecdfa446
GKH
426 return;
427
f38d223b
LF
428 if (ieee->mode < IEEE_N_24G) {
429 if (skb->len > ieee->rts) {
ecdfa446
GKH
430 tcb_desc->bRTSEnable = true;
431 tcb_desc->rts_rate = MGN_24M;
f38d223b 432 } else if (ieee->current_network.buseprotection) {
ecdfa446
GKH
433 tcb_desc->bRTSEnable = true;
434 tcb_desc->bCTSEnable = true;
435 tcb_desc->rts_rate = MGN_24M;
436 }
ecdfa446 437 return;
285b7c00 438 }
3a6b70c3 439
285b7c00
MK
440 pHTInfo = ieee->pHTInfo;
441
442 while (true) {
443 if (pHTInfo->IOTAction & HT_IOT_ACT_FORCED_CTS2SELF) {
444 tcb_desc->bCTSEnable = true;
445 tcb_desc->rts_rate = MGN_24M;
446 tcb_desc->bRTSEnable = true;
447 break;
448 } else if (pHTInfo->IOTAction & (HT_IOT_ACT_FORCED_RTS |
449 HT_IOT_ACT_PURE_N_MODE)) {
450 tcb_desc->bRTSEnable = true;
451 tcb_desc->rts_rate = MGN_24M;
452 break;
453 }
454 if (ieee->current_network.buseprotection) {
455 tcb_desc->bRTSEnable = true;
456 tcb_desc->bCTSEnable = true;
457 tcb_desc->rts_rate = MGN_24M;
458 break;
459 }
460 if (pHTInfo->bCurrentHTSupport && pHTInfo->bEnableHT) {
461 u8 HTOpMode = pHTInfo->CurrentOpMode;
462
463 if ((pHTInfo->bCurBW40MHz && (HTOpMode == 2 ||
464 HTOpMode == 3)) ||
465 (!pHTInfo->bCurBW40MHz && HTOpMode == 3)) {
94a79942 466 tcb_desc->rts_rate = MGN_24M;
ecdfa446
GKH
467 tcb_desc->bRTSEnable = true;
468 break;
469 }
ecdfa446 470 }
285b7c00
MK
471 if (skb->len > ieee->rts) {
472 tcb_desc->rts_rate = MGN_24M;
473 tcb_desc->bRTSEnable = true;
474 break;
475 }
476 if (tcb_desc->bAMPDUEnable) {
477 tcb_desc->rts_rate = MGN_24M;
478 tcb_desc->bRTSEnable = false;
479 break;
480 }
481 goto NO_PROTECTION;
ecdfa446
GKH
482 }
483 if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
484 tcb_desc->bUseShortPreamble = true;
94a79942 485 if (ieee->iw_mode == IW_MODE_MASTER)
fc00af0c 486 goto NO_PROTECTION;
ecdfa446
GKH
487 return;
488NO_PROTECTION:
489 tcb_desc->bRTSEnable = false;
490 tcb_desc->bCTSEnable = false;
f38d223b 491 tcb_desc->rts_rate = 0;
ecdfa446 492 tcb_desc->RTSSC = 0;
f38d223b 493 tcb_desc->bRTSBW = false;
ecdfa446
GKH
494}
495
496
ec0dc6be
LF
497static void rtllib_txrate_selectmode(struct rtllib_device *ieee,
498 struct cb_desc *tcb_desc)
ecdfa446 499{
94a79942 500 if (ieee->bTxDisableRateFallBack)
ecdfa446
GKH
501 tcb_desc->bTxDisableRateFallBack = true;
502
94a79942 503 if (ieee->bTxUseDriverAssingedRate)
ecdfa446 504 tcb_desc->bTxUseDriverAssingedRate = true;
f38d223b
LF
505 if (!tcb_desc->bTxDisableRateFallBack ||
506 !tcb_desc->bTxUseDriverAssingedRate) {
507 if (ieee->iw_mode == IW_MODE_INFRA ||
508 ieee->iw_mode == IW_MODE_ADHOC)
ecdfa446
GKH
509 tcb_desc->RATRIndex = 0;
510 }
511}
512
511998ea
MK
513static u16 rtllib_query_seqnum(struct rtllib_device *ieee, struct sk_buff *skb,
514 u8 *dst)
ecdfa446 515{
94a79942
LF
516 u16 seqnum = 0;
517
14fc4235 518 if (is_multicast_ether_addr(dst))
94a79942 519 return 0;
f38d223b 520 if (IsQoSDataFrame(skb->data)) {
60554f2b 521 struct tx_ts_record *pTS = NULL;
3a6b70c3 522
f38d223b
LF
523 if (!GetTs(ieee, (struct ts_common_info **)(&pTS), dst,
524 skb->priority, TX_DIR, true))
94a79942 525 return 0;
94a79942 526 seqnum = pTS->TxCurSeq;
ecdfa446 527 pTS->TxCurSeq = (pTS->TxCurSeq+1)%4096;
94a79942
LF
528 return seqnum;
529 }
530 return 0;
531}
532
533static int wme_downgrade_ac(struct sk_buff *skb)
534{
535 switch (skb->priority) {
f38d223b
LF
536 case 6:
537 case 7:
538 skb->priority = 5; /* VO -> VI */
539 return 0;
540 case 4:
541 case 5:
542 skb->priority = 3; /* VI -> BE */
543 return 0;
544 case 0:
545 case 3:
546 skb->priority = 1; /* BE -> BK */
547 return 0;
548 default:
549 return -1;
ecdfa446
GKH
550 }
551}
552
a8f4f33a
MK
553static u8 rtllib_current_rate(struct rtllib_device *ieee)
554{
555 if (ieee->mode & IEEE_MODE_MASK)
556 return ieee->rate;
557
558 if (ieee->HTCurrentOperaRate)
559 return ieee->HTCurrentOperaRate;
560 else
561 return ieee->rate & 0x7F;
562}
563
511998ea 564static int rtllib_xmit_inter(struct sk_buff *skb, struct net_device *dev)
ecdfa446 565{
f38d223b
LF
566 struct rtllib_device *ieee = (struct rtllib_device *)
567 netdev_priv_rsl(dev);
94a79942
LF
568 struct rtllib_txb *txb = NULL;
569 struct rtllib_hdr_3addrqos *frag_hdr;
ecdfa446
GKH
570 int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
571 unsigned long flags;
572 struct net_device_stats *stats = &ieee->stats;
573 int ether_type = 0, encrypt;
574 int bytes, fc, qos_ctl = 0, hdr_len;
575 struct sk_buff *skb_frag;
94a79942 576 struct rtllib_hdr_3addrqos header = { /* Ensure zero initialized */
ecdfa446
GKH
577 .duration_id = 0,
578 .seq_ctl = 0,
579 .qos_ctl = 0
580 };
ecdfa446 581 int qos_actived = ieee->current_network.qos_data.active;
06c11107
MK
582 u8 dest[ETH_ALEN];
583 u8 src[ETH_ALEN];
32c44cb5 584 struct lib80211_crypt_data *crypt = NULL;
3b83db43 585 struct cb_desc *tcb_desc;
94a79942
LF
586 u8 bIsMulticast = false;
587 u8 IsAmsdu = false;
f38d223b 588 bool bdhcp = false;
ecdfa446
GKH
589
590 spin_lock_irqsave(&ieee->lock, flags);
591
cd017123 592 /* If there is no driver handler to take the TXB, don't bother
14b40d92
MK
593 * creating it...
594 */
f38d223b
LF
595 if ((!ieee->hard_start_xmit && !(ieee->softmac_features &
596 IEEE_SOFTMAC_TX_QUEUE)) ||
597 ((!ieee->softmac_data_hard_start_xmit &&
598 (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
d69d2054 599 netdev_warn(ieee->dev, "No xmit handler.\n");
ecdfa446
GKH
600 goto success;
601 }
602
603
f38d223b 604 if (likely(ieee->raw_tx == 0)) {
ecdfa446 605 if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
d69d2054
MK
606 netdev_warn(ieee->dev, "skb too small (%d).\n",
607 skb->len);
ecdfa446
GKH
608 goto success;
609 }
94a79942 610 /* Save source and destination addresses */
b57ceb19
MK
611 ether_addr_copy(dest, skb->data);
612 ether_addr_copy(src, skb->data + ETH_ALEN);
ecdfa446
GKH
613
614 memset(skb->cb, 0, sizeof(skb->cb));
615 ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
616
f38d223b 617 if (ieee->iw_mode == IW_MODE_MONITOR) {
94a79942
LF
618 txb = rtllib_alloc_txb(1, skb->len, GFP_ATOMIC);
619 if (unlikely(!txb)) {
d69d2054
MK
620 netdev_warn(ieee->dev,
621 "Could not allocate TXB\n");
94a79942
LF
622 goto failed;
623 }
ecdfa446 624
94a79942 625 txb->encrypted = 0;
198e0d17 626 txb->payload_size = cpu_to_le16(skb->len);
f38d223b
LF
627 memcpy(skb_put(txb->fragments[0], skb->len), skb->data,
628 skb->len);
ecdfa446 629
ecdfa446
GKH
630 goto success;
631 }
ecdfa446 632
55dc4eb3 633 if (skb->len > 282) {
4c29207a 634 if (ether_type == ETH_P_IP) {
f38d223b
LF
635 const struct iphdr *ip = (struct iphdr *)
636 ((u8 *)skb->data+14);
4c29207a 637 if (ip->protocol == IPPROTO_UDP) {
f38d223b
LF
638 struct udphdr *udp;
639
640 udp = (struct udphdr *)((u8 *)ip +
641 (ip->ihl << 2));
642 if (((((u8 *)udp)[1] == 68) &&
643 (((u8 *)udp)[3] == 67)) ||
644 ((((u8 *)udp)[1] == 67) &&
645 (((u8 *)udp)[3] == 68))) {
65a43784 646 bdhcp = true;
94a79942 647 ieee->LPSDelayCnt = 200;
65a43784 648 }
649 }
4c29207a 650 } else if (ether_type == ETH_P_ARP) {
d69d2054
MK
651 netdev_info(ieee->dev,
652 "=================>DHCP Protocol start tx ARP pkt!!\n");
94a79942 653 bdhcp = true;
f38d223b
LF
654 ieee->LPSDelayCnt =
655 ieee->current_network.tim.tim_count;
65a43784 656 }
94a79942 657 }
65a43784 658
94a79942 659 skb->priority = rtllib_classify(skb, IsAmsdu);
0ddcf5fd 660 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
94a79942
LF
661 encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
662 ieee->host_encrypt && crypt && crypt->ops;
663 if (!encrypt && ieee->ieee802_1x &&
f38d223b 664 ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
94a79942
LF
665 stats->tx_dropped++;
666 goto success;
667 }
94a79942
LF
668 if (crypt && !encrypt && ether_type == ETH_P_PAE) {
669 struct eapol *eap = (struct eapol *)(skb->data +
f38d223b
LF
670 sizeof(struct ethhdr) - SNAP_SIZE -
671 sizeof(u16));
e77c752f
MK
672 netdev_dbg(ieee->dev,
673 "TX: IEEE 802.11 EAPOL frame: %s\n",
674 eap_get_type(eap->type));
94a79942 675 }
ecdfa446 676
94a79942
LF
677 /* Advance the SKB to the start of the payload */
678 skb_pull(skb, sizeof(struct ethhdr));
ecdfa446 679
f38d223b 680 /* Determine total amount of storage required for TXB packets */
94a79942 681 bytes = skb->len + SNAP_SIZE + sizeof(u16);
ecdfa446
GKH
682
683 if (encrypt)
94a79942 684 fc = RTLLIB_FTYPE_DATA | RTLLIB_FCTL_WEP;
ecdfa446 685 else
94a79942 686 fc = RTLLIB_FTYPE_DATA;
ecdfa446 687
94a79942
LF
688 if (qos_actived)
689 fc |= RTLLIB_STYPE_QOS_DATA;
ecdfa446 690 else
94a79942 691 fc |= RTLLIB_STYPE_DATA;
ecdfa446
GKH
692
693 if (ieee->iw_mode == IW_MODE_INFRA) {
94a79942 694 fc |= RTLLIB_FCTL_TODS;
ecdfa446 695 /* To DS: Addr1 = BSSID, Addr2 = SA,
14b40d92
MK
696 * Addr3 = DA
697 */
b57ceb19
MK
698 ether_addr_copy(header.addr1,
699 ieee->current_network.bssid);
700 ether_addr_copy(header.addr2, src);
94a79942 701 if (IsAmsdu)
b57ceb19
MK
702 ether_addr_copy(header.addr3,
703 ieee->current_network.bssid);
94a79942 704 else
b57ceb19 705 ether_addr_copy(header.addr3, dest);
ecdfa446
GKH
706 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
707 /* not From/To DS: Addr1 = DA, Addr2 = SA,
14b40d92
MK
708 * Addr3 = BSSID
709 */
b57ceb19
MK
710 ether_addr_copy(header.addr1, dest);
711 ether_addr_copy(header.addr2, src);
712 ether_addr_copy(header.addr3,
713 ieee->current_network.bssid);
ecdfa446
GKH
714 }
715
14fc4235 716 bIsMulticast = is_multicast_ether_addr(header.addr1);
94a79942 717
f38d223b 718 header.frame_ctl = cpu_to_le16(fc);
ecdfa446
GKH
719
720 /* Determine fragmentation size based on destination (multicast
14b40d92
MK
721 * and broadcast are not fragmented)
722 */
94a79942 723 if (bIsMulticast) {
ecdfa446
GKH
724 frag_size = MAX_FRAG_THRESHOLD;
725 qos_ctl |= QOS_CTL_NOTCONTAIN_ACK;
94a79942
LF
726 } else {
727 frag_size = ieee->fts;
ecdfa446
GKH
728 qos_ctl = 0;
729 }
730
94a79942
LF
731 if (qos_actived) {
732 hdr_len = RTLLIB_3ADDR_LEN + 2;
733
f38d223b
LF
734 /* in case we are a client verify acm is not set for this ac */
735 while (unlikely(ieee->wmm_acm & (0x01 << skb->priority))) {
d69d2054
MK
736 netdev_info(ieee->dev, "skb->priority = %x\n",
737 skb->priority);
f38d223b
LF
738 if (wme_downgrade_ac(skb))
739 break;
d69d2054 740 netdev_info(ieee->dev, "converted skb->priority = %x\n",
f38d223b
LF
741 skb->priority);
742 }
743 qos_ctl |= skb->priority;
744 header.qos_ctl = cpu_to_le16(qos_ctl & RTLLIB_QOS_TID);
ecdfa446 745 } else {
94a79942 746 hdr_len = RTLLIB_3ADDR_LEN;
ecdfa446
GKH
747 }
748 /* Determine amount of payload per fragment. Regardless of if
f38d223b
LF
749 * this stack is providing the full 802.11 header, one will
750 * eventually be affixed to this fragment -- so we must account
14b40d92
MK
751 * for it when determining the amount of payload space.
752 */
ecdfa446
GKH
753 bytes_per_frag = frag_size - hdr_len;
754 if (ieee->config &
f38d223b 755 (CFG_RTLLIB_COMPUTE_FCS | CFG_RTLLIB_RESERVE_FCS))
94a79942 756 bytes_per_frag -= RTLLIB_FCS_LEN;
ecdfa446 757
cd017123 758 /* Each fragment may need to have room for encrypting
14b40d92
MK
759 * pre/postfix
760 */
94a79942 761 if (encrypt) {
32c44cb5
SM
762 bytes_per_frag -= crypt->ops->extra_mpdu_prefix_len +
763 crypt->ops->extra_mpdu_postfix_len +
764 crypt->ops->extra_msdu_prefix_len +
765 crypt->ops->extra_msdu_postfix_len;
94a79942 766 }
ecdfa446 767 /* Number of fragments is the total bytes_per_frag /
14b40d92
MK
768 * payload_per_fragment
769 */
ecdfa446
GKH
770 nr_frags = bytes / bytes_per_frag;
771 bytes_last_frag = bytes % bytes_per_frag;
772 if (bytes_last_frag)
773 nr_frags++;
774 else
775 bytes_last_frag = bytes_per_frag;
776
f38d223b
LF
777 /* When we allocate the TXB we allocate enough space for the
778 * reserve and full fragment bytes (bytes_per_frag doesn't
14b40d92
MK
779 * include prefix, postfix, header, FCS, etc.)
780 */
f38d223b
LF
781 txb = rtllib_alloc_txb(nr_frags, frag_size +
782 ieee->tx_headroom, GFP_ATOMIC);
ecdfa446 783 if (unlikely(!txb)) {
d69d2054 784 netdev_warn(ieee->dev, "Could not allocate TXB\n");
ecdfa446
GKH
785 goto failed;
786 }
787 txb->encrypted = encrypt;
198e0d17 788 txb->payload_size = cpu_to_le16(bytes);
ecdfa446 789
94a79942 790 if (qos_actived)
ecdfa446 791 txb->queue_index = UP2AC(skb->priority);
f38d223b
LF
792 else
793 txb->queue_index = WME_AC_BE;
ecdfa446 794
ecdfa446
GKH
795 for (i = 0; i < nr_frags; i++) {
796 skb_frag = txb->fragments[i];
f38d223b
LF
797 tcb_desc = (struct cb_desc *)(skb_frag->cb +
798 MAX_DEV_ADDR_SIZE);
799 if (qos_actived) {
94a79942 800 skb_frag->priority = skb->priority;
ecdfa446
GKH
801 tcb_desc->queue_index = UP2AC(skb->priority);
802 } else {
94a79942
LF
803 skb_frag->priority = WME_AC_BE;
804 tcb_desc->queue_index = WME_AC_BE;
ecdfa446
GKH
805 }
806 skb_reserve(skb_frag, ieee->tx_headroom);
807
f38d223b 808 if (encrypt) {
ecdfa446
GKH
809 if (ieee->hwsec_active)
810 tcb_desc->bHwSec = 1;
811 else
812 tcb_desc->bHwSec = 0;
f38d223b 813 skb_reserve(skb_frag,
32c44cb5
SM
814 crypt->ops->extra_mpdu_prefix_len +
815 crypt->ops->extra_msdu_prefix_len);
94a79942 816 } else {
ecdfa446
GKH
817 tcb_desc->bHwSec = 0;
818 }
f38d223b
LF
819 frag_hdr = (struct rtllib_hdr_3addrqos *)
820 skb_put(skb_frag, hdr_len);
ecdfa446
GKH
821 memcpy(frag_hdr, &header, hdr_len);
822
f38d223b 823 /* If this is not the last fragment, then add the
14b40d92
MK
824 * MOREFRAGS bit to the frame control
825 */
ecdfa446
GKH
826 if (i != nr_frags - 1) {
827 frag_hdr->frame_ctl = cpu_to_le16(
94a79942 828 fc | RTLLIB_FCTL_MOREFRAGS);
ecdfa446
GKH
829 bytes = bytes_per_frag;
830
831 } else {
f38d223b 832 /* The last fragment has the remaining length */
ecdfa446
GKH
833 bytes = bytes_last_frag;
834 }
f38d223b
LF
835 if ((qos_actived) && (!bIsMulticast)) {
836 frag_hdr->seq_ctl =
198e0d17
RK
837 cpu_to_le16(rtllib_query_seqnum(ieee, skb_frag,
838 header.addr1));
f38d223b 839 frag_hdr->seq_ctl =
86005e16 840 cpu_to_le16(le16_to_cpu(frag_hdr->seq_ctl)<<4 | i);
ecdfa446 841 } else {
f38d223b
LF
842 frag_hdr->seq_ctl =
843 cpu_to_le16(ieee->seq_ctrl[0]<<4 | i);
ecdfa446 844 }
ecdfa446
GKH
845 /* Put a SNAP header on the first fragment */
846 if (i == 0) {
94a79942 847 rtllib_put_snap(
f38d223b
LF
848 skb_put(skb_frag, SNAP_SIZE +
849 sizeof(u16)), ether_type);
ecdfa446
GKH
850 bytes -= SNAP_SIZE + sizeof(u16);
851 }
852
853 memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
854
855 /* Advance the SKB... */
856 skb_pull(skb, bytes);
857
f38d223b
LF
858 /* Encryption routine will move the header forward in
859 * order to insert the IV between the header and the
14b40d92
MK
860 * payload
861 */
ecdfa446 862 if (encrypt)
f38d223b
LF
863 rtllib_encrypt_fragment(ieee, skb_frag,
864 hdr_len);
ecdfa446 865 if (ieee->config &
f38d223b 866 (CFG_RTLLIB_COMPUTE_FCS | CFG_RTLLIB_RESERVE_FCS))
ecdfa446
GKH
867 skb_put(skb_frag, 4);
868 }
869
94a79942 870 if ((qos_actived) && (!bIsMulticast)) {
f38d223b
LF
871 if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
872 ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
873 else
874 ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
ecdfa446 875 } else {
f38d223b
LF
876 if (ieee->seq_ctrl[0] == 0xFFF)
877 ieee->seq_ctrl[0] = 0;
878 else
879 ieee->seq_ctrl[0]++;
ecdfa446 880 }
f38d223b 881 } else {
94a79942 882 if (unlikely(skb->len < sizeof(struct rtllib_hdr_3addr))) {
d69d2054
MK
883 netdev_warn(ieee->dev, "skb too small (%d).\n",
884 skb->len);
ecdfa446
GKH
885 goto success;
886 }
887
94a79942 888 txb = rtllib_alloc_txb(1, skb->len, GFP_ATOMIC);
f38d223b 889 if (!txb) {
d69d2054 890 netdev_warn(ieee->dev, "Could not allocate TXB\n");
ecdfa446
GKH
891 goto failed;
892 }
893
894 txb->encrypted = 0;
198e0d17 895 txb->payload_size = cpu_to_le16(skb->len);
f38d223b
LF
896 memcpy(skb_put(txb->fragments[0], skb->len), skb->data,
897 skb->len);
ecdfa446
GKH
898 }
899
900 success:
f38d223b
LF
901 if (txb) {
902 struct cb_desc *tcb_desc = (struct cb_desc *)
903 (txb->fragments[0]->cb + MAX_DEV_ADDR_SIZE);
ecdfa446 904 tcb_desc->bTxEnableFwCalcDur = 1;
94a79942 905 tcb_desc->priority = skb->priority;
65a43784 906
94a79942 907 if (ether_type == ETH_P_PAE) {
f38d223b
LF
908 if (ieee->pHTInfo->IOTAction &
909 HT_IOT_ACT_WA_IOT_Broadcom) {
910 tcb_desc->data_rate =
911 MgntQuery_TxRateExcludeCCKRates(ieee);
94a79942 912 tcb_desc->bTxDisableRateFallBack = false;
f38d223b 913 } else {
94a79942 914 tcb_desc->data_rate = ieee->basic_rate;
65a43784 915 tcb_desc->bTxDisableRateFallBack = 1;
94a79942
LF
916 }
917
65a43784 918
919 tcb_desc->RATRIndex = 7;
920 tcb_desc->bTxUseDriverAssingedRate = 1;
94a79942
LF
921 } else {
922 if (is_multicast_ether_addr(header.addr1))
923 tcb_desc->bMulticast = 1;
924 if (is_broadcast_ether_addr(header.addr1))
925 tcb_desc->bBroadcast = 1;
94a79942 926 rtllib_txrate_selectmode(ieee, tcb_desc);
f38d223b 927 if (tcb_desc->bMulticast || tcb_desc->bBroadcast)
94a79942
LF
928 tcb_desc->data_rate = ieee->basic_rate;
929 else
a8f4f33a 930 tcb_desc->data_rate = rtllib_current_rate(ieee);
f38d223b 931
4bb01423 932 if (bdhcp) {
f38d223b
LF
933 if (ieee->pHTInfo->IOTAction &
934 HT_IOT_ACT_WA_IOT_Broadcom) {
935 tcb_desc->data_rate =
936 MgntQuery_TxRateExcludeCCKRates(ieee);
94a79942 937 tcb_desc->bTxDisableRateFallBack = false;
f38d223b 938 } else {
94a79942
LF
939 tcb_desc->data_rate = MGN_1M;
940 tcb_desc->bTxDisableRateFallBack = 1;
941 }
65a43784 942
943
94a79942
LF
944 tcb_desc->RATRIndex = 7;
945 tcb_desc->bTxUseDriverAssingedRate = 1;
946 tcb_desc->bdhcp = 1;
947 }
948
949 rtllib_qurey_ShortPreambleMode(ieee, tcb_desc);
f38d223b
LF
950 rtllib_tx_query_agg_cap(ieee, txb->fragments[0],
951 tcb_desc);
94a79942
LF
952 rtllib_query_HTCapShortGI(ieee, tcb_desc);
953 rtllib_query_BandwidthMode(ieee, tcb_desc);
f38d223b
LF
954 rtllib_query_protectionmode(ieee, tcb_desc,
955 txb->fragments[0]);
94a79942 956 }
ecdfa446
GKH
957 }
958 spin_unlock_irqrestore(&ieee->lock, flags);
959 dev_kfree_skb_any(skb);
960 if (txb) {
f38d223b 961 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE) {
94a79942 962 dev->stats.tx_packets++;
83efd529 963 dev->stats.tx_bytes += le16_to_cpu(txb->payload_size);
94a79942 964 rtllib_softmac_xmit(txb, ieee);
f38d223b 965 } else {
94a79942 966 if ((*ieee->hard_start_xmit)(txb, dev) == 0) {
ecdfa446 967 stats->tx_packets++;
83efd529 968 stats->tx_bytes += le16_to_cpu(txb->payload_size);
ecdfa446
GKH
969 return 0;
970 }
94a79942 971 rtllib_txb_free(txb);
ecdfa446
GKH
972 }
973 }
974
975 return 0;
976
977 failed:
978 spin_unlock_irqrestore(&ieee->lock, flags);
979 netif_stop_queue(dev);
980 stats->tx_errors++;
981 return 1;
982
983}
94a79942
LF
984int rtllib_xmit(struct sk_buff *skb, struct net_device *dev)
985{
986 memset(skb->cb, 0, sizeof(skb->cb));
987 return rtllib_xmit_inter(skb, dev);
988}
3b28499c 989EXPORT_SYMBOL(rtllib_xmit);
This page took 0.743963 seconds and 5 git commands to generate.