iwl-agn-rs.c 75 KB

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