iwl-4965-rs.c 75 KB

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