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