staging: vt6655: deadcode remove aes_ccmp/rc4/tkip
[cascardo/linux.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 "tcrc.h"
60 #include "wctl.h"
61 #include "rf.h"
62
63 /*---------------------  Static Definitions -------------------------*/
64
65 /*---------------------  Static Classes  ----------------------------*/
66
67 /*---------------------  Static Variables  --------------------------*/
68
69 /*---------------------  Static Functions  --------------------------*/
70
71 /*---------------------  Static Definitions -------------------------*/
72 #define CRITICAL_PACKET_LEN      256    // if packet size < 256 -> in-direct send
73                                         //    packet size >= 256 -> direct send
74
75 static const unsigned short wTimeStampOff[2][MAX_RATE] = {
76         {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23}, // Long Preamble
77         {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23}, // Short Preamble
78 };
79
80 static const unsigned short wFB_Opt0[2][5] = {
81         {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, // fallback_rate0
82         {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, // fallback_rate1
83 };
84 static const unsigned short wFB_Opt1[2][5] = {
85         {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, // fallback_rate0
86         {RATE_6M , RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, // fallback_rate1
87 };
88
89 #define RTSDUR_BB       0
90 #define RTSDUR_BA       1
91 #define RTSDUR_AA       2
92 #define CTSDUR_BA       3
93 #define RTSDUR_BA_F0    4
94 #define RTSDUR_AA_F0    5
95 #define RTSDUR_BA_F1    6
96 #define RTSDUR_AA_F1    7
97 #define CTSDUR_BA_F0    8
98 #define CTSDUR_BA_F1    9
99 #define DATADUR_B       10
100 #define DATADUR_A       11
101 #define DATADUR_A_F0    12
102 #define DATADUR_A_F1    13
103
104 /*---------------------  Static Functions  --------------------------*/
105 static
106 void
107 s_vFillRTSHead(
108         struct vnt_private *pDevice,
109         unsigned char byPktType,
110         void *pvRTS,
111         unsigned int    cbFrameLength,
112         bool bNeedAck,
113         bool bDisCRC,
114         struct ieee80211_hdr *hdr,
115         unsigned short wCurrentRate,
116         unsigned char byFBOption
117 );
118
119 static
120 void
121 s_vGenerateTxParameter(
122         struct vnt_private *pDevice,
123         unsigned char byPktType,
124         void *pTxBufHead,
125         void *pvRrvTime,
126         void *pvRTS,
127         void *pvCTS,
128         unsigned int    cbFrameSize,
129         bool bNeedACK,
130         unsigned int    uDMAIdx,
131         void *psEthHeader,
132         unsigned short wCurrentRate
133 );
134
135 static unsigned int
136 s_cbFillTxBufHead(struct vnt_private *pDevice, unsigned char byPktType,
137                   unsigned char *pbyTxBufferAddr, unsigned int cbFrameBodySize,
138                   unsigned int uDMAIdx, PSTxDesc pHeadTD,
139                   unsigned char *pPacket, unsigned int uNodeIndex,
140                   unsigned int *puMACfragNum);
141
142 static
143 __le16
144 s_uFillDataHead(
145         struct vnt_private *pDevice,
146         unsigned char byPktType,
147         void *pTxDataHead,
148         unsigned int cbFrameLength,
149         unsigned int uDMAIdx,
150         bool bNeedAck,
151         unsigned int uFragIdx,
152         unsigned int cbLastFragmentSize,
153         unsigned int uMACfragNum,
154         unsigned char byFBOption,
155         unsigned short wCurrentRate,
156         bool is_pspoll
157 );
158
159 /*---------------------  Export Variables  --------------------------*/
160
161 static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
162 {
163         return cpu_to_le16(wTimeStampOff[priv->byPreambleType % 2]
164                                                         [rate % MAX_RATE]);
165 }
166
167 /*byPktType : PK_TYPE_11A     0
168   PK_TYPE_11B     1
169   PK_TYPE_11GB    2
170   PK_TYPE_11GA    3
171 */
172 static
173 unsigned int
174 s_uGetTxRsvTime(
175         struct vnt_private *pDevice,
176         unsigned char byPktType,
177         unsigned int cbFrameLength,
178         unsigned short wRate,
179         bool bNeedAck
180 )
181 {
182         unsigned int uDataTime, uAckTime;
183
184         uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wRate);
185         if (byPktType == PK_TYPE_11B) //llb,CCK mode
186                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (unsigned short)pDevice->byTopCCKBasicRate);
187         else //11g 2.4G OFDM mode & 11a 5G OFDM mode
188                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (unsigned short)pDevice->byTopOFDMBasicRate);
189
190         if (bNeedAck)
191                 return uDataTime + pDevice->uSIFS + uAckTime;
192         else
193                 return uDataTime;
194 }
195
196 static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
197                                     u32 frame_length, u16 rate, bool need_ack)
198 {
199         return cpu_to_le16((u16)s_uGetTxRsvTime(priv, pkt_type,
200                                                 frame_length, rate, need_ack));
201 }
202
203 //byFreqType: 0=>5GHZ 1=>2.4GHZ
204 static
205 __le16
206 s_uGetRTSCTSRsvTime(
207         struct vnt_private *pDevice,
208         unsigned char byRTSRsvType,
209         unsigned char byPktType,
210         unsigned int cbFrameLength,
211         unsigned short wCurrentRate
212 )
213 {
214         unsigned int uRrvTime  , uRTSTime, uCTSTime, uAckTime, uDataTime;
215
216         uRrvTime = uRTSTime = uCTSTime = uAckTime = uDataTime = 0;
217
218         uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wCurrentRate);
219         if (byRTSRsvType == 0) { //RTSTxRrvTime_bb
220                 uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
221                 uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
222         } else if (byRTSRsvType == 1) { //RTSTxRrvTime_ba, only in 2.4GHZ
223                 uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
224                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
225                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
226         } else if (byRTSRsvType == 2) { //RTSTxRrvTime_aa
227                 uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopOFDMBasicRate);
228                 uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
229         } else if (byRTSRsvType == 3) { //CTSTxRrvTime_ba, only in 2.4GHZ
230                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
231                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
232                 uRrvTime = uCTSTime + uAckTime + uDataTime + 2*pDevice->uSIFS;
233                 return cpu_to_le16((u16)uRrvTime);
234         }
235
236         //RTSRrvTime
237         uRrvTime = uRTSTime + uCTSTime + uAckTime + uDataTime + 3*pDevice->uSIFS;
238         return cpu_to_le16((u16)uRrvTime);
239 }
240
241 //byFreqType 0: 5GHz, 1:2.4Ghz
242 static
243 unsigned int
244 s_uGetDataDuration(
245         struct vnt_private *pDevice,
246         unsigned char byDurType,
247         unsigned int cbFrameLength,
248         unsigned char byPktType,
249         unsigned short wRate,
250         bool bNeedAck,
251         unsigned int uFragIdx,
252         unsigned int cbLastFragmentSize,
253         unsigned int uMACfragNum,
254         unsigned char byFBOption
255 )
256 {
257         bool bLastFrag = 0;
258         unsigned int uAckTime = 0, uNextPktTime = 0;
259
260         if (uFragIdx == (uMACfragNum-1))
261                 bLastFrag = 1;
262
263         switch (byDurType) {
264         case DATADUR_B:    //DATADUR_B
265                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
266                         if (bNeedAck) {
267                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
268                                 return pDevice->uSIFS + uAckTime;
269                         } else {
270                                 return 0;
271                         }
272                 } else {//First Frag or Mid Frag
273                         if (uFragIdx == (uMACfragNum-2))
274                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wRate, bNeedAck);
275                         else
276                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
277
278                         if (bNeedAck) {
279                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
280                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
281                         } else {
282                                 return pDevice->uSIFS + uNextPktTime;
283                         }
284                 }
285                 break;
286
287         case DATADUR_A:    //DATADUR_A
288                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
289                         if (bNeedAck) {
290                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
291                                 return pDevice->uSIFS + uAckTime;
292                         } else {
293                                 return 0;
294                         }
295                 } else {//First Frag or Mid Frag
296                         if (uFragIdx == (uMACfragNum-2))
297                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wRate, bNeedAck);
298                         else
299                                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
300
301                         if (bNeedAck) {
302                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
303                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
304                         } else {
305                                 return pDevice->uSIFS + uNextPktTime;
306                         }
307                 }
308                 break;
309
310         case DATADUR_A_F0:    //DATADUR_A_F0
311                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
312                         if (bNeedAck) {
313                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
314                                 return pDevice->uSIFS + uAckTime;
315                         } else {
316                                 return 0;
317                         }
318                 } else { //First Frag or Mid Frag
319                         if (byFBOption == AUTO_FB_0) {
320                                 if (wRate < RATE_18M)
321                                         wRate = RATE_18M;
322                                 else if (wRate > RATE_54M)
323                                         wRate = RATE_54M;
324
325                                 if (uFragIdx == (uMACfragNum-2))
326                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
327                                 else
328                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
329
330                         } else { // (byFBOption == AUTO_FB_1)
331                                 if (wRate < RATE_18M)
332                                         wRate = RATE_18M;
333                                 else if (wRate > RATE_54M)
334                                         wRate = RATE_54M;
335
336                                 if (uFragIdx == (uMACfragNum-2))
337                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
338                                 else
339                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
340
341                         }
342
343                         if (bNeedAck) {
344                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
345                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
346                         } else {
347                                 return pDevice->uSIFS + uNextPktTime;
348                         }
349                 }
350                 break;
351
352         case DATADUR_A_F1:    //DATADUR_A_F1
353                 if (((uMACfragNum == 1)) || bLastFrag) {//Non Frag or Last Frag
354                         if (bNeedAck) {
355                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
356                                 return pDevice->uSIFS + uAckTime;
357                         } else {
358                                 return 0;
359                         }
360                 } else { //First Frag or Mid Frag
361                         if (byFBOption == AUTO_FB_0) {
362                                 if (wRate < RATE_18M)
363                                         wRate = RATE_18M;
364                                 else if (wRate > RATE_54M)
365                                         wRate = RATE_54M;
366
367                                 if (uFragIdx == (uMACfragNum-2))
368                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
369                                 else
370                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
371
372                         } else { // (byFBOption == AUTO_FB_1)
373                                 if (wRate < RATE_18M)
374                                         wRate = RATE_18M;
375                                 else if (wRate > RATE_54M)
376                                         wRate = RATE_54M;
377
378                                 if (uFragIdx == (uMACfragNum-2))
379                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
380                                 else
381                                         uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
382                         }
383                         if (bNeedAck) {
384                                 uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
385                                 return pDevice->uSIFS + uAckTime + uNextPktTime;
386                         } else {
387                                 return pDevice->uSIFS + uNextPktTime;
388                         }
389                 }
390                 break;
391
392         default:
393                 break;
394         }
395
396         ASSERT(false);
397         return 0;
398 }
399
400 //byFreqType: 0=>5GHZ 1=>2.4GHZ
401 static
402 __le16
403 s_uGetRTSCTSDuration(
404         struct vnt_private *pDevice,
405         unsigned char byDurType,
406         unsigned int cbFrameLength,
407         unsigned char byPktType,
408         unsigned short wRate,
409         bool bNeedAck,
410         unsigned char byFBOption
411 )
412 {
413         unsigned int uCTSTime = 0, uDurTime = 0;
414
415         switch (byDurType) {
416         case RTSDUR_BB:    //RTSDuration_bb
417                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
418                 uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
419                 break;
420
421         case RTSDUR_BA:    //RTSDuration_ba
422                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
423                 uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
424                 break;
425
426         case RTSDUR_AA:    //RTSDuration_aa
427                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
428                 uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
429                 break;
430
431         case CTSDUR_BA:    //CTSDuration_ba
432                 uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
433                 break;
434
435         case RTSDUR_BA_F0: //RTSDuration_ba_f0
436                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
437                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
438                         uDurTime = uCTSTime + 2 * pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
439                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
440                         uDurTime = uCTSTime + 2 * pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
441
442                 break;
443
444         case RTSDUR_AA_F0: //RTSDuration_aa_f0
445                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
446                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
447                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
448                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
449                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
450
451                 break;
452
453         case RTSDUR_BA_F1: //RTSDuration_ba_f1
454                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
455                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
456                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
457                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
458                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
459
460                 break;
461
462         case RTSDUR_AA_F1: //RTSDuration_aa_f1
463                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
464                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
465                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
466                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
467                         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
468
469                 break;
470
471         case CTSDUR_BA_F0: //CTSDuration_ba_f0
472                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
473                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
474                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
475                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
476
477                 break;
478
479         case CTSDUR_BA_F1: //CTSDuration_ba_f1
480                 if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
481                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
482                 else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <= RATE_54M))
483                         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
484
485                 break;
486
487         default:
488                 break;
489         }
490
491         return cpu_to_le16((u16)uDurTime);
492 }
493
494 static
495 __le16
496 s_uFillDataHead(
497         struct vnt_private *pDevice,
498         unsigned char byPktType,
499         void *pTxDataHead,
500         unsigned int cbFrameLength,
501         unsigned int uDMAIdx,
502         bool bNeedAck,
503         unsigned int uFragIdx,
504         unsigned int cbLastFragmentSize,
505         unsigned int uMACfragNum,
506         unsigned char byFBOption,
507         unsigned short wCurrentRate,
508         bool is_pspoll
509 )
510 {
511
512         if (pTxDataHead == NULL)
513                 return 0;
514
515
516         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
517                 if (byFBOption == AUTO_FB_NONE) {
518                         struct vnt_tx_datahead_g *buf = pTxDataHead;
519                         /* Get SignalField, ServiceField & Length */
520                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
521                                           byPktType, &buf->a);
522
523                         vnt_get_phy_field(pDevice, cbFrameLength,
524                                           pDevice->byTopCCKBasicRate,
525                                           PK_TYPE_11B, &buf->b);
526
527                         if (is_pspoll) {
528                                 __le16 dur = cpu_to_le16(pDevice->current_aid | BIT(14) | BIT(15));
529
530                                 buf->duration_a = dur;
531                                 buf->duration_b = dur;
532                         } else {
533                                 /* Get Duration and TimeStamp */
534                                 buf->duration_a =
535                                         cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength,
536                                                                             byPktType, wCurrentRate, bNeedAck, uFragIdx,
537                                                                             cbLastFragmentSize, uMACfragNum,
538                                                                             byFBOption));
539                                 buf->duration_b =
540                                         cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength,
541                                                                             PK_TYPE_11B, pDevice->byTopCCKBasicRate,
542                                                                             bNeedAck, uFragIdx, cbLastFragmentSize,
543                                                                             uMACfragNum, byFBOption));
544                         }
545
546                         buf->time_stamp_off_a = vnt_time_stamp_off(pDevice, wCurrentRate);
547                         buf->time_stamp_off_b = vnt_time_stamp_off(pDevice, pDevice->byTopCCKBasicRate);
548
549                         return buf->duration_a;
550                 } else {
551                         /* Auto Fallback */
552                         struct vnt_tx_datahead_g_fb *buf = pTxDataHead;
553                         /* Get SignalField, ServiceField & Length */
554                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
555                                           byPktType, &buf->a);
556
557                         vnt_get_phy_field(pDevice, cbFrameLength,
558                                           pDevice->byTopCCKBasicRate,
559                                           PK_TYPE_11B, &buf->b);
560                         /* Get Duration and TimeStamp */
561                         buf->duration_a = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
562                                                                               wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
563                         buf->duration_b = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength, PK_TYPE_11B,
564                                                                                pDevice->byTopCCKBasicRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
565                         buf->duration_a_f0 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F0, cbFrameLength, byPktType,
566                                                                                   wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
567                         buf->duration_a_f1 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F1, cbFrameLength, byPktType,
568                                                                                  wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
569
570                         buf->time_stamp_off_a = vnt_time_stamp_off(pDevice, wCurrentRate);
571                         buf->time_stamp_off_b = vnt_time_stamp_off(pDevice, pDevice->byTopCCKBasicRate);
572
573                         return buf->duration_a;
574                 } //if (byFBOption == AUTO_FB_NONE)
575         } else if (byPktType == PK_TYPE_11A) {
576                 if ((byFBOption != AUTO_FB_NONE)) {
577                         /* Auto Fallback */
578                         struct vnt_tx_datahead_a_fb *buf = pTxDataHead;
579                         /* Get SignalField, ServiceField & Length */
580                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
581                                           byPktType, &buf->a);
582
583                         /* Get Duration and TimeStampOff */
584                         buf->duration = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
585                                                                             wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
586                         buf->duration_f0 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F0, cbFrameLength, byPktType,
587                                                                                wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
588                         buf->duration_f1 = cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A_F1, cbFrameLength, byPktType,
589                                                                                 wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption));
590                         buf->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
591                         return buf->duration;
592                 } else {
593                         struct vnt_tx_datahead_ab *buf = pTxDataHead;
594                         /* Get SignalField, ServiceField & Length */
595                         vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
596                                           byPktType, &buf->ab);
597
598                         if (is_pspoll) {
599                                 __le16 dur = cpu_to_le16(pDevice->current_aid | BIT(14) | BIT(15));
600
601                                 buf->duration = dur;
602                         } else {
603                                 /* Get Duration and TimeStampOff */
604                                 buf->duration =
605                                         cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
606                                                                             wCurrentRate, bNeedAck, uFragIdx,
607                                                                             cbLastFragmentSize, uMACfragNum,
608                                                                             byFBOption));
609                         }
610
611                         buf->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
612                         return buf->duration;
613                 }
614         } else {
615                 struct vnt_tx_datahead_ab *buf = pTxDataHead;
616                 /* Get SignalField, ServiceField & Length */
617                 vnt_get_phy_field(pDevice, cbFrameLength, wCurrentRate,
618                                   byPktType, &buf->ab);
619
620                 if (is_pspoll) {
621                         __le16 dur = cpu_to_le16(pDevice->current_aid | BIT(14) | BIT(15));
622
623                         buf->duration = dur;
624                 } else {
625                         /* Get Duration and TimeStampOff */
626                         buf->duration =
627                                 cpu_to_le16((u16)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength, byPktType,
628                                                                     wCurrentRate, bNeedAck, uFragIdx,
629                                                                     cbLastFragmentSize, uMACfragNum,
630                                                                     byFBOption));
631                 }
632
633                 buf->time_stamp_off = vnt_time_stamp_off(pDevice, wCurrentRate);
634                 return buf->duration;
635         }
636         return 0;
637 }
638
639
640 static
641 void
642 s_vFillRTSHead(
643         struct vnt_private *pDevice,
644         unsigned char byPktType,
645         void *pvRTS,
646         unsigned int cbFrameLength,
647         bool bNeedAck,
648         bool bDisCRC,
649         struct ieee80211_hdr *hdr,
650         unsigned short wCurrentRate,
651         unsigned char byFBOption
652 )
653 {
654         unsigned int uRTSFrameLen = 20;
655
656         if (pvRTS == NULL)
657                 return;
658
659         if (bDisCRC) {
660                 // When CRCDIS bit is on, H/W forgot to generate FCS for RTS frame,
661                 // in this case we need to decrease its length by 4.
662                 uRTSFrameLen -= 4;
663         }
664
665         // Note: So far RTSHead dosen't appear in ATIM & Beacom DMA, so we don't need to take them into account.
666         //       Otherwise, we need to modify codes for them.
667         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
668                 if (byFBOption == AUTO_FB_NONE) {
669                         struct vnt_rts_g *buf = pvRTS;
670                         /* Get SignalField, ServiceField & Length */
671                         vnt_get_phy_field(pDevice, uRTSFrameLen,
672                                           pDevice->byTopCCKBasicRate,
673                                           PK_TYPE_11B, &buf->b);
674
675                         vnt_get_phy_field(pDevice, uRTSFrameLen,
676                                           pDevice->byTopOFDMBasicRate,
677                                           byPktType, &buf->a);
678                         /* Get Duration */
679                         buf->duration_bb =
680                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
681                                                      cbFrameLength, PK_TYPE_11B,
682                                                      pDevice->byTopCCKBasicRate,
683                                                      bNeedAck, byFBOption);
684                         buf->duration_aa =
685                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
686                                                      cbFrameLength, byPktType,
687                                                      wCurrentRate, bNeedAck,
688                                                      byFBOption);
689                         buf->duration_ba =
690                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA,
691                                                      cbFrameLength, byPktType,
692                                                      wCurrentRate, bNeedAck,
693                                                      byFBOption);
694
695                         buf->data.duration = buf->duration_aa;
696                         /* Get RTS Frame body */
697                         buf->data.frame_control =
698                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
699                                                     IEEE80211_STYPE_RTS);
700
701                         ether_addr_copy(buf->data.ra, hdr->addr1);
702                         ether_addr_copy(buf->data.ta, hdr->addr2);
703                 } else {
704                         struct vnt_rts_g_fb *buf = pvRTS;
705                         /* Get SignalField, ServiceField & Length */
706                         vnt_get_phy_field(pDevice, uRTSFrameLen,
707                                           pDevice->byTopCCKBasicRate,
708                                           PK_TYPE_11B, &buf->b);
709
710                         vnt_get_phy_field(pDevice, uRTSFrameLen,
711                                           pDevice->byTopOFDMBasicRate,
712                                           byPktType, &buf->a);
713                         /* Get Duration */
714                         buf->duration_bb =
715                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
716                                                      cbFrameLength, PK_TYPE_11B,
717                                                      pDevice->byTopCCKBasicRate,
718                                                      bNeedAck, byFBOption);
719                         buf->duration_aa =
720                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
721                                                      cbFrameLength, byPktType,
722                                                      wCurrentRate, bNeedAck,
723                                                      byFBOption);
724                         buf->duration_ba =
725                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA,
726                                                      cbFrameLength, byPktType,
727                                                      wCurrentRate, bNeedAck,
728                                                      byFBOption);
729                         buf->rts_duration_ba_f0 =
730                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA_F0,
731                                                      cbFrameLength, byPktType,
732                                                      wCurrentRate, bNeedAck,
733                                                      byFBOption);
734                         buf->rts_duration_aa_f0 =
735                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F0,
736                                                      cbFrameLength, byPktType,
737                                                      wCurrentRate, bNeedAck,
738                                                      byFBOption);
739                         buf->rts_duration_ba_f1 =
740                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_BA_F1,
741                                                      cbFrameLength, byPktType,
742                                                      wCurrentRate, bNeedAck,
743                                                      byFBOption);
744                         buf->rts_duration_aa_f1 =
745                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F1,
746                                                      cbFrameLength, byPktType,
747                                                      wCurrentRate, bNeedAck,
748                                                      byFBOption);
749                         buf->data.duration = buf->duration_aa;
750                         /* Get RTS Frame body */
751                         buf->data.frame_control =
752                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
753                                                     IEEE80211_STYPE_RTS);
754
755                         ether_addr_copy(buf->data.ra, hdr->addr1);
756                         ether_addr_copy(buf->data.ta, hdr->addr2);
757                 } // if (byFBOption == AUTO_FB_NONE)
758         } else if (byPktType == PK_TYPE_11A) {
759                 if (byFBOption == AUTO_FB_NONE) {
760                         struct vnt_rts_ab *buf = pvRTS;
761                         /* Get SignalField, ServiceField & Length */
762                         vnt_get_phy_field(pDevice, uRTSFrameLen,
763                                           pDevice->byTopOFDMBasicRate,
764                                           byPktType, &buf->ab);
765                         /* Get Duration */
766                         buf->duration =
767                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
768                                                      cbFrameLength, byPktType,
769                                                      wCurrentRate, bNeedAck,
770                                                      byFBOption);
771                         buf->data.duration = buf->duration;
772                         /* Get RTS Frame body */
773                         buf->data.frame_control =
774                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
775                                                     IEEE80211_STYPE_RTS);
776
777                         ether_addr_copy(buf->data.ra, hdr->addr1);
778                         ether_addr_copy(buf->data.ta, hdr->addr2);
779                 } else {
780                         struct vnt_rts_a_fb *buf = pvRTS;
781                         /* Get SignalField, ServiceField & Length */
782                         vnt_get_phy_field(pDevice, uRTSFrameLen,
783                                           pDevice->byTopOFDMBasicRate,
784                                           byPktType, &buf->a);
785                         /* Get Duration */
786                         buf->duration =
787                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
788                                                      cbFrameLength, byPktType,
789                                                      wCurrentRate, bNeedAck,
790                                                      byFBOption);
791                         buf->rts_duration_f0 =
792                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F0,
793                                                      cbFrameLength, byPktType,
794                                                      wCurrentRate, bNeedAck,
795                                                      byFBOption);
796                         buf->rts_duration_f1 =
797                                 s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F1,
798                                                      cbFrameLength, byPktType,
799                                                      wCurrentRate, bNeedAck,
800                                                      byFBOption);
801                         buf->data.duration = buf->duration;
802                         /* Get RTS Frame body */
803                         buf->data.frame_control =
804                                         cpu_to_le16(IEEE80211_FTYPE_CTL |
805                                                     IEEE80211_STYPE_RTS);
806
807                         ether_addr_copy(buf->data.ra, hdr->addr1);
808                         ether_addr_copy(buf->data.ta, hdr->addr2);
809                 }
810         } else if (byPktType == PK_TYPE_11B) {
811                 struct vnt_rts_ab *buf = pvRTS;
812                 /* Get SignalField, ServiceField & Length */
813                 vnt_get_phy_field(pDevice, uRTSFrameLen,
814                                   pDevice->byTopCCKBasicRate,
815                                   PK_TYPE_11B, &buf->ab);
816                 /* Get Duration */
817                 buf->duration =
818                         s_uGetRTSCTSDuration(pDevice, RTSDUR_BB, cbFrameLength,
819                                              byPktType, wCurrentRate, bNeedAck,
820                                              byFBOption);
821
822                 buf->data.duration = buf->duration;
823                 /* Get RTS Frame body */
824                 buf->data.frame_control =
825                         cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
826
827                 ether_addr_copy(buf->data.ra, hdr->addr1);
828                 ether_addr_copy(buf->data.ta, hdr->addr2);
829         }
830 }
831
832 static
833 void
834 s_vFillCTSHead(
835         struct vnt_private *pDevice,
836         unsigned int uDMAIdx,
837         unsigned char byPktType,
838         void *pvCTS,
839         unsigned int cbFrameLength,
840         bool bNeedAck,
841         bool bDisCRC,
842         unsigned short wCurrentRate,
843         unsigned char byFBOption
844 )
845 {
846         unsigned int uCTSFrameLen = 14;
847
848         if (pvCTS == NULL)
849                 return;
850
851         if (bDisCRC) {
852                 // When CRCDIS bit is on, H/W forgot to generate FCS for CTS frame,
853                 // in this case we need to decrease its length by 4.
854                 uCTSFrameLen -= 4;
855         }
856
857         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
858                 if (byFBOption != AUTO_FB_NONE && uDMAIdx != TYPE_ATIMDMA && uDMAIdx != TYPE_BEACONDMA) {
859                         // Auto Fall back
860                         struct vnt_cts_fb *buf = pvCTS;
861                         /* Get SignalField, ServiceField & Length */
862                         vnt_get_phy_field(pDevice, uCTSFrameLen,
863                                           pDevice->byTopCCKBasicRate,
864                                           PK_TYPE_11B, &buf->b);
865
866                         buf->duration_ba =
867                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA,
868                                                      cbFrameLength, byPktType,
869                                                      wCurrentRate, bNeedAck,
870                                                      byFBOption);
871
872                         /* Get CTSDuration_ba_f0 */
873                         buf->cts_duration_ba_f0 =
874                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA_F0,
875                                                      cbFrameLength, byPktType,
876                                                      wCurrentRate, bNeedAck,
877                                                      byFBOption);
878
879                         /* Get CTSDuration_ba_f1 */
880                         buf->cts_duration_ba_f1 =
881                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA_F1,
882                                                      cbFrameLength, byPktType,
883                                                      wCurrentRate, bNeedAck,
884                                                      byFBOption);
885
886                         /* Get CTS Frame body */
887                         buf->data.duration = buf->duration_ba;
888
889                         buf->data.frame_control =
890                                 cpu_to_le16(IEEE80211_FTYPE_CTL |
891                                             IEEE80211_STYPE_CTS);
892
893                         buf->reserved2 = 0x0;
894
895                         ether_addr_copy(buf->data.ra,
896                                         pDevice->abyCurrentNetAddr);
897                 } else { //if (byFBOption != AUTO_FB_NONE && uDMAIdx != TYPE_ATIMDMA && uDMAIdx != TYPE_BEACONDMA)
898                         struct vnt_cts *buf = pvCTS;
899                         /* Get SignalField, ServiceField & Length */
900                         vnt_get_phy_field(pDevice, uCTSFrameLen,
901                                           pDevice->byTopCCKBasicRate,
902                                           PK_TYPE_11B, &buf->b);
903
904                         /* Get CTSDuration_ba */
905                         buf->duration_ba =
906                                 s_uGetRTSCTSDuration(pDevice, CTSDUR_BA,
907                                                      cbFrameLength, byPktType,
908                                                      wCurrentRate, bNeedAck,
909                                                      byFBOption);
910
911                         /* Get CTS Frame body */
912                         buf->data.duration = buf->duration_ba;
913
914                         buf->data.frame_control =
915                                 cpu_to_le16(IEEE80211_FTYPE_CTL |
916                                             IEEE80211_STYPE_CTS);
917
918                         buf->reserved2 = 0x0;
919                         ether_addr_copy(buf->data.ra,
920                                         pDevice->abyCurrentNetAddr);
921                 }
922         }
923 }
924
925 /*+
926  *
927  * Description:
928  *      Generate FIFO control for MAC & Baseband controller
929  *
930  * Parameters:
931  *  In:
932  *      pDevice         - Pointer to adapter
933  *      pTxDataHead     - Transmit Data Buffer
934  *      pTxBufHead      - pTxBufHead
935  *      pvRrvTime        - pvRrvTime
936  *      pvRTS            - RTS Buffer
937  *      pCTS            - CTS Buffer
938  *      cbFrameSize     - Transmit Data Length (Hdr+Payload+FCS)
939  *      bNeedACK        - If need ACK
940  *      uDescIdx        - Desc Index
941  *  Out:
942  *      none
943  *
944  * Return Value: none
945  *
946  -*/
947 // unsigned int cbFrameSize,//Hdr+Payload+FCS
948 static
949 void
950 s_vGenerateTxParameter(
951         struct vnt_private *pDevice,
952         unsigned char byPktType,
953         void *pTxBufHead,
954         void *pvRrvTime,
955         void *pvRTS,
956         void *pvCTS,
957         unsigned int cbFrameSize,
958         bool bNeedACK,
959         unsigned int uDMAIdx,
960         void *psEthHeader,
961         unsigned short wCurrentRate
962 )
963 {
964         unsigned short wFifoCtl;
965         bool bDisCRC = false;
966         unsigned char byFBOption = AUTO_FB_NONE;
967
968         PSTxBufHead pFifoHead = (PSTxBufHead)pTxBufHead;
969
970         pFifoHead->wReserved = wCurrentRate;
971         wFifoCtl = pFifoHead->wFIFOCtl;
972
973         if (wFifoCtl & FIFOCTL_CRCDIS)
974                 bDisCRC = true;
975
976         if (wFifoCtl & FIFOCTL_AUTO_FB_0)
977                 byFBOption = AUTO_FB_0;
978         else if (wFifoCtl & FIFOCTL_AUTO_FB_1)
979                 byFBOption = AUTO_FB_1;
980
981         if (!pvRrvTime)
982                 return;
983
984         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
985                 if (pvRTS != NULL) { //RTS_need
986                         /* Fill RsvTime */
987                         struct vnt_rrv_time_rts *buf = pvRrvTime;
988
989                         buf->rts_rrv_time_aa = s_uGetRTSCTSRsvTime(pDevice, 2, byPktType, cbFrameSize, wCurrentRate);
990                         buf->rts_rrv_time_ba = s_uGetRTSCTSRsvTime(pDevice, 1, byPktType, cbFrameSize, wCurrentRate);
991                         buf->rts_rrv_time_bb = s_uGetRTSCTSRsvTime(pDevice, 0, byPktType, cbFrameSize, wCurrentRate);
992                         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK);
993                         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate, bNeedACK);
994
995                         s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
996                 } else {//RTS_needless, PCF mode
997                         struct vnt_rrv_time_cts *buf = pvRrvTime;
998
999                         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK);
1000                         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate, bNeedACK);
1001                         buf->cts_rrv_time_ba = s_uGetRTSCTSRsvTime(pDevice, 3, byPktType, cbFrameSize, wCurrentRate);
1002
1003                         //Fill CTS
1004                         s_vFillCTSHead(pDevice, uDMAIdx, byPktType, pvCTS, cbFrameSize, bNeedACK, bDisCRC, wCurrentRate, byFBOption);
1005                 }
1006         } else if (byPktType == PK_TYPE_11A) {
1007                 if (pvRTS != NULL) {//RTS_need, non PCF mode
1008                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1009
1010                         buf->rts_rrv_time = s_uGetRTSCTSRsvTime(pDevice, 2, byPktType, cbFrameSize, wCurrentRate);
1011                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK);
1012
1013                         //Fill RTS
1014                         s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1015                 } else if (pvRTS == NULL) {//RTS_needless, non PCF mode
1016                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1017
1018                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11A, cbFrameSize, wCurrentRate, bNeedACK);
1019                 }
1020         } else if (byPktType == PK_TYPE_11B) {
1021                 if ((pvRTS != NULL)) {//RTS_need, non PCF mode
1022                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1023
1024                         buf->rts_rrv_time = s_uGetRTSCTSRsvTime(pDevice, 0, byPktType, cbFrameSize, wCurrentRate);
1025                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, wCurrentRate, bNeedACK);
1026
1027                         //Fill RTS
1028                         s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1029                 } else { //RTS_needless, non PCF mode
1030                         struct vnt_rrv_time_ab *buf = pvRrvTime;
1031
1032                         buf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B, cbFrameSize, wCurrentRate, bNeedACK);
1033                 }
1034         }
1035 }
1036
1037 static unsigned int
1038 s_cbFillTxBufHead(struct vnt_private *pDevice, unsigned char byPktType,
1039                   unsigned char *pbyTxBufferAddr, unsigned int cbFrameBodySize,
1040                   unsigned int uDMAIdx, PSTxDesc pHeadTD,
1041                   unsigned char *pPacket, unsigned int is_pspoll,
1042                   unsigned int *puMACfragNum)
1043 {
1044         PDEVICE_TD_INFO td_info = pHeadTD->pTDInfo;
1045         struct sk_buff *skb = td_info->skb;
1046         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1047         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1048         struct vnt_tx_fifo_head *tx_buffer_head =
1049                         (struct vnt_tx_fifo_head *)td_info->buf;
1050         u16 fifo_ctl = le16_to_cpu(tx_buffer_head->fifo_ctl);
1051         unsigned int cbFrameSize;
1052         __le16 uDuration;
1053         unsigned char *pbyBuffer;
1054         unsigned int uLength = 0;
1055         unsigned int cbMICHDR = 0;
1056         unsigned int uMACfragNum = 1;
1057         unsigned int uPadding = 0;
1058         unsigned int cbReqCount = 0;
1059         bool bNeedACK = (bool)(fifo_ctl & FIFOCTL_NEEDACK);
1060         bool bRTS = (bool)(fifo_ctl & FIFOCTL_RTS);
1061         PSTxDesc       ptdCurr;
1062         unsigned int cbHeaderLength = 0;
1063         void *pvRrvTime;
1064         struct vnt_mic_hdr *pMICHDR;
1065         void *pvRTS;
1066         void *pvCTS;
1067         void *pvTxDataHd;
1068         unsigned short wTxBufSize;   // FFinfo size
1069         unsigned char byFBOption = AUTO_FB_NONE;
1070
1071         pvRrvTime = pMICHDR = pvRTS = pvCTS = pvTxDataHd = NULL;
1072
1073         cbFrameSize = skb->len + 4;
1074
1075         if (info->control.hw_key) {
1076                 switch (info->control.hw_key->cipher) {
1077                 case WLAN_CIPHER_SUITE_CCMP:
1078                         cbMICHDR = sizeof(struct vnt_mic_hdr);
1079                 default:
1080                         break;
1081                 }
1082
1083                 cbFrameSize += info->control.hw_key->icv_len;
1084
1085                 if (pDevice->byLocalID > REV_ID_VT3253_A1) {
1086                         //MAC Header should be padding 0 to DW alignment.
1087                         uPadding = 4 - (ieee80211_get_hdrlen_from_skb(skb) % 4);
1088                         uPadding %= 4;
1089                 }
1090         }
1091
1092         //
1093         // Use for AUTO FALL BACK
1094         //
1095         if (fifo_ctl & FIFOCTL_AUTO_FB_0)
1096                 byFBOption = AUTO_FB_0;
1097         else if (fifo_ctl & FIFOCTL_AUTO_FB_1)
1098                 byFBOption = AUTO_FB_1;
1099
1100         //////////////////////////////////////////////////////
1101         //Set RrvTime/RTS/CTS Buffer
1102         wTxBufSize = sizeof(STxBufHead);
1103         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
1104
1105                 if (byFBOption == AUTO_FB_NONE) {
1106                         if (bRTS == true) {//RTS_need
1107                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1108                                 pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts));
1109                                 pvRTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1110                                 pvCTS = NULL;
1111                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1112                                                         cbMICHDR + sizeof(struct vnt_rts_g));
1113                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1114                                                         cbMICHDR + sizeof(struct vnt_rts_g) +
1115                                                         sizeof(struct vnt_tx_datahead_g);
1116                         } else { //RTS_needless
1117                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1118                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts));
1119                                 pvRTS = NULL;
1120                                 pvCTS = (void *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1121                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1122                                                 sizeof(struct vnt_rrv_time_cts) + cbMICHDR + sizeof(struct vnt_cts));
1123                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1124                                                         cbMICHDR + sizeof(struct vnt_cts) + sizeof(struct vnt_tx_datahead_g);
1125                         }
1126                 } else {
1127                         // Auto Fall Back
1128                         if (bRTS == true) {//RTS_need
1129                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1130                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts));
1131                                 pvRTS = (void *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1132                                 pvCTS = NULL;
1133                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1134                                         cbMICHDR + sizeof(struct vnt_rts_g_fb));
1135                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1136                                         cbMICHDR + sizeof(struct vnt_rts_g_fb) + sizeof(struct vnt_tx_datahead_g_fb);
1137                         } else { //RTS_needless
1138                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1139                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts));
1140                                 pvRTS = NULL;
1141                                 pvCTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1142                                 pvTxDataHd = (void  *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1143                                         cbMICHDR + sizeof(struct vnt_cts_fb));
1144                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1145                                         cbMICHDR + sizeof(struct vnt_cts_fb) + sizeof(struct vnt_tx_datahead_g_fb);
1146                         }
1147                 } // Auto Fall Back
1148         } else {//802.11a/b packet
1149
1150                 if (byFBOption == AUTO_FB_NONE) {
1151                         if (bRTS == true) {
1152                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1153                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1154                                 pvRTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1155                                 pvCTS = NULL;
1156                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1157                                         sizeof(struct vnt_rrv_time_ab) + cbMICHDR + sizeof(struct vnt_rts_ab));
1158                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1159                                         cbMICHDR + sizeof(struct vnt_rts_ab) + sizeof(struct vnt_tx_datahead_ab);
1160                         } else { //RTS_needless, need MICHDR
1161                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1162                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1163                                 pvRTS = NULL;
1164                                 pvCTS = NULL;
1165                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1166                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1167                                         cbMICHDR + sizeof(struct vnt_tx_datahead_ab);
1168                         }
1169                 } else {
1170                         // Auto Fall Back
1171                         if (bRTS == true) {//RTS_need
1172                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1173                                 pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1174                                 pvRTS = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1175                                 pvCTS = NULL;
1176                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize +
1177                                         sizeof(struct vnt_rrv_time_ab) + cbMICHDR + sizeof(struct vnt_rts_a_fb));
1178                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1179                                         cbMICHDR + sizeof(struct vnt_rts_a_fb) + sizeof(struct vnt_tx_datahead_a_fb);
1180                         } else { //RTS_needless
1181                                 pvRrvTime = (void *)(pbyTxBufferAddr + wTxBufSize);
1182                                 pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab));
1183                                 pvRTS = NULL;
1184                                 pvCTS = NULL;
1185                                 pvTxDataHd = (void *)(pbyTxBufferAddr + wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1186                                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1187                                         cbMICHDR + sizeof(struct vnt_tx_datahead_a_fb);
1188                         }
1189                 } // Auto Fall Back
1190         }
1191
1192         td_info->mic_hdr = pMICHDR;
1193
1194         memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderLength - wTxBufSize));
1195
1196         /* Fill FIFO,RrvTime,RTS,and CTS */
1197         s_vGenerateTxParameter(pDevice, byPktType, tx_buffer_head, pvRrvTime, pvRTS, pvCTS,
1198                                cbFrameSize, bNeedACK, uDMAIdx, hdr, pDevice->wCurrentRate);
1199         /* Fill DataHead */
1200         uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, uDMAIdx, bNeedACK,
1201                                     0, 0, uMACfragNum, byFBOption, pDevice->wCurrentRate, is_pspoll);
1202
1203         hdr->duration_id = uDuration;
1204
1205         cbReqCount = cbHeaderLength + uPadding + cbFrameBodySize;
1206         pbyBuffer = (unsigned char *)pHeadTD->pTDInfo->buf;
1207         uLength = cbHeaderLength + uPadding;
1208
1209         /* Copy the Packet into a tx Buffer */
1210         memcpy((pbyBuffer + uLength), pPacket, cbFrameBodySize);
1211
1212         ptdCurr = (PSTxDesc)pHeadTD;
1213
1214         ptdCurr->pTDInfo->dwReqCount = cbReqCount - uPadding;
1215         ptdCurr->pTDInfo->dwHeaderLength = cbHeaderLength;
1216         ptdCurr->pTDInfo->skb_dma = ptdCurr->pTDInfo->buf_dma;
1217         ptdCurr->buff_addr = cpu_to_le32(ptdCurr->pTDInfo->skb_dma);
1218         /* Set TSR1 & ReqCount in TxDescHead */
1219         ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP | EDMSDU);
1220         ptdCurr->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
1221
1222         *puMACfragNum = uMACfragNum;
1223
1224         return cbHeaderLength;
1225 }
1226
1227 static void vnt_fill_txkey(struct ieee80211_hdr *hdr, u8 *key_buffer,
1228                            struct ieee80211_key_conf *tx_key,
1229                            struct sk_buff *skb, u16 payload_len,
1230                            struct vnt_mic_hdr *mic_hdr)
1231 {
1232         struct ieee80211_key_seq seq;
1233         u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
1234
1235         /* strip header and icv len from payload */
1236         payload_len -= ieee80211_get_hdrlen_from_skb(skb);
1237         payload_len -= tx_key->icv_len;
1238
1239         switch (tx_key->cipher) {
1240         case WLAN_CIPHER_SUITE_WEP40:
1241         case WLAN_CIPHER_SUITE_WEP104:
1242                 memcpy(key_buffer, iv, 3);
1243                 memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
1244
1245                 if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
1246                         memcpy(key_buffer + 8, iv, 3);
1247                         memcpy(key_buffer + 11,
1248                                tx_key->key, WLAN_KEY_LEN_WEP40);
1249                 }
1250
1251                 break;
1252         case WLAN_CIPHER_SUITE_TKIP:
1253                 ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
1254
1255                 break;
1256         case WLAN_CIPHER_SUITE_CCMP:
1257
1258                 if (!mic_hdr)
1259                         return;
1260
1261                 mic_hdr->id = 0x59;
1262                 mic_hdr->payload_len = cpu_to_be16(payload_len);
1263                 ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
1264
1265                 ieee80211_get_key_tx_seq(tx_key, &seq);
1266
1267                 memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1268
1269                 if (ieee80211_has_a4(hdr->frame_control))
1270                         mic_hdr->hlen = cpu_to_be16(28);
1271                 else
1272                         mic_hdr->hlen = cpu_to_be16(22);
1273
1274                 ether_addr_copy(mic_hdr->addr1, hdr->addr1);
1275                 ether_addr_copy(mic_hdr->addr2, hdr->addr2);
1276                 ether_addr_copy(mic_hdr->addr3, hdr->addr3);
1277
1278                 mic_hdr->frame_control = cpu_to_le16(
1279                         le16_to_cpu(hdr->frame_control) & 0xc78f);
1280                 mic_hdr->seq_ctrl = cpu_to_le16(
1281                                 le16_to_cpu(hdr->seq_ctrl) & 0xf);
1282
1283                 if (ieee80211_has_a4(hdr->frame_control))
1284                         ether_addr_copy(mic_hdr->addr4, hdr->addr4);
1285
1286                 memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);
1287
1288                 break;
1289         default:
1290                 break;
1291         }
1292 }
1293
1294 int vnt_generate_fifo_header(struct vnt_private *priv, u32 dma_idx,
1295                              PSTxDesc head_td, struct sk_buff *skb)
1296 {
1297         PDEVICE_TD_INFO td_info = head_td->pTDInfo;
1298         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1299         struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
1300         struct ieee80211_rate *rate;
1301         struct ieee80211_key_conf *tx_key;
1302         struct ieee80211_hdr *hdr;
1303         struct vnt_tx_fifo_head *tx_buffer_head =
1304                         (struct vnt_tx_fifo_head *)td_info->buf;
1305         u32 frag;
1306         u16 tx_body_size = skb->len, current_rate;
1307         u8 pkt_type;
1308         bool is_pspoll = false;
1309
1310         memset(tx_buffer_head, 0, sizeof(*tx_buffer_head));
1311
1312         hdr = (struct ieee80211_hdr *)(skb->data);
1313
1314         rate = ieee80211_get_tx_rate(priv->hw, info);
1315
1316         current_rate = rate->hw_value;
1317         if (priv->wCurrentRate != current_rate &&
1318                         !(priv->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
1319                 priv->wCurrentRate = current_rate;
1320
1321                 RFbSetPower(priv, priv->wCurrentRate,
1322                             priv->hw->conf.chandef.chan->hw_value);
1323         }
1324
1325         if (current_rate > RATE_11M)
1326                 pkt_type = (u8)priv->byPacketType;
1327         else
1328                 pkt_type = PK_TYPE_11B;
1329
1330         /*Set fifo controls */
1331         if (pkt_type == PK_TYPE_11A)
1332                 tx_buffer_head->fifo_ctl = 0;
1333         else if (pkt_type == PK_TYPE_11B)
1334                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
1335         else if (pkt_type == PK_TYPE_11GB)
1336                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
1337         else if (pkt_type == PK_TYPE_11GA)
1338                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
1339
1340         /* generate interrupt */
1341         tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT);
1342
1343         if (!ieee80211_is_data(hdr->frame_control)) {
1344                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
1345                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_ISDMA0);
1346                 tx_buffer_head->time_stamp =
1347                         cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
1348         } else {
1349                 tx_buffer_head->time_stamp =
1350                         cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
1351         }
1352
1353         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
1354                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
1355
1356         if (ieee80211_has_retry(hdr->frame_control))
1357                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
1358
1359         if (tx_rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1360                 priv->byPreambleType = PREAMBLE_SHORT;
1361         else
1362                 priv->byPreambleType = PREAMBLE_LONG;
1363
1364         if (tx_rate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1365                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
1366
1367         if (ieee80211_has_a4(hdr->frame_control)) {
1368                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
1369                 priv->bLongHeader = true;
1370         }
1371
1372         if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
1373                 is_pspoll = true;
1374
1375         tx_buffer_head->frag_ctl =
1376                         cpu_to_le16(ieee80211_get_hdrlen_from_skb(skb) << 10);
1377
1378         if (info->control.hw_key) {
1379                 tx_key = info->control.hw_key;
1380
1381                 switch (info->control.hw_key->cipher) {
1382                 case WLAN_CIPHER_SUITE_WEP40:
1383                 case WLAN_CIPHER_SUITE_WEP104:
1384                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
1385                         break;
1386                 case WLAN_CIPHER_SUITE_TKIP:
1387                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
1388                         break;
1389                 case WLAN_CIPHER_SUITE_CCMP:
1390                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
1391                 default:
1392                         break;
1393                 }
1394         }
1395
1396         tx_buffer_head->current_rate = cpu_to_le16(current_rate);
1397
1398         /* legacy rates TODO use ieee80211_tx_rate */
1399         if (current_rate >= RATE_18M && ieee80211_is_data(hdr->frame_control)) {
1400                 if (priv->byAutoFBCtrl == AUTO_FB_0)
1401                         tx_buffer_head->fifo_ctl |=
1402                                                 cpu_to_le16(FIFOCTL_AUTO_FB_0);
1403                 else if (priv->byAutoFBCtrl == AUTO_FB_1)
1404                         tx_buffer_head->fifo_ctl |=
1405                                                 cpu_to_le16(FIFOCTL_AUTO_FB_1);
1406
1407         }
1408
1409         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
1410
1411         s_cbFillTxBufHead(priv, pkt_type, (u8 *)tx_buffer_head, skb->len,
1412                           dma_idx, head_td, (u8 *)skb->data,
1413                           is_pspoll, &frag);
1414
1415         if (info->control.hw_key) {
1416                 tx_key = info->control.hw_key;
1417                 if (tx_key->keylen > 0)
1418                         vnt_fill_txkey(hdr, tx_buffer_head->tx_key,
1419                                 tx_key, skb, tx_body_size, td_info->mic_hdr);
1420         }
1421
1422         return 0;
1423 }
1424
1425 static int vnt_beacon_xmit(struct vnt_private *priv,
1426                            struct sk_buff *skb)
1427 {
1428         struct vnt_tx_short_buf_head *short_head =
1429                 (struct vnt_tx_short_buf_head *)priv->tx_beacon_bufs;
1430         struct ieee80211_mgmt *mgmt_hdr = (struct ieee80211_mgmt *)
1431                                 (priv->tx_beacon_bufs + sizeof(*short_head));
1432         struct ieee80211_tx_info *info;
1433         u32 frame_size = skb->len + 4;
1434         u16 current_rate;
1435
1436         memset(priv->tx_beacon_bufs, 0, sizeof(*short_head));
1437
1438         if (priv->byBBType == BB_TYPE_11A) {
1439                 current_rate = RATE_6M;
1440
1441                 /* Get SignalField,ServiceField,Length */
1442                 vnt_get_phy_field(priv, frame_size, current_rate,
1443                                   PK_TYPE_11A, &short_head->ab);
1444
1445                 /* Get Duration and TimeStampOff */
1446                 short_head->duration =
1447                         cpu_to_le16((u16)s_uGetDataDuration(priv, DATADUR_B,
1448                                     frame_size, PK_TYPE_11A, current_rate,
1449                                     false, 0, 0, 1, AUTO_FB_NONE));
1450
1451                 short_head->time_stamp_off =
1452                                 vnt_time_stamp_off(priv, current_rate);
1453         } else {
1454                 current_rate = RATE_1M;
1455                 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
1456
1457                 /* Get SignalField,ServiceField,Length */
1458                 vnt_get_phy_field(priv, frame_size, current_rate,
1459                                   PK_TYPE_11B, &short_head->ab);
1460
1461                 /* Get Duration and TimeStampOff */
1462                 short_head->duration =
1463                         cpu_to_le16((u16)s_uGetDataDuration(priv, DATADUR_B,
1464                                     frame_size, PK_TYPE_11B, current_rate,
1465                                     false, 0, 0, 1, AUTO_FB_NONE));
1466
1467                 short_head->time_stamp_off =
1468                         vnt_time_stamp_off(priv, current_rate);
1469         }
1470
1471         short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT);
1472
1473         /* Copy Beacon */
1474         memcpy(mgmt_hdr, skb->data, skb->len);
1475
1476         /* time stamp always 0 */
1477         mgmt_hdr->u.beacon.timestamp = 0;
1478
1479         info = IEEE80211_SKB_CB(skb);
1480         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1481                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
1482
1483                 hdr->duration_id = 0;
1484                 hdr->seq_ctrl = cpu_to_le16(priv->wSeqCounter << 4);
1485         }
1486
1487         priv->wSeqCounter++;
1488         if (priv->wSeqCounter > 0x0fff)
1489                 priv->wSeqCounter = 0;
1490
1491         priv->wBCNBufLen = sizeof(*short_head) + skb->len;
1492
1493         MACvSetCurrBCNTxDescAddr(priv->PortOffset, priv->tx_beacon_dma);
1494
1495         MACvSetCurrBCNLength(priv->PortOffset, priv->wBCNBufLen);
1496         /* Set auto Transmit on */
1497         MACvRegBitsOn(priv->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
1498         /* Poll Transmit the adapter */
1499         MACvTransmitBCN(priv->PortOffset);
1500
1501         return 0;
1502 }
1503
1504 int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
1505 {
1506         struct sk_buff *beacon;
1507
1508         beacon = ieee80211_beacon_get(priv->hw, vif);
1509         if (!beacon)
1510                 return -ENOMEM;
1511
1512         if (vnt_beacon_xmit(priv, beacon)) {
1513                 ieee80211_free_txskb(priv->hw, beacon);
1514                 return -ENODEV;
1515         }
1516
1517         return 0;
1518 }
1519
1520 int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
1521                       struct ieee80211_bss_conf *conf)
1522 {
1523         int ret;
1524
1525         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
1526
1527         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1528
1529         CARDvSetFirstNextTBTT(priv->PortOffset, conf->beacon_int);
1530
1531         CARDbSetBeaconPeriod(priv, conf->beacon_int);
1532
1533         ret = vnt_beacon_make(priv, vif);
1534
1535         return ret;
1536 }