4965-rs.c 80 KB

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