iwl-4965-rs.c 75 KB

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