iwl-agn-rs.c 86 KB

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