rs.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/slab.h>
  30. #include <net/mac80211.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include "rs.h"
  36. #include "fw-api.h"
  37. #include "sta.h"
  38. #include "iwl-op-mode.h"
  39. #include "mvm.h"
  40. #define RS_NAME "iwl-mvm-rs"
  41. #define NUM_TRY_BEFORE_ANT_TOGGLE 1
  42. #define IWL_NUMBER_TRY 1
  43. #define IWL_HT_NUMBER_TRY 3
  44. #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
  45. #define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
  46. #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  47. /* max allowed rate miss before sync LQ cmd */
  48. #define IWL_MISSED_RATE_MAX 15
  49. #define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
  50. static u8 rs_ht_to_legacy[] = {
  51. [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
  52. [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
  53. [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
  54. [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
  55. [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
  56. [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
  57. [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
  58. [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
  59. [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
  60. [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
  61. };
  62. static const u8 ant_toggle_lookup[] = {
  63. [ANT_NONE] = ANT_NONE,
  64. [ANT_A] = ANT_B,
  65. [ANT_B] = ANT_C,
  66. [ANT_AB] = ANT_BC,
  67. [ANT_C] = ANT_A,
  68. [ANT_AC] = ANT_AB,
  69. [ANT_BC] = ANT_AC,
  70. [ANT_ABC] = ANT_ABC,
  71. };
  72. #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
  73. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  74. IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
  75. IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
  76. IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
  77. IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
  78. IWL_RATE_##rp##M_INDEX, \
  79. IWL_RATE_##rn##M_INDEX }
  80. #define IWL_DECLARE_MCS_RATE(s) \
  81. [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
  82. IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
  83. IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
  84. IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
  85. IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
  86. IWL_RATE_INVM_INDEX, \
  87. IWL_RATE_INVM_INDEX }
  88. /*
  89. * Parameter order:
  90. * rate, ht rate, prev rate, next rate
  91. *
  92. * If there isn't a valid next or previous rate then INV is used which
  93. * maps to IWL_RATE_INVALID
  94. *
  95. */
  96. static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
  97. IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
  98. IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
  99. IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
  100. IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
  101. IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
  102. IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
  103. IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
  104. IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
  105. IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
  106. IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
  107. IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
  108. IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
  109. IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
  110. IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
  111. IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
  112. };
  113. enum rs_column_mode {
  114. RS_INVALID = 0,
  115. RS_LEGACY,
  116. RS_SISO,
  117. RS_MIMO2,
  118. };
  119. #define MAX_NEXT_COLUMNS 5
  120. #define MAX_COLUMN_CHECKS 3
  121. typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
  122. struct ieee80211_sta *sta,
  123. struct iwl_scale_tbl_info *tbl);
  124. struct rs_tx_column {
  125. enum rs_column_mode mode;
  126. u8 ant;
  127. bool sgi;
  128. enum rs_column next_columns[MAX_NEXT_COLUMNS];
  129. allow_column_func_t checks[MAX_COLUMN_CHECKS];
  130. };
  131. static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  132. struct iwl_scale_tbl_info *tbl)
  133. {
  134. if (!sta->ht_cap.ht_supported)
  135. return false;
  136. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  137. return false;
  138. if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
  139. return false;
  140. if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
  141. return false;
  142. return true;
  143. }
  144. static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  145. struct iwl_scale_tbl_info *tbl)
  146. {
  147. if (!sta->ht_cap.ht_supported)
  148. return false;
  149. return true;
  150. }
  151. static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  152. struct iwl_scale_tbl_info *tbl)
  153. {
  154. struct rs_rate *rate = &tbl->rate;
  155. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  156. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  157. if (is_ht20(rate) && (ht_cap->cap &
  158. IEEE80211_HT_CAP_SGI_20))
  159. return true;
  160. if (is_ht40(rate) && (ht_cap->cap &
  161. IEEE80211_HT_CAP_SGI_40))
  162. return true;
  163. if (is_ht80(rate) && (vht_cap->cap &
  164. IEEE80211_VHT_CAP_SHORT_GI_80))
  165. return true;
  166. return false;
  167. }
  168. static const struct rs_tx_column rs_tx_columns[] = {
  169. [RS_COLUMN_LEGACY_ANT_A] = {
  170. .mode = RS_LEGACY,
  171. .ant = ANT_A,
  172. .next_columns = {
  173. RS_COLUMN_LEGACY_ANT_B,
  174. RS_COLUMN_SISO_ANT_A,
  175. RS_COLUMN_MIMO2,
  176. RS_COLUMN_INVALID,
  177. RS_COLUMN_INVALID,
  178. },
  179. },
  180. [RS_COLUMN_LEGACY_ANT_B] = {
  181. .mode = RS_LEGACY,
  182. .ant = ANT_B,
  183. .next_columns = {
  184. RS_COLUMN_LEGACY_ANT_A,
  185. RS_COLUMN_SISO_ANT_B,
  186. RS_COLUMN_MIMO2,
  187. RS_COLUMN_INVALID,
  188. RS_COLUMN_INVALID,
  189. },
  190. },
  191. [RS_COLUMN_SISO_ANT_A] = {
  192. .mode = RS_SISO,
  193. .ant = ANT_A,
  194. .next_columns = {
  195. RS_COLUMN_SISO_ANT_B,
  196. RS_COLUMN_MIMO2,
  197. RS_COLUMN_SISO_ANT_A_SGI,
  198. RS_COLUMN_INVALID,
  199. RS_COLUMN_INVALID,
  200. },
  201. .checks = {
  202. rs_siso_allow,
  203. },
  204. },
  205. [RS_COLUMN_SISO_ANT_B] = {
  206. .mode = RS_SISO,
  207. .ant = ANT_B,
  208. .next_columns = {
  209. RS_COLUMN_SISO_ANT_A,
  210. RS_COLUMN_MIMO2,
  211. RS_COLUMN_SISO_ANT_B_SGI,
  212. RS_COLUMN_INVALID,
  213. RS_COLUMN_INVALID,
  214. },
  215. .checks = {
  216. rs_siso_allow,
  217. },
  218. },
  219. [RS_COLUMN_SISO_ANT_A_SGI] = {
  220. .mode = RS_SISO,
  221. .ant = ANT_A,
  222. .sgi = true,
  223. .next_columns = {
  224. RS_COLUMN_SISO_ANT_B_SGI,
  225. RS_COLUMN_MIMO2_SGI,
  226. RS_COLUMN_SISO_ANT_A,
  227. RS_COLUMN_INVALID,
  228. RS_COLUMN_INVALID,
  229. },
  230. .checks = {
  231. rs_siso_allow,
  232. rs_sgi_allow,
  233. },
  234. },
  235. [RS_COLUMN_SISO_ANT_B_SGI] = {
  236. .mode = RS_SISO,
  237. .ant = ANT_B,
  238. .sgi = true,
  239. .next_columns = {
  240. RS_COLUMN_SISO_ANT_A_SGI,
  241. RS_COLUMN_MIMO2_SGI,
  242. RS_COLUMN_SISO_ANT_B,
  243. RS_COLUMN_INVALID,
  244. RS_COLUMN_INVALID,
  245. },
  246. .checks = {
  247. rs_siso_allow,
  248. rs_sgi_allow,
  249. },
  250. },
  251. [RS_COLUMN_MIMO2] = {
  252. .mode = RS_MIMO2,
  253. .ant = ANT_AB,
  254. .next_columns = {
  255. RS_COLUMN_SISO_ANT_A,
  256. RS_COLUMN_MIMO2_SGI,
  257. RS_COLUMN_INVALID,
  258. RS_COLUMN_INVALID,
  259. RS_COLUMN_INVALID,
  260. },
  261. .checks = {
  262. rs_mimo_allow,
  263. },
  264. },
  265. [RS_COLUMN_MIMO2_SGI] = {
  266. .mode = RS_MIMO2,
  267. .ant = ANT_AB,
  268. .sgi = true,
  269. .next_columns = {
  270. RS_COLUMN_SISO_ANT_A_SGI,
  271. RS_COLUMN_MIMO2,
  272. RS_COLUMN_INVALID,
  273. RS_COLUMN_INVALID,
  274. RS_COLUMN_INVALID,
  275. },
  276. .checks = {
  277. rs_mimo_allow,
  278. rs_sgi_allow,
  279. },
  280. },
  281. };
  282. static inline u8 rs_extract_rate(u32 rate_n_flags)
  283. {
  284. /* also works for HT because bits 7:6 are zero there */
  285. return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
  286. }
  287. static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
  288. {
  289. int idx = 0;
  290. if (rate_n_flags & RATE_MCS_HT_MSK) {
  291. idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
  292. idx += IWL_RATE_MCS_0_INDEX;
  293. /* skip 9M not supported in HT*/
  294. if (idx >= IWL_RATE_9M_INDEX)
  295. idx += 1;
  296. if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
  297. return idx;
  298. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  299. idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  300. idx += IWL_RATE_MCS_0_INDEX;
  301. /* skip 9M not supported in VHT*/
  302. if (idx >= IWL_RATE_9M_INDEX)
  303. idx++;
  304. if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
  305. return idx;
  306. } else {
  307. /* legacy rate format, search for match in table */
  308. u8 legacy_rate = rs_extract_rate(rate_n_flags);
  309. for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
  310. if (iwl_rates[idx].plcp == legacy_rate)
  311. return idx;
  312. }
  313. return -1;
  314. }
  315. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  316. struct sk_buff *skb,
  317. struct ieee80211_sta *sta,
  318. struct iwl_lq_sta *lq_sta);
  319. static void rs_fill_link_cmd(struct iwl_mvm *mvm,
  320. struct ieee80211_sta *sta,
  321. struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
  322. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
  323. #ifdef CONFIG_MAC80211_DEBUGFS
  324. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  325. u32 *rate_n_flags);
  326. #else
  327. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  328. u32 *rate_n_flags)
  329. {}
  330. #endif
  331. /**
  332. * The following tables contain the expected throughput metrics for all rates
  333. *
  334. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  335. *
  336. * where invalid entries are zeros.
  337. *
  338. * CCK rates are only valid in legacy table and will only be used in G
  339. * (2.4 GHz) band.
  340. */
  341. static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
  342. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
  343. };
  344. /* Expected TpT tables. 4 indexes:
  345. * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
  346. */
  347. static s32 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
  348. {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
  349. {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
  350. {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
  351. {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
  352. };
  353. static s32 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
  354. {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
  355. {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
  356. {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
  357. {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
  358. };
  359. static s32 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
  360. {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
  361. {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
  362. {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
  363. {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
  364. };
  365. static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
  366. {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
  367. {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
  368. {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
  369. {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
  370. };
  371. static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
  372. {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
  373. {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
  374. {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
  375. {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
  376. };
  377. static s32 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
  378. {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
  379. {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
  380. {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
  381. {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
  382. };
  383. /* mbps, mcs */
  384. static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
  385. { "1", "BPSK DSSS"},
  386. { "2", "QPSK DSSS"},
  387. {"5.5", "BPSK CCK"},
  388. { "11", "QPSK CCK"},
  389. { "6", "BPSK 1/2"},
  390. { "9", "BPSK 1/2"},
  391. { "12", "QPSK 1/2"},
  392. { "18", "QPSK 3/4"},
  393. { "24", "16QAM 1/2"},
  394. { "36", "16QAM 3/4"},
  395. { "48", "64QAM 2/3"},
  396. { "54", "64QAM 3/4"},
  397. { "60", "64QAM 5/6"},
  398. };
  399. #define MCS_INDEX_PER_STREAM (8)
  400. static const char *rs_pretty_ant(u8 ant)
  401. {
  402. static const char * const ant_name[] = {
  403. [ANT_NONE] = "None",
  404. [ANT_A] = "A",
  405. [ANT_B] = "B",
  406. [ANT_AB] = "AB",
  407. [ANT_C] = "C",
  408. [ANT_AC] = "AC",
  409. [ANT_BC] = "BC",
  410. [ANT_ABC] = "ABC",
  411. };
  412. if (ant > ANT_ABC)
  413. return "UNKNOWN";
  414. return ant_name[ant];
  415. }
  416. static const char *rs_pretty_lq_type(enum iwl_table_type type)
  417. {
  418. static const char * const lq_types[] = {
  419. [LQ_NONE] = "NONE",
  420. [LQ_LEGACY_A] = "LEGACY_A",
  421. [LQ_LEGACY_G] = "LEGACY_G",
  422. [LQ_HT_SISO] = "HT SISO",
  423. [LQ_HT_MIMO2] = "HT MIMO",
  424. [LQ_VHT_SISO] = "VHT SISO",
  425. [LQ_VHT_MIMO2] = "VHT MIMO",
  426. };
  427. if (type < LQ_NONE || type >= LQ_MAX)
  428. return "UNKNOWN";
  429. return lq_types[type];
  430. }
  431. static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
  432. const char *prefix)
  433. {
  434. IWL_DEBUG_RATE(mvm, "%s: (%s: %d) ANT: %s BW: %d SGI: %d\n",
  435. prefix, rs_pretty_lq_type(rate->type),
  436. rate->index, rs_pretty_ant(rate->ant),
  437. rate->bw, rate->sgi);
  438. }
  439. static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
  440. {
  441. window->data = 0;
  442. window->success_counter = 0;
  443. window->success_ratio = IWL_INVALID_VALUE;
  444. window->counter = 0;
  445. window->average_tpt = IWL_INVALID_VALUE;
  446. }
  447. static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  448. {
  449. return (ant_type & valid_antenna) == ant_type;
  450. }
  451. #ifdef CONFIG_MAC80211_DEBUGFS
  452. /**
  453. * Program the device to use fixed rate for frame transmit
  454. * This is for debugging/testing only
  455. * once the device start use fixed rate, we need to reload the module
  456. * to being back the normal operation.
  457. */
  458. static void rs_program_fix_rate(struct iwl_mvm *mvm,
  459. struct iwl_lq_sta *lq_sta)
  460. {
  461. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  462. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  463. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  464. IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
  465. lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
  466. if (lq_sta->dbg_fixed_rate) {
  467. rs_fill_link_cmd(NULL, NULL, lq_sta, lq_sta->dbg_fixed_rate);
  468. iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, false);
  469. }
  470. }
  471. #endif
  472. static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
  473. struct iwl_lq_sta *lq_data, u8 tid,
  474. struct ieee80211_sta *sta)
  475. {
  476. int ret = -EAGAIN;
  477. IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
  478. sta->addr, tid);
  479. ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
  480. if (ret == -EAGAIN) {
  481. /*
  482. * driver and mac80211 is out of sync
  483. * this might be cause by reloading firmware
  484. * stop the tx ba session here
  485. */
  486. IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
  487. tid);
  488. ieee80211_stop_tx_ba_session(sta, tid);
  489. }
  490. return ret;
  491. }
  492. static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
  493. struct iwl_lq_sta *lq_data,
  494. struct ieee80211_sta *sta)
  495. {
  496. if (tid < IWL_MAX_TID_COUNT)
  497. rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
  498. else
  499. IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
  500. tid, IWL_MAX_TID_COUNT);
  501. }
  502. static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
  503. {
  504. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  505. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  506. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  507. }
  508. /*
  509. * Static function to get the expected throughput from an iwl_scale_tbl_info
  510. * that wraps a NULL pointer check
  511. */
  512. static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
  513. {
  514. if (tbl->expected_tpt)
  515. return tbl->expected_tpt[rs_index];
  516. return 0;
  517. }
  518. /**
  519. * rs_collect_tx_data - Update the success/failure sliding window
  520. *
  521. * We keep a sliding window of the last 62 packets transmitted
  522. * at this rate. window->data contains the bitmask of successful
  523. * packets.
  524. */
  525. static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
  526. int scale_index, int attempts, int successes)
  527. {
  528. struct iwl_rate_scale_data *window = NULL;
  529. static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
  530. s32 fail_count, tpt;
  531. if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
  532. return -EINVAL;
  533. /* Select window for current tx bit rate */
  534. window = &(tbl->win[scale_index]);
  535. /* Get expected throughput */
  536. tpt = get_expected_tpt(tbl, scale_index);
  537. /*
  538. * Keep track of only the latest 62 tx frame attempts in this rate's
  539. * history window; anything older isn't really relevant any more.
  540. * If we have filled up the sliding window, drop the oldest attempt;
  541. * if the oldest attempt (highest bit in bitmap) shows "success",
  542. * subtract "1" from the success counter (this is the main reason
  543. * we keep these bitmaps!).
  544. */
  545. while (attempts > 0) {
  546. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  547. /* remove earliest */
  548. window->counter = IWL_RATE_MAX_WINDOW - 1;
  549. if (window->data & mask) {
  550. window->data &= ~mask;
  551. window->success_counter--;
  552. }
  553. }
  554. /* Increment frames-attempted counter */
  555. window->counter++;
  556. /* Shift bitmap by one frame to throw away oldest history */
  557. window->data <<= 1;
  558. /* Mark the most recent #successes attempts as successful */
  559. if (successes > 0) {
  560. window->success_counter++;
  561. window->data |= 0x1;
  562. successes--;
  563. }
  564. attempts--;
  565. }
  566. /* Calculate current success ratio, avoid divide-by-0! */
  567. if (window->counter > 0)
  568. window->success_ratio = 128 * (100 * window->success_counter)
  569. / window->counter;
  570. else
  571. window->success_ratio = IWL_INVALID_VALUE;
  572. fail_count = window->counter - window->success_counter;
  573. /* Calculate average throughput, if we have enough history. */
  574. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  575. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  576. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  577. else
  578. window->average_tpt = IWL_INVALID_VALUE;
  579. return 0;
  580. }
  581. /* Convert rs_rate object into ucode rate bitmask */
  582. static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
  583. struct rs_rate *rate)
  584. {
  585. u32 ucode_rate = 0;
  586. int index = rate->index;
  587. ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
  588. RATE_MCS_ANT_ABC_MSK);
  589. if (is_legacy(rate)) {
  590. ucode_rate |= iwl_rates[index].plcp;
  591. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  592. ucode_rate |= RATE_MCS_CCK_MSK;
  593. return ucode_rate;
  594. }
  595. if (is_ht(rate)) {
  596. if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
  597. IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
  598. index = IWL_LAST_HT_RATE;
  599. }
  600. ucode_rate |= RATE_MCS_HT_MSK;
  601. if (is_ht_siso(rate))
  602. ucode_rate |= iwl_rates[index].plcp_ht_siso;
  603. else if (is_ht_mimo2(rate))
  604. ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
  605. else
  606. WARN_ON_ONCE(1);
  607. } else if (is_vht(rate)) {
  608. if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
  609. IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
  610. index = IWL_LAST_VHT_RATE;
  611. }
  612. ucode_rate |= RATE_MCS_VHT_MSK;
  613. if (is_vht_siso(rate))
  614. ucode_rate |= iwl_rates[index].plcp_vht_siso;
  615. else if (is_vht_mimo2(rate))
  616. ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
  617. else
  618. WARN_ON_ONCE(1);
  619. } else {
  620. IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
  621. }
  622. ucode_rate |= rate->bw;
  623. if (rate->sgi)
  624. ucode_rate |= RATE_MCS_SGI_MSK;
  625. return ucode_rate;
  626. }
  627. /* Convert a ucode rate into an rs_rate object */
  628. static int rs_rate_from_ucode_rate(const u32 ucode_rate,
  629. enum ieee80211_band band,
  630. struct rs_rate *rate)
  631. {
  632. u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
  633. u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
  634. u8 nss;
  635. memset(rate, 0, sizeof(struct rs_rate));
  636. rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
  637. if (rate->index == IWL_RATE_INVALID) {
  638. rate->index = -1;
  639. return -EINVAL;
  640. }
  641. rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
  642. /* Legacy */
  643. if (!(ucode_rate & RATE_MCS_HT_MSK) &&
  644. !(ucode_rate & RATE_MCS_VHT_MSK)) {
  645. if (num_of_ant == 1) {
  646. if (band == IEEE80211_BAND_5GHZ)
  647. rate->type = LQ_LEGACY_A;
  648. else
  649. rate->type = LQ_LEGACY_G;
  650. }
  651. return 0;
  652. }
  653. /* HT or VHT */
  654. if (ucode_rate & RATE_MCS_SGI_MSK)
  655. rate->sgi = true;
  656. rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
  657. if (ucode_rate & RATE_MCS_HT_MSK) {
  658. nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
  659. RATE_HT_MCS_NSS_POS) + 1;
  660. if (nss == 1) {
  661. rate->type = LQ_HT_SISO;
  662. WARN_ON_ONCE(num_of_ant != 1);
  663. } else if (nss == 2) {
  664. rate->type = LQ_HT_MIMO2;
  665. WARN_ON_ONCE(num_of_ant != 2);
  666. } else {
  667. WARN_ON_ONCE(1);
  668. }
  669. } else if (ucode_rate & RATE_MCS_VHT_MSK) {
  670. nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
  671. RATE_VHT_MCS_NSS_POS) + 1;
  672. if (nss == 1) {
  673. rate->type = LQ_VHT_SISO;
  674. WARN_ON_ONCE(num_of_ant != 1);
  675. } else if (nss == 2) {
  676. rate->type = LQ_VHT_MIMO2;
  677. WARN_ON_ONCE(num_of_ant != 2);
  678. } else {
  679. WARN_ON_ONCE(1);
  680. }
  681. }
  682. WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
  683. WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
  684. !is_vht(rate));
  685. return 0;
  686. }
  687. /* switch to another antenna/antennas and return 1 */
  688. /* if no other valid antenna found, return 0 */
  689. static int rs_toggle_antenna(u32 valid_ant, u32 *ucode_rate,
  690. struct rs_rate *rate)
  691. {
  692. u8 new_ant_type;
  693. if (!rate->ant || rate->ant > ANT_ABC)
  694. return 0;
  695. if (!rs_is_valid_ant(valid_ant, rate->ant))
  696. return 0;
  697. new_ant_type = ant_toggle_lookup[rate->ant];
  698. while ((new_ant_type != rate->ant) &&
  699. !rs_is_valid_ant(valid_ant, new_ant_type))
  700. new_ant_type = ant_toggle_lookup[new_ant_type];
  701. if (new_ant_type == rate->ant)
  702. return 0;
  703. rate->ant = new_ant_type;
  704. /* TODO: get rid of ucode_rate here. This should handle only rs_rate */
  705. *ucode_rate &= ~RATE_MCS_ANT_ABC_MSK;
  706. *ucode_rate |= new_ant_type << RATE_MCS_ANT_POS;
  707. return 1;
  708. }
  709. static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
  710. struct rs_rate *rate)
  711. {
  712. if (is_legacy(rate))
  713. return lq_sta->active_legacy_rate;
  714. else if (is_siso(rate))
  715. return lq_sta->active_siso_rate;
  716. else if (is_mimo2(rate))
  717. return lq_sta->active_mimo2_rate;
  718. WARN_ON_ONCE(1);
  719. return 0;
  720. }
  721. static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
  722. int rate_type)
  723. {
  724. u8 high = IWL_RATE_INVALID;
  725. u8 low = IWL_RATE_INVALID;
  726. /* 802.11A or ht walks to the next literal adjacent rate in
  727. * the rate table */
  728. if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
  729. int i;
  730. u32 mask;
  731. /* Find the previous rate that is in the rate mask */
  732. i = index - 1;
  733. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  734. if (rate_mask & mask) {
  735. low = i;
  736. break;
  737. }
  738. }
  739. /* Find the next rate that is in the rate mask */
  740. i = index + 1;
  741. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  742. if (rate_mask & mask) {
  743. high = i;
  744. break;
  745. }
  746. }
  747. return (high << 8) | low;
  748. }
  749. low = index;
  750. while (low != IWL_RATE_INVALID) {
  751. low = iwl_rates[low].prev_rs;
  752. if (low == IWL_RATE_INVALID)
  753. break;
  754. if (rate_mask & (1 << low))
  755. break;
  756. IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
  757. }
  758. high = index;
  759. while (high != IWL_RATE_INVALID) {
  760. high = iwl_rates[high].next_rs;
  761. if (high == IWL_RATE_INVALID)
  762. break;
  763. if (rate_mask & (1 << high))
  764. break;
  765. IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
  766. }
  767. return (high << 8) | low;
  768. }
  769. static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
  770. struct rs_rate *rate,
  771. u8 scale_index, u8 ht_possible)
  772. {
  773. s32 low;
  774. u16 rate_mask;
  775. u16 high_low;
  776. u8 switch_to_legacy = 0;
  777. struct iwl_mvm *mvm = lq_sta->drv;
  778. /* check if we need to switch from HT to legacy rates.
  779. * assumption is that mandatory rates (1Mbps or 6Mbps)
  780. * are always supported (spec demand) */
  781. if (!is_legacy(rate) && (!ht_possible || !scale_index)) {
  782. switch_to_legacy = 1;
  783. WARN_ON_ONCE(scale_index < IWL_RATE_MCS_0_INDEX &&
  784. scale_index > IWL_RATE_MCS_9_INDEX);
  785. scale_index = rs_ht_to_legacy[scale_index];
  786. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  787. rate->type = LQ_LEGACY_A;
  788. else
  789. rate->type = LQ_LEGACY_G;
  790. if (num_of_ant(rate->ant) > 1)
  791. rate->ant =
  792. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  793. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  794. rate->sgi = false;
  795. }
  796. rate_mask = rs_get_supported_rates(lq_sta, rate);
  797. /* Mask with station rate restriction */
  798. if (is_legacy(rate)) {
  799. /* supp_rates has no CCK bits in A mode */
  800. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  801. rate_mask = (u16)(rate_mask &
  802. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  803. else
  804. rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
  805. }
  806. /* If we switched from HT to legacy, check current rate */
  807. if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
  808. low = scale_index;
  809. goto out;
  810. }
  811. high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
  812. rate->type);
  813. low = high_low & 0xff;
  814. if (low == IWL_RATE_INVALID)
  815. low = scale_index;
  816. out:
  817. rate->index = low;
  818. return ucode_rate_from_rs_rate(lq_sta->drv, rate);
  819. }
  820. /* Simple function to compare two rate scale table types */
  821. static inline bool rs_rate_match(struct rs_rate *a,
  822. struct rs_rate *b)
  823. {
  824. return (a->type == b->type) && (a->ant == b->ant) && (a->sgi == b->sgi);
  825. }
  826. static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
  827. {
  828. if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  829. return RATE_MCS_CHAN_WIDTH_40;
  830. else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  831. return RATE_MCS_CHAN_WIDTH_80;
  832. else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  833. return RATE_MCS_CHAN_WIDTH_160;
  834. return RATE_MCS_CHAN_WIDTH_20;
  835. }
  836. /*
  837. * mac80211 sends us Tx status
  838. */
  839. static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
  840. struct ieee80211_sta *sta, void *priv_sta,
  841. struct sk_buff *skb)
  842. {
  843. int legacy_success;
  844. int retries;
  845. int mac_index, i;
  846. struct iwl_lq_sta *lq_sta = priv_sta;
  847. struct iwl_lq_cmd *table;
  848. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  849. struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
  850. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  851. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  852. enum mac80211_rate_control_flags mac_flags;
  853. u32 ucode_rate;
  854. struct rs_rate rate;
  855. struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
  856. /* Treat uninitialized rate scaling data same as non-existing. */
  857. if (!lq_sta) {
  858. IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
  859. return;
  860. } else if (!lq_sta->drv) {
  861. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  862. return;
  863. }
  864. if (!ieee80211_is_data(hdr->frame_control) ||
  865. info->flags & IEEE80211_TX_CTL_NO_ACK)
  866. return;
  867. /* This packet was aggregated but doesn't carry status info */
  868. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  869. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  870. return;
  871. /*
  872. * Ignore this Tx frame response if its initial rate doesn't match
  873. * that of latest Link Quality command. There may be stragglers
  874. * from a previous Link Quality command, but we're no longer interested
  875. * in those; they're either from the "active" mode while we're trying
  876. * to check "search" mode, or a prior "search" mode after we've moved
  877. * to a new "search" mode (which might become the new "active" mode).
  878. */
  879. table = &lq_sta->lq;
  880. ucode_rate = le32_to_cpu(table->rs_table[0]);
  881. rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
  882. if (info->band == IEEE80211_BAND_5GHZ)
  883. rate.index -= IWL_FIRST_OFDM_RATE;
  884. mac_flags = info->status.rates[0].flags;
  885. mac_index = info->status.rates[0].idx;
  886. /* For HT packets, map MCS to PLCP */
  887. if (mac_flags & IEEE80211_TX_RC_MCS) {
  888. /* Remove # of streams */
  889. mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
  890. if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
  891. mac_index++;
  892. /*
  893. * mac80211 HT index is always zero-indexed; we need to move
  894. * HT OFDM rates after CCK rates in 2.4 GHz band
  895. */
  896. if (info->band == IEEE80211_BAND_2GHZ)
  897. mac_index += IWL_FIRST_OFDM_RATE;
  898. } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
  899. mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
  900. if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
  901. mac_index++;
  902. }
  903. /* Here we actually compare this rate to the latest LQ command */
  904. if ((mac_index < 0) ||
  905. (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
  906. (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
  907. (rate.ant != info->status.antenna) ||
  908. (!!(ucode_rate & RATE_MCS_HT_MSK) !=
  909. !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
  910. (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
  911. !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
  912. (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
  913. !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
  914. (rate.index != mac_index)) {
  915. IWL_DEBUG_RATE(mvm,
  916. "initial rate %d does not match %d (0x%x)\n",
  917. mac_index, rate.index, ucode_rate);
  918. /*
  919. * Since rates mis-match, the last LQ command may have failed.
  920. * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
  921. * ... driver.
  922. */
  923. lq_sta->missed_rate_counter++;
  924. if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
  925. lq_sta->missed_rate_counter = 0;
  926. IWL_DEBUG_RATE(mvm,
  927. "Too many rates mismatch. Send sync LQ. rs_state %d\n",
  928. lq_sta->rs_state);
  929. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  930. }
  931. /* Regardless, ignore this status info for outdated rate */
  932. return;
  933. } else
  934. /* Rate did match, so reset the missed_rate_counter */
  935. lq_sta->missed_rate_counter = 0;
  936. /* Figure out if rate scale algorithm is in active or search table */
  937. if (rs_rate_match(&rate,
  938. &(lq_sta->lq_info[lq_sta->active_tbl].rate))) {
  939. curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  940. other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  941. } else if (rs_rate_match(&rate,
  942. &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) {
  943. curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  944. other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  945. } else {
  946. IWL_DEBUG_RATE(mvm,
  947. "Neither active nor search matches tx rate\n");
  948. tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  949. rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
  950. tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  951. rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
  952. rs_dump_rate(mvm, &rate, "ACTUAL");
  953. /*
  954. * no matching table found, let's by-pass the data collection
  955. * and continue to perform rate scale to find the rate table
  956. */
  957. rs_stay_in_table(lq_sta, true);
  958. goto done;
  959. }
  960. /*
  961. * Updating the frame history depends on whether packets were
  962. * aggregated.
  963. *
  964. * For aggregation, all packets were transmitted at the same rate, the
  965. * first index into rate scale table.
  966. */
  967. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  968. ucode_rate = le32_to_cpu(table->rs_table[0]);
  969. rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
  970. rs_collect_tx_data(curr_tbl, rate.index,
  971. info->status.ampdu_len,
  972. info->status.ampdu_ack_len);
  973. /* Update success/fail counts if not searching for new mode */
  974. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  975. lq_sta->total_success += info->status.ampdu_ack_len;
  976. lq_sta->total_failed += (info->status.ampdu_len -
  977. info->status.ampdu_ack_len);
  978. }
  979. } else {
  980. /*
  981. * For legacy, update frame history with for each Tx retry.
  982. */
  983. retries = info->status.rates[0].count - 1;
  984. /* HW doesn't send more than 15 retries */
  985. retries = min(retries, 15);
  986. /* The last transmission may have been successful */
  987. legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  988. /* Collect data for each rate used during failed TX attempts */
  989. for (i = 0; i <= retries; ++i) {
  990. ucode_rate = le32_to_cpu(table->rs_table[i]);
  991. rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
  992. /*
  993. * Only collect stats if retried rate is in the same RS
  994. * table as active/search.
  995. */
  996. if (rs_rate_match(&rate, &curr_tbl->rate))
  997. tmp_tbl = curr_tbl;
  998. else if (rs_rate_match(&rate, &other_tbl->rate))
  999. tmp_tbl = other_tbl;
  1000. else {
  1001. IWL_DEBUG_RATE(mvm,
  1002. "Tx packet rate doesn't match ACTIVE or SEARCH tables\n");
  1003. rs_dump_rate(mvm, &rate, "Tx PACKET:");
  1004. rs_dump_rate(mvm, &curr_tbl->rate, "CURRENT:");
  1005. rs_dump_rate(mvm, &other_tbl->rate, "OTHER:");
  1006. continue;
  1007. }
  1008. rs_collect_tx_data(tmp_tbl, rate.index, 1,
  1009. i < retries ? 0 : legacy_success);
  1010. }
  1011. /* Update success/fail counts if not searching for new mode */
  1012. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1013. lq_sta->total_success += legacy_success;
  1014. lq_sta->total_failed += retries + (1 - legacy_success);
  1015. }
  1016. }
  1017. /* The last TX rate is cached in lq_sta; it's set in if/else above */
  1018. lq_sta->last_rate_n_flags = ucode_rate;
  1019. done:
  1020. /* See if there's a better rate or modulation mode to try. */
  1021. if (sta && sta->supp_rates[sband->band])
  1022. rs_rate_scale_perform(mvm, skb, sta, lq_sta);
  1023. }
  1024. /*
  1025. * Begin a period of staying with a selected modulation mode.
  1026. * Set "stay_in_tbl" flag to prevent any mode switches.
  1027. * Set frame tx success limits according to legacy vs. high-throughput,
  1028. * and reset overall (spanning all rates) tx success history statistics.
  1029. * These control how long we stay using same modulation mode before
  1030. * searching for a new mode.
  1031. */
  1032. static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
  1033. struct iwl_lq_sta *lq_sta)
  1034. {
  1035. IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
  1036. lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
  1037. if (is_legacy) {
  1038. lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  1039. lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  1040. lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
  1041. } else {
  1042. lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  1043. lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  1044. lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  1045. }
  1046. lq_sta->table_count = 0;
  1047. lq_sta->total_failed = 0;
  1048. lq_sta->total_success = 0;
  1049. lq_sta->flush_timer = jiffies;
  1050. lq_sta->visited_columns = 0;
  1051. }
  1052. static s32 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  1053. const struct rs_tx_column *column,
  1054. u32 bw)
  1055. {
  1056. /* Used to choose among HT tables */
  1057. s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
  1058. if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
  1059. column->mode != RS_SISO &&
  1060. column->mode != RS_MIMO2))
  1061. return expected_tpt_legacy;
  1062. /* Legacy rates have only one table */
  1063. if (column->mode == RS_LEGACY)
  1064. return expected_tpt_legacy;
  1065. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  1066. /* Choose among many HT tables depending on number of streams
  1067. * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
  1068. * status */
  1069. if (column->mode == RS_SISO) {
  1070. switch (bw) {
  1071. case RATE_MCS_CHAN_WIDTH_20:
  1072. ht_tbl_pointer = expected_tpt_siso_20MHz;
  1073. break;
  1074. case RATE_MCS_CHAN_WIDTH_40:
  1075. ht_tbl_pointer = expected_tpt_siso_40MHz;
  1076. break;
  1077. case RATE_MCS_CHAN_WIDTH_80:
  1078. ht_tbl_pointer = expected_tpt_siso_80MHz;
  1079. break;
  1080. default:
  1081. WARN_ON_ONCE(1);
  1082. }
  1083. } else if (column->mode == RS_MIMO2) {
  1084. switch (bw) {
  1085. case RATE_MCS_CHAN_WIDTH_20:
  1086. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  1087. break;
  1088. case RATE_MCS_CHAN_WIDTH_40:
  1089. ht_tbl_pointer = expected_tpt_mimo2_40MHz;
  1090. break;
  1091. case RATE_MCS_CHAN_WIDTH_80:
  1092. ht_tbl_pointer = expected_tpt_mimo2_80MHz;
  1093. break;
  1094. default:
  1095. WARN_ON_ONCE(1);
  1096. }
  1097. } else {
  1098. WARN_ON_ONCE(1);
  1099. }
  1100. if (!column->sgi && !lq_sta->is_agg) /* Normal */
  1101. return ht_tbl_pointer[0];
  1102. else if (column->sgi && !lq_sta->is_agg) /* SGI */
  1103. return ht_tbl_pointer[1];
  1104. else if (!column->sgi && lq_sta->is_agg) /* AGG */
  1105. return ht_tbl_pointer[2];
  1106. else /* AGG+SGI */
  1107. return ht_tbl_pointer[3];
  1108. }
  1109. static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  1110. struct iwl_scale_tbl_info *tbl)
  1111. {
  1112. struct rs_rate *rate = &tbl->rate;
  1113. const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
  1114. tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
  1115. }
  1116. /*
  1117. * Find starting rate for new "search" high-throughput mode of modulation.
  1118. * Goal is to find lowest expected rate (under perfect conditions) that is
  1119. * above the current measured throughput of "active" mode, to give new mode
  1120. * a fair chance to prove itself without too many challenges.
  1121. *
  1122. * This gets called when transitioning to more aggressive modulation
  1123. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  1124. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  1125. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  1126. * bit rate will typically need to increase, but not if performance was bad.
  1127. */
  1128. static s32 rs_get_best_rate(struct iwl_mvm *mvm,
  1129. struct iwl_lq_sta *lq_sta,
  1130. struct iwl_scale_tbl_info *tbl, /* "search" */
  1131. u16 rate_mask, s8 index)
  1132. {
  1133. /* "active" values */
  1134. struct iwl_scale_tbl_info *active_tbl =
  1135. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1136. s32 active_sr = active_tbl->win[index].success_ratio;
  1137. s32 active_tpt = active_tbl->expected_tpt[index];
  1138. /* expected "search" throughput */
  1139. s32 *tpt_tbl = tbl->expected_tpt;
  1140. s32 new_rate, high, low, start_hi;
  1141. u16 high_low;
  1142. s8 rate = index;
  1143. new_rate = high = low = start_hi = IWL_RATE_INVALID;
  1144. while (1) {
  1145. high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
  1146. tbl->rate.type);
  1147. low = high_low & 0xff;
  1148. high = (high_low >> 8) & 0xff;
  1149. /*
  1150. * Lower the "search" bit rate, to give new "search" mode
  1151. * approximately the same throughput as "active" if:
  1152. *
  1153. * 1) "Active" mode has been working modestly well (but not
  1154. * great), and expected "search" throughput (under perfect
  1155. * conditions) at candidate rate is above the actual
  1156. * measured "active" throughput (but less than expected
  1157. * "active" throughput under perfect conditions).
  1158. * OR
  1159. * 2) "Active" mode has been working perfectly or very well
  1160. * and expected "search" throughput (under perfect
  1161. * conditions) at candidate rate is above expected
  1162. * "active" throughput (under perfect conditions).
  1163. */
  1164. if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
  1165. ((active_sr > RS_SR_FORCE_DECREASE) &&
  1166. (active_sr <= IWL_RATE_HIGH_TH) &&
  1167. (tpt_tbl[rate] <= active_tpt))) ||
  1168. ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
  1169. (tpt_tbl[rate] > active_tpt))) {
  1170. /* (2nd or later pass)
  1171. * If we've already tried to raise the rate, and are
  1172. * now trying to lower it, use the higher rate. */
  1173. if (start_hi != IWL_RATE_INVALID) {
  1174. new_rate = start_hi;
  1175. break;
  1176. }
  1177. new_rate = rate;
  1178. /* Loop again with lower rate */
  1179. if (low != IWL_RATE_INVALID)
  1180. rate = low;
  1181. /* Lower rate not available, use the original */
  1182. else
  1183. break;
  1184. /* Else try to raise the "search" rate to match "active" */
  1185. } else {
  1186. /* (2nd or later pass)
  1187. * If we've already tried to lower the rate, and are
  1188. * now trying to raise it, use the lower rate. */
  1189. if (new_rate != IWL_RATE_INVALID)
  1190. break;
  1191. /* Loop again with higher rate */
  1192. else if (high != IWL_RATE_INVALID) {
  1193. start_hi = high;
  1194. rate = high;
  1195. /* Higher rate not available, use the original */
  1196. } else {
  1197. new_rate = rate;
  1198. break;
  1199. }
  1200. }
  1201. }
  1202. return new_rate;
  1203. }
  1204. static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
  1205. {
  1206. if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
  1207. return RATE_MCS_CHAN_WIDTH_80;
  1208. else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
  1209. return RATE_MCS_CHAN_WIDTH_40;
  1210. return RATE_MCS_CHAN_WIDTH_20;
  1211. }
  1212. /*
  1213. * Check whether we should continue using same modulation mode, or
  1214. * begin search for a new mode, based on:
  1215. * 1) # tx successes or failures while using this mode
  1216. * 2) # times calling this function
  1217. * 3) elapsed time in this mode (not used, for now)
  1218. */
  1219. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
  1220. {
  1221. struct iwl_scale_tbl_info *tbl;
  1222. int i;
  1223. int active_tbl;
  1224. int flush_interval_passed = 0;
  1225. struct iwl_mvm *mvm;
  1226. mvm = lq_sta->drv;
  1227. active_tbl = lq_sta->active_tbl;
  1228. tbl = &(lq_sta->lq_info[active_tbl]);
  1229. /* If we've been disallowing search, see if we should now allow it */
  1230. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1231. /* Elapsed time using current modulation mode */
  1232. if (lq_sta->flush_timer)
  1233. flush_interval_passed =
  1234. time_after(jiffies,
  1235. (unsigned long)(lq_sta->flush_timer +
  1236. RS_STAY_IN_COLUMN_TIMEOUT));
  1237. /*
  1238. * Check if we should allow search for new modulation mode.
  1239. * If many frames have failed or succeeded, or we've used
  1240. * this same modulation for a long time, allow search, and
  1241. * reset history stats that keep track of whether we should
  1242. * allow a new search. Also (below) reset all bitmaps and
  1243. * stats in active history.
  1244. */
  1245. if (force_search ||
  1246. (lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1247. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1248. ((!lq_sta->search_better_tbl) &&
  1249. (lq_sta->flush_timer) && (flush_interval_passed))) {
  1250. IWL_DEBUG_RATE(mvm,
  1251. "LQ: stay is expired %d %d %d\n",
  1252. lq_sta->total_failed,
  1253. lq_sta->total_success,
  1254. flush_interval_passed);
  1255. /* Allow search for new mode */
  1256. lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
  1257. IWL_DEBUG_RATE(mvm,
  1258. "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
  1259. lq_sta->total_failed = 0;
  1260. lq_sta->total_success = 0;
  1261. lq_sta->flush_timer = 0;
  1262. /* mark the current column as visited */
  1263. lq_sta->visited_columns = BIT(tbl->column);
  1264. /*
  1265. * Else if we've used this modulation mode enough repetitions
  1266. * (regardless of elapsed time or success/failure), reset
  1267. * history bitmaps and rate-specific stats for all rates in
  1268. * active table.
  1269. */
  1270. } else {
  1271. lq_sta->table_count++;
  1272. if (lq_sta->table_count >=
  1273. lq_sta->table_count_limit) {
  1274. lq_sta->table_count = 0;
  1275. IWL_DEBUG_RATE(mvm,
  1276. "LQ: stay in table clear win\n");
  1277. for (i = 0; i < IWL_RATE_COUNT; i++)
  1278. rs_rate_scale_clear_window(
  1279. &(tbl->win[i]));
  1280. }
  1281. }
  1282. /* If transitioning to allow "search", reset all history
  1283. * bitmaps and stats in active table (this will become the new
  1284. * "search" table). */
  1285. if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
  1286. IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
  1287. for (i = 0; i < IWL_RATE_COUNT; i++)
  1288. rs_rate_scale_clear_window(&(tbl->win[i]));
  1289. }
  1290. }
  1291. }
  1292. /*
  1293. * setup rate table in uCode
  1294. */
  1295. static void rs_update_rate_tbl(struct iwl_mvm *mvm,
  1296. struct ieee80211_sta *sta,
  1297. struct iwl_lq_sta *lq_sta,
  1298. struct rs_rate *rate)
  1299. {
  1300. u32 ucode_rate;
  1301. ucode_rate = ucode_rate_from_rs_rate(mvm, rate);
  1302. rs_fill_link_cmd(mvm, sta, lq_sta, ucode_rate);
  1303. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1304. }
  1305. static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
  1306. struct ieee80211_hdr *hdr)
  1307. {
  1308. u8 tid = IWL_MAX_TID_COUNT;
  1309. if (ieee80211_is_data_qos(hdr->frame_control)) {
  1310. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1311. tid = qc[0] & 0xf;
  1312. }
  1313. if (unlikely(tid > IWL_MAX_TID_COUNT))
  1314. tid = IWL_MAX_TID_COUNT;
  1315. return tid;
  1316. }
  1317. static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
  1318. struct iwl_lq_sta *lq_sta,
  1319. struct ieee80211_sta *sta,
  1320. struct iwl_scale_tbl_info *tbl)
  1321. {
  1322. int i, j, n;
  1323. enum rs_column next_col_id;
  1324. const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
  1325. const struct rs_tx_column *next_col;
  1326. allow_column_func_t allow_func;
  1327. u8 valid_ants = iwl_fw_valid_tx_ant(mvm->fw);
  1328. s32 *expected_tpt_tbl;
  1329. s32 tpt, max_expected_tpt;
  1330. for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
  1331. next_col_id = curr_col->next_columns[i];
  1332. if (next_col_id == RS_COLUMN_INVALID)
  1333. continue;
  1334. if (lq_sta->visited_columns & BIT(next_col_id)) {
  1335. IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
  1336. next_col_id);
  1337. continue;
  1338. }
  1339. next_col = &rs_tx_columns[next_col_id];
  1340. if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
  1341. IWL_DEBUG_RATE(mvm,
  1342. "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
  1343. next_col_id, valid_ants, next_col->ant);
  1344. continue;
  1345. }
  1346. for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
  1347. allow_func = next_col->checks[j];
  1348. if (allow_func && !allow_func(mvm, sta, tbl))
  1349. break;
  1350. }
  1351. if (j != MAX_COLUMN_CHECKS) {
  1352. IWL_DEBUG_RATE(mvm,
  1353. "Skip column %d: not allowed (check %d failed)\n",
  1354. next_col_id, j);
  1355. continue;
  1356. }
  1357. tpt = lq_sta->last_tpt / 100;
  1358. expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
  1359. tbl->rate.bw);
  1360. if (WARN_ON_ONCE(!expected_tpt_tbl))
  1361. continue;
  1362. max_expected_tpt = 0;
  1363. for (n = 0; n < IWL_RATE_COUNT; n++)
  1364. if (expected_tpt_tbl[n] > max_expected_tpt)
  1365. max_expected_tpt = expected_tpt_tbl[n];
  1366. if (tpt >= max_expected_tpt) {
  1367. IWL_DEBUG_RATE(mvm,
  1368. "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
  1369. next_col_id, max_expected_tpt, tpt);
  1370. continue;
  1371. }
  1372. break;
  1373. }
  1374. if (i == MAX_NEXT_COLUMNS)
  1375. return RS_COLUMN_INVALID;
  1376. IWL_DEBUG_RATE(mvm, "Found potential column %d\n", next_col_id);
  1377. return next_col_id;
  1378. }
  1379. static int rs_switch_to_column(struct iwl_mvm *mvm,
  1380. struct iwl_lq_sta *lq_sta,
  1381. struct ieee80211_sta *sta,
  1382. enum rs_column col_id)
  1383. {
  1384. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1385. struct iwl_scale_tbl_info *search_tbl =
  1386. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1387. struct rs_rate *rate = &search_tbl->rate;
  1388. const struct rs_tx_column *column = &rs_tx_columns[col_id];
  1389. const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
  1390. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1391. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1392. u16 rate_mask = 0;
  1393. u32 rate_idx = 0;
  1394. memcpy(search_tbl, tbl, sz);
  1395. rate->sgi = column->sgi;
  1396. rate->ant = column->ant;
  1397. if (column->mode == RS_LEGACY) {
  1398. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1399. rate->type = LQ_LEGACY_A;
  1400. else
  1401. rate->type = LQ_LEGACY_G;
  1402. rate_mask = lq_sta->active_legacy_rate;
  1403. } else if (column->mode == RS_SISO) {
  1404. rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
  1405. rate_mask = lq_sta->active_siso_rate;
  1406. } else if (column->mode == RS_MIMO2) {
  1407. rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
  1408. rate_mask = lq_sta->active_mimo2_rate;
  1409. } else {
  1410. WARN_ON_ONCE("Bad column mode");
  1411. }
  1412. rate->bw = rs_bw_from_sta_bw(sta);
  1413. search_tbl->column = col_id;
  1414. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1415. /* Get the best matching rate if we're changing modes. e.g.
  1416. * SISO->MIMO, LEGACY->SISO, MIMO->SISO
  1417. */
  1418. if (curr_column->mode != column->mode) {
  1419. rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
  1420. rate_mask, rate->index);
  1421. if ((rate_idx == IWL_RATE_INVALID) ||
  1422. !(BIT(rate_idx) & rate_mask)) {
  1423. IWL_DEBUG_RATE(mvm,
  1424. "can not switch with index %d"
  1425. " rate mask %x\n",
  1426. rate_idx, rate_mask);
  1427. goto err;
  1428. }
  1429. rate->index = rate_idx;
  1430. }
  1431. /* TODO: remove current_rate and keep using rs_rate all the way until
  1432. * we need to fill in the rs_table in the LQ command
  1433. */
  1434. search_tbl->current_rate = ucode_rate_from_rs_rate(mvm, rate);
  1435. IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
  1436. col_id, rate->index);
  1437. lq_sta->visited_columns |= BIT(col_id);
  1438. return 0;
  1439. err:
  1440. rate->type = LQ_NONE;
  1441. return -1;
  1442. }
  1443. /*
  1444. * Do rate scaling and search for new modulation mode.
  1445. */
  1446. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  1447. struct sk_buff *skb,
  1448. struct ieee80211_sta *sta,
  1449. struct iwl_lq_sta *lq_sta)
  1450. {
  1451. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1452. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1453. int low = IWL_RATE_INVALID;
  1454. int high = IWL_RATE_INVALID;
  1455. int index;
  1456. int i;
  1457. struct iwl_rate_scale_data *window = NULL;
  1458. int current_tpt = IWL_INVALID_VALUE;
  1459. int low_tpt = IWL_INVALID_VALUE;
  1460. int high_tpt = IWL_INVALID_VALUE;
  1461. u32 fail_count;
  1462. s8 scale_action = 0;
  1463. u16 rate_mask;
  1464. u8 update_lq = 0;
  1465. struct iwl_scale_tbl_info *tbl, *tbl1;
  1466. u16 rate_scale_index_msk = 0;
  1467. u8 active_tbl = 0;
  1468. u8 done_search = 0;
  1469. u16 high_low;
  1470. s32 sr;
  1471. u8 tid = IWL_MAX_TID_COUNT;
  1472. u8 prev_agg = lq_sta->is_agg;
  1473. struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
  1474. struct iwl_mvm_tid_data *tid_data;
  1475. struct rs_rate *rate;
  1476. /* Send management frames and NO_ACK data using lowest rate. */
  1477. /* TODO: this could probably be improved.. */
  1478. if (!ieee80211_is_data(hdr->frame_control) ||
  1479. info->flags & IEEE80211_TX_CTL_NO_ACK)
  1480. return;
  1481. lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
  1482. tid = rs_get_tid(lq_sta, hdr);
  1483. if ((tid != IWL_MAX_TID_COUNT) &&
  1484. (lq_sta->tx_agg_tid_en & (1 << tid))) {
  1485. tid_data = &sta_priv->tid_data[tid];
  1486. if (tid_data->state == IWL_AGG_OFF)
  1487. lq_sta->is_agg = 0;
  1488. else
  1489. lq_sta->is_agg = 1;
  1490. } else {
  1491. lq_sta->is_agg = 0;
  1492. }
  1493. /*
  1494. * Select rate-scale / modulation-mode table to work with in
  1495. * the rest of this function: "search" if searching for better
  1496. * modulation mode, or "active" if doing rate scaling within a mode.
  1497. */
  1498. if (!lq_sta->search_better_tbl)
  1499. active_tbl = lq_sta->active_tbl;
  1500. else
  1501. active_tbl = 1 - lq_sta->active_tbl;
  1502. tbl = &(lq_sta->lq_info[active_tbl]);
  1503. rate = &tbl->rate;
  1504. if (prev_agg != lq_sta->is_agg) {
  1505. IWL_DEBUG_RATE(mvm,
  1506. "Aggregation changed: prev %d current %d. Update expected TPT table\n",
  1507. prev_agg, lq_sta->is_agg);
  1508. rs_set_expected_tpt_table(lq_sta, tbl);
  1509. }
  1510. /* current tx rate */
  1511. index = lq_sta->last_txrate_idx;
  1512. /* rates available for this association, and for modulation mode */
  1513. rate_mask = rs_get_supported_rates(lq_sta, rate);
  1514. /* mask with station rate restriction */
  1515. if (is_legacy(rate)) {
  1516. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1517. /* supp_rates has no CCK bits in A mode */
  1518. rate_scale_index_msk = (u16) (rate_mask &
  1519. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  1520. else
  1521. rate_scale_index_msk = (u16) (rate_mask &
  1522. lq_sta->supp_rates);
  1523. } else {
  1524. rate_scale_index_msk = rate_mask;
  1525. }
  1526. if (!rate_scale_index_msk)
  1527. rate_scale_index_msk = rate_mask;
  1528. if (!((BIT(index) & rate_scale_index_msk))) {
  1529. IWL_ERR(mvm, "Current Rate is not valid\n");
  1530. if (lq_sta->search_better_tbl) {
  1531. /* revert to active table if search table is not valid*/
  1532. rate->type = LQ_NONE;
  1533. lq_sta->search_better_tbl = 0;
  1534. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1535. /* get "active" rate info */
  1536. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1537. tbl->rate.index = index;
  1538. rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
  1539. }
  1540. return;
  1541. }
  1542. /* Get expected throughput table and history window for current rate */
  1543. if (!tbl->expected_tpt) {
  1544. IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
  1545. return;
  1546. }
  1547. /* force user max rate if set by user */
  1548. if ((lq_sta->max_rate_idx != -1) &&
  1549. (lq_sta->max_rate_idx < index)) {
  1550. index = lq_sta->max_rate_idx;
  1551. update_lq = 1;
  1552. window = &(tbl->win[index]);
  1553. IWL_DEBUG_RATE(mvm,
  1554. "Forcing user max rate %d\n",
  1555. index);
  1556. goto lq_update;
  1557. }
  1558. window = &(tbl->win[index]);
  1559. /*
  1560. * If there is not enough history to calculate actual average
  1561. * throughput, keep analyzing results of more tx frames, without
  1562. * changing rate or mode (bypass most of the rest of this function).
  1563. * Set up new rate table in uCode only if old rate is not supported
  1564. * in current association (use new rate found above).
  1565. */
  1566. fail_count = window->counter - window->success_counter;
  1567. if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1568. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
  1569. IWL_DEBUG_RATE(mvm,
  1570. "(%s: %d): Test Window: succ %d total %d\n",
  1571. rs_pretty_lq_type(rate->type),
  1572. index, window->success_counter, window->counter);
  1573. /* Can't calculate this yet; not enough history */
  1574. window->average_tpt = IWL_INVALID_VALUE;
  1575. /* Should we stay with this modulation mode,
  1576. * or search for a new one? */
  1577. rs_stay_in_table(lq_sta, false);
  1578. goto out;
  1579. }
  1580. /* Else we have enough samples; calculate estimate of
  1581. * actual average throughput */
  1582. if (window->average_tpt != ((window->success_ratio *
  1583. tbl->expected_tpt[index] + 64) / 128)) {
  1584. window->average_tpt = ((window->success_ratio *
  1585. tbl->expected_tpt[index] + 64) / 128);
  1586. }
  1587. /* If we are searching for better modulation mode, check success. */
  1588. if (lq_sta->search_better_tbl) {
  1589. /* If good success, continue using the "search" mode;
  1590. * no need to send new link quality command, since we're
  1591. * continuing to use the setup that we've been trying. */
  1592. if (window->average_tpt > lq_sta->last_tpt) {
  1593. IWL_DEBUG_RATE(mvm,
  1594. "SWITCHING TO NEW TABLE SR: %d "
  1595. "cur-tpt %d old-tpt %d\n",
  1596. window->success_ratio,
  1597. window->average_tpt,
  1598. lq_sta->last_tpt);
  1599. /* Swap tables; "search" becomes "active" */
  1600. lq_sta->active_tbl = active_tbl;
  1601. current_tpt = window->average_tpt;
  1602. /* Else poor success; go back to mode in "active" table */
  1603. } else {
  1604. IWL_DEBUG_RATE(mvm,
  1605. "GOING BACK TO THE OLD TABLE: SR %d "
  1606. "cur-tpt %d old-tpt %d\n",
  1607. window->success_ratio,
  1608. window->average_tpt,
  1609. lq_sta->last_tpt);
  1610. /* Nullify "search" table */
  1611. rate->type = LQ_NONE;
  1612. /* Revert to "active" table */
  1613. active_tbl = lq_sta->active_tbl;
  1614. tbl = &(lq_sta->lq_info[active_tbl]);
  1615. /* Revert to "active" rate and throughput info */
  1616. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1617. current_tpt = lq_sta->last_tpt;
  1618. /* Need to set up a new rate table in uCode */
  1619. update_lq = 1;
  1620. }
  1621. /* Either way, we've made a decision; modulation mode
  1622. * search is done, allow rate adjustment next time. */
  1623. lq_sta->search_better_tbl = 0;
  1624. done_search = 1; /* Don't switch modes below! */
  1625. goto lq_update;
  1626. }
  1627. /* (Else) not in search of better modulation mode, try for better
  1628. * starting rate, while staying in this mode. */
  1629. high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
  1630. rate->type);
  1631. low = high_low & 0xff;
  1632. high = (high_low >> 8) & 0xff;
  1633. /* If user set max rate, dont allow higher than user constrain */
  1634. if ((lq_sta->max_rate_idx != -1) &&
  1635. (lq_sta->max_rate_idx < high))
  1636. high = IWL_RATE_INVALID;
  1637. sr = window->success_ratio;
  1638. /* Collect measured throughputs for current and adjacent rates */
  1639. current_tpt = window->average_tpt;
  1640. if (low != IWL_RATE_INVALID)
  1641. low_tpt = tbl->win[low].average_tpt;
  1642. if (high != IWL_RATE_INVALID)
  1643. high_tpt = tbl->win[high].average_tpt;
  1644. IWL_DEBUG_RATE(mvm,
  1645. "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
  1646. rs_pretty_lq_type(rate->type), index, current_tpt, sr,
  1647. low, high, low_tpt, high_tpt);
  1648. scale_action = 0;
  1649. /* Too many failures, decrease rate */
  1650. if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
  1651. IWL_DEBUG_RATE(mvm,
  1652. "decrease rate because of low SR\n");
  1653. scale_action = -1;
  1654. /* No throughput measured yet for adjacent rates; try increase. */
  1655. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  1656. (high_tpt == IWL_INVALID_VALUE)) {
  1657. if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH) {
  1658. IWL_DEBUG_RATE(mvm,
  1659. "Good SR and no high rate measurement. "
  1660. "Increase rate\n");
  1661. scale_action = 1;
  1662. } else if (low != IWL_RATE_INVALID) {
  1663. IWL_DEBUG_RATE(mvm,
  1664. "Remain in current rate\n");
  1665. scale_action = 0;
  1666. }
  1667. }
  1668. /* Both adjacent throughputs are measured, but neither one has better
  1669. * throughput; we're using the best rate, don't change it! */
  1670. else if ((low_tpt != IWL_INVALID_VALUE) &&
  1671. (high_tpt != IWL_INVALID_VALUE) &&
  1672. (low_tpt < current_tpt) &&
  1673. (high_tpt < current_tpt)) {
  1674. IWL_DEBUG_RATE(mvm,
  1675. "Both high and low are worse. "
  1676. "Maintain rate\n");
  1677. scale_action = 0;
  1678. }
  1679. /* At least one adjacent rate's throughput is measured,
  1680. * and may have better performance. */
  1681. else {
  1682. /* Higher adjacent rate's throughput is measured */
  1683. if (high_tpt != IWL_INVALID_VALUE) {
  1684. /* Higher rate has better throughput */
  1685. if (high_tpt > current_tpt &&
  1686. sr >= IWL_RATE_INCREASE_TH) {
  1687. IWL_DEBUG_RATE(mvm,
  1688. "Higher rate is better and good "
  1689. "SR. Increate rate\n");
  1690. scale_action = 1;
  1691. } else {
  1692. IWL_DEBUG_RATE(mvm,
  1693. "Higher rate isn't better OR "
  1694. "no good SR. Maintain rate\n");
  1695. scale_action = 0;
  1696. }
  1697. /* Lower adjacent rate's throughput is measured */
  1698. } else if (low_tpt != IWL_INVALID_VALUE) {
  1699. /* Lower rate has better throughput */
  1700. if (low_tpt > current_tpt) {
  1701. IWL_DEBUG_RATE(mvm,
  1702. "Lower rate is better. "
  1703. "Decrease rate\n");
  1704. scale_action = -1;
  1705. } else if (sr >= IWL_RATE_INCREASE_TH) {
  1706. IWL_DEBUG_RATE(mvm,
  1707. "Lower rate isn't better and "
  1708. "good SR. Increase rate\n");
  1709. scale_action = 1;
  1710. }
  1711. }
  1712. }
  1713. /* Sanity check; asked for decrease, but success rate or throughput
  1714. * has been good at old rate. Don't change it. */
  1715. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  1716. ((sr > IWL_RATE_HIGH_TH) ||
  1717. (current_tpt > (100 * tbl->expected_tpt[low])))) {
  1718. IWL_DEBUG_RATE(mvm,
  1719. "Sanity check failed. Maintain rate\n");
  1720. scale_action = 0;
  1721. }
  1722. /* Force a search in case BT doesn't like us being in MIMO */
  1723. if (is_mimo(rate) &&
  1724. !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
  1725. IWL_DEBUG_RATE(mvm,
  1726. "BT Coex forbids MIMO. Search for new config\n");
  1727. rs_stay_in_table(lq_sta, true);
  1728. goto lq_update;
  1729. }
  1730. switch (scale_action) {
  1731. case -1:
  1732. /* Decrease starting rate, update uCode's rate table */
  1733. if (low != IWL_RATE_INVALID) {
  1734. update_lq = 1;
  1735. index = low;
  1736. } else {
  1737. IWL_DEBUG_RATE(mvm,
  1738. "At the bottom rate. Can't decrease\n");
  1739. }
  1740. break;
  1741. case 1:
  1742. /* Increase starting rate, update uCode's rate table */
  1743. if (high != IWL_RATE_INVALID) {
  1744. update_lq = 1;
  1745. index = high;
  1746. } else {
  1747. IWL_DEBUG_RATE(mvm,
  1748. "At the top rate. Can't increase\n");
  1749. }
  1750. break;
  1751. case 0:
  1752. /* No change */
  1753. default:
  1754. break;
  1755. }
  1756. lq_update:
  1757. /* Replace uCode's rate table for the destination station. */
  1758. if (update_lq) {
  1759. tbl->rate.index = index;
  1760. rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
  1761. }
  1762. rs_stay_in_table(lq_sta, false);
  1763. /*
  1764. * Search for new modulation mode if we're:
  1765. * 1) Not changing rates right now
  1766. * 2) Not just finishing up a search
  1767. * 3) Allowing a new search
  1768. */
  1769. if (!update_lq && !done_search &&
  1770. lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
  1771. && window->counter) {
  1772. enum rs_column next_column;
  1773. /* Save current throughput to compare with "search" throughput*/
  1774. lq_sta->last_tpt = current_tpt;
  1775. IWL_DEBUG_RATE(mvm,
  1776. "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
  1777. update_lq, done_search, lq_sta->rs_state,
  1778. window->counter);
  1779. next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
  1780. if (next_column != RS_COLUMN_INVALID) {
  1781. int ret = rs_switch_to_column(mvm, lq_sta, sta,
  1782. next_column);
  1783. if (!ret)
  1784. lq_sta->search_better_tbl = 1;
  1785. } else {
  1786. IWL_DEBUG_RATE(mvm,
  1787. "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
  1788. lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
  1789. }
  1790. /* If new "search" mode was selected, set up in uCode table */
  1791. if (lq_sta->search_better_tbl) {
  1792. /* Access the "search" table, clear its history. */
  1793. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1794. for (i = 0; i < IWL_RATE_COUNT; i++)
  1795. rs_rate_scale_clear_window(&(tbl->win[i]));
  1796. /* Use new "search" start rate */
  1797. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1798. rs_dump_rate(mvm, &tbl->rate,
  1799. "Switch to SEARCH TABLE:");
  1800. rs_fill_link_cmd(mvm, sta, lq_sta, tbl->current_rate);
  1801. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1802. } else {
  1803. done_search = 1;
  1804. }
  1805. }
  1806. if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
  1807. /* If the "active" (non-search) mode was legacy,
  1808. * and we've tried switching antennas,
  1809. * but we haven't been able to try HT modes (not available),
  1810. * stay with best antenna legacy modulation for a while
  1811. * before next round of mode comparisons. */
  1812. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1813. if (is_legacy(&tbl1->rate) && !sta->ht_cap.ht_supported) {
  1814. IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
  1815. rs_set_stay_in_table(mvm, 1, lq_sta);
  1816. } else {
  1817. /* If we're in an HT mode, and all 3 mode switch actions
  1818. * have been tried and compared, stay in this best modulation
  1819. * mode for a while before next round of mode comparisons. */
  1820. if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
  1821. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  1822. (tid != IWL_MAX_TID_COUNT)) {
  1823. tid_data = &sta_priv->tid_data[tid];
  1824. if (tid_data->state == IWL_AGG_OFF) {
  1825. IWL_DEBUG_RATE(mvm,
  1826. "try to aggregate tid %d\n",
  1827. tid);
  1828. rs_tl_turn_on_agg(mvm, tid,
  1829. lq_sta, sta);
  1830. }
  1831. }
  1832. rs_set_stay_in_table(mvm, 0, lq_sta);
  1833. }
  1834. }
  1835. out:
  1836. tbl->rate.index = index;
  1837. tbl->current_rate = ucode_rate_from_rs_rate(mvm, &tbl->rate);
  1838. lq_sta->last_txrate_idx = index;
  1839. }
  1840. /**
  1841. * rs_initialize_lq - Initialize a station's hardware rate table
  1842. *
  1843. * The uCode's station table contains a table of fallback rates
  1844. * for automatic fallback during transmission.
  1845. *
  1846. * NOTE: This sets up a default set of values. These will be replaced later
  1847. * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
  1848. * rc80211_simple.
  1849. *
  1850. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  1851. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  1852. * which requires station table entry to exist).
  1853. */
  1854. static void rs_initialize_lq(struct iwl_mvm *mvm,
  1855. struct ieee80211_sta *sta,
  1856. struct iwl_lq_sta *lq_sta,
  1857. enum ieee80211_band band,
  1858. bool init)
  1859. {
  1860. struct iwl_scale_tbl_info *tbl;
  1861. struct rs_rate *rate;
  1862. int i;
  1863. u32 ucode_rate;
  1864. u8 active_tbl = 0;
  1865. u8 valid_tx_ant;
  1866. if (!sta || !lq_sta)
  1867. return;
  1868. i = lq_sta->last_txrate_idx;
  1869. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1870. if (!lq_sta->search_better_tbl)
  1871. active_tbl = lq_sta->active_tbl;
  1872. else
  1873. active_tbl = 1 - lq_sta->active_tbl;
  1874. tbl = &(lq_sta->lq_info[active_tbl]);
  1875. rate = &tbl->rate;
  1876. if ((i < 0) || (i >= IWL_RATE_COUNT))
  1877. i = 0;
  1878. rate->index = i;
  1879. rate->ant = first_antenna(valid_tx_ant);
  1880. rate->sgi = false;
  1881. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  1882. if (band == IEEE80211_BAND_5GHZ)
  1883. rate->type = LQ_LEGACY_A;
  1884. else
  1885. rate->type = LQ_LEGACY_G;
  1886. ucode_rate = ucode_rate_from_rs_rate(mvm, rate);
  1887. tbl->current_rate = ucode_rate;
  1888. WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
  1889. if (rate->ant == ANT_A)
  1890. tbl->column = RS_COLUMN_LEGACY_ANT_A;
  1891. else
  1892. tbl->column = RS_COLUMN_LEGACY_ANT_B;
  1893. rs_set_expected_tpt_table(lq_sta, tbl);
  1894. rs_fill_link_cmd(NULL, NULL, lq_sta, ucode_rate);
  1895. /* TODO restore station should remember the lq cmd */
  1896. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
  1897. }
  1898. static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
  1899. struct ieee80211_tx_rate_control *txrc)
  1900. {
  1901. struct sk_buff *skb = txrc->skb;
  1902. struct ieee80211_supported_band *sband = txrc->sband;
  1903. struct iwl_op_mode *op_mode __maybe_unused =
  1904. (struct iwl_op_mode *)mvm_r;
  1905. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  1906. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1907. struct iwl_lq_sta *lq_sta = mvm_sta;
  1908. /* Get max rate if user set max rate */
  1909. if (lq_sta) {
  1910. lq_sta->max_rate_idx = txrc->max_rate_idx;
  1911. if ((sband->band == IEEE80211_BAND_5GHZ) &&
  1912. (lq_sta->max_rate_idx != -1))
  1913. lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
  1914. if ((lq_sta->max_rate_idx < 0) ||
  1915. (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
  1916. lq_sta->max_rate_idx = -1;
  1917. }
  1918. /* Treat uninitialized rate scaling data same as non-existing. */
  1919. if (lq_sta && !lq_sta->drv) {
  1920. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  1921. mvm_sta = NULL;
  1922. }
  1923. /* Send management frames and NO_ACK data using lowest rate. */
  1924. if (rate_control_send_low(sta, mvm_sta, txrc))
  1925. return;
  1926. iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
  1927. info->band, &info->control.rates[0]);
  1928. info->control.rates[0].count = 1;
  1929. }
  1930. static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
  1931. gfp_t gfp)
  1932. {
  1933. struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  1934. struct iwl_op_mode *op_mode __maybe_unused =
  1935. (struct iwl_op_mode *)mvm_rate;
  1936. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  1937. IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
  1938. return &sta_priv->lq_sta;
  1939. }
  1940. static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
  1941. int nss)
  1942. {
  1943. u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
  1944. (0x3 << (2 * (nss - 1)));
  1945. rx_mcs >>= (2 * (nss - 1));
  1946. if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
  1947. return IWL_RATE_MCS_7_INDEX;
  1948. else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
  1949. return IWL_RATE_MCS_8_INDEX;
  1950. else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
  1951. return IWL_RATE_MCS_9_INDEX;
  1952. WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
  1953. return -1;
  1954. }
  1955. static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
  1956. struct ieee80211_sta_vht_cap *vht_cap,
  1957. struct iwl_lq_sta *lq_sta)
  1958. {
  1959. int i;
  1960. int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
  1961. if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
  1962. for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
  1963. if (i == IWL_RATE_9M_INDEX)
  1964. continue;
  1965. /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
  1966. if (i == IWL_RATE_MCS_9_INDEX &&
  1967. sta->bandwidth == IEEE80211_STA_RX_BW_20)
  1968. continue;
  1969. lq_sta->active_siso_rate |= BIT(i);
  1970. }
  1971. }
  1972. if (sta->rx_nss < 2)
  1973. return;
  1974. highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
  1975. if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
  1976. for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
  1977. if (i == IWL_RATE_9M_INDEX)
  1978. continue;
  1979. /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
  1980. if (i == IWL_RATE_MCS_9_INDEX &&
  1981. sta->bandwidth == IEEE80211_STA_RX_BW_20)
  1982. continue;
  1983. lq_sta->active_mimo2_rate |= BIT(i);
  1984. }
  1985. }
  1986. }
  1987. /*
  1988. * Called after adding a new station to initialize rate scaling
  1989. */
  1990. void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1991. enum ieee80211_band band, bool init)
  1992. {
  1993. int i, j;
  1994. struct ieee80211_hw *hw = mvm->hw;
  1995. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1996. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  1997. struct iwl_mvm_sta *sta_priv;
  1998. struct iwl_lq_sta *lq_sta;
  1999. struct ieee80211_supported_band *sband;
  2000. unsigned long supp; /* must be unsigned long for for_each_set_bit */
  2001. sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  2002. lq_sta = &sta_priv->lq_sta;
  2003. memset(lq_sta, 0, sizeof(*lq_sta));
  2004. sband = hw->wiphy->bands[band];
  2005. lq_sta->lq.sta_id = sta_priv->sta_id;
  2006. for (j = 0; j < LQ_SIZE; j++)
  2007. for (i = 0; i < IWL_RATE_COUNT; i++)
  2008. rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
  2009. lq_sta->flush_timer = 0;
  2010. lq_sta->supp_rates = sta->supp_rates[sband->band];
  2011. IWL_DEBUG_RATE(mvm,
  2012. "LQ: *** rate scale station global init for station %d ***\n",
  2013. sta_priv->sta_id);
  2014. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2015. * the lowest or the highest rate.. Could consider using RSSI from
  2016. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2017. * after assoc.. */
  2018. lq_sta->max_rate_idx = -1;
  2019. lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
  2020. lq_sta->band = sband->band;
  2021. /*
  2022. * active legacy rates as per supported rates bitmap
  2023. */
  2024. supp = sta->supp_rates[sband->band];
  2025. lq_sta->active_legacy_rate = 0;
  2026. for_each_set_bit(i, &supp, BITS_PER_LONG)
  2027. lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
  2028. /* TODO: should probably account for rx_highest for both HT/VHT */
  2029. if (!vht_cap || !vht_cap->vht_supported) {
  2030. /* active_siso_rate mask includes 9 MBits (bit 5),
  2031. * and CCK (bits 0-3), supp_rates[] does not;
  2032. * shift to convert format, force 9 MBits off.
  2033. */
  2034. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2035. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2036. lq_sta->active_siso_rate &= ~((u16)0x2);
  2037. lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
  2038. /* Same here */
  2039. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2040. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2041. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2042. lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
  2043. lq_sta->is_vht = false;
  2044. } else {
  2045. rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
  2046. lq_sta->is_vht = true;
  2047. }
  2048. IWL_DEBUG_RATE(mvm,
  2049. "SISO-RATE=%X MIMO2-RATE=%X VHT=%d\n",
  2050. lq_sta->active_siso_rate,
  2051. lq_sta->active_mimo2_rate,
  2052. lq_sta->is_vht);
  2053. /* These values will be overridden later */
  2054. lq_sta->lq.single_stream_ant_msk =
  2055. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  2056. lq_sta->lq.dual_stream_ant_msk = ANT_AB;
  2057. /* as default allow aggregation for all tids */
  2058. lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
  2059. lq_sta->drv = mvm;
  2060. /* Set last_txrate_idx to lowest rate */
  2061. lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
  2062. if (sband->band == IEEE80211_BAND_5GHZ)
  2063. lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  2064. lq_sta->is_agg = 0;
  2065. #ifdef CONFIG_MAC80211_DEBUGFS
  2066. lq_sta->dbg_fixed_rate = 0;
  2067. #endif
  2068. rs_initialize_lq(mvm, sta, lq_sta, band, init);
  2069. }
  2070. static void rs_rate_update(void *mvm_r,
  2071. struct ieee80211_supported_band *sband,
  2072. struct cfg80211_chan_def *chandef,
  2073. struct ieee80211_sta *sta, void *priv_sta,
  2074. u32 changed)
  2075. {
  2076. u8 tid;
  2077. struct iwl_op_mode *op_mode =
  2078. (struct iwl_op_mode *)mvm_r;
  2079. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  2080. /* Stop any ongoing aggregations as rs starts off assuming no agg */
  2081. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
  2082. ieee80211_stop_tx_ba_session(sta, tid);
  2083. iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
  2084. }
  2085. static void rs_fill_link_cmd(struct iwl_mvm *mvm,
  2086. struct ieee80211_sta *sta,
  2087. struct iwl_lq_sta *lq_sta, u32 new_rate)
  2088. {
  2089. struct rs_rate rate;
  2090. int index = 0;
  2091. int repeat_rate = 0;
  2092. u8 ant_toggle_cnt = 0;
  2093. u8 use_ht_possible = 1;
  2094. u8 valid_tx_ant = 0;
  2095. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2096. /* Override starting rate (index 0) if needed for debug purposes */
  2097. rs_dbgfs_set_mcs(lq_sta, &new_rate);
  2098. rs_rate_from_ucode_rate(new_rate, lq_sta->band, &rate);
  2099. /* How many times should we repeat the initial rate? */
  2100. if (is_legacy(&rate)) {
  2101. ant_toggle_cnt = 1;
  2102. repeat_rate = IWL_NUMBER_TRY;
  2103. } else {
  2104. repeat_rate = min(IWL_HT_NUMBER_TRY,
  2105. LINK_QUAL_AGG_DISABLE_START_DEF - 1);
  2106. }
  2107. lq_cmd->mimo_delim = is_mimo(&rate) ? 1 : 0;
  2108. /* Fill 1st table entry (index 0) */
  2109. lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
  2110. if (num_of_ant(rate.ant) == 1)
  2111. lq_cmd->single_stream_ant_msk = rate.ant;
  2112. /* otherwise we don't modify the existing value */
  2113. index++;
  2114. repeat_rate--;
  2115. if (mvm)
  2116. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2117. /* Fill rest of rate table */
  2118. while (index < LINK_QUAL_MAX_RETRY_NUM) {
  2119. /* Repeat initial/next rate.
  2120. * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
  2121. * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
  2122. while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
  2123. if (is_legacy(&rate)) {
  2124. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2125. ant_toggle_cnt++;
  2126. else if (mvm &&
  2127. rs_toggle_antenna(valid_tx_ant,
  2128. &new_rate, &rate))
  2129. ant_toggle_cnt = 1;
  2130. }
  2131. /* Override next rate if needed for debug purposes */
  2132. rs_dbgfs_set_mcs(lq_sta, &new_rate);
  2133. /* Fill next table entry */
  2134. lq_cmd->rs_table[index] =
  2135. cpu_to_le32(new_rate);
  2136. repeat_rate--;
  2137. index++;
  2138. }
  2139. rs_rate_from_ucode_rate(new_rate, lq_sta->band, &rate);
  2140. /* Indicate to uCode which entries might be MIMO.
  2141. * If initial rate was MIMO, this will finally end up
  2142. * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  2143. if (is_mimo(&rate))
  2144. lq_cmd->mimo_delim = index;
  2145. /* Get next rate */
  2146. new_rate = rs_get_lower_rate(lq_sta, &rate, rate.index,
  2147. use_ht_possible);
  2148. /* How many times should we repeat the next rate? */
  2149. if (is_legacy(&rate)) {
  2150. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2151. ant_toggle_cnt++;
  2152. else if (mvm &&
  2153. rs_toggle_antenna(valid_tx_ant,
  2154. &new_rate, &rate))
  2155. ant_toggle_cnt = 1;
  2156. repeat_rate = IWL_NUMBER_TRY;
  2157. } else {
  2158. repeat_rate = IWL_HT_NUMBER_TRY;
  2159. }
  2160. /* Don't allow HT rates after next pass.
  2161. * rs_get_lower_rate() will change type to LQ_LEGACY_A
  2162. * or LQ_LEGACY_G.
  2163. */
  2164. use_ht_possible = 0;
  2165. /* Override next rate if needed for debug purposes */
  2166. rs_dbgfs_set_mcs(lq_sta, &new_rate);
  2167. /* Fill next table entry */
  2168. lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
  2169. index++;
  2170. repeat_rate--;
  2171. }
  2172. lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2173. lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2174. lq_cmd->agg_time_limit =
  2175. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2176. if (sta)
  2177. lq_cmd->agg_time_limit =
  2178. cpu_to_le16(iwl_mvm_bt_coex_agg_time_limit(mvm, sta));
  2179. }
  2180. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2181. {
  2182. return hw->priv;
  2183. }
  2184. /* rate scale requires free function to be implemented */
  2185. static void rs_free(void *mvm_rate)
  2186. {
  2187. return;
  2188. }
  2189. static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
  2190. void *mvm_sta)
  2191. {
  2192. struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
  2193. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2194. IWL_DEBUG_RATE(mvm, "enter\n");
  2195. IWL_DEBUG_RATE(mvm, "leave\n");
  2196. }
  2197. #ifdef CONFIG_MAC80211_DEBUGFS
  2198. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  2199. u32 *rate_n_flags)
  2200. {
  2201. struct iwl_mvm *mvm;
  2202. u8 valid_tx_ant;
  2203. u8 ant_sel_tx;
  2204. mvm = lq_sta->drv;
  2205. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2206. if (lq_sta->dbg_fixed_rate) {
  2207. ant_sel_tx =
  2208. ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
  2209. >> RATE_MCS_ANT_POS);
  2210. if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
  2211. *rate_n_flags = lq_sta->dbg_fixed_rate;
  2212. } else {
  2213. lq_sta->dbg_fixed_rate = 0;
  2214. IWL_ERR(mvm,
  2215. "Invalid antenna selection 0x%X, Valid is 0x%X\n",
  2216. ant_sel_tx, valid_tx_ant);
  2217. IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
  2218. }
  2219. }
  2220. }
  2221. static int rs_pretty_print_rate(char *buf, const u32 rate)
  2222. {
  2223. char *type, *bw;
  2224. u8 mcs = 0, nss = 0;
  2225. u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
  2226. if (!(rate & RATE_MCS_HT_MSK) &&
  2227. !(rate & RATE_MCS_VHT_MSK)) {
  2228. int index = iwl_hwrate_to_plcp_idx(rate);
  2229. return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
  2230. rs_pretty_ant(ant), iwl_rate_mcs[index].mbps);
  2231. }
  2232. if (rate & RATE_MCS_VHT_MSK) {
  2233. type = "VHT";
  2234. mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
  2235. nss = ((rate & RATE_VHT_MCS_NSS_MSK)
  2236. >> RATE_VHT_MCS_NSS_POS) + 1;
  2237. } else if (rate & RATE_MCS_HT_MSK) {
  2238. type = "HT";
  2239. mcs = rate & RATE_HT_MCS_INDEX_MSK;
  2240. } else {
  2241. type = "Unknown"; /* shouldn't happen */
  2242. }
  2243. switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
  2244. case RATE_MCS_CHAN_WIDTH_20:
  2245. bw = "20Mhz";
  2246. break;
  2247. case RATE_MCS_CHAN_WIDTH_40:
  2248. bw = "40Mhz";
  2249. break;
  2250. case RATE_MCS_CHAN_WIDTH_80:
  2251. bw = "80Mhz";
  2252. break;
  2253. case RATE_MCS_CHAN_WIDTH_160:
  2254. bw = "160Mhz";
  2255. break;
  2256. default:
  2257. bw = "BAD BW";
  2258. }
  2259. return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
  2260. type, rs_pretty_ant(ant), bw, mcs, nss,
  2261. (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
  2262. (rate & RATE_MCS_STBC_MSK) ? "STBC " : "",
  2263. (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
  2264. (rate & RATE_MCS_BF_MSK) ? "BF " : "",
  2265. (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
  2266. }
  2267. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  2268. const char __user *user_buf, size_t count, loff_t *ppos)
  2269. {
  2270. struct iwl_lq_sta *lq_sta = file->private_data;
  2271. struct iwl_mvm *mvm;
  2272. char buf[64];
  2273. size_t buf_size;
  2274. u32 parsed_rate;
  2275. mvm = lq_sta->drv;
  2276. memset(buf, 0, sizeof(buf));
  2277. buf_size = min(count, sizeof(buf) - 1);
  2278. if (copy_from_user(buf, user_buf, buf_size))
  2279. return -EFAULT;
  2280. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2281. lq_sta->dbg_fixed_rate = parsed_rate;
  2282. else
  2283. lq_sta->dbg_fixed_rate = 0;
  2284. rs_program_fix_rate(mvm, lq_sta);
  2285. return count;
  2286. }
  2287. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  2288. char __user *user_buf, size_t count, loff_t *ppos)
  2289. {
  2290. char *buff;
  2291. int desc = 0;
  2292. int i = 0;
  2293. ssize_t ret;
  2294. struct iwl_lq_sta *lq_sta = file->private_data;
  2295. struct iwl_mvm *mvm;
  2296. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2297. struct rs_rate *rate = &tbl->rate;
  2298. mvm = lq_sta->drv;
  2299. buff = kmalloc(2048, GFP_KERNEL);
  2300. if (!buff)
  2301. return -ENOMEM;
  2302. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2303. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  2304. lq_sta->total_failed, lq_sta->total_success,
  2305. lq_sta->active_legacy_rate);
  2306. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2307. lq_sta->dbg_fixed_rate);
  2308. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  2309. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
  2310. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
  2311. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
  2312. desc += sprintf(buff+desc, "lq type %s\n",
  2313. (is_legacy(rate)) ? "legacy" :
  2314. is_vht(rate) ? "VHT" : "HT");
  2315. if (!is_legacy(rate)) {
  2316. desc += sprintf(buff+desc, " %s",
  2317. (is_siso(rate)) ? "SISO" : "MIMO2");
  2318. desc += sprintf(buff+desc, " %s",
  2319. (is_ht20(rate)) ? "20MHz" :
  2320. (is_ht40(rate)) ? "40MHz" :
  2321. (is_ht80(rate)) ? "80Mhz" : "BAD BW");
  2322. desc += sprintf(buff+desc, " %s %s\n",
  2323. (rate->sgi) ? "SGI" : "NGI",
  2324. (lq_sta->is_agg) ? "AGG on" : "");
  2325. }
  2326. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  2327. lq_sta->last_rate_n_flags);
  2328. desc += sprintf(buff+desc,
  2329. "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
  2330. lq_sta->lq.flags,
  2331. lq_sta->lq.mimo_delim,
  2332. lq_sta->lq.single_stream_ant_msk,
  2333. lq_sta->lq.dual_stream_ant_msk);
  2334. desc += sprintf(buff+desc,
  2335. "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2336. le16_to_cpu(lq_sta->lq.agg_time_limit),
  2337. lq_sta->lq.agg_disable_start_th,
  2338. lq_sta->lq.agg_frame_cnt_limit);
  2339. desc += sprintf(buff+desc,
  2340. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2341. lq_sta->lq.initial_rate_index[0],
  2342. lq_sta->lq.initial_rate_index[1],
  2343. lq_sta->lq.initial_rate_index[2],
  2344. lq_sta->lq.initial_rate_index[3]);
  2345. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2346. u32 rate = le32_to_cpu(lq_sta->lq.rs_table[i]);
  2347. desc += sprintf(buff+desc,
  2348. " rate[%d] 0x%X ",
  2349. i, rate);
  2350. desc += rs_pretty_print_rate(buff+desc, rate);
  2351. }
  2352. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2353. kfree(buff);
  2354. return ret;
  2355. }
  2356. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2357. .write = rs_sta_dbgfs_scale_table_write,
  2358. .read = rs_sta_dbgfs_scale_table_read,
  2359. .open = simple_open,
  2360. .llseek = default_llseek,
  2361. };
  2362. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  2363. char __user *user_buf, size_t count, loff_t *ppos)
  2364. {
  2365. char *buff;
  2366. int desc = 0;
  2367. int i, j;
  2368. ssize_t ret;
  2369. struct iwl_scale_tbl_info *tbl;
  2370. struct rs_rate *rate;
  2371. struct iwl_lq_sta *lq_sta = file->private_data;
  2372. buff = kmalloc(1024, GFP_KERNEL);
  2373. if (!buff)
  2374. return -ENOMEM;
  2375. for (i = 0; i < LQ_SIZE; i++) {
  2376. tbl = &(lq_sta->lq_info[i]);
  2377. rate = &tbl->rate;
  2378. desc += sprintf(buff+desc,
  2379. "%s type=%d SGI=%d BW=%s DUP=0\n"
  2380. "rate=0x%X\n",
  2381. lq_sta->active_tbl == i ? "*" : "x",
  2382. rate->type,
  2383. rate->sgi,
  2384. is_ht20(rate) ? "20Mhz" :
  2385. is_ht40(rate) ? "40Mhz" :
  2386. is_ht80(rate) ? "80Mhz" : "ERR",
  2387. tbl->current_rate);
  2388. for (j = 0; j < IWL_RATE_COUNT; j++) {
  2389. desc += sprintf(buff+desc,
  2390. "counter=%d success=%d %%=%d\n",
  2391. tbl->win[j].counter,
  2392. tbl->win[j].success_counter,
  2393. tbl->win[j].success_ratio);
  2394. }
  2395. }
  2396. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2397. kfree(buff);
  2398. return ret;
  2399. }
  2400. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2401. .read = rs_sta_dbgfs_stats_table_read,
  2402. .open = simple_open,
  2403. .llseek = default_llseek,
  2404. };
  2405. static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
  2406. {
  2407. struct iwl_lq_sta *lq_sta = mvm_sta;
  2408. lq_sta->rs_sta_dbgfs_scale_table_file =
  2409. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  2410. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2411. lq_sta->rs_sta_dbgfs_stats_table_file =
  2412. debugfs_create_file("rate_stats_table", S_IRUSR, dir,
  2413. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2414. lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
  2415. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  2416. &lq_sta->tx_agg_tid_en);
  2417. }
  2418. static void rs_remove_debugfs(void *mvm, void *mvm_sta)
  2419. {
  2420. struct iwl_lq_sta *lq_sta = mvm_sta;
  2421. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2422. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2423. debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
  2424. }
  2425. #endif
  2426. /*
  2427. * Initialization of rate scaling information is done by driver after
  2428. * the station is added. Since mac80211 calls this function before a
  2429. * station is added we ignore it.
  2430. */
  2431. static void rs_rate_init_stub(void *mvm_r,
  2432. struct ieee80211_supported_band *sband,
  2433. struct cfg80211_chan_def *chandef,
  2434. struct ieee80211_sta *sta, void *mvm_sta)
  2435. {
  2436. }
  2437. static struct rate_control_ops rs_mvm_ops = {
  2438. .module = NULL,
  2439. .name = RS_NAME,
  2440. .tx_status = rs_tx_status,
  2441. .get_rate = rs_get_rate,
  2442. .rate_init = rs_rate_init_stub,
  2443. .alloc = rs_alloc,
  2444. .free = rs_free,
  2445. .alloc_sta = rs_alloc_sta,
  2446. .free_sta = rs_free_sta,
  2447. .rate_update = rs_rate_update,
  2448. #ifdef CONFIG_MAC80211_DEBUGFS
  2449. .add_sta_debugfs = rs_add_debugfs,
  2450. .remove_sta_debugfs = rs_remove_debugfs,
  2451. #endif
  2452. };
  2453. int iwl_mvm_rate_control_register(void)
  2454. {
  2455. return ieee80211_rate_control_register(&rs_mvm_ops);
  2456. }
  2457. void iwl_mvm_rate_control_unregister(void)
  2458. {
  2459. ieee80211_rate_control_unregister(&rs_mvm_ops);
  2460. }
  2461. /**
  2462. * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
  2463. * Tx protection, according to this rquest and previous requests,
  2464. * and send the LQ command.
  2465. * @mvmsta: The station
  2466. * @enable: Enable Tx protection?
  2467. */
  2468. int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  2469. bool enable)
  2470. {
  2471. struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
  2472. lockdep_assert_held(&mvm->mutex);
  2473. if (enable) {
  2474. if (mvmsta->tx_protection == 0)
  2475. lq->flags |= LQ_FLAG_USE_RTS_MSK;
  2476. mvmsta->tx_protection++;
  2477. } else {
  2478. mvmsta->tx_protection--;
  2479. if (mvmsta->tx_protection == 0)
  2480. lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
  2481. }
  2482. return iwl_mvm_send_lq_cmd(mvm, lq, false);
  2483. }