sta_info.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. * Copyright 2013-2014 Intel Mobile Communications GmbH
  5. * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/timer.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/codel.h>
  22. #include <net/mac80211.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "rate.h"
  26. #include "sta_info.h"
  27. #include "debugfs_sta.h"
  28. #include "mesh.h"
  29. #include "wme.h"
  30. /**
  31. * DOC: STA information lifetime rules
  32. *
  33. * STA info structures (&struct sta_info) are managed in a hash table
  34. * for faster lookup and a list for iteration. They are managed using
  35. * RCU, i.e. access to the list and hash table is protected by RCU.
  36. *
  37. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  38. * owns that structure. It must then insert it into the hash table using
  39. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  40. * case (which acquires an rcu read section but must not be called from
  41. * within one) will the pointer still be valid after the call. Note that
  42. * the caller may not do much with the STA info before inserting it, in
  43. * particular, it may not start any mesh peer link management or add
  44. * encryption keys.
  45. *
  46. * When the insertion fails (sta_info_insert()) returns non-zero), the
  47. * structure will have been freed by sta_info_insert()!
  48. *
  49. * Station entries are added by mac80211 when you establish a link with a
  50. * peer. This means different things for the different type of interfaces
  51. * we support. For a regular station this mean we add the AP sta when we
  52. * receive an association response from the AP. For IBSS this occurs when
  53. * get to know about a peer on the same IBSS. For WDS we add the sta for
  54. * the peer immediately upon device open. When using AP mode we add stations
  55. * for each respective station upon request from userspace through nl80211.
  56. *
  57. * In order to remove a STA info structure, various sta_info_destroy_*()
  58. * calls are available.
  59. *
  60. * There is no concept of ownership on a STA entry, each structure is
  61. * owned by the global hash table/list until it is removed. All users of
  62. * the structure need to be RCU protected so that the structure won't be
  63. * freed before they are done using it.
  64. */
  65. static const struct rhashtable_params sta_rht_params = {
  66. .nelem_hint = 3, /* start small */
  67. .automatic_shrinking = true,
  68. .head_offset = offsetof(struct sta_info, hash_node),
  69. .key_offset = offsetof(struct sta_info, addr),
  70. .key_len = ETH_ALEN,
  71. .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
  72. };
  73. /* Caller must hold local->sta_mtx */
  74. static int sta_info_hash_del(struct ieee80211_local *local,
  75. struct sta_info *sta)
  76. {
  77. return rhltable_remove(&local->sta_hash, &sta->hash_node,
  78. sta_rht_params);
  79. }
  80. static void __cleanup_single_sta(struct sta_info *sta)
  81. {
  82. int ac, i;
  83. struct tid_ampdu_tx *tid_tx;
  84. struct ieee80211_sub_if_data *sdata = sta->sdata;
  85. struct ieee80211_local *local = sdata->local;
  86. struct fq *fq = &local->fq;
  87. struct ps_data *ps;
  88. if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
  89. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  90. test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
  91. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  92. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  93. ps = &sdata->bss->ps;
  94. else if (ieee80211_vif_is_mesh(&sdata->vif))
  95. ps = &sdata->u.mesh.ps;
  96. else
  97. return;
  98. clear_sta_flag(sta, WLAN_STA_PS_STA);
  99. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  100. clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
  101. atomic_dec(&ps->num_sta_ps);
  102. }
  103. if (sta->sta.txq[0]) {
  104. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  105. struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
  106. spin_lock_bh(&fq->lock);
  107. ieee80211_txq_purge(local, txqi);
  108. spin_unlock_bh(&fq->lock);
  109. }
  110. }
  111. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  112. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  113. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  114. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  115. }
  116. if (ieee80211_vif_is_mesh(&sdata->vif))
  117. mesh_sta_cleanup(sta);
  118. cancel_work_sync(&sta->drv_deliver_wk);
  119. /*
  120. * Destroy aggregation state here. It would be nice to wait for the
  121. * driver to finish aggregation stop and then clean up, but for now
  122. * drivers have to handle aggregation stop being requested, followed
  123. * directly by station destruction.
  124. */
  125. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  126. kfree(sta->ampdu_mlme.tid_start_tx[i]);
  127. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  128. if (!tid_tx)
  129. continue;
  130. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  131. kfree(tid_tx);
  132. }
  133. }
  134. static void cleanup_single_sta(struct sta_info *sta)
  135. {
  136. struct ieee80211_sub_if_data *sdata = sta->sdata;
  137. struct ieee80211_local *local = sdata->local;
  138. __cleanup_single_sta(sta);
  139. sta_info_free(local, sta);
  140. }
  141. struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
  142. const u8 *addr)
  143. {
  144. return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
  145. }
  146. /* protected by RCU */
  147. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  148. const u8 *addr)
  149. {
  150. struct ieee80211_local *local = sdata->local;
  151. struct rhlist_head *tmp;
  152. struct sta_info *sta;
  153. rcu_read_lock();
  154. for_each_sta_info(local, addr, sta, tmp) {
  155. if (sta->sdata == sdata) {
  156. rcu_read_unlock();
  157. /* this is safe as the caller must already hold
  158. * another rcu read section or the mutex
  159. */
  160. return sta;
  161. }
  162. }
  163. rcu_read_unlock();
  164. return NULL;
  165. }
  166. /*
  167. * Get sta info either from the specified interface
  168. * or from one of its vlans
  169. */
  170. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  171. const u8 *addr)
  172. {
  173. struct ieee80211_local *local = sdata->local;
  174. struct rhlist_head *tmp;
  175. struct sta_info *sta;
  176. rcu_read_lock();
  177. for_each_sta_info(local, addr, sta, tmp) {
  178. if (sta->sdata == sdata ||
  179. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
  180. rcu_read_unlock();
  181. /* this is safe as the caller must already hold
  182. * another rcu read section or the mutex
  183. */
  184. return sta;
  185. }
  186. }
  187. rcu_read_unlock();
  188. return NULL;
  189. }
  190. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  191. int idx)
  192. {
  193. struct ieee80211_local *local = sdata->local;
  194. struct sta_info *sta;
  195. int i = 0;
  196. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  197. if (sdata != sta->sdata)
  198. continue;
  199. if (i < idx) {
  200. ++i;
  201. continue;
  202. }
  203. return sta;
  204. }
  205. return NULL;
  206. }
  207. /**
  208. * sta_info_free - free STA
  209. *
  210. * @local: pointer to the global information
  211. * @sta: STA info to free
  212. *
  213. * This function must undo everything done by sta_info_alloc()
  214. * that may happen before sta_info_insert(). It may only be
  215. * called when sta_info_insert() has not been attempted (and
  216. * if that fails, the station is freed anyway.)
  217. */
  218. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  219. {
  220. if (sta->rate_ctrl)
  221. rate_control_free_sta(sta);
  222. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  223. if (sta->sta.txq[0])
  224. kfree(to_txq_info(sta->sta.txq[0]));
  225. kfree(rcu_dereference_raw(sta->sta.rates));
  226. #ifdef CONFIG_MAC80211_MESH
  227. kfree(sta->mesh);
  228. #endif
  229. free_percpu(sta->pcpu_rx_stats);
  230. kfree(sta);
  231. }
  232. /* Caller must hold local->sta_mtx */
  233. static int sta_info_hash_add(struct ieee80211_local *local,
  234. struct sta_info *sta)
  235. {
  236. return rhltable_insert(&local->sta_hash, &sta->hash_node,
  237. sta_rht_params);
  238. }
  239. static void sta_deliver_ps_frames(struct work_struct *wk)
  240. {
  241. struct sta_info *sta;
  242. sta = container_of(wk, struct sta_info, drv_deliver_wk);
  243. if (sta->dead)
  244. return;
  245. local_bh_disable();
  246. if (!test_sta_flag(sta, WLAN_STA_PS_STA))
  247. ieee80211_sta_ps_deliver_wakeup(sta);
  248. else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
  249. ieee80211_sta_ps_deliver_poll_response(sta);
  250. else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
  251. ieee80211_sta_ps_deliver_uapsd(sta);
  252. local_bh_enable();
  253. }
  254. static int sta_prepare_rate_control(struct ieee80211_local *local,
  255. struct sta_info *sta, gfp_t gfp)
  256. {
  257. if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
  258. return 0;
  259. sta->rate_ctrl = local->rate_ctrl;
  260. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  261. sta, gfp);
  262. if (!sta->rate_ctrl_priv)
  263. return -ENOMEM;
  264. return 0;
  265. }
  266. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  267. const u8 *addr, gfp_t gfp)
  268. {
  269. struct ieee80211_local *local = sdata->local;
  270. struct ieee80211_hw *hw = &local->hw;
  271. struct sta_info *sta;
  272. int i;
  273. sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
  274. if (!sta)
  275. return NULL;
  276. if (ieee80211_hw_check(hw, USES_RSS)) {
  277. sta->pcpu_rx_stats =
  278. alloc_percpu(struct ieee80211_sta_rx_stats);
  279. if (!sta->pcpu_rx_stats)
  280. goto free;
  281. }
  282. spin_lock_init(&sta->lock);
  283. spin_lock_init(&sta->ps_lock);
  284. INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
  285. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  286. mutex_init(&sta->ampdu_mlme.mtx);
  287. #ifdef CONFIG_MAC80211_MESH
  288. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  289. sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
  290. if (!sta->mesh)
  291. goto free;
  292. spin_lock_init(&sta->mesh->plink_lock);
  293. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  294. !sdata->u.mesh.user_mpm)
  295. init_timer(&sta->mesh->plink_timer);
  296. sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
  297. }
  298. #endif
  299. memcpy(sta->addr, addr, ETH_ALEN);
  300. memcpy(sta->sta.addr, addr, ETH_ALEN);
  301. sta->sta.max_rx_aggregation_subframes =
  302. local->hw.max_rx_aggregation_subframes;
  303. sta->local = local;
  304. sta->sdata = sdata;
  305. sta->rx_stats.last_rx = jiffies;
  306. u64_stats_init(&sta->rx_stats.syncp);
  307. sta->sta_state = IEEE80211_STA_NONE;
  308. /* Mark TID as unreserved */
  309. sta->reserved_tid = IEEE80211_TID_UNRESERVED;
  310. sta->last_connected = ktime_get_seconds();
  311. ewma_signal_init(&sta->rx_stats_avg.signal);
  312. for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
  313. ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
  314. if (local->ops->wake_tx_queue) {
  315. void *txq_data;
  316. int size = sizeof(struct txq_info) +
  317. ALIGN(hw->txq_data_size, sizeof(void *));
  318. txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
  319. if (!txq_data)
  320. goto free;
  321. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  322. struct txq_info *txq = txq_data + i * size;
  323. ieee80211_txq_init(sdata, sta, txq, i);
  324. }
  325. }
  326. if (sta_prepare_rate_control(local, sta, gfp))
  327. goto free_txq;
  328. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  329. /*
  330. * timer_to_tid must be initialized with identity mapping
  331. * to enable session_timer's data differentiation. See
  332. * sta_rx_agg_session_timer_expired for usage.
  333. */
  334. sta->timer_to_tid[i] = i;
  335. }
  336. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  337. skb_queue_head_init(&sta->ps_tx_buf[i]);
  338. skb_queue_head_init(&sta->tx_filtered[i]);
  339. }
  340. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  341. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  342. sta->sta.smps_mode = IEEE80211_SMPS_OFF;
  343. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  344. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  345. struct ieee80211_supported_band *sband;
  346. u8 smps;
  347. sband = ieee80211_get_sband(sdata);
  348. if (!sband)
  349. goto free_txq;
  350. smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
  351. IEEE80211_HT_CAP_SM_PS_SHIFT;
  352. /*
  353. * Assume that hostapd advertises our caps in the beacon and
  354. * this is the known_smps_mode for a station that just assciated
  355. */
  356. switch (smps) {
  357. case WLAN_HT_SMPS_CONTROL_DISABLED:
  358. sta->known_smps_mode = IEEE80211_SMPS_OFF;
  359. break;
  360. case WLAN_HT_SMPS_CONTROL_STATIC:
  361. sta->known_smps_mode = IEEE80211_SMPS_STATIC;
  362. break;
  363. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  364. sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
  365. break;
  366. default:
  367. WARN_ON(1);
  368. }
  369. }
  370. sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
  371. sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
  372. sta->cparams.target = MS2TIME(20);
  373. sta->cparams.interval = MS2TIME(100);
  374. sta->cparams.ecn = true;
  375. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  376. return sta;
  377. free_txq:
  378. if (sta->sta.txq[0])
  379. kfree(to_txq_info(sta->sta.txq[0]));
  380. free:
  381. #ifdef CONFIG_MAC80211_MESH
  382. kfree(sta->mesh);
  383. #endif
  384. kfree(sta);
  385. return NULL;
  386. }
  387. static int sta_info_insert_check(struct sta_info *sta)
  388. {
  389. struct ieee80211_sub_if_data *sdata = sta->sdata;
  390. /*
  391. * Can't be a WARN_ON because it can be triggered through a race:
  392. * something inserts a STA (on one CPU) without holding the RTNL
  393. * and another CPU turns off the net device.
  394. */
  395. if (unlikely(!ieee80211_sdata_running(sdata)))
  396. return -ENETDOWN;
  397. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  398. is_multicast_ether_addr(sta->sta.addr)))
  399. return -EINVAL;
  400. /* The RCU read lock is required by rhashtable due to
  401. * asynchronous resize/rehash. We also require the mutex
  402. * for correctness.
  403. */
  404. rcu_read_lock();
  405. lockdep_assert_held(&sdata->local->sta_mtx);
  406. if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
  407. ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
  408. rcu_read_unlock();
  409. return -ENOTUNIQ;
  410. }
  411. rcu_read_unlock();
  412. return 0;
  413. }
  414. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  415. struct ieee80211_sub_if_data *sdata,
  416. struct sta_info *sta)
  417. {
  418. enum ieee80211_sta_state state;
  419. int err = 0;
  420. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  421. err = drv_sta_state(local, sdata, sta, state, state + 1);
  422. if (err)
  423. break;
  424. }
  425. if (!err) {
  426. /*
  427. * Drivers using legacy sta_add/sta_remove callbacks only
  428. * get uploaded set to true after sta_add is called.
  429. */
  430. if (!local->ops->sta_add)
  431. sta->uploaded = true;
  432. return 0;
  433. }
  434. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  435. sdata_info(sdata,
  436. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  437. sta->sta.addr, state + 1, err);
  438. err = 0;
  439. }
  440. /* unwind on error */
  441. for (; state > IEEE80211_STA_NOTEXIST; state--)
  442. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  443. return err;
  444. }
  445. /*
  446. * should be called with sta_mtx locked
  447. * this function replaces the mutex lock
  448. * with a RCU lock
  449. */
  450. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  451. {
  452. struct ieee80211_local *local = sta->local;
  453. struct ieee80211_sub_if_data *sdata = sta->sdata;
  454. struct station_info *sinfo = NULL;
  455. int err = 0;
  456. lockdep_assert_held(&local->sta_mtx);
  457. /* check if STA exists already */
  458. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  459. err = -EEXIST;
  460. goto out_err;
  461. }
  462. sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
  463. if (!sinfo) {
  464. err = -ENOMEM;
  465. goto out_err;
  466. }
  467. local->num_sta++;
  468. local->sta_generation++;
  469. smp_mb();
  470. /* simplify things and don't accept BA sessions yet */
  471. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  472. /* make the station visible */
  473. err = sta_info_hash_add(local, sta);
  474. if (err)
  475. goto out_drop_sta;
  476. list_add_tail_rcu(&sta->list, &local->sta_list);
  477. /* notify driver */
  478. err = sta_info_insert_drv_state(local, sdata, sta);
  479. if (err)
  480. goto out_remove;
  481. set_sta_flag(sta, WLAN_STA_INSERTED);
  482. /* accept BA sessions now */
  483. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  484. ieee80211_sta_debugfs_add(sta);
  485. rate_control_add_sta_debugfs(sta);
  486. sinfo->generation = local->sta_generation;
  487. cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
  488. kfree(sinfo);
  489. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  490. /* move reference to rcu-protected */
  491. rcu_read_lock();
  492. mutex_unlock(&local->sta_mtx);
  493. if (ieee80211_vif_is_mesh(&sdata->vif))
  494. mesh_accept_plinks_update(sdata);
  495. return 0;
  496. out_remove:
  497. sta_info_hash_del(local, sta);
  498. list_del_rcu(&sta->list);
  499. out_drop_sta:
  500. local->num_sta--;
  501. synchronize_net();
  502. __cleanup_single_sta(sta);
  503. out_err:
  504. mutex_unlock(&local->sta_mtx);
  505. kfree(sinfo);
  506. rcu_read_lock();
  507. return err;
  508. }
  509. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  510. {
  511. struct ieee80211_local *local = sta->local;
  512. int err;
  513. might_sleep();
  514. mutex_lock(&local->sta_mtx);
  515. err = sta_info_insert_check(sta);
  516. if (err) {
  517. mutex_unlock(&local->sta_mtx);
  518. rcu_read_lock();
  519. goto out_free;
  520. }
  521. err = sta_info_insert_finish(sta);
  522. if (err)
  523. goto out_free;
  524. return 0;
  525. out_free:
  526. sta_info_free(local, sta);
  527. return err;
  528. }
  529. int sta_info_insert(struct sta_info *sta)
  530. {
  531. int err = sta_info_insert_rcu(sta);
  532. rcu_read_unlock();
  533. return err;
  534. }
  535. static inline void __bss_tim_set(u8 *tim, u16 id)
  536. {
  537. /*
  538. * This format has been mandated by the IEEE specifications,
  539. * so this line may not be changed to use the __set_bit() format.
  540. */
  541. tim[id / 8] |= (1 << (id % 8));
  542. }
  543. static inline void __bss_tim_clear(u8 *tim, u16 id)
  544. {
  545. /*
  546. * This format has been mandated by the IEEE specifications,
  547. * so this line may not be changed to use the __clear_bit() format.
  548. */
  549. tim[id / 8] &= ~(1 << (id % 8));
  550. }
  551. static inline bool __bss_tim_get(u8 *tim, u16 id)
  552. {
  553. /*
  554. * This format has been mandated by the IEEE specifications,
  555. * so this line may not be changed to use the test_bit() format.
  556. */
  557. return tim[id / 8] & (1 << (id % 8));
  558. }
  559. static unsigned long ieee80211_tids_for_ac(int ac)
  560. {
  561. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  562. switch (ac) {
  563. case IEEE80211_AC_VO:
  564. return BIT(6) | BIT(7);
  565. case IEEE80211_AC_VI:
  566. return BIT(4) | BIT(5);
  567. case IEEE80211_AC_BE:
  568. return BIT(0) | BIT(3);
  569. case IEEE80211_AC_BK:
  570. return BIT(1) | BIT(2);
  571. default:
  572. WARN_ON(1);
  573. return 0;
  574. }
  575. }
  576. static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
  577. {
  578. struct ieee80211_local *local = sta->local;
  579. struct ps_data *ps;
  580. bool indicate_tim = false;
  581. u8 ignore_for_tim = sta->sta.uapsd_queues;
  582. int ac;
  583. u16 id = sta->sta.aid;
  584. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  585. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  586. if (WARN_ON_ONCE(!sta->sdata->bss))
  587. return;
  588. ps = &sta->sdata->bss->ps;
  589. #ifdef CONFIG_MAC80211_MESH
  590. } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  591. ps = &sta->sdata->u.mesh.ps;
  592. #endif
  593. } else {
  594. return;
  595. }
  596. /* No need to do anything if the driver does all */
  597. if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
  598. return;
  599. if (sta->dead)
  600. goto done;
  601. /*
  602. * If all ACs are delivery-enabled then we should build
  603. * the TIM bit for all ACs anyway; if only some are then
  604. * we ignore those and build the TIM bit using only the
  605. * non-enabled ones.
  606. */
  607. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  608. ignore_for_tim = 0;
  609. if (ignore_pending)
  610. ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
  611. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  612. unsigned long tids;
  613. if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
  614. continue;
  615. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  616. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  617. if (indicate_tim)
  618. break;
  619. tids = ieee80211_tids_for_ac(ac);
  620. indicate_tim |=
  621. sta->driver_buffered_tids & tids;
  622. indicate_tim |=
  623. sta->txq_buffered_tids & tids;
  624. }
  625. done:
  626. spin_lock_bh(&local->tim_lock);
  627. if (indicate_tim == __bss_tim_get(ps->tim, id))
  628. goto out_unlock;
  629. if (indicate_tim)
  630. __bss_tim_set(ps->tim, id);
  631. else
  632. __bss_tim_clear(ps->tim, id);
  633. if (local->ops->set_tim && !WARN_ON(sta->dead)) {
  634. local->tim_in_locked_section = true;
  635. drv_set_tim(local, &sta->sta, indicate_tim);
  636. local->tim_in_locked_section = false;
  637. }
  638. out_unlock:
  639. spin_unlock_bh(&local->tim_lock);
  640. }
  641. void sta_info_recalc_tim(struct sta_info *sta)
  642. {
  643. __sta_info_recalc_tim(sta, false);
  644. }
  645. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  646. {
  647. struct ieee80211_tx_info *info;
  648. int timeout;
  649. if (!skb)
  650. return false;
  651. info = IEEE80211_SKB_CB(skb);
  652. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  653. timeout = (sta->listen_interval *
  654. sta->sdata->vif.bss_conf.beacon_int *
  655. 32 / 15625) * HZ;
  656. if (timeout < STA_TX_BUFFER_EXPIRE)
  657. timeout = STA_TX_BUFFER_EXPIRE;
  658. return time_after(jiffies, info->control.jiffies + timeout);
  659. }
  660. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  661. struct sta_info *sta, int ac)
  662. {
  663. unsigned long flags;
  664. struct sk_buff *skb;
  665. /*
  666. * First check for frames that should expire on the filtered
  667. * queue. Frames here were rejected by the driver and are on
  668. * a separate queue to avoid reordering with normal PS-buffered
  669. * frames. They also aren't accounted for right now in the
  670. * total_ps_buffered counter.
  671. */
  672. for (;;) {
  673. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  674. skb = skb_peek(&sta->tx_filtered[ac]);
  675. if (sta_info_buffer_expired(sta, skb))
  676. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  677. else
  678. skb = NULL;
  679. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  680. /*
  681. * Frames are queued in order, so if this one
  682. * hasn't expired yet we can stop testing. If
  683. * we actually reached the end of the queue we
  684. * also need to stop, of course.
  685. */
  686. if (!skb)
  687. break;
  688. ieee80211_free_txskb(&local->hw, skb);
  689. }
  690. /*
  691. * Now also check the normal PS-buffered queue, this will
  692. * only find something if the filtered queue was emptied
  693. * since the filtered frames are all before the normal PS
  694. * buffered frames.
  695. */
  696. for (;;) {
  697. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  698. skb = skb_peek(&sta->ps_tx_buf[ac]);
  699. if (sta_info_buffer_expired(sta, skb))
  700. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  701. else
  702. skb = NULL;
  703. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  704. /*
  705. * frames are queued in order, so if this one
  706. * hasn't expired yet (or we reached the end of
  707. * the queue) we can stop testing
  708. */
  709. if (!skb)
  710. break;
  711. local->total_ps_buffered--;
  712. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  713. sta->sta.addr);
  714. ieee80211_free_txskb(&local->hw, skb);
  715. }
  716. /*
  717. * Finally, recalculate the TIM bit for this station -- it might
  718. * now be clear because the station was too slow to retrieve its
  719. * frames.
  720. */
  721. sta_info_recalc_tim(sta);
  722. /*
  723. * Return whether there are any frames still buffered, this is
  724. * used to check whether the cleanup timer still needs to run,
  725. * if there are no frames we don't need to rearm the timer.
  726. */
  727. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  728. skb_queue_empty(&sta->tx_filtered[ac]));
  729. }
  730. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  731. struct sta_info *sta)
  732. {
  733. bool have_buffered = false;
  734. int ac;
  735. /* This is only necessary for stations on BSS/MBSS interfaces */
  736. if (!sta->sdata->bss &&
  737. !ieee80211_vif_is_mesh(&sta->sdata->vif))
  738. return false;
  739. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  740. have_buffered |=
  741. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  742. return have_buffered;
  743. }
  744. static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
  745. {
  746. struct ieee80211_local *local;
  747. struct ieee80211_sub_if_data *sdata;
  748. int ret;
  749. might_sleep();
  750. if (!sta)
  751. return -ENOENT;
  752. local = sta->local;
  753. sdata = sta->sdata;
  754. lockdep_assert_held(&local->sta_mtx);
  755. /*
  756. * Before removing the station from the driver and
  757. * rate control, it might still start new aggregation
  758. * sessions -- block that to make sure the tear-down
  759. * will be sufficient.
  760. */
  761. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  762. ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
  763. /*
  764. * Before removing the station from the driver there might be pending
  765. * rx frames on RSS queues sent prior to the disassociation - wait for
  766. * all such frames to be processed.
  767. */
  768. drv_sync_rx_queues(local, sta);
  769. ret = sta_info_hash_del(local, sta);
  770. if (WARN_ON(ret))
  771. return ret;
  772. /*
  773. * for TDLS peers, make sure to return to the base channel before
  774. * removal.
  775. */
  776. if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
  777. drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
  778. clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  779. }
  780. list_del_rcu(&sta->list);
  781. sta->removed = true;
  782. drv_sta_pre_rcu_remove(local, sta->sdata, sta);
  783. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  784. rcu_access_pointer(sdata->u.vlan.sta) == sta)
  785. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  786. return 0;
  787. }
  788. static void __sta_info_destroy_part2(struct sta_info *sta)
  789. {
  790. struct ieee80211_local *local = sta->local;
  791. struct ieee80211_sub_if_data *sdata = sta->sdata;
  792. struct station_info *sinfo;
  793. int ret;
  794. /*
  795. * NOTE: This assumes at least synchronize_net() was done
  796. * after _part1 and before _part2!
  797. */
  798. might_sleep();
  799. lockdep_assert_held(&local->sta_mtx);
  800. /* now keys can no longer be reached */
  801. ieee80211_free_sta_keys(local, sta);
  802. /* disable TIM bit - last chance to tell driver */
  803. __sta_info_recalc_tim(sta, true);
  804. sta->dead = true;
  805. local->num_sta--;
  806. local->sta_generation++;
  807. while (sta->sta_state > IEEE80211_STA_NONE) {
  808. ret = sta_info_move_state(sta, sta->sta_state - 1);
  809. if (ret) {
  810. WARN_ON_ONCE(1);
  811. break;
  812. }
  813. }
  814. if (sta->uploaded) {
  815. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  816. IEEE80211_STA_NOTEXIST);
  817. WARN_ON_ONCE(ret != 0);
  818. }
  819. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  820. sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
  821. if (sinfo)
  822. sta_set_sinfo(sta, sinfo);
  823. cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
  824. kfree(sinfo);
  825. rate_control_remove_sta_debugfs(sta);
  826. ieee80211_sta_debugfs_remove(sta);
  827. cleanup_single_sta(sta);
  828. }
  829. int __must_check __sta_info_destroy(struct sta_info *sta)
  830. {
  831. int err = __sta_info_destroy_part1(sta);
  832. if (err)
  833. return err;
  834. synchronize_net();
  835. __sta_info_destroy_part2(sta);
  836. return 0;
  837. }
  838. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  839. {
  840. struct sta_info *sta;
  841. int ret;
  842. mutex_lock(&sdata->local->sta_mtx);
  843. sta = sta_info_get(sdata, addr);
  844. ret = __sta_info_destroy(sta);
  845. mutex_unlock(&sdata->local->sta_mtx);
  846. return ret;
  847. }
  848. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  849. const u8 *addr)
  850. {
  851. struct sta_info *sta;
  852. int ret;
  853. mutex_lock(&sdata->local->sta_mtx);
  854. sta = sta_info_get_bss(sdata, addr);
  855. ret = __sta_info_destroy(sta);
  856. mutex_unlock(&sdata->local->sta_mtx);
  857. return ret;
  858. }
  859. static void sta_info_cleanup(unsigned long data)
  860. {
  861. struct ieee80211_local *local = (struct ieee80211_local *) data;
  862. struct sta_info *sta;
  863. bool timer_needed = false;
  864. rcu_read_lock();
  865. list_for_each_entry_rcu(sta, &local->sta_list, list)
  866. if (sta_info_cleanup_expire_buffered(local, sta))
  867. timer_needed = true;
  868. rcu_read_unlock();
  869. if (local->quiescing)
  870. return;
  871. if (!timer_needed)
  872. return;
  873. mod_timer(&local->sta_cleanup,
  874. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  875. }
  876. int sta_info_init(struct ieee80211_local *local)
  877. {
  878. int err;
  879. err = rhltable_init(&local->sta_hash, &sta_rht_params);
  880. if (err)
  881. return err;
  882. spin_lock_init(&local->tim_lock);
  883. mutex_init(&local->sta_mtx);
  884. INIT_LIST_HEAD(&local->sta_list);
  885. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  886. (unsigned long)local);
  887. return 0;
  888. }
  889. void sta_info_stop(struct ieee80211_local *local)
  890. {
  891. del_timer_sync(&local->sta_cleanup);
  892. rhltable_destroy(&local->sta_hash);
  893. }
  894. int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
  895. {
  896. struct ieee80211_local *local = sdata->local;
  897. struct sta_info *sta, *tmp;
  898. LIST_HEAD(free_list);
  899. int ret = 0;
  900. might_sleep();
  901. WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
  902. WARN_ON(vlans && !sdata->bss);
  903. mutex_lock(&local->sta_mtx);
  904. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  905. if (sdata == sta->sdata ||
  906. (vlans && sdata->bss == sta->sdata->bss)) {
  907. if (!WARN_ON(__sta_info_destroy_part1(sta)))
  908. list_add(&sta->free_list, &free_list);
  909. ret++;
  910. }
  911. }
  912. if (!list_empty(&free_list)) {
  913. synchronize_net();
  914. list_for_each_entry_safe(sta, tmp, &free_list, free_list)
  915. __sta_info_destroy_part2(sta);
  916. }
  917. mutex_unlock(&local->sta_mtx);
  918. return ret;
  919. }
  920. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  921. unsigned long exp_time)
  922. {
  923. struct ieee80211_local *local = sdata->local;
  924. struct sta_info *sta, *tmp;
  925. mutex_lock(&local->sta_mtx);
  926. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  927. unsigned long last_active = ieee80211_sta_last_active(sta);
  928. if (sdata != sta->sdata)
  929. continue;
  930. if (time_is_before_jiffies(last_active + exp_time)) {
  931. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  932. sta->sta.addr);
  933. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  934. test_sta_flag(sta, WLAN_STA_PS_STA))
  935. atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
  936. WARN_ON(__sta_info_destroy(sta));
  937. }
  938. }
  939. mutex_unlock(&local->sta_mtx);
  940. }
  941. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  942. const u8 *addr,
  943. const u8 *localaddr)
  944. {
  945. struct ieee80211_local *local = hw_to_local(hw);
  946. struct rhlist_head *tmp;
  947. struct sta_info *sta;
  948. /*
  949. * Just return a random station if localaddr is NULL
  950. * ... first in list.
  951. */
  952. for_each_sta_info(local, addr, sta, tmp) {
  953. if (localaddr &&
  954. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  955. continue;
  956. if (!sta->uploaded)
  957. return NULL;
  958. return &sta->sta;
  959. }
  960. return NULL;
  961. }
  962. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  963. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  964. const u8 *addr)
  965. {
  966. struct sta_info *sta;
  967. if (!vif)
  968. return NULL;
  969. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  970. if (!sta)
  971. return NULL;
  972. if (!sta->uploaded)
  973. return NULL;
  974. return &sta->sta;
  975. }
  976. EXPORT_SYMBOL(ieee80211_find_sta);
  977. /* powersave support code */
  978. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  979. {
  980. struct ieee80211_sub_if_data *sdata = sta->sdata;
  981. struct ieee80211_local *local = sdata->local;
  982. struct sk_buff_head pending;
  983. int filtered = 0, buffered = 0, ac, i;
  984. unsigned long flags;
  985. struct ps_data *ps;
  986. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  987. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  988. u.ap);
  989. if (sdata->vif.type == NL80211_IFTYPE_AP)
  990. ps = &sdata->bss->ps;
  991. else if (ieee80211_vif_is_mesh(&sdata->vif))
  992. ps = &sdata->u.mesh.ps;
  993. else
  994. return;
  995. clear_sta_flag(sta, WLAN_STA_SP);
  996. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  997. sta->driver_buffered_tids = 0;
  998. sta->txq_buffered_tids = 0;
  999. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  1000. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  1001. if (sta->sta.txq[0]) {
  1002. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  1003. if (!txq_has_queue(sta->sta.txq[i]))
  1004. continue;
  1005. drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
  1006. }
  1007. }
  1008. skb_queue_head_init(&pending);
  1009. /* sync with ieee80211_tx_h_unicast_ps_buf */
  1010. spin_lock(&sta->ps_lock);
  1011. /* Send all buffered frames to the station */
  1012. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1013. int count = skb_queue_len(&pending), tmp;
  1014. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  1015. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  1016. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  1017. tmp = skb_queue_len(&pending);
  1018. filtered += tmp - count;
  1019. count = tmp;
  1020. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  1021. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  1022. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  1023. tmp = skb_queue_len(&pending);
  1024. buffered += tmp - count;
  1025. }
  1026. ieee80211_add_pending_skbs(local, &pending);
  1027. /* now we're no longer in the deliver code */
  1028. clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1029. /* The station might have polled and then woken up before we responded,
  1030. * so clear these flags now to avoid them sticking around.
  1031. */
  1032. clear_sta_flag(sta, WLAN_STA_PSPOLL);
  1033. clear_sta_flag(sta, WLAN_STA_UAPSD);
  1034. spin_unlock(&sta->ps_lock);
  1035. atomic_dec(&ps->num_sta_ps);
  1036. /* This station just woke up and isn't aware of our SMPS state */
  1037. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  1038. !ieee80211_smps_is_restrictive(sta->known_smps_mode,
  1039. sdata->smps_mode) &&
  1040. sta->known_smps_mode != sdata->bss->req_smps &&
  1041. sta_info_tx_streams(sta) != 1) {
  1042. ht_dbg(sdata,
  1043. "%pM just woke up and MIMO capable - update SMPS\n",
  1044. sta->sta.addr);
  1045. ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
  1046. sta->sta.addr,
  1047. sdata->vif.bss_conf.bssid);
  1048. }
  1049. local->total_ps_buffered -= buffered;
  1050. sta_info_recalc_tim(sta);
  1051. ps_dbg(sdata,
  1052. "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
  1053. sta->sta.addr, sta->sta.aid, filtered, buffered);
  1054. ieee80211_check_fast_xmit(sta);
  1055. }
  1056. static void ieee80211_send_null_response(struct sta_info *sta, int tid,
  1057. enum ieee80211_frame_release_type reason,
  1058. bool call_driver, bool more_data)
  1059. {
  1060. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1061. struct ieee80211_local *local = sdata->local;
  1062. struct ieee80211_qos_hdr *nullfunc;
  1063. struct sk_buff *skb;
  1064. int size = sizeof(*nullfunc);
  1065. __le16 fc;
  1066. bool qos = sta->sta.wme;
  1067. struct ieee80211_tx_info *info;
  1068. struct ieee80211_chanctx_conf *chanctx_conf;
  1069. if (qos) {
  1070. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1071. IEEE80211_STYPE_QOS_NULLFUNC |
  1072. IEEE80211_FCTL_FROMDS);
  1073. } else {
  1074. size -= 2;
  1075. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1076. IEEE80211_STYPE_NULLFUNC |
  1077. IEEE80211_FCTL_FROMDS);
  1078. }
  1079. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  1080. if (!skb)
  1081. return;
  1082. skb_reserve(skb, local->hw.extra_tx_headroom);
  1083. nullfunc = skb_put(skb, size);
  1084. nullfunc->frame_control = fc;
  1085. nullfunc->duration_id = 0;
  1086. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  1087. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  1088. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  1089. nullfunc->seq_ctrl = 0;
  1090. skb->priority = tid;
  1091. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  1092. if (qos) {
  1093. nullfunc->qos_ctrl = cpu_to_le16(tid);
  1094. if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
  1095. nullfunc->qos_ctrl |=
  1096. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  1097. if (more_data)
  1098. nullfunc->frame_control |=
  1099. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1100. }
  1101. }
  1102. info = IEEE80211_SKB_CB(skb);
  1103. /*
  1104. * Tell TX path to send this frame even though the
  1105. * STA may still remain is PS mode after this frame
  1106. * exchange. Also set EOSP to indicate this packet
  1107. * ends the poll/service period.
  1108. */
  1109. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1110. IEEE80211_TX_STATUS_EOSP |
  1111. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1112. info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
  1113. if (call_driver)
  1114. drv_allow_buffered_frames(local, sta, BIT(tid), 1,
  1115. reason, false);
  1116. skb->dev = sdata->dev;
  1117. rcu_read_lock();
  1118. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1119. if (WARN_ON(!chanctx_conf)) {
  1120. rcu_read_unlock();
  1121. kfree_skb(skb);
  1122. return;
  1123. }
  1124. info->band = chanctx_conf->def.chan->band;
  1125. ieee80211_xmit(sdata, sta, skb);
  1126. rcu_read_unlock();
  1127. }
  1128. static int find_highest_prio_tid(unsigned long tids)
  1129. {
  1130. /* lower 3 TIDs aren't ordered perfectly */
  1131. if (tids & 0xF8)
  1132. return fls(tids) - 1;
  1133. /* TID 0 is BE just like TID 3 */
  1134. if (tids & BIT(0))
  1135. return 0;
  1136. return fls(tids) - 1;
  1137. }
  1138. /* Indicates if the MORE_DATA bit should be set in the last
  1139. * frame obtained by ieee80211_sta_ps_get_frames.
  1140. * Note that driver_release_tids is relevant only if
  1141. * reason = IEEE80211_FRAME_RELEASE_PSPOLL
  1142. */
  1143. static bool
  1144. ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
  1145. enum ieee80211_frame_release_type reason,
  1146. unsigned long driver_release_tids)
  1147. {
  1148. int ac;
  1149. /* If the driver has data on more than one TID then
  1150. * certainly there's more data if we release just a
  1151. * single frame now (from a single TID). This will
  1152. * only happen for PS-Poll.
  1153. */
  1154. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  1155. hweight16(driver_release_tids) > 1)
  1156. return true;
  1157. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1158. if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
  1159. continue;
  1160. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1161. !skb_queue_empty(&sta->ps_tx_buf[ac]))
  1162. return true;
  1163. }
  1164. return false;
  1165. }
  1166. static void
  1167. ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
  1168. enum ieee80211_frame_release_type reason,
  1169. struct sk_buff_head *frames,
  1170. unsigned long *driver_release_tids)
  1171. {
  1172. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1173. struct ieee80211_local *local = sdata->local;
  1174. int ac;
  1175. /* Get response frame(s) and more data bit for the last one. */
  1176. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1177. unsigned long tids;
  1178. if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
  1179. continue;
  1180. tids = ieee80211_tids_for_ac(ac);
  1181. /* if we already have frames from software, then we can't also
  1182. * release from hardware queues
  1183. */
  1184. if (skb_queue_empty(frames)) {
  1185. *driver_release_tids |=
  1186. sta->driver_buffered_tids & tids;
  1187. *driver_release_tids |= sta->txq_buffered_tids & tids;
  1188. }
  1189. if (!*driver_release_tids) {
  1190. struct sk_buff *skb;
  1191. while (n_frames > 0) {
  1192. skb = skb_dequeue(&sta->tx_filtered[ac]);
  1193. if (!skb) {
  1194. skb = skb_dequeue(
  1195. &sta->ps_tx_buf[ac]);
  1196. if (skb)
  1197. local->total_ps_buffered--;
  1198. }
  1199. if (!skb)
  1200. break;
  1201. n_frames--;
  1202. __skb_queue_tail(frames, skb);
  1203. }
  1204. }
  1205. /* If we have more frames buffered on this AC, then abort the
  1206. * loop since we can't send more data from other ACs before
  1207. * the buffered frames from this.
  1208. */
  1209. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1210. !skb_queue_empty(&sta->ps_tx_buf[ac]))
  1211. break;
  1212. }
  1213. }
  1214. static void
  1215. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  1216. int n_frames, u8 ignored_acs,
  1217. enum ieee80211_frame_release_type reason)
  1218. {
  1219. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1220. struct ieee80211_local *local = sdata->local;
  1221. unsigned long driver_release_tids = 0;
  1222. struct sk_buff_head frames;
  1223. bool more_data;
  1224. /* Service or PS-Poll period starts */
  1225. set_sta_flag(sta, WLAN_STA_SP);
  1226. __skb_queue_head_init(&frames);
  1227. ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
  1228. &frames, &driver_release_tids);
  1229. more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
  1230. if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
  1231. driver_release_tids =
  1232. BIT(find_highest_prio_tid(driver_release_tids));
  1233. if (skb_queue_empty(&frames) && !driver_release_tids) {
  1234. int tid, ac;
  1235. /*
  1236. * For PS-Poll, this can only happen due to a race condition
  1237. * when we set the TIM bit and the station notices it, but
  1238. * before it can poll for the frame we expire it.
  1239. *
  1240. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1241. * At each unscheduled SP for a non-AP STA, the AP shall
  1242. * attempt to transmit at least one MSDU or MMPDU, but no
  1243. * more than the value specified in the Max SP Length field
  1244. * in the QoS Capability element from delivery-enabled ACs,
  1245. * that are destined for the non-AP STA.
  1246. *
  1247. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1248. */
  1249. /* This will evaluate to 1, 3, 5 or 7. */
  1250. for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
  1251. if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
  1252. break;
  1253. tid = 7 - 2 * ac;
  1254. ieee80211_send_null_response(sta, tid, reason, true, false);
  1255. } else if (!driver_release_tids) {
  1256. struct sk_buff_head pending;
  1257. struct sk_buff *skb;
  1258. int num = 0;
  1259. u16 tids = 0;
  1260. bool need_null = false;
  1261. skb_queue_head_init(&pending);
  1262. while ((skb = __skb_dequeue(&frames))) {
  1263. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1264. struct ieee80211_hdr *hdr = (void *) skb->data;
  1265. u8 *qoshdr = NULL;
  1266. num++;
  1267. /*
  1268. * Tell TX path to send this frame even though the
  1269. * STA may still remain is PS mode after this frame
  1270. * exchange.
  1271. */
  1272. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
  1273. info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
  1274. /*
  1275. * Use MoreData flag to indicate whether there are
  1276. * more buffered frames for this STA
  1277. */
  1278. if (more_data || !skb_queue_empty(&frames))
  1279. hdr->frame_control |=
  1280. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1281. else
  1282. hdr->frame_control &=
  1283. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1284. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1285. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1286. qoshdr = ieee80211_get_qos_ctl(hdr);
  1287. tids |= BIT(skb->priority);
  1288. __skb_queue_tail(&pending, skb);
  1289. /* end service period after last frame or add one */
  1290. if (!skb_queue_empty(&frames))
  1291. continue;
  1292. if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
  1293. /* for PS-Poll, there's only one frame */
  1294. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1295. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1296. break;
  1297. }
  1298. /* For uAPSD, things are a bit more complicated. If the
  1299. * last frame has a QoS header (i.e. is a QoS-data or
  1300. * QoS-nulldata frame) then just set the EOSP bit there
  1301. * and be done.
  1302. * If the frame doesn't have a QoS header (which means
  1303. * it should be a bufferable MMPDU) then we can't set
  1304. * the EOSP bit in the QoS header; add a QoS-nulldata
  1305. * frame to the list to send it after the MMPDU.
  1306. *
  1307. * Note that this code is only in the mac80211-release
  1308. * code path, we assume that the driver will not buffer
  1309. * anything but QoS-data frames, or if it does, will
  1310. * create the QoS-nulldata frame by itself if needed.
  1311. *
  1312. * Cf. 802.11-2012 10.2.1.10 (c).
  1313. */
  1314. if (qoshdr) {
  1315. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1316. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1317. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1318. } else {
  1319. /* The standard isn't completely clear on this
  1320. * as it says the more-data bit should be set
  1321. * if there are more BUs. The QoS-Null frame
  1322. * we're about to send isn't buffered yet, we
  1323. * only create it below, but let's pretend it
  1324. * was buffered just in case some clients only
  1325. * expect more-data=0 when eosp=1.
  1326. */
  1327. hdr->frame_control |=
  1328. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1329. need_null = true;
  1330. num++;
  1331. }
  1332. break;
  1333. }
  1334. drv_allow_buffered_frames(local, sta, tids, num,
  1335. reason, more_data);
  1336. ieee80211_add_pending_skbs(local, &pending);
  1337. if (need_null)
  1338. ieee80211_send_null_response(
  1339. sta, find_highest_prio_tid(tids),
  1340. reason, false, false);
  1341. sta_info_recalc_tim(sta);
  1342. } else {
  1343. int tid;
  1344. /*
  1345. * We need to release a frame that is buffered somewhere in the
  1346. * driver ... it'll have to handle that.
  1347. * Note that the driver also has to check the number of frames
  1348. * on the TIDs we're releasing from - if there are more than
  1349. * n_frames it has to set the more-data bit (if we didn't ask
  1350. * it to set it anyway due to other buffered frames); if there
  1351. * are fewer than n_frames it has to make sure to adjust that
  1352. * to allow the service period to end properly.
  1353. */
  1354. drv_release_buffered_frames(local, sta, driver_release_tids,
  1355. n_frames, reason, more_data);
  1356. /*
  1357. * Note that we don't recalculate the TIM bit here as it would
  1358. * most likely have no effect at all unless the driver told us
  1359. * that the TID(s) became empty before returning here from the
  1360. * release function.
  1361. * Either way, however, when the driver tells us that the TID(s)
  1362. * became empty or we find that a txq became empty, we'll do the
  1363. * TIM recalculation.
  1364. */
  1365. if (!sta->sta.txq[0])
  1366. return;
  1367. for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
  1368. if (!(driver_release_tids & BIT(tid)) ||
  1369. txq_has_queue(sta->sta.txq[tid]))
  1370. continue;
  1371. sta_info_recalc_tim(sta);
  1372. break;
  1373. }
  1374. }
  1375. }
  1376. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1377. {
  1378. u8 ignore_for_response = sta->sta.uapsd_queues;
  1379. /*
  1380. * If all ACs are delivery-enabled then we should reply
  1381. * from any of them, if only some are enabled we reply
  1382. * only from the non-enabled ones.
  1383. */
  1384. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1385. ignore_for_response = 0;
  1386. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1387. IEEE80211_FRAME_RELEASE_PSPOLL);
  1388. }
  1389. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1390. {
  1391. int n_frames = sta->sta.max_sp;
  1392. u8 delivery_enabled = sta->sta.uapsd_queues;
  1393. /*
  1394. * If we ever grow support for TSPEC this might happen if
  1395. * the TSPEC update from hostapd comes in between a trigger
  1396. * frame setting WLAN_STA_UAPSD in the RX path and this
  1397. * actually getting called.
  1398. */
  1399. if (!delivery_enabled)
  1400. return;
  1401. switch (sta->sta.max_sp) {
  1402. case 1:
  1403. n_frames = 2;
  1404. break;
  1405. case 2:
  1406. n_frames = 4;
  1407. break;
  1408. case 3:
  1409. n_frames = 6;
  1410. break;
  1411. case 0:
  1412. /* XXX: what is a good value? */
  1413. n_frames = 128;
  1414. break;
  1415. }
  1416. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1417. IEEE80211_FRAME_RELEASE_UAPSD);
  1418. }
  1419. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1420. struct ieee80211_sta *pubsta, bool block)
  1421. {
  1422. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1423. trace_api_sta_block_awake(sta->local, pubsta, block);
  1424. if (block) {
  1425. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1426. ieee80211_clear_fast_xmit(sta);
  1427. return;
  1428. }
  1429. if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1430. return;
  1431. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1432. set_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1433. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1434. ieee80211_queue_work(hw, &sta->drv_deliver_wk);
  1435. } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
  1436. test_sta_flag(sta, WLAN_STA_UAPSD)) {
  1437. /* must be asleep in this case */
  1438. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1439. ieee80211_queue_work(hw, &sta->drv_deliver_wk);
  1440. } else {
  1441. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1442. ieee80211_check_fast_xmit(sta);
  1443. }
  1444. }
  1445. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1446. void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
  1447. {
  1448. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1449. struct ieee80211_local *local = sta->local;
  1450. trace_api_eosp(local, pubsta);
  1451. clear_sta_flag(sta, WLAN_STA_SP);
  1452. }
  1453. EXPORT_SYMBOL(ieee80211_sta_eosp);
  1454. void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
  1455. {
  1456. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1457. enum ieee80211_frame_release_type reason;
  1458. bool more_data;
  1459. trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
  1460. reason = IEEE80211_FRAME_RELEASE_UAPSD;
  1461. more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
  1462. reason, 0);
  1463. ieee80211_send_null_response(sta, tid, reason, false, more_data);
  1464. }
  1465. EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
  1466. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1467. u8 tid, bool buffered)
  1468. {
  1469. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1470. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1471. return;
  1472. trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
  1473. if (buffered)
  1474. set_bit(tid, &sta->driver_buffered_tids);
  1475. else
  1476. clear_bit(tid, &sta->driver_buffered_tids);
  1477. sta_info_recalc_tim(sta);
  1478. }
  1479. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1480. static void
  1481. ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
  1482. {
  1483. struct ieee80211_local *local = sdata->local;
  1484. bool allow_p2p_go_ps = sdata->vif.p2p;
  1485. struct sta_info *sta;
  1486. rcu_read_lock();
  1487. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  1488. if (sdata != sta->sdata ||
  1489. !test_sta_flag(sta, WLAN_STA_ASSOC))
  1490. continue;
  1491. if (!sta->sta.support_p2p_ps) {
  1492. allow_p2p_go_ps = false;
  1493. break;
  1494. }
  1495. }
  1496. rcu_read_unlock();
  1497. if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
  1498. sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
  1499. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
  1500. }
  1501. }
  1502. int sta_info_move_state(struct sta_info *sta,
  1503. enum ieee80211_sta_state new_state)
  1504. {
  1505. might_sleep();
  1506. if (sta->sta_state == new_state)
  1507. return 0;
  1508. /* check allowed transitions first */
  1509. switch (new_state) {
  1510. case IEEE80211_STA_NONE:
  1511. if (sta->sta_state != IEEE80211_STA_AUTH)
  1512. return -EINVAL;
  1513. break;
  1514. case IEEE80211_STA_AUTH:
  1515. if (sta->sta_state != IEEE80211_STA_NONE &&
  1516. sta->sta_state != IEEE80211_STA_ASSOC)
  1517. return -EINVAL;
  1518. break;
  1519. case IEEE80211_STA_ASSOC:
  1520. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1521. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1522. return -EINVAL;
  1523. break;
  1524. case IEEE80211_STA_AUTHORIZED:
  1525. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1526. return -EINVAL;
  1527. break;
  1528. default:
  1529. WARN(1, "invalid state %d", new_state);
  1530. return -EINVAL;
  1531. }
  1532. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1533. sta->sta.addr, new_state);
  1534. /*
  1535. * notify the driver before the actual changes so it can
  1536. * fail the transition
  1537. */
  1538. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1539. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1540. sta->sta_state, new_state);
  1541. if (err)
  1542. return err;
  1543. }
  1544. /* reflect the change in all state variables */
  1545. switch (new_state) {
  1546. case IEEE80211_STA_NONE:
  1547. if (sta->sta_state == IEEE80211_STA_AUTH)
  1548. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1549. break;
  1550. case IEEE80211_STA_AUTH:
  1551. if (sta->sta_state == IEEE80211_STA_NONE) {
  1552. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1553. } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1554. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1555. ieee80211_recalc_min_chandef(sta->sdata);
  1556. if (!sta->sta.support_p2p_ps)
  1557. ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
  1558. }
  1559. break;
  1560. case IEEE80211_STA_ASSOC:
  1561. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1562. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1563. ieee80211_recalc_min_chandef(sta->sdata);
  1564. if (!sta->sta.support_p2p_ps)
  1565. ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
  1566. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1567. ieee80211_vif_dec_num_mcast(sta->sdata);
  1568. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1569. ieee80211_clear_fast_xmit(sta);
  1570. ieee80211_clear_fast_rx(sta);
  1571. }
  1572. break;
  1573. case IEEE80211_STA_AUTHORIZED:
  1574. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1575. ieee80211_vif_inc_num_mcast(sta->sdata);
  1576. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1577. ieee80211_check_fast_xmit(sta);
  1578. ieee80211_check_fast_rx(sta);
  1579. }
  1580. break;
  1581. default:
  1582. break;
  1583. }
  1584. sta->sta_state = new_state;
  1585. return 0;
  1586. }
  1587. u8 sta_info_tx_streams(struct sta_info *sta)
  1588. {
  1589. struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
  1590. u8 rx_streams;
  1591. if (!sta->sta.ht_cap.ht_supported)
  1592. return 1;
  1593. if (sta->sta.vht_cap.vht_supported) {
  1594. int i;
  1595. u16 tx_mcs_map =
  1596. le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
  1597. for (i = 7; i >= 0; i--)
  1598. if ((tx_mcs_map & (0x3 << (i * 2))) !=
  1599. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  1600. return i + 1;
  1601. }
  1602. if (ht_cap->mcs.rx_mask[3])
  1603. rx_streams = 4;
  1604. else if (ht_cap->mcs.rx_mask[2])
  1605. rx_streams = 3;
  1606. else if (ht_cap->mcs.rx_mask[1])
  1607. rx_streams = 2;
  1608. else
  1609. rx_streams = 1;
  1610. if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
  1611. return rx_streams;
  1612. return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  1613. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
  1614. }
  1615. static struct ieee80211_sta_rx_stats *
  1616. sta_get_last_rx_stats(struct sta_info *sta)
  1617. {
  1618. struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
  1619. struct ieee80211_local *local = sta->local;
  1620. int cpu;
  1621. if (!ieee80211_hw_check(&local->hw, USES_RSS))
  1622. return stats;
  1623. for_each_possible_cpu(cpu) {
  1624. struct ieee80211_sta_rx_stats *cpustats;
  1625. cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1626. if (time_after(cpustats->last_rx, stats->last_rx))
  1627. stats = cpustats;
  1628. }
  1629. return stats;
  1630. }
  1631. static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
  1632. struct rate_info *rinfo)
  1633. {
  1634. rinfo->bw = STA_STATS_GET(BW, rate);
  1635. switch (STA_STATS_GET(TYPE, rate)) {
  1636. case STA_STATS_RATE_TYPE_VHT:
  1637. rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
  1638. rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
  1639. rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
  1640. if (STA_STATS_GET(SGI, rate))
  1641. rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
  1642. break;
  1643. case STA_STATS_RATE_TYPE_HT:
  1644. rinfo->flags = RATE_INFO_FLAGS_MCS;
  1645. rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
  1646. if (STA_STATS_GET(SGI, rate))
  1647. rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
  1648. break;
  1649. case STA_STATS_RATE_TYPE_LEGACY: {
  1650. struct ieee80211_supported_band *sband;
  1651. u16 brate;
  1652. unsigned int shift;
  1653. int band = STA_STATS_GET(LEGACY_BAND, rate);
  1654. int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
  1655. rinfo->flags = 0;
  1656. sband = local->hw.wiphy->bands[band];
  1657. brate = sband->bitrates[rate_idx].bitrate;
  1658. if (rinfo->bw == RATE_INFO_BW_5)
  1659. shift = 2;
  1660. else if (rinfo->bw == RATE_INFO_BW_10)
  1661. shift = 1;
  1662. else
  1663. shift = 0;
  1664. rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
  1670. {
  1671. u16 rate = ACCESS_ONCE(sta_get_last_rx_stats(sta)->last_rate);
  1672. if (rate == STA_STATS_RATE_INVALID)
  1673. return -EINVAL;
  1674. sta_stats_decode_rate(sta->local, rate, rinfo);
  1675. return 0;
  1676. }
  1677. static void sta_set_tidstats(struct sta_info *sta,
  1678. struct cfg80211_tid_stats *tidstats,
  1679. int tid)
  1680. {
  1681. struct ieee80211_local *local = sta->local;
  1682. if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
  1683. unsigned int start;
  1684. do {
  1685. start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
  1686. tidstats->rx_msdu = sta->rx_stats.msdu[tid];
  1687. } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
  1688. tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
  1689. }
  1690. if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
  1691. tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
  1692. tidstats->tx_msdu = sta->tx_stats.msdu[tid];
  1693. }
  1694. if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
  1695. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  1696. tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
  1697. tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
  1698. }
  1699. if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
  1700. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  1701. tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
  1702. tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
  1703. }
  1704. }
  1705. static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
  1706. {
  1707. unsigned int start;
  1708. u64 value;
  1709. do {
  1710. start = u64_stats_fetch_begin(&rxstats->syncp);
  1711. value = rxstats->bytes;
  1712. } while (u64_stats_fetch_retry(&rxstats->syncp, start));
  1713. return value;
  1714. }
  1715. void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
  1716. {
  1717. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1718. struct ieee80211_local *local = sdata->local;
  1719. u32 thr = 0;
  1720. int i, ac, cpu;
  1721. struct ieee80211_sta_rx_stats *last_rxstats;
  1722. last_rxstats = sta_get_last_rx_stats(sta);
  1723. sinfo->generation = sdata->local->sta_generation;
  1724. /* do before driver, so beacon filtering drivers have a
  1725. * chance to e.g. just add the number of filtered beacons
  1726. * (or just modify the value entirely, of course)
  1727. */
  1728. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1729. sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
  1730. drv_sta_statistics(local, sdata, &sta->sta, sinfo);
  1731. sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
  1732. BIT(NL80211_STA_INFO_STA_FLAGS) |
  1733. BIT(NL80211_STA_INFO_BSS_PARAM) |
  1734. BIT(NL80211_STA_INFO_CONNECTED_TIME) |
  1735. BIT(NL80211_STA_INFO_RX_DROP_MISC);
  1736. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1737. sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
  1738. sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
  1739. }
  1740. sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
  1741. sinfo->inactive_time =
  1742. jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
  1743. if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
  1744. BIT(NL80211_STA_INFO_TX_BYTES)))) {
  1745. sinfo->tx_bytes = 0;
  1746. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  1747. sinfo->tx_bytes += sta->tx_stats.bytes[ac];
  1748. sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
  1749. }
  1750. if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
  1751. sinfo->tx_packets = 0;
  1752. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  1753. sinfo->tx_packets += sta->tx_stats.packets[ac];
  1754. sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
  1755. }
  1756. if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
  1757. BIT(NL80211_STA_INFO_RX_BYTES)))) {
  1758. sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
  1759. if (sta->pcpu_rx_stats) {
  1760. for_each_possible_cpu(cpu) {
  1761. struct ieee80211_sta_rx_stats *cpurxs;
  1762. cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1763. sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
  1764. }
  1765. }
  1766. sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
  1767. }
  1768. if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
  1769. sinfo->rx_packets = sta->rx_stats.packets;
  1770. if (sta->pcpu_rx_stats) {
  1771. for_each_possible_cpu(cpu) {
  1772. struct ieee80211_sta_rx_stats *cpurxs;
  1773. cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1774. sinfo->rx_packets += cpurxs->packets;
  1775. }
  1776. }
  1777. sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
  1778. }
  1779. if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
  1780. sinfo->tx_retries = sta->status_stats.retry_count;
  1781. sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
  1782. }
  1783. if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
  1784. sinfo->tx_failed = sta->status_stats.retry_failed;
  1785. sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
  1786. }
  1787. sinfo->rx_dropped_misc = sta->rx_stats.dropped;
  1788. if (sta->pcpu_rx_stats) {
  1789. for_each_possible_cpu(cpu) {
  1790. struct ieee80211_sta_rx_stats *cpurxs;
  1791. cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1792. sinfo->rx_dropped_misc += cpurxs->dropped;
  1793. }
  1794. }
  1795. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1796. !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
  1797. sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
  1798. BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
  1799. sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
  1800. }
  1801. if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
  1802. ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
  1803. if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
  1804. sinfo->signal = (s8)last_rxstats->last_signal;
  1805. sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
  1806. }
  1807. if (!sta->pcpu_rx_stats &&
  1808. !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
  1809. sinfo->signal_avg =
  1810. -ewma_signal_read(&sta->rx_stats_avg.signal);
  1811. sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
  1812. }
  1813. }
  1814. /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
  1815. * the sta->rx_stats struct, so the check here is fine with and without
  1816. * pcpu statistics
  1817. */
  1818. if (last_rxstats->chains &&
  1819. !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
  1820. BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
  1821. sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
  1822. if (!sta->pcpu_rx_stats)
  1823. sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
  1824. sinfo->chains = last_rxstats->chains;
  1825. for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
  1826. sinfo->chain_signal[i] =
  1827. last_rxstats->chain_signal_last[i];
  1828. sinfo->chain_signal_avg[i] =
  1829. -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
  1830. }
  1831. }
  1832. if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
  1833. sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
  1834. &sinfo->txrate);
  1835. sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
  1836. }
  1837. if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
  1838. if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
  1839. sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
  1840. }
  1841. sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
  1842. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
  1843. struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
  1844. sta_set_tidstats(sta, tidstats, i);
  1845. }
  1846. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1847. #ifdef CONFIG_MAC80211_MESH
  1848. sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
  1849. BIT(NL80211_STA_INFO_PLID) |
  1850. BIT(NL80211_STA_INFO_PLINK_STATE) |
  1851. BIT(NL80211_STA_INFO_LOCAL_PM) |
  1852. BIT(NL80211_STA_INFO_PEER_PM) |
  1853. BIT(NL80211_STA_INFO_NONPEER_PM);
  1854. sinfo->llid = sta->mesh->llid;
  1855. sinfo->plid = sta->mesh->plid;
  1856. sinfo->plink_state = sta->mesh->plink_state;
  1857. if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
  1858. sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
  1859. sinfo->t_offset = sta->mesh->t_offset;
  1860. }
  1861. sinfo->local_pm = sta->mesh->local_pm;
  1862. sinfo->peer_pm = sta->mesh->peer_pm;
  1863. sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
  1864. #endif
  1865. }
  1866. sinfo->bss_param.flags = 0;
  1867. if (sdata->vif.bss_conf.use_cts_prot)
  1868. sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
  1869. if (sdata->vif.bss_conf.use_short_preamble)
  1870. sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
  1871. if (sdata->vif.bss_conf.use_short_slot)
  1872. sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
  1873. sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
  1874. sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
  1875. sinfo->sta_flags.set = 0;
  1876. sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
  1877. BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
  1878. BIT(NL80211_STA_FLAG_WME) |
  1879. BIT(NL80211_STA_FLAG_MFP) |
  1880. BIT(NL80211_STA_FLAG_AUTHENTICATED) |
  1881. BIT(NL80211_STA_FLAG_ASSOCIATED) |
  1882. BIT(NL80211_STA_FLAG_TDLS_PEER);
  1883. if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  1884. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  1885. if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
  1886. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  1887. if (sta->sta.wme)
  1888. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
  1889. if (test_sta_flag(sta, WLAN_STA_MFP))
  1890. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
  1891. if (test_sta_flag(sta, WLAN_STA_AUTH))
  1892. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
  1893. if (test_sta_flag(sta, WLAN_STA_ASSOC))
  1894. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
  1895. if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
  1896. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  1897. thr = sta_get_expected_throughput(sta);
  1898. if (thr != 0) {
  1899. sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
  1900. sinfo->expected_throughput = thr;
  1901. }
  1902. }
  1903. u32 sta_get_expected_throughput(struct sta_info *sta)
  1904. {
  1905. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1906. struct ieee80211_local *local = sdata->local;
  1907. struct rate_control_ref *ref = NULL;
  1908. u32 thr = 0;
  1909. if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
  1910. ref = local->rate_ctrl;
  1911. /* check if the driver has a SW RC implementation */
  1912. if (ref && ref->ops->get_expected_throughput)
  1913. thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
  1914. else
  1915. thr = drv_get_expected_throughput(local, sta);
  1916. return thr;
  1917. }
  1918. unsigned long ieee80211_sta_last_active(struct sta_info *sta)
  1919. {
  1920. struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
  1921. if (time_after(stats->last_rx, sta->status_stats.last_ack))
  1922. return stats->last_rx;
  1923. return sta->status_stats.last_ack;
  1924. }
  1925. static void sta_update_codel_params(struct sta_info *sta, u32 thr)
  1926. {
  1927. if (!sta->sdata->local->ops->wake_tx_queue)
  1928. return;
  1929. if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
  1930. sta->cparams.target = MS2TIME(50);
  1931. sta->cparams.interval = MS2TIME(300);
  1932. sta->cparams.ecn = false;
  1933. } else {
  1934. sta->cparams.target = MS2TIME(20);
  1935. sta->cparams.interval = MS2TIME(100);
  1936. sta->cparams.ecn = true;
  1937. }
  1938. }
  1939. void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
  1940. u32 thr)
  1941. {
  1942. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1943. sta_update_codel_params(sta, thr);
  1944. }