iwl-4965-rs.c 84 KB

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