iwl-agn-rs.c 88 KB

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