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