staging: vt6655: mac80211 conversion: s_vFillRTSHead convert to using struct ieee8021...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6655 / rxtx.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: rxtx.c
20  *
21  * Purpose: handle WMAC/802.3/802.11 rx & tx functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: May 20, 2003
26  *
27  * Functions:
28  *      s_vGenerateTxParameter - Generate tx dma required parameter.
29  *      vGenerateMACHeader - Translate 802.3 to 802.11 header
30  *      cbGetFragCount - Calculate fragment number count
31  *      csBeacon_xmit - beacon tx function
32  *      csMgmt_xmit - management tx function
33  *      s_cbFillTxBufHead - fulfill tx dma buffer header
34  *      s_uGetDataDuration - get tx data required duration
35  *      s_uFillDataHead- fulfill tx data duration header
36  *      s_uGetRTSCTSDuration- get rtx/cts required duration
37  *      s_uGetRTSCTSRsvTime- get rts/cts reserved time
38  *      s_uGetTxRsvTime- get frame reserved time
39  *      s_vFillCTSHead- fulfill CTS ctl header
40  *      s_vFillFragParameter- Set fragment ctl parameter.
41  *      s_vFillRTSHead- fulfill RTS ctl header
42  *      s_vFillTxKey- fulfill tx encrypt key
43  *      s_vSWencryption- Software encrypt header
44  *      vDMA0_tx_80211- tx 802.11 frame via dma0
45  *      vGenerateFIFOHeader- Generate tx FIFO ctl header
46  *
47  * Revision History:
48  *
49  */
50
51 #include "device.h"
52 #include "rxtx.h"
53 #include "tether.h"
54 #include "card.h"
55 #include "bssdb.h"
56 #include "mac.h"
57 #include "baseband.h"
58 #include "michael.h"
59 #include "tkip.h"
60 #include "tcrc.h"
61 #include "wctl.h"
62 #include "wroute.h"
63 #include "hostap.h"
64 #include "rf.h"
65
66 /*---------------------  Static Definitions -------------------------*/
67
68 /*---------------------  Static Classes  ----------------------------*/
69
70 /*---------------------  Static Variables  --------------------------*/
71
72 /*---------------------  Static Functions  --------------------------*/
73
74 /*---------------------  Static Definitions -------------------------*/
75 #define CRITICAL_PACKET_LEN      256    // if packet size < 256 -> in-direct send
76                                         //    packet size >= 256 -> direct send
77
78 static const unsigned short wTimeStampOff[2][MAX_RATE] = {
79         {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23}, // Long Preamble
80         {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23}, // Short Preamble
81 };
82
83 static const unsigned short wFB_Opt0[2][5] = {
84         {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, // fallback_rate0
85         {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, // fallback_rate1
86 };
87 static const unsigned short wFB_Opt1[2][5] = {
88         {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, // fallback_rate0
89         {RATE_6M , RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, // fallback_rate1
90 };
91
92 #define RTSDUR_BB       0
93 #define RTSDUR_BA       1
94 #define RTSDUR_AA       2
95 #define CTSDUR_BA       3
96 #define RTSDUR_BA_F0    4
97 #define RTSDUR_AA_F0    5
98 #define RTSDUR_BA_F1    6
99 #define RTSDUR_AA_F1    7
100 #define CTSDUR_BA_F0    8
101 #define CTSDUR_BA_F1    9
102 #define DATADUR_B       10
103 #define DATADUR_A       11
104 #define DATADUR_A_F0    12
105 #define DATADUR_A_F1    13
106
107 /*---------------------  Static Functions  --------------------------*/
108
109 static
110 void
111 s_vFillTxKey(
112         struct vnt_private *pDevice,
113         unsigned char *pbyBuf,
114         unsigned char *pbyIVHead,
115         PSKeyItem  pTransmitKey,
116         unsigned char *pbyHdrBuf,
117         unsigned short wPayloadLen,
118         unsigned char *pMICHDR
119 );
120
121 static
122 void
123 s_vFillRTSHead(
124         struct vnt_private *pDevice,
125         unsigned char byPktType,
126         void *pvRTS,
127         unsigned int    cbFrameLength,
128         bool bNeedAck,
129         bool bDisCRC,
130         struct ieee80211_hdr *hdr,
131         unsigned short wCurrentRate,
132         unsigned char byFBOption
133 );
134
135 static
136 void
137 s_vGenerateTxParameter(
138         struct vnt_private *pDevice,
139         unsigned char byPktType,
140         void *pTxBufHead,
141         void *pvRrvTime,
142         void *pvRTS,
143         void *pvCTS,
144         unsigned int    cbFrameSize,
145         bool bNeedACK,
146         unsigned int    uDMAIdx,
147         void *psEthHeader,
148         unsigned short wCurrentRate
149 );
150
151 static unsigned int
152 s_cbFillTxBufHead(struct vnt_private *pDevice, unsigned char byPktType,
153                   unsigned char *pbyTxBufferAddr, unsigned int cbFrameBodySize,
154                   unsigned int uDMAIdx, PSTxDesc pHeadTD,
155                   PSEthernetHeader psEthHeader, unsigned char *pPacket,
156                   bool bNeedEncrypt, PSKeyItem pTransmitKey,
157                   unsigned int uNodeIndex, unsigned int *puMACfragNum);
158
159 static
160 __le16
161 s_uFillDataHead(
162         struct vnt_private *pDevice,
163         unsigned char byPktType,
164         void *pTxDataHead,
165         unsigned int cbFrameLength,
166         unsigned int uDMAIdx,
167         bool bNeedAck,
168         unsigned int uFragIdx,
169         unsigned int cbLastFragmentSize,
170         unsigned int uMACfragNum,
171         unsigned char byFBOption,
172         unsigned short wCurrentRate
173 );
174
175 /*---------------------  Export Variables  --------------------------*/
176
177 static
178 void
179 s_vFillTxKey(
180         struct vnt_private *pDevice,
181         unsigned char *pbyBuf,
182         unsigned char *pbyIVHead,
183         PSKeyItem  pTransmitKey,
184         unsigned char *pbyHdrBuf,
185         unsigned short wPayloadLen,
186         unsigned char *pMICHDR
187 )
188 {
189         struct vnt_mic_hdr *mic_hdr = (struct vnt_mic_hdr *)pMICHDR;
190         unsigned long *pdwIV = (unsigned long *)pbyIVHead;
191         unsigned long *pdwExtIV = (unsigned long *)((unsigned char *)pbyIVHead+4);
192         PS802_11Header  pMACHeader = (PS802_11Header)pbyHdrBuf;
193         unsigned long dwRevIVCounter;
194         unsigned char byKeyIndex = 0;
195
196         //Fill TXKEY
197         if (pTransmitKey == NULL)
198                 return;
199
200         dwRevIVCounter = cpu_to_le32(pDevice->dwIVCounter);
201         *pdwIV = pDevice->dwIVCounter;
202         byKeyIndex = pTransmitKey->dwKeyIndex & 0xf;
203
204         if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
205                 if (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN) {
206                         memcpy(pDevice->abyPRNG, (unsigned char *)&(dwRevIVCounter), 3);
207                         memcpy(pDevice->abyPRNG+3, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
208                 } else {
209                         memcpy(pbyBuf, (unsigned char *)&(dwRevIVCounter), 3);
210                         memcpy(pbyBuf+3, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
211                         if (pTransmitKey->uKeyLength == WLAN_WEP40_KEYLEN) {
212                                 memcpy(pbyBuf+8, (unsigned char *)&(dwRevIVCounter), 3);
213                                 memcpy(pbyBuf+11, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
214                         }
215                         memcpy(pDevice->abyPRNG, pbyBuf, 16);
216                 }
217                 // Append IV after Mac Header
218                 *pdwIV &= WEP_IV_MASK;//00000000 11111111 11111111 11111111
219                 *pdwIV |= (unsigned long)byKeyIndex << 30;
220                 *pdwIV = cpu_to_le32(*pdwIV);
221                 pDevice->dwIVCounter++;
222                 if (pDevice->dwIVCounter > WEP_IV_MASK)
223                         pDevice->dwIVCounter = 0;
224
225         } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
226                 pTransmitKey->wTSC15_0++;
227                 if (pTransmitKey->wTSC15_0 == 0)
228                         pTransmitKey->dwTSC47_16++;
229
230                 TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
231                             pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16, pDevice->abyPRNG);
232                 memcpy(pbyBuf, pDevice->abyPRNG, 16);
233                 // Make IV
234                 memcpy(pdwIV, pDevice->abyPRNG, 3);
235
236                 *(pbyIVHead+3) = (unsigned char)(((byKeyIndex << 6) & 0xc0) | 0x20); // 0x20 is ExtIV
237                 // Append IV&ExtIV after Mac Header
238                 *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
239                 pr_debug("vFillTxKey()---- pdwExtIV: %lx\n", *pdwExtIV);
240
241         } else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
242                 pTransmitKey->wTSC15_0++;
243                 if (pTransmitKey->wTSC15_0 == 0)
244                         pTransmitKey->dwTSC47_16++;
245
246                 memcpy(pbyBuf, pTransmitKey->abyKey, 16);
247
248                 // Make IV
249                 *pdwIV = 0;
250                 *(pbyIVHead+3) = (unsigned char)(((byKeyIndex << 6) & 0xc0) | 0x20); // 0x20 is ExtIV
251                 *pdwIV |= cpu_to_le16((unsigned short)(pTransmitKey->wTSC15_0));
252                 //Append IV&ExtIV after Mac Header
253                 *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
254
255                 /* MICHDR0 */
256                 mic_hdr->id = 0x59;
257                 mic_hdr->tx_priority = 0;
258                 ether_addr_copy(mic_hdr->mic_addr2, pMACHeader->abyAddr2);
259
260                 /* ccmp pn big endian order */
261                 mic_hdr->ccmp_pn[0] = (u8)(pTransmitKey->dwTSC47_16 >> 24);
262                 mic_hdr->ccmp_pn[1] = (u8)(pTransmitKey->dwTSC47_16 >> 16);
263                 mic_hdr->ccmp_pn[2] = (u8)(pTransmitKey->dwTSC47_16 >> 8);
264                 mic_hdr->ccmp_pn[3] = (u8)pTransmitKey->dwTSC47_16;
265                 mic_hdr->ccmp_pn[4] = (u8)(pTransmitKey->wTSC15_0 >> 8);
266                 mic_hdr->ccmp_pn[5] = (u8)pTransmitKey->wTSC15_0;
267
268                 /* MICHDR1 */
269                 mic_hdr->payload_len = cpu_to_be16(wPayloadLen);
270
271                 if (pDevice->bLongHeader)
272                         mic_hdr->hlen = cpu_to_be16(28);
273                 else
274                         mic_hdr->hlen = cpu_to_be16(22);
275
276                 ether_addr_copy(mic_hdr->addr1, pMACHeader->abyAddr1);
277                 ether_addr_copy(mic_hdr->addr2, pMACHeader->abyAddr2);
278
279                 /* MICHDR2 */
280                 ether_addr_copy(mic_hdr->addr3, pMACHeader->abyAddr3);
281                 mic_hdr->frame_control =
282                                 cpu_to_le16(pMACHeader->wFrameCtl & 0xc78f);
283                 mic_hdr->seq_ctrl = cpu_to_le16(pMACHeader->wSeqCtl & 0xf);
284
285                 if (pDevice->bLongHeader)
286                         ether_addr_copy(mic_hdr->addr4, pMACHeader->abyAddr4);
287         }
288 }
289
290 static
291 void
292 s_vSWencryption(
293         struct vnt_private *pDevice,
294         PSKeyItem           pTransmitKey,
295         unsigned char *pbyPayloadHead,
296         unsigned short wPayloadSize
297 )
298 {
299         unsigned int cbICVlen = 4;
300         unsigned long dwICV = 0xFFFFFFFFL;
301         unsigned long *pdwICV;
302
303         if (pTransmitKey == NULL)
304                 return;
305
306         if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
307                 //=======================================================================
308                 // Append ICV after payload
309                 dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
310                 pdwICV = (unsigned long *)(pbyPayloadHead + wPayloadSize);
311                 // finally, we must invert dwCRC to get the correct answer
312                 *pdwICV = cpu_to_le32(~dwICV);
313                 // RC4 encryption
314                 rc4_init(&pDevice->SBox, pDevice->abyPRNG, pTransmitKey->uKeyLength + 3);
315                 rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
316                 //=======================================================================
317         } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
318                 //=======================================================================
319                 //Append ICV after payload
320                 dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
321                 pdwICV = (unsigned long *)(pbyPayloadHead + wPayloadSize);
322                 // finally, we must invert dwCRC to get the correct answer
323                 *pdwICV = cpu_to_le32(~dwICV);
324                 // RC4 encryption
325                 rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
326                 rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
327                 //=======================================================================
328         }
329 }
330
331 static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
332 {
333         return cpu_to_le16(wTimeStampOff[priv->byPreambleType % 2]
334                                                         [rate % MAX_RATE]);
335 }
336
337 /*byPktType : PK_TYPE_11A     0
338   PK_TYPE_11B     1
339   PK_TYPE_11GB    2
340   PK_TYPE_11GA    3
341 */
342 static
343 unsigned int
344 s_uGetTxRsvTime(
345         struct vnt_private *pDevice,
346         unsigned char byPktType,
347         unsigned int cbFrameLength,
348         unsigned short wRate,
349         bool bNeedAck
350 )
351 {
352         unsigned int uDataTime, uAckTime;
353
354         uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wRate);
355         if (byPktType == PK_TYPE_11B) //llb,CCK mode
356                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (unsigned short)pDevice->byTopCCKBasicRate);
357         else //11g 2.4G OFDM mode & 11a 5G OFDM mode
358                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (unsigned short)pDevice->byTopOFDMBasicRate);
359
360         if (bNeedAck)
361                 return uDataTime + pDevice->uSIFS + uAckTime;
362         else
363                 return uDataTime;
364 }
365
366 static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
367                                     u32 frame_length, u16 rate, bool need_ack)
368 {
369         return cpu_to_le16((u16)s_uGetTxRsvTime(priv, pkt_type,
370                                                 frame_length, rate, need_ack));
371 }
372
373 //byFreqType: 0=>5GHZ 1=>2.4GHZ
374 static
375 __le16
376 s_uGetRTSCTSRsvTime(
377         struct vnt_private *pDevice,
378         unsigned char byRTSRsvType,
379         unsigned char byPktType,
380         unsigned int cbFrameLength,
381         unsigned short wCurrentRate
382 )
383 {
384         unsigned int uRrvTime  , uRTSTime, uCTSTime, uAckTime, uDataTime;
385
386         uRrvTime = uRTSTime = uCTSTime = uAckTime = uDataTime = 0;
387
388         uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wCurrentRate);
389         if (byRTSRsvType == 0) { //RTSTxRrvTime_bb
390                 uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
391                 uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
392         } else if (byRTSRsvType == 1) { //RTSTxRrvTime_ba, only in 2.4GHZ
393                 uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
394                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
395                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
396         } else if (byRTSRsvType == 2) { //RTSTxRrvTime_aa
397                 uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopOFDMBasicRate);
398                 uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
399         } else if (byRTSRsvType == 3) { //CTSTxRrvTime_ba, only in 2.4GHZ
400                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
401                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
402                 uRrvTime = uCTSTime + uAckTime + uDataTime + 2*pDevice->uSIFS;
403                 return cpu_to_le16((u16)uRrvTime);
404         }
405
406         //RTSRrvTime
407         uRrvTime = uRTSTime + uCTSTime + uAckTime + uDataTime + 3*pDevice->uSIFS;
408         return cpu_to_le16((u16)uRrvTime);
409 }
410
411 //byFreqType 0: 5GHz, 1:2.4Ghz
412 static
413 unsigned int
414 s_uGetDataDuration(
415         struct vnt_private *pDevice,
416         unsigned char byDurType,
417         unsigned int cbFrameLength,
418         unsigned char byPktType,
419         unsigned short wRate,
420         bool bNeedAck,
421         unsigned int uFragIdx,
422         unsigned int cbLastFragmentSize,
423         unsigned int uMACfragNum,
424         unsigned char byFBOption
425 )
426 {
427         bool bLastFrag = 0;
428         unsigned int uAckTime = 0, uNextPktTime = 0;
429
430         if (uFragIdx == (uMACfragNum-1))
431                 bLastFrag = 1;
432
433         switch (byDurType) {
434         case DATADUR_B:    //DATADUR_B
435                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
436                         if (bNeedAck) {
437                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
438                                 return pDevice->uSIFS + uAckTime;
439                         } else {
440                                 return 0;
441                         }
442                 } else {//First Frag or Mid Frag
443                         if (uFragIdx == (uMACfragNum-2))
444                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wRate, bNeedAck);
445                         else
446                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
447
448                         if (bNeedAck) {
449                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
450                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
451                         } else {
452                                 return pDevice->uSIFS + uNextPktTime;
453                         }
454                 }
455                 break;
456
457         case DATADUR_A:    //DATADUR_A
458                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
459                         if (bNeedAck) {
460                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
461                                 return pDevice->uSIFS + uAckTime;
462                         } else {
463                                 return 0;
464                         }
465                 } else {//First Frag or Mid Frag
466                         if (uFragIdx == (uMACfragNum-2))
467                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wRate, bNeedAck);
468                         else
469                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
470
471                         if (bNeedAck) {
472                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
473                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
474                         } else {
475                                 return pDevice->uSIFS + uNextPktTime;
476                         }
477                 }
478                 break;
479
480         case DATADUR_A_F0:    //DATADUR_A_F0
481                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
482                         if (bNeedAck) {
483                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
484                                 return pDevice->uSIFS + uAckTime;
485                         } else {
486                                 return 0;
487                         }
488                 } else { //First Frag or Mid Frag
489                         if (byFBOption == AUTO_FB_0) {
490                                 if (wRate < RATE_18M)
491                                         wRate = RATE_18M;
492                                 else if (wRate > RATE_54M)
493                                         wRate = RATE_54M;
494
495                                 if (uFragIdx == (uMACfragNum-2))
496                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
497                                 else
498                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
499
500                         } else { // (byFBOption == AUTO_FB_1)
501                                 if (wRate < RATE_18M)
502                                         wRate = RATE_18M;
503                                 else if (wRate > RATE_54M)
504                                         wRate = RATE_54M;
505
506                                 if (uFragIdx == (uMACfragNum-2))
507                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
508                                 else
509                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
510
511                         }
512
513                         if (bNeedAck) {
514                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
515                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
516                         } else {
517                                 return pDevice->uSIFS + uNextPktTime;
518                         }
519                 }
520                 break;
521
522         case DATADUR_A_F1:    //DATADUR_A_F1
523                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
524                         if (bNeedAck) {
525                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
526                                 return pDevice->uSIFS + uAckTime;
527                         } else {
528                                 return 0;
529                         }
530                 } else { //First Frag or Mid Frag
531                         if (byFBOption == AUTO_FB_0) {
532                                 if (wRate < RATE_18M)
533                                         wRate = RATE_18M;
534                                 else if (wRate > RATE_54M)
535                                         wRate = RATE_54M;
536
537                                 if (uFragIdx == (uMACfragNum-2))
538                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
539                                 else
540                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
541
542                         } else { // (byFBOption == AUTO_FB_1)
543                                 if (wRate < RATE_18M)
544                                         wRate = RATE_18M;
545                                 else if (wRate > RATE_54M)
546                                         wRate = RATE_54M;
547
548                                 if (uFragIdx == (uMACfragNum-2))
549                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
550                                 else
551                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
552                         }
553                         if (bNeedAck) {
554                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
555                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
556                         } else {
557                                 return pDevice->uSIFS + uNextPktTime;
558                         }
559                 }
560                 break;
561
562         default:
563                 break;
564         }
565
566         ASSERT(false);
567         return 0;
568 }
569
570 //byFreqType: 0=>5GHZ 1=>2.4GHZ
571 static
572 __le16
573 s_uGetRTSCTSDuration(
574         struct vnt_private *pDevice,
575         unsigned char byDurType,
576         unsigned int cbFrameLength,
577         unsigned char byPktType,
578         unsigned short wRate,
579         bool bNeedAck,
580         unsigned char byFBOption
581 )
582 {
583         unsigned int uCTSTime = 0, uDurTime = 0;
584
585         switch (byDurType) {
586         case RTSDUR_BB:    //RTSDuration_bb
587                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
588                 uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
589                 break;
590
591         case RTSDUR_BA:    //RTSDuration_ba
592                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
593                 uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
594                 break;
595
596         case RTSDUR_AA:    //RTSDuration_aa
597                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
598                 uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
599                 break;
600
601         case CTSDUR_BA:    //CTSDuration_ba
602                 uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
603                 break;
604
605         case RTSDUR_BA_F0: //RTSDuration_ba_f0
606                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
607                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
608                         uDurTime = uCTSTime + 2 * pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
609                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
610                         uDurTime = uCTSTime + 2 * pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
611
612                 break;
613
614         case RTSDUR_AA_F0: //RTSDuration_aa_f0
615                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
616                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
617                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
618                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
619                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
620
621                 break;
622
623         case RTSDUR_BA_F1: //RTSDuration_ba_f1
624                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
625                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
626                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
627                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
628                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
629
630                 break;
631
632         case RTSDUR_AA_F1: //RTSDuration_aa_f1
633                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
634                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
635                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
636                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
637                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
638
639                 break;
640
641         case CTSDUR_BA_F0: //CTSDuration_ba_f0
642                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
643                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
644                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
645                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
646
647                 break;
648
649         case CTSDUR_BA_F1: //CTSDuration_ba_f1
650                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
651                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
652                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
653                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
654
655                 break;
656
657         default:
658                 break;
659         }
660
661         return cpu_to_le16((u16)uDurTime);
662 }
663
664 static
665 __le16
666 s_uFillDataHead(
667         struct vnt_private *pDevice,
668         unsigned char byPktType,
669         void *pTxDataHead,
670         unsigned int cbFrameLength,
671         unsigned int uDMAIdx,
672         bool bNeedAck,
673         unsigned int uFragIdx,
674         unsigned int cbLastFragmentSize,
675         unsigned int uMACfragNum,
676         unsigned char byFBOption,
677         unsigned short wCurrentRate
678 )
679 {
680
681         if (pTxDataHead == NULL)
682                 return 0;
683
684
685         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
686                 if (byFBOption == AUTO_FB_NONE) {
687                         struct vnt_tx_datahead_g *buf = pTxDataHead;
688                         /* Get SignalField, ServiceField & Length */
689                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
690                                           byPktType, &buf->a);
691
692                         vnt_get_phy_field(pDevice, cbFrameLength,
693                                           pDevice->byTopCCKBasicRate,
694                                           PK_TYPE_11B, &buf->b);
695
696                         /* Get Duration and TimeStamp */
697                         buf->duration_a = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength,
698                                                                               byPktType, wCurrentRate, bNeedAck, uFragIdx,
699                                                                               cbLastFragmentSize, uMACfragNum,
700                                                                               byFBOption));
701                         buf->duration_b = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength,
702                                                                               PK_TYPE_11B, pDevice->byTopCCKBasicRate,
703                                                                               bNeedAck, uFragIdx, cbLastFragmentSize,
704                                                                               uMACfragNum, byFBOption));
705
706                         buf->time_stamp_off_a = vnt_time_stamp_off(pDevice, wCurrentRate);
707                         buf->time_stamp_off_b = vnt_time_stamp_off(pDevice, pDevice->byTopCCKBasicRate);
708
709                         return buf->duration_a;
710                 } else {
711                         /* Auto Fallback */
712                         struct vnt_tx_datahead_g_fb *buf = pTxDataHead;
713                         /* Get SignalField, ServiceField & Length */
714                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
715                                           byPktType, &buf->a);
716
717                         vnt_get_phy_field(pDevice, cbFrameLength,
718                                           pDevice->byTopCCKBasicRate,
719                                           PK_TYPE_11B, &buf->b);
720                         /* Get Duration and TimeStamp */
721                         buf->duration_a = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
722                                                                               wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
723                         buf->duration_b = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength, PK_TYPE_11B,
724                                                                                pDevice->byTopCCKBasicRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
725                         buf->duration_a_f0 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F0, cbFrameLength, byPktType,
726                                                                                   wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
727                         buf->duration_a_f1 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F1, cbFrameLength, byPktType,
728                                                                                  wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
729
730                         buf->time_stamp_off_a = vnt_time_stamp_off(pDevice, wCurrentRate);
731                         buf->time_stamp_off_b = vnt_time_stamp_off(pDevice, pDevice->byTopCCKBasicRate);
732
733                         return buf->duration_a;
734                 } //if (byFBOption == AUTO_FB_NONE)
735         } else if (byPktType == PK_TYPE_11A) {
736                 if ((byFBOption != AUTO_FB_NONE)) {
737                         /* Auto Fallback */
738                         struct vnt_tx_datahead_a_fb *buf = pTxDataHead;
739                         /* Get SignalField, ServiceField & Length */
740                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
741                                           byPktType, &buf->a);
742
743                         /* Get Duration and TimeStampOff */
744                         buf->duration = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
745                                                                             wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
746                         buf->duration_f0 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F0, cbFrameLength, byPktType,
747                                                                                wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
748                         buf->duration_f1 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F1, cbFrameLength, byPktType,
749                                                                                 wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
750                         buf->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
751                         return buf->duration;
752                 } else {
753                         struct vnt_tx_datahead_ab *buf = pTxDataHead;
754                         /* Get SignalField, ServiceField & Length */
755                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
756                                           byPktType, &buf->ab);
757
758                         /* Get Duration and TimeStampOff */
759                         buf->duration = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
760                                                                             wCurrentRate, bNeedAck, uFragIdx,
761                                                                             cbLastFragmentSize, uMACfragNum,
762                                                                             byFBOption));
763
764                         buf->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
765                         return buf->duration;
766                 }
767         } else {
768                 struct vnt_tx_datahead_ab *buf = pTxDataHead;
769                 /* Get SignalField, ServiceField & Length */
770                 vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
771                                   byPktType, &buf->ab);
772                 /* Get Duration and TimeStampOff */
773                 buf->duration = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength, byPktType,
774                                                                     wCurrentRate, bNeedAck, uFragIdx,
775                                                                     cbLastFragmentSize, uMACfragNum,
776                                                                     byFBOption));
777                 buf->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
778                 return buf->duration;
779         }
780         return 0;
781 }
782
783 static
784 void
785 s_vFillRTSHead(
786         struct vnt_private *pDevice,
787         unsigned char byPktType,
788         void *pvRTS,
789         unsigned int cbFrameLength,
790         bool bNeedAck,
791         bool bDisCRC,
792         struct ieee80211_hdr *hdr,
793         unsigned short wCurrentRate,
794         unsigned char byFBOption
795 )
796 {
797         unsigned int uRTSFrameLen = 20;
798
799         if (pvRTS == NULL)
800                 return;
801
802         if (bDisCRC) {
803                 // When CRCDIS bit is on, H/W forgot to generate FCS for RTS frame,
804                 // in this case we need to decrease its length by 4.
805                 uRTSFrameLen -= 4;
806         }
807
808         // Note: So far RTSHead dosen't appear in ATIM & Beacom DMA, so we don't need to take them into account.
809         //       Otherwise, we need to modify codes for them.
810         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
811                 if (byFBOption == AUTO_FB_NONE) {
812                         struct vnt_rts_g *buf = pvRTS;
813                         /* Get SignalField, ServiceField & Length */
814                         vnt_get_phy_field(pDevice, uRTSFrameLen,
815                                           pDevice->byTopCCKBasicRate,
816                                           PK_TYPE_11B, &buf->b);
817
818                         vnt_get_phy_field(pDevice, uRTSFrameLen,
819                                           pDevice->byTopOFDMBasicRate,
820                                           byPktType, &buf->a);
821                         /* Get Duration */
822                         buf->duration_bb =
823                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
824                                                      cbFrameLength, PK_TYPE_11B,
825                                                      pDevice->byTopCCKBasicRate,
826                                                      bNeedAck, byFBOption);
827                         buf->duration_aa =
828                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
829                                                      cbFrameLength, byPktType,
830                                                      wCurrentRate, bNeedAck,
831                                                      byFBOption);
832                         buf->duration_ba =
833                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA,
834                                                      cbFrameLength, byPktType,
835                                                      wCurrentRate, bNeedAck,
836                                                      byFBOption);
837
838                         buf->data.duration = buf->duration_aa;
839                         /* Get RTS Frame body */
840                         buf->data.frame_control =
841                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
842                                                     IEEE80211_STYPE_RTS);
843
844                         ether_addr_copy(buf->data.ra, hdr->addr1);
845                         ether_addr_copy(buf->data.ta, hdr->addr2);
846                 } else {
847                         struct vnt_rts_g_fb *buf = pvRTS;
848                         /* Get SignalField, ServiceField & Length */
849                         vnt_get_phy_field(pDevice, uRTSFrameLen,
850                                           pDevice->byTopCCKBasicRate,
851                                           PK_TYPE_11B, &buf->b);
852
853                         vnt_get_phy_field(pDevice, uRTSFrameLen,
854                                           pDevice->byTopOFDMBasicRate,
855                                           byPktType, &buf->a);
856                         /* Get Duration */
857                         buf->duration_bb =
858                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
859                                                      cbFrameLength, PK_TYPE_11B,
860                                                      pDevice->byTopCCKBasicRate,
861                                                      bNeedAck, byFBOption);
862                         buf->duration_aa =
863                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
864                                                      cbFrameLength, byPktType,
865                                                      wCurrentRate, bNeedAck,
866                                                      byFBOption);
867                         buf->duration_ba =
868                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA,
869                                                      cbFrameLength, byPktType,
870                                                      wCurrentRate, bNeedAck,
871                                                      byFBOption);
872                         buf->rts_duration_ba_f0 =
873                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA_F0,
874                                                      cbFrameLength, byPktType,
875                                                      wCurrentRate, bNeedAck,
876                                                      byFBOption);
877                         buf->rts_duration_aa_f0 =
878                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F0,
879                                                      cbFrameLength, byPktType,
880                                                      wCurrentRate, bNeedAck,
881                                                      byFBOption);
882                         buf->rts_duration_ba_f1 =
883                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA_F1,
884                                                      cbFrameLength, byPktType,
885                                                      wCurrentRate, bNeedAck,
886                                                      byFBOption);
887                         buf->rts_duration_aa_f1 =
888                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F1,
889                                                      cbFrameLength, byPktType,
890                                                      wCurrentRate, bNeedAck,
891                                                      byFBOption);
892                         buf->data.duration = buf->duration_aa;
893                         /* Get RTS Frame body */
894                         buf->data.frame_control =
895                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
896                                                     IEEE80211_STYPE_RTS);
897
898                         ether_addr_copy(buf->data.ra, hdr->addr1);
899                         ether_addr_copy(buf->data.ta, hdr->addr2);
900                 } // if (byFBOption == AUTO_FB_NONE)
901         } else if (byPktType == PK_TYPE_11A) {
902                 if (byFBOption == AUTO_FB_NONE) {
903                         struct vnt_rts_ab *buf = pvRTS;
904                         /* Get SignalField, ServiceField & Length */
905                         vnt_get_phy_field(pDevice, uRTSFrameLen,
906                                           pDevice->byTopOFDMBasicRate,
907                                           byPktType, &buf->ab);
908                         /* Get Duration */
909                         buf->duration =
910                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
911                                                      cbFrameLength, byPktType,
912                                                      wCurrentRate, bNeedAck,
913                                                      byFBOption);
914                         buf->data.duration = buf->duration;
915                         /* Get RTS Frame body */
916                         buf->data.frame_control =
917                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
918                                                     IEEE80211_STYPE_RTS);
919
920                         ether_addr_copy(buf->data.ra, hdr->addr1);
921                         ether_addr_copy(buf->data.ta, hdr->addr2);
922                 } else {
923                         struct vnt_rts_a_fb *buf = pvRTS;
924                         /* Get SignalField, ServiceField & Length */
925                         vnt_get_phy_field(pDevice, uRTSFrameLen,
926                                           pDevice->byTopOFDMBasicRate,
927                                           byPktType, &buf->a);
928                         /* Get Duration */
929                         buf->duration =
930                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
931                                                      cbFrameLength, byPktType,
932                                                      wCurrentRate, bNeedAck,
933                                                      byFBOption);
934                         buf->rts_duration_f0 =
935                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F0,
936                                                      cbFrameLength, byPktType,
937                                                      wCurrentRate, bNeedAck,
938                                                      byFBOption);
939                         buf->rts_duration_f1 =
940                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F1,
941                                                      cbFrameLength, byPktType,
942                                                      wCurrentRate, bNeedAck,
943                                                      byFBOption);
944                         buf->data.duration = buf->duration;
945                         /* Get RTS Frame body */
946                         buf->data.frame_control =
947                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
948                                                     IEEE80211_STYPE_RTS);
949
950                         ether_addr_copy(buf->data.ra, hdr->addr1);
951                         ether_addr_copy(buf->data.ta, hdr->addr2);
952                 }
953         } else if (byPktType == PK_TYPE_11B) {
954                 struct vnt_rts_ab *buf = pvRTS;
955                 /* Get SignalField, ServiceField & Length */
956                 vnt_get_phy_field(pDevice, uRTSFrameLen,
957                                   pDevice->byTopCCKBasicRate,
958                                   PK_TYPE_11B, &buf->ab);
959                 /* Get Duration */
960                 buf->duration =
961                         s_uGetRTSCTSDuration(pDevice, RTSDUR_BB, cbFrameLength,
962                                              byPktType, wCurrentRate, bNeedAck,
963                                              byFBOption);
964
965                 buf->data.duration = buf->duration;
966                 /* Get RTS Frame body */
967                 buf->data.frame_control =
968                         cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
969
970                 ether_addr_copy(buf->data.ra, hdr->addr1);
971                 ether_addr_copy(buf->data.ta, hdr->addr2);
972         }
973 }
974
975 static
976 void
977 s_vFillCTSHead(
978         struct vnt_private *pDevice,
979         unsigned int uDMAIdx,
980         unsigned char byPktType,
981         void *pvCTS,
982         unsigned int cbFrameLength,
983         bool bNeedAck,
984         bool bDisCRC,
985         unsigned short wCurrentRate,
986         unsigned char byFBOption
987 )
988 {
989         unsigned int uCTSFrameLen = 14;
990
991         if (pvCTS == NULL)
992                 return;
993
994         if (bDisCRC) {
995                 // When CRCDIS bit is on, H/W forgot to generate FCS for CTS frame,
996                 // in this case we need to decrease its length by 4.
997                 uCTSFrameLen -= 4;
998         }
999
1000         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
1001                 if (byFBOption != AUTO_FB_NONE && uDMAIdx != TYPE_ATIMDMA && uDMAIdx != TYPE_BEACONDMA) {
1002                         // Auto Fall back
1003                         struct vnt_cts_fb *buf = pvCTS;
1004                         /* Get SignalField, ServiceField & Length */
1005                         vnt_get_phy_field(pDevice, uCTSFrameLen,
1006                                           pDevice->byTopCCKBasicRate,
1007                                           PK_TYPE_11B, &buf->b);
1008
1009                         buf->duration_ba =
1010                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA,
1011                                                      cbFrameLength, byPktType,
1012                                                      wCurrentRate, bNeedAck,
1013                                                      byFBOption);
1014
1015                         /* Get CTSDuration_ba_f0 */
1016                         buf->cts_duration_ba_f0 =
1017                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA_F0,
1018                                                      cbFrameLength, byPktType,
1019                                                      wCurrentRate, bNeedAck,
1020                                                      byFBOption);
1021
1022                         /* Get CTSDuration_ba_f1 */
1023                         buf->cts_duration_ba_f1 =
1024                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA_F1,
1025                                                      cbFrameLength, byPktType,
1026                                                      wCurrentRate, bNeedAck,
1027                                                      byFBOption);
1028
1029                         /* Get CTS Frame body */
1030                         buf->data.duration = buf->duration_ba;
1031
1032                         buf->data.frame_control =
1033                                 cpu_to_le16(IEEE80211_FTYPE_CTL |
1034                                             IEEE80211_STYPE_CTS);
1035
1036                         buf->reserved2 = 0x0;
1037
1038                         ether_addr_copy(buf->data.ra,
1039                                         pDevice->abyCurrentNetAddr);
1040                 } else { //if (byFBOption != AUTO_FB_NONE && uDMAIdx != TYPE_ATIMDMA && uDMAIdx != TYPE_BEACONDMA)
1041                         struct vnt_cts *buf = pvCTS;
1042                         /* Get SignalField, ServiceField & Length */
1043                         vnt_get_phy_field(pDevice, uCTSFrameLen,
1044                                           pDevice->byTopCCKBasicRate,
1045                                           PK_TYPE_11B, &buf->b);
1046
1047                         /* Get CTSDuration_ba */
1048                         buf->duration_ba =
1049                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA,
1050                                                      cbFrameLength, byPktType,
1051                                                      wCurrentRate, bNeedAck,
1052                                                      byFBOption);
1053
1054                         /* Get CTS Frame body */
1055                         buf->data.duration = buf->duration_ba;
1056
1057                         buf->data.frame_control =
1058                                 cpu_to_le16(IEEE80211_FTYPE_CTL |
1059                                             IEEE80211_STYPE_CTS);
1060
1061                         buf->reserved2 = 0x0;
1062                         ether_addr_copy(buf->data.ra,
1063                                         pDevice->abyCurrentNetAddr);
1064                 }
1065         }
1066 }
1067
1068 /*+
1069  *
1070  * Description:
1071  *      Generate FIFO control for MAC & Baseband controller
1072  *
1073  * Parameters:
1074  *  In:
1075  *      pDevice         - Pointer to adapter
1076  *      pTxDataHead     - Transmit Data Buffer
1077  *      pTxBufHead      - pTxBufHead
1078  *      pvRrvTime        - pvRrvTime
1079  *      pvRTS            - RTS Buffer
1080  *      pCTS            - CTS Buffer
1081  *      cbFrameSize     - Transmit Data Length (Hdr+Payload+FCS)
1082  *      bNeedACK        - If need ACK
1083  *      uDescIdx        - Desc Index
1084  *  Out:
1085  *      none
1086  *
1087  * Return Value: none
1088  *
1089  -*/
1090 // unsigned int cbFrameSize,//Hdr+Payload+FCS
1091 static
1092 void
1093 s_vGenerateTxParameter(
1094         struct vnt_private *pDevice,
1095         unsigned char byPktType,
1096         void *pTxBufHead,
1097         void *pvRrvTime,
1098         void *pvRTS,
1099         void *pvCTS,
1100         unsigned int cbFrameSize,
1101         bool bNeedACK,
1102         unsigned int uDMAIdx,
1103         void *psEthHeader,
1104         unsigned short wCurrentRate
1105 )
1106 {
1107         unsigned int cbMACHdLen = WLAN_HDR_ADDR3_LEN; //24
1108         unsigned short wFifoCtl;
1109         bool bDisCRC = false;
1110         unsigned char byFBOption = AUTO_FB_NONE;
1111
1112         PSTxBufHead pFifoHead = (PSTxBufHead)pTxBufHead;
1113
1114         pFifoHead->wReserved = wCurrentRate;
1115         wFifoCtl = pFifoHead->wFIFOCtl;
1116
1117         if (wFifoCtl & FIFOCTL_CRCDIS)
1118                 bDisCRC = true;
1119
1120         if (wFifoCtl & FIFOCTL_AUTO_FB_0)
1121                 byFBOption = AUTO_FB_0;
1122         else if (wFifoCtl & FIFOCTL_AUTO_FB_1)
1123                 byFBOption = AUTO_FB_1;
1124
1125         if (pDevice->bLongHeader)
1126                 cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;
1127
1128         if (!pvRrvTime)
1129                 return;
1130
1131         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
1132                 if (pvRTS != NULL) { //RTS_need
1133                         /* Fill RsvTime */
1134                         struct vnt_rrv_time_rts *buf = pvRrvTime;
1135
1136                         buf->rts_rrv_time_aa = s_uGetRTSCTSRsvTime(pDevice, 2, byPktType, cbFrameSize, wCurrentRate);
1137                         buf->rts_rrv_time_ba = s_uGetRTSCTSRsvTime(pDevice, 1, byPktType, cbFrameSize, wCurrentRate);
1138                         buf->rts_rrv_time_bb = s_uGetRTSCTSRsvTime(pDevice, 0, byPktType, cbFrameSize, wCurrentRate);
1139                         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK);
1140                         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate, bNeedACK);
1141
1142                         s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1143                 } else {//RTS_needless, PCF mode
1144                         struct vnt_rrv_time_cts *buf = pvRrvTime;
1145
1146                         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK);
1147                         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate, bNeedACK);
1148                         buf->cts_rrv_time_ba = s_uGetRTSCTSRsvTime(pDevice, 3, byPktType, cbFrameSize, wCurrentRate);
1149
1150                         //Fill CTS
1151                         s_vFillCTSHead(pDevice, uDMAIdx, byPktType, pvCTS, cbFrameSize, bNeedACK, bDisCRC, wCurrentRate, byFBOption);
1152                 }
1153         } else if (byPktType == PK_TYPE_11A) {
1154                 if (pvRTS != NULL) {//RTS_need, non PCF mode
1155                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1156
1157                         buf->rts_rrv_time = s_uGetRTSCTSRsvTime(pDevice, 2, byPktType, cbFrameSize, wCurrentRate);
1158                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK);
1159
1160                         //Fill RTS
1161                         s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1162                 } else if (pvRTS == NULL) {//RTS_needless, non PCF mode
1163                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1164
1165                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11A, cbFrameSize, wCurrentRate, bNeedACK);
1166                 }
1167         } else if (byPktType == PK_TYPE_11B) {
1168                 if ((pvRTS != NULL)) {//RTS_need, non PCF mode
1169                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1170
1171                         buf->rts_rrv_time = s_uGetRTSCTSRsvTime(pDevice, 0, byPktType, cbFrameSize, wCurrentRate);
1172                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, wCurrentRate, bNeedACK);
1173
1174                         //Fill RTS
1175                         s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1176                 } else { //RTS_needless, non PCF mode
1177                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1178
1179                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, wCurrentRate, bNeedACK);
1180                 }
1181         }
1182 }
1183
1184 static unsigned int
1185 s_cbFillTxBufHead(struct vnt_private *pDevice, unsigned char byPktType,
1186                   unsigned char *pbyTxBufferAddr, unsigned int cbFrameBodySize,
1187                   unsigned int uDMAIdx, PSTxDesc pHeadTD,
1188                   PSEthernetHeader psEthHeader, unsigned char *pPacket,
1189                   bool bNeedEncrypt, PSKeyItem pTransmitKey,
1190                   unsigned int is_pspoll, unsigned int *puMACfragNum)
1191 {
1192         PDEVICE_TD_INFO td_info = pHeadTD->pTDInfo;
1193         struct sk_buff *skb = td_info->skb;
1194         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1195         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1196         struct vnt_tx_fifo_head *tx_buffer_head =
1197                         (struct vnt_tx_fifo_head *)td_info->buf;
1198         u16 fifo_ctl = le16_to_cpu(tx_buffer_head->fifo_ctl);
1199         unsigned int cbFrameSize;
1200         __le16 uDuration;
1201         unsigned char *pbyBuffer;
1202         unsigned int uLength = 0;
1203         unsigned int cbMICHDR = 0;
1204         unsigned int uMACfragNum = 1;
1205         unsigned int uPadding = 0;
1206         unsigned int cbReqCount = 0;
1207         bool bNeedACK = (bool)(fifo_ctl & FIFOCTL_NEEDACK);
1208         bool bRTS = (bool)(fifo_ctl & FIFOCTL_RTS);
1209         PSTxDesc       ptdCurr;
1210         unsigned int cbHeaderLength = 0;
1211         void *pvRrvTime;
1212         struct vnt_mic_hdr *pMICHDR;
1213         void *pvRTS;
1214         void *pvCTS;
1215         void *pvTxDataHd;
1216         unsigned short wTxBufSize;   // FFinfo size
1217         unsigned char byFBOption = AUTO_FB_NONE;
1218
1219         pvRrvTime = pMICHDR = pvRTS = pvCTS = pvTxDataHd = NULL;
1220
1221         cbFrameSize = skb->len + 4;
1222
1223         if (info->control.hw_key) {
1224                 switch (info->control.hw_key->cipher) {
1225                 case WLAN_CIPHER_SUITE_CCMP:
1226                         cbMICHDR = sizeof(struct vnt_mic_hdr);
1227                 default:
1228                         break;
1229                 }
1230
1231                 cbFrameSize += info->control.hw_key->icv_len;
1232
1233                 if (pDevice->byLocalID > REV_ID_VT3253_A1) {
1234                         //MAC Header should be padding 0 to DW alignment.
1235                         uPadding = 4 - (ieee80211_get_hdrlen_from_skb(skb) % 4);
1236                         uPadding %= 4;
1237                 }
1238         }
1239
1240         //
1241         // Use for AUTO FALL BACK
1242         //
1243         if (fifo_ctl & FIFOCTL_AUTO_FB_0)
1244                 byFBOption = AUTO_FB_0;
1245         else if (fifo_ctl & FIFOCTL_AUTO_FB_1)
1246                 byFBOption = AUTO_FB_1;
1247
1248         //////////////////////////////////////////////////////
1249         //Set RrvTime/RTS/CTS Buffer
1250         wTxBufSize = sizeof(STxBufHead);
1251         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
1252
1253                 if (byFBOption == AUTO_FB_NONE) {
1254                         if (bRTS == true) {//RTS_need
1255                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1256                                 pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts));
1257                                 pvRTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1258                                 pvCTS = NULL;
1259                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1260                                                         cbMICHDR + sizeof(struct vnt_rts_g));
1261                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1262                                                         cbMICHDR + sizeof(struct vnt_rts_g) +
1263                                                         sizeof(struct vnt_tx_datahead_g);
1264                         } else { //RTS_needless
1265                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1266                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts));
1267                                 pvRTS = NULL;
1268                                 pvCTS = (void *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1269                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1270                                                 sizeof(struct vnt_rrv_time_cts) + cbMICHDR + sizeof(struct vnt_cts));
1271                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1272                                                         cbMICHDR + sizeof(struct vnt_cts) + sizeof(struct vnt_tx_datahead_g);
1273                         }
1274                 } else {
1275                         // Auto Fall Back
1276                         if (bRTS == true) {//RTS_need
1277                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1278                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts));
1279                                 pvRTS = (void *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1280                                 pvCTS = NULL;
1281                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1282                                         cbMICHDR + sizeof(struct vnt_rts_g_fb));
1283                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1284                                         cbMICHDR + sizeof(struct vnt_rts_g_fb) + sizeof(struct vnt_tx_datahead_g_fb);
1285                         } else { //RTS_needless
1286                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1287                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts));
1288                                 pvRTS = NULL;
1289                                 pvCTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1290                                 pvTxDataHd = (void  *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1291                                         cbMICHDR + sizeof(struct vnt_cts_fb));
1292                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1293                                         cbMICHDR + sizeof(struct vnt_cts_fb) + sizeof(struct vnt_tx_datahead_g_fb);
1294                         }
1295                 } // Auto Fall Back
1296         } else {//802.11a/b packet
1297
1298                 if (byFBOption == AUTO_FB_NONE) {
1299                         if (bRTS == true) {
1300                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1301                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1302                                 pvRTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1303                                 pvCTS = NULL;
1304                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1305                                         sizeof(struct vnt_rrv_time_ab) + cbMICHDR + sizeof(struct vnt_rts_ab));
1306                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1307                                         cbMICHDR + sizeof(struct vnt_rts_ab) + sizeof(struct vnt_tx_datahead_ab);
1308                         } else { //RTS_needless, need MICHDR
1309                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1310                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1311                                 pvRTS = NULL;
1312                                 pvCTS = NULL;
1313                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1314                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1315                                         cbMICHDR + sizeof(struct vnt_tx_datahead_ab);
1316                         }
1317                 } else {
1318                         // Auto Fall Back
1319                         if (bRTS == true) {//RTS_need
1320                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1321                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1322                                 pvRTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1323                                 pvCTS = NULL;
1324                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1325                                         sizeof(struct vnt_rrv_time_ab) + cbMICHDR + sizeof(struct vnt_rts_a_fb));
1326                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1327                                         cbMICHDR + sizeof(struct vnt_rts_a_fb) + sizeof(struct vnt_tx_datahead_a_fb);
1328                         } else { //RTS_needless
1329                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1330                                 pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1331                                 pvRTS = NULL;
1332                                 pvCTS = NULL;
1333                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1334                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1335                                         cbMICHDR + sizeof(struct vnt_tx_datahead_a_fb);
1336                         }
1337                 } // Auto Fall Back
1338         }
1339
1340         td_info->mic_hdr = pMICHDR;
1341
1342         memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderLength - wTxBufSize));
1343
1344         /* Fill FIFO,RrvTime,RTS,and CTS */
1345         s_vGenerateTxParameter(pDevice, byPktType, tx_buffer_head, pvRrvTime, pvRTS, pvCTS,
1346                                cbFrameSize, bNeedACK, uDMAIdx, hdr, pDevice->wCurrentRate);
1347         /* Fill DataHead */
1348         uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, uDMAIdx, bNeedACK,
1349                                     0, 0, uMACfragNum, byFBOption, pDevice->wCurrentRate);
1350
1351         hdr->duration_id = uDuration;
1352
1353         cbReqCount = cbHeaderLength + uPadding + cbFrameBodySize;
1354         pbyBuffer = (unsigned char *)pHeadTD->pTDInfo->buf;
1355         uLength = cbHeaderLength + uPadding;
1356
1357         /* Copy the Packet into a tx Buffer */
1358         memcpy((pbyBuffer + uLength), pPacket, cbFrameBodySize);
1359
1360         ptdCurr = (PSTxDesc)pHeadTD;
1361
1362         ptdCurr->pTDInfo->dwReqCount = cbReqCount - uPadding;
1363         ptdCurr->pTDInfo->dwHeaderLength = cbHeaderLength;
1364         ptdCurr->pTDInfo->skb_dma = ptdCurr->pTDInfo->buf_dma;
1365         ptdCurr->buff_addr = cpu_to_le32(ptdCurr->pTDInfo->skb_dma);
1366         /* Set TSR1 & ReqCount in TxDescHead */
1367         ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP | EDMSDU);
1368         ptdCurr->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
1369
1370         *puMACfragNum = uMACfragNum;
1371
1372         return cbHeaderLength;
1373 }
1374
1375 void
1376 vGenerateFIFOHeader(struct vnt_private *pDevice, unsigned char byPktType,
1377                     unsigned char *pbyTxBufferAddr, bool bNeedEncrypt,
1378                     unsigned int cbPayloadSize, unsigned int uDMAIdx,
1379                     PSTxDesc pHeadTD, PSEthernetHeader psEthHeader, unsigned char *pPacket,
1380                     PSKeyItem pTransmitKey, unsigned int uNodeIndex, unsigned int *puMACfragNum,
1381                     unsigned int *pcbHeaderSize)
1382 {
1383         unsigned int wTxBufSize;       // FFinfo size
1384         bool bNeedACK;
1385         bool bIsAdhoc;
1386         unsigned short cbMacHdLen;
1387         PSTxBufHead     pTxBufHead = (PSTxBufHead) pbyTxBufferAddr;
1388
1389         wTxBufSize = sizeof(STxBufHead);
1390
1391         memset(pTxBufHead, 0, wTxBufSize);
1392         //Set FIFOCTL_NEEDACK
1393
1394         if ((pDevice->op_mode == NL80211_IFTYPE_ADHOC) ||
1395             (pDevice->op_mode == NL80211_IFTYPE_AP)) {
1396                 if (is_multicast_ether_addr(&(psEthHeader->abyDstAddr[0]))) {
1397                         bNeedACK = false;
1398                         pTxBufHead->wFIFOCtl = pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
1399                 } else {
1400                         bNeedACK = true;
1401                         pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
1402                 }
1403                 bIsAdhoc = true;
1404         } else {
1405                 // MSDUs in Infra mode always need ACK
1406                 bNeedACK = true;
1407                 pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
1408                 bIsAdhoc = false;
1409         }
1410
1411         pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
1412         pTxBufHead->wTimeStamp = cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
1413
1414         //Set FIFOCTL_LHEAD
1415         if (pDevice->bLongHeader)
1416                 pTxBufHead->wFIFOCtl |= FIFOCTL_LHEAD;
1417
1418         //Set FIFOCTL_GENINT
1419
1420         pTxBufHead->wFIFOCtl |= FIFOCTL_GENINT;
1421
1422         //Set FIFOCTL_ISDMA0
1423         if (TYPE_TXDMA0 == uDMAIdx)
1424                 pTxBufHead->wFIFOCtl |= FIFOCTL_ISDMA0;
1425
1426         //Set FRAGCTL_MACHDCNT
1427         if (pDevice->bLongHeader)
1428                 cbMacHdLen = WLAN_HDR_ADDR3_LEN + 6;
1429         else
1430                 cbMacHdLen = WLAN_HDR_ADDR3_LEN;
1431
1432         pTxBufHead->wFragCtl |= cpu_to_le16((unsigned short)(cbMacHdLen << 10));
1433
1434         //Set packet type
1435         if (byPktType == PK_TYPE_11A) //0000 0000 0000 0000
1436                 ;
1437         else if (byPktType == PK_TYPE_11B) //0000 0001 0000 0000
1438                 pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
1439         else if (byPktType == PK_TYPE_11GB) //0000 0010 0000 0000
1440                 pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
1441         else if (byPktType == PK_TYPE_11GA) //0000 0011 0000 0000
1442                 pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
1443
1444         //Set FIFOCTL_GrpAckPolicy
1445         if (pDevice->bGrpAckPolicy == true) //0000 0100 0000 0000
1446                 pTxBufHead->wFIFOCtl |= FIFOCTL_GRPACK;
1447
1448         //Set Auto Fallback Ctl
1449         if (pDevice->wCurrentRate >= RATE_18M) {
1450                 if (pDevice->byAutoFBCtrl == AUTO_FB_0)
1451                         pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_0;
1452                 else if (pDevice->byAutoFBCtrl == AUTO_FB_1)
1453                         pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_1;
1454         }
1455
1456         //Set FRAGCTL_WEPTYP
1457         pDevice->bAES = false;
1458
1459         //Set FRAGCTL_WEPTYP
1460         if (pDevice->byLocalID > REV_ID_VT3253_A1) {
1461                 if ((bNeedEncrypt) && (pTransmitKey != NULL))  { //WEP enabled
1462                         if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
1463                                 pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
1464                         } else if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) { //WEP40 or WEP104
1465                                 if (pTransmitKey->uKeyLength != WLAN_WEP232_KEYLEN)
1466                                         pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
1467                         } else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) { //CCMP
1468                                 pTxBufHead->wFragCtl |= FRAGCTL_AES;
1469                         }
1470                 }
1471         }
1472
1473         RFbSetPower(pDevice, pDevice->wCurrentRate, pDevice->byCurrentCh);
1474
1475         pTxBufHead->byTxPower = pDevice->byCurPwr;
1476
1477         *pcbHeaderSize = s_cbFillTxBufHead(pDevice, byPktType, pbyTxBufferAddr, cbPayloadSize,
1478                                            uDMAIdx, pHeadTD, psEthHeader, pPacket, bNeedEncrypt,
1479                                            pTransmitKey, uNodeIndex, puMACfragNum);
1480 }
1481
1482 /*+
1483  *
1484  * Description:
1485  *      Translate 802.3 to 802.11 header
1486  *
1487  * Parameters:
1488  *  In:
1489  *      pDevice         - Pointer to adapter
1490  *      dwTxBufferAddr  - Transmit Buffer
1491  *      pPacket         - Packet from upper layer
1492  *      cbPacketSize    - Transmit Data Length
1493  *  Out:
1494  *      pcbHeadSize         - Header size of MAC&Baseband control and 802.11 Header
1495  *      pcbAppendPayload    - size of append payload for 802.1H translation
1496  *
1497  * Return Value: none
1498  *
1499  -*/
1500
1501 void
1502 vGenerateMACHeader(
1503         struct vnt_private *pDevice,
1504         unsigned char *pbyBufferAddr,
1505         __le16 wDuration,
1506         PSEthernetHeader psEthHeader,
1507         bool bNeedEncrypt,
1508         unsigned short wFragType,
1509         unsigned int uDMAIdx,
1510         unsigned int uFragIdx
1511 )
1512 {
1513         PS802_11Header  pMACHeader = (PS802_11Header)pbyBufferAddr;
1514
1515         memset(pMACHeader, 0, (sizeof(S802_11Header)));
1516
1517         if (uDMAIdx == TYPE_ATIMDMA)
1518                 pMACHeader->wFrameCtl = TYPE_802_11_ATIM;
1519         else
1520                 pMACHeader->wFrameCtl = TYPE_802_11_DATA;
1521
1522         if (pDevice->op_mode == NL80211_IFTYPE_AP) {
1523                 ether_addr_copy(&(pMACHeader->abyAddr1[0]),
1524                                 &(psEthHeader->abyDstAddr[0]));
1525                 ether_addr_copy(&(pMACHeader->abyAddr2[0]),
1526                                 &(pDevice->abyBSSID[0]));
1527                 ether_addr_copy(&(pMACHeader->abyAddr3[0]),
1528                                 &(psEthHeader->abySrcAddr[0]));
1529                 pMACHeader->wFrameCtl |= FC_FROMDS;
1530         } else {
1531                 if (pDevice->op_mode == NL80211_IFTYPE_ADHOC) {
1532                         ether_addr_copy(&(pMACHeader->abyAddr1[0]),
1533                                         &(psEthHeader->abyDstAddr[0]));
1534                         ether_addr_copy(&(pMACHeader->abyAddr2[0]),
1535                                         &(psEthHeader->abySrcAddr[0]));
1536                         ether_addr_copy(&(pMACHeader->abyAddr3[0]),
1537                                         &(pDevice->abyBSSID[0]));
1538                 } else {
1539                         ether_addr_copy(&(pMACHeader->abyAddr3[0]),
1540                                         &(psEthHeader->abyDstAddr[0]));
1541                         ether_addr_copy(&(pMACHeader->abyAddr2[0]),
1542                                         &(psEthHeader->abySrcAddr[0]));
1543                         ether_addr_copy(&(pMACHeader->abyAddr1[0]),
1544                                         &(pDevice->abyBSSID[0]));
1545                         pMACHeader->wFrameCtl |= FC_TODS;
1546                 }
1547         }
1548
1549         if (bNeedEncrypt)
1550                 pMACHeader->wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_ISWEP(1));
1551
1552         pMACHeader->wDurationID = le16_to_cpu(wDuration);
1553
1554         if (pDevice->bLongHeader) {
1555                 PWLAN_80211HDR_A4 pMACA4Header  = (PWLAN_80211HDR_A4) pbyBufferAddr;
1556
1557                 pMACHeader->wFrameCtl |= (FC_TODS | FC_FROMDS);
1558                 memcpy(pMACA4Header->abyAddr4, pDevice->abyBSSID, WLAN_ADDR_LEN);
1559         }
1560         pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
1561
1562         //Set FragNumber in Sequence Control
1563         pMACHeader->wSeqCtl |= cpu_to_le16((unsigned short)uFragIdx);
1564
1565         if ((wFragType == FRAGCTL_ENDFRAG) || (wFragType == FRAGCTL_NONFRAG)) {
1566                 pDevice->wSeqCounter++;
1567                 if (pDevice->wSeqCounter > 0x0fff)
1568                         pDevice->wSeqCounter = 0;
1569         }
1570
1571         if ((wFragType == FRAGCTL_STAFRAG) || (wFragType == FRAGCTL_MIDFRAG)) //StartFrag or MidFrag
1572                 pMACHeader->wFrameCtl |= FC_MOREFRAG;
1573 }
1574
1575 CMD_STATUS csMgmt_xmit(struct vnt_private *pDevice, PSTxMgmtPacket pPacket)
1576 {
1577         PSTxDesc        pFrstTD;
1578         unsigned char byPktType;
1579         unsigned char *pbyTxBufferAddr;
1580         void *pvRTS;
1581         struct vnt_cts *pCTS;
1582         void *pvTxDataHd;
1583         unsigned int uDuration;
1584         unsigned int cbReqCount;
1585         PS802_11Header  pMACHeader;
1586         unsigned int cbHeaderSize;
1587         unsigned int cbFrameBodySize;
1588         bool bNeedACK;
1589         bool bIsPSPOLL = false;
1590         PSTxBufHead     pTxBufHead;
1591         unsigned int cbFrameSize;
1592         unsigned int cbIVlen = 0;
1593         unsigned int cbICVlen = 0;
1594         unsigned int cbMIClen = 0;
1595         unsigned int cbFCSlen = 4;
1596         unsigned int uPadding = 0;
1597         unsigned short wTxBufSize;
1598         unsigned int cbMacHdLen;
1599         SEthernetHeader sEthHeader;
1600         void *pvRrvTime;
1601         void *pMICHDR;
1602         PSMgmtObject    pMgmt = pDevice->pMgmt;
1603         unsigned short wCurrentRate = RATE_1M;
1604
1605         if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0)
1606                 return CMD_STATUS_RESOURCES;
1607
1608         pFrstTD = pDevice->apCurrTD[TYPE_TXDMA0];
1609         pbyTxBufferAddr = (unsigned char *)pFrstTD->pTDInfo->buf;
1610         cbFrameBodySize = pPacket->cbPayloadLen;
1611         pTxBufHead = (PSTxBufHead) pbyTxBufferAddr;
1612         wTxBufSize = sizeof(STxBufHead);
1613         memset(pTxBufHead, 0, wTxBufSize);
1614
1615         if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
1616                 wCurrentRate = RATE_6M;
1617                 byPktType = PK_TYPE_11A;
1618         } else {
1619                 wCurrentRate = RATE_1M;
1620                 byPktType = PK_TYPE_11B;
1621         }
1622
1623         // SetPower will cause error power TX state for OFDM Date packet in TX buffer.
1624         // 2004.11.11 Kyle -- Using OFDM power to tx MngPkt will decrease the connection capability.
1625         //                    And cmd timer will wait data pkt TX finish before scanning so it's OK
1626         //                    to set power here.
1627         if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING)
1628                 RFbSetPower(pDevice, wCurrentRate, pDevice->byCurrentCh);
1629         else
1630                 RFbSetPower(pDevice, wCurrentRate, pMgmt->uCurrChannel);
1631
1632         pTxBufHead->byTxPower = pDevice->byCurPwr;
1633         //+++++++++++++++++++++ Patch VT3253 A1 performance +++++++++++++++++++++++++++
1634         if (pDevice->byFOETuning) {
1635                 if ((pPacket->p80211Header->sA3.wFrameCtl & TYPE_DATE_NULL) == TYPE_DATE_NULL) {
1636                         wCurrentRate = RATE_24M;
1637                         byPktType = PK_TYPE_11GA;
1638                 }
1639         }
1640
1641         //Set packet type
1642         if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
1643                 pTxBufHead->wFIFOCtl = 0;
1644         } else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
1645                 pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
1646         } else if (byPktType == PK_TYPE_11GB) {//0000 0010 0000 0000
1647                 pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
1648         } else if (byPktType == PK_TYPE_11GA) {//0000 0011 0000 0000
1649                 pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
1650         }
1651
1652         pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
1653         pTxBufHead->wTimeStamp = cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
1654
1655         if (is_multicast_ether_addr(&(pPacket->p80211Header->sA3.abyAddr1[0])))
1656                 bNeedACK = false;
1657         else {
1658                 bNeedACK = true;
1659                 pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
1660         }
1661
1662         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) ||
1663             (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
1664                 pTxBufHead->wFIFOCtl |= FIFOCTL_LRETRY;
1665         }
1666
1667         pTxBufHead->wFIFOCtl |= (FIFOCTL_GENINT | FIFOCTL_ISDMA0);
1668
1669         if ((pPacket->p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_CTL_PSPOLL) {
1670                 bIsPSPOLL = true;
1671                 cbMacHdLen = WLAN_HDR_ADDR2_LEN;
1672         } else {
1673                 cbMacHdLen = WLAN_HDR_ADDR3_LEN;
1674         }
1675
1676         //Set FRAGCTL_MACHDCNT
1677         pTxBufHead->wFragCtl |= cpu_to_le16((unsigned short)(cbMacHdLen << 10));
1678
1679         // Notes:
1680         // Although spec says MMPDU can be fragmented; In most cases,
1681         // no one will send a MMPDU under fragmentation. With RTS may occur.
1682         pDevice->bAES = false;  //Set FRAGCTL_WEPTYP
1683
1684         if (WLAN_GET_FC_ISWEP(pPacket->p80211Header->sA4.wFrameCtl) != 0) {
1685                 if (pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) {
1686                         cbIVlen = 4;
1687                         cbICVlen = 4;
1688                         pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
1689                 } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
1690                         cbIVlen = 8;//IV+ExtIV
1691                         cbMIClen = 8;
1692                         cbICVlen = 4;
1693                         pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
1694                         //We need to get seed here for filling TxKey entry.
1695                 } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
1696                         cbIVlen = 8;//RSN Header
1697                         cbICVlen = 8;//MIC
1698                         pTxBufHead->wFragCtl |= FRAGCTL_AES;
1699                         pDevice->bAES = true;
1700                 }
1701                 //MAC Header should be padding 0 to DW alignment.
1702                 uPadding = 4 - (cbMacHdLen%4);
1703                 uPadding %= 4;
1704         }
1705
1706         cbFrameSize = cbMacHdLen + cbFrameBodySize + cbIVlen + cbMIClen + cbICVlen + cbFCSlen;
1707
1708         //Set FIFOCTL_GrpAckPolicy
1709         if (pDevice->bGrpAckPolicy == true) //0000 0100 0000 0000
1710                 pTxBufHead->wFIFOCtl |= FIFOCTL_GRPACK;
1711
1712         //the rest of pTxBufHead->wFragCtl:FragTyp will be set later in s_vFillFragParameter()
1713
1714         //Set RrvTime/RTS/CTS Buffer
1715         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
1716                 pvRrvTime = (void *) (pbyTxBufferAddr + wTxBufSize);
1717                 pMICHDR = NULL;
1718                 pvRTS = NULL;
1719                 pCTS = (struct vnt_cts *)(pbyTxBufferAddr + wTxBufSize +
1720                                         sizeof(struct vnt_rrv_time_cts));
1721                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1722                                 sizeof(struct vnt_rrv_time_cts) + sizeof(struct vnt_cts));
1723                 cbHeaderSize = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1724                                 sizeof(struct vnt_cts) + sizeof(struct vnt_tx_datahead_g);
1725         } else { // 802.11a/b packet
1726                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1727                 pMICHDR = NULL;
1728                 pvRTS = NULL;
1729                 pCTS = NULL;
1730                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1731                         sizeof(struct vnt_rrv_time_ab));
1732                 cbHeaderSize = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1733                         sizeof(struct vnt_tx_datahead_ab);
1734         }
1735
1736         memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderSize - wTxBufSize));
1737
1738         ether_addr_copy(&(sEthHeader.abyDstAddr[0]),
1739                         &(pPacket->p80211Header->sA3.abyAddr1[0]));
1740         ether_addr_copy(&(sEthHeader.abySrcAddr[0]),
1741                         &(pPacket->p80211Header->sA3.abyAddr2[0]));
1742         //=========================
1743         //    No Fragmentation
1744         //=========================
1745         pTxBufHead->wFragCtl |= (unsigned short)FRAGCTL_NONFRAG;
1746
1747         //Fill FIFO,RrvTime,RTS,and CTS
1748         s_vGenerateTxParameter(pDevice, byPktType, pbyTxBufferAddr, pvRrvTime, pvRTS, pCTS,
1749                                cbFrameSize, bNeedACK, TYPE_TXDMA0, &sEthHeader, wCurrentRate);
1750
1751         //Fill DataHead
1752         uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, TYPE_TXDMA0, bNeedACK,
1753                                     0, 0, 1, AUTO_FB_NONE, wCurrentRate);
1754
1755         pMACHeader = (PS802_11Header) (pbyTxBufferAddr + cbHeaderSize);
1756
1757         cbReqCount = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen + cbFrameBodySize;
1758
1759         if (WLAN_GET_FC_ISWEP(pPacket->p80211Header->sA4.wFrameCtl) != 0) {
1760                 unsigned char *pbyIVHead;
1761                 unsigned char *pbyPayloadHead;
1762                 unsigned char *pbyBSSID;
1763                 PSKeyItem       pTransmitKey = NULL;
1764
1765                 pbyIVHead = (unsigned char *)(pbyTxBufferAddr + cbHeaderSize + cbMacHdLen + uPadding);
1766                 pbyPayloadHead = (unsigned char *)(pbyTxBufferAddr + cbHeaderSize + cbMacHdLen + uPadding + cbIVlen);
1767
1768                 //Fill TXKEY
1769                 //Kyle: Need fix: TKIP and AES did't encrypt Mnt Packet.
1770                 //s_vFillTxKey(pDevice, (unsigned char *)pTxBufHead->adwTxKey, NULL);
1771
1772                 //Fill IV(ExtIV,RSNHDR)
1773                 //s_vFillPrePayload(pDevice, pbyIVHead, NULL);
1774                 //---------------------------
1775                 // S/W or H/W Encryption
1776                 //---------------------------
1777                 do {
1778                         if ((pDevice->op_mode == NL80211_IFTYPE_STATION) &&
1779                             (pDevice->bLinkPass == true)) {
1780                                 pbyBSSID = pDevice->abyBSSID;
1781                                 // get pairwise key
1782                                 if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
1783                                         // get group key
1784                                         if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
1785                                                 pr_debug("Get GTK\n");
1786                                                 break;
1787                                         }
1788                                 } else {
1789                                         pr_debug("Get PTK\n");
1790                                         break;
1791                                 }
1792                         }
1793                         // get group key
1794                         pbyBSSID = pDevice->abyBroadcastAddr;
1795                         if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
1796                                 pTransmitKey = NULL;
1797                                 pr_debug("KEY is NULL. OP Mode[%d]\n",
1798                                          pDevice->op_mode);
1799                         } else {
1800                                 pr_debug("Get GTK\n");
1801                         }
1802                 } while (false);
1803                 //Fill TXKEY
1804                 s_vFillTxKey(pDevice, (unsigned char *)(pTxBufHead->adwTxKey), pbyIVHead, pTransmitKey,
1805                              (unsigned char *)pMACHeader, (unsigned short)cbFrameBodySize, NULL);
1806
1807                 memcpy(pMACHeader, pPacket->p80211Header, cbMacHdLen);
1808                 memcpy(pbyPayloadHead, ((unsigned char *)(pPacket->p80211Header) + cbMacHdLen),
1809                        cbFrameBodySize);
1810         } else {
1811                 // Copy the Packet into a tx Buffer
1812                 memcpy(pMACHeader, pPacket->p80211Header, pPacket->cbMPDULen);
1813         }
1814
1815         pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
1816         pDevice->wSeqCounter++;
1817         if (pDevice->wSeqCounter > 0x0fff)
1818                 pDevice->wSeqCounter = 0;
1819
1820         if (bIsPSPOLL) {
1821                 // The MAC will automatically replace the Duration-field of MAC header by Duration-field
1822                 // of  FIFO control header.
1823                 // This will cause AID-field of PS-POLL packet to be incorrect (Because PS-POLL's AID field is
1824                 // in the same place of other packet's Duration-field).
1825                 // And it will cause Cisco-AP to issue Disassociation-packet
1826                 if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
1827                         ((struct vnt_tx_datahead_g *)pvTxDataHd)->duration_a = cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
1828                         ((struct vnt_tx_datahead_g *)pvTxDataHd)->duration_b = cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
1829                 } else {
1830                         ((struct vnt_tx_datahead_ab *)pvTxDataHd)->duration = cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
1831                 }
1832         }
1833
1834         // first TD is the only TD
1835         //Set TSR1 & ReqCount in TxDescHead
1836         pFrstTD->m_td1TD1.byTCR = (TCR_STP | TCR_EDP | EDMSDU);
1837         pFrstTD->pTDInfo->skb_dma = pFrstTD->pTDInfo->buf_dma;
1838         pFrstTD->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
1839         pFrstTD->buff_addr = cpu_to_le32(pFrstTD->pTDInfo->skb_dma);
1840         pFrstTD->pTDInfo->byFlags = 0;
1841
1842         if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
1843                 // Disable PS
1844                 MACbPSWakeup(pDevice->PortOffset);
1845         }
1846         pDevice->bPWBitOn = false;
1847
1848         wmb();
1849         pFrstTD->m_td0TD0.f1Owner = OWNED_BY_NIC;
1850         wmb();
1851
1852         pDevice->iTDUsed[TYPE_TXDMA0]++;
1853
1854         if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 1)
1855                 pr_debug(" available td0 <= 1\n");
1856
1857         pDevice->apCurrTD[TYPE_TXDMA0] = pFrstTD->next;
1858
1859         pDevice->nTxDataTimeCout = 0; //2008-8-21 chester <add> for send null packet
1860
1861         // Poll Transmit the adapter
1862         MACvTransmit0(pDevice->PortOffset);
1863
1864         return CMD_STATUS_PENDING;
1865 }
1866
1867 CMD_STATUS csBeacon_xmit(struct vnt_private *pDevice, PSTxMgmtPacket pPacket)
1868 {
1869         unsigned char byPktType;
1870         unsigned char *pbyBuffer = (unsigned char *)pDevice->tx_beacon_bufs;
1871         unsigned int cbFrameSize = pPacket->cbMPDULen + WLAN_FCS_LEN;
1872         unsigned int cbHeaderSize = 0;
1873         struct vnt_tx_short_buf_head *short_head =
1874                                 (struct vnt_tx_short_buf_head *)pbyBuffer;
1875         PS802_11Header   pMACHeader;
1876         unsigned short wCurrentRate;
1877
1878         memset(short_head, 0, sizeof(*short_head));
1879
1880         if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
1881                 wCurrentRate = RATE_6M;
1882                 byPktType = PK_TYPE_11A;
1883         } else {
1884                 wCurrentRate = RATE_2M;
1885                 byPktType = PK_TYPE_11B;
1886         }
1887
1888         //Set Preamble type always long
1889         pDevice->byPreambleType = PREAMBLE_LONG;
1890
1891         /* Set FIFOCTL_GENINT */
1892         short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT);
1893
1894         /* Set packet type & Get Duration */
1895         if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
1896                 short_head->duration =
1897                         cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A,
1898                                     cbFrameSize, byPktType, wCurrentRate, false,
1899                                     0, 0, 1, AUTO_FB_NONE));
1900         } else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
1901                 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
1902
1903                 short_head->duration =
1904                         cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B,
1905                                     cbFrameSize, byPktType, wCurrentRate, false,
1906                                     0, 0, 1, AUTO_FB_NONE));
1907         }
1908
1909         vnt_get_phy_field(pDevice, cbFrameSize,
1910                           wCurrentRate, byPktType, &short_head->ab);
1911
1912         /* Get TimeStampOff */
1913         short_head->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
1914         cbHeaderSize = sizeof(struct vnt_tx_short_buf_head);
1915
1916         //Generate Beacon Header
1917         pMACHeader = (PS802_11Header)(pbyBuffer + cbHeaderSize);
1918         memcpy(pMACHeader, pPacket->p80211Header, pPacket->cbMPDULen);
1919
1920         pMACHeader->wDurationID = 0;
1921         pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
1922         pDevice->wSeqCounter++;
1923         if (pDevice->wSeqCounter > 0x0fff)
1924                 pDevice->wSeqCounter = 0;
1925
1926         // Set Beacon buffer length
1927         pDevice->wBCNBufLen = pPacket->cbMPDULen + cbHeaderSize;
1928
1929         MACvSetCurrBCNTxDescAddr(pDevice->PortOffset, (pDevice->tx_beacon_dma));
1930
1931         MACvSetCurrBCNLength(pDevice->PortOffset, pDevice->wBCNBufLen);
1932         // Set auto Transmit on
1933         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
1934         // Poll Transmit the adapter
1935         MACvTransmitBCN(pDevice->PortOffset);
1936
1937         return CMD_STATUS_PENDING;
1938 }
1939
1940 unsigned int
1941 cbGetFragCount(
1942         struct vnt_private *pDevice,
1943         PSKeyItem        pTransmitKey,
1944         unsigned int cbFrameBodySize,
1945         PSEthernetHeader psEthHeader
1946 )
1947 {
1948         unsigned int cbMACHdLen;
1949         unsigned int cbFrameSize;
1950         unsigned int cbFragmentSize; //Hdr+(IV)+payoad+(MIC)+(ICV)+FCS
1951         unsigned int cbFragPayloadSize;
1952         unsigned int cbLastFragPayloadSize;
1953         unsigned int cbIVlen = 0;
1954         unsigned int cbICVlen = 0;
1955         unsigned int cbMIClen = 0;
1956         unsigned int cbFCSlen = 4;
1957         unsigned int uMACfragNum = 1;
1958         bool bNeedACK;
1959
1960         if ((pDevice->op_mode == NL80211_IFTYPE_ADHOC) ||
1961             (pDevice->op_mode == NL80211_IFTYPE_AP)) {
1962                 if (is_multicast_ether_addr(&(psEthHeader->abyDstAddr[0])))
1963                         bNeedACK = false;
1964                 else
1965                         bNeedACK = true;
1966         } else {
1967                 // MSDUs in Infra mode always need ACK
1968                 bNeedACK = true;
1969         }
1970
1971         if (pDevice->bLongHeader)
1972                 cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;
1973         else
1974                 cbMACHdLen = WLAN_HDR_ADDR3_LEN;
1975
1976         if (pDevice->bEncryptionEnable == true) {
1977                 if (pTransmitKey == NULL) {
1978                         if ((pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) ||
1979                             (pDevice->pMgmt->eAuthenMode < WMAC_AUTH_WPA)) {
1980                                 cbIVlen = 4;
1981                                 cbICVlen = 4;
1982                         } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
1983                                 cbIVlen = 8;//IV+ExtIV
1984                                 cbMIClen = 8;
1985                                 cbICVlen = 4;
1986                         } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
1987                                 cbIVlen = 8;//RSN Header
1988                                 cbICVlen = 8;//MIC
1989                         }
1990                 } else if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
1991                         cbIVlen = 4;
1992                         cbICVlen = 4;
1993                 } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
1994                         cbIVlen = 8;//IV+ExtIV
1995                         cbMIClen = 8;
1996                         cbICVlen = 4;
1997                 } else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
1998                         cbIVlen = 8;//RSN Header
1999                         cbICVlen = 8;//MIC
2000                 }
2001         }
2002
2003         cbFrameSize = cbMACHdLen + cbIVlen + (cbFrameBodySize + cbMIClen) + cbICVlen + cbFCSlen;
2004
2005         if ((cbFrameSize > pDevice->wFragmentationThreshold) && (bNeedACK == true)) {
2006                 // Fragmentation
2007                 cbFragmentSize = pDevice->wFragmentationThreshold;
2008                 cbFragPayloadSize = cbFragmentSize - cbMACHdLen - cbIVlen - cbICVlen - cbFCSlen;
2009                 uMACfragNum = (unsigned short) ((cbFrameBodySize + cbMIClen) / cbFragPayloadSize);
2010                 cbLastFragPayloadSize = (cbFrameBodySize + cbMIClen) % cbFragPayloadSize;
2011                 if (cbLastFragPayloadSize == 0)
2012                         cbLastFragPayloadSize = cbFragPayloadSize;
2013                 else
2014                         uMACfragNum++;
2015         }
2016         return uMACfragNum;
2017 }
2018
2019 void vDMA0_tx_80211(struct vnt_private *pDevice, struct sk_buff *skb,
2020                     unsigned char *pbMPDU, unsigned int cbMPDULen)
2021 {
2022         PSTxDesc        pFrstTD;
2023         unsigned char byPktType;
2024         unsigned char *pbyTxBufferAddr;
2025         void *pvRTS;
2026         void *pvCTS;
2027         void *pvTxDataHd;
2028         unsigned int uDuration;
2029         unsigned int cbReqCount;
2030         PS802_11Header  pMACHeader;
2031         unsigned int cbHeaderSize;
2032         unsigned int cbFrameBodySize;
2033         bool bNeedACK;
2034         bool bIsPSPOLL = false;
2035         PSTxBufHead     pTxBufHead;
2036         unsigned int cbFrameSize;
2037         unsigned int cbIVlen = 0;
2038         unsigned int cbICVlen = 0;
2039         unsigned int cbMIClen = 0;
2040         unsigned int cbFCSlen = 4;
2041         unsigned int uPadding = 0;
2042         unsigned int cbMICHDR = 0;
2043         unsigned int uLength = 0;
2044         u32 dwMICKey0, dwMICKey1;
2045         u32 dwMIC_Priority;
2046         u32 *pdwMIC_L;
2047         u32 *pdwMIC_R;
2048         unsigned short wTxBufSize;
2049         unsigned int cbMacHdLen;
2050         SEthernetHeader sEthHeader;
2051         void *pvRrvTime;
2052         void *pMICHDR;
2053         PSMgmtObject    pMgmt = pDevice->pMgmt;
2054         unsigned short wCurrentRate = RATE_1M;
2055         PUWLAN_80211HDR  p80211Header;
2056         unsigned int uNodeIndex = 0;
2057         bool bNodeExist = false;
2058         SKeyItem        STempKey;
2059         PSKeyItem       pTransmitKey = NULL;
2060         unsigned char *pbyIVHead;
2061         unsigned char *pbyPayloadHead;
2062         unsigned char *pbyMacHdr;
2063
2064         unsigned int cbExtSuppRate = 0;
2065
2066         pvRrvTime = pMICHDR = pvRTS = pvCTS = pvTxDataHd = NULL;
2067
2068         if (cbMPDULen <= WLAN_HDR_ADDR3_LEN)
2069                 cbFrameBodySize = 0;
2070         else
2071                 cbFrameBodySize = cbMPDULen - WLAN_HDR_ADDR3_LEN;
2072
2073         p80211Header = (PUWLAN_80211HDR)pbMPDU;
2074
2075         pFrstTD = pDevice->apCurrTD[TYPE_TXDMA0];
2076         pbyTxBufferAddr = (unsigned char *)pFrstTD->pTDInfo->buf;
2077         pTxBufHead = (PSTxBufHead) pbyTxBufferAddr;
2078         wTxBufSize = sizeof(STxBufHead);
2079         memset(pTxBufHead, 0, wTxBufSize);
2080
2081         if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2082                 wCurrentRate = RATE_6M;
2083                 byPktType = PK_TYPE_11A;
2084         } else {
2085                 wCurrentRate = RATE_1M;
2086                 byPktType = PK_TYPE_11B;
2087         }
2088
2089         // SetPower will cause error power TX state for OFDM Date packet in TX buffer.
2090         // 2004.11.11 Kyle -- Using OFDM power to tx MngPkt will decrease the connection capability.
2091         //                    And cmd timer will wait data pkt TX to finish before scanning so it's OK
2092         //                    to set power here.
2093         if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING)
2094                 RFbSetPower(pDevice, wCurrentRate, pDevice->byCurrentCh);
2095         else
2096                 RFbSetPower(pDevice, wCurrentRate, pMgmt->uCurrChannel);
2097
2098         pTxBufHead->byTxPower = pDevice->byCurPwr;
2099
2100         //+++++++++++++++++++++ Patch VT3253 A1 performance +++++++++++++++++++++++++++
2101         if (pDevice->byFOETuning) {
2102                 if ((p80211Header->sA3.wFrameCtl & TYPE_DATE_NULL) == TYPE_DATE_NULL) {
2103                         wCurrentRate = RATE_24M;
2104                         byPktType = PK_TYPE_11GA;
2105                 }
2106         }
2107
2108         pr_debug("vDMA0_tx_80211: p80211Header->sA3.wFrameCtl = %x\n",
2109                  p80211Header->sA3.wFrameCtl);
2110
2111         //Set packet type
2112         if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
2113                 pTxBufHead->wFIFOCtl = 0;
2114         } else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
2115                 pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
2116         } else if (byPktType == PK_TYPE_11GB) {//0000 0010 0000 0000
2117                 pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
2118         } else if (byPktType == PK_TYPE_11GA) {//0000 0011 0000 0000
2119                 pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
2120         }
2121
2122         pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
2123         pTxBufHead->wTimeStamp = cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
2124
2125         if (is_multicast_ether_addr(&(p80211Header->sA3.abyAddr1[0]))) {
2126                 bNeedACK = false;
2127                 if (pDevice->bEnableHostWEP) {
2128                         uNodeIndex = 0;
2129                         bNodeExist = true;
2130                 }
2131         } else {
2132                 if (pDevice->bEnableHostWEP) {
2133                         if (BSSDBbIsSTAInNodeDB(pDevice->pMgmt, (unsigned char *)(p80211Header->sA3.abyAddr1), &uNodeIndex))
2134                                 bNodeExist = true;
2135                 }
2136                 bNeedACK = true;
2137                 pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
2138         }
2139
2140         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) ||
2141             (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
2142                 pTxBufHead->wFIFOCtl |= FIFOCTL_LRETRY;
2143         }
2144
2145         pTxBufHead->wFIFOCtl |= (FIFOCTL_GENINT | FIFOCTL_ISDMA0);
2146
2147         if ((p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_CTL_PSPOLL) {
2148                 bIsPSPOLL = true;
2149                 cbMacHdLen = WLAN_HDR_ADDR2_LEN;
2150         } else {
2151                 cbMacHdLen = WLAN_HDR_ADDR3_LEN;
2152         }
2153
2154         // hostapd deamon ext support rate patch
2155         if (WLAN_GET_FC_FSTYPE(p80211Header->sA4.wFrameCtl) == WLAN_FSTYPE_ASSOCRESP) {
2156                 if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len != 0)
2157                         cbExtSuppRate += ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len + WLAN_IEHDR_LEN;
2158
2159                 if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len != 0)
2160                         cbExtSuppRate += ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len + WLAN_IEHDR_LEN;
2161
2162                 if (cbExtSuppRate > 0)
2163                         cbFrameBodySize = WLAN_ASSOCRESP_OFF_SUPP_RATES;
2164         }
2165
2166         //Set FRAGCTL_MACHDCNT
2167         pTxBufHead->wFragCtl |= cpu_to_le16((unsigned short)cbMacHdLen << 10);
2168
2169         // Notes:
2170         // Although spec says MMPDU can be fragmented; In most cases,
2171         // no one will send a MMPDU under fragmentation. With RTS may occur.
2172         pDevice->bAES = false;  //Set FRAGCTL_WEPTYP
2173
2174         if (WLAN_GET_FC_ISWEP(p80211Header->sA4.wFrameCtl) != 0) {
2175                 if (pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) {
2176                         cbIVlen = 4;
2177                         cbICVlen = 4;
2178                         pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
2179                 } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2180                         cbIVlen = 8;//IV+ExtIV
2181                         cbMIClen = 8;
2182                         cbICVlen = 4;
2183                         pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
2184                         //We need to get seed here for filling TxKey entry.
2185                 } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2186                         cbIVlen = 8;//RSN Header
2187                         cbICVlen = 8;//MIC
2188                         cbMICHDR = sizeof(struct vnt_mic_hdr);
2189                         pTxBufHead->wFragCtl |= FRAGCTL_AES;
2190                         pDevice->bAES = true;
2191                 }
2192                 //MAC Header should be padding 0 to DW alignment.
2193                 uPadding = 4 - (cbMacHdLen%4);
2194                 uPadding %= 4;
2195         }
2196
2197         cbFrameSize = cbMacHdLen + cbFrameBodySize + cbIVlen + cbMIClen + cbICVlen + cbFCSlen + cbExtSuppRate;
2198
2199         //Set FIFOCTL_GrpAckPolicy
2200         if (pDevice->bGrpAckPolicy == true) //0000 0100 0000 0000
2201                 pTxBufHead->wFIFOCtl |= FIFOCTL_GRPACK;
2202
2203         //the rest of pTxBufHead->wFragCtl:FragTyp will be set later in s_vFillFragParameter()
2204
2205         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
2206
2207                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
2208                 pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize +
2209                                         sizeof(struct vnt_rrv_time_cts));
2210                 pvRTS = NULL;
2211                 pvCTS = (struct vnt_cts *)(pbyTxBufferAddr + wTxBufSize +
2212                                 sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
2213                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
2214                         sizeof(struct vnt_rrv_time_cts) + cbMICHDR + sizeof(struct vnt_cts));
2215                 cbHeaderSize = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
2216                                 cbMICHDR + sizeof(struct vnt_cts) + sizeof(struct vnt_tx_datahead_g);
2217
2218         } else {//802.11a/b packet
2219
2220                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
2221                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr +
2222                                 wTxBufSize + sizeof(struct vnt_rrv_time_ab));
2223                 pvRTS = NULL;
2224                 pvCTS = NULL;
2225                 pvTxDataHd = (void *)(pbyTxBufferAddr +
2226                         wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
2227                 cbHeaderSize = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
2228                                 cbMICHDR + sizeof(struct vnt_tx_datahead_ab);
2229
2230         }
2231
2232         memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderSize - wTxBufSize));
2233         ether_addr_copy(&(sEthHeader.abyDstAddr[0]),
2234                         &(p80211Header->sA3.abyAddr1[0]));
2235         ether_addr_copy(&(sEthHeader.abySrcAddr[0]),
2236                         &(p80211Header->sA3.abyAddr2[0]));
2237         //=========================
2238         //    No Fragmentation
2239         //=========================
2240         pTxBufHead->wFragCtl |= (unsigned short)FRAGCTL_NONFRAG;
2241
2242         //Fill FIFO,RrvTime,RTS,and CTS
2243         s_vGenerateTxParameter(pDevice, byPktType, pbyTxBufferAddr, pvRrvTime, pvRTS, pvCTS,
2244                                cbFrameSize, bNeedACK, TYPE_TXDMA0, &sEthHeader, wCurrentRate);
2245
2246         //Fill DataHead
2247         uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, TYPE_TXDMA0, bNeedACK,
2248                                     0, 0, 1, AUTO_FB_NONE, wCurrentRate);
2249
2250         pMACHeader = (PS802_11Header) (pbyTxBufferAddr + cbHeaderSize);
2251
2252         cbReqCount = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen + (cbFrameBodySize + cbMIClen) + cbExtSuppRate;
2253
2254         pbyMacHdr = (unsigned char *)(pbyTxBufferAddr + cbHeaderSize);
2255         pbyPayloadHead = (unsigned char *)(pbyMacHdr + cbMacHdLen + uPadding + cbIVlen);
2256         pbyIVHead = (unsigned char *)(pbyMacHdr + cbMacHdLen + uPadding);
2257
2258         // Copy the Packet into a tx Buffer
2259         memcpy(pbyMacHdr, pbMPDU, cbMacHdLen);
2260
2261         // version set to 0, patch for hostapd deamon
2262         pMACHeader->wFrameCtl &= cpu_to_le16(0xfffc);
2263         memcpy(pbyPayloadHead, (pbMPDU + cbMacHdLen), cbFrameBodySize);
2264
2265         // replace support rate, patch for hostapd deamon(only support 11M)
2266         if (WLAN_GET_FC_FSTYPE(p80211Header->sA4.wFrameCtl) == WLAN_FSTYPE_ASSOCRESP) {
2267                 if (cbExtSuppRate != 0) {
2268                         if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len != 0)
2269                                 memcpy((pbyPayloadHead + cbFrameBodySize),
2270                                        pMgmt->abyCurrSuppRates,
2271                                        ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len + WLAN_IEHDR_LEN
2272 );
2273                         if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len != 0)
2274                                 memcpy((pbyPayloadHead + cbFrameBodySize) + ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len + WLAN_IEHDR_LEN,
2275                                        pMgmt->abyCurrExtSuppRates,
2276                                        ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len + WLAN_IEHDR_LEN
2277 );
2278                 }
2279         }
2280
2281         // Set wep
2282         if (WLAN_GET_FC_ISWEP(p80211Header->sA4.wFrameCtl) != 0) {
2283                 if (pDevice->bEnableHostWEP) {
2284                         pTransmitKey = &STempKey;
2285                         pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2286                         pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2287                         pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2288                         pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2289                         pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2290                         memcpy(pTransmitKey->abyKey,
2291                                &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2292                                pTransmitKey->uKeyLength
2293 );
2294                 }
2295
2296                 if ((pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
2297                         dwMICKey0 = *(u32 *)(&pTransmitKey->abyKey[16]);
2298                         dwMICKey1 = *(u32 *)(&pTransmitKey->abyKey[20]);
2299
2300                         // DO Software Michael
2301                         MIC_vInit(dwMICKey0, dwMICKey1);
2302                         MIC_vAppend((unsigned char *)&(sEthHeader.abyDstAddr[0]), 12);
2303                         dwMIC_Priority = 0;
2304                         MIC_vAppend((unsigned char *)&dwMIC_Priority, 4);
2305                         pr_debug("DMA0_tx_8021:MIC KEY: %X, %X\n",
2306                                  dwMICKey0, dwMICKey1);
2307
2308                         uLength = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen;
2309
2310                         MIC_vAppend((pbyTxBufferAddr + uLength), cbFrameBodySize);
2311
2312                         pdwMIC_L = (u32 *)(pbyTxBufferAddr + uLength + cbFrameBodySize);
2313                         pdwMIC_R = (u32 *)(pbyTxBufferAddr + uLength + cbFrameBodySize + 4);
2314
2315                         MIC_vGetMIC(pdwMIC_L, pdwMIC_R);
2316                         MIC_vUnInit();
2317
2318                         if (pDevice->bTxMICFail == true) {
2319                                 *pdwMIC_L = 0;
2320                                 *pdwMIC_R = 0;
2321                                 pDevice->bTxMICFail = false;
2322                         }
2323
2324                         pr_debug("uLength: %d, %d\n", uLength, cbFrameBodySize);
2325                         pr_debug("cbReqCount:%d, %d, %d, %d\n",
2326                                  cbReqCount, cbHeaderSize, uPadding, cbIVlen);
2327                         pr_debug("MIC:%x, %x\n", *pdwMIC_L, *pdwMIC_R);
2328
2329                 }
2330
2331                 s_vFillTxKey(pDevice, (unsigned char *)(pTxBufHead->adwTxKey), pbyIVHead, pTransmitKey,
2332                              pbyMacHdr, (unsigned short)cbFrameBodySize, (unsigned char *)pMICHDR);
2333
2334                 if (pDevice->bEnableHostWEP) {
2335                         pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16 = pTransmitKey->dwTSC47_16;
2336                         pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0 = pTransmitKey->wTSC15_0;
2337                 }
2338
2339                 if ((pDevice->byLocalID <= REV_ID_VT3253_A1))
2340                         s_vSWencryption(pDevice, pTransmitKey, pbyPayloadHead, (unsigned short)(cbFrameBodySize + cbMIClen));
2341         }
2342
2343         pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
2344         pDevice->wSeqCounter++;
2345         if (pDevice->wSeqCounter > 0x0fff)
2346                 pDevice->wSeqCounter = 0;
2347
2348         if (bIsPSPOLL) {
2349                 // The MAC will automatically replace the Duration-field of MAC header by Duration-field
2350                 // of  FIFO control header.
2351                 // This will cause AID-field of PS-POLL packet be incorrect (Because PS-POLL's AID field is
2352                 // in the same place of other packet's Duration-field).
2353                 // And it will cause Cisco-AP to issue Disassociation-packet
2354                 if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
2355                         ((struct vnt_tx_datahead_g *)pvTxDataHd)->duration_a = cpu_to_le16(p80211Header->sA2.wDurationID);
2356                         ((struct vnt_tx_datahead_g *)pvTxDataHd)->duration_b = cpu_to_le16(p80211Header->sA2.wDurationID);
2357                 } else {
2358                         ((struct vnt_tx_datahead_ab *)pvTxDataHd)->duration = cpu_to_le16(p80211Header->sA2.wDurationID);
2359                 }
2360         }
2361
2362         // first TD is the only TD
2363         //Set TSR1 & ReqCount in TxDescHead
2364         pFrstTD->pTDInfo->skb = skb;
2365         pFrstTD->m_td1TD1.byTCR = (TCR_STP | TCR_EDP | EDMSDU);
2366         pFrstTD->pTDInfo->skb_dma = pFrstTD->pTDInfo->buf_dma;
2367         pFrstTD->m_td1TD1.wReqCount = cpu_to_le16(cbReqCount);
2368         pFrstTD->buff_addr = cpu_to_le32(pFrstTD->pTDInfo->skb_dma);
2369         pFrstTD->pTDInfo->byFlags = 0;
2370         pFrstTD->pTDInfo->byFlags |= TD_FLAGS_PRIV_SKB;
2371
2372         if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2373                 // Disable PS
2374                 MACbPSWakeup(pDevice->PortOffset);
2375         }
2376         pDevice->bPWBitOn = false;
2377
2378         wmb();
2379         pFrstTD->m_td0TD0.f1Owner = OWNED_BY_NIC;
2380         wmb();
2381
2382         pDevice->iTDUsed[TYPE_TXDMA0]++;
2383
2384         if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 1)
2385                 pr_debug(" available td0 <= 1\n");
2386
2387         pDevice->apCurrTD[TYPE_TXDMA0] = pFrstTD->next;
2388
2389         // Poll Transmit the adapter
2390         MACvTransmit0(pDevice->PortOffset);
2391 }
2392
2393 static void vnt_fill_txkey(struct ieee80211_hdr *hdr, u8 *key_buffer,
2394                            struct ieee80211_key_conf *tx_key,
2395                            struct sk_buff *skb, u16 payload_len,
2396                            struct vnt_mic_hdr *mic_hdr)
2397 {
2398         struct ieee80211_key_seq seq;
2399         u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
2400
2401         /* strip header and icv len from payload */
2402         payload_len -= ieee80211_get_hdrlen_from_skb(skb);
2403         payload_len -= tx_key->icv_len;
2404
2405         switch (tx_key->cipher) {
2406         case WLAN_CIPHER_SUITE_WEP40:
2407         case WLAN_CIPHER_SUITE_WEP104:
2408                 memcpy(key_buffer, iv, 3);
2409                 memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
2410
2411                 if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
2412                         memcpy(key_buffer + 8, iv, 3);
2413                         memcpy(key_buffer + 11,
2414                                tx_key->key, WLAN_KEY_LEN_WEP40);
2415                 }
2416
2417                 break;
2418         case WLAN_CIPHER_SUITE_TKIP:
2419                 ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
2420
2421                 break;
2422         case WLAN_CIPHER_SUITE_CCMP:
2423
2424                 if (!mic_hdr)
2425                         return;
2426
2427                 mic_hdr->id = 0x59;
2428                 mic_hdr->payload_len = cpu_to_be16(payload_len);
2429                 ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
2430
2431                 ieee80211_get_key_tx_seq(tx_key, &seq);
2432
2433                 memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
2434
2435                 if (ieee80211_has_a4(hdr->frame_control))
2436                         mic_hdr->hlen = cpu_to_be16(28);
2437                 else
2438                         mic_hdr->hlen = cpu_to_be16(22);
2439
2440                 ether_addr_copy(mic_hdr->addr1, hdr->addr1);
2441                 ether_addr_copy(mic_hdr->addr2, hdr->addr2);
2442                 ether_addr_copy(mic_hdr->addr3, hdr->addr3);
2443
2444                 mic_hdr->frame_control = cpu_to_le16(
2445                         le16_to_cpu(hdr->frame_control) & 0xc78f);
2446                 mic_hdr->seq_ctrl = cpu_to_le16(
2447                                 le16_to_cpu(hdr->seq_ctrl) & 0xf);
2448
2449                 if (ieee80211_has_a4(hdr->frame_control))
2450                         ether_addr_copy(mic_hdr->addr4, hdr->addr4);
2451
2452                 memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);
2453
2454                 break;
2455         default:
2456                 break;
2457         }
2458 }
2459
2460 int vnt_generate_fifo_header(struct vnt_private *priv, u32 dma_idx,
2461                              PSTxDesc head_td, struct sk_buff *skb)
2462 {
2463         PDEVICE_TD_INFO td_info = head_td->pTDInfo;
2464         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2465         struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
2466         struct ieee80211_rate *rate;
2467         struct ieee80211_key_conf *tx_key;
2468         struct ieee80211_hdr *hdr;
2469         struct vnt_tx_fifo_head *tx_buffer_head =
2470                         (struct vnt_tx_fifo_head *)td_info->buf;
2471         u32 frag;
2472         u16 tx_body_size = skb->len, current_rate;
2473         u8 pkt_type;
2474         bool is_pspoll = false;
2475
2476         memset(tx_buffer_head, 0, sizeof(*tx_buffer_head));
2477
2478         hdr = (struct ieee80211_hdr *)(skb->data);
2479
2480         rate = ieee80211_get_tx_rate(priv->hw, info);
2481
2482         current_rate = rate->hw_value;
2483         if (priv->wCurrentRate != current_rate &&
2484                         !(priv->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
2485                 priv->wCurrentRate = current_rate;
2486
2487                 RFbSetPower(priv, priv->wCurrentRate,
2488                             priv->hw->conf.chandef.chan->hw_value);
2489         }
2490
2491         if (current_rate > RATE_11M)
2492                 pkt_type = (u8)priv->byPacketType;
2493         else
2494                 pkt_type = PK_TYPE_11B;
2495
2496         /*Set fifo controls */
2497         if (pkt_type == PK_TYPE_11A)
2498                 tx_buffer_head->fifo_ctl = 0;
2499         else if (pkt_type == PK_TYPE_11B)
2500                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
2501         else if (pkt_type == PK_TYPE_11GB)
2502                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
2503         else if (pkt_type == PK_TYPE_11GA)
2504                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
2505
2506         /* generate interrupt */
2507         tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT);
2508
2509         if (!ieee80211_is_data(hdr->frame_control)) {
2510                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
2511                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_ISDMA0);
2512                 tx_buffer_head->time_stamp =
2513                         cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
2514         } else {
2515                 tx_buffer_head->time_stamp =
2516                         cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
2517         }
2518
2519         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
2520                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
2521
2522         if (ieee80211_has_retry(hdr->frame_control))
2523                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
2524
2525         if (tx_rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
2526                 priv->byPreambleType = PREAMBLE_SHORT;
2527         else
2528                 priv->byPreambleType = PREAMBLE_LONG;
2529
2530         if (tx_rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
2531                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
2532
2533         if (ieee80211_has_a4(hdr->frame_control)) {
2534                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
2535                 priv->bLongHeader = true;
2536         }
2537
2538         if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
2539                 is_pspoll = true;
2540
2541         tx_buffer_head->frag_ctl =
2542                         cpu_to_le16(ieee80211_get_hdrlen_from_skb(skb) << 10);
2543
2544         if (info->control.hw_key) {
2545                 tx_key = info->control.hw_key;
2546
2547                 switch (info->control.hw_key->cipher) {
2548                 case WLAN_CIPHER_SUITE_WEP40:
2549                 case WLAN_CIPHER_SUITE_WEP104:
2550                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
2551                         break;
2552                 case WLAN_CIPHER_SUITE_TKIP:
2553                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
2554                         break;
2555                 case WLAN_CIPHER_SUITE_CCMP:
2556                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
2557                 default:
2558                         break;
2559                 }
2560         }
2561
2562         tx_buffer_head->current_rate = cpu_to_le16(current_rate);
2563
2564         /* legacy rates TODO use ieee80211_tx_rate */
2565         if (current_rate >= RATE_18M && ieee80211_is_data(hdr->frame_control)) {
2566                 if (priv->byAutoFBCtrl == AUTO_FB_0)
2567                         tx_buffer_head->fifo_ctl |=
2568                                                 cpu_to_le16(FIFOCTL_AUTO_FB_0);
2569                 else if (priv->byAutoFBCtrl == AUTO_FB_1)
2570                         tx_buffer_head->fifo_ctl |=
2571                                                 cpu_to_le16(FIFOCTL_AUTO_FB_1);
2572
2573         }
2574
2575         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
2576
2577         s_cbFillTxBufHead(priv, pkt_type, (u8 *)tx_buffer_head, skb->len,
2578                           dma_idx, head_td, NULL, (u8 *)skb->data,
2579                           false, NULL, is_pspoll, &frag);
2580
2581         if (info->control.hw_key) {
2582                 tx_key = info->control.hw_key;
2583                 if (tx_key->keylen > 0)
2584                         vnt_fill_txkey(hdr, tx_buffer_head->tx_key,
2585                                 tx_key, skb, tx_body_size, td_info->mic_hdr);
2586         }
2587
2588         return 0;
2589 }
2590
2591 static int vnt_beacon_xmit(struct vnt_private *priv,
2592                            struct sk_buff *skb)
2593 {
2594         struct vnt_tx_short_buf_head *short_head =
2595                 (struct vnt_tx_short_buf_head *)priv->tx_beacon_bufs;
2596         struct ieee80211_mgmt *mgmt_hdr = (struct ieee80211_mgmt *)
2597                                 (priv->tx_beacon_bufs + sizeof(*short_head));
2598         struct ieee80211_tx_info *info;
2599         u32 frame_size = skb->len + 4;
2600         u16 current_rate;
2601
2602         memset(priv->tx_beacon_bufs, 0, sizeof(*short_head));
2603
2604         if (priv->byBBType == BB_TYPE_11A) {
2605                 current_rate = RATE_6M;
2606
2607                 /* Get SignalField,ServiceField,Length */
2608                 vnt_get_phy_field(priv, frame_size, current_rate,
2609                                   PK_TYPE_11A, &short_head->ab);
2610
2611                 /* Get Duration and TimeStampOff */
2612                 short_head->duration =
2613                         cpu_to_le16((u16)s_uGetDataDuration(priv, DATADUR_B,
2614                                     frame_size, PK_TYPE_11A, current_rate,
2615                                     false, 0, 0, 1, AUTO_FB_NONE));
2616
2617                 short_head->time_stamp_off =
2618                                 vnt_time_stamp_off(priv, current_rate);
2619         } else {
2620                 current_rate = RATE_1M;
2621                 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
2622
2623                 /* Get SignalField,ServiceField,Length */
2624                 vnt_get_phy_field(priv, frame_size, current_rate,
2625                                   PK_TYPE_11B, &short_head->ab);
2626
2627                 /* Get Duration and TimeStampOff */
2628                 short_head->duration =
2629                         cpu_to_le16((u16)s_uGetDataDuration(priv, DATADUR_B,
2630                                     frame_size, PK_TYPE_11B, current_rate,
2631                                     false, 0, 0, 1, AUTO_FB_NONE));
2632
2633                 short_head->time_stamp_off =
2634                         vnt_time_stamp_off(priv, current_rate);
2635         }
2636
2637         short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT);
2638
2639         /* Copy Beacon */
2640         memcpy(mgmt_hdr, skb->data, skb->len);
2641
2642         /* time stamp always 0 */
2643         mgmt_hdr->u.beacon.timestamp = 0;
2644
2645         info = IEEE80211_SKB_CB(skb);
2646         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2647                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
2648
2649                 hdr->duration_id = 0;
2650                 hdr->seq_ctrl = cpu_to_le16(priv->wSeqCounter << 4);
2651         }
2652
2653         priv->wSeqCounter++;
2654         if (priv->wSeqCounter > 0x0fff)
2655                 priv->wSeqCounter = 0;
2656
2657         priv->wBCNBufLen = sizeof(*short_head) + skb->len;
2658
2659         MACvSetCurrBCNTxDescAddr(priv->PortOffset, priv->tx_beacon_dma);
2660
2661         MACvSetCurrBCNLength(priv->PortOffset, priv->wBCNBufLen);
2662         /* Set auto Transmit on */
2663         MACvRegBitsOn(priv->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
2664         /* Poll Transmit the adapter */
2665         MACvTransmitBCN(priv->PortOffset);
2666
2667         return 0;
2668 }
2669
2670 int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
2671 {
2672         struct sk_buff *beacon;
2673
2674         beacon = ieee80211_beacon_get(priv->hw, vif);
2675         if (!beacon)
2676                 return -ENOMEM;
2677
2678         if (vnt_beacon_xmit(priv, beacon)) {
2679                 ieee80211_free_txskb(priv->hw, beacon);
2680                 return -ENODEV;
2681         }
2682
2683         return 0;
2684 }
2685
2686 int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
2687                       struct ieee80211_bss_conf *conf)
2688 {
2689         int ret;
2690
2691         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
2692
2693         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
2694
2695         CARDvSetFirstNextTBTT(priv->PortOffset, conf->beacon_int);
2696
2697         CARDbSetBeaconPeriod(priv, conf->beacon_int);
2698
2699         ret = vnt_beacon_make(priv, vif);
2700
2701         return ret;
2702 }