iwl-4965-rs.c 80 KB

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