iwl-agn-rs.c 75 KB

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