rs.c 83 KB

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