iwl-agn-rs.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2009 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/wireless.h>
  30. #include <net/mac80211.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include "iwl-dev.h"
  36. #include "iwl-sta.h"
  37. #include "iwl-core.h"
  38. #define RS_NAME "iwl-agn-rs"
  39. #define NUM_TRY_BEFORE_ANT_TOGGLE 1
  40. #define IWL_NUMBER_TRY 1
  41. #define IWL_HT_NUMBER_TRY 3
  42. #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
  43. #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
  44. #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  45. /* max allowed rate miss before sync LQ cmd */
  46. #define IWL_MISSED_RATE_MAX 15
  47. /* max time to accum history 2 seconds */
  48. #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
  49. static u8 rs_ht_to_legacy[] = {
  50. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  51. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  52. IWL_RATE_6M_INDEX,
  53. IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
  54. IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
  55. IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
  56. IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
  57. };
  58. static const u8 ant_toggle_lookup[] = {
  59. /*ANT_NONE -> */ ANT_NONE,
  60. /*ANT_A -> */ ANT_B,
  61. /*ANT_B -> */ ANT_C,
  62. /*ANT_AB -> */ ANT_BC,
  63. /*ANT_C -> */ ANT_A,
  64. /*ANT_AC -> */ ANT_AB,
  65. /*ANT_BC -> */ ANT_AC,
  66. /*ANT_ABC -> */ ANT_ABC,
  67. };
  68. /**
  69. * struct iwl_rate_scale_data -- tx success history for one rate
  70. */
  71. struct iwl_rate_scale_data {
  72. u64 data; /* bitmap of successful frames */
  73. s32 success_counter; /* number of frames successful */
  74. s32 success_ratio; /* per-cent * 128 */
  75. s32 counter; /* number of frames attempted */
  76. s32 average_tpt; /* success ratio * expected throughput */
  77. unsigned long stamp;
  78. };
  79. /**
  80. * struct iwl_scale_tbl_info -- tx params and success history for all rates
  81. *
  82. * There are two of these in struct iwl_lq_sta,
  83. * one for "active", and one for "search".
  84. */
  85. struct iwl_scale_tbl_info {
  86. enum iwl_table_type lq_type;
  87. u8 ant_type;
  88. u8 is_SGI; /* 1 = short guard interval */
  89. u8 is_ht40; /* 1 = 40 MHz channel width */
  90. u8 is_dup; /* 1 = duplicated data streams */
  91. u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
  92. u8 max_search; /* maximun number of tables we can search */
  93. s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
  94. u32 current_rate; /* rate_n_flags, uCode API format */
  95. struct iwl_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
  96. };
  97. struct iwl_traffic_load {
  98. unsigned long time_stamp; /* age of the oldest statistics */
  99. u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
  100. * slice */
  101. u32 total; /* total num of packets during the
  102. * last TID_MAX_TIME_DIFF */
  103. u8 queue_count; /* number of queues that has
  104. * been used since the last cleanup */
  105. u8 head; /* start of the circular buffer */
  106. };
  107. /**
  108. * struct iwl_lq_sta -- driver's rate scaling private structure
  109. *
  110. * Pointer to this gets passed back and forth between driver and mac80211.
  111. */
  112. struct iwl_lq_sta {
  113. u8 active_tbl; /* index of active table, range 0-1 */
  114. u8 enable_counter; /* indicates HT mode */
  115. u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
  116. u8 search_better_tbl; /* 1: currently trying alternate mode */
  117. s32 last_tpt;
  118. /* The following determine when to search for a new mode */
  119. u32 table_count_limit;
  120. u32 max_failure_limit; /* # failed frames before new search */
  121. u32 max_success_limit; /* # successful frames before new search */
  122. u32 table_count;
  123. u32 total_failed; /* total failed frames, any/all rates */
  124. u32 total_success; /* total successful frames, any/all rates */
  125. u64 flush_timer; /* time staying in mode before new search */
  126. u8 action_counter; /* # mode-switch actions tried */
  127. u8 is_green;
  128. u8 is_dup;
  129. enum ieee80211_band band;
  130. u8 ibss_sta_added;
  131. /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
  132. u32 supp_rates;
  133. u16 active_legacy_rate;
  134. u16 active_siso_rate;
  135. u16 active_mimo2_rate;
  136. u16 active_mimo3_rate;
  137. u16 active_rate_basic;
  138. s8 max_rate_idx; /* Max rate set by user */
  139. u8 missed_rate_counter;
  140. struct iwl_link_quality_cmd lq;
  141. struct iwl_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
  142. struct iwl_traffic_load load[TID_MAX_LOAD_COUNT];
  143. u8 tx_agg_tid_en;
  144. #ifdef CONFIG_MAC80211_DEBUGFS
  145. struct dentry *rs_sta_dbgfs_scale_table_file;
  146. struct dentry *rs_sta_dbgfs_stats_table_file;
  147. struct dentry *rs_sta_dbgfs_rate_scale_data_file;
  148. struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
  149. u32 dbg_fixed_rate;
  150. #endif
  151. struct iwl_priv *drv;
  152. /* used to be in sta_info */
  153. int last_txrate_idx;
  154. /* last tx rate_n_flags */
  155. u32 last_rate_n_flags;
  156. };
  157. static void rs_rate_scale_perform(struct iwl_priv *priv,
  158. struct sk_buff *skb,
  159. struct ieee80211_sta *sta,
  160. struct iwl_lq_sta *lq_sta);
  161. static void rs_fill_link_cmd(struct iwl_priv *priv,
  162. struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
  163. #ifdef CONFIG_MAC80211_DEBUGFS
  164. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  165. u32 *rate_n_flags, int index);
  166. #else
  167. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  168. u32 *rate_n_flags, int index)
  169. {}
  170. #endif
  171. /*
  172. * Expected throughput metrics for following rates:
  173. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  174. * "G" is the only table that supports CCK (the first 4 rates).
  175. */
  176. static s32 expected_tpt_A[IWL_RATE_COUNT] = {
  177. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
  178. };
  179. static s32 expected_tpt_G[IWL_RATE_COUNT] = {
  180. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
  181. };
  182. static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
  183. 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
  184. };
  185. static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
  186. 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
  187. };
  188. static s32 expected_tpt_mimo2_20MHz[IWL_RATE_COUNT] = {
  189. 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
  190. };
  191. static s32 expected_tpt_mimo2_20MHzSGI[IWL_RATE_COUNT] = {
  192. 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
  193. };
  194. static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
  195. 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
  196. };
  197. static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
  198. 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
  199. };
  200. static s32 expected_tpt_mimo2_40MHz[IWL_RATE_COUNT] = {
  201. 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
  202. };
  203. static s32 expected_tpt_mimo2_40MHzSGI[IWL_RATE_COUNT] = {
  204. 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
  205. };
  206. /* Expected throughput metric MIMO3 */
  207. static s32 expected_tpt_mimo3_20MHz[IWL_RATE_COUNT] = {
  208. 0, 0, 0, 0, 99, 99, 153, 186, 208, 239, 256, 263, 268
  209. };
  210. static s32 expected_tpt_mimo3_20MHzSGI[IWL_RATE_COUNT] = {
  211. 0, 0, 0, 0, 106, 106, 162, 194, 215, 246, 262, 268, 273
  212. };
  213. static s32 expected_tpt_mimo3_40MHz[IWL_RATE_COUNT] = {
  214. 0, 0, 0, 0, 152, 152, 211, 239, 255, 279, 290, 294, 297
  215. };
  216. static s32 expected_tpt_mimo3_40MHzSGI[IWL_RATE_COUNT] = {
  217. 0, 0, 0, 0, 160, 160, 219, 245, 261, 284, 294, 297, 300
  218. };
  219. /* mbps, mcs */
  220. const static struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
  221. {"1", ""},
  222. {"2", ""},
  223. {"5.5", ""},
  224. {"11", ""},
  225. {"6", "BPSK 1/2"},
  226. {"9", "BPSK 1/2"},
  227. {"12", "QPSK 1/2"},
  228. {"18", "QPSK 3/4"},
  229. {"24", "16QAM 1/2"},
  230. {"36", "16QAM 3/4"},
  231. {"48", "64QAM 2/3"},
  232. {"54", "64QAM 3/4"},
  233. {"60", "64QAM 5/6"}
  234. };
  235. #define MCS_INDEX_PER_STREAM (8)
  236. static inline u8 rs_extract_rate(u32 rate_n_flags)
  237. {
  238. return (u8)(rate_n_flags & 0xFF);
  239. }
  240. static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
  241. {
  242. window->data = 0;
  243. window->success_counter = 0;
  244. window->success_ratio = IWL_INVALID_VALUE;
  245. window->counter = 0;
  246. window->average_tpt = IWL_INVALID_VALUE;
  247. window->stamp = 0;
  248. }
  249. static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  250. {
  251. return (ant_type & valid_antenna) == ant_type;
  252. }
  253. /*
  254. * removes the old data from the statistics. All data that is older than
  255. * TID_MAX_TIME_DIFF, will be deleted.
  256. */
  257. static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
  258. {
  259. /* The oldest age we want to keep */
  260. u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
  261. while (tl->queue_count &&
  262. (tl->time_stamp < oldest_time)) {
  263. tl->total -= tl->packet_count[tl->head];
  264. tl->packet_count[tl->head] = 0;
  265. tl->time_stamp += TID_QUEUE_CELL_SPACING;
  266. tl->queue_count--;
  267. tl->head++;
  268. if (tl->head >= TID_QUEUE_MAX_SIZE)
  269. tl->head = 0;
  270. }
  271. }
  272. /*
  273. * increment traffic load value for tid and also remove
  274. * any old values if passed the certain time period
  275. */
  276. static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
  277. struct ieee80211_hdr *hdr)
  278. {
  279. u32 curr_time = jiffies_to_msecs(jiffies);
  280. u32 time_diff;
  281. s32 index;
  282. struct iwl_traffic_load *tl = NULL;
  283. u8 tid;
  284. if (ieee80211_is_data_qos(hdr->frame_control)) {
  285. u8 *qc = ieee80211_get_qos_ctl(hdr);
  286. tid = qc[0] & 0xf;
  287. } else
  288. return MAX_TID_COUNT;
  289. if (unlikely(tid >= TID_MAX_LOAD_COUNT))
  290. return MAX_TID_COUNT;
  291. tl = &lq_data->load[tid];
  292. curr_time -= curr_time % TID_ROUND_VALUE;
  293. /* Happens only for the first packet. Initialize the data */
  294. if (!(tl->queue_count)) {
  295. tl->total = 1;
  296. tl->time_stamp = curr_time;
  297. tl->queue_count = 1;
  298. tl->head = 0;
  299. tl->packet_count[0] = 1;
  300. return MAX_TID_COUNT;
  301. }
  302. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  303. index = time_diff / TID_QUEUE_CELL_SPACING;
  304. /* The history is too long: remove data that is older than */
  305. /* TID_MAX_TIME_DIFF */
  306. if (index >= TID_QUEUE_MAX_SIZE)
  307. rs_tl_rm_old_stats(tl, curr_time);
  308. index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
  309. tl->packet_count[index] = tl->packet_count[index] + 1;
  310. tl->total = tl->total + 1;
  311. if ((index + 1) > tl->queue_count)
  312. tl->queue_count = index + 1;
  313. return tid;
  314. }
  315. /*
  316. get the traffic load value for tid
  317. */
  318. static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
  319. {
  320. u32 curr_time = jiffies_to_msecs(jiffies);
  321. u32 time_diff;
  322. s32 index;
  323. struct iwl_traffic_load *tl = NULL;
  324. if (tid >= TID_MAX_LOAD_COUNT)
  325. return 0;
  326. tl = &(lq_data->load[tid]);
  327. curr_time -= curr_time % TID_ROUND_VALUE;
  328. if (!(tl->queue_count))
  329. return 0;
  330. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  331. index = time_diff / TID_QUEUE_CELL_SPACING;
  332. /* The history is too long: remove data that is older than */
  333. /* TID_MAX_TIME_DIFF */
  334. if (index >= TID_QUEUE_MAX_SIZE)
  335. rs_tl_rm_old_stats(tl, curr_time);
  336. return tl->total;
  337. }
  338. static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
  339. struct iwl_lq_sta *lq_data, u8 tid,
  340. struct ieee80211_sta *sta)
  341. {
  342. if (rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
  343. IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
  344. sta->addr, tid);
  345. ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
  346. }
  347. }
  348. static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
  349. struct iwl_lq_sta *lq_data,
  350. struct ieee80211_sta *sta)
  351. {
  352. if ((tid < TID_MAX_LOAD_COUNT))
  353. rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
  354. else if (tid == IWL_AGG_ALL_TID)
  355. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
  356. rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
  357. }
  358. static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
  359. {
  360. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  361. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  362. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  363. }
  364. /**
  365. * rs_collect_tx_data - Update the success/failure sliding window
  366. *
  367. * We keep a sliding window of the last 62 packets transmitted
  368. * at this rate. window->data contains the bitmask of successful
  369. * packets.
  370. */
  371. static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
  372. int scale_index, s32 tpt, int retries,
  373. int successes)
  374. {
  375. struct iwl_rate_scale_data *window = NULL;
  376. static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
  377. s32 fail_count;
  378. if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
  379. return -EINVAL;
  380. /* Select data for current tx bit rate */
  381. window = &(windows[scale_index]);
  382. /*
  383. * Keep track of only the latest 62 tx frame attempts in this rate's
  384. * history window; anything older isn't really relevant any more.
  385. * If we have filled up the sliding window, drop the oldest attempt;
  386. * if the oldest attempt (highest bit in bitmap) shows "success",
  387. * subtract "1" from the success counter (this is the main reason
  388. * we keep these bitmaps!).
  389. */
  390. while (retries > 0) {
  391. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  392. /* remove earliest */
  393. window->counter = IWL_RATE_MAX_WINDOW - 1;
  394. if (window->data & mask) {
  395. window->data &= ~mask;
  396. window->success_counter--;
  397. }
  398. }
  399. /* Increment frames-attempted counter */
  400. window->counter++;
  401. /* Shift bitmap by one frame (throw away oldest history),
  402. * OR in "1", and increment "success" if this
  403. * frame was successful. */
  404. window->data <<= 1;
  405. if (successes > 0) {
  406. window->success_counter++;
  407. window->data |= 0x1;
  408. successes--;
  409. }
  410. retries--;
  411. }
  412. /* Calculate current success ratio, avoid divide-by-0! */
  413. if (window->counter > 0)
  414. window->success_ratio = 128 * (100 * window->success_counter)
  415. / window->counter;
  416. else
  417. window->success_ratio = IWL_INVALID_VALUE;
  418. fail_count = window->counter - window->success_counter;
  419. /* Calculate average throughput, if we have enough history. */
  420. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  421. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  422. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  423. else
  424. window->average_tpt = IWL_INVALID_VALUE;
  425. /* Tag this window as having been updated */
  426. window->stamp = jiffies;
  427. return 0;
  428. }
  429. /*
  430. * Fill uCode API rate_n_flags field, based on "search" or "active" table.
  431. */
  432. /* FIXME:RS:remove this function and put the flags statically in the table */
  433. static u32 rate_n_flags_from_tbl(struct iwl_priv *priv,
  434. struct iwl_scale_tbl_info *tbl,
  435. int index, u8 use_green)
  436. {
  437. u32 rate_n_flags = 0;
  438. if (is_legacy(tbl->lq_type)) {
  439. rate_n_flags = iwl_rates[index].plcp;
  440. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  441. rate_n_flags |= RATE_MCS_CCK_MSK;
  442. } else if (is_Ht(tbl->lq_type)) {
  443. if (index > IWL_LAST_OFDM_RATE) {
  444. IWL_ERR(priv, "Invalid HT rate index %d\n", index);
  445. index = IWL_LAST_OFDM_RATE;
  446. }
  447. rate_n_flags = RATE_MCS_HT_MSK;
  448. if (is_siso(tbl->lq_type))
  449. rate_n_flags |= iwl_rates[index].plcp_siso;
  450. else if (is_mimo2(tbl->lq_type))
  451. rate_n_flags |= iwl_rates[index].plcp_mimo2;
  452. else
  453. rate_n_flags |= iwl_rates[index].plcp_mimo3;
  454. } else {
  455. IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
  456. }
  457. rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
  458. RATE_MCS_ANT_ABC_MSK);
  459. if (is_Ht(tbl->lq_type)) {
  460. if (tbl->is_ht40) {
  461. if (tbl->is_dup)
  462. rate_n_flags |= RATE_MCS_DUP_MSK;
  463. else
  464. rate_n_flags |= RATE_MCS_HT40_MSK;
  465. }
  466. if (tbl->is_SGI)
  467. rate_n_flags |= RATE_MCS_SGI_MSK;
  468. if (use_green) {
  469. rate_n_flags |= RATE_MCS_GF_MSK;
  470. if (is_siso(tbl->lq_type) && tbl->is_SGI) {
  471. rate_n_flags &= ~RATE_MCS_SGI_MSK;
  472. IWL_ERR(priv, "GF was set with SGI:SISO\n");
  473. }
  474. }
  475. }
  476. return rate_n_flags;
  477. }
  478. /*
  479. * Interpret uCode API's rate_n_flags format,
  480. * fill "search" or "active" tx mode table.
  481. */
  482. static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
  483. enum ieee80211_band band,
  484. struct iwl_scale_tbl_info *tbl,
  485. int *rate_idx)
  486. {
  487. u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
  488. u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
  489. u8 mcs;
  490. *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  491. if (*rate_idx == IWL_RATE_INVALID) {
  492. *rate_idx = -1;
  493. return -EINVAL;
  494. }
  495. tbl->is_SGI = 0; /* default legacy setup */
  496. tbl->is_ht40 = 0;
  497. tbl->is_dup = 0;
  498. tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
  499. tbl->lq_type = LQ_NONE;
  500. tbl->max_search = IWL_MAX_SEARCH;
  501. /* legacy rate format */
  502. if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
  503. if (num_of_ant == 1) {
  504. if (band == IEEE80211_BAND_5GHZ)
  505. tbl->lq_type = LQ_A;
  506. else
  507. tbl->lq_type = LQ_G;
  508. }
  509. /* HT rate format */
  510. } else {
  511. if (rate_n_flags & RATE_MCS_SGI_MSK)
  512. tbl->is_SGI = 1;
  513. if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
  514. (rate_n_flags & RATE_MCS_DUP_MSK))
  515. tbl->is_ht40 = 1;
  516. if (rate_n_flags & RATE_MCS_DUP_MSK)
  517. tbl->is_dup = 1;
  518. mcs = rs_extract_rate(rate_n_flags);
  519. /* SISO */
  520. if (mcs <= IWL_RATE_SISO_60M_PLCP) {
  521. if (num_of_ant == 1)
  522. tbl->lq_type = LQ_SISO; /*else NONE*/
  523. /* MIMO2 */
  524. } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
  525. if (num_of_ant == 2)
  526. tbl->lq_type = LQ_MIMO2;
  527. /* MIMO3 */
  528. } else {
  529. if (num_of_ant == 3) {
  530. tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
  531. tbl->lq_type = LQ_MIMO3;
  532. }
  533. }
  534. }
  535. return 0;
  536. }
  537. /* switch to another antenna/antennas and return 1 */
  538. /* if no other valid antenna found, return 0 */
  539. static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
  540. struct iwl_scale_tbl_info *tbl)
  541. {
  542. u8 new_ant_type;
  543. if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
  544. return 0;
  545. if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
  546. return 0;
  547. new_ant_type = ant_toggle_lookup[tbl->ant_type];
  548. while ((new_ant_type != tbl->ant_type) &&
  549. !rs_is_valid_ant(valid_ant, new_ant_type))
  550. new_ant_type = ant_toggle_lookup[new_ant_type];
  551. if (new_ant_type == tbl->ant_type)
  552. return 0;
  553. tbl->ant_type = new_ant_type;
  554. *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
  555. *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
  556. return 1;
  557. }
  558. /**
  559. * Green-field mode is valid if the station supports it and
  560. * there are no non-GF stations present in the BSS.
  561. */
  562. static inline u8 rs_use_green(struct ieee80211_sta *sta,
  563. struct iwl_ht_info *ht_conf)
  564. {
  565. return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
  566. !(ht_conf->non_GF_STA_present);
  567. }
  568. /**
  569. * rs_get_supported_rates - get the available rates
  570. *
  571. * if management frame or broadcast frame only return
  572. * basic available rates.
  573. *
  574. */
  575. static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
  576. struct ieee80211_hdr *hdr,
  577. enum iwl_table_type rate_type)
  578. {
  579. if (hdr && is_multicast_ether_addr(hdr->addr1) &&
  580. lq_sta->active_rate_basic)
  581. return lq_sta->active_rate_basic;
  582. if (is_legacy(rate_type)) {
  583. return lq_sta->active_legacy_rate;
  584. } else {
  585. if (is_siso(rate_type))
  586. return lq_sta->active_siso_rate;
  587. else if (is_mimo2(rate_type))
  588. return lq_sta->active_mimo2_rate;
  589. else
  590. return lq_sta->active_mimo3_rate;
  591. }
  592. }
  593. static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
  594. int rate_type)
  595. {
  596. u8 high = IWL_RATE_INVALID;
  597. u8 low = IWL_RATE_INVALID;
  598. /* 802.11A or ht walks to the next literal adjacent rate in
  599. * the rate table */
  600. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  601. int i;
  602. u32 mask;
  603. /* Find the previous rate that is in the rate mask */
  604. i = index - 1;
  605. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  606. if (rate_mask & mask) {
  607. low = i;
  608. break;
  609. }
  610. }
  611. /* Find the next rate that is in the rate mask */
  612. i = index + 1;
  613. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  614. if (rate_mask & mask) {
  615. high = i;
  616. break;
  617. }
  618. }
  619. return (high << 8) | low;
  620. }
  621. low = index;
  622. while (low != IWL_RATE_INVALID) {
  623. low = iwl_rates[low].prev_rs;
  624. if (low == IWL_RATE_INVALID)
  625. break;
  626. if (rate_mask & (1 << low))
  627. break;
  628. IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
  629. }
  630. high = index;
  631. while (high != IWL_RATE_INVALID) {
  632. high = iwl_rates[high].next_rs;
  633. if (high == IWL_RATE_INVALID)
  634. break;
  635. if (rate_mask & (1 << high))
  636. break;
  637. IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
  638. }
  639. return (high << 8) | low;
  640. }
  641. static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
  642. struct iwl_scale_tbl_info *tbl,
  643. u8 scale_index, u8 ht_possible)
  644. {
  645. s32 low;
  646. u16 rate_mask;
  647. u16 high_low;
  648. u8 switch_to_legacy = 0;
  649. u8 is_green = lq_sta->is_green;
  650. /* check if we need to switch from HT to legacy rates.
  651. * assumption is that mandatory rates (1Mbps or 6Mbps)
  652. * are always supported (spec demand) */
  653. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
  654. switch_to_legacy = 1;
  655. scale_index = rs_ht_to_legacy[scale_index];
  656. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  657. tbl->lq_type = LQ_A;
  658. else
  659. tbl->lq_type = LQ_G;
  660. if (num_of_ant(tbl->ant_type) > 1)
  661. tbl->ant_type = ANT_A;/*FIXME:RS*/
  662. tbl->is_ht40 = 0;
  663. tbl->is_SGI = 0;
  664. tbl->max_search = IWL_MAX_SEARCH;
  665. }
  666. rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
  667. /* Mask with station rate restriction */
  668. if (is_legacy(tbl->lq_type)) {
  669. /* supp_rates has no CCK bits in A mode */
  670. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  671. rate_mask = (u16)(rate_mask &
  672. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  673. else
  674. rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
  675. }
  676. /* If we switched from HT to legacy, check current rate */
  677. if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
  678. low = scale_index;
  679. goto out;
  680. }
  681. high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
  682. tbl->lq_type);
  683. low = high_low & 0xff;
  684. if (low == IWL_RATE_INVALID)
  685. low = scale_index;
  686. out:
  687. return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
  688. }
  689. /*
  690. * mac80211 sends us Tx status
  691. */
  692. static void rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
  693. struct ieee80211_sta *sta, void *priv_sta,
  694. struct sk_buff *skb)
  695. {
  696. int status;
  697. u8 retries;
  698. int rs_index, index = 0;
  699. struct iwl_lq_sta *lq_sta = priv_sta;
  700. struct iwl_link_quality_cmd *table;
  701. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  702. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  703. struct ieee80211_hw *hw = priv->hw;
  704. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  705. struct iwl_rate_scale_data *window = NULL;
  706. struct iwl_rate_scale_data *search_win = NULL;
  707. u32 tx_rate;
  708. struct iwl_scale_tbl_info tbl_type;
  709. struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
  710. u8 active_index = 0;
  711. s32 tpt = 0;
  712. IWL_DEBUG_RATE_LIMIT(priv, "get frame ack response, update rate scale window\n");
  713. if (!ieee80211_is_data(hdr->frame_control) ||
  714. info->flags & IEEE80211_TX_CTL_NO_ACK)
  715. return;
  716. /* This packet was aggregated but doesn't carry rate scale info */
  717. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  718. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  719. return;
  720. if (info->flags & IEEE80211_TX_STAT_AMPDU)
  721. retries = 0;
  722. else
  723. retries = info->status.rates[0].count - 1;
  724. if (retries > 15)
  725. retries = 15;
  726. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
  727. !lq_sta->ibss_sta_added)
  728. goto out;
  729. table = &lq_sta->lq;
  730. active_index = lq_sta->active_tbl;
  731. curr_tbl = &(lq_sta->lq_info[active_index]);
  732. search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
  733. window = (struct iwl_rate_scale_data *)&(curr_tbl->win[0]);
  734. search_win = (struct iwl_rate_scale_data *)&(search_tbl->win[0]);
  735. /*
  736. * Ignore this Tx frame response if its initial rate doesn't match
  737. * that of latest Link Quality command. There may be stragglers
  738. * from a previous Link Quality command, but we're no longer interested
  739. * in those; they're either from the "active" mode while we're trying
  740. * to check "search" mode, or a prior "search" mode after we've moved
  741. * to a new "search" mode (which might become the new "active" mode).
  742. */
  743. tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
  744. rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
  745. if (priv->band == IEEE80211_BAND_5GHZ)
  746. rs_index -= IWL_FIRST_OFDM_RATE;
  747. if ((info->status.rates[0].idx < 0) ||
  748. (tbl_type.is_SGI != !!(info->status.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)) ||
  749. (tbl_type.is_ht40 != !!(info->status.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
  750. (tbl_type.is_dup != !!(info->status.rates[0].flags & IEEE80211_TX_RC_DUP_DATA)) ||
  751. (tbl_type.ant_type != info->antenna_sel_tx) ||
  752. (!!(tx_rate & RATE_MCS_HT_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_MCS)) ||
  753. (!!(tx_rate & RATE_MCS_GF_MSK) != !!(info->status.rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
  754. (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
  755. hw->wiphy->bands[info->band]->bitrates[info->status.rates[0].idx].bitrate)) {
  756. IWL_DEBUG_RATE(priv, "initial rate does not match 0x%x\n", tx_rate);
  757. /* the last LQ command could failed so the LQ in ucode not
  758. * the same in driver sync up
  759. */
  760. lq_sta->missed_rate_counter++;
  761. if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
  762. lq_sta->missed_rate_counter = 0;
  763. iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  764. }
  765. goto out;
  766. }
  767. lq_sta->missed_rate_counter = 0;
  768. /* Update frame history window with "failure" for each Tx retry. */
  769. while (retries) {
  770. /* Look up the rate and other info used for each tx attempt.
  771. * Each tx attempt steps one entry deeper in the rate table. */
  772. tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
  773. rs_get_tbl_info_from_mcs(tx_rate, priv->band,
  774. &tbl_type, &rs_index);
  775. /* If type matches "search" table,
  776. * add failure to "search" history */
  777. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  778. (tbl_type.ant_type == search_tbl->ant_type) &&
  779. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  780. if (search_tbl->expected_tpt)
  781. tpt = search_tbl->expected_tpt[rs_index];
  782. else
  783. tpt = 0;
  784. rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
  785. /* Else if type matches "current/active" table,
  786. * add failure to "current/active" history */
  787. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  788. (tbl_type.ant_type == curr_tbl->ant_type) &&
  789. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  790. if (curr_tbl->expected_tpt)
  791. tpt = curr_tbl->expected_tpt[rs_index];
  792. else
  793. tpt = 0;
  794. rs_collect_tx_data(window, rs_index, tpt, 1, 0);
  795. }
  796. /* If not searching for a new mode, increment failed counter
  797. * ... this helps determine when to start searching again */
  798. if (lq_sta->stay_in_tbl)
  799. lq_sta->total_failed++;
  800. --retries;
  801. index++;
  802. }
  803. /*
  804. * Find (by rate) the history window to update with final Tx attempt;
  805. * if Tx was successful first try, use original rate,
  806. * else look up the rate that was, finally, successful.
  807. */
  808. tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
  809. lq_sta->last_rate_n_flags = tx_rate;
  810. rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
  811. /* Update frame history window with "success" if Tx got ACKed ... */
  812. status = !!(info->flags & IEEE80211_TX_STAT_ACK);
  813. /* If type matches "search" table,
  814. * add final tx status to "search" history */
  815. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  816. (tbl_type.ant_type == search_tbl->ant_type) &&
  817. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  818. if (search_tbl->expected_tpt)
  819. tpt = search_tbl->expected_tpt[rs_index];
  820. else
  821. tpt = 0;
  822. if (info->flags & IEEE80211_TX_STAT_AMPDU)
  823. rs_collect_tx_data(search_win, rs_index, tpt,
  824. info->status.ampdu_ack_len,
  825. info->status.ampdu_ack_map);
  826. else
  827. rs_collect_tx_data(search_win, rs_index, tpt,
  828. 1, status);
  829. /* Else if type matches "current/active" table,
  830. * add final tx status to "current/active" history */
  831. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  832. (tbl_type.ant_type == curr_tbl->ant_type) &&
  833. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  834. if (curr_tbl->expected_tpt)
  835. tpt = curr_tbl->expected_tpt[rs_index];
  836. else
  837. tpt = 0;
  838. if (info->flags & IEEE80211_TX_STAT_AMPDU)
  839. rs_collect_tx_data(window, rs_index, tpt,
  840. info->status.ampdu_ack_len,
  841. info->status.ampdu_ack_map);
  842. else
  843. rs_collect_tx_data(window, rs_index, tpt,
  844. 1, status);
  845. }
  846. /* If not searching for new mode, increment success/failed counter
  847. * ... these help determine when to start searching again */
  848. if (lq_sta->stay_in_tbl) {
  849. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  850. lq_sta->total_success += info->status.ampdu_ack_map;
  851. lq_sta->total_failed +=
  852. (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
  853. } else {
  854. if (status)
  855. lq_sta->total_success++;
  856. else
  857. lq_sta->total_failed++;
  858. }
  859. }
  860. /* See if there's a better rate or modulation mode to try. */
  861. if (sta && sta->supp_rates[sband->band])
  862. rs_rate_scale_perform(priv, skb, sta, lq_sta);
  863. out:
  864. return;
  865. }
  866. /*
  867. * Begin a period of staying with a selected modulation mode.
  868. * Set "stay_in_tbl" flag to prevent any mode switches.
  869. * Set frame tx success limits according to legacy vs. high-throughput,
  870. * and reset overall (spanning all rates) tx success history statistics.
  871. * These control how long we stay using same modulation mode before
  872. * searching for a new mode.
  873. */
  874. static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
  875. struct iwl_lq_sta *lq_sta)
  876. {
  877. IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
  878. lq_sta->stay_in_tbl = 1; /* only place this gets set */
  879. if (is_legacy) {
  880. lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  881. lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  882. lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
  883. } else {
  884. lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  885. lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  886. lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  887. }
  888. lq_sta->table_count = 0;
  889. lq_sta->total_failed = 0;
  890. lq_sta->total_success = 0;
  891. lq_sta->flush_timer = jiffies;
  892. lq_sta->action_counter = 0;
  893. }
  894. /*
  895. * Find correct throughput table for given mode of modulation
  896. */
  897. static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  898. struct iwl_scale_tbl_info *tbl)
  899. {
  900. if (is_legacy(tbl->lq_type)) {
  901. if (!is_a_band(tbl->lq_type))
  902. tbl->expected_tpt = expected_tpt_G;
  903. else
  904. tbl->expected_tpt = expected_tpt_A;
  905. } else if (is_siso(tbl->lq_type)) {
  906. if (tbl->is_ht40 && !lq_sta->is_dup)
  907. if (tbl->is_SGI)
  908. tbl->expected_tpt = expected_tpt_siso40MHzSGI;
  909. else
  910. tbl->expected_tpt = expected_tpt_siso40MHz;
  911. else if (tbl->is_SGI)
  912. tbl->expected_tpt = expected_tpt_siso20MHzSGI;
  913. else
  914. tbl->expected_tpt = expected_tpt_siso20MHz;
  915. } else if (is_mimo2(tbl->lq_type)) {
  916. if (tbl->is_ht40 && !lq_sta->is_dup)
  917. if (tbl->is_SGI)
  918. tbl->expected_tpt = expected_tpt_mimo2_40MHzSGI;
  919. else
  920. tbl->expected_tpt = expected_tpt_mimo2_40MHz;
  921. else if (tbl->is_SGI)
  922. tbl->expected_tpt = expected_tpt_mimo2_20MHzSGI;
  923. else
  924. tbl->expected_tpt = expected_tpt_mimo2_20MHz;
  925. } else if (is_mimo3(tbl->lq_type)) {
  926. if (tbl->is_ht40 && !lq_sta->is_dup)
  927. if (tbl->is_SGI)
  928. tbl->expected_tpt = expected_tpt_mimo3_40MHzSGI;
  929. else
  930. tbl->expected_tpt = expected_tpt_mimo3_40MHz;
  931. else if (tbl->is_SGI)
  932. tbl->expected_tpt = expected_tpt_mimo3_20MHzSGI;
  933. else
  934. tbl->expected_tpt = expected_tpt_mimo3_20MHz;
  935. } else
  936. tbl->expected_tpt = expected_tpt_G;
  937. }
  938. /*
  939. * Find starting rate for new "search" high-throughput mode of modulation.
  940. * Goal is to find lowest expected rate (under perfect conditions) that is
  941. * above the current measured throughput of "active" mode, to give new mode
  942. * a fair chance to prove itself without too many challenges.
  943. *
  944. * This gets called when transitioning to more aggressive modulation
  945. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  946. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  947. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  948. * bit rate will typically need to increase, but not if performance was bad.
  949. */
  950. static s32 rs_get_best_rate(struct iwl_priv *priv,
  951. struct iwl_lq_sta *lq_sta,
  952. struct iwl_scale_tbl_info *tbl, /* "search" */
  953. u16 rate_mask, s8 index)
  954. {
  955. /* "active" values */
  956. struct iwl_scale_tbl_info *active_tbl =
  957. &(lq_sta->lq_info[lq_sta->active_tbl]);
  958. s32 active_sr = active_tbl->win[index].success_ratio;
  959. s32 active_tpt = active_tbl->expected_tpt[index];
  960. /* expected "search" throughput */
  961. s32 *tpt_tbl = tbl->expected_tpt;
  962. s32 new_rate, high, low, start_hi;
  963. u16 high_low;
  964. s8 rate = index;
  965. new_rate = high = low = start_hi = IWL_RATE_INVALID;
  966. for (; ;) {
  967. high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
  968. tbl->lq_type);
  969. low = high_low & 0xff;
  970. high = (high_low >> 8) & 0xff;
  971. /*
  972. * Lower the "search" bit rate, to give new "search" mode
  973. * approximately the same throughput as "active" if:
  974. *
  975. * 1) "Active" mode has been working modestly well (but not
  976. * great), and expected "search" throughput (under perfect
  977. * conditions) at candidate rate is above the actual
  978. * measured "active" throughput (but less than expected
  979. * "active" throughput under perfect conditions).
  980. * OR
  981. * 2) "Active" mode has been working perfectly or very well
  982. * and expected "search" throughput (under perfect
  983. * conditions) at candidate rate is above expected
  984. * "active" throughput (under perfect conditions).
  985. */
  986. if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
  987. ((active_sr > IWL_RATE_DECREASE_TH) &&
  988. (active_sr <= IWL_RATE_HIGH_TH) &&
  989. (tpt_tbl[rate] <= active_tpt))) ||
  990. ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
  991. (tpt_tbl[rate] > active_tpt))) {
  992. /* (2nd or later pass)
  993. * If we've already tried to raise the rate, and are
  994. * now trying to lower it, use the higher rate. */
  995. if (start_hi != IWL_RATE_INVALID) {
  996. new_rate = start_hi;
  997. break;
  998. }
  999. new_rate = rate;
  1000. /* Loop again with lower rate */
  1001. if (low != IWL_RATE_INVALID)
  1002. rate = low;
  1003. /* Lower rate not available, use the original */
  1004. else
  1005. break;
  1006. /* Else try to raise the "search" rate to match "active" */
  1007. } else {
  1008. /* (2nd or later pass)
  1009. * If we've already tried to lower the rate, and are
  1010. * now trying to raise it, use the lower rate. */
  1011. if (new_rate != IWL_RATE_INVALID)
  1012. break;
  1013. /* Loop again with higher rate */
  1014. else if (high != IWL_RATE_INVALID) {
  1015. start_hi = high;
  1016. rate = high;
  1017. /* Higher rate not available, use the original */
  1018. } else {
  1019. new_rate = rate;
  1020. break;
  1021. }
  1022. }
  1023. }
  1024. return new_rate;
  1025. }
  1026. /*
  1027. * Set up search table for MIMO2
  1028. */
  1029. static int rs_switch_to_mimo2(struct iwl_priv *priv,
  1030. struct iwl_lq_sta *lq_sta,
  1031. struct ieee80211_conf *conf,
  1032. struct ieee80211_sta *sta,
  1033. struct iwl_scale_tbl_info *tbl, int index)
  1034. {
  1035. u16 rate_mask;
  1036. s32 rate;
  1037. s8 is_green = lq_sta->is_green;
  1038. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1039. return -1;
  1040. if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
  1041. == WLAN_HT_CAP_SM_PS_STATIC)
  1042. return -1;
  1043. /* Need both Tx chains/antennas to support MIMO */
  1044. if (priv->hw_params.tx_chains_num < 2)
  1045. return -1;
  1046. IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
  1047. tbl->lq_type = LQ_MIMO2;
  1048. tbl->is_dup = lq_sta->is_dup;
  1049. tbl->action = 0;
  1050. tbl->max_search = IWL_MAX_SEARCH;
  1051. rate_mask = lq_sta->active_mimo2_rate;
  1052. if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
  1053. tbl->is_ht40 = 1;
  1054. else
  1055. tbl->is_ht40 = 0;
  1056. rs_set_expected_tpt_table(lq_sta, tbl);
  1057. rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
  1058. IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
  1059. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1060. IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
  1061. rate, rate_mask);
  1062. return -1;
  1063. }
  1064. tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
  1065. IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
  1066. tbl->current_rate, is_green);
  1067. return 0;
  1068. }
  1069. /*
  1070. * Set up search table for MIMO3
  1071. */
  1072. static int rs_switch_to_mimo3(struct iwl_priv *priv,
  1073. struct iwl_lq_sta *lq_sta,
  1074. struct ieee80211_conf *conf,
  1075. struct ieee80211_sta *sta,
  1076. struct iwl_scale_tbl_info *tbl, int index)
  1077. {
  1078. u16 rate_mask;
  1079. s32 rate;
  1080. s8 is_green = lq_sta->is_green;
  1081. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1082. return -1;
  1083. if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
  1084. == WLAN_HT_CAP_SM_PS_STATIC)
  1085. return -1;
  1086. /* Need both Tx chains/antennas to support MIMO */
  1087. if (priv->hw_params.tx_chains_num < 3)
  1088. return -1;
  1089. IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO3\n");
  1090. tbl->lq_type = LQ_MIMO3;
  1091. tbl->is_dup = lq_sta->is_dup;
  1092. tbl->action = 0;
  1093. tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
  1094. rate_mask = lq_sta->active_mimo3_rate;
  1095. if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
  1096. tbl->is_ht40 = 1;
  1097. else
  1098. tbl->is_ht40 = 0;
  1099. rs_set_expected_tpt_table(lq_sta, tbl);
  1100. rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
  1101. IWL_DEBUG_RATE(priv, "LQ: MIMO3 best rate %d mask %X\n",
  1102. rate, rate_mask);
  1103. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1104. IWL_DEBUG_RATE(priv, "Can't switch with index %d rate mask %x\n",
  1105. rate, rate_mask);
  1106. return -1;
  1107. }
  1108. tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
  1109. IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
  1110. tbl->current_rate, is_green);
  1111. return 0;
  1112. }
  1113. /*
  1114. * Set up search table for SISO
  1115. */
  1116. static int rs_switch_to_siso(struct iwl_priv *priv,
  1117. struct iwl_lq_sta *lq_sta,
  1118. struct ieee80211_conf *conf,
  1119. struct ieee80211_sta *sta,
  1120. struct iwl_scale_tbl_info *tbl, int index)
  1121. {
  1122. u16 rate_mask;
  1123. u8 is_green = lq_sta->is_green;
  1124. s32 rate;
  1125. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1126. return -1;
  1127. IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
  1128. tbl->is_dup = lq_sta->is_dup;
  1129. tbl->lq_type = LQ_SISO;
  1130. tbl->action = 0;
  1131. tbl->max_search = IWL_MAX_SEARCH;
  1132. rate_mask = lq_sta->active_siso_rate;
  1133. if (iwl_is_ht40_tx_allowed(priv, &sta->ht_cap))
  1134. tbl->is_ht40 = 1;
  1135. else
  1136. tbl->is_ht40 = 0;
  1137. if (is_green)
  1138. tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
  1139. rs_set_expected_tpt_table(lq_sta, tbl);
  1140. rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
  1141. IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
  1142. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1143. IWL_DEBUG_RATE(priv, "can not switch with index %d rate mask %x\n",
  1144. rate, rate_mask);
  1145. return -1;
  1146. }
  1147. tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, rate, is_green);
  1148. IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
  1149. tbl->current_rate, is_green);
  1150. return 0;
  1151. }
  1152. /*
  1153. * Try to switch to new modulation mode from legacy
  1154. */
  1155. static int rs_move_legacy_other(struct iwl_priv *priv,
  1156. struct iwl_lq_sta *lq_sta,
  1157. struct ieee80211_conf *conf,
  1158. struct ieee80211_sta *sta,
  1159. int index)
  1160. {
  1161. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1162. struct iwl_scale_tbl_info *search_tbl =
  1163. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1164. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1165. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1166. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1167. u8 start_action = tbl->action;
  1168. u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
  1169. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  1170. int ret = 0;
  1171. u8 update_search_tbl_counter = 0;
  1172. if (!iwl_ht_enabled(priv))
  1173. /* stay in Legacy */
  1174. tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
  1175. else if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
  1176. tbl->action > IWL_LEGACY_SWITCH_SISO)
  1177. tbl->action = IWL_LEGACY_SWITCH_SISO;
  1178. for (; ;) {
  1179. lq_sta->action_counter++;
  1180. switch (tbl->action) {
  1181. case IWL_LEGACY_SWITCH_ANTENNA1:
  1182. case IWL_LEGACY_SWITCH_ANTENNA2:
  1183. IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
  1184. if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
  1185. tx_chains_num <= 1) ||
  1186. (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
  1187. tx_chains_num <= 2))
  1188. break;
  1189. /* Don't change antenna if success has been great */
  1190. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1191. break;
  1192. /* Set up search table to try other antenna */
  1193. memcpy(search_tbl, tbl, sz);
  1194. if (rs_toggle_antenna(valid_tx_ant,
  1195. &search_tbl->current_rate, search_tbl)) {
  1196. update_search_tbl_counter = 1;
  1197. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1198. goto out;
  1199. }
  1200. break;
  1201. case IWL_LEGACY_SWITCH_SISO:
  1202. IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
  1203. /* Set up search table to try SISO */
  1204. memcpy(search_tbl, tbl, sz);
  1205. search_tbl->is_SGI = 0;
  1206. ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
  1207. search_tbl, index);
  1208. if (!ret) {
  1209. lq_sta->action_counter = 0;
  1210. goto out;
  1211. }
  1212. break;
  1213. case IWL_LEGACY_SWITCH_MIMO2_AB:
  1214. case IWL_LEGACY_SWITCH_MIMO2_AC:
  1215. case IWL_LEGACY_SWITCH_MIMO2_BC:
  1216. IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
  1217. /* Set up search table to try MIMO */
  1218. memcpy(search_tbl, tbl, sz);
  1219. search_tbl->is_SGI = 0;
  1220. if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
  1221. search_tbl->ant_type = ANT_AB;
  1222. else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
  1223. search_tbl->ant_type = ANT_AC;
  1224. else
  1225. search_tbl->ant_type = ANT_BC;
  1226. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1227. break;
  1228. ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
  1229. search_tbl, index);
  1230. if (!ret) {
  1231. lq_sta->action_counter = 0;
  1232. goto out;
  1233. }
  1234. break;
  1235. case IWL_LEGACY_SWITCH_MIMO3_ABC:
  1236. IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO3\n");
  1237. /* Set up search table to try MIMO3 */
  1238. memcpy(search_tbl, tbl, sz);
  1239. search_tbl->is_SGI = 0;
  1240. search_tbl->ant_type = ANT_ABC;
  1241. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1242. break;
  1243. ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
  1244. search_tbl, index);
  1245. if (!ret) {
  1246. lq_sta->action_counter = 0;
  1247. goto out;
  1248. }
  1249. break;
  1250. }
  1251. tbl->action++;
  1252. if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
  1253. tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
  1254. if (tbl->action == start_action)
  1255. break;
  1256. }
  1257. search_tbl->lq_type = LQ_NONE;
  1258. return 0;
  1259. out:
  1260. lq_sta->search_better_tbl = 1;
  1261. tbl->action++;
  1262. if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
  1263. tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
  1264. if (update_search_tbl_counter)
  1265. search_tbl->action = tbl->action;
  1266. return 0;
  1267. }
  1268. /*
  1269. * Try to switch to new modulation mode from SISO
  1270. */
  1271. static int rs_move_siso_to_other(struct iwl_priv *priv,
  1272. struct iwl_lq_sta *lq_sta,
  1273. struct ieee80211_conf *conf,
  1274. struct ieee80211_sta *sta, int index)
  1275. {
  1276. u8 is_green = lq_sta->is_green;
  1277. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1278. struct iwl_scale_tbl_info *search_tbl =
  1279. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1280. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1281. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1282. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1283. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1284. u8 start_action = tbl->action;
  1285. u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
  1286. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  1287. u8 update_search_tbl_counter = 0;
  1288. int ret;
  1289. if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE &&
  1290. tbl->action > IWL_SISO_SWITCH_ANTENNA2) {
  1291. /* stay in SISO */
  1292. tbl->action = IWL_SISO_SWITCH_ANTENNA1;
  1293. }
  1294. for (;;) {
  1295. lq_sta->action_counter++;
  1296. switch (tbl->action) {
  1297. case IWL_SISO_SWITCH_ANTENNA1:
  1298. case IWL_SISO_SWITCH_ANTENNA2:
  1299. IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
  1300. if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
  1301. tx_chains_num <= 1) ||
  1302. (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
  1303. tx_chains_num <= 2))
  1304. break;
  1305. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1306. break;
  1307. memcpy(search_tbl, tbl, sz);
  1308. if (rs_toggle_antenna(valid_tx_ant,
  1309. &search_tbl->current_rate, search_tbl)) {
  1310. update_search_tbl_counter = 1;
  1311. goto out;
  1312. }
  1313. break;
  1314. case IWL_SISO_SWITCH_MIMO2_AB:
  1315. case IWL_SISO_SWITCH_MIMO2_AC:
  1316. case IWL_SISO_SWITCH_MIMO2_BC:
  1317. IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
  1318. memcpy(search_tbl, tbl, sz);
  1319. search_tbl->is_SGI = 0;
  1320. if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
  1321. search_tbl->ant_type = ANT_AB;
  1322. else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
  1323. search_tbl->ant_type = ANT_AC;
  1324. else
  1325. search_tbl->ant_type = ANT_BC;
  1326. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1327. break;
  1328. ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
  1329. search_tbl, index);
  1330. if (!ret)
  1331. goto out;
  1332. break;
  1333. case IWL_SISO_SWITCH_GI:
  1334. if (!tbl->is_ht40 && !(ht_cap->cap &
  1335. IEEE80211_HT_CAP_SGI_20))
  1336. break;
  1337. if (tbl->is_ht40 && !(ht_cap->cap &
  1338. IEEE80211_HT_CAP_SGI_40))
  1339. break;
  1340. IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
  1341. memcpy(search_tbl, tbl, sz);
  1342. if (is_green) {
  1343. if (!tbl->is_SGI)
  1344. break;
  1345. else
  1346. IWL_ERR(priv,
  1347. "SGI was set in GF+SISO\n");
  1348. }
  1349. search_tbl->is_SGI = !tbl->is_SGI;
  1350. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1351. if (tbl->is_SGI) {
  1352. s32 tpt = lq_sta->last_tpt / 100;
  1353. if (tpt >= search_tbl->expected_tpt[index])
  1354. break;
  1355. }
  1356. search_tbl->current_rate =
  1357. rate_n_flags_from_tbl(priv, search_tbl,
  1358. index, is_green);
  1359. update_search_tbl_counter = 1;
  1360. goto out;
  1361. case IWL_SISO_SWITCH_MIMO3_ABC:
  1362. IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO3\n");
  1363. memcpy(search_tbl, tbl, sz);
  1364. search_tbl->is_SGI = 0;
  1365. search_tbl->ant_type = ANT_ABC;
  1366. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1367. break;
  1368. ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
  1369. search_tbl, index);
  1370. if (!ret)
  1371. goto out;
  1372. break;
  1373. }
  1374. tbl->action++;
  1375. if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
  1376. tbl->action = IWL_SISO_SWITCH_ANTENNA1;
  1377. if (tbl->action == start_action)
  1378. break;
  1379. }
  1380. search_tbl->lq_type = LQ_NONE;
  1381. return 0;
  1382. out:
  1383. lq_sta->search_better_tbl = 1;
  1384. tbl->action++;
  1385. if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
  1386. tbl->action = IWL_SISO_SWITCH_ANTENNA1;
  1387. if (update_search_tbl_counter)
  1388. search_tbl->action = tbl->action;
  1389. return 0;
  1390. }
  1391. /*
  1392. * Try to switch to new modulation mode from MIMO2
  1393. */
  1394. static int rs_move_mimo2_to_other(struct iwl_priv *priv,
  1395. struct iwl_lq_sta *lq_sta,
  1396. struct ieee80211_conf *conf,
  1397. struct ieee80211_sta *sta, int index)
  1398. {
  1399. s8 is_green = lq_sta->is_green;
  1400. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1401. struct iwl_scale_tbl_info *search_tbl =
  1402. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1403. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1404. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1405. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1406. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1407. u8 start_action = tbl->action;
  1408. u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
  1409. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  1410. u8 update_search_tbl_counter = 0;
  1411. int ret;
  1412. if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
  1413. (tbl->action < IWL_MIMO2_SWITCH_SISO_A ||
  1414. tbl->action > IWL_MIMO2_SWITCH_SISO_C)) {
  1415. /* switch in SISO */
  1416. tbl->action = IWL_MIMO2_SWITCH_SISO_A;
  1417. }
  1418. for (;;) {
  1419. lq_sta->action_counter++;
  1420. switch (tbl->action) {
  1421. case IWL_MIMO2_SWITCH_ANTENNA1:
  1422. case IWL_MIMO2_SWITCH_ANTENNA2:
  1423. IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
  1424. if (tx_chains_num <= 2)
  1425. break;
  1426. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1427. break;
  1428. memcpy(search_tbl, tbl, sz);
  1429. if (rs_toggle_antenna(valid_tx_ant,
  1430. &search_tbl->current_rate, search_tbl)) {
  1431. update_search_tbl_counter = 1;
  1432. goto out;
  1433. }
  1434. break;
  1435. case IWL_MIMO2_SWITCH_SISO_A:
  1436. case IWL_MIMO2_SWITCH_SISO_B:
  1437. case IWL_MIMO2_SWITCH_SISO_C:
  1438. IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
  1439. /* Set up new search table for SISO */
  1440. memcpy(search_tbl, tbl, sz);
  1441. if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
  1442. search_tbl->ant_type = ANT_A;
  1443. else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
  1444. search_tbl->ant_type = ANT_B;
  1445. else
  1446. search_tbl->ant_type = ANT_C;
  1447. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1448. break;
  1449. ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
  1450. search_tbl, index);
  1451. if (!ret)
  1452. goto out;
  1453. break;
  1454. case IWL_MIMO2_SWITCH_GI:
  1455. if (!tbl->is_ht40 && !(ht_cap->cap &
  1456. IEEE80211_HT_CAP_SGI_20))
  1457. break;
  1458. if (tbl->is_ht40 && !(ht_cap->cap &
  1459. IEEE80211_HT_CAP_SGI_40))
  1460. break;
  1461. IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
  1462. /* Set up new search table for MIMO2 */
  1463. memcpy(search_tbl, tbl, sz);
  1464. search_tbl->is_SGI = !tbl->is_SGI;
  1465. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1466. /*
  1467. * If active table already uses the fastest possible
  1468. * modulation (dual stream with short guard interval),
  1469. * and it's working well, there's no need to look
  1470. * for a better type of modulation!
  1471. */
  1472. if (tbl->is_SGI) {
  1473. s32 tpt = lq_sta->last_tpt / 100;
  1474. if (tpt >= search_tbl->expected_tpt[index])
  1475. break;
  1476. }
  1477. search_tbl->current_rate =
  1478. rate_n_flags_from_tbl(priv, search_tbl,
  1479. index, is_green);
  1480. update_search_tbl_counter = 1;
  1481. goto out;
  1482. case IWL_MIMO2_SWITCH_MIMO3_ABC:
  1483. IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to MIMO3\n");
  1484. memcpy(search_tbl, tbl, sz);
  1485. search_tbl->is_SGI = 0;
  1486. search_tbl->ant_type = ANT_ABC;
  1487. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1488. break;
  1489. ret = rs_switch_to_mimo3(priv, lq_sta, conf, sta,
  1490. search_tbl, index);
  1491. if (!ret)
  1492. goto out;
  1493. break;
  1494. }
  1495. tbl->action++;
  1496. if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
  1497. tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
  1498. if (tbl->action == start_action)
  1499. break;
  1500. }
  1501. search_tbl->lq_type = LQ_NONE;
  1502. return 0;
  1503. out:
  1504. lq_sta->search_better_tbl = 1;
  1505. tbl->action++;
  1506. if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
  1507. tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
  1508. if (update_search_tbl_counter)
  1509. search_tbl->action = tbl->action;
  1510. return 0;
  1511. }
  1512. /*
  1513. * Try to switch to new modulation mode from MIMO3
  1514. */
  1515. static int rs_move_mimo3_to_other(struct iwl_priv *priv,
  1516. struct iwl_lq_sta *lq_sta,
  1517. struct ieee80211_conf *conf,
  1518. struct ieee80211_sta *sta, int index)
  1519. {
  1520. s8 is_green = lq_sta->is_green;
  1521. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1522. struct iwl_scale_tbl_info *search_tbl =
  1523. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1524. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1525. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1526. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1527. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1528. u8 start_action = tbl->action;
  1529. u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
  1530. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  1531. int ret;
  1532. u8 update_search_tbl_counter = 0;
  1533. if ((iwl_tx_ant_restriction(priv) == IWL_ANT_OK_SINGLE) &&
  1534. (tbl->action < IWL_MIMO3_SWITCH_SISO_A ||
  1535. tbl->action > IWL_MIMO3_SWITCH_SISO_C)) {
  1536. /* switch in SISO */
  1537. tbl->action = IWL_MIMO3_SWITCH_SISO_A;
  1538. }
  1539. for (;;) {
  1540. lq_sta->action_counter++;
  1541. switch (tbl->action) {
  1542. case IWL_MIMO3_SWITCH_ANTENNA1:
  1543. case IWL_MIMO3_SWITCH_ANTENNA2:
  1544. IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle Antennas\n");
  1545. if (tx_chains_num <= 3)
  1546. break;
  1547. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1548. break;
  1549. memcpy(search_tbl, tbl, sz);
  1550. if (rs_toggle_antenna(valid_tx_ant,
  1551. &search_tbl->current_rate, search_tbl))
  1552. goto out;
  1553. break;
  1554. case IWL_MIMO3_SWITCH_SISO_A:
  1555. case IWL_MIMO3_SWITCH_SISO_B:
  1556. case IWL_MIMO3_SWITCH_SISO_C:
  1557. IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to SISO\n");
  1558. /* Set up new search table for SISO */
  1559. memcpy(search_tbl, tbl, sz);
  1560. if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
  1561. search_tbl->ant_type = ANT_A;
  1562. else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
  1563. search_tbl->ant_type = ANT_B;
  1564. else
  1565. search_tbl->ant_type = ANT_C;
  1566. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1567. break;
  1568. ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
  1569. search_tbl, index);
  1570. if (!ret)
  1571. goto out;
  1572. break;
  1573. case IWL_MIMO3_SWITCH_MIMO2_AB:
  1574. case IWL_MIMO3_SWITCH_MIMO2_AC:
  1575. case IWL_MIMO3_SWITCH_MIMO2_BC:
  1576. IWL_DEBUG_RATE(priv, "LQ: MIMO3 switch to MIMO2\n");
  1577. memcpy(search_tbl, tbl, sz);
  1578. search_tbl->is_SGI = 0;
  1579. if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
  1580. search_tbl->ant_type = ANT_AB;
  1581. else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
  1582. search_tbl->ant_type = ANT_AC;
  1583. else
  1584. search_tbl->ant_type = ANT_BC;
  1585. if (!rs_is_valid_ant(valid_tx_ant, search_tbl->ant_type))
  1586. break;
  1587. ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
  1588. search_tbl, index);
  1589. if (!ret)
  1590. goto out;
  1591. break;
  1592. case IWL_MIMO3_SWITCH_GI:
  1593. if (!tbl->is_ht40 && !(ht_cap->cap &
  1594. IEEE80211_HT_CAP_SGI_20))
  1595. break;
  1596. if (tbl->is_ht40 && !(ht_cap->cap &
  1597. IEEE80211_HT_CAP_SGI_40))
  1598. break;
  1599. IWL_DEBUG_RATE(priv, "LQ: MIMO3 toggle SGI/NGI\n");
  1600. /* Set up new search table for MIMO */
  1601. memcpy(search_tbl, tbl, sz);
  1602. search_tbl->is_SGI = !tbl->is_SGI;
  1603. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1604. /*
  1605. * If active table already uses the fastest possible
  1606. * modulation (dual stream with short guard interval),
  1607. * and it's working well, there's no need to look
  1608. * for a better type of modulation!
  1609. */
  1610. if (tbl->is_SGI) {
  1611. s32 tpt = lq_sta->last_tpt / 100;
  1612. if (tpt >= search_tbl->expected_tpt[index])
  1613. break;
  1614. }
  1615. search_tbl->current_rate =
  1616. rate_n_flags_from_tbl(priv, search_tbl,
  1617. index, is_green);
  1618. update_search_tbl_counter = 1;
  1619. goto out;
  1620. }
  1621. tbl->action++;
  1622. if (tbl->action > IWL_MIMO3_SWITCH_GI)
  1623. tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
  1624. if (tbl->action == start_action)
  1625. break;
  1626. }
  1627. search_tbl->lq_type = LQ_NONE;
  1628. return 0;
  1629. out:
  1630. lq_sta->search_better_tbl = 1;
  1631. tbl->action++;
  1632. if (tbl->action > IWL_MIMO3_SWITCH_GI)
  1633. tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
  1634. if (update_search_tbl_counter)
  1635. search_tbl->action = tbl->action;
  1636. return 0;
  1637. }
  1638. /*
  1639. * Check whether we should continue using same modulation mode, or
  1640. * begin search for a new mode, based on:
  1641. * 1) # tx successes or failures while using this mode
  1642. * 2) # times calling this function
  1643. * 3) elapsed time in this mode (not used, for now)
  1644. */
  1645. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta)
  1646. {
  1647. struct iwl_scale_tbl_info *tbl;
  1648. int i;
  1649. int active_tbl;
  1650. int flush_interval_passed = 0;
  1651. struct iwl_priv *priv;
  1652. priv = lq_sta->drv;
  1653. active_tbl = lq_sta->active_tbl;
  1654. tbl = &(lq_sta->lq_info[active_tbl]);
  1655. /* If we've been disallowing search, see if we should now allow it */
  1656. if (lq_sta->stay_in_tbl) {
  1657. /* Elapsed time using current modulation mode */
  1658. if (lq_sta->flush_timer)
  1659. flush_interval_passed =
  1660. time_after(jiffies,
  1661. (unsigned long)(lq_sta->flush_timer +
  1662. IWL_RATE_SCALE_FLUSH_INTVL));
  1663. /*
  1664. * Check if we should allow search for new modulation mode.
  1665. * If many frames have failed or succeeded, or we've used
  1666. * this same modulation for a long time, allow search, and
  1667. * reset history stats that keep track of whether we should
  1668. * allow a new search. Also (below) reset all bitmaps and
  1669. * stats in active history.
  1670. */
  1671. if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1672. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1673. ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
  1674. && (flush_interval_passed))) {
  1675. IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
  1676. lq_sta->total_failed,
  1677. lq_sta->total_success,
  1678. flush_interval_passed);
  1679. /* Allow search for new mode */
  1680. lq_sta->stay_in_tbl = 0; /* only place reset */
  1681. lq_sta->total_failed = 0;
  1682. lq_sta->total_success = 0;
  1683. lq_sta->flush_timer = 0;
  1684. /*
  1685. * Else if we've used this modulation mode enough repetitions
  1686. * (regardless of elapsed time or success/failure), reset
  1687. * history bitmaps and rate-specific stats for all rates in
  1688. * active table.
  1689. */
  1690. } else {
  1691. lq_sta->table_count++;
  1692. if (lq_sta->table_count >=
  1693. lq_sta->table_count_limit) {
  1694. lq_sta->table_count = 0;
  1695. IWL_DEBUG_RATE(priv, "LQ: stay in table clear win\n");
  1696. for (i = 0; i < IWL_RATE_COUNT; i++)
  1697. rs_rate_scale_clear_window(
  1698. &(tbl->win[i]));
  1699. }
  1700. }
  1701. /* If transitioning to allow "search", reset all history
  1702. * bitmaps and stats in active table (this will become the new
  1703. * "search" table). */
  1704. if (!lq_sta->stay_in_tbl) {
  1705. for (i = 0; i < IWL_RATE_COUNT; i++)
  1706. rs_rate_scale_clear_window(&(tbl->win[i]));
  1707. }
  1708. }
  1709. }
  1710. /*
  1711. * setup rate table in uCode
  1712. * return rate_n_flags as used in the table
  1713. */
  1714. static u32 rs_update_rate_tbl(struct iwl_priv *priv,
  1715. struct iwl_lq_sta *lq_sta,
  1716. struct iwl_scale_tbl_info *tbl,
  1717. int index, u8 is_green)
  1718. {
  1719. u32 rate;
  1720. /* Update uCode's rate table. */
  1721. rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
  1722. rs_fill_link_cmd(priv, lq_sta, rate);
  1723. iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  1724. return rate;
  1725. }
  1726. /*
  1727. * Do rate scaling and search for new modulation mode.
  1728. */
  1729. static void rs_rate_scale_perform(struct iwl_priv *priv,
  1730. struct sk_buff *skb,
  1731. struct ieee80211_sta *sta,
  1732. struct iwl_lq_sta *lq_sta)
  1733. {
  1734. struct ieee80211_hw *hw = priv->hw;
  1735. struct ieee80211_conf *conf = &hw->conf;
  1736. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1737. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1738. int low = IWL_RATE_INVALID;
  1739. int high = IWL_RATE_INVALID;
  1740. int index;
  1741. int i;
  1742. struct iwl_rate_scale_data *window = NULL;
  1743. int current_tpt = IWL_INVALID_VALUE;
  1744. int low_tpt = IWL_INVALID_VALUE;
  1745. int high_tpt = IWL_INVALID_VALUE;
  1746. u32 fail_count;
  1747. s8 scale_action = 0;
  1748. u16 rate_mask;
  1749. u8 update_lq = 0;
  1750. struct iwl_scale_tbl_info *tbl, *tbl1;
  1751. u16 rate_scale_index_msk = 0;
  1752. u32 rate;
  1753. u8 is_green = 0;
  1754. u8 active_tbl = 0;
  1755. u8 done_search = 0;
  1756. u16 high_low;
  1757. s32 sr;
  1758. u8 tid = MAX_TID_COUNT;
  1759. struct iwl_tid_data *tid_data;
  1760. IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
  1761. /* Send management frames and NO_ACK data using lowest rate. */
  1762. /* TODO: this could probably be improved.. */
  1763. if (!ieee80211_is_data(hdr->frame_control) ||
  1764. info->flags & IEEE80211_TX_CTL_NO_ACK)
  1765. return;
  1766. if (!sta || !lq_sta)
  1767. return;
  1768. lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
  1769. tid = rs_tl_add_packet(lq_sta, hdr);
  1770. /*
  1771. * Select rate-scale / modulation-mode table to work with in
  1772. * the rest of this function: "search" if searching for better
  1773. * modulation mode, or "active" if doing rate scaling within a mode.
  1774. */
  1775. if (!lq_sta->search_better_tbl)
  1776. active_tbl = lq_sta->active_tbl;
  1777. else
  1778. active_tbl = 1 - lq_sta->active_tbl;
  1779. tbl = &(lq_sta->lq_info[active_tbl]);
  1780. if (is_legacy(tbl->lq_type))
  1781. lq_sta->is_green = 0;
  1782. else
  1783. lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
  1784. is_green = lq_sta->is_green;
  1785. /* current tx rate */
  1786. index = lq_sta->last_txrate_idx;
  1787. IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
  1788. tbl->lq_type);
  1789. /* rates available for this association, and for modulation mode */
  1790. rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
  1791. IWL_DEBUG_RATE(priv, "mask 0x%04X \n", rate_mask);
  1792. /* mask with station rate restriction */
  1793. if (is_legacy(tbl->lq_type)) {
  1794. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1795. /* supp_rates has no CCK bits in A mode */
  1796. rate_scale_index_msk = (u16) (rate_mask &
  1797. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  1798. else
  1799. rate_scale_index_msk = (u16) (rate_mask &
  1800. lq_sta->supp_rates);
  1801. } else
  1802. rate_scale_index_msk = rate_mask;
  1803. if (!rate_scale_index_msk)
  1804. rate_scale_index_msk = rate_mask;
  1805. if (!((1 << index) & rate_scale_index_msk)) {
  1806. IWL_ERR(priv, "Current Rate is not valid\n");
  1807. if (lq_sta->search_better_tbl) {
  1808. /* revert to active table if search table is not valid*/
  1809. tbl->lq_type = LQ_NONE;
  1810. lq_sta->search_better_tbl = 0;
  1811. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1812. /* get "active" rate info */
  1813. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1814. rate = rs_update_rate_tbl(priv, lq_sta,
  1815. tbl, index, is_green);
  1816. }
  1817. return;
  1818. }
  1819. /* Get expected throughput table and history window for current rate */
  1820. if (!tbl->expected_tpt) {
  1821. IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
  1822. return;
  1823. }
  1824. /* force user max rate if set by user */
  1825. if ((lq_sta->max_rate_idx != -1) &&
  1826. (lq_sta->max_rate_idx < index)) {
  1827. index = lq_sta->max_rate_idx;
  1828. update_lq = 1;
  1829. window = &(tbl->win[index]);
  1830. goto lq_update;
  1831. }
  1832. window = &(tbl->win[index]);
  1833. /*
  1834. * If there is not enough history to calculate actual average
  1835. * throughput, keep analyzing results of more tx frames, without
  1836. * changing rate or mode (bypass most of the rest of this function).
  1837. * Set up new rate table in uCode only if old rate is not supported
  1838. * in current association (use new rate found above).
  1839. */
  1840. fail_count = window->counter - window->success_counter;
  1841. if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1842. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
  1843. IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
  1844. "for index %d\n",
  1845. window->success_counter, window->counter, index);
  1846. /* Can't calculate this yet; not enough history */
  1847. window->average_tpt = IWL_INVALID_VALUE;
  1848. /* Should we stay with this modulation mode,
  1849. * or search for a new one? */
  1850. rs_stay_in_table(lq_sta);
  1851. goto out;
  1852. }
  1853. /* Else we have enough samples; calculate estimate of
  1854. * actual average throughput */
  1855. BUG_ON(window->average_tpt != ((window->success_ratio *
  1856. tbl->expected_tpt[index] + 64) / 128));
  1857. /* If we are searching for better modulation mode, check success. */
  1858. if (lq_sta->search_better_tbl &&
  1859. (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI)) {
  1860. /* If good success, continue using the "search" mode;
  1861. * no need to send new link quality command, since we're
  1862. * continuing to use the setup that we've been trying. */
  1863. if (window->average_tpt > lq_sta->last_tpt) {
  1864. IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
  1865. "suc=%d cur-tpt=%d old-tpt=%d\n",
  1866. window->success_ratio,
  1867. window->average_tpt,
  1868. lq_sta->last_tpt);
  1869. if (!is_legacy(tbl->lq_type))
  1870. lq_sta->enable_counter = 1;
  1871. /* Swap tables; "search" becomes "active" */
  1872. lq_sta->active_tbl = active_tbl;
  1873. current_tpt = window->average_tpt;
  1874. /* Else poor success; go back to mode in "active" table */
  1875. } else {
  1876. IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
  1877. "suc=%d cur-tpt=%d old-tpt=%d\n",
  1878. window->success_ratio,
  1879. window->average_tpt,
  1880. lq_sta->last_tpt);
  1881. /* Nullify "search" table */
  1882. tbl->lq_type = LQ_NONE;
  1883. /* Revert to "active" table */
  1884. active_tbl = lq_sta->active_tbl;
  1885. tbl = &(lq_sta->lq_info[active_tbl]);
  1886. /* Revert to "active" rate and throughput info */
  1887. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1888. current_tpt = lq_sta->last_tpt;
  1889. /* Need to set up a new rate table in uCode */
  1890. update_lq = 1;
  1891. }
  1892. /* Either way, we've made a decision; modulation mode
  1893. * search is done, allow rate adjustment next time. */
  1894. lq_sta->search_better_tbl = 0;
  1895. done_search = 1; /* Don't switch modes below! */
  1896. goto lq_update;
  1897. }
  1898. /* (Else) not in search of better modulation mode, try for better
  1899. * starting rate, while staying in this mode. */
  1900. high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
  1901. tbl->lq_type);
  1902. low = high_low & 0xff;
  1903. high = (high_low >> 8) & 0xff;
  1904. /* If user set max rate, dont allow higher than user constrain */
  1905. if ((lq_sta->max_rate_idx != -1) &&
  1906. (lq_sta->max_rate_idx < high))
  1907. high = IWL_RATE_INVALID;
  1908. sr = window->success_ratio;
  1909. /* Collect measured throughputs for current and adjacent rates */
  1910. current_tpt = window->average_tpt;
  1911. if (low != IWL_RATE_INVALID)
  1912. low_tpt = tbl->win[low].average_tpt;
  1913. if (high != IWL_RATE_INVALID)
  1914. high_tpt = tbl->win[high].average_tpt;
  1915. scale_action = 0;
  1916. /* Too many failures, decrease rate */
  1917. if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
  1918. IWL_DEBUG_RATE(priv, "decrease rate because of low success_ratio\n");
  1919. scale_action = -1;
  1920. /* No throughput measured yet for adjacent rates; try increase. */
  1921. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  1922. (high_tpt == IWL_INVALID_VALUE)) {
  1923. if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
  1924. scale_action = 1;
  1925. else if (low != IWL_RATE_INVALID)
  1926. scale_action = 0;
  1927. }
  1928. /* Both adjacent throughputs are measured, but neither one has better
  1929. * throughput; we're using the best rate, don't change it! */
  1930. else if ((low_tpt != IWL_INVALID_VALUE) &&
  1931. (high_tpt != IWL_INVALID_VALUE) &&
  1932. (low_tpt < current_tpt) &&
  1933. (high_tpt < current_tpt))
  1934. scale_action = 0;
  1935. /* At least one adjacent rate's throughput is measured,
  1936. * and may have better performance. */
  1937. else {
  1938. /* Higher adjacent rate's throughput is measured */
  1939. if (high_tpt != IWL_INVALID_VALUE) {
  1940. /* Higher rate has better throughput */
  1941. if (high_tpt > current_tpt &&
  1942. sr >= IWL_RATE_INCREASE_TH) {
  1943. scale_action = 1;
  1944. } else {
  1945. scale_action = 0;
  1946. }
  1947. /* Lower adjacent rate's throughput is measured */
  1948. } else if (low_tpt != IWL_INVALID_VALUE) {
  1949. /* Lower rate has better throughput */
  1950. if (low_tpt > current_tpt) {
  1951. IWL_DEBUG_RATE(priv,
  1952. "decrease rate because of low tpt\n");
  1953. scale_action = -1;
  1954. } else if (sr >= IWL_RATE_INCREASE_TH) {
  1955. scale_action = 1;
  1956. }
  1957. }
  1958. }
  1959. /* Sanity check; asked for decrease, but success rate or throughput
  1960. * has been good at old rate. Don't change it. */
  1961. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  1962. ((sr > IWL_RATE_HIGH_TH) ||
  1963. (current_tpt > (100 * tbl->expected_tpt[low]))))
  1964. scale_action = 0;
  1965. if (!iwl_ht_enabled(priv) && !is_legacy(tbl->lq_type))
  1966. scale_action = -1;
  1967. if (iwl_tx_ant_restriction(priv) != IWL_ANT_OK_MULTI &&
  1968. (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type)))
  1969. scale_action = -1;
  1970. switch (scale_action) {
  1971. case -1:
  1972. /* Decrease starting rate, update uCode's rate table */
  1973. if (low != IWL_RATE_INVALID) {
  1974. update_lq = 1;
  1975. index = low;
  1976. }
  1977. break;
  1978. case 1:
  1979. /* Increase starting rate, update uCode's rate table */
  1980. if (high != IWL_RATE_INVALID) {
  1981. update_lq = 1;
  1982. index = high;
  1983. }
  1984. break;
  1985. case 0:
  1986. /* No change */
  1987. default:
  1988. break;
  1989. }
  1990. IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
  1991. "high %d type %d\n",
  1992. index, scale_action, low, high, tbl->lq_type);
  1993. lq_update:
  1994. /* Replace uCode's rate table for the destination station. */
  1995. if (update_lq)
  1996. rate = rs_update_rate_tbl(priv, lq_sta,
  1997. tbl, index, is_green);
  1998. if (iwl_tx_ant_restriction(priv) == IWL_ANT_OK_MULTI) {
  1999. /* Should we stay with this modulation mode,
  2000. * or search for a new one? */
  2001. rs_stay_in_table(lq_sta);
  2002. }
  2003. /*
  2004. * Search for new modulation mode if we're:
  2005. * 1) Not changing rates right now
  2006. * 2) Not just finishing up a search
  2007. * 3) Allowing a new search
  2008. */
  2009. if (!update_lq && !done_search && !lq_sta->stay_in_tbl && window->counter) {
  2010. /* Save current throughput to compare with "search" throughput*/
  2011. lq_sta->last_tpt = current_tpt;
  2012. /* Select a new "search" modulation mode to try.
  2013. * If one is found, set up the new "search" table. */
  2014. if (is_legacy(tbl->lq_type))
  2015. rs_move_legacy_other(priv, lq_sta, conf, sta, index);
  2016. else if (is_siso(tbl->lq_type))
  2017. rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
  2018. else if (is_mimo2(tbl->lq_type))
  2019. rs_move_mimo2_to_other(priv, lq_sta, conf, sta, index);
  2020. else
  2021. rs_move_mimo3_to_other(priv, lq_sta, conf, sta, index);
  2022. /* If new "search" mode was selected, set up in uCode table */
  2023. if (lq_sta->search_better_tbl) {
  2024. /* Access the "search" table, clear its history. */
  2025. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  2026. for (i = 0; i < IWL_RATE_COUNT; i++)
  2027. rs_rate_scale_clear_window(&(tbl->win[i]));
  2028. /* Use new "search" start rate */
  2029. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  2030. IWL_DEBUG_RATE(priv, "Switch current mcs: %X index: %d\n",
  2031. tbl->current_rate, index);
  2032. rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
  2033. iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  2034. } else
  2035. done_search = 1;
  2036. }
  2037. if (done_search && !lq_sta->stay_in_tbl) {
  2038. /* If the "active" (non-search) mode was legacy,
  2039. * and we've tried switching antennas,
  2040. * but we haven't been able to try HT modes (not available),
  2041. * stay with best antenna legacy modulation for a while
  2042. * before next round of mode comparisons. */
  2043. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2044. if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
  2045. lq_sta->action_counter > tbl1->max_search) {
  2046. IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
  2047. rs_set_stay_in_table(priv, 1, lq_sta);
  2048. }
  2049. /* If we're in an HT mode, and all 3 mode switch actions
  2050. * have been tried and compared, stay in this best modulation
  2051. * mode for a while before next round of mode comparisons. */
  2052. if (lq_sta->enable_counter &&
  2053. (lq_sta->action_counter >= tbl1->max_search) &&
  2054. iwl_ht_enabled(priv)) {
  2055. if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
  2056. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  2057. (tid != MAX_TID_COUNT)) {
  2058. tid_data =
  2059. &priv->stations[lq_sta->lq.sta_id].tid[tid];
  2060. if (tid_data->agg.state == IWL_AGG_OFF) {
  2061. IWL_DEBUG_RATE(priv,
  2062. "try to aggregate tid %d\n",
  2063. tid);
  2064. rs_tl_turn_on_agg(priv, tid,
  2065. lq_sta, sta);
  2066. }
  2067. }
  2068. rs_set_stay_in_table(priv, 0, lq_sta);
  2069. }
  2070. }
  2071. out:
  2072. tbl->current_rate = rate_n_flags_from_tbl(priv, tbl, index, is_green);
  2073. i = index;
  2074. lq_sta->last_txrate_idx = i;
  2075. return;
  2076. }
  2077. static void rs_initialize_lq(struct iwl_priv *priv,
  2078. struct ieee80211_conf *conf,
  2079. struct ieee80211_sta *sta,
  2080. struct iwl_lq_sta *lq_sta)
  2081. {
  2082. struct iwl_scale_tbl_info *tbl;
  2083. int rate_idx;
  2084. int i;
  2085. u32 rate;
  2086. u8 use_green = rs_use_green(sta, &priv->current_ht_config);
  2087. u8 active_tbl = 0;
  2088. u8 valid_tx_ant;
  2089. if (!sta || !lq_sta)
  2090. goto out;
  2091. i = lq_sta->last_txrate_idx;
  2092. if ((lq_sta->lq.sta_id == 0xff) &&
  2093. (priv->iw_mode == NL80211_IFTYPE_ADHOC))
  2094. goto out;
  2095. valid_tx_ant = priv->hw_params.valid_tx_ant;
  2096. if (!lq_sta->search_better_tbl)
  2097. active_tbl = lq_sta->active_tbl;
  2098. else
  2099. active_tbl = 1 - lq_sta->active_tbl;
  2100. tbl = &(lq_sta->lq_info[active_tbl]);
  2101. if ((i < 0) || (i >= IWL_RATE_COUNT))
  2102. i = 0;
  2103. rate = iwl_rates[i].plcp;
  2104. tbl->ant_type = first_antenna(valid_tx_ant);
  2105. rate |= tbl->ant_type << RATE_MCS_ANT_POS;
  2106. if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
  2107. rate |= RATE_MCS_CCK_MSK;
  2108. rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
  2109. if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
  2110. rs_toggle_antenna(valid_tx_ant, &rate, tbl);
  2111. rate = rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
  2112. tbl->current_rate = rate;
  2113. rs_set_expected_tpt_table(lq_sta, tbl);
  2114. rs_fill_link_cmd(NULL, lq_sta, rate);
  2115. iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  2116. out:
  2117. return;
  2118. }
  2119. static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
  2120. struct ieee80211_tx_rate_control *txrc)
  2121. {
  2122. struct sk_buff *skb = txrc->skb;
  2123. struct ieee80211_supported_band *sband = txrc->sband;
  2124. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  2125. struct ieee80211_conf *conf = &priv->hw->conf;
  2126. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  2127. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2128. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2129. struct iwl_lq_sta *lq_sta = priv_sta;
  2130. int rate_idx;
  2131. IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
  2132. /* Get max rate if user set max rate */
  2133. if (lq_sta) {
  2134. lq_sta->max_rate_idx = txrc->max_rate_idx;
  2135. if ((sband->band == IEEE80211_BAND_5GHZ) &&
  2136. (lq_sta->max_rate_idx != -1))
  2137. lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
  2138. if ((lq_sta->max_rate_idx < 0) ||
  2139. (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
  2140. lq_sta->max_rate_idx = -1;
  2141. }
  2142. /* Send management frames and NO_ACK data using lowest rate. */
  2143. if (rate_control_send_low(sta, priv_sta, txrc))
  2144. return;
  2145. rate_idx = lq_sta->last_txrate_idx;
  2146. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
  2147. !lq_sta->ibss_sta_added) {
  2148. u8 sta_id = iwl_find_station(priv, hdr->addr1);
  2149. if (sta_id == IWL_INVALID_STATION) {
  2150. IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n",
  2151. hdr->addr1);
  2152. sta_id = iwl_add_station(priv, hdr->addr1,
  2153. false, CMD_ASYNC, ht_cap);
  2154. }
  2155. if ((sta_id != IWL_INVALID_STATION)) {
  2156. lq_sta->lq.sta_id = sta_id;
  2157. lq_sta->lq.rs_table[0].rate_n_flags = 0;
  2158. lq_sta->ibss_sta_added = 1;
  2159. rs_initialize_lq(priv, conf, sta, lq_sta);
  2160. }
  2161. }
  2162. if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
  2163. rate_idx -= IWL_FIRST_OFDM_RATE;
  2164. /* 6M and 9M shared same MCS index */
  2165. rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
  2166. if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
  2167. IWL_RATE_MIMO3_6M_PLCP)
  2168. rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
  2169. else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
  2170. IWL_RATE_MIMO2_6M_PLCP)
  2171. rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
  2172. info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  2173. if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
  2174. info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
  2175. if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
  2176. info->control.rates[0].flags |= IEEE80211_TX_RC_DUP_DATA;
  2177. if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
  2178. info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  2179. if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
  2180. info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
  2181. } else {
  2182. if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT)
  2183. rate_idx = rate_lowest_index(sband, sta);
  2184. else if (sband->band == IEEE80211_BAND_5GHZ)
  2185. rate_idx -= IWL_FIRST_OFDM_RATE;
  2186. }
  2187. info->control.rates[0].idx = rate_idx;
  2188. }
  2189. static void *rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
  2190. gfp_t gfp)
  2191. {
  2192. struct iwl_lq_sta *lq_sta;
  2193. struct iwl_priv *priv;
  2194. int i, j;
  2195. priv = (struct iwl_priv *)priv_rate;
  2196. IWL_DEBUG_RATE(priv, "create station rate scale window\n");
  2197. lq_sta = kzalloc(sizeof(struct iwl_lq_sta), gfp);
  2198. if (lq_sta == NULL)
  2199. return NULL;
  2200. lq_sta->lq.sta_id = 0xff;
  2201. for (j = 0; j < LQ_SIZE; j++)
  2202. for (i = 0; i < IWL_RATE_COUNT; i++)
  2203. rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
  2204. return lq_sta;
  2205. }
  2206. static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
  2207. struct ieee80211_sta *sta, void *priv_sta)
  2208. {
  2209. int i, j;
  2210. struct iwl_priv *priv = (struct iwl_priv *)priv_r;
  2211. struct ieee80211_conf *conf = &priv->hw->conf;
  2212. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  2213. struct iwl_lq_sta *lq_sta = priv_sta;
  2214. u16 mask_bit = 0;
  2215. int count;
  2216. int start_rate = 0;
  2217. lq_sta->flush_timer = 0;
  2218. lq_sta->supp_rates = sta->supp_rates[sband->band];
  2219. for (j = 0; j < LQ_SIZE; j++)
  2220. for (i = 0; i < IWL_RATE_COUNT; i++)
  2221. rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
  2222. IWL_DEBUG_RATE(priv, "LQ: *** rate scale station global init ***\n");
  2223. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2224. * the lowest or the highest rate.. Could consider using RSSI from
  2225. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2226. * after assoc.. */
  2227. lq_sta->ibss_sta_added = 0;
  2228. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  2229. u8 sta_id = iwl_find_station(priv,
  2230. sta->addr);
  2231. /* for IBSS the call are from tasklet */
  2232. IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
  2233. if (sta_id == IWL_INVALID_STATION) {
  2234. IWL_DEBUG_RATE(priv, "LQ: ADD station %pM\n", sta->addr);
  2235. sta_id = iwl_add_station(priv, sta->addr, false,
  2236. CMD_ASYNC, ht_cap);
  2237. }
  2238. if ((sta_id != IWL_INVALID_STATION)) {
  2239. lq_sta->lq.sta_id = sta_id;
  2240. lq_sta->lq.rs_table[0].rate_n_flags = 0;
  2241. }
  2242. /* FIXME: this is w/a remove it later */
  2243. priv->assoc_station_added = 1;
  2244. }
  2245. lq_sta->is_dup = 0;
  2246. lq_sta->max_rate_idx = -1;
  2247. lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
  2248. lq_sta->is_green = rs_use_green(sta, &priv->current_ht_config);
  2249. lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
  2250. lq_sta->active_rate_basic = priv->active_rate_basic;
  2251. lq_sta->band = priv->band;
  2252. /*
  2253. * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
  2254. * supp_rates[] does not; shift to convert format, force 9 MBits off.
  2255. */
  2256. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2257. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2258. lq_sta->active_siso_rate &= ~((u16)0x2);
  2259. lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
  2260. /* Same here */
  2261. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2262. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2263. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2264. lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
  2265. lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
  2266. lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
  2267. lq_sta->active_mimo3_rate &= ~((u16)0x2);
  2268. lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
  2269. IWL_DEBUG_RATE(priv, "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
  2270. lq_sta->active_siso_rate,
  2271. lq_sta->active_mimo2_rate,
  2272. lq_sta->active_mimo3_rate);
  2273. /* These values will be overridden later */
  2274. lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
  2275. lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
  2276. /* as default allow aggregation for all tids */
  2277. lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
  2278. lq_sta->drv = priv;
  2279. /* Find highest tx rate supported by hardware and destination station */
  2280. mask_bit = sta->supp_rates[sband->band];
  2281. count = sband->n_bitrates;
  2282. if (sband->band == IEEE80211_BAND_5GHZ) {
  2283. count += IWL_FIRST_OFDM_RATE;
  2284. start_rate = IWL_FIRST_OFDM_RATE;
  2285. mask_bit <<= IWL_FIRST_OFDM_RATE;
  2286. }
  2287. mask_bit = mask_bit & lq_sta->active_legacy_rate;
  2288. lq_sta->last_txrate_idx = 4;
  2289. for (i = start_rate; i < count; i++)
  2290. if (mask_bit & BIT(i))
  2291. lq_sta->last_txrate_idx = i;
  2292. rs_initialize_lq(priv, conf, sta, lq_sta);
  2293. }
  2294. static void rs_fill_link_cmd(struct iwl_priv *priv,
  2295. struct iwl_lq_sta *lq_sta, u32 new_rate)
  2296. {
  2297. struct iwl_scale_tbl_info tbl_type;
  2298. int index = 0;
  2299. int rate_idx;
  2300. int repeat_rate = 0;
  2301. u8 ant_toggle_cnt = 0;
  2302. u8 use_ht_possible = 1;
  2303. u8 valid_tx_ant = 0;
  2304. struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
  2305. /* Override starting rate (index 0) if needed for debug purposes */
  2306. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  2307. /* Interpret new_rate (rate_n_flags) */
  2308. memset(&tbl_type, 0, sizeof(tbl_type));
  2309. rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
  2310. &tbl_type, &rate_idx);
  2311. /* How many times should we repeat the initial rate? */
  2312. if (is_legacy(tbl_type.lq_type)) {
  2313. ant_toggle_cnt = 1;
  2314. repeat_rate = IWL_NUMBER_TRY;
  2315. } else {
  2316. repeat_rate = IWL_HT_NUMBER_TRY;
  2317. }
  2318. lq_cmd->general_params.mimo_delimiter =
  2319. is_mimo(tbl_type.lq_type) ? 1 : 0;
  2320. /* Fill 1st table entry (index 0) */
  2321. lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
  2322. if (num_of_ant(tbl_type.ant_type) == 1) {
  2323. lq_cmd->general_params.single_stream_ant_msk =
  2324. tbl_type.ant_type;
  2325. } else if (num_of_ant(tbl_type.ant_type) == 2) {
  2326. lq_cmd->general_params.dual_stream_ant_msk =
  2327. tbl_type.ant_type;
  2328. } /* otherwise we don't modify the existing value */
  2329. index++;
  2330. repeat_rate--;
  2331. if (priv)
  2332. valid_tx_ant = priv->hw_params.valid_tx_ant;
  2333. /* Fill rest of rate table */
  2334. while (index < LINK_QUAL_MAX_RETRY_NUM) {
  2335. /* Repeat initial/next rate.
  2336. * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
  2337. * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
  2338. while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
  2339. if (is_legacy(tbl_type.lq_type)) {
  2340. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2341. ant_toggle_cnt++;
  2342. else if (priv &&
  2343. rs_toggle_antenna(valid_tx_ant,
  2344. &new_rate, &tbl_type))
  2345. ant_toggle_cnt = 1;
  2346. }
  2347. /* Override next rate if needed for debug purposes */
  2348. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  2349. /* Fill next table entry */
  2350. lq_cmd->rs_table[index].rate_n_flags =
  2351. cpu_to_le32(new_rate);
  2352. repeat_rate--;
  2353. index++;
  2354. }
  2355. rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
  2356. &rate_idx);
  2357. /* Indicate to uCode which entries might be MIMO.
  2358. * If initial rate was MIMO, this will finally end up
  2359. * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  2360. if (is_mimo(tbl_type.lq_type))
  2361. lq_cmd->general_params.mimo_delimiter = index;
  2362. /* Get next rate */
  2363. new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
  2364. use_ht_possible);
  2365. /* How many times should we repeat the next rate? */
  2366. if (is_legacy(tbl_type.lq_type)) {
  2367. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2368. ant_toggle_cnt++;
  2369. else if (priv &&
  2370. rs_toggle_antenna(valid_tx_ant,
  2371. &new_rate, &tbl_type))
  2372. ant_toggle_cnt = 1;
  2373. repeat_rate = IWL_NUMBER_TRY;
  2374. } else {
  2375. repeat_rate = IWL_HT_NUMBER_TRY;
  2376. }
  2377. /* Don't allow HT rates after next pass.
  2378. * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
  2379. use_ht_possible = 0;
  2380. /* Override next rate if needed for debug purposes */
  2381. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  2382. /* Fill next table entry */
  2383. lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
  2384. index++;
  2385. repeat_rate--;
  2386. }
  2387. lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_MAX;
  2388. lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2389. lq_cmd->agg_params.agg_time_limit =
  2390. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2391. }
  2392. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2393. {
  2394. return hw->priv;
  2395. }
  2396. /* rate scale requires free function to be implemented */
  2397. static void rs_free(void *priv_rate)
  2398. {
  2399. return;
  2400. }
  2401. static void rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
  2402. void *priv_sta)
  2403. {
  2404. struct iwl_lq_sta *lq_sta = priv_sta;
  2405. struct iwl_priv *priv __maybe_unused = priv_r;
  2406. IWL_DEBUG_RATE(priv, "enter\n");
  2407. kfree(lq_sta);
  2408. IWL_DEBUG_RATE(priv, "leave\n");
  2409. }
  2410. #ifdef CONFIG_MAC80211_DEBUGFS
  2411. static int open_file_generic(struct inode *inode, struct file *file)
  2412. {
  2413. file->private_data = inode->i_private;
  2414. return 0;
  2415. }
  2416. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  2417. u32 *rate_n_flags, int index)
  2418. {
  2419. struct iwl_priv *priv;
  2420. u8 valid_tx_ant;
  2421. u8 ant_sel_tx;
  2422. priv = lq_sta->drv;
  2423. valid_tx_ant = priv->hw_params.valid_tx_ant;
  2424. if (lq_sta->dbg_fixed_rate) {
  2425. ant_sel_tx =
  2426. ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
  2427. >> RATE_MCS_ANT_POS);
  2428. if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
  2429. *rate_n_flags = lq_sta->dbg_fixed_rate;
  2430. IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
  2431. } else {
  2432. lq_sta->dbg_fixed_rate = 0;
  2433. IWL_ERR(priv,
  2434. "Invalid antenna selection 0x%X, Valid is 0x%X\n",
  2435. ant_sel_tx, valid_tx_ant);
  2436. IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
  2437. }
  2438. } else {
  2439. IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
  2440. }
  2441. }
  2442. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  2443. const char __user *user_buf, size_t count, loff_t *ppos)
  2444. {
  2445. struct iwl_lq_sta *lq_sta = file->private_data;
  2446. struct iwl_priv *priv;
  2447. char buf[64];
  2448. int buf_size;
  2449. u32 parsed_rate;
  2450. priv = lq_sta->drv;
  2451. memset(buf, 0, sizeof(buf));
  2452. buf_size = min(count, sizeof(buf) - 1);
  2453. if (copy_from_user(buf, user_buf, buf_size))
  2454. return -EFAULT;
  2455. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2456. lq_sta->dbg_fixed_rate = parsed_rate;
  2457. else
  2458. lq_sta->dbg_fixed_rate = 0;
  2459. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  2460. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2461. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2462. lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2463. IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
  2464. lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
  2465. if (lq_sta->dbg_fixed_rate) {
  2466. rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
  2467. iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
  2468. }
  2469. return count;
  2470. }
  2471. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  2472. char __user *user_buf, size_t count, loff_t *ppos)
  2473. {
  2474. char *buff;
  2475. int desc = 0;
  2476. int i = 0;
  2477. int index = 0;
  2478. ssize_t ret;
  2479. struct iwl_lq_sta *lq_sta = file->private_data;
  2480. struct iwl_priv *priv;
  2481. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2482. priv = lq_sta->drv;
  2483. buff = kmalloc(1024, GFP_KERNEL);
  2484. if (!buff)
  2485. return -ENOMEM;
  2486. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2487. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  2488. lq_sta->total_failed, lq_sta->total_success,
  2489. lq_sta->active_legacy_rate);
  2490. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2491. lq_sta->dbg_fixed_rate);
  2492. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  2493. (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
  2494. (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
  2495. (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
  2496. desc += sprintf(buff+desc, "lq type %s\n",
  2497. (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
  2498. if (is_Ht(tbl->lq_type)) {
  2499. desc += sprintf(buff+desc, " %s",
  2500. (is_siso(tbl->lq_type)) ? "SISO" :
  2501. ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
  2502. desc += sprintf(buff+desc, " %s",
  2503. (tbl->is_ht40) ? "40MHz" : "20MHz");
  2504. desc += sprintf(buff+desc, " %s %s\n", (tbl->is_SGI) ? "SGI" : "",
  2505. (lq_sta->is_green) ? "GF enabled" : "");
  2506. }
  2507. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  2508. lq_sta->last_rate_n_flags);
  2509. desc += sprintf(buff+desc, "general:"
  2510. "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  2511. lq_sta->lq.general_params.flags,
  2512. lq_sta->lq.general_params.mimo_delimiter,
  2513. lq_sta->lq.general_params.single_stream_ant_msk,
  2514. lq_sta->lq.general_params.dual_stream_ant_msk);
  2515. desc += sprintf(buff+desc, "agg:"
  2516. "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2517. le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
  2518. lq_sta->lq.agg_params.agg_dis_start_th,
  2519. lq_sta->lq.agg_params.agg_frame_cnt_limit);
  2520. desc += sprintf(buff+desc,
  2521. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2522. lq_sta->lq.general_params.start_rate_index[0],
  2523. lq_sta->lq.general_params.start_rate_index[1],
  2524. lq_sta->lq.general_params.start_rate_index[2],
  2525. lq_sta->lq.general_params.start_rate_index[3]);
  2526. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2527. index = iwl_hwrate_to_plcp_idx(
  2528. le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
  2529. if (is_legacy(tbl->lq_type)) {
  2530. desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
  2531. i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
  2532. iwl_rate_mcs[index].mbps);
  2533. } else {
  2534. desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps (%s)\n",
  2535. i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
  2536. iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
  2537. }
  2538. }
  2539. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2540. kfree(buff);
  2541. return ret;
  2542. }
  2543. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2544. .write = rs_sta_dbgfs_scale_table_write,
  2545. .read = rs_sta_dbgfs_scale_table_read,
  2546. .open = open_file_generic,
  2547. };
  2548. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  2549. char __user *user_buf, size_t count, loff_t *ppos)
  2550. {
  2551. char *buff;
  2552. int desc = 0;
  2553. int i, j;
  2554. ssize_t ret;
  2555. struct iwl_lq_sta *lq_sta = file->private_data;
  2556. buff = kmalloc(1024, GFP_KERNEL);
  2557. if (!buff)
  2558. return -ENOMEM;
  2559. for (i = 0; i < LQ_SIZE; i++) {
  2560. desc += sprintf(buff+desc,
  2561. "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
  2562. "rate=0x%X\n",
  2563. lq_sta->active_tbl == i ? "*" : "x",
  2564. lq_sta->lq_info[i].lq_type,
  2565. lq_sta->lq_info[i].is_SGI,
  2566. lq_sta->lq_info[i].is_ht40,
  2567. lq_sta->lq_info[i].is_dup,
  2568. lq_sta->is_green,
  2569. lq_sta->lq_info[i].current_rate);
  2570. for (j = 0; j < IWL_RATE_COUNT; j++) {
  2571. desc += sprintf(buff+desc,
  2572. "counter=%d success=%d %%=%d\n",
  2573. lq_sta->lq_info[i].win[j].counter,
  2574. lq_sta->lq_info[i].win[j].success_counter,
  2575. lq_sta->lq_info[i].win[j].success_ratio);
  2576. }
  2577. }
  2578. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2579. kfree(buff);
  2580. return ret;
  2581. }
  2582. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2583. .read = rs_sta_dbgfs_stats_table_read,
  2584. .open = open_file_generic,
  2585. };
  2586. static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
  2587. char __user *user_buf, size_t count, loff_t *ppos)
  2588. {
  2589. char buff[120];
  2590. int desc = 0;
  2591. ssize_t ret;
  2592. struct iwl_lq_sta *lq_sta = file->private_data;
  2593. struct iwl_priv *priv;
  2594. struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
  2595. priv = lq_sta->drv;
  2596. if (is_Ht(tbl->lq_type))
  2597. desc += sprintf(buff+desc,
  2598. "Bit Rate= %d Mb/s\n",
  2599. tbl->expected_tpt[lq_sta->last_txrate_idx]);
  2600. else
  2601. desc += sprintf(buff+desc,
  2602. "Bit Rate= %d Mb/s\n",
  2603. iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
  2604. desc += sprintf(buff+desc,
  2605. "Signal Level= %d dBm\tNoise Level= %d dBm\n",
  2606. priv->last_rx_rssi, priv->last_rx_noise);
  2607. desc += sprintf(buff+desc,
  2608. "Tsf= 0x%llx\tBeacon time= 0x%08X\n",
  2609. priv->last_tsf, priv->last_beacon_time);
  2610. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2611. return ret;
  2612. }
  2613. static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
  2614. .read = rs_sta_dbgfs_rate_scale_data_read,
  2615. .open = open_file_generic,
  2616. };
  2617. static void rs_add_debugfs(void *priv, void *priv_sta,
  2618. struct dentry *dir)
  2619. {
  2620. struct iwl_lq_sta *lq_sta = priv_sta;
  2621. lq_sta->rs_sta_dbgfs_scale_table_file =
  2622. debugfs_create_file("rate_scale_table", 0600, dir,
  2623. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2624. lq_sta->rs_sta_dbgfs_stats_table_file =
  2625. debugfs_create_file("rate_stats_table", 0600, dir,
  2626. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2627. lq_sta->rs_sta_dbgfs_rate_scale_data_file =
  2628. debugfs_create_file("rate_scale_data", 0600, dir,
  2629. lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
  2630. lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
  2631. debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
  2632. &lq_sta->tx_agg_tid_en);
  2633. }
  2634. static void rs_remove_debugfs(void *priv, void *priv_sta)
  2635. {
  2636. struct iwl_lq_sta *lq_sta = priv_sta;
  2637. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2638. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2639. debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
  2640. debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
  2641. }
  2642. #endif
  2643. static struct rate_control_ops rs_ops = {
  2644. .module = NULL,
  2645. .name = RS_NAME,
  2646. .tx_status = rs_tx_status,
  2647. .get_rate = rs_get_rate,
  2648. .rate_init = rs_rate_init,
  2649. .alloc = rs_alloc,
  2650. .free = rs_free,
  2651. .alloc_sta = rs_alloc_sta,
  2652. .free_sta = rs_free_sta,
  2653. #ifdef CONFIG_MAC80211_DEBUGFS
  2654. .add_sta_debugfs = rs_add_debugfs,
  2655. .remove_sta_debugfs = rs_remove_debugfs,
  2656. #endif
  2657. };
  2658. int iwlagn_rate_control_register(void)
  2659. {
  2660. return ieee80211_rate_control_register(&rs_ops);
  2661. }
  2662. void iwlagn_rate_control_unregister(void)
  2663. {
  2664. ieee80211_rate_control_unregister(&rs_ops);
  2665. }