iwl-agn-rs.c 87 KB

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