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