iwl-agn-rs.c 87 KB

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