iwl-agn-rs.c 75 KB

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