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