iwl-4965-rs.c 76 KB

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