Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[cascardo/linux.git] / drivers / staging / rtl8821ae / rtl8821ae / trx.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * wlanfae <wlanfae@realtek.com>
23  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24  * Hsinchu 300, Taiwan.
25  *
26  * Larry Finger <Larry.Finger@lwfinger.net>
27  *
28  *****************************************************************************/
29
30 #include "../wifi.h"
31 #include "../pci.h"
32 #include "../base.h"
33 #include "../stats.h"
34 #include "reg.h"
35 #include "def.h"
36 #include "phy.h"
37 #include "trx.h"
38 #include "led.h"
39 #include "dm.h"
40
41 static u8 _rtl8821ae_map_hwqueue_to_fwqueue(struct sk_buff *skb, u8 hw_queue)
42 {
43         u16 fc = rtl_get_fc(skb);
44
45         if (unlikely(ieee80211_is_beacon(fc)))
46                 return QSLT_BEACON;
47         if (ieee80211_is_mgmt(fc) || ieee80211_is_ctl(fc))
48                 return QSLT_MGNT;
49
50         return skb->priority;
51 }
52
53 /* mac80211's rate_idx is like this:
54  *
55  * 2.4G band:rx_status->band == IEEE80211_BAND_2GHZ
56  *
57  * B/G rate:
58  * (rx_status->flag & RX_FLAG_HT) = 0,
59  * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
60  *
61  * N rate:
62  * (rx_status->flag & RX_FLAG_HT) = 1,
63  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
64  *
65  * 5G band:rx_status->band == IEEE80211_BAND_5GHZ
66  * A rate:
67  * (rx_status->flag & RX_FLAG_HT) = 0,
68  * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
69  *
70  * N rate:
71  * (rx_status->flag & RX_FLAG_HT) = 1,
72  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
73  */
74 static int _rtl8821ae_rate_mapping(struct ieee80211_hw *hw,
75         bool isht, u8 desc_rate)
76 {
77         int rate_idx;
78
79         if (false == isht) {
80                 if (IEEE80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
81                         switch (desc_rate) {
82                         case DESC_RATE1M:
83                                 rate_idx = 0;
84                                 break;
85                         case DESC_RATE2M:
86                                 rate_idx = 1;
87                                 break;
88                         case DESC_RATE5_5M:
89                                 rate_idx = 2;
90                                 break;
91                         case DESC_RATE11M:
92                                 rate_idx = 3;
93                                 break;
94                         case DESC_RATE6M:
95                                 rate_idx = 4;
96                                 break;
97                         case DESC_RATE9M:
98                                 rate_idx = 5;
99                                 break;
100                         case DESC_RATE12M:
101                                 rate_idx = 6;
102                                 break;
103                         case DESC_RATE18M:
104                                 rate_idx = 7;
105                                 break;
106                         case DESC_RATE24M:
107                                 rate_idx = 8;
108                                 break;
109                         case DESC_RATE36M:
110                                 rate_idx = 9;
111                                 break;
112                         case DESC_RATE48M:
113                                 rate_idx = 10;
114                                 break;
115                         case DESC_RATE54M:
116                                 rate_idx = 11;
117                                 break;
118                         default:
119                                 rate_idx = 0;
120                                 break;
121                         }
122                 } else {
123                         switch (desc_rate) {
124                         case DESC_RATE6M:
125                                 rate_idx = 0;
126                                 break;
127                         case DESC_RATE9M:
128                                 rate_idx = 1;
129                                 break;
130                         case DESC_RATE12M:
131                                 rate_idx = 2;
132                                 break;
133                         case DESC_RATE18M:
134                                 rate_idx = 3;
135                                 break;
136                         case DESC_RATE24M:
137                                 rate_idx = 4;
138                                 break;
139                         case DESC_RATE36M:
140                                 rate_idx = 5;
141                                 break;
142                         case DESC_RATE48M:
143                                 rate_idx = 6;
144                                 break;
145                         case DESC_RATE54M:
146                                 rate_idx = 7;
147                                 break;
148                         default:
149                                 rate_idx = 0;
150                                 break;
151                         }
152                 }
153         } else {
154                 switch(desc_rate) {
155                 case DESC_RATEMCS0:
156                         rate_idx = 0;
157                         break;
158                 case DESC_RATEMCS1:
159                         rate_idx = 1;
160                         break;
161                 case DESC_RATEMCS2:
162                         rate_idx = 2;
163                         break;
164                 case DESC_RATEMCS3:
165                         rate_idx = 3;
166                         break;
167                 case DESC_RATEMCS4:
168                         rate_idx = 4;
169                         break;
170                 case DESC_RATEMCS5:
171                         rate_idx = 5;
172                         break;
173                 case DESC_RATEMCS6:
174                         rate_idx = 6;
175                         break;
176                 case DESC_RATEMCS7:
177                         rate_idx = 7;
178                         break;
179                 case DESC_RATEMCS8:
180                         rate_idx = 8;
181                         break;
182                 case DESC_RATEMCS9:
183                         rate_idx = 9;
184                         break;
185                 case DESC_RATEMCS10:
186                         rate_idx = 10;
187                         break;
188                 case DESC_RATEMCS11:
189                         rate_idx = 11;
190                         break;
191                 case DESC_RATEMCS12:
192                         rate_idx = 12;
193                         break;
194                 case DESC_RATEMCS13:
195                         rate_idx = 13;
196                         break;
197                 case DESC_RATEMCS14:
198                         rate_idx = 14;
199                         break;
200                 case DESC_RATEMCS15:
201                         rate_idx = 15;
202                         break;
203                 default:
204                         rate_idx = 0;
205                         break;
206                 }
207         }
208         return rate_idx;
209 }
210
211 static void _rtl8821ae_query_rxphystatus(struct ieee80211_hw *hw,
212                         struct rtl_stats *pstatus, u8 *pdesc,
213                         struct rx_fwinfo_8821ae *p_drvinfo, bool bpacket_match_bssid,
214                         bool bpacket_toself, bool b_packet_beacon)
215 {
216         struct rtl_priv *rtlpriv = rtl_priv(hw);
217         struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
218         struct phy_sts_cck_8821ae_t *cck_buf;
219         struct phy_status_rpt *p_phystRpt = (struct phy_status_rpt *)p_drvinfo;
220         struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
221         char rx_pwr_all = 0, rx_pwr[4];
222         u8 rf_rx_num = 0, evm, pwdb_all;
223         u8 i, max_spatial_stream;
224         u32 rssi, total_rssi = 0;
225         bool b_is_cck = pstatus->b_is_cck;
226         u8 lan_idx,vga_idx;
227
228         /* Record it for next packet processing */
229         pstatus->b_packet_matchbssid = bpacket_match_bssid;
230         pstatus->b_packet_toself = bpacket_toself;
231         pstatus->b_packet_beacon = b_packet_beacon;
232         pstatus->rx_mimo_signalquality[0] = -1;
233         pstatus->rx_mimo_signalquality[1] = -1;
234
235         if (b_is_cck) {
236                 u8 cck_highpwr;
237                 u8 cck_agc_rpt;
238                 /* CCK Driver info Structure is not the same as OFDM packet. */
239                 cck_buf = (struct phy_sts_cck_8821ae_t *)p_drvinfo;
240                 cck_agc_rpt = cck_buf->cck_agc_rpt;
241
242                 /* (1)Hardware does not provide RSSI for CCK */
243                 /* (2)PWDB, Average PWDB calculated by
244                  * hardware (for rate adaptive) */
245                 if (ppsc->rfpwr_state == ERFON)
246                         cck_highpwr = (u8) rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2,
247                                                          BIT(9));
248                 else
249                         cck_highpwr = false;
250
251                 lan_idx = ((cck_agc_rpt & 0xE0) >> 5);
252                 vga_idx = (cck_agc_rpt & 0x1f);
253                 switch (lan_idx) {
254                         case 7:
255                                 if(vga_idx <= 27)
256                                         rx_pwr_all = -100 + 2*(27-vga_idx); /*VGA_idx = 27~2*/
257                                 else
258                                         rx_pwr_all = -100;
259                                 break;
260                         case 6:
261                                 rx_pwr_all = -48 + 2*(2-vga_idx); /*VGA_idx = 2~0*/
262                                 break;
263                         case 5:
264                                 rx_pwr_all = -42 + 2*(7-vga_idx); /*VGA_idx = 7~5*/
265                                 break;
266                         case 4:
267                                 rx_pwr_all = -36 + 2*(7-vga_idx); /*VGA_idx = 7~4*/
268                                 break;
269                         case 3:
270                                 rx_pwr_all = -24 + 2*(7-vga_idx); /*VGA_idx = 7~0*/
271                                 break;
272                         case 2:
273                                 if(cck_highpwr)
274                                         rx_pwr_all = -12 + 2*(5-vga_idx); /*VGA_idx = 5~0*/
275                                 else
276                                         rx_pwr_all = -6+ 2*(5-vga_idx);
277                                 break;
278                         case 1:
279                                 rx_pwr_all = 8-2*vga_idx;
280                                 break;
281                         case 0:
282                                 rx_pwr_all = 14-2*vga_idx;
283                                 break;
284                         default:
285                                 break;
286                 }
287                 rx_pwr_all += 6;
288                 pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
289                 /* CCK gain is smaller than OFDM/MCS gain,  */
290                 /* so we add gain diff by experiences,
291                  * the val is 6 */
292                 pwdb_all += 6;
293                 if(pwdb_all > 100)
294                         pwdb_all = 100;
295                 /* modify the offset to make the same
296                  * gain index with OFDM. */
297                 if(pwdb_all > 34 && pwdb_all <= 42)
298                         pwdb_all -= 2;
299                 else if(pwdb_all > 26 && pwdb_all <= 34)
300                         pwdb_all -= 6;
301                 else if(pwdb_all > 14 && pwdb_all <= 26)
302                         pwdb_all -= 8;
303                 else if(pwdb_all > 4 && pwdb_all <= 14)
304                         pwdb_all -= 4;
305                 if (cck_highpwr == false){
306                         if (pwdb_all >= 80)
307                                 pwdb_all =((pwdb_all-80)<<1)+((pwdb_all-80)>>1)+80;
308                         else if((pwdb_all <= 78) && (pwdb_all >= 20))
309                                 pwdb_all += 3;
310                         if(pwdb_all>100)
311                                 pwdb_all = 100;
312                 }
313
314                 pstatus->rx_pwdb_all = pwdb_all;
315                 pstatus->recvsignalpower = rx_pwr_all;
316
317                 /* (3) Get Signal Quality (EVM) */
318                 if (bpacket_match_bssid) {
319                         u8 sq;
320
321                         if (pstatus->rx_pwdb_all > 40)
322                                 sq = 100;
323                         else {
324                                 sq = cck_buf->sq_rpt;
325                                 if (sq > 64)
326                                         sq = 0;
327                                 else if (sq < 20)
328                                         sq = 100;
329                                 else
330                                         sq = ((64 - sq) * 100) / 44;
331                         }
332
333                         pstatus->signalquality = sq;
334                         pstatus->rx_mimo_signalquality[0] = sq;
335                         pstatus->rx_mimo_signalquality[1] = -1;
336                 }
337         } else {
338                 rtlpriv->dm.brfpath_rxenable[0] =
339                     rtlpriv->dm.brfpath_rxenable[1] = true;
340
341                 /* (1)Get RSSI for HT rate */
342                 for (i = RF90_PATH_A; i < RF6052_MAX_PATH; i++) {
343
344                         /* we will judge RF RX path now. */
345                         if (rtlpriv->dm.brfpath_rxenable[i])
346                                 rf_rx_num++;
347
348                         rx_pwr[i] = ((p_drvinfo->gain_trsw[i] & 0x3f) * 2) - 110;
349
350                         /* Translate DBM to percentage. */
351                         rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
352                         total_rssi += rssi;
353
354                         /* Get Rx snr value in DB */
355                         rtlpriv->stats.rx_snr_db[i] = (long)(p_drvinfo->rxsnr[i] / 2);
356
357                         /* Record Signal Strength for next packet */
358                         if (bpacket_match_bssid)
359                                 pstatus->rx_mimo_signalstrength[i] = (u8) rssi;
360                 }
361
362                 /* (2)PWDB, Average PWDB calculated by
363                  * hardware (for rate adaptive) */
364                 rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
365
366                 pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
367                 pstatus->rx_pwdb_all = pwdb_all;
368                 pstatus->rxpower = rx_pwr_all;
369                 pstatus->recvsignalpower = rx_pwr_all;
370
371                 /* (3)EVM of HT rate */
372                 if (pstatus->b_is_ht && pstatus->rate >= DESC_RATEMCS8 &&
373                     pstatus->rate <= DESC_RATEMCS15)
374                         max_spatial_stream = 2;
375                 else
376                         max_spatial_stream = 1;
377
378                 for (i = 0; i < max_spatial_stream; i++) {
379                         evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
380
381                         if (bpacket_match_bssid) {
382                                 /* Fill value in RFD, Get the first
383                                  * spatial stream only */
384                                 if (i == 0)
385                                         pstatus->signalquality = (u8) (evm & 0xff);
386                                 pstatus->rx_mimo_signalquality[i] = (u8) (evm & 0xff);
387                         }
388                 }
389         }
390
391         /* UI BSS List signal strength(in percentage),
392          * make it good looking, from 0~100. */
393         if (b_is_cck)
394                 pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
395                         pwdb_all));
396         else if (rf_rx_num != 0)
397                 pstatus->signalstrength = (u8)(rtl_signal_scale_mapping(hw,
398                         total_rssi /= rf_rx_num));
399         /*HW antenna diversity*/
400         rtldm->fat_table.antsel_rx_keep_0 = p_phystRpt->ant_sel;
401         rtldm->fat_table.antsel_rx_keep_1 = p_phystRpt->ant_sel_b;
402         rtldm->fat_table.antsel_rx_keep_2 = p_phystRpt->antsel_rx_keep_2;
403
404 }
405 #if 0
406 static void _rtl8821ae_smart_antenna(struct ieee80211_hw *hw,
407         struct rtl_stats *pstatus)
408 {
409         struct rtl_dm *rtldm= rtl_dm(rtl_priv(hw));
410         struct rtl_efuse *rtlefuse =rtl_efuse(rtl_priv(hw));
411         u8 antsel_tr_mux;
412         struct fast_ant_trainning *pfat_table = &(rtldm->fat_table);
413
414         if (rtlefuse->antenna_div_type == CG_TRX_SMART_ANTDIV) {
415                 if (pfat_table->fat_state == FAT_TRAINING_STATE) {
416                         if (pstatus->b_packet_toself) {
417                                 antsel_tr_mux = (pfat_table->antsel_rx_keep_2 << 2) |
418                                         (pfat_table->antsel_rx_keep_1 << 1) | pfat_table->antsel_rx_keep_0;
419                                 pfat_table->ant_sum_rssi[antsel_tr_mux] += pstatus->rx_pwdb_all;
420                                 pfat_table->ant_rssi_cnt[antsel_tr_mux]++;
421                         }
422                 }
423         } else if ((rtlefuse->antenna_div_type == CG_TRX_HW_ANTDIV) ||
424         (rtlefuse->antenna_div_type == CGCS_RX_HW_ANTDIV)) {
425                 if (pstatus->b_packet_toself || pstatus->b_packet_matchbssid) {
426                         antsel_tr_mux = (pfat_table->antsel_rx_keep_2 << 2) |
427                                         (pfat_table->antsel_rx_keep_1 << 1) | pfat_table->antsel_rx_keep_0;
428                         rtl8821ae_dm_ant_sel_statistics(hw, antsel_tr_mux, 0, pstatus->rx_pwdb_all);
429                 }
430
431         }
432 }
433 #endif
434 static void _rtl8821ae_translate_rx_signal_stuff(struct ieee80211_hw *hw,
435                 struct sk_buff *skb, struct rtl_stats *pstatus,
436                 u8 *pdesc, struct rx_fwinfo_8821ae *p_drvinfo)
437 {
438         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
439         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
440         struct ieee80211_hdr *hdr;
441         u8 *tmp_buf;
442         u8 *praddr;
443         u8 *psaddr;
444         u16 fc, type;
445         bool b_packet_matchbssid, b_packet_toself, b_packet_beacon;
446
447         tmp_buf = skb->data + pstatus->rx_drvinfo_size + pstatus->rx_bufshift;
448
449         hdr = (struct ieee80211_hdr *)tmp_buf;
450         fc = le16_to_cpu(hdr->frame_control);
451         type = WLAN_FC_GET_TYPE(fc);
452         praddr = hdr->addr1;
453         psaddr = ieee80211_get_SA(hdr);
454         memcpy(pstatus->psaddr, psaddr, ETH_ALEN);
455
456         b_packet_matchbssid = ((IEEE80211_FTYPE_CTL != type) &&
457              (!ether_addr_equal(mac->bssid, (fc & IEEE80211_FCTL_TODS) ?
458                                   hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS) ?
459                                   hdr->addr2 : hdr->addr3)) && (!pstatus->b_hwerror) &&
460                                   (!pstatus->b_crc) && (!pstatus->b_icv));
461
462         b_packet_toself = b_packet_matchbssid &&
463             (!ether_addr_equal(praddr, rtlefuse->dev_addr));
464
465         if (ieee80211_is_beacon(fc))
466                 b_packet_beacon = true;
467         else
468                 b_packet_beacon = false;
469
470         if (b_packet_beacon && b_packet_matchbssid)
471                 rtl_priv(hw)->dm.dbginfo.num_qry_beacon_pkt++;
472
473         _rtl8821ae_query_rxphystatus(hw, pstatus, pdesc, p_drvinfo,
474                                    b_packet_matchbssid, b_packet_toself,
475                                    b_packet_beacon);
476         /*_rtl8821ae_smart_antenna(hw, pstatus); */
477         rtl_process_phyinfo(hw, tmp_buf, pstatus);
478 }
479
480 static void _rtl8821ae_insert_emcontent(struct rtl_tcb_desc *ptcb_desc,
481                                       u8 *virtualaddress)
482 {
483         u32 dwtmp = 0;
484         memset(virtualaddress, 0, 8);
485
486         SET_EARLYMODE_PKTNUM(virtualaddress, ptcb_desc->empkt_num);
487         if (ptcb_desc->empkt_num == 1)
488                 dwtmp = ptcb_desc->empkt_len[0];
489         else {
490                 dwtmp = ptcb_desc->empkt_len[0];
491                 dwtmp += ((dwtmp%4)?(4-dwtmp%4):0)+4;
492                 dwtmp += ptcb_desc->empkt_len[1];
493         }
494         SET_EARLYMODE_LEN0(virtualaddress, dwtmp);
495
496         if (ptcb_desc->empkt_num <= 3)
497                 dwtmp = ptcb_desc->empkt_len[2];
498         else {
499                 dwtmp = ptcb_desc->empkt_len[2];
500                 dwtmp += ((dwtmp%4)?(4-dwtmp%4):0)+4;
501                 dwtmp += ptcb_desc->empkt_len[3];
502         }
503         SET_EARLYMODE_LEN1(virtualaddress, dwtmp);
504         if (ptcb_desc->empkt_num <= 5)
505                 dwtmp = ptcb_desc->empkt_len[4];
506         else {
507                 dwtmp = ptcb_desc->empkt_len[4];
508                 dwtmp += ((dwtmp%4)?(4-dwtmp%4):0)+4;
509                 dwtmp += ptcb_desc->empkt_len[5];
510         }
511         SET_EARLYMODE_LEN2_1(virtualaddress, dwtmp & 0xF);
512         SET_EARLYMODE_LEN2_2(virtualaddress, dwtmp >> 4);
513         if (ptcb_desc->empkt_num <= 7)
514                 dwtmp = ptcb_desc->empkt_len[6];
515         else {
516                 dwtmp = ptcb_desc->empkt_len[6];
517                 dwtmp += ((dwtmp%4)?(4-dwtmp%4):0)+4;
518                 dwtmp += ptcb_desc->empkt_len[7];
519         }
520         SET_EARLYMODE_LEN3(virtualaddress, dwtmp);
521         if (ptcb_desc->empkt_num <= 9)
522                 dwtmp = ptcb_desc->empkt_len[8];
523         else {
524                 dwtmp = ptcb_desc->empkt_len[8];
525                 dwtmp += ((dwtmp%4)?(4-dwtmp%4):0)+4;
526                 dwtmp += ptcb_desc->empkt_len[9];
527         }
528         SET_EARLYMODE_LEN4(virtualaddress, dwtmp);
529 }
530
531 bool rtl8821ae_rx_query_desc(struct ieee80211_hw *hw,
532                            struct rtl_stats *status,
533                            struct ieee80211_rx_status *rx_status,
534                            u8 *pdesc, struct sk_buff *skb)
535 {
536         struct rtl_priv *rtlpriv = rtl_priv(hw);
537         struct rx_fwinfo_8821ae *p_drvinfo;
538         struct ieee80211_hdr *hdr;
539
540         u32 phystatus = GET_RX_DESC_PHYST(pdesc);
541
542         status->length = (u16) GET_RX_DESC_PKT_LEN(pdesc);
543         status->rx_drvinfo_size = (u8) GET_RX_DESC_DRV_INFO_SIZE(pdesc) *
544             RX_DRV_INFO_SIZE_UNIT;
545         status->rx_bufshift = (u8) (GET_RX_DESC_SHIFT(pdesc) & 0x03);
546         status->b_icv = (u16) GET_RX_DESC_ICV(pdesc);
547         status->b_crc = (u16) GET_RX_DESC_CRC32(pdesc);
548         status->b_hwerror = (status->b_crc | status->b_icv);
549         status->decrypted = !GET_RX_DESC_SWDEC(pdesc);
550         status->rate = (u8) GET_RX_DESC_RXMCS(pdesc);
551         status->b_shortpreamble = (u16) GET_RX_DESC_SPLCP(pdesc);
552         status->b_isampdu = (bool) (GET_RX_DESC_PAGGR(pdesc) == 1);
553         status->b_isfirst_ampdu = (bool) (GET_RX_DESC_PAGGR(pdesc) == 1);
554         status->timestamp_low = GET_RX_DESC_TSFL(pdesc);
555         status->rx_is40Mhzpacket = (bool) GET_RX_DESC_BW(pdesc);
556         status->macid = GET_RX_DESC_MACID(pdesc);
557         status->b_is_ht = (bool)GET_RX_DESC_RXHT(pdesc);
558
559         status->b_is_cck = RX_HAL_IS_CCK_RATE(status->rate);
560
561         if (GET_RX_STATUS_DESC_RPT_SEL(pdesc))
562                 status->packet_report_type = C2H_PACKET;
563         else
564                 status->packet_report_type = NORMAL_RX;
565
566         if (GET_RX_STATUS_DESC_PATTERN_MATCH(pdesc))
567                 status->wake_match = BIT(2);
568         else if (GET_RX_STATUS_DESC_MAGIC_MATCH(pdesc))
569                 status->wake_match = BIT(1);
570         else if (GET_RX_STATUS_DESC_UNICAST_MATCH(pdesc))
571                 status->wake_match = BIT(0);
572         else
573                 status->wake_match = 0;
574
575         if (status->wake_match)
576                 RT_TRACE(COMP_RXDESC,DBG_LOUD,
577                 ("GGGGGGGGGGGGGet Wakeup Packet!! WakeMatch=%d\n",status->wake_match ));
578         rx_status->freq = hw->conf.chandef.chan->center_freq;
579         rx_status->band = hw->conf.chandef.chan->band;
580
581         hdr = (struct ieee80211_hdr *)(skb->data + status->rx_drvinfo_size
582                         + status->rx_bufshift);
583
584         if (status->b_crc)
585                 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
586
587         if (status->rx_is40Mhzpacket)
588                 rx_status->flag |= RX_FLAG_40MHZ;
589
590         if (status->b_is_ht)
591                 rx_status->flag |= RX_FLAG_HT;
592
593         rx_status->flag |= RX_FLAG_MACTIME_MPDU;
594
595         /* hw will set status->decrypted true, if it finds the
596          * frame is open data frame or mgmt frame. */
597         /* So hw will not decryption robust management frame
598          * for IEEE80211w but still set status->decrypted
599          * true, so here we should set it back to undecrypted
600          * for IEEE80211w frame, and mac80211 sw will help
601          * to decrypt it */
602         if (status->decrypted) {
603                 if (!hdr) {
604                         WARN_ON_ONCE(true);
605                         pr_err("decrypted is true but hdr NULL, from skb %p\n",
606                                 rtl_get_hdr(skb));
607                                 return false;
608                 }
609
610                 if ((_ieee80211_is_robust_mgmt_frame(hdr)) &&
611                         (ieee80211_has_protected(hdr->frame_control)))
612                         rx_status->flag &= ~RX_FLAG_DECRYPTED;
613                 else
614                         rx_status->flag |= RX_FLAG_DECRYPTED;
615         }
616
617         /* rate_idx: index of data rate into band's
618          * supported rates or MCS index if HT rates
619          * are use (RX_FLAG_HT)*/
620         /* Notice: this is diff with windows define */
621         rx_status->rate_idx = _rtl8821ae_rate_mapping(hw,
622                                 status->b_is_ht, status->rate);
623
624         rx_status->mactime = status->timestamp_low;
625         if (phystatus == true) {
626                 p_drvinfo = (struct rx_fwinfo_8821ae *)(skb->data +
627                                                      status->rx_bufshift);
628
629                 _rtl8821ae_translate_rx_signal_stuff(hw,
630                                                    skb, status, pdesc,
631                                                    p_drvinfo);
632         }
633
634         /*rx_status->qual = status->signal; */
635         rx_status->signal = status->recvsignalpower + 10;
636         /*rx_status->noise = -status->noise; */
637         if (status->packet_report_type == TX_REPORT2){
638                 status->macid_valid_entry[0] = GET_RX_RPT2_DESC_MACID_VALID_1(pdesc);
639                 status->macid_valid_entry[1] = GET_RX_RPT2_DESC_MACID_VALID_2(pdesc);
640         }
641         return true;
642 }
643
644 void rtl8821ae_tx_fill_desc(struct ieee80211_hw *hw,
645                           struct ieee80211_hdr *hdr, u8 *pdesc_tx, u8 *txbd,
646                           struct ieee80211_tx_info *info,
647                           struct ieee80211_sta *sta,
648                           struct sk_buff *skb,
649                           u8 hw_queue, struct rtl_tcb_desc *ptcb_desc)
650 {
651         struct rtl_priv *rtlpriv = rtl_priv(hw);
652         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
653         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
654         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
655         u8 *pdesc = (u8 *) pdesc_tx;
656         u16 seq_number;
657         u16 fc = le16_to_cpu(hdr->frame_control);
658         unsigned int buf_len = 0;
659         unsigned int skb_len = skb->len;
660         u8 fw_qsel = _rtl8821ae_map_hwqueue_to_fwqueue(skb, hw_queue);
661         bool b_firstseg = ((hdr->seq_ctrl &
662                             cpu_to_le16(IEEE80211_SCTL_FRAG)) == 0);
663         bool b_lastseg = ((hdr->frame_control &
664                            cpu_to_le16(IEEE80211_FCTL_MOREFRAGS)) == 0);
665         dma_addr_t mapping;
666         u8 bw_40 = 0;
667         u8 short_gi = 0;
668
669         if (mac->opmode == NL80211_IFTYPE_STATION) {
670                 bw_40 = mac->bw_40;
671         } else if (mac->opmode == NL80211_IFTYPE_AP ||
672                 mac->opmode == NL80211_IFTYPE_ADHOC) {
673                 if (sta)
674                         bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
675         }
676         seq_number = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
677         rtl_get_tcb_desc(hw, info, sta, skb, ptcb_desc);
678         /* reserve 8 byte for AMPDU early mode */
679         if (rtlhal->b_earlymode_enable) {
680                 skb_push(skb, EM_HDR_LEN);
681                 memset(skb->data, 0, EM_HDR_LEN);
682         }
683         buf_len = skb->len;
684         mapping = pci_map_single(rtlpci->pdev, skb->data, skb->len,
685                                  PCI_DMA_TODEVICE);
686         if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
687                 RT_TRACE(COMP_SEND, DBG_TRACE,
688                          ("DMA mapping error"));
689                 return;
690         }
691         CLEAR_PCI_TX_DESC_CONTENT(pdesc, sizeof(struct tx_desc_8821ae));
692         if (ieee80211_is_nullfunc(fc) || ieee80211_is_ctl(fc)) {
693                 b_firstseg = true;
694                 b_lastseg = true;
695         }
696         if (b_firstseg) {
697                 if (rtlhal->b_earlymode_enable) {
698                         SET_TX_DESC_PKT_OFFSET(pdesc, 1);
699                         SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN + EM_HDR_LEN);
700                         if (ptcb_desc->empkt_num) {
701                                 RT_TRACE(COMP_SEND, DBG_TRACE,
702                                          ("Insert 8 byte.pTcb->EMPktNum:%d\n",
703                                           ptcb_desc->empkt_num));
704                                 _rtl8821ae_insert_emcontent(ptcb_desc, (u8 *)(skb->data));
705                         }
706                 } else {
707                         SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
708                 }
709
710                 /* ptcb_desc->use_driver_rate = true; */
711                 SET_TX_DESC_TX_RATE(pdesc, ptcb_desc->hw_rate);
712                 if (ptcb_desc->hw_rate > DESC_RATEMCS0) {
713                         short_gi = (ptcb_desc->use_shortgi) ? 1 : 0;
714                 } else {
715                         short_gi = (ptcb_desc->use_shortpreamble) ? 1 :0;
716                 }
717                 SET_TX_DESC_DATA_SHORTGI(pdesc, short_gi);
718
719                 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
720                         SET_TX_DESC_AGG_ENABLE(pdesc, 1);
721                         SET_TX_DESC_MAX_AGG_NUM(pdesc, 0x14);
722                 }
723                 SET_TX_DESC_SEQ(pdesc, seq_number);
724                 SET_TX_DESC_RTS_ENABLE(pdesc, ((ptcb_desc->b_rts_enable &&
725                                                 !ptcb_desc->b_cts_enable) ? 1 : 0));
726                 SET_TX_DESC_HW_RTS_ENABLE(pdesc,0);
727                 SET_TX_DESC_CTS2SELF(pdesc, ((ptcb_desc->b_cts_enable) ? 1 : 0));
728         /*      SET_TX_DESC_RTS_STBC(pdesc, ((ptcb_desc->b_rts_stbc) ? 1 : 0));*/
729
730                 SET_TX_DESC_RTS_RATE(pdesc, ptcb_desc->rts_rate);
731         /*      SET_TX_DESC_RTS_BW(pdesc, 0);*/
732                 SET_TX_DESC_RTS_SC(pdesc, ptcb_desc->rts_sc);
733                 SET_TX_DESC_RTS_SHORT(pdesc, ((ptcb_desc->rts_rate <= DESC_RATE54M) ?
734                         (ptcb_desc->b_rts_use_shortpreamble ? 1 : 0) :
735                         (ptcb_desc->b_rts_use_shortgi ? 1 : 0)));
736
737                 if(ptcb_desc->btx_enable_sw_calc_duration)
738                         SET_TX_DESC_NAV_USE_HDR(pdesc, 1);
739
740                 if (bw_40) {
741                         if (ptcb_desc->b_packet_bw) {
742                                 SET_TX_DESC_DATA_BW(pdesc, 1);
743                                 SET_TX_DESC_TX_SUB_CARRIER(pdesc, 3);
744                         } else {
745                                 SET_TX_DESC_DATA_BW(pdesc, 0);
746                                 SET_TX_DESC_TX_SUB_CARRIER(pdesc, mac->cur_40_prime_sc);
747                         }
748                 } else {
749                         SET_TX_DESC_DATA_BW(pdesc, 0);
750                         SET_TX_DESC_TX_SUB_CARRIER(pdesc, 0);
751                 }
752
753                 SET_TX_DESC_LINIP(pdesc, 0);
754                 SET_TX_DESC_PKT_SIZE(pdesc, (u16) skb_len);
755                 if (sta) {
756                         u8 ampdu_density = sta->ht_cap.ampdu_density;
757                         SET_TX_DESC_AMPDU_DENSITY(pdesc, ampdu_density);
758                 }
759                 if (info->control.hw_key) {
760                         struct ieee80211_key_conf *keyconf = info->control.hw_key;
761                         switch (keyconf->cipher) {
762                         case WLAN_CIPHER_SUITE_WEP40:
763                         case WLAN_CIPHER_SUITE_WEP104:
764                         case WLAN_CIPHER_SUITE_TKIP:
765                                 SET_TX_DESC_SEC_TYPE(pdesc, 0x1);
766                                 break;
767                         case WLAN_CIPHER_SUITE_CCMP:
768                                 SET_TX_DESC_SEC_TYPE(pdesc, 0x3);
769                                 break;
770                         default:
771                                 SET_TX_DESC_SEC_TYPE(pdesc, 0x0);
772                                 break;
773
774                         }
775                 }
776
777                 SET_TX_DESC_QUEUE_SEL(pdesc, fw_qsel);
778                 SET_TX_DESC_DATA_RATE_FB_LIMIT(pdesc, 0x1F);
779                 SET_TX_DESC_RTS_RATE_FB_LIMIT(pdesc, 0xF);
780                 SET_TX_DESC_DISABLE_FB(pdesc, ptcb_desc->disable_ratefallback ? 1 : 0);
781                 SET_TX_DESC_USE_RATE(pdesc, ptcb_desc->use_driver_rate ? 1 : 0);
782
783 #if 0
784                 SET_TX_DESC_USE_RATE(pdesc, 1);
785                 SET_TX_DESC_TX_RATE(pdesc, 0x04);
786
787                 SET_TX_DESC_RETRY_LIMIT_ENABLE(pdesc, 1);
788                 SET_TX_DESC_DATA_RETRY_LIMIT(pdesc, 0x3f);
789 #endif
790
791                 /*SET_TX_DESC_PWR_STATUS(pdesc, pwr_status);*/
792                 /* Set TxRate and RTSRate in TxDesc  */
793                 /* This prevent Tx initial rate of new-coming packets */
794                 /* from being overwritten by retried  packet rate.*/
795                 if (!ptcb_desc->use_driver_rate) {
796                         /*SET_TX_DESC_RTS_RATE(pdesc, 0x08); */
797                         /* SET_TX_DESC_TX_RATE(pdesc, 0x0b); */
798                 }
799                 if (ieee80211_is_data_qos(fc)) {
800                         if (mac->rdg_en) {
801                                 RT_TRACE(COMP_SEND, DBG_TRACE,
802                                         ("Enable RDG function.\n"));
803                                 SET_TX_DESC_RDG_ENABLE(pdesc, 1);
804                                 SET_TX_DESC_HTC(pdesc, 1);
805                         }
806                 }
807         }
808
809         SET_TX_DESC_FIRST_SEG(pdesc, (b_firstseg ? 1 : 0));
810         SET_TX_DESC_LAST_SEG(pdesc, (b_lastseg ? 1 : 0));
811         SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16) buf_len);
812         SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, cpu_to_le32(mapping));
813         //if (rtlpriv->dm.b_useramask) {
814         if(1){
815                 SET_TX_DESC_RATE_ID(pdesc, ptcb_desc->ratr_index);
816                 SET_TX_DESC_MACID(pdesc, ptcb_desc->mac_id);
817         } else {
818                 SET_TX_DESC_RATE_ID(pdesc, 0xC + ptcb_desc->ratr_index);
819                 SET_TX_DESC_MACID(pdesc, ptcb_desc->mac_id);
820         }
821 /*      if (ieee80211_is_data_qos(fc))
822                 SET_TX_DESC_QOS(pdesc, 1);
823 */
824         if (!ieee80211_is_data_qos(fc))  {
825                 SET_TX_DESC_HWSEQ_EN(pdesc, 1);
826                 SET_TX_DESC_HWSEQ_SEL(pdesc, 0);
827         }
828         SET_TX_DESC_MORE_FRAG(pdesc, (b_lastseg ? 0 : 1));
829         if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
830             is_broadcast_ether_addr(ieee80211_get_DA(hdr))) {
831                 SET_TX_DESC_BMC(pdesc, 1);
832         }
833
834         rtl8821ae_dm_set_tx_ant_by_tx_info(hw,pdesc,ptcb_desc->mac_id);
835         RT_TRACE(COMP_SEND, DBG_TRACE, ("\n"));
836 }
837
838 void rtl8821ae_tx_fill_cmddesc(struct ieee80211_hw *hw,
839                              u8 *pdesc, bool b_firstseg,
840                              bool b_lastseg, struct sk_buff *skb)
841 {
842         struct rtl_priv *rtlpriv = rtl_priv(hw);
843         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
844         u8 fw_queue = QSLT_BEACON;
845
846         dma_addr_t mapping = pci_map_single(rtlpci->pdev,
847                                             skb->data, skb->len,
848                                             PCI_DMA_TODEVICE);
849
850         if (pci_dma_mapping_error(rtlpci->pdev, mapping)) {
851                 RT_TRACE(COMP_SEND, DBG_TRACE,
852                          ("DMA mapping error"));
853                 return;
854         }
855         CLEAR_PCI_TX_DESC_CONTENT(pdesc, TX_DESC_SIZE);
856
857         SET_TX_DESC_FIRST_SEG(pdesc, 1);
858         SET_TX_DESC_LAST_SEG(pdesc, 1);
859
860         SET_TX_DESC_PKT_SIZE((u8 *) pdesc, (u16) (skb->len));
861
862         SET_TX_DESC_OFFSET(pdesc, USB_HWDESC_HEADER_LEN);
863
864         SET_TX_DESC_USE_RATE(pdesc, 1);
865         SET_TX_DESC_TX_RATE(pdesc, DESC_RATE1M);
866         SET_TX_DESC_DISABLE_FB(pdesc, 1);
867
868         SET_TX_DESC_DATA_BW(pdesc, 0);
869
870         SET_TX_DESC_HWSEQ_EN(pdesc, 1);
871
872         SET_TX_DESC_QUEUE_SEL(pdesc, fw_queue);
873 /*
874         if(IsCtrlNDPA(VirtualAddress) || IsMgntNDPA(VirtualAddress))
875         {
876                 SET_TX_DESC_DATA_RETRY_LIMIT_8812(pDesc, 5);
877                 SET_TX_DESC_RETRY_LIMIT_ENABLE_8812(pDesc, 1);
878
879                 if(IsMgntNDPA(VirtualAddress))
880                 {
881                         SET_TX_DESC_NDPA_8812(pDesc, 1);
882                         SET_TX_DESC_RTS_SC_8812(pDesc, SCMapping_8812(Adapter, pTcb));
883                 }
884                 else
885                 {
886                         SET_TX_DESC_NDPA_8812(pDesc, 2);
887                         SET_TX_DESC_RTS_SC_8812(pDesc, SCMapping_8812(Adapter, pTcb));
888                 }
889         }*/
890
891         SET_TX_DESC_TX_BUFFER_SIZE(pdesc, (u16) (skb->len));
892
893         SET_TX_DESC_TX_BUFFER_ADDRESS(pdesc, cpu_to_le32(mapping));
894
895         SET_TX_DESC_MACID(pdesc, 0);
896
897         SET_TX_DESC_OWN(pdesc, 1);
898
899         RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
900                       "H2C Tx Cmd Content\n",
901                       pdesc, TX_DESC_SIZE);
902 }
903
904 void rtl8821ae_set_desc(struct ieee80211_hw * hw, u8 *pdesc, bool istx, u8 desc_name, u8 *val)
905 {
906         if (istx == true) {
907                 switch (desc_name) {
908                 case HW_DESC_OWN:
909                         SET_TX_DESC_OWN(pdesc, 1);
910                         break;
911                 case HW_DESC_TX_NEXTDESC_ADDR:
912                         SET_TX_DESC_NEXT_DESC_ADDRESS(pdesc, *(u32 *) val);
913                         break;
914                 default:
915                         RT_ASSERT(false, ("ERR txdesc :%d"
916                                           " not process\n", desc_name));
917                         break;
918                 }
919         } else {
920                 switch (desc_name) {
921                 case HW_DESC_RXOWN:
922                         SET_RX_DESC_OWN(pdesc, 1);
923                         break;
924                 case HW_DESC_RXBUFF_ADDR:
925                         SET_RX_DESC_BUFF_ADDR(pdesc, *(u32 *) val);
926                         break;
927                 case HW_DESC_RXPKT_LEN:
928                         SET_RX_DESC_PKT_LEN(pdesc, *(u32 *) val);
929                         break;
930                 case HW_DESC_RXERO:
931                         SET_RX_DESC_EOR(pdesc, 1);
932                         break;
933                 default:
934                         RT_ASSERT(false, ("ERR rxdesc :%d "
935                                           "not process\n", desc_name));
936                         break;
937                 }
938         }
939 }
940
941 u32 rtl8821ae_get_desc(u8 *pdesc, bool istx, u8 desc_name)
942 {
943         u32 ret = 0;
944
945         if (istx == true) {
946                 switch (desc_name) {
947                 case HW_DESC_OWN:
948                         ret = GET_TX_DESC_OWN(pdesc);
949                         break;
950                 case HW_DESC_TXBUFF_ADDR:
951                         ret = GET_TX_DESC_TX_BUFFER_ADDRESS(pdesc);
952                         break;
953                 default:
954                         RT_ASSERT(false, ("ERR txdesc :%d "
955                                           "not process\n", desc_name));
956                         break;
957                 }
958         } else {
959                 switch (desc_name) {
960                 case HW_DESC_OWN:
961                         ret = GET_RX_DESC_OWN(pdesc);
962                         break;
963                 case HW_DESC_RXPKT_LEN:
964                         ret = GET_RX_DESC_PKT_LEN(pdesc);
965                         break;
966                 default:
967                         RT_ASSERT(false, ("ERR rxdesc :%d "
968                                           "not process\n", desc_name));
969                         break;
970                 }
971         }
972         return ret;
973 }
974
975 bool rtl8821ae_is_tx_desc_closed(struct ieee80211_hw *hw,
976                                  u8 hw_queue, u16 index)
977 {
978         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
979         struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[hw_queue];
980         u8 *entry = (u8 *)(&ring->desc[ring->idx]);
981         u8 own = (u8) rtl8821ae_get_desc(entry, true, HW_DESC_OWN);
982
983         /*
984          *beacon packet will only use the first
985          *descriptor defautly,and the own may not
986          *be cleared by the hardware
987          */
988         if (own)
989                 return false;
990         else
991                 return true;
992 }
993
994
995 void rtl8821ae_tx_polling(struct ieee80211_hw *hw, u8 hw_queue)
996 {
997         struct rtl_priv *rtlpriv = rtl_priv(hw);
998
999         if (hw_queue == BEACON_QUEUE) {
1000                 rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG, BIT(4));
1001         } else {
1002                 rtl_write_word(rtlpriv, REG_PCIE_CTRL_REG,
1003                                BIT(0) << (hw_queue));
1004         }
1005 }