iwl-agn-rs.c 73 KB

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