base.c 75 KB

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