iwl-agn-rs.c 94 KB

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