iwl-4965-rs.c 77 KB

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