iwl-agn-rs.c 74 KB

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