iwl-4965-rs.c 78 KB

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