base.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. // SPDX-License-Identifier: GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Copyright(c) 2009-2012 Realtek Corporation.
  5. *
  6. * Contact Information:
  7. * wlanfae <wlanfae@realtek.com>
  8. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  9. * Hsinchu 300, Taiwan.
  10. *
  11. * Larry Finger <Larry.Finger@lwfinger.net>
  12. *
  13. *****************************************************************************/
  14. #include "wifi.h"
  15. #include "rc.h"
  16. #include "base.h"
  17. #include "efuse.h"
  18. #include "cam.h"
  19. #include "ps.h"
  20. #include "regd.h"
  21. #include "pci.h"
  22. #include <linux/ip.h>
  23. #include <linux/module.h>
  24. #include <linux/udp.h>
  25. /*
  26. *NOTICE!!!: This file will be very big, we should
  27. *keep it clear under following roles:
  28. *
  29. *This file include following parts, so, if you add new
  30. *functions into this file, please check which part it
  31. *should includes. or check if you should add new part
  32. *for this file:
  33. *
  34. *1) mac80211 init functions
  35. *2) tx information functions
  36. *3) functions called by core.c
  37. *4) wq & timer callback functions
  38. *5) frame process functions
  39. *6) IOT functions
  40. *7) sysfs functions
  41. *8) vif functions
  42. *9) ...
  43. */
  44. /*********************************************************
  45. *
  46. * mac80211 init functions
  47. *
  48. *********************************************************/
  49. static struct ieee80211_channel rtl_channeltable_2g[] = {
  50. {.center_freq = 2412, .hw_value = 1,},
  51. {.center_freq = 2417, .hw_value = 2,},
  52. {.center_freq = 2422, .hw_value = 3,},
  53. {.center_freq = 2427, .hw_value = 4,},
  54. {.center_freq = 2432, .hw_value = 5,},
  55. {.center_freq = 2437, .hw_value = 6,},
  56. {.center_freq = 2442, .hw_value = 7,},
  57. {.center_freq = 2447, .hw_value = 8,},
  58. {.center_freq = 2452, .hw_value = 9,},
  59. {.center_freq = 2457, .hw_value = 10,},
  60. {.center_freq = 2462, .hw_value = 11,},
  61. {.center_freq = 2467, .hw_value = 12,},
  62. {.center_freq = 2472, .hw_value = 13,},
  63. {.center_freq = 2484, .hw_value = 14,},
  64. };
  65. static struct ieee80211_channel rtl_channeltable_5g[] = {
  66. {.center_freq = 5180, .hw_value = 36,},
  67. {.center_freq = 5200, .hw_value = 40,},
  68. {.center_freq = 5220, .hw_value = 44,},
  69. {.center_freq = 5240, .hw_value = 48,},
  70. {.center_freq = 5260, .hw_value = 52,},
  71. {.center_freq = 5280, .hw_value = 56,},
  72. {.center_freq = 5300, .hw_value = 60,},
  73. {.center_freq = 5320, .hw_value = 64,},
  74. {.center_freq = 5500, .hw_value = 100,},
  75. {.center_freq = 5520, .hw_value = 104,},
  76. {.center_freq = 5540, .hw_value = 108,},
  77. {.center_freq = 5560, .hw_value = 112,},
  78. {.center_freq = 5580, .hw_value = 116,},
  79. {.center_freq = 5600, .hw_value = 120,},
  80. {.center_freq = 5620, .hw_value = 124,},
  81. {.center_freq = 5640, .hw_value = 128,},
  82. {.center_freq = 5660, .hw_value = 132,},
  83. {.center_freq = 5680, .hw_value = 136,},
  84. {.center_freq = 5700, .hw_value = 140,},
  85. {.center_freq = 5745, .hw_value = 149,},
  86. {.center_freq = 5765, .hw_value = 153,},
  87. {.center_freq = 5785, .hw_value = 157,},
  88. {.center_freq = 5805, .hw_value = 161,},
  89. {.center_freq = 5825, .hw_value = 165,},
  90. };
  91. static struct ieee80211_rate rtl_ratetable_2g[] = {
  92. {.bitrate = 10, .hw_value = 0x00,},
  93. {.bitrate = 20, .hw_value = 0x01,},
  94. {.bitrate = 55, .hw_value = 0x02,},
  95. {.bitrate = 110, .hw_value = 0x03,},
  96. {.bitrate = 60, .hw_value = 0x04,},
  97. {.bitrate = 90, .hw_value = 0x05,},
  98. {.bitrate = 120, .hw_value = 0x06,},
  99. {.bitrate = 180, .hw_value = 0x07,},
  100. {.bitrate = 240, .hw_value = 0x08,},
  101. {.bitrate = 360, .hw_value = 0x09,},
  102. {.bitrate = 480, .hw_value = 0x0a,},
  103. {.bitrate = 540, .hw_value = 0x0b,},
  104. };
  105. static struct ieee80211_rate rtl_ratetable_5g[] = {
  106. {.bitrate = 60, .hw_value = 0x04,},
  107. {.bitrate = 90, .hw_value = 0x05,},
  108. {.bitrate = 120, .hw_value = 0x06,},
  109. {.bitrate = 180, .hw_value = 0x07,},
  110. {.bitrate = 240, .hw_value = 0x08,},
  111. {.bitrate = 360, .hw_value = 0x09,},
  112. {.bitrate = 480, .hw_value = 0x0a,},
  113. {.bitrate = 540, .hw_value = 0x0b,},
  114. };
  115. static const struct ieee80211_supported_band rtl_band_2ghz = {
  116. .band = NL80211_BAND_2GHZ,
  117. .channels = rtl_channeltable_2g,
  118. .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
  119. .bitrates = rtl_ratetable_2g,
  120. .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
  121. .ht_cap = {0},
  122. };
  123. static struct ieee80211_supported_band rtl_band_5ghz = {
  124. .band = NL80211_BAND_5GHZ,
  125. .channels = rtl_channeltable_5g,
  126. .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
  127. .bitrates = rtl_ratetable_5g,
  128. .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
  129. .ht_cap = {0},
  130. };
  131. static const u8 tid_to_ac[] = {
  132. 2, /* IEEE80211_AC_BE */
  133. 3, /* IEEE80211_AC_BK */
  134. 3, /* IEEE80211_AC_BK */
  135. 2, /* IEEE80211_AC_BE */
  136. 1, /* IEEE80211_AC_VI */
  137. 1, /* IEEE80211_AC_VI */
  138. 0, /* IEEE80211_AC_VO */
  139. 0, /* IEEE80211_AC_VO */
  140. };
  141. u8 rtl_tid_to_ac(u8 tid)
  142. {
  143. return tid_to_ac[tid];
  144. }
  145. static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
  146. struct ieee80211_sta_ht_cap *ht_cap)
  147. {
  148. struct rtl_priv *rtlpriv = rtl_priv(hw);
  149. struct rtl_phy *rtlphy = &rtlpriv->phy;
  150. ht_cap->ht_supported = true;
  151. ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  152. IEEE80211_HT_CAP_SGI_40 |
  153. IEEE80211_HT_CAP_SGI_20 |
  154. IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
  155. if (rtlpriv->rtlhal.disable_amsdu_8k)
  156. ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
  157. /*
  158. *Maximum length of AMPDU that the STA can receive.
  159. *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
  160. */
  161. ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  162. /*Minimum MPDU start spacing , */
  163. ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
  164. ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  165. /*hw->wiphy->bands[NL80211_BAND_2GHZ]
  166. *base on ant_num
  167. *rx_mask: RX mask
  168. *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
  169. *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
  170. *if rx_ant >= 3 rx_mask[2]= 0xff;
  171. *if BW_40 rx_mask[4]= 0x01;
  172. *highest supported RX rate
  173. */
  174. if (rtlpriv->dm.supp_phymode_switch) {
  175. pr_info("Support phy mode switch\n");
  176. ht_cap->mcs.rx_mask[0] = 0xFF;
  177. ht_cap->mcs.rx_mask[1] = 0xFF;
  178. ht_cap->mcs.rx_mask[4] = 0x01;
  179. ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
  180. } else {
  181. if (get_rf_type(rtlphy) == RF_1T2R ||
  182. get_rf_type(rtlphy) == RF_2T2R) {
  183. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  184. "1T2R or 2T2R\n");
  185. ht_cap->mcs.rx_mask[0] = 0xFF;
  186. ht_cap->mcs.rx_mask[1] = 0xFF;
  187. ht_cap->mcs.rx_mask[4] = 0x01;
  188. ht_cap->mcs.rx_highest =
  189. cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
  190. } else if (get_rf_type(rtlphy) == RF_1T1R) {
  191. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
  192. ht_cap->mcs.rx_mask[0] = 0xFF;
  193. ht_cap->mcs.rx_mask[1] = 0x00;
  194. ht_cap->mcs.rx_mask[4] = 0x01;
  195. ht_cap->mcs.rx_highest =
  196. cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
  197. }
  198. }
  199. }
  200. static void _rtl_init_hw_vht_capab(struct ieee80211_hw *hw,
  201. struct ieee80211_sta_vht_cap *vht_cap)
  202. {
  203. struct rtl_priv *rtlpriv = rtl_priv(hw);
  204. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  205. if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE ||
  206. rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE) {
  207. u16 mcs_map;
  208. vht_cap->vht_supported = true;
  209. vht_cap->cap =
  210. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  211. IEEE80211_VHT_CAP_SHORT_GI_80 |
  212. IEEE80211_VHT_CAP_TXSTBC |
  213. IEEE80211_VHT_CAP_RXSTBC_1 |
  214. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  215. IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  216. IEEE80211_VHT_CAP_HTC_VHT |
  217. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
  218. IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
  219. IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
  220. 0;
  221. mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
  222. IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
  223. IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
  224. IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
  225. IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
  226. IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
  227. IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
  228. IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
  229. vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  230. vht_cap->vht_mcs.rx_highest =
  231. cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
  232. vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  233. vht_cap->vht_mcs.tx_highest =
  234. cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
  235. } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
  236. u16 mcs_map;
  237. vht_cap->vht_supported = true;
  238. vht_cap->cap =
  239. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  240. IEEE80211_VHT_CAP_SHORT_GI_80 |
  241. IEEE80211_VHT_CAP_TXSTBC |
  242. IEEE80211_VHT_CAP_RXSTBC_1 |
  243. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  244. IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  245. IEEE80211_VHT_CAP_HTC_VHT |
  246. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
  247. IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
  248. IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
  249. 0;
  250. mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
  251. IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
  252. IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
  253. IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
  254. IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
  255. IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
  256. IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
  257. IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
  258. vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  259. vht_cap->vht_mcs.rx_highest =
  260. cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
  261. vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  262. vht_cap->vht_mcs.tx_highest =
  263. cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
  264. }
  265. }
  266. static void _rtl_init_mac80211(struct ieee80211_hw *hw)
  267. {
  268. struct rtl_priv *rtlpriv = rtl_priv(hw);
  269. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  270. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  271. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  272. struct ieee80211_supported_band *sband;
  273. if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
  274. rtlhal->bandset == BAND_ON_BOTH) {
  275. /* 1: 2.4 G bands */
  276. /* <1> use mac->bands as mem for hw->wiphy->bands */
  277. sband = &rtlmac->bands[NL80211_BAND_2GHZ];
  278. /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
  279. * to default value(1T1R)
  280. */
  281. memcpy(&rtlmac->bands[NL80211_BAND_2GHZ], &rtl_band_2ghz,
  282. sizeof(struct ieee80211_supported_band));
  283. /* <3> init ht cap base on ant_num */
  284. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  285. /* <4> set mac->sband to wiphy->sband */
  286. hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
  287. /* 2: 5 G bands */
  288. /* <1> use mac->bands as mem for hw->wiphy->bands */
  289. sband = &rtlmac->bands[NL80211_BAND_5GHZ];
  290. /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
  291. * to default value(1T1R)
  292. */
  293. memcpy(&rtlmac->bands[NL80211_BAND_5GHZ], &rtl_band_5ghz,
  294. sizeof(struct ieee80211_supported_band));
  295. /* <3> init ht cap base on ant_num */
  296. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  297. _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
  298. /* <4> set mac->sband to wiphy->sband */
  299. hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
  300. } else {
  301. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  302. /* <1> use mac->bands as mem for hw->wiphy->bands */
  303. sband = &rtlmac->bands[NL80211_BAND_2GHZ];
  304. /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
  305. * to default value(1T1R)
  306. */
  307. memcpy(&rtlmac->bands[NL80211_BAND_2GHZ],
  308. &rtl_band_2ghz,
  309. sizeof(struct ieee80211_supported_band));
  310. /* <3> init ht cap base on ant_num */
  311. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  312. /* <4> set mac->sband to wiphy->sband */
  313. hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
  314. } else if (rtlhal->current_bandtype == BAND_ON_5G) {
  315. /* <1> use mac->bands as mem for hw->wiphy->bands */
  316. sband = &rtlmac->bands[NL80211_BAND_5GHZ];
  317. /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
  318. * to default value(1T1R)
  319. */
  320. memcpy(&rtlmac->bands[NL80211_BAND_5GHZ],
  321. &rtl_band_5ghz,
  322. sizeof(struct ieee80211_supported_band));
  323. /* <3> init ht cap base on ant_num */
  324. _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
  325. _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
  326. /* <4> set mac->sband to wiphy->sband */
  327. hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
  328. } else {
  329. pr_err("Err BAND %d\n",
  330. rtlhal->current_bandtype);
  331. }
  332. }
  333. /* <5> set hw caps */
  334. ieee80211_hw_set(hw, SIGNAL_DBM);
  335. ieee80211_hw_set(hw, RX_INCLUDES_FCS);
  336. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  337. ieee80211_hw_set(hw, CONNECTION_MONITOR);
  338. ieee80211_hw_set(hw, MFP_CAPABLE);
  339. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  340. ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
  341. ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
  342. ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
  343. /* swlps or hwlps has been set in diff chip in init_sw_vars */
  344. if (rtlpriv->psc.swctrl_lps) {
  345. ieee80211_hw_set(hw, SUPPORTS_PS);
  346. ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
  347. }
  348. if (rtlpriv->psc.fwctrl_lps) {
  349. ieee80211_hw_set(hw, SUPPORTS_PS);
  350. ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
  351. }
  352. hw->wiphy->interface_modes =
  353. BIT(NL80211_IFTYPE_AP) |
  354. BIT(NL80211_IFTYPE_STATION) |
  355. BIT(NL80211_IFTYPE_ADHOC) |
  356. BIT(NL80211_IFTYPE_MESH_POINT) |
  357. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  358. BIT(NL80211_IFTYPE_P2P_GO);
  359. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  360. hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  361. hw->wiphy->rts_threshold = 2347;
  362. hw->queues = AC_MAX;
  363. hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
  364. /* TODO: Correct this value for our hw */
  365. hw->max_listen_interval = MAX_LISTEN_INTERVAL;
  366. hw->max_rate_tries = MAX_RATE_TRIES;
  367. /* hw->max_rates = 1; */
  368. hw->sta_data_size = sizeof(struct rtl_sta_info);
  369. /* wowlan is not supported by kernel if CONFIG_PM is not defined */
  370. #ifdef CONFIG_PM
  371. if (rtlpriv->psc.wo_wlan_mode) {
  372. if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
  373. rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
  374. if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
  375. rtlpriv->wowlan.n_patterns =
  376. MAX_SUPPORT_WOL_PATTERN_NUM;
  377. rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
  378. rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
  379. }
  380. hw->wiphy->wowlan = &rtlpriv->wowlan;
  381. }
  382. #endif
  383. /* <6> mac address */
  384. if (is_valid_ether_addr(rtlefuse->dev_addr)) {
  385. SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
  386. } else {
  387. u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
  388. get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
  389. SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
  390. }
  391. }
  392. static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
  393. {
  394. struct rtl_priv *rtlpriv = rtl_priv(hw);
  395. /* <1> timer */
  396. timer_setup(&rtlpriv->works.watchdog_timer,
  397. rtl_watch_dog_timer_callback, 0);
  398. timer_setup(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
  399. rtl_easy_concurrent_retrytimer_callback, 0);
  400. /* <2> work queue */
  401. rtlpriv->works.hw = hw;
  402. rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
  403. INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
  404. (void *)rtl_watchdog_wq_callback);
  405. INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
  406. (void *)rtl_ips_nic_off_wq_callback);
  407. INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
  408. (void *)rtl_swlps_wq_callback);
  409. INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
  410. (void *)rtl_swlps_rfon_wq_callback);
  411. INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
  412. (void *)rtl_fwevt_wq_callback);
  413. INIT_DELAYED_WORK(&rtlpriv->works.c2hcmd_wq,
  414. (void *)rtl_c2hcmd_wq_callback);
  415. }
  416. void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
  417. {
  418. struct rtl_priv *rtlpriv = rtl_priv(hw);
  419. del_timer_sync(&rtlpriv->works.watchdog_timer);
  420. cancel_delayed_work(&rtlpriv->works.watchdog_wq);
  421. cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
  422. cancel_delayed_work(&rtlpriv->works.ps_work);
  423. cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
  424. cancel_delayed_work(&rtlpriv->works.fwevt_wq);
  425. cancel_delayed_work(&rtlpriv->works.c2hcmd_wq);
  426. }
  427. void rtl_init_rfkill(struct ieee80211_hw *hw)
  428. {
  429. struct rtl_priv *rtlpriv = rtl_priv(hw);
  430. bool radio_state;
  431. bool blocked;
  432. u8 valid = 0;
  433. /*set init state to on */
  434. rtlpriv->rfkill.rfkill_state = true;
  435. wiphy_rfkill_set_hw_state(hw->wiphy, 0);
  436. radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
  437. if (valid) {
  438. pr_info("rtlwifi: wireless switch is %s\n",
  439. rtlpriv->rfkill.rfkill_state ? "on" : "off");
  440. rtlpriv->rfkill.rfkill_state = radio_state;
  441. blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
  442. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  443. }
  444. wiphy_rfkill_start_polling(hw->wiphy);
  445. }
  446. void rtl_deinit_rfkill(struct ieee80211_hw *hw)
  447. {
  448. wiphy_rfkill_stop_polling(hw->wiphy);
  449. }
  450. int rtl_init_core(struct ieee80211_hw *hw)
  451. {
  452. struct rtl_priv *rtlpriv = rtl_priv(hw);
  453. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  454. /* <1> init mac80211 */
  455. _rtl_init_mac80211(hw);
  456. rtlmac->hw = hw;
  457. /* <2> rate control register */
  458. hw->rate_control_algorithm = "rtl_rc";
  459. /*
  460. * <3> init CRDA must come after init
  461. * mac80211 hw in _rtl_init_mac80211.
  462. */
  463. if (rtl_regd_init(hw, rtl_reg_notifier)) {
  464. pr_err("REGD init failed\n");
  465. return 1;
  466. }
  467. /* <4> locks */
  468. mutex_init(&rtlpriv->locks.conf_mutex);
  469. mutex_init(&rtlpriv->locks.ips_mutex);
  470. mutex_init(&rtlpriv->locks.lps_mutex);
  471. spin_lock_init(&rtlpriv->locks.irq_th_lock);
  472. spin_lock_init(&rtlpriv->locks.h2c_lock);
  473. spin_lock_init(&rtlpriv->locks.rf_ps_lock);
  474. spin_lock_init(&rtlpriv->locks.rf_lock);
  475. spin_lock_init(&rtlpriv->locks.waitq_lock);
  476. spin_lock_init(&rtlpriv->locks.entry_list_lock);
  477. spin_lock_init(&rtlpriv->locks.c2hcmd_lock);
  478. spin_lock_init(&rtlpriv->locks.scan_list_lock);
  479. spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
  480. spin_lock_init(&rtlpriv->locks.fw_ps_lock);
  481. spin_lock_init(&rtlpriv->locks.iqk_lock);
  482. /* <5> init list */
  483. INIT_LIST_HEAD(&rtlpriv->entry_list);
  484. INIT_LIST_HEAD(&rtlpriv->c2hcmd_list);
  485. INIT_LIST_HEAD(&rtlpriv->scan_list.list);
  486. rtlmac->link_state = MAC80211_NOLINK;
  487. /* <6> init deferred work */
  488. _rtl_init_deferred_work(hw);
  489. return 0;
  490. }
  491. static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw);
  492. void rtl_deinit_core(struct ieee80211_hw *hw)
  493. {
  494. rtl_c2hcmd_launcher(hw, 0);
  495. rtl_free_entries_from_scan_list(hw);
  496. }
  497. void rtl_init_rx_config(struct ieee80211_hw *hw)
  498. {
  499. struct rtl_priv *rtlpriv = rtl_priv(hw);
  500. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  501. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *)&mac->rx_conf);
  502. }
  503. /*********************************************************
  504. *
  505. * tx information functions
  506. *
  507. *********************************************************/
  508. static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
  509. struct rtl_tcb_desc *tcb_desc,
  510. struct ieee80211_tx_info *info)
  511. {
  512. struct rtl_priv *rtlpriv = rtl_priv(hw);
  513. u8 rate_flag = info->control.rates[0].flags;
  514. tcb_desc->use_shortpreamble = false;
  515. /* 1M can only use Long Preamble. 11B spec */
  516. if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
  517. return;
  518. else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  519. tcb_desc->use_shortpreamble = true;
  520. }
  521. static void _rtl_query_shortgi(struct ieee80211_hw *hw,
  522. struct ieee80211_sta *sta,
  523. struct rtl_tcb_desc *tcb_desc,
  524. struct ieee80211_tx_info *info)
  525. {
  526. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  527. u8 rate_flag = info->control.rates[0].flags;
  528. u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
  529. u8 sgi_80 = 0, bw_80 = 0;
  530. tcb_desc->use_shortgi = false;
  531. if (!sta)
  532. return;
  533. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  534. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  535. sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
  536. if (!sta->ht_cap.ht_supported && !sta->vht_cap.vht_supported)
  537. return;
  538. if (!sgi_40 && !sgi_20)
  539. return;
  540. if (mac->opmode == NL80211_IFTYPE_STATION) {
  541. bw_40 = mac->bw_40;
  542. bw_80 = mac->bw_80;
  543. } else if (mac->opmode == NL80211_IFTYPE_AP ||
  544. mac->opmode == NL80211_IFTYPE_ADHOC ||
  545. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  546. bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  547. bw_80 = sta->vht_cap.vht_supported;
  548. }
  549. if (bw_80) {
  550. if (sgi_80)
  551. tcb_desc->use_shortgi = true;
  552. else
  553. tcb_desc->use_shortgi = false;
  554. } else {
  555. if (bw_40 && sgi_40)
  556. tcb_desc->use_shortgi = true;
  557. else if (!bw_40 && sgi_20)
  558. tcb_desc->use_shortgi = true;
  559. }
  560. if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
  561. tcb_desc->use_shortgi = false;
  562. }
  563. static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
  564. struct rtl_tcb_desc *tcb_desc,
  565. struct ieee80211_tx_info *info)
  566. {
  567. struct rtl_priv *rtlpriv = rtl_priv(hw);
  568. u8 rate_flag = info->control.rates[0].flags;
  569. /* Common Settings */
  570. tcb_desc->rts_stbc = false;
  571. tcb_desc->cts_enable = false;
  572. tcb_desc->rts_sc = 0;
  573. tcb_desc->rts_bw = false;
  574. tcb_desc->rts_use_shortpreamble = false;
  575. tcb_desc->rts_use_shortgi = false;
  576. if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  577. /* Use CTS-to-SELF in protection mode. */
  578. tcb_desc->rts_enable = true;
  579. tcb_desc->cts_enable = true;
  580. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  581. } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
  582. /* Use RTS-CTS in protection mode. */
  583. tcb_desc->rts_enable = true;
  584. tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
  585. }
  586. }
  587. u8 rtl_mrate_idx_to_arfr_id(struct ieee80211_hw *hw, u8 rate_index,
  588. enum wireless_mode wirelessmode)
  589. {
  590. struct rtl_priv *rtlpriv = rtl_priv(hw);
  591. struct rtl_phy *rtlphy = &rtlpriv->phy;
  592. u8 ret = 0;
  593. switch (rate_index) {
  594. case RATR_INX_WIRELESS_NGB:
  595. if (rtlphy->rf_type == RF_1T1R)
  596. ret = RATEID_IDX_BGN_40M_1SS;
  597. else
  598. ret = RATEID_IDX_BGN_40M_2SS;
  599. break;
  600. case RATR_INX_WIRELESS_N:
  601. case RATR_INX_WIRELESS_NG:
  602. if (rtlphy->rf_type == RF_1T1R)
  603. ret = RATEID_IDX_GN_N1SS;
  604. else
  605. ret = RATEID_IDX_GN_N2SS;
  606. break;
  607. case RATR_INX_WIRELESS_NB:
  608. if (rtlphy->rf_type == RF_1T1R)
  609. ret = RATEID_IDX_BGN_20M_1SS_BN;
  610. else
  611. ret = RATEID_IDX_BGN_20M_2SS_BN;
  612. break;
  613. case RATR_INX_WIRELESS_GB:
  614. ret = RATEID_IDX_BG;
  615. break;
  616. case RATR_INX_WIRELESS_G:
  617. ret = RATEID_IDX_G;
  618. break;
  619. case RATR_INX_WIRELESS_B:
  620. ret = RATEID_IDX_B;
  621. break;
  622. case RATR_INX_WIRELESS_MC:
  623. if (wirelessmode == WIRELESS_MODE_B ||
  624. wirelessmode == WIRELESS_MODE_G ||
  625. wirelessmode == WIRELESS_MODE_N_24G ||
  626. wirelessmode == WIRELESS_MODE_AC_24G)
  627. ret = RATEID_IDX_BG;
  628. else
  629. ret = RATEID_IDX_G;
  630. break;
  631. case RATR_INX_WIRELESS_AC_5N:
  632. if (rtlphy->rf_type == RF_1T1R)
  633. ret = RATEID_IDX_VHT_1SS;
  634. else
  635. ret = RATEID_IDX_VHT_2SS;
  636. break;
  637. case RATR_INX_WIRELESS_AC_24N:
  638. if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
  639. if (rtlphy->rf_type == RF_1T1R)
  640. ret = RATEID_IDX_VHT_1SS;
  641. else
  642. ret = RATEID_IDX_VHT_2SS;
  643. } else {
  644. if (rtlphy->rf_type == RF_1T1R)
  645. ret = RATEID_IDX_MIX1;
  646. else
  647. ret = RATEID_IDX_MIX2;
  648. }
  649. break;
  650. default:
  651. ret = RATEID_IDX_BGN_40M_2SS;
  652. break;
  653. }
  654. return ret;
  655. }
  656. static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
  657. struct ieee80211_sta *sta,
  658. struct rtl_tcb_desc *tcb_desc)
  659. {
  660. #define SET_RATE_ID(rate_id) \
  661. ((rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) ? \
  662. rtl_mrate_idx_to_arfr_id(hw, rate_id, \
  663. (sta_entry ? sta_entry->wireless_mode : \
  664. WIRELESS_MODE_G)) : \
  665. rate_id)
  666. struct rtl_priv *rtlpriv = rtl_priv(hw);
  667. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  668. struct rtl_sta_info *sta_entry = NULL;
  669. u8 ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
  670. if (sta) {
  671. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  672. ratr_index = sta_entry->ratr_index;
  673. }
  674. if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
  675. if (mac->opmode == NL80211_IFTYPE_STATION) {
  676. tcb_desc->ratr_index = 0;
  677. } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
  678. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  679. if (tcb_desc->multicast || tcb_desc->broadcast) {
  680. tcb_desc->hw_rate =
  681. rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
  682. tcb_desc->use_driver_rate = 1;
  683. tcb_desc->ratr_index =
  684. SET_RATE_ID(RATR_INX_WIRELESS_MC);
  685. } else {
  686. tcb_desc->ratr_index = ratr_index;
  687. }
  688. } else if (mac->opmode == NL80211_IFTYPE_AP) {
  689. tcb_desc->ratr_index = ratr_index;
  690. }
  691. }
  692. if (rtlpriv->dm.useramask) {
  693. tcb_desc->ratr_index = ratr_index;
  694. /* TODO we will differentiate adhoc and station future */
  695. if (mac->opmode == NL80211_IFTYPE_STATION ||
  696. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  697. tcb_desc->mac_id = 0;
  698. if (sta &&
  699. (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID))
  700. ; /* use sta_entry->ratr_index */
  701. else if (mac->mode == WIRELESS_MODE_AC_5G)
  702. tcb_desc->ratr_index =
  703. SET_RATE_ID(RATR_INX_WIRELESS_AC_5N);
  704. else if (mac->mode == WIRELESS_MODE_AC_24G)
  705. tcb_desc->ratr_index =
  706. SET_RATE_ID(RATR_INX_WIRELESS_AC_24N);
  707. else if (mac->mode == WIRELESS_MODE_N_24G)
  708. tcb_desc->ratr_index =
  709. SET_RATE_ID(RATR_INX_WIRELESS_NGB);
  710. else if (mac->mode == WIRELESS_MODE_N_5G)
  711. tcb_desc->ratr_index =
  712. SET_RATE_ID(RATR_INX_WIRELESS_NG);
  713. else if (mac->mode & WIRELESS_MODE_G)
  714. tcb_desc->ratr_index =
  715. SET_RATE_ID(RATR_INX_WIRELESS_GB);
  716. else if (mac->mode & WIRELESS_MODE_B)
  717. tcb_desc->ratr_index =
  718. SET_RATE_ID(RATR_INX_WIRELESS_B);
  719. else if (mac->mode & WIRELESS_MODE_A)
  720. tcb_desc->ratr_index =
  721. SET_RATE_ID(RATR_INX_WIRELESS_G);
  722. } else if (mac->opmode == NL80211_IFTYPE_AP ||
  723. mac->opmode == NL80211_IFTYPE_ADHOC) {
  724. if (sta) {
  725. if (sta->aid > 0)
  726. tcb_desc->mac_id = sta->aid + 1;
  727. else
  728. tcb_desc->mac_id = 1;
  729. } else {
  730. tcb_desc->mac_id = 0;
  731. }
  732. }
  733. }
  734. #undef SET_RATE_ID
  735. }
  736. static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
  737. struct ieee80211_sta *sta,
  738. struct rtl_tcb_desc *tcb_desc)
  739. {
  740. struct rtl_priv *rtlpriv = rtl_priv(hw);
  741. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  742. tcb_desc->packet_bw = false;
  743. if (!sta)
  744. return;
  745. if (mac->opmode == NL80211_IFTYPE_AP ||
  746. mac->opmode == NL80211_IFTYPE_ADHOC ||
  747. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  748. if (!(sta->ht_cap.ht_supported) ||
  749. !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  750. return;
  751. } else if (mac->opmode == NL80211_IFTYPE_STATION) {
  752. if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
  753. return;
  754. }
  755. if (tcb_desc->multicast || tcb_desc->broadcast)
  756. return;
  757. /*use legency rate, shall use 20MHz */
  758. if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
  759. return;
  760. tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
  761. if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
  762. rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8821AE ||
  763. (rtlpriv->cfg->spec_ver & RTL_SPEC_SUPPORT_VHT)) {
  764. if (mac->opmode == NL80211_IFTYPE_AP ||
  765. mac->opmode == NL80211_IFTYPE_ADHOC ||
  766. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  767. if (!(sta->vht_cap.vht_supported))
  768. return;
  769. } else if (mac->opmode == NL80211_IFTYPE_STATION) {
  770. if (!mac->bw_80 ||
  771. !(sta->vht_cap.vht_supported))
  772. return;
  773. }
  774. if (tcb_desc->hw_rate <=
  775. rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
  776. return;
  777. tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
  778. }
  779. }
  780. static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
  781. struct ieee80211_sta *sta)
  782. {
  783. struct rtl_priv *rtlpriv = rtl_priv(hw);
  784. struct rtl_phy *rtlphy = &rtlpriv->phy;
  785. u8 hw_rate;
  786. u16 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
  787. if ((get_rf_type(rtlphy) == RF_2T2R) &&
  788. (tx_mcs_map & 0x000c) != 0x000c) {
  789. if ((tx_mcs_map & 0x000c) >> 2 ==
  790. IEEE80211_VHT_MCS_SUPPORT_0_7)
  791. hw_rate =
  792. rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
  793. else if ((tx_mcs_map & 0x000c) >> 2 ==
  794. IEEE80211_VHT_MCS_SUPPORT_0_8)
  795. hw_rate =
  796. rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS8];
  797. else
  798. hw_rate =
  799. rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
  800. } else {
  801. if ((tx_mcs_map & 0x0003) ==
  802. IEEE80211_VHT_MCS_SUPPORT_0_7)
  803. hw_rate =
  804. rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
  805. else if ((tx_mcs_map & 0x0003) ==
  806. IEEE80211_VHT_MCS_SUPPORT_0_8)
  807. hw_rate =
  808. rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS8];
  809. else
  810. hw_rate =
  811. rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
  812. }
  813. return hw_rate;
  814. }
  815. static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
  816. struct ieee80211_sta *sta)
  817. {
  818. struct rtl_priv *rtlpriv = rtl_priv(hw);
  819. struct rtl_phy *rtlphy = &rtlpriv->phy;
  820. u8 hw_rate;
  821. if (get_rf_type(rtlphy) == RF_2T2R &&
  822. sta->ht_cap.mcs.rx_mask[1] != 0)
  823. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
  824. else
  825. hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
  826. return hw_rate;
  827. }
  828. /* mac80211's rate_idx is like this:
  829. *
  830. * 2.4G band:rx_status->band == NL80211_BAND_2GHZ
  831. *
  832. * B/G rate:
  833. * (rx_status->flag & RX_FLAG_HT) = 0,
  834. * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
  835. *
  836. * N rate:
  837. * (rx_status->flag & RX_FLAG_HT) = 1,
  838. * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
  839. *
  840. * 5G band:rx_status->band == NL80211_BAND_5GHZ
  841. * A rate:
  842. * (rx_status->flag & RX_FLAG_HT) = 0,
  843. * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
  844. *
  845. * N rate:
  846. * (rx_status->flag & RX_FLAG_HT) = 1,
  847. * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
  848. *
  849. * VHT rates:
  850. * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
  851. * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
  852. */
  853. int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
  854. u8 desc_rate)
  855. {
  856. int rate_idx;
  857. if (isvht) {
  858. switch (desc_rate) {
  859. case DESC_RATEVHT1SS_MCS0:
  860. rate_idx = 0;
  861. break;
  862. case DESC_RATEVHT1SS_MCS1:
  863. rate_idx = 1;
  864. break;
  865. case DESC_RATEVHT1SS_MCS2:
  866. rate_idx = 2;
  867. break;
  868. case DESC_RATEVHT1SS_MCS3:
  869. rate_idx = 3;
  870. break;
  871. case DESC_RATEVHT1SS_MCS4:
  872. rate_idx = 4;
  873. break;
  874. case DESC_RATEVHT1SS_MCS5:
  875. rate_idx = 5;
  876. break;
  877. case DESC_RATEVHT1SS_MCS6:
  878. rate_idx = 6;
  879. break;
  880. case DESC_RATEVHT1SS_MCS7:
  881. rate_idx = 7;
  882. break;
  883. case DESC_RATEVHT1SS_MCS8:
  884. rate_idx = 8;
  885. break;
  886. case DESC_RATEVHT1SS_MCS9:
  887. rate_idx = 9;
  888. break;
  889. case DESC_RATEVHT2SS_MCS0:
  890. rate_idx = 0;
  891. break;
  892. case DESC_RATEVHT2SS_MCS1:
  893. rate_idx = 1;
  894. break;
  895. case DESC_RATEVHT2SS_MCS2:
  896. rate_idx = 2;
  897. break;
  898. case DESC_RATEVHT2SS_MCS3:
  899. rate_idx = 3;
  900. break;
  901. case DESC_RATEVHT2SS_MCS4:
  902. rate_idx = 4;
  903. break;
  904. case DESC_RATEVHT2SS_MCS5:
  905. rate_idx = 5;
  906. break;
  907. case DESC_RATEVHT2SS_MCS6:
  908. rate_idx = 6;
  909. break;
  910. case DESC_RATEVHT2SS_MCS7:
  911. rate_idx = 7;
  912. break;
  913. case DESC_RATEVHT2SS_MCS8:
  914. rate_idx = 8;
  915. break;
  916. case DESC_RATEVHT2SS_MCS9:
  917. rate_idx = 9;
  918. break;
  919. default:
  920. rate_idx = 0;
  921. break;
  922. }
  923. return rate_idx;
  924. }
  925. if (!isht) {
  926. if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ) {
  927. switch (desc_rate) {
  928. case DESC_RATE1M:
  929. rate_idx = 0;
  930. break;
  931. case DESC_RATE2M:
  932. rate_idx = 1;
  933. break;
  934. case DESC_RATE5_5M:
  935. rate_idx = 2;
  936. break;
  937. case DESC_RATE11M:
  938. rate_idx = 3;
  939. break;
  940. case DESC_RATE6M:
  941. rate_idx = 4;
  942. break;
  943. case DESC_RATE9M:
  944. rate_idx = 5;
  945. break;
  946. case DESC_RATE12M:
  947. rate_idx = 6;
  948. break;
  949. case DESC_RATE18M:
  950. rate_idx = 7;
  951. break;
  952. case DESC_RATE24M:
  953. rate_idx = 8;
  954. break;
  955. case DESC_RATE36M:
  956. rate_idx = 9;
  957. break;
  958. case DESC_RATE48M:
  959. rate_idx = 10;
  960. break;
  961. case DESC_RATE54M:
  962. rate_idx = 11;
  963. break;
  964. default:
  965. rate_idx = 0;
  966. break;
  967. }
  968. } else {
  969. switch (desc_rate) {
  970. case DESC_RATE6M:
  971. rate_idx = 0;
  972. break;
  973. case DESC_RATE9M:
  974. rate_idx = 1;
  975. break;
  976. case DESC_RATE12M:
  977. rate_idx = 2;
  978. break;
  979. case DESC_RATE18M:
  980. rate_idx = 3;
  981. break;
  982. case DESC_RATE24M:
  983. rate_idx = 4;
  984. break;
  985. case DESC_RATE36M:
  986. rate_idx = 5;
  987. break;
  988. case DESC_RATE48M:
  989. rate_idx = 6;
  990. break;
  991. case DESC_RATE54M:
  992. rate_idx = 7;
  993. break;
  994. default:
  995. rate_idx = 0;
  996. break;
  997. }
  998. }
  999. } else {
  1000. switch (desc_rate) {
  1001. case DESC_RATEMCS0:
  1002. rate_idx = 0;
  1003. break;
  1004. case DESC_RATEMCS1:
  1005. rate_idx = 1;
  1006. break;
  1007. case DESC_RATEMCS2:
  1008. rate_idx = 2;
  1009. break;
  1010. case DESC_RATEMCS3:
  1011. rate_idx = 3;
  1012. break;
  1013. case DESC_RATEMCS4:
  1014. rate_idx = 4;
  1015. break;
  1016. case DESC_RATEMCS5:
  1017. rate_idx = 5;
  1018. break;
  1019. case DESC_RATEMCS6:
  1020. rate_idx = 6;
  1021. break;
  1022. case DESC_RATEMCS7:
  1023. rate_idx = 7;
  1024. break;
  1025. case DESC_RATEMCS8:
  1026. rate_idx = 8;
  1027. break;
  1028. case DESC_RATEMCS9:
  1029. rate_idx = 9;
  1030. break;
  1031. case DESC_RATEMCS10:
  1032. rate_idx = 10;
  1033. break;
  1034. case DESC_RATEMCS11:
  1035. rate_idx = 11;
  1036. break;
  1037. case DESC_RATEMCS12:
  1038. rate_idx = 12;
  1039. break;
  1040. case DESC_RATEMCS13:
  1041. rate_idx = 13;
  1042. break;
  1043. case DESC_RATEMCS14:
  1044. rate_idx = 14;
  1045. break;
  1046. case DESC_RATEMCS15:
  1047. rate_idx = 15;
  1048. break;
  1049. default:
  1050. rate_idx = 0;
  1051. break;
  1052. }
  1053. }
  1054. return rate_idx;
  1055. }
  1056. static u8 _rtl_get_tx_hw_rate(struct ieee80211_hw *hw,
  1057. struct ieee80211_tx_info *info)
  1058. {
  1059. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1060. struct ieee80211_tx_rate *r = &info->status.rates[0];
  1061. struct ieee80211_rate *txrate;
  1062. u8 hw_value = 0x0;
  1063. if (r->flags & IEEE80211_TX_RC_MCS) {
  1064. /* HT MCS0-15 */
  1065. hw_value = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15] - 15 +
  1066. r->idx;
  1067. } else if (r->flags & IEEE80211_TX_RC_VHT_MCS) {
  1068. /* VHT MCS0-9, NSS */
  1069. if (ieee80211_rate_get_vht_nss(r) == 2)
  1070. hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
  1071. else
  1072. hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
  1073. hw_value = hw_value - 9 + ieee80211_rate_get_vht_mcs(r);
  1074. } else {
  1075. /* legacy */
  1076. txrate = ieee80211_get_tx_rate(hw, info);
  1077. if (txrate)
  1078. hw_value = txrate->hw_value;
  1079. }
  1080. /* check 5G band */
  1081. if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G &&
  1082. hw_value < rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M])
  1083. hw_value = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M];
  1084. return hw_value;
  1085. }
  1086. void rtl_get_tcb_desc(struct ieee80211_hw *hw,
  1087. struct ieee80211_tx_info *info,
  1088. struct ieee80211_sta *sta,
  1089. struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
  1090. {
  1091. #define SET_RATE_ID(rate_id) \
  1092. ((rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) ? \
  1093. rtl_mrate_idx_to_arfr_id(hw, rate_id, \
  1094. (sta_entry ? sta_entry->wireless_mode : \
  1095. WIRELESS_MODE_G)) : \
  1096. rate_id)
  1097. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1098. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  1099. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  1100. struct rtl_sta_info *sta_entry =
  1101. (sta ? (struct rtl_sta_info *)sta->drv_priv : NULL);
  1102. __le16 fc = rtl_get_fc(skb);
  1103. tcb_desc->hw_rate = _rtl_get_tx_hw_rate(hw, info);
  1104. if (rtl_is_tx_report_skb(hw, skb))
  1105. tcb_desc->use_spe_rpt = 1;
  1106. if (!ieee80211_is_data(fc)) {
  1107. tcb_desc->use_driver_rate = true;
  1108. tcb_desc->ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
  1109. tcb_desc->disable_ratefallback = 1;
  1110. tcb_desc->mac_id = 0;
  1111. tcb_desc->packet_bw = false;
  1112. return;
  1113. }
  1114. /*
  1115. * We set data rate INX 0
  1116. * in rtl_rc.c if skb is special data or
  1117. * mgt which need low data rate.
  1118. */
  1119. /*
  1120. * So tcb_desc->hw_rate is just used for
  1121. * special data and mgt frames
  1122. */
  1123. if (info->control.rates[0].idx == 0 || ieee80211_is_nullfunc(fc)) {
  1124. tcb_desc->use_driver_rate = true;
  1125. tcb_desc->ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
  1126. tcb_desc->disable_ratefallback = 1;
  1127. } else if (sta && sta->vht_cap.vht_supported) {
  1128. /*
  1129. * Because hw will never use hw_rate
  1130. * when tcb_desc->use_driver_rate = false
  1131. * so we never set highest N rate here,
  1132. * and N rate will all be controlled by FW
  1133. * when tcb_desc->use_driver_rate = false
  1134. */
  1135. tcb_desc->hw_rate = _rtl_get_vht_highest_n_rate(hw, sta);
  1136. } else if (sta && sta->ht_cap.ht_supported) {
  1137. tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw, sta);
  1138. } else {
  1139. enum rtl_var_map var = RTL_RC_OFDM_RATE54M;
  1140. if (rtlmac->mode == WIRELESS_MODE_B)
  1141. var = RTL_RC_CCK_RATE11M;
  1142. tcb_desc->hw_rate = rtlpriv->cfg->maps[var];
  1143. }
  1144. if (is_multicast_ether_addr(hdr->addr1))
  1145. tcb_desc->multicast = 1;
  1146. else if (is_broadcast_ether_addr(hdr->addr1))
  1147. tcb_desc->broadcast = 1;
  1148. _rtl_txrate_selectmode(hw, sta, tcb_desc);
  1149. _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
  1150. _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
  1151. _rtl_query_shortgi(hw, sta, tcb_desc, info);
  1152. _rtl_query_protection_mode(hw, tcb_desc, info);
  1153. #undef SET_RATE_ID
  1154. }
  1155. bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
  1156. {
  1157. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1158. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1159. __le16 fc = rtl_get_fc(skb);
  1160. if (rtlpriv->dm.supp_phymode_switch &&
  1161. mac->link_state < MAC80211_LINKED &&
  1162. (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
  1163. if (rtlpriv->cfg->ops->chk_switch_dmdp)
  1164. rtlpriv->cfg->ops->chk_switch_dmdp(hw);
  1165. }
  1166. if (ieee80211_is_auth(fc)) {
  1167. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
  1168. mac->link_state = MAC80211_LINKING;
  1169. /* Dul mac */
  1170. rtlpriv->phy.need_iqk = true;
  1171. }
  1172. return true;
  1173. }
  1174. struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
  1175. u8 *bssid, u16 tid);
  1176. static void process_agg_start(struct ieee80211_hw *hw,
  1177. struct ieee80211_hdr *hdr, u16 tid)
  1178. {
  1179. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1180. struct ieee80211_rx_status rx_status = { 0 };
  1181. struct sk_buff *skb_delba = NULL;
  1182. skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
  1183. if (skb_delba) {
  1184. rx_status.freq = hw->conf.chandef.chan->center_freq;
  1185. rx_status.band = hw->conf.chandef.chan->band;
  1186. rx_status.flag |= RX_FLAG_DECRYPTED;
  1187. rx_status.flag |= RX_FLAG_MACTIME_START;
  1188. rx_status.rate_idx = 0;
  1189. rx_status.signal = 50 + 10;
  1190. memcpy(IEEE80211_SKB_RXCB(skb_delba),
  1191. &rx_status, sizeof(rx_status));
  1192. RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
  1193. "fake del\n",
  1194. skb_delba->data,
  1195. skb_delba->len);
  1196. ieee80211_rx_irqsafe(hw, skb_delba);
  1197. }
  1198. }
  1199. bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
  1200. {
  1201. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1202. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  1203. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1204. __le16 fc = rtl_get_fc(skb);
  1205. u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
  1206. u8 category;
  1207. if (!ieee80211_is_action(fc))
  1208. return true;
  1209. category = *act;
  1210. act++;
  1211. switch (category) {
  1212. case ACT_CAT_BA:
  1213. switch (*act) {
  1214. case ACT_ADDBAREQ:
  1215. if (mac->act_scanning)
  1216. return false;
  1217. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  1218. "%s ACT_ADDBAREQ From :%pM\n",
  1219. is_tx ? "Tx" : "Rx", hdr->addr2);
  1220. RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
  1221. skb->data, skb->len);
  1222. if (!is_tx) {
  1223. struct ieee80211_sta *sta = NULL;
  1224. struct rtl_sta_info *sta_entry = NULL;
  1225. struct rtl_tid_data *tid_data;
  1226. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1227. u16 capab = 0, tid = 0;
  1228. rcu_read_lock();
  1229. sta = rtl_find_sta(hw, hdr->addr3);
  1230. if (!sta) {
  1231. RT_TRACE(rtlpriv, COMP_SEND | COMP_RECV,
  1232. DBG_DMESG, "sta is NULL\n");
  1233. rcu_read_unlock();
  1234. return true;
  1235. }
  1236. sta_entry =
  1237. (struct rtl_sta_info *)sta->drv_priv;
  1238. if (!sta_entry) {
  1239. rcu_read_unlock();
  1240. return true;
  1241. }
  1242. capab =
  1243. le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  1244. tid = (capab &
  1245. IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  1246. if (tid >= MAX_TID_COUNT) {
  1247. rcu_read_unlock();
  1248. return true;
  1249. }
  1250. tid_data = &sta_entry->tids[tid];
  1251. if (tid_data->agg.rx_agg_state ==
  1252. RTL_RX_AGG_START)
  1253. process_agg_start(hw, hdr, tid);
  1254. rcu_read_unlock();
  1255. }
  1256. break;
  1257. case ACT_ADDBARSP:
  1258. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  1259. "%s ACT_ADDBARSP From :%pM\n",
  1260. is_tx ? "Tx" : "Rx", hdr->addr2);
  1261. break;
  1262. case ACT_DELBA:
  1263. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  1264. "ACT_ADDBADEL From :%pM\n", hdr->addr2);
  1265. break;
  1266. }
  1267. break;
  1268. default:
  1269. break;
  1270. }
  1271. return true;
  1272. }
  1273. static void setup_special_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc,
  1274. int type)
  1275. {
  1276. struct ieee80211_hw *hw = rtlpriv->hw;
  1277. rtlpriv->ra.is_special_data = true;
  1278. if (rtlpriv->cfg->ops->get_btc_status())
  1279. rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
  1280. rtlpriv, type);
  1281. rtl_lps_leave(hw);
  1282. ppsc->last_delaylps_stamp_jiffies = jiffies;
  1283. }
  1284. static const u8 *rtl_skb_ether_type_ptr(struct ieee80211_hw *hw,
  1285. struct sk_buff *skb, bool is_enc)
  1286. {
  1287. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1288. u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  1289. u8 encrypt_header_len = 0;
  1290. u8 offset;
  1291. switch (rtlpriv->sec.pairwise_enc_algorithm) {
  1292. case WEP40_ENCRYPTION:
  1293. case WEP104_ENCRYPTION:
  1294. encrypt_header_len = 4;/*WEP_IV_LEN*/
  1295. break;
  1296. case TKIP_ENCRYPTION:
  1297. encrypt_header_len = 8;/*TKIP_IV_LEN*/
  1298. break;
  1299. case AESCCMP_ENCRYPTION:
  1300. encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
  1301. break;
  1302. default:
  1303. break;
  1304. }
  1305. offset = mac_hdr_len + SNAP_SIZE;
  1306. if (is_enc)
  1307. offset += encrypt_header_len;
  1308. return skb->data + offset;
  1309. }
  1310. /*should call before software enc*/
  1311. u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
  1312. bool is_enc)
  1313. {
  1314. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1315. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1316. __le16 fc = rtl_get_fc(skb);
  1317. u16 ether_type;
  1318. const u8 *ether_type_ptr;
  1319. const struct iphdr *ip;
  1320. if (!ieee80211_is_data(fc))
  1321. goto end;
  1322. ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, is_enc);
  1323. ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
  1324. if (ether_type == ETH_P_IP) {
  1325. ip = (struct iphdr *)((u8 *)ether_type_ptr +
  1326. PROTOC_TYPE_SIZE);
  1327. if (ip->protocol == IPPROTO_UDP) {
  1328. struct udphdr *udp = (struct udphdr *)((u8 *)ip +
  1329. (ip->ihl << 2));
  1330. if (((((u8 *)udp)[1] == 68) &&
  1331. (((u8 *)udp)[3] == 67)) ||
  1332. ((((u8 *)udp)[1] == 67) &&
  1333. (((u8 *)udp)[3] == 68))) {
  1334. /* 68 : UDP BOOTP client
  1335. * 67 : UDP BOOTP server
  1336. */
  1337. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
  1338. DBG_DMESG, "dhcp %s !!\n",
  1339. (is_tx) ? "Tx" : "Rx");
  1340. if (is_tx)
  1341. setup_special_tx(rtlpriv, ppsc,
  1342. PACKET_DHCP);
  1343. return true;
  1344. }
  1345. }
  1346. } else if (ether_type == ETH_P_ARP) {
  1347. if (is_tx)
  1348. setup_special_tx(rtlpriv, ppsc, PACKET_ARP);
  1349. return true;
  1350. } else if (ether_type == ETH_P_PAE) {
  1351. /* EAPOL is seen as in-4way */
  1352. rtlpriv->btcoexist.btc_info.in_4way = true;
  1353. rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
  1354. rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
  1355. RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
  1356. "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
  1357. if (is_tx) {
  1358. rtlpriv->ra.is_special_data = true;
  1359. rtl_lps_leave(hw);
  1360. ppsc->last_delaylps_stamp_jiffies = jiffies;
  1361. setup_special_tx(rtlpriv, ppsc, PACKET_EAPOL);
  1362. }
  1363. return true;
  1364. } else if (ether_type == ETH_P_IPV6) {
  1365. /* TODO: Handle any IPv6 cases that need special handling.
  1366. * For now, always return false
  1367. */
  1368. goto end;
  1369. }
  1370. end:
  1371. rtlpriv->ra.is_special_data = false;
  1372. return false;
  1373. }
  1374. bool rtl_is_tx_report_skb(struct ieee80211_hw *hw, struct sk_buff *skb)
  1375. {
  1376. u16 ether_type;
  1377. const u8 *ether_type_ptr;
  1378. ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, true);
  1379. ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
  1380. /* EAPOL */
  1381. if (ether_type == ETH_P_PAE)
  1382. return true;
  1383. return false;
  1384. }
  1385. static u16 rtl_get_tx_report_sn(struct ieee80211_hw *hw)
  1386. {
  1387. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1388. struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
  1389. u16 sn;
  1390. /*
  1391. * SW_DEFINE[11:8] are reserved (driver fills zeros)
  1392. * SW_DEFINE[7:2] are used by driver
  1393. * SW_DEFINE[1:0] are reserved for firmware (driver fills zeros)
  1394. */
  1395. sn = (atomic_inc_return(&tx_report->sn) & 0x003F) << 2;
  1396. tx_report->last_sent_sn = sn;
  1397. tx_report->last_sent_time = jiffies;
  1398. RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
  1399. "Send TX-Report sn=0x%X\n", sn);
  1400. return sn;
  1401. }
  1402. void rtl_get_tx_report(struct rtl_tcb_desc *ptcb_desc, u8 *pdesc,
  1403. struct ieee80211_hw *hw)
  1404. {
  1405. if (ptcb_desc->use_spe_rpt) {
  1406. u16 sn = rtl_get_tx_report_sn(hw);
  1407. SET_TX_DESC_SPE_RPT(pdesc, 1);
  1408. SET_TX_DESC_SW_DEFINE(pdesc, sn);
  1409. }
  1410. }
  1411. void rtl_tx_report_handler(struct ieee80211_hw *hw, u8 *tmp_buf, u8 c2h_cmd_len)
  1412. {
  1413. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1414. struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
  1415. u16 sn;
  1416. u8 st, retry;
  1417. if (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_FW_C2H) {
  1418. sn = tmp_buf[6];
  1419. st = tmp_buf[7] & 0xC0;
  1420. retry = tmp_buf[8] & 0x3F;
  1421. } else {
  1422. sn = ((tmp_buf[7] & 0x0F) << 8) | tmp_buf[6];
  1423. st = tmp_buf[0] & 0xC0;
  1424. retry = tmp_buf[2] & 0x3F;
  1425. }
  1426. tx_report->last_recv_sn = sn;
  1427. RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
  1428. "Recv TX-Report st=0x%02X sn=0x%X retry=0x%X\n",
  1429. st, sn, retry);
  1430. }
  1431. bool rtl_check_tx_report_acked(struct ieee80211_hw *hw)
  1432. {
  1433. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1434. struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
  1435. if (tx_report->last_sent_sn == tx_report->last_recv_sn)
  1436. return true;
  1437. if (time_before(tx_report->last_sent_time + 3 * HZ, jiffies)) {
  1438. RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_WARNING,
  1439. "Check TX-Report timeout!! s_sn=0x%X r_sn=0x%X\n",
  1440. tx_report->last_sent_sn, tx_report->last_recv_sn);
  1441. return true; /* 3 sec. (timeout) seen as acked */
  1442. }
  1443. return false;
  1444. }
  1445. void rtl_wait_tx_report_acked(struct ieee80211_hw *hw, u32 wait_ms)
  1446. {
  1447. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1448. int i;
  1449. for (i = 0; i < wait_ms; i++) {
  1450. if (rtl_check_tx_report_acked(hw))
  1451. break;
  1452. usleep_range(1000, 2000);
  1453. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  1454. "Wait 1ms (%d/%d) to disable key.\n", i, wait_ms);
  1455. }
  1456. }
  1457. u32 rtl_get_hal_edca_param(struct ieee80211_hw *hw,
  1458. struct ieee80211_vif *vif,
  1459. enum wireless_mode wirelessmode,
  1460. struct ieee80211_tx_queue_params *param)
  1461. {
  1462. u32 reg = 0;
  1463. u8 sifstime = 10;
  1464. u8 slottime = 20;
  1465. /* AIFS = AIFSN * slot time + SIFS */
  1466. switch (wirelessmode) {
  1467. case WIRELESS_MODE_A:
  1468. case WIRELESS_MODE_N_24G:
  1469. case WIRELESS_MODE_N_5G:
  1470. case WIRELESS_MODE_AC_5G:
  1471. case WIRELESS_MODE_AC_24G:
  1472. sifstime = 16;
  1473. slottime = 9;
  1474. break;
  1475. case WIRELESS_MODE_G:
  1476. slottime = (vif->bss_conf.use_short_slot ? 9 : 20);
  1477. break;
  1478. default:
  1479. break;
  1480. }
  1481. reg |= (param->txop & 0x7FF) << 16;
  1482. reg |= (fls(param->cw_max) & 0xF) << 12;
  1483. reg |= (fls(param->cw_min) & 0xF) << 8;
  1484. reg |= (param->aifs & 0x0F) * slottime + sifstime;
  1485. return reg;
  1486. }
  1487. /*********************************************************
  1488. *
  1489. * functions called by core.c
  1490. *
  1491. *********************************************************/
  1492. int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1493. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  1494. {
  1495. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1496. struct rtl_tid_data *tid_data;
  1497. struct rtl_sta_info *sta_entry = NULL;
  1498. if (!sta)
  1499. return -EINVAL;
  1500. if (unlikely(tid >= MAX_TID_COUNT))
  1501. return -EINVAL;
  1502. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1503. if (!sta_entry)
  1504. return -ENXIO;
  1505. tid_data = &sta_entry->tids[tid];
  1506. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  1507. "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
  1508. tid_data->seq_number);
  1509. *ssn = tid_data->seq_number;
  1510. tid_data->agg.agg_state = RTL_AGG_START;
  1511. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1512. return 0;
  1513. }
  1514. int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1515. struct ieee80211_sta *sta, u16 tid)
  1516. {
  1517. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1518. struct rtl_tid_data *tid_data;
  1519. struct rtl_sta_info *sta_entry = NULL;
  1520. if (!sta)
  1521. return -EINVAL;
  1522. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  1523. "on ra = %pM tid = %d\n", sta->addr, tid);
  1524. if (unlikely(tid >= MAX_TID_COUNT))
  1525. return -EINVAL;
  1526. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1527. tid_data = &sta_entry->tids[tid];
  1528. sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
  1529. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1530. return 0;
  1531. }
  1532. int rtl_rx_agg_start(struct ieee80211_hw *hw,
  1533. struct ieee80211_sta *sta, u16 tid)
  1534. {
  1535. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1536. struct rtl_tid_data *tid_data;
  1537. struct rtl_sta_info *sta_entry = NULL;
  1538. u8 reject_agg;
  1539. if (!sta)
  1540. return -EINVAL;
  1541. if (unlikely(tid >= MAX_TID_COUNT))
  1542. return -EINVAL;
  1543. if (rtlpriv->cfg->ops->get_btc_status()) {
  1544. rtlpriv->btcoexist.btc_ops->btc_get_ampdu_cfg(rtlpriv,
  1545. &reject_agg,
  1546. NULL, NULL);
  1547. if (reject_agg)
  1548. return -EINVAL;
  1549. }
  1550. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1551. if (!sta_entry)
  1552. return -ENXIO;
  1553. tid_data = &sta_entry->tids[tid];
  1554. RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
  1555. "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
  1556. tid_data->seq_number);
  1557. tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
  1558. return 0;
  1559. }
  1560. int rtl_rx_agg_stop(struct ieee80211_hw *hw,
  1561. struct ieee80211_sta *sta, u16 tid)
  1562. {
  1563. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1564. struct rtl_sta_info *sta_entry = NULL;
  1565. if (!sta)
  1566. return -EINVAL;
  1567. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  1568. "on ra = %pM tid = %d\n", sta->addr, tid);
  1569. if (unlikely(tid >= MAX_TID_COUNT))
  1570. return -EINVAL;
  1571. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1572. sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
  1573. return 0;
  1574. }
  1575. int rtl_tx_agg_oper(struct ieee80211_hw *hw,
  1576. struct ieee80211_sta *sta, u16 tid)
  1577. {
  1578. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1579. struct rtl_sta_info *sta_entry = NULL;
  1580. if (!sta)
  1581. return -EINVAL;
  1582. RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
  1583. "on ra = %pM tid = %d\n", sta->addr, tid);
  1584. if (unlikely(tid >= MAX_TID_COUNT))
  1585. return -EINVAL;
  1586. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  1587. sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
  1588. return 0;
  1589. }
  1590. void rtl_rx_ampdu_apply(struct rtl_priv *rtlpriv)
  1591. {
  1592. struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
  1593. u8 reject_agg = 0, ctrl_agg_size = 0, agg_size = 0;
  1594. if (rtlpriv->cfg->ops->get_btc_status())
  1595. btc_ops->btc_get_ampdu_cfg(rtlpriv, &reject_agg,
  1596. &ctrl_agg_size, &agg_size);
  1597. RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
  1598. "Set RX AMPDU: coex - reject=%d, ctrl_agg_size=%d, size=%d",
  1599. reject_agg, ctrl_agg_size, agg_size);
  1600. rtlpriv->hw->max_rx_aggregation_subframes =
  1601. (ctrl_agg_size ? agg_size : IEEE80211_MAX_AMPDU_BUF_HT);
  1602. }
  1603. /*********************************************************
  1604. *
  1605. * wq & timer callback functions
  1606. *
  1607. *********************************************************/
  1608. /* this function is used for roaming */
  1609. void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
  1610. {
  1611. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1612. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1613. if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
  1614. return;
  1615. if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
  1616. return;
  1617. /* check if this really is a beacon */
  1618. if (!ieee80211_is_beacon(hdr->frame_control) &&
  1619. !ieee80211_is_probe_resp(hdr->frame_control))
  1620. return;
  1621. /* min. beacon length + FCS_LEN */
  1622. if (skb->len <= 40 + FCS_LEN)
  1623. return;
  1624. /* and only beacons from the associated BSSID, please */
  1625. if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
  1626. return;
  1627. rtlpriv->link_info.bcn_rx_inperiod++;
  1628. }
  1629. static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw)
  1630. {
  1631. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1632. struct rtl_bssid_entry *entry, *next;
  1633. list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
  1634. list_del(&entry->list);
  1635. kfree(entry);
  1636. rtlpriv->scan_list.num--;
  1637. }
  1638. }
  1639. void rtl_scan_list_expire(struct ieee80211_hw *hw)
  1640. {
  1641. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1642. struct rtl_bssid_entry *entry, *next;
  1643. unsigned long flags;
  1644. spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
  1645. list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
  1646. /* 180 seconds */
  1647. if (jiffies_to_msecs(jiffies - entry->age) < 180000)
  1648. continue;
  1649. list_del(&entry->list);
  1650. rtlpriv->scan_list.num--;
  1651. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
  1652. "BSSID=%pM is expire in scan list (total=%d)\n",
  1653. entry->bssid, rtlpriv->scan_list.num);
  1654. kfree(entry);
  1655. }
  1656. spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
  1657. rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
  1658. }
  1659. void rtl_collect_scan_list(struct ieee80211_hw *hw, struct sk_buff *skb)
  1660. {
  1661. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1662. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1663. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1664. unsigned long flags;
  1665. struct rtl_bssid_entry *entry;
  1666. bool entry_found = false;
  1667. /* check if it is scanning */
  1668. if (!mac->act_scanning)
  1669. return;
  1670. /* check if this really is a beacon */
  1671. if (!ieee80211_is_beacon(hdr->frame_control) &&
  1672. !ieee80211_is_probe_resp(hdr->frame_control))
  1673. return;
  1674. spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
  1675. list_for_each_entry(entry, &rtlpriv->scan_list.list, list) {
  1676. if (memcmp(entry->bssid, hdr->addr3, ETH_ALEN) == 0) {
  1677. list_del_init(&entry->list);
  1678. entry_found = true;
  1679. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
  1680. "Update BSSID=%pM to scan list (total=%d)\n",
  1681. hdr->addr3, rtlpriv->scan_list.num);
  1682. break;
  1683. }
  1684. }
  1685. if (!entry_found) {
  1686. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1687. if (!entry)
  1688. goto label_err;
  1689. memcpy(entry->bssid, hdr->addr3, ETH_ALEN);
  1690. rtlpriv->scan_list.num++;
  1691. RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
  1692. "Add BSSID=%pM to scan list (total=%d)\n",
  1693. hdr->addr3, rtlpriv->scan_list.num);
  1694. }
  1695. entry->age = jiffies;
  1696. list_add_tail(&entry->list, &rtlpriv->scan_list.list);
  1697. label_err:
  1698. spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
  1699. }
  1700. void rtl_watchdog_wq_callback(void *data)
  1701. {
  1702. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  1703. struct rtl_works,
  1704. watchdog_wq);
  1705. struct ieee80211_hw *hw = rtlworks->hw;
  1706. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1707. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  1708. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  1709. bool busytraffic = false;
  1710. bool tx_busy_traffic = false;
  1711. bool rx_busy_traffic = false;
  1712. bool higher_busytraffic = false;
  1713. bool higher_busyrxtraffic = false;
  1714. u8 idx, tid;
  1715. u32 rx_cnt_inp4eriod = 0;
  1716. u32 tx_cnt_inp4eriod = 0;
  1717. u32 aver_rx_cnt_inperiod = 0;
  1718. u32 aver_tx_cnt_inperiod = 0;
  1719. u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
  1720. u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
  1721. if (is_hal_stop(rtlhal))
  1722. return;
  1723. /* <1> Determine if action frame is allowed */
  1724. if (mac->link_state > MAC80211_NOLINK) {
  1725. if (mac->cnt_after_linked < 20)
  1726. mac->cnt_after_linked++;
  1727. } else {
  1728. mac->cnt_after_linked = 0;
  1729. }
  1730. /* <2> to check if traffic busy, if
  1731. * busytraffic we don't change channel
  1732. */
  1733. if (mac->link_state >= MAC80211_LINKED) {
  1734. /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
  1735. for (idx = 0; idx <= 2; idx++) {
  1736. rtlpriv->link_info.num_rx_in4period[idx] =
  1737. rtlpriv->link_info.num_rx_in4period[idx + 1];
  1738. rtlpriv->link_info.num_tx_in4period[idx] =
  1739. rtlpriv->link_info.num_tx_in4period[idx + 1];
  1740. }
  1741. rtlpriv->link_info.num_rx_in4period[3] =
  1742. rtlpriv->link_info.num_rx_inperiod;
  1743. rtlpriv->link_info.num_tx_in4period[3] =
  1744. rtlpriv->link_info.num_tx_inperiod;
  1745. for (idx = 0; idx <= 3; idx++) {
  1746. rx_cnt_inp4eriod +=
  1747. rtlpriv->link_info.num_rx_in4period[idx];
  1748. tx_cnt_inp4eriod +=
  1749. rtlpriv->link_info.num_tx_in4period[idx];
  1750. }
  1751. aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
  1752. aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
  1753. /* (2) check traffic busy */
  1754. if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
  1755. busytraffic = true;
  1756. if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
  1757. rx_busy_traffic = true;
  1758. else
  1759. tx_busy_traffic = false;
  1760. }
  1761. /* Higher Tx/Rx data. */
  1762. if (aver_rx_cnt_inperiod > 4000 ||
  1763. aver_tx_cnt_inperiod > 4000) {
  1764. higher_busytraffic = true;
  1765. /* Extremely high Rx data. */
  1766. if (aver_rx_cnt_inperiod > 5000)
  1767. higher_busyrxtraffic = true;
  1768. }
  1769. /* check every tid's tx traffic */
  1770. for (tid = 0; tid <= 7; tid++) {
  1771. for (idx = 0; idx <= 2; idx++)
  1772. rtlpriv->link_info.tidtx_in4period[tid][idx] =
  1773. rtlpriv->link_info.tidtx_in4period[tid]
  1774. [idx + 1];
  1775. rtlpriv->link_info.tidtx_in4period[tid][3] =
  1776. rtlpriv->link_info.tidtx_inperiod[tid];
  1777. for (idx = 0; idx <= 3; idx++)
  1778. tidtx_inp4eriod[tid] +=
  1779. rtlpriv->link_info.tidtx_in4period[tid][idx];
  1780. aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
  1781. if (aver_tidtx_inperiod[tid] > 5000)
  1782. rtlpriv->link_info.higher_busytxtraffic[tid] =
  1783. true;
  1784. else
  1785. rtlpriv->link_info.higher_busytxtraffic[tid] =
  1786. false;
  1787. }
  1788. /* PS is controlled by coex. */
  1789. if (rtlpriv->cfg->ops->get_btc_status() &&
  1790. rtlpriv->btcoexist.btc_ops->btc_is_bt_ctrl_lps(rtlpriv))
  1791. goto label_lps_done;
  1792. if (rtlpriv->link_info.num_rx_inperiod +
  1793. rtlpriv->link_info.num_tx_inperiod > 8 ||
  1794. rtlpriv->link_info.num_rx_inperiod > 2)
  1795. rtl_lps_leave(hw);
  1796. else
  1797. rtl_lps_enter(hw);
  1798. label_lps_done:
  1799. ;
  1800. }
  1801. rtlpriv->link_info.num_rx_inperiod = 0;
  1802. rtlpriv->link_info.num_tx_inperiod = 0;
  1803. for (tid = 0; tid <= 7; tid++)
  1804. rtlpriv->link_info.tidtx_inperiod[tid] = 0;
  1805. rtlpriv->link_info.busytraffic = busytraffic;
  1806. rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
  1807. rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
  1808. rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
  1809. rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
  1810. rtlpriv->stats.txbytesunicast_inperiod =
  1811. rtlpriv->stats.txbytesunicast -
  1812. rtlpriv->stats.txbytesunicast_last;
  1813. rtlpriv->stats.rxbytesunicast_inperiod =
  1814. rtlpriv->stats.rxbytesunicast -
  1815. rtlpriv->stats.rxbytesunicast_last;
  1816. rtlpriv->stats.txbytesunicast_last = rtlpriv->stats.txbytesunicast;
  1817. rtlpriv->stats.rxbytesunicast_last = rtlpriv->stats.rxbytesunicast;
  1818. rtlpriv->stats.txbytesunicast_inperiod_tp =
  1819. (u32)(rtlpriv->stats.txbytesunicast_inperiod * 8 / 2 /
  1820. 1024 / 1024);
  1821. rtlpriv->stats.rxbytesunicast_inperiod_tp =
  1822. (u32)(rtlpriv->stats.rxbytesunicast_inperiod * 8 / 2 /
  1823. 1024 / 1024);
  1824. /* <3> DM */
  1825. if (!rtlpriv->cfg->mod_params->disable_watchdog)
  1826. rtlpriv->cfg->ops->dm_watchdog(hw);
  1827. /* <4> roaming */
  1828. if (mac->link_state == MAC80211_LINKED &&
  1829. mac->opmode == NL80211_IFTYPE_STATION) {
  1830. if ((rtlpriv->link_info.bcn_rx_inperiod +
  1831. rtlpriv->link_info.num_rx_inperiod) == 0) {
  1832. rtlpriv->link_info.roam_times++;
  1833. RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
  1834. "AP off for %d s\n",
  1835. (rtlpriv->link_info.roam_times * 2));
  1836. /* if we can't recv beacon for 10s,
  1837. * we should reconnect this AP
  1838. */
  1839. if (rtlpriv->link_info.roam_times >= 5) {
  1840. pr_err("AP off, try to reconnect now\n");
  1841. rtlpriv->link_info.roam_times = 0;
  1842. ieee80211_connection_loss(
  1843. rtlpriv->mac80211.vif);
  1844. }
  1845. } else {
  1846. rtlpriv->link_info.roam_times = 0;
  1847. }
  1848. }
  1849. if (rtlpriv->cfg->ops->get_btc_status())
  1850. rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
  1851. if (rtlpriv->btcoexist.btc_info.in_4way) {
  1852. if (time_after(jiffies, rtlpriv->btcoexist.btc_info.in_4way_ts +
  1853. msecs_to_jiffies(IN_4WAY_TIMEOUT_TIME)))
  1854. rtlpriv->btcoexist.btc_info.in_4way = false;
  1855. }
  1856. rtlpriv->link_info.bcn_rx_inperiod = 0;
  1857. /* <6> scan list */
  1858. rtl_scan_list_expire(hw);
  1859. }
  1860. void rtl_watch_dog_timer_callback(struct timer_list *t)
  1861. {
  1862. struct rtl_priv *rtlpriv = from_timer(rtlpriv, t, works.watchdog_timer);
  1863. queue_delayed_work(rtlpriv->works.rtl_wq,
  1864. &rtlpriv->works.watchdog_wq, 0);
  1865. mod_timer(&rtlpriv->works.watchdog_timer,
  1866. jiffies + MSECS(RTL_WATCH_DOG_TIME));
  1867. }
  1868. void rtl_fwevt_wq_callback(void *data)
  1869. {
  1870. struct rtl_works *rtlworks =
  1871. container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
  1872. struct ieee80211_hw *hw = rtlworks->hw;
  1873. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1874. rtlpriv->cfg->ops->c2h_command_handle(hw);
  1875. }
  1876. void rtl_c2hcmd_enqueue(struct ieee80211_hw *hw, u8 tag, u8 len, u8 *val)
  1877. {
  1878. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1879. unsigned long flags;
  1880. struct rtl_c2hcmd *c2hcmd;
  1881. c2hcmd = kmalloc(sizeof(*c2hcmd),
  1882. in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  1883. if (!c2hcmd)
  1884. goto label_err;
  1885. c2hcmd->val = kmalloc(len,
  1886. in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  1887. if (!c2hcmd->val)
  1888. goto label_err2;
  1889. /* fill data */
  1890. c2hcmd->tag = tag;
  1891. c2hcmd->len = len;
  1892. memcpy(c2hcmd->val, val, len);
  1893. /* enqueue */
  1894. spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
  1895. list_add_tail(&c2hcmd->list, &rtlpriv->c2hcmd_list);
  1896. spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
  1897. /* wake up wq */
  1898. queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.c2hcmd_wq, 0);
  1899. return;
  1900. label_err2:
  1901. kfree(c2hcmd);
  1902. label_err:
  1903. RT_TRACE(rtlpriv, COMP_CMD, DBG_WARNING,
  1904. "C2H cmd enqueue fail.\n");
  1905. }
  1906. void rtl_c2hcmd_launcher(struct ieee80211_hw *hw, int exec)
  1907. {
  1908. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1909. unsigned long flags;
  1910. struct rtl_c2hcmd *c2hcmd;
  1911. int i;
  1912. for (i = 0; i < 200; i++) {
  1913. /* dequeue a task */
  1914. spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
  1915. c2hcmd = list_first_entry_or_null(&rtlpriv->c2hcmd_list,
  1916. struct rtl_c2hcmd, list);
  1917. if (c2hcmd)
  1918. list_del(&c2hcmd->list);
  1919. spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
  1920. /* do it */
  1921. if (!c2hcmd)
  1922. break;
  1923. if (rtlpriv->cfg->ops->c2h_content_parsing && exec)
  1924. rtlpriv->cfg->ops->c2h_content_parsing(hw,
  1925. c2hcmd->tag, c2hcmd->len, c2hcmd->val);
  1926. /* free */
  1927. kfree(c2hcmd->val);
  1928. kfree(c2hcmd);
  1929. }
  1930. }
  1931. void rtl_c2hcmd_wq_callback(void *data)
  1932. {
  1933. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  1934. struct rtl_works,
  1935. c2hcmd_wq);
  1936. struct ieee80211_hw *hw = rtlworks->hw;
  1937. rtl_c2hcmd_launcher(hw, 1);
  1938. }
  1939. void rtl_easy_concurrent_retrytimer_callback(struct timer_list *t)
  1940. {
  1941. struct rtl_priv *rtlpriv =
  1942. from_timer(rtlpriv, t, works.dualmac_easyconcurrent_retrytimer);
  1943. struct ieee80211_hw *hw = rtlpriv->hw;
  1944. struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
  1945. if (!buddy_priv)
  1946. return;
  1947. rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
  1948. }
  1949. /*********************************************************
  1950. *
  1951. * frame process functions
  1952. *
  1953. *********************************************************/
  1954. u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
  1955. {
  1956. struct ieee80211_mgmt *mgmt = (void *)data;
  1957. u8 *pos, *end;
  1958. pos = (u8 *)mgmt->u.beacon.variable;
  1959. end = data + len;
  1960. while (pos < end) {
  1961. if (pos + 2 + pos[1] > end)
  1962. return NULL;
  1963. if (pos[0] == ie)
  1964. return pos;
  1965. pos += 2 + pos[1];
  1966. }
  1967. return NULL;
  1968. }
  1969. /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
  1970. /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
  1971. static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
  1972. enum ieee80211_smps_mode smps,
  1973. u8 *da, u8 *bssid)
  1974. {
  1975. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  1976. struct sk_buff *skb;
  1977. struct ieee80211_mgmt *action_frame;
  1978. /* 27 = header + category + action + smps mode */
  1979. skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
  1980. if (!skb)
  1981. return NULL;
  1982. skb_reserve(skb, hw->extra_tx_headroom);
  1983. action_frame = skb_put_zero(skb, 27);
  1984. memcpy(action_frame->da, da, ETH_ALEN);
  1985. memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
  1986. memcpy(action_frame->bssid, bssid, ETH_ALEN);
  1987. action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1988. IEEE80211_STYPE_ACTION);
  1989. action_frame->u.action.category = WLAN_CATEGORY_HT;
  1990. action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
  1991. switch (smps) {
  1992. case IEEE80211_SMPS_AUTOMATIC:/* 0 */
  1993. case IEEE80211_SMPS_NUM_MODES:/* 4 */
  1994. WARN_ON(1);
  1995. /* fall through */
  1996. case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
  1997. action_frame->u.action.u.ht_smps.smps_control =
  1998. WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
  1999. break;
  2000. case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
  2001. action_frame->u.action.u.ht_smps.smps_control =
  2002. WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
  2003. break;
  2004. case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
  2005. action_frame->u.action.u.ht_smps.smps_control =
  2006. WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
  2007. break;
  2008. }
  2009. return skb;
  2010. }
  2011. int rtl_send_smps_action(struct ieee80211_hw *hw,
  2012. struct ieee80211_sta *sta,
  2013. enum ieee80211_smps_mode smps)
  2014. {
  2015. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2016. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  2017. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  2018. struct sk_buff *skb = NULL;
  2019. struct rtl_tcb_desc tcb_desc;
  2020. u8 bssid[ETH_ALEN] = {0};
  2021. memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
  2022. if (rtlpriv->mac80211.act_scanning)
  2023. goto err_free;
  2024. if (!sta)
  2025. goto err_free;
  2026. if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
  2027. goto err_free;
  2028. if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
  2029. goto err_free;
  2030. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
  2031. memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
  2032. else
  2033. memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
  2034. skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
  2035. /* this is a type = mgmt * stype = action frame */
  2036. if (skb) {
  2037. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2038. struct rtl_sta_info *sta_entry =
  2039. (struct rtl_sta_info *)sta->drv_priv;
  2040. sta_entry->mimo_ps = smps;
  2041. /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true); */
  2042. info->control.rates[0].idx = 0;
  2043. info->band = hw->conf.chandef.chan->band;
  2044. rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
  2045. }
  2046. return 1;
  2047. err_free:
  2048. return 0;
  2049. }
  2050. void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
  2051. {
  2052. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2053. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  2054. enum io_type iotype;
  2055. if (!is_hal_stop(rtlhal)) {
  2056. switch (operation) {
  2057. case SCAN_OPT_BACKUP:
  2058. iotype = IO_CMD_PAUSE_DM_BY_SCAN;
  2059. rtlpriv->cfg->ops->set_hw_reg(hw,
  2060. HW_VAR_IO_CMD,
  2061. (u8 *)&iotype);
  2062. break;
  2063. case SCAN_OPT_RESTORE:
  2064. iotype = IO_CMD_RESUME_DM_BY_SCAN;
  2065. rtlpriv->cfg->ops->set_hw_reg(hw,
  2066. HW_VAR_IO_CMD,
  2067. (u8 *)&iotype);
  2068. break;
  2069. default:
  2070. pr_err("Unknown Scan Backup operation.\n");
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. /* because mac80211 have issues when can receive del ba
  2076. * so here we just make a fake del_ba if we receive a ba_req
  2077. * but rx_agg was opened to let mac80211 release some ba
  2078. * related resources, so please this del_ba for tx
  2079. */
  2080. struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
  2081. u8 *sa, u8 *bssid, u16 tid)
  2082. {
  2083. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  2084. struct sk_buff *skb;
  2085. struct ieee80211_mgmt *action_frame;
  2086. u16 params;
  2087. /* 27 = header + category + action + smps mode */
  2088. skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
  2089. if (!skb)
  2090. return NULL;
  2091. skb_reserve(skb, hw->extra_tx_headroom);
  2092. action_frame = skb_put_zero(skb, 34);
  2093. memcpy(action_frame->sa, sa, ETH_ALEN);
  2094. memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
  2095. memcpy(action_frame->bssid, bssid, ETH_ALEN);
  2096. action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2097. IEEE80211_STYPE_ACTION);
  2098. action_frame->u.action.category = WLAN_CATEGORY_BACK;
  2099. action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
  2100. params = (u16)(1 << 11); /* bit 11 initiator */
  2101. params |= (u16)(tid << 12); /* bit 15:12 TID number */
  2102. action_frame->u.action.u.delba.params = cpu_to_le16(params);
  2103. action_frame->u.action.u.delba.reason_code =
  2104. cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
  2105. return skb;
  2106. }
  2107. bool rtl_check_beacon_key(struct ieee80211_hw *hw, void *data, unsigned int len)
  2108. {
  2109. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2110. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  2111. struct rtl_phy *rtlphy = &rtlpriv->phy;
  2112. struct ieee80211_hdr *hdr = data;
  2113. struct ieee80211_ht_cap *ht_cap_ie;
  2114. struct ieee80211_ht_operation *ht_oper_ie = NULL;
  2115. struct rtl_beacon_keys bcn_key = {};
  2116. struct rtl_beacon_keys *cur_bcn_key;
  2117. u8 *ht_cap;
  2118. u8 ht_cap_len;
  2119. u8 *ht_oper;
  2120. u8 ht_oper_len;
  2121. u8 *ds_param;
  2122. u8 ds_param_len;
  2123. if (mac->opmode != NL80211_IFTYPE_STATION)
  2124. return false;
  2125. /* check if this really is a beacon*/
  2126. if (!ieee80211_is_beacon(hdr->frame_control))
  2127. return false;
  2128. /* min. beacon length + FCS_LEN */
  2129. if (len <= 40 + FCS_LEN)
  2130. return false;
  2131. cur_bcn_key = &mac->cur_beacon_keys;
  2132. if (rtlpriv->mac80211.link_state == MAC80211_NOLINK) {
  2133. if (cur_bcn_key->valid) {
  2134. cur_bcn_key->valid = false;
  2135. RT_TRACE(rtlpriv, COMP_BEACON, DBG_LOUD,
  2136. "Reset cur_beacon_keys.valid to false!\n");
  2137. }
  2138. return false;
  2139. }
  2140. /* and only beacons from the associated BSSID, please */
  2141. if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
  2142. return false;
  2143. /***** Parsing DS Param IE ******/
  2144. ds_param = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_DS_PARAMS);
  2145. if (ds_param && !(ds_param[1] < sizeof(*ds_param))) {
  2146. ds_param_len = ds_param[1];
  2147. bcn_key.bcn_channel = ds_param[2];
  2148. } else {
  2149. ds_param = NULL;
  2150. }
  2151. /***** Parsing HT Cap. IE ******/
  2152. ht_cap = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_HT_CAPABILITY);
  2153. if (ht_cap && !(ht_cap[1] < sizeof(*ht_cap))) {
  2154. ht_cap_len = ht_cap[1];
  2155. ht_cap_ie = (struct ieee80211_ht_cap *)&ht_cap[2];
  2156. bcn_key.ht_cap_info = ht_cap_ie->cap_info;
  2157. } else {
  2158. ht_cap = NULL;
  2159. }
  2160. /***** Parsing HT Info. IE ******/
  2161. ht_oper = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_HT_OPERATION);
  2162. if (ht_oper && !(ht_oper[1] < sizeof(*ht_oper))) {
  2163. ht_oper_len = ht_oper[1];
  2164. ht_oper_ie = (struct ieee80211_ht_operation *)&ht_oper[2];
  2165. } else {
  2166. ht_oper = NULL;
  2167. }
  2168. /* update bcn_key */
  2169. if (!ds_param && ht_oper && ht_oper_ie)
  2170. bcn_key.bcn_channel = ht_oper_ie->primary_chan;
  2171. if (ht_oper && ht_oper_ie)
  2172. bcn_key.ht_info_infos_0_sco = ht_oper_ie->ht_param & 0x03;
  2173. bcn_key.valid = true;
  2174. /* update cur_beacon_keys or compare beacon key */
  2175. if (rtlpriv->mac80211.link_state != MAC80211_LINKED &&
  2176. rtlpriv->mac80211.link_state != MAC80211_LINKED_SCANNING)
  2177. return true;
  2178. if (!cur_bcn_key->valid) {
  2179. /* update cur_beacon_keys */
  2180. memcpy(cur_bcn_key, &bcn_key, sizeof(bcn_key));
  2181. cur_bcn_key->valid = true;
  2182. RT_TRACE(rtlpriv, COMP_BEACON, DBG_LOUD,
  2183. "Beacon key update!ch=%d, ht_cap_info=0x%x, sco=0x%x\n",
  2184. cur_bcn_key->bcn_channel,
  2185. cur_bcn_key->ht_cap_info,
  2186. cur_bcn_key->ht_info_infos_0_sco);
  2187. return true;
  2188. }
  2189. /* compare beacon key */
  2190. if (!memcmp(cur_bcn_key, &bcn_key, sizeof(bcn_key))) {
  2191. /* same beacon key */
  2192. mac->new_beacon_cnt = 0;
  2193. goto chk_exit;
  2194. }
  2195. if (cur_bcn_key->bcn_channel == bcn_key.bcn_channel &&
  2196. cur_bcn_key->ht_cap_info == bcn_key.ht_cap_info) {
  2197. /* Beacon HT info IE, secondary channel offset check */
  2198. /* 40M -> 20M */
  2199. if (cur_bcn_key->ht_info_infos_0_sco >
  2200. bcn_key.ht_info_infos_0_sco) {
  2201. /* Not a new beacon */
  2202. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2203. "Beacon BW change! sco:0x%x -> 0x%x\n",
  2204. cur_bcn_key->ht_info_infos_0_sco,
  2205. bcn_key.ht_info_infos_0_sco);
  2206. cur_bcn_key->ht_info_infos_0_sco =
  2207. bcn_key.ht_info_infos_0_sco;
  2208. } else {
  2209. /* 20M -> 40M */
  2210. if (rtlphy->max_ht_chan_bw >= HT_CHANNEL_WIDTH_20_40) {
  2211. /* Not a new beacon */
  2212. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2213. "Beacon BW change! sco:0x%x -> 0x%x\n",
  2214. cur_bcn_key->ht_info_infos_0_sco,
  2215. bcn_key.ht_info_infos_0_sco);
  2216. cur_bcn_key->ht_info_infos_0_sco =
  2217. bcn_key.ht_info_infos_0_sco;
  2218. } else {
  2219. mac->new_beacon_cnt++;
  2220. }
  2221. }
  2222. } else {
  2223. mac->new_beacon_cnt++;
  2224. }
  2225. if (mac->new_beacon_cnt == 1) {
  2226. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2227. "Get new beacon.\n");
  2228. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2229. "Cur : ch=%d, ht_cap=0x%x, sco=0x%x\n",
  2230. cur_bcn_key->bcn_channel,
  2231. cur_bcn_key->ht_cap_info,
  2232. cur_bcn_key->ht_info_infos_0_sco);
  2233. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2234. "New RX : ch=%d, ht_cap=0x%x, sco=0x%x\n",
  2235. bcn_key.bcn_channel,
  2236. bcn_key.ht_cap_info,
  2237. bcn_key.ht_info_infos_0_sco);
  2238. } else if (mac->new_beacon_cnt > 1) {
  2239. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2240. "new beacon cnt: %d\n",
  2241. mac->new_beacon_cnt);
  2242. }
  2243. if (mac->new_beacon_cnt > 3) {
  2244. ieee80211_connection_loss(rtlpriv->mac80211.vif);
  2245. RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
  2246. "new beacon cnt >3, disconnect !\n");
  2247. }
  2248. chk_exit:
  2249. return true;
  2250. }
  2251. /*********************************************************
  2252. *
  2253. * IOT functions
  2254. *
  2255. *********************************************************/
  2256. static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
  2257. struct octet_string vendor_ie)
  2258. {
  2259. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2260. bool matched = false;
  2261. static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
  2262. static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
  2263. static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
  2264. static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
  2265. static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
  2266. static u8 racap[] = { 0x00, 0x0c, 0x43 };
  2267. static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
  2268. static u8 marvcap[] = { 0x00, 0x50, 0x43 };
  2269. if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
  2270. memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
  2271. rtlpriv->mac80211.vendor = PEER_ATH;
  2272. matched = true;
  2273. } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
  2274. memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
  2275. memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
  2276. rtlpriv->mac80211.vendor = PEER_BROAD;
  2277. matched = true;
  2278. } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
  2279. rtlpriv->mac80211.vendor = PEER_RAL;
  2280. matched = true;
  2281. } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
  2282. rtlpriv->mac80211.vendor = PEER_CISCO;
  2283. matched = true;
  2284. } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
  2285. rtlpriv->mac80211.vendor = PEER_MARV;
  2286. matched = true;
  2287. }
  2288. return matched;
  2289. }
  2290. static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
  2291. unsigned int len)
  2292. {
  2293. struct ieee80211_mgmt *mgmt = (void *)data;
  2294. struct octet_string vendor_ie;
  2295. u8 *pos, *end;
  2296. pos = (u8 *)mgmt->u.beacon.variable;
  2297. end = data + len;
  2298. while (pos < end) {
  2299. if (pos[0] == 221) {
  2300. vendor_ie.length = pos[1];
  2301. vendor_ie.octet = &pos[2];
  2302. if (rtl_chk_vendor_ouisub(hw, vendor_ie))
  2303. return true;
  2304. }
  2305. if (pos + 2 + pos[1] > end)
  2306. return false;
  2307. pos += 2 + pos[1];
  2308. }
  2309. return false;
  2310. }
  2311. void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
  2312. {
  2313. struct rtl_priv *rtlpriv = rtl_priv(hw);
  2314. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  2315. struct ieee80211_hdr *hdr = (void *)data;
  2316. u32 vendor = PEER_UNKNOWN;
  2317. static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
  2318. static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
  2319. static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
  2320. static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
  2321. static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
  2322. static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
  2323. static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
  2324. static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
  2325. static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
  2326. static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
  2327. static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
  2328. static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
  2329. static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
  2330. static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
  2331. static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
  2332. static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
  2333. if (mac->opmode != NL80211_IFTYPE_STATION)
  2334. return;
  2335. if (mac->link_state == MAC80211_NOLINK) {
  2336. mac->vendor = PEER_UNKNOWN;
  2337. return;
  2338. }
  2339. if (mac->cnt_after_linked > 2)
  2340. return;
  2341. /* check if this really is a beacon */
  2342. if (!ieee80211_is_beacon(hdr->frame_control))
  2343. return;
  2344. /* min. beacon length + FCS_LEN */
  2345. if (len <= 40 + FCS_LEN)
  2346. return;
  2347. /* and only beacons from the associated BSSID, please */
  2348. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  2349. return;
  2350. if (rtl_find_221_ie(hw, data, len))
  2351. vendor = mac->vendor;
  2352. if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
  2353. (memcmp(mac->bssid, ap5_2, 3) == 0) ||
  2354. (memcmp(mac->bssid, ap5_3, 3) == 0) ||
  2355. (memcmp(mac->bssid, ap5_4, 3) == 0) ||
  2356. (memcmp(mac->bssid, ap5_5, 3) == 0) ||
  2357. (memcmp(mac->bssid, ap5_6, 3) == 0) ||
  2358. vendor == PEER_ATH) {
  2359. vendor = PEER_ATH;
  2360. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
  2361. } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
  2362. (memcmp(mac->bssid, ap4_5, 3) == 0) ||
  2363. (memcmp(mac->bssid, ap4_1, 3) == 0) ||
  2364. (memcmp(mac->bssid, ap4_2, 3) == 0) ||
  2365. (memcmp(mac->bssid, ap4_3, 3) == 0) ||
  2366. vendor == PEER_RAL) {
  2367. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
  2368. vendor = PEER_RAL;
  2369. } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
  2370. vendor == PEER_CISCO) {
  2371. vendor = PEER_CISCO;
  2372. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
  2373. } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
  2374. (memcmp(mac->bssid, ap3_2, 3) == 0) ||
  2375. (memcmp(mac->bssid, ap3_3, 3) == 0) ||
  2376. vendor == PEER_BROAD) {
  2377. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
  2378. vendor = PEER_BROAD;
  2379. } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
  2380. vendor == PEER_MARV) {
  2381. vendor = PEER_MARV;
  2382. RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
  2383. }
  2384. mac->vendor = vendor;
  2385. }
  2386. MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
  2387. MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
  2388. MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
  2389. MODULE_LICENSE("GPL");
  2390. MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
  2391. struct rtl_global_var rtl_global_var = {};
  2392. int rtl_core_module_init(void)
  2393. {
  2394. if (rtl_rate_control_register())
  2395. pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
  2396. /* add debugfs */
  2397. rtl_debugfs_add_topdir();
  2398. /* init some global vars */
  2399. INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
  2400. spin_lock_init(&rtl_global_var.glb_list_lock);
  2401. return 0;
  2402. }
  2403. void rtl_core_module_exit(void)
  2404. {
  2405. /*RC*/
  2406. rtl_rate_control_unregister();
  2407. /* remove debugfs */
  2408. rtl_debugfs_remove_topdir();
  2409. }