iwl-agn-rs.c 88 KB

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