iwl-agn-rs.c 87 KB

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