sta.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <net/mac80211.h>
  68. #include "mvm.h"
  69. #include "sta.h"
  70. #include "rs.h"
  71. /*
  72. * New version of ADD_STA_sta command added new fields at the end of the
  73. * structure, so sending the size of the relevant API's structure is enough to
  74. * support both API versions.
  75. */
  76. static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
  77. {
  78. return iwl_mvm_has_new_rx_api(mvm) ?
  79. sizeof(struct iwl_mvm_add_sta_cmd) :
  80. sizeof(struct iwl_mvm_add_sta_cmd_v7);
  81. }
  82. static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
  83. enum nl80211_iftype iftype)
  84. {
  85. int sta_id;
  86. u32 reserved_ids = 0;
  87. BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
  88. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  89. lockdep_assert_held(&mvm->mutex);
  90. /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
  91. if (iftype != NL80211_IFTYPE_STATION)
  92. reserved_ids = BIT(0);
  93. /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
  94. for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
  95. if (BIT(sta_id) & reserved_ids)
  96. continue;
  97. if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  98. lockdep_is_held(&mvm->mutex)))
  99. return sta_id;
  100. }
  101. return IWL_MVM_STATION_COUNT;
  102. }
  103. /* send station add/update command to firmware */
  104. int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  105. bool update, unsigned int flags)
  106. {
  107. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  108. struct iwl_mvm_add_sta_cmd add_sta_cmd = {
  109. .sta_id = mvm_sta->sta_id,
  110. .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
  111. .add_modify = update ? 1 : 0,
  112. .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
  113. STA_FLG_MIMO_EN_MSK),
  114. .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
  115. };
  116. int ret;
  117. u32 status;
  118. u32 agg_size = 0, mpdu_dens = 0;
  119. if (!update || (flags & STA_MODIFY_QUEUES)) {
  120. add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  121. memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
  122. if (flags & STA_MODIFY_QUEUES)
  123. add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
  124. }
  125. switch (sta->bandwidth) {
  126. case IEEE80211_STA_RX_BW_160:
  127. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
  128. /* fall through */
  129. case IEEE80211_STA_RX_BW_80:
  130. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
  131. /* fall through */
  132. case IEEE80211_STA_RX_BW_40:
  133. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
  134. /* fall through */
  135. case IEEE80211_STA_RX_BW_20:
  136. if (sta->ht_cap.ht_supported)
  137. add_sta_cmd.station_flags |=
  138. cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
  139. break;
  140. }
  141. switch (sta->rx_nss) {
  142. case 1:
  143. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  144. break;
  145. case 2:
  146. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
  147. break;
  148. case 3 ... 8:
  149. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
  150. break;
  151. }
  152. switch (sta->smps_mode) {
  153. case IEEE80211_SMPS_AUTOMATIC:
  154. case IEEE80211_SMPS_NUM_MODES:
  155. WARN_ON(1);
  156. break;
  157. case IEEE80211_SMPS_STATIC:
  158. /* override NSS */
  159. add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
  160. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  161. break;
  162. case IEEE80211_SMPS_DYNAMIC:
  163. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
  164. break;
  165. case IEEE80211_SMPS_OFF:
  166. /* nothing */
  167. break;
  168. }
  169. if (sta->ht_cap.ht_supported) {
  170. add_sta_cmd.station_flags_msk |=
  171. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  172. STA_FLG_AGG_MPDU_DENS_MSK);
  173. mpdu_dens = sta->ht_cap.ampdu_density;
  174. }
  175. if (sta->vht_cap.vht_supported) {
  176. agg_size = sta->vht_cap.cap &
  177. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  178. agg_size >>=
  179. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  180. } else if (sta->ht_cap.ht_supported) {
  181. agg_size = sta->ht_cap.ampdu_factor;
  182. }
  183. add_sta_cmd.station_flags |=
  184. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  185. add_sta_cmd.station_flags |=
  186. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  187. status = ADD_STA_SUCCESS;
  188. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  189. iwl_mvm_add_sta_cmd_size(mvm),
  190. &add_sta_cmd, &status);
  191. if (ret)
  192. return ret;
  193. switch (status & IWL_ADD_STA_STATUS_MASK) {
  194. case ADD_STA_SUCCESS:
  195. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  196. break;
  197. default:
  198. ret = -EIO;
  199. IWL_ERR(mvm, "ADD_STA failed\n");
  200. break;
  201. }
  202. return ret;
  203. }
  204. static void iwl_mvm_rx_agg_session_expired(unsigned long data)
  205. {
  206. struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
  207. struct iwl_mvm_baid_data *ba_data;
  208. struct ieee80211_sta *sta;
  209. struct iwl_mvm_sta *mvm_sta;
  210. unsigned long timeout;
  211. rcu_read_lock();
  212. ba_data = rcu_dereference(*rcu_ptr);
  213. if (WARN_ON(!ba_data))
  214. goto unlock;
  215. if (!ba_data->timeout)
  216. goto unlock;
  217. timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
  218. if (time_is_after_jiffies(timeout)) {
  219. mod_timer(&ba_data->session_timer, timeout);
  220. goto unlock;
  221. }
  222. /* Timer expired */
  223. sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
  224. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  225. ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
  226. sta->addr, ba_data->tid);
  227. unlock:
  228. rcu_read_unlock();
  229. }
  230. static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
  231. struct ieee80211_sta *sta)
  232. {
  233. unsigned long used_hw_queues;
  234. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  235. unsigned int wdg_timeout =
  236. iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
  237. u32 ac;
  238. lockdep_assert_held(&mvm->mutex);
  239. used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
  240. /* Find available queues, and allocate them to the ACs */
  241. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  242. u8 queue = find_first_zero_bit(&used_hw_queues,
  243. mvm->first_agg_queue);
  244. if (queue >= mvm->first_agg_queue) {
  245. IWL_ERR(mvm, "Failed to allocate STA queue\n");
  246. return -EBUSY;
  247. }
  248. __set_bit(queue, &used_hw_queues);
  249. mvmsta->hw_queue[ac] = queue;
  250. }
  251. /* Found a place for all queues - enable them */
  252. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  253. iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
  254. mvmsta->hw_queue[ac],
  255. iwl_mvm_ac_to_tx_fifo[ac], 0,
  256. wdg_timeout);
  257. mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
  258. }
  259. return 0;
  260. }
  261. static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
  262. struct ieee80211_sta *sta)
  263. {
  264. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  265. unsigned long sta_msk;
  266. int i;
  267. lockdep_assert_held(&mvm->mutex);
  268. /* disable the TDLS STA-specific queues */
  269. sta_msk = mvmsta->tfd_queue_msk;
  270. for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
  271. iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
  272. }
  273. /* Disable aggregations for a bitmap of TIDs for a given station */
  274. static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
  275. unsigned long disable_agg_tids,
  276. bool remove_queue)
  277. {
  278. struct iwl_mvm_add_sta_cmd cmd = {};
  279. struct ieee80211_sta *sta;
  280. struct iwl_mvm_sta *mvmsta;
  281. u32 status;
  282. u8 sta_id;
  283. int ret;
  284. spin_lock_bh(&mvm->queue_info_lock);
  285. sta_id = mvm->queue_info[queue].ra_sta_id;
  286. spin_unlock_bh(&mvm->queue_info_lock);
  287. rcu_read_lock();
  288. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  289. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  290. rcu_read_unlock();
  291. return -EINVAL;
  292. }
  293. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  294. mvmsta->tid_disable_agg |= disable_agg_tids;
  295. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  296. cmd.sta_id = mvmsta->sta_id;
  297. cmd.add_modify = STA_MODE_MODIFY;
  298. cmd.modify_mask = STA_MODIFY_QUEUES;
  299. if (disable_agg_tids)
  300. cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
  301. if (remove_queue)
  302. cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
  303. cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
  304. cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
  305. rcu_read_unlock();
  306. /* Notify FW of queue removal from the STA queues */
  307. status = ADD_STA_SUCCESS;
  308. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  309. iwl_mvm_add_sta_cmd_size(mvm),
  310. &cmd, &status);
  311. return ret;
  312. }
  313. static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
  314. {
  315. struct ieee80211_sta *sta;
  316. struct iwl_mvm_sta *mvmsta;
  317. unsigned long tid_bitmap;
  318. unsigned long agg_tids = 0;
  319. s8 sta_id;
  320. int tid;
  321. lockdep_assert_held(&mvm->mutex);
  322. spin_lock_bh(&mvm->queue_info_lock);
  323. sta_id = mvm->queue_info[queue].ra_sta_id;
  324. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  325. spin_unlock_bh(&mvm->queue_info_lock);
  326. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  327. lockdep_is_held(&mvm->mutex));
  328. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  329. return -EINVAL;
  330. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  331. spin_lock_bh(&mvmsta->lock);
  332. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  333. if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
  334. agg_tids |= BIT(tid);
  335. }
  336. spin_unlock_bh(&mvmsta->lock);
  337. return agg_tids;
  338. }
  339. /*
  340. * Remove a queue from a station's resources.
  341. * Note that this only marks as free. It DOESN'T delete a BA agreement, and
  342. * doesn't disable the queue
  343. */
  344. static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
  345. {
  346. struct ieee80211_sta *sta;
  347. struct iwl_mvm_sta *mvmsta;
  348. unsigned long tid_bitmap;
  349. unsigned long disable_agg_tids = 0;
  350. u8 sta_id;
  351. int tid;
  352. lockdep_assert_held(&mvm->mutex);
  353. spin_lock_bh(&mvm->queue_info_lock);
  354. sta_id = mvm->queue_info[queue].ra_sta_id;
  355. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  356. spin_unlock_bh(&mvm->queue_info_lock);
  357. rcu_read_lock();
  358. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  359. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  360. rcu_read_unlock();
  361. return 0;
  362. }
  363. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  364. spin_lock_bh(&mvmsta->lock);
  365. /* Unmap MAC queues and TIDs from this queue */
  366. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  367. if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
  368. disable_agg_tids |= BIT(tid);
  369. mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
  370. }
  371. mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
  372. spin_unlock_bh(&mvmsta->lock);
  373. rcu_read_unlock();
  374. spin_lock_bh(&mvm->queue_info_lock);
  375. /* Unmap MAC queues and TIDs from this queue */
  376. mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
  377. mvm->queue_info[queue].hw_queue_refcount = 0;
  378. mvm->queue_info[queue].tid_bitmap = 0;
  379. spin_unlock_bh(&mvm->queue_info_lock);
  380. return disable_agg_tids;
  381. }
  382. static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
  383. unsigned long tfd_queue_mask, u8 ac)
  384. {
  385. int queue = 0;
  386. u8 ac_to_queue[IEEE80211_NUM_ACS];
  387. int i;
  388. lockdep_assert_held(&mvm->queue_info_lock);
  389. memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
  390. /* See what ACs the existing queues for this STA have */
  391. for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
  392. /* Only DATA queues can be shared */
  393. if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
  394. i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
  395. continue;
  396. /* Don't try and take queues being reconfigured */
  397. if (mvm->queue_info[queue].status ==
  398. IWL_MVM_QUEUE_RECONFIGURING)
  399. continue;
  400. ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
  401. }
  402. /*
  403. * The queue to share is chosen only from DATA queues as follows (in
  404. * descending priority):
  405. * 1. An AC_BE queue
  406. * 2. Same AC queue
  407. * 3. Highest AC queue that is lower than new AC
  408. * 4. Any existing AC (there always is at least 1 DATA queue)
  409. */
  410. /* Priority 1: An AC_BE queue */
  411. if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
  412. queue = ac_to_queue[IEEE80211_AC_BE];
  413. /* Priority 2: Same AC queue */
  414. else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  415. queue = ac_to_queue[ac];
  416. /* Priority 3a: If new AC is VO and VI exists - use VI */
  417. else if (ac == IEEE80211_AC_VO &&
  418. ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
  419. queue = ac_to_queue[IEEE80211_AC_VI];
  420. /* Priority 3b: No BE so only AC less than the new one is BK */
  421. else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
  422. queue = ac_to_queue[IEEE80211_AC_BK];
  423. /* Priority 4a: No BE nor BK - use VI if exists */
  424. else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
  425. queue = ac_to_queue[IEEE80211_AC_VI];
  426. /* Priority 4b: No BE, BK nor VI - use VO if exists */
  427. else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
  428. queue = ac_to_queue[IEEE80211_AC_VO];
  429. /* Make sure queue found (or not) is legal */
  430. if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
  431. !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
  432. (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
  433. IWL_ERR(mvm, "No DATA queues available to share\n");
  434. return -ENOSPC;
  435. }
  436. /* Make sure the queue isn't in the middle of being reconfigured */
  437. if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
  438. IWL_ERR(mvm,
  439. "TXQ %d is in the middle of re-config - try again\n",
  440. queue);
  441. return -EBUSY;
  442. }
  443. return queue;
  444. }
  445. /*
  446. * If a given queue has a higher AC than the TID stream that is being compared
  447. * to, the queue needs to be redirected to the lower AC. This function does that
  448. * in such a case, otherwise - if no redirection required - it does nothing,
  449. * unless the %force param is true.
  450. */
  451. int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
  452. int ac, int ssn, unsigned int wdg_timeout,
  453. bool force)
  454. {
  455. struct iwl_scd_txq_cfg_cmd cmd = {
  456. .scd_queue = queue,
  457. .action = SCD_CFG_DISABLE_QUEUE,
  458. };
  459. bool shared_queue;
  460. unsigned long mq;
  461. int ret;
  462. /*
  463. * If the AC is lower than current one - FIFO needs to be redirected to
  464. * the lowest one of the streams in the queue. Check if this is needed
  465. * here.
  466. * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
  467. * value 3 and VO with value 0, so to check if ac X is lower than ac Y
  468. * we need to check if the numerical value of X is LARGER than of Y.
  469. */
  470. spin_lock_bh(&mvm->queue_info_lock);
  471. if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
  472. spin_unlock_bh(&mvm->queue_info_lock);
  473. IWL_DEBUG_TX_QUEUES(mvm,
  474. "No redirection needed on TXQ #%d\n",
  475. queue);
  476. return 0;
  477. }
  478. cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
  479. cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
  480. cmd.tid = mvm->queue_info[queue].txq_tid;
  481. mq = mvm->queue_info[queue].hw_queue_to_mac80211;
  482. shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
  483. spin_unlock_bh(&mvm->queue_info_lock);
  484. IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
  485. queue, iwl_mvm_ac_to_tx_fifo[ac]);
  486. /* Stop MAC queues and wait for this queue to empty */
  487. iwl_mvm_stop_mac_queues(mvm, mq);
  488. ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
  489. if (ret) {
  490. IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
  491. queue);
  492. ret = -EIO;
  493. goto out;
  494. }
  495. /* Before redirecting the queue we need to de-activate it */
  496. iwl_trans_txq_disable(mvm->trans, queue, false);
  497. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
  498. if (ret)
  499. IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
  500. ret);
  501. /* Make sure the SCD wrptr is correctly set before reconfiguring */
  502. iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
  503. /* Update the TID "owner" of the queue */
  504. spin_lock_bh(&mvm->queue_info_lock);
  505. mvm->queue_info[queue].txq_tid = tid;
  506. spin_unlock_bh(&mvm->queue_info_lock);
  507. /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
  508. /* Redirect to lower AC */
  509. iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
  510. cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
  511. ssn);
  512. /* Update AC marking of the queue */
  513. spin_lock_bh(&mvm->queue_info_lock);
  514. mvm->queue_info[queue].mac80211_ac = ac;
  515. spin_unlock_bh(&mvm->queue_info_lock);
  516. /*
  517. * Mark queue as shared in transport if shared
  518. * Note this has to be done after queue enablement because enablement
  519. * can also set this value, and there is no indication there to shared
  520. * queues
  521. */
  522. if (shared_queue)
  523. iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
  524. out:
  525. /* Continue using the MAC queues */
  526. iwl_mvm_start_mac_queues(mvm, mq);
  527. return ret;
  528. }
  529. static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
  530. struct ieee80211_sta *sta, u8 ac, int tid,
  531. struct ieee80211_hdr *hdr)
  532. {
  533. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  534. struct iwl_trans_txq_scd_cfg cfg = {
  535. .fifo = iwl_mvm_ac_to_tx_fifo[ac],
  536. .sta_id = mvmsta->sta_id,
  537. .tid = tid,
  538. .frame_limit = IWL_FRAME_LIMIT,
  539. };
  540. unsigned int wdg_timeout =
  541. iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
  542. u8 mac_queue = mvmsta->vif->hw_queue[ac];
  543. int queue = -1;
  544. bool using_inactive_queue = false;
  545. unsigned long disable_agg_tids = 0;
  546. enum iwl_mvm_agg_state queue_state;
  547. bool shared_queue = false;
  548. int ssn;
  549. unsigned long tfd_queue_mask;
  550. int ret;
  551. lockdep_assert_held(&mvm->mutex);
  552. spin_lock_bh(&mvmsta->lock);
  553. tfd_queue_mask = mvmsta->tfd_queue_msk;
  554. spin_unlock_bh(&mvmsta->lock);
  555. spin_lock_bh(&mvm->queue_info_lock);
  556. /*
  557. * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
  558. * exists
  559. */
  560. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  561. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  562. queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  563. IWL_MVM_DQA_MIN_MGMT_QUEUE,
  564. IWL_MVM_DQA_MAX_MGMT_QUEUE);
  565. if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
  566. IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
  567. queue);
  568. /* If no such queue is found, we'll use a DATA queue instead */
  569. }
  570. if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
  571. (mvm->queue_info[mvmsta->reserved_queue].status ==
  572. IWL_MVM_QUEUE_RESERVED ||
  573. mvm->queue_info[mvmsta->reserved_queue].status ==
  574. IWL_MVM_QUEUE_INACTIVE)) {
  575. queue = mvmsta->reserved_queue;
  576. mvm->queue_info[queue].reserved = true;
  577. IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
  578. }
  579. if (queue < 0)
  580. queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  581. IWL_MVM_DQA_MIN_DATA_QUEUE,
  582. IWL_MVM_DQA_MAX_DATA_QUEUE);
  583. /*
  584. * Check if this queue is already allocated but inactive.
  585. * In such a case, we'll need to first free this queue before enabling
  586. * it again, so we'll mark it as reserved to make sure no new traffic
  587. * arrives on it
  588. */
  589. if (queue > 0 &&
  590. mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
  591. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
  592. using_inactive_queue = true;
  593. IWL_DEBUG_TX_QUEUES(mvm,
  594. "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
  595. queue, mvmsta->sta_id, tid);
  596. }
  597. /* No free queue - we'll have to share */
  598. if (queue <= 0) {
  599. queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
  600. if (queue > 0) {
  601. shared_queue = true;
  602. mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
  603. }
  604. }
  605. /*
  606. * Mark TXQ as ready, even though it hasn't been fully configured yet,
  607. * to make sure no one else takes it.
  608. * This will allow avoiding re-acquiring the lock at the end of the
  609. * configuration. On error we'll mark it back as free.
  610. */
  611. if ((queue > 0) && !shared_queue)
  612. mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
  613. spin_unlock_bh(&mvm->queue_info_lock);
  614. /* This shouldn't happen - out of queues */
  615. if (WARN_ON(queue <= 0)) {
  616. IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
  617. tid, cfg.sta_id);
  618. return queue;
  619. }
  620. /*
  621. * Actual en/disablement of aggregations is through the ADD_STA HCMD,
  622. * but for configuring the SCD to send A-MPDUs we need to mark the queue
  623. * as aggregatable.
  624. * Mark all DATA queues as allowing to be aggregated at some point
  625. */
  626. cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
  627. queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
  628. /*
  629. * If this queue was previously inactive (idle) - we need to free it
  630. * first
  631. */
  632. if (using_inactive_queue) {
  633. struct iwl_scd_txq_cfg_cmd cmd = {
  634. .scd_queue = queue,
  635. .action = SCD_CFG_DISABLE_QUEUE,
  636. };
  637. u8 txq_curr_ac;
  638. disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
  639. spin_lock_bh(&mvm->queue_info_lock);
  640. txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
  641. cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
  642. cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[txq_curr_ac];
  643. cmd.tid = mvm->queue_info[queue].txq_tid;
  644. spin_unlock_bh(&mvm->queue_info_lock);
  645. /* Disable the queue */
  646. if (disable_agg_tids)
  647. iwl_mvm_invalidate_sta_queue(mvm, queue,
  648. disable_agg_tids, false);
  649. iwl_trans_txq_disable(mvm->trans, queue, false);
  650. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
  651. &cmd);
  652. if (ret) {
  653. IWL_ERR(mvm,
  654. "Failed to free inactive queue %d (ret=%d)\n",
  655. queue, ret);
  656. /* Re-mark the inactive queue as inactive */
  657. spin_lock_bh(&mvm->queue_info_lock);
  658. mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
  659. spin_unlock_bh(&mvm->queue_info_lock);
  660. return ret;
  661. }
  662. /* If TXQ is allocated to another STA, update removal in FW */
  663. if (cmd.sta_id != mvmsta->sta_id)
  664. iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
  665. }
  666. IWL_DEBUG_TX_QUEUES(mvm,
  667. "Allocating %squeue #%d to sta %d on tid %d\n",
  668. shared_queue ? "shared " : "", queue,
  669. mvmsta->sta_id, tid);
  670. if (shared_queue) {
  671. /* Disable any open aggs on this queue */
  672. disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
  673. if (disable_agg_tids) {
  674. IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
  675. queue);
  676. iwl_mvm_invalidate_sta_queue(mvm, queue,
  677. disable_agg_tids, false);
  678. }
  679. }
  680. ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  681. iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
  682. wdg_timeout);
  683. /*
  684. * Mark queue as shared in transport if shared
  685. * Note this has to be done after queue enablement because enablement
  686. * can also set this value, and there is no indication there to shared
  687. * queues
  688. */
  689. if (shared_queue)
  690. iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
  691. spin_lock_bh(&mvmsta->lock);
  692. mvmsta->tid_data[tid].txq_id = queue;
  693. mvmsta->tid_data[tid].is_tid_active = true;
  694. mvmsta->tfd_queue_msk |= BIT(queue);
  695. queue_state = mvmsta->tid_data[tid].state;
  696. if (mvmsta->reserved_queue == queue)
  697. mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
  698. spin_unlock_bh(&mvmsta->lock);
  699. if (!shared_queue) {
  700. ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
  701. if (ret)
  702. goto out_err;
  703. /* If we need to re-enable aggregations... */
  704. if (queue_state == IWL_AGG_ON) {
  705. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  706. if (ret)
  707. goto out_err;
  708. }
  709. } else {
  710. /* Redirect queue, if needed */
  711. ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
  712. wdg_timeout, false);
  713. if (ret)
  714. goto out_err;
  715. }
  716. return 0;
  717. out_err:
  718. iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
  719. return ret;
  720. }
  721. static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
  722. {
  723. struct iwl_scd_txq_cfg_cmd cmd = {
  724. .scd_queue = queue,
  725. .action = SCD_CFG_UPDATE_QUEUE_TID,
  726. };
  727. s8 sta_id;
  728. int tid;
  729. unsigned long tid_bitmap;
  730. int ret;
  731. lockdep_assert_held(&mvm->mutex);
  732. spin_lock_bh(&mvm->queue_info_lock);
  733. sta_id = mvm->queue_info[queue].ra_sta_id;
  734. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  735. spin_unlock_bh(&mvm->queue_info_lock);
  736. if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
  737. return;
  738. /* Find any TID for queue */
  739. tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
  740. cmd.tid = tid;
  741. cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  742. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
  743. if (ret)
  744. IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
  745. queue, ret);
  746. else
  747. IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
  748. queue, tid);
  749. }
  750. static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
  751. {
  752. struct ieee80211_sta *sta;
  753. struct iwl_mvm_sta *mvmsta;
  754. s8 sta_id;
  755. int tid = -1;
  756. unsigned long tid_bitmap;
  757. unsigned int wdg_timeout;
  758. int ssn;
  759. int ret = true;
  760. lockdep_assert_held(&mvm->mutex);
  761. spin_lock_bh(&mvm->queue_info_lock);
  762. sta_id = mvm->queue_info[queue].ra_sta_id;
  763. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  764. spin_unlock_bh(&mvm->queue_info_lock);
  765. /* Find TID for queue, and make sure it is the only one on the queue */
  766. tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
  767. if (tid_bitmap != BIT(tid)) {
  768. IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
  769. queue, tid_bitmap);
  770. return;
  771. }
  772. IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
  773. tid);
  774. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  775. lockdep_is_held(&mvm->mutex));
  776. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  777. return;
  778. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  779. wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
  780. ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
  781. ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
  782. tid_to_mac80211_ac[tid], ssn,
  783. wdg_timeout, true);
  784. if (ret) {
  785. IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
  786. return;
  787. }
  788. /* If aggs should be turned back on - do it */
  789. if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
  790. struct iwl_mvm_add_sta_cmd cmd;
  791. mvmsta->tid_disable_agg &= ~BIT(tid);
  792. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  793. cmd.sta_id = mvmsta->sta_id;
  794. cmd.add_modify = STA_MODE_MODIFY;
  795. cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  796. cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
  797. cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
  798. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  799. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  800. if (!ret) {
  801. IWL_DEBUG_TX_QUEUES(mvm,
  802. "TXQ #%d is now aggregated again\n",
  803. queue);
  804. /* Mark queue intenally as aggregating again */
  805. iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
  806. }
  807. }
  808. spin_lock_bh(&mvm->queue_info_lock);
  809. mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
  810. spin_unlock_bh(&mvm->queue_info_lock);
  811. }
  812. static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
  813. {
  814. if (tid == IWL_MAX_TID_COUNT)
  815. return IEEE80211_AC_VO; /* MGMT */
  816. return tid_to_mac80211_ac[tid];
  817. }
  818. static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
  819. struct ieee80211_sta *sta, int tid)
  820. {
  821. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  822. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  823. struct sk_buff *skb;
  824. struct ieee80211_hdr *hdr;
  825. struct sk_buff_head deferred_tx;
  826. u8 mac_queue;
  827. bool no_queue = false; /* Marks if there is a problem with the queue */
  828. u8 ac;
  829. lockdep_assert_held(&mvm->mutex);
  830. skb = skb_peek(&tid_data->deferred_tx_frames);
  831. if (!skb)
  832. return;
  833. hdr = (void *)skb->data;
  834. ac = iwl_mvm_tid_to_ac_queue(tid);
  835. mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
  836. if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
  837. iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
  838. IWL_ERR(mvm,
  839. "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
  840. mvmsta->sta_id, tid);
  841. /*
  842. * Mark queue as problematic so later the deferred traffic is
  843. * freed, as we can do nothing with it
  844. */
  845. no_queue = true;
  846. }
  847. __skb_queue_head_init(&deferred_tx);
  848. /* Disable bottom-halves when entering TX path */
  849. local_bh_disable();
  850. spin_lock(&mvmsta->lock);
  851. skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
  852. spin_unlock(&mvmsta->lock);
  853. while ((skb = __skb_dequeue(&deferred_tx)))
  854. if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
  855. ieee80211_free_txskb(mvm->hw, skb);
  856. local_bh_enable();
  857. /* Wake queue */
  858. iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
  859. }
  860. void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
  861. {
  862. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
  863. add_stream_wk);
  864. struct ieee80211_sta *sta;
  865. struct iwl_mvm_sta *mvmsta;
  866. unsigned long deferred_tid_traffic;
  867. int queue, sta_id, tid;
  868. /* Check inactivity of queues */
  869. iwl_mvm_inactivity_check(mvm);
  870. mutex_lock(&mvm->mutex);
  871. /* Reconfigure queues requiring reconfiguation */
  872. for (queue = 0; queue < IWL_MAX_HW_QUEUES; queue++) {
  873. bool reconfig;
  874. bool change_owner;
  875. spin_lock_bh(&mvm->queue_info_lock);
  876. reconfig = (mvm->queue_info[queue].status ==
  877. IWL_MVM_QUEUE_RECONFIGURING);
  878. /*
  879. * We need to take into account a situation in which a TXQ was
  880. * allocated to TID x, and then turned shared by adding TIDs y
  881. * and z. If TID x becomes inactive and is removed from the TXQ,
  882. * ownership must be given to one of the remaining TIDs.
  883. * This is mainly because if TID x continues - a new queue can't
  884. * be allocated for it as long as it is an owner of another TXQ.
  885. */
  886. change_owner = !(mvm->queue_info[queue].tid_bitmap &
  887. BIT(mvm->queue_info[queue].txq_tid)) &&
  888. (mvm->queue_info[queue].status ==
  889. IWL_MVM_QUEUE_SHARED);
  890. spin_unlock_bh(&mvm->queue_info_lock);
  891. if (reconfig)
  892. iwl_mvm_unshare_queue(mvm, queue);
  893. else if (change_owner)
  894. iwl_mvm_change_queue_owner(mvm, queue);
  895. }
  896. /* Go over all stations with deferred traffic */
  897. for_each_set_bit(sta_id, mvm->sta_deferred_frames,
  898. IWL_MVM_STATION_COUNT) {
  899. clear_bit(sta_id, mvm->sta_deferred_frames);
  900. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  901. lockdep_is_held(&mvm->mutex));
  902. if (IS_ERR_OR_NULL(sta))
  903. continue;
  904. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  905. deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
  906. for_each_set_bit(tid, &deferred_tid_traffic,
  907. IWL_MAX_TID_COUNT + 1)
  908. iwl_mvm_tx_deferred_stream(mvm, sta, tid);
  909. }
  910. mutex_unlock(&mvm->mutex);
  911. }
  912. static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
  913. struct ieee80211_sta *sta,
  914. enum nl80211_iftype vif_type)
  915. {
  916. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  917. int queue;
  918. /*
  919. * Check for inactive queues, so we don't reach a situation where we
  920. * can't add a STA due to a shortage in queues that doesn't really exist
  921. */
  922. iwl_mvm_inactivity_check(mvm);
  923. spin_lock_bh(&mvm->queue_info_lock);
  924. /* Make sure we have free resources for this STA */
  925. if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
  926. !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
  927. (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
  928. IWL_MVM_QUEUE_FREE))
  929. queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
  930. else
  931. queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  932. IWL_MVM_DQA_MIN_DATA_QUEUE,
  933. IWL_MVM_DQA_MAX_DATA_QUEUE);
  934. if (queue < 0) {
  935. spin_unlock_bh(&mvm->queue_info_lock);
  936. IWL_ERR(mvm, "No available queues for new station\n");
  937. return -ENOSPC;
  938. }
  939. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
  940. spin_unlock_bh(&mvm->queue_info_lock);
  941. mvmsta->reserved_queue = queue;
  942. IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
  943. queue, mvmsta->sta_id);
  944. return 0;
  945. }
  946. /*
  947. * In DQA mode, after a HW restart the queues should be allocated as before, in
  948. * order to avoid race conditions when there are shared queues. This function
  949. * does the re-mapping and queue allocation.
  950. *
  951. * Note that re-enabling aggregations isn't done in this function.
  952. */
  953. static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
  954. struct iwl_mvm_sta *mvm_sta)
  955. {
  956. unsigned int wdg_timeout =
  957. iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
  958. int i;
  959. struct iwl_trans_txq_scd_cfg cfg = {
  960. .sta_id = mvm_sta->sta_id,
  961. .frame_limit = IWL_FRAME_LIMIT,
  962. };
  963. /* Make sure reserved queue is still marked as such (or allocated) */
  964. mvm->queue_info[mvm_sta->reserved_queue].status =
  965. IWL_MVM_QUEUE_RESERVED;
  966. for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
  967. struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
  968. int txq_id = tid_data->txq_id;
  969. int ac;
  970. u8 mac_queue;
  971. if (txq_id == IEEE80211_INVAL_HW_QUEUE)
  972. continue;
  973. skb_queue_head_init(&tid_data->deferred_tx_frames);
  974. ac = tid_to_mac80211_ac[i];
  975. mac_queue = mvm_sta->vif->hw_queue[ac];
  976. cfg.tid = i;
  977. cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
  978. cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
  979. txq_id == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
  980. IWL_DEBUG_TX_QUEUES(mvm,
  981. "Re-mapping sta %d tid %d to queue %d\n",
  982. mvm_sta->sta_id, i, txq_id);
  983. iwl_mvm_enable_txq(mvm, txq_id, mac_queue,
  984. IEEE80211_SEQ_TO_SN(tid_data->seq_number),
  985. &cfg, wdg_timeout);
  986. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
  987. }
  988. atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
  989. }
  990. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  991. struct ieee80211_vif *vif,
  992. struct ieee80211_sta *sta)
  993. {
  994. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  995. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  996. struct iwl_mvm_rxq_dup_data *dup_data;
  997. int i, ret, sta_id;
  998. lockdep_assert_held(&mvm->mutex);
  999. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  1000. sta_id = iwl_mvm_find_free_sta_id(mvm,
  1001. ieee80211_vif_type_p2p(vif));
  1002. else
  1003. sta_id = mvm_sta->sta_id;
  1004. if (sta_id == IWL_MVM_STATION_COUNT)
  1005. return -ENOSPC;
  1006. spin_lock_init(&mvm_sta->lock);
  1007. /* In DQA mode, if this is a HW restart, re-alloc existing queues */
  1008. if (iwl_mvm_is_dqa_supported(mvm) &&
  1009. test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  1010. iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
  1011. goto update_fw;
  1012. }
  1013. mvm_sta->sta_id = sta_id;
  1014. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  1015. mvmvif->color);
  1016. mvm_sta->vif = vif;
  1017. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  1018. mvm_sta->tx_protection = 0;
  1019. mvm_sta->tt_tx_protection = false;
  1020. /* HW restart, don't assume the memory has been zeroed */
  1021. atomic_set(&mvm->pending_frames[sta_id], 0);
  1022. mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
  1023. mvm_sta->tfd_queue_msk = 0;
  1024. /*
  1025. * Allocate new queues for a TDLS station, unless we're in DQA mode,
  1026. * and then they'll be allocated dynamically
  1027. */
  1028. if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
  1029. ret = iwl_mvm_tdls_sta_init(mvm, sta);
  1030. if (ret)
  1031. return ret;
  1032. } else if (!iwl_mvm_is_dqa_supported(mvm)) {
  1033. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1034. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  1035. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  1036. }
  1037. /* for HW restart - reset everything but the sequence number */
  1038. for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
  1039. u16 seq = mvm_sta->tid_data[i].seq_number;
  1040. memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
  1041. mvm_sta->tid_data[i].seq_number = seq;
  1042. if (!iwl_mvm_is_dqa_supported(mvm))
  1043. continue;
  1044. /*
  1045. * Mark all queues for this STA as unallocated and defer TX
  1046. * frames until the queue is allocated
  1047. */
  1048. mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
  1049. skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
  1050. }
  1051. mvm_sta->deferred_traffic_tid_map = 0;
  1052. mvm_sta->agg_tids = 0;
  1053. if (iwl_mvm_has_new_rx_api(mvm) &&
  1054. !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  1055. dup_data = kcalloc(mvm->trans->num_rx_queues,
  1056. sizeof(*dup_data),
  1057. GFP_KERNEL);
  1058. if (!dup_data)
  1059. return -ENOMEM;
  1060. mvm_sta->dup_data = dup_data;
  1061. }
  1062. if (iwl_mvm_is_dqa_supported(mvm)) {
  1063. ret = iwl_mvm_reserve_sta_stream(mvm, sta,
  1064. ieee80211_vif_type_p2p(vif));
  1065. if (ret)
  1066. goto err;
  1067. }
  1068. update_fw:
  1069. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
  1070. if (ret)
  1071. goto err;
  1072. if (vif->type == NL80211_IFTYPE_STATION) {
  1073. if (!sta->tdls) {
  1074. WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
  1075. mvmvif->ap_sta_id = sta_id;
  1076. } else {
  1077. WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
  1078. }
  1079. }
  1080. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  1081. return 0;
  1082. err:
  1083. if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
  1084. iwl_mvm_tdls_sta_deinit(mvm, sta);
  1085. return ret;
  1086. }
  1087. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  1088. bool drain)
  1089. {
  1090. struct iwl_mvm_add_sta_cmd cmd = {};
  1091. int ret;
  1092. u32 status;
  1093. lockdep_assert_held(&mvm->mutex);
  1094. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  1095. cmd.sta_id = mvmsta->sta_id;
  1096. cmd.add_modify = STA_MODE_MODIFY;
  1097. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  1098. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  1099. status = ADD_STA_SUCCESS;
  1100. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1101. iwl_mvm_add_sta_cmd_size(mvm),
  1102. &cmd, &status);
  1103. if (ret)
  1104. return ret;
  1105. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1106. case ADD_STA_SUCCESS:
  1107. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  1108. mvmsta->sta_id);
  1109. break;
  1110. default:
  1111. ret = -EIO;
  1112. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  1113. mvmsta->sta_id);
  1114. break;
  1115. }
  1116. return ret;
  1117. }
  1118. /*
  1119. * Remove a station from the FW table. Before sending the command to remove
  1120. * the station validate that the station is indeed known to the driver (sanity
  1121. * only).
  1122. */
  1123. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  1124. {
  1125. struct ieee80211_sta *sta;
  1126. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  1127. .sta_id = sta_id,
  1128. };
  1129. int ret;
  1130. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1131. lockdep_is_held(&mvm->mutex));
  1132. /* Note: internal stations are marked as error values */
  1133. if (!sta) {
  1134. IWL_ERR(mvm, "Invalid station id\n");
  1135. return -EINVAL;
  1136. }
  1137. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
  1138. sizeof(rm_sta_cmd), &rm_sta_cmd);
  1139. if (ret) {
  1140. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  1141. return ret;
  1142. }
  1143. return 0;
  1144. }
  1145. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  1146. {
  1147. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  1148. u8 sta_id;
  1149. /*
  1150. * The mutex is needed because of the SYNC cmd, but not only: if the
  1151. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  1152. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  1153. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  1154. * that later.
  1155. */
  1156. mutex_lock(&mvm->mutex);
  1157. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  1158. int ret;
  1159. struct ieee80211_sta *sta =
  1160. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1161. lockdep_is_held(&mvm->mutex));
  1162. /*
  1163. * This station is in use or RCU-removed; the latter happens in
  1164. * managed mode, where mac80211 removes the station before we
  1165. * can remove it from firmware (we can only do that after the
  1166. * MAC is marked unassociated), and possibly while the deauth
  1167. * frame to disconnect from the AP is still queued. Then, the
  1168. * station pointer is -ENOENT when the last skb is reclaimed.
  1169. */
  1170. if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
  1171. continue;
  1172. if (PTR_ERR(sta) == -EINVAL) {
  1173. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  1174. sta_id);
  1175. continue;
  1176. }
  1177. if (!sta) {
  1178. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  1179. sta_id);
  1180. continue;
  1181. }
  1182. WARN_ON(PTR_ERR(sta) != -EBUSY);
  1183. /* This station was removed and we waited until it got drained,
  1184. * we can now proceed and remove it.
  1185. */
  1186. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  1187. if (ret) {
  1188. IWL_ERR(mvm,
  1189. "Couldn't remove sta %d after it was drained\n",
  1190. sta_id);
  1191. continue;
  1192. }
  1193. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  1194. clear_bit(sta_id, mvm->sta_drained);
  1195. if (mvm->tfd_drained[sta_id]) {
  1196. unsigned long i, msk = mvm->tfd_drained[sta_id];
  1197. for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
  1198. iwl_mvm_disable_txq(mvm, i, i,
  1199. IWL_MAX_TID_COUNT, 0);
  1200. mvm->tfd_drained[sta_id] = 0;
  1201. IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
  1202. sta_id, msk);
  1203. }
  1204. }
  1205. mutex_unlock(&mvm->mutex);
  1206. }
  1207. static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
  1208. struct ieee80211_vif *vif,
  1209. struct iwl_mvm_sta *mvm_sta)
  1210. {
  1211. int ac;
  1212. int i;
  1213. lockdep_assert_held(&mvm->mutex);
  1214. for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
  1215. if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
  1216. continue;
  1217. ac = iwl_mvm_tid_to_ac_queue(i);
  1218. iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
  1219. vif->hw_queue[ac], i, 0);
  1220. mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
  1221. }
  1222. }
  1223. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  1224. struct ieee80211_vif *vif,
  1225. struct ieee80211_sta *sta)
  1226. {
  1227. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1228. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1229. int ret;
  1230. lockdep_assert_held(&mvm->mutex);
  1231. if (iwl_mvm_has_new_rx_api(mvm))
  1232. kfree(mvm_sta->dup_data);
  1233. if ((vif->type == NL80211_IFTYPE_STATION &&
  1234. mvmvif->ap_sta_id == mvm_sta->sta_id) ||
  1235. iwl_mvm_is_dqa_supported(mvm)){
  1236. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  1237. if (ret)
  1238. return ret;
  1239. /* flush its queues here since we are freeing mvm_sta */
  1240. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
  1241. if (ret)
  1242. return ret;
  1243. ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
  1244. mvm_sta->tfd_queue_msk);
  1245. if (ret)
  1246. return ret;
  1247. ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
  1248. /* If DQA is supported - the queues can be disabled now */
  1249. if (iwl_mvm_is_dqa_supported(mvm)) {
  1250. u8 reserved_txq = mvm_sta->reserved_queue;
  1251. enum iwl_mvm_queue_status *status;
  1252. iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
  1253. /*
  1254. * If no traffic has gone through the reserved TXQ - it
  1255. * is still marked as IWL_MVM_QUEUE_RESERVED, and
  1256. * should be manually marked as free again
  1257. */
  1258. spin_lock_bh(&mvm->queue_info_lock);
  1259. status = &mvm->queue_info[reserved_txq].status;
  1260. if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
  1261. (*status != IWL_MVM_QUEUE_FREE),
  1262. "sta_id %d reserved txq %d status %d",
  1263. mvm_sta->sta_id, reserved_txq, *status)) {
  1264. spin_unlock_bh(&mvm->queue_info_lock);
  1265. return -EINVAL;
  1266. }
  1267. *status = IWL_MVM_QUEUE_FREE;
  1268. spin_unlock_bh(&mvm->queue_info_lock);
  1269. }
  1270. if (vif->type == NL80211_IFTYPE_STATION &&
  1271. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  1272. /* if associated - we can't remove the AP STA now */
  1273. if (vif->bss_conf.assoc)
  1274. return ret;
  1275. /* unassoc - go ahead - remove the AP STA now */
  1276. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  1277. /* clear d0i3_ap_sta_id if no longer relevant */
  1278. if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
  1279. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  1280. }
  1281. }
  1282. /*
  1283. * This shouldn't happen - the TDLS channel switch should be canceled
  1284. * before the STA is removed.
  1285. */
  1286. if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
  1287. mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
  1288. cancel_delayed_work(&mvm->tdls_cs.dwork);
  1289. }
  1290. /*
  1291. * Make sure that the tx response code sees the station as -EBUSY and
  1292. * calls the drain worker.
  1293. */
  1294. spin_lock_bh(&mvm_sta->lock);
  1295. /*
  1296. * There are frames pending on the AC queues for this station.
  1297. * We need to wait until all the frames are drained...
  1298. */
  1299. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  1300. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  1301. ERR_PTR(-EBUSY));
  1302. spin_unlock_bh(&mvm_sta->lock);
  1303. /* disable TDLS sta queues on drain complete */
  1304. if (sta->tdls) {
  1305. mvm->tfd_drained[mvm_sta->sta_id] =
  1306. mvm_sta->tfd_queue_msk;
  1307. IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
  1308. mvm_sta->sta_id);
  1309. }
  1310. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  1311. } else {
  1312. spin_unlock_bh(&mvm_sta->lock);
  1313. if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
  1314. iwl_mvm_tdls_sta_deinit(mvm, sta);
  1315. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  1316. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  1317. }
  1318. return ret;
  1319. }
  1320. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  1321. struct ieee80211_vif *vif,
  1322. u8 sta_id)
  1323. {
  1324. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  1325. lockdep_assert_held(&mvm->mutex);
  1326. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  1327. return ret;
  1328. }
  1329. int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
  1330. struct iwl_mvm_int_sta *sta,
  1331. u32 qmask, enum nl80211_iftype iftype)
  1332. {
  1333. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  1334. sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
  1335. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  1336. return -ENOSPC;
  1337. }
  1338. sta->tfd_queue_msk = qmask;
  1339. /* put a non-NULL value so iterating over the stations won't stop */
  1340. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  1341. return 0;
  1342. }
  1343. static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
  1344. struct iwl_mvm_int_sta *sta)
  1345. {
  1346. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  1347. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  1348. sta->sta_id = IWL_MVM_STATION_COUNT;
  1349. }
  1350. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  1351. struct iwl_mvm_int_sta *sta,
  1352. const u8 *addr,
  1353. u16 mac_id, u16 color)
  1354. {
  1355. struct iwl_mvm_add_sta_cmd cmd;
  1356. int ret;
  1357. u32 status;
  1358. lockdep_assert_held(&mvm->mutex);
  1359. memset(&cmd, 0, sizeof(cmd));
  1360. cmd.sta_id = sta->sta_id;
  1361. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  1362. color));
  1363. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  1364. cmd.tid_disable_tx = cpu_to_le16(0xffff);
  1365. if (addr)
  1366. memcpy(cmd.addr, addr, ETH_ALEN);
  1367. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1368. iwl_mvm_add_sta_cmd_size(mvm),
  1369. &cmd, &status);
  1370. if (ret)
  1371. return ret;
  1372. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1373. case ADD_STA_SUCCESS:
  1374. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  1375. return 0;
  1376. default:
  1377. ret = -EIO;
  1378. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  1379. status);
  1380. break;
  1381. }
  1382. return ret;
  1383. }
  1384. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  1385. {
  1386. unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
  1387. mvm->cfg->base_params->wd_timeout :
  1388. IWL_WATCHDOG_DISABLED;
  1389. int ret;
  1390. lockdep_assert_held(&mvm->mutex);
  1391. /* Map Aux queue to fifo - needs to happen before adding Aux station */
  1392. if (!iwl_mvm_is_dqa_supported(mvm))
  1393. iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
  1394. IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
  1395. /* Allocate aux station and assign to it the aux queue */
  1396. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
  1397. NL80211_IFTYPE_UNSPECIFIED);
  1398. if (ret)
  1399. return ret;
  1400. if (iwl_mvm_is_dqa_supported(mvm)) {
  1401. struct iwl_trans_txq_scd_cfg cfg = {
  1402. .fifo = IWL_MVM_TX_FIFO_MCAST,
  1403. .sta_id = mvm->aux_sta.sta_id,
  1404. .tid = IWL_MAX_TID_COUNT,
  1405. .aggregate = false,
  1406. .frame_limit = IWL_FRAME_LIMIT,
  1407. };
  1408. iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
  1409. wdg_timeout);
  1410. }
  1411. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  1412. MAC_INDEX_AUX, 0);
  1413. if (ret)
  1414. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  1415. return ret;
  1416. }
  1417. int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1418. {
  1419. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1420. lockdep_assert_held(&mvm->mutex);
  1421. return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
  1422. mvmvif->id, 0);
  1423. }
  1424. int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1425. {
  1426. int ret;
  1427. lockdep_assert_held(&mvm->mutex);
  1428. ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
  1429. if (ret)
  1430. IWL_WARN(mvm, "Failed sending remove station\n");
  1431. return ret;
  1432. }
  1433. void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
  1434. {
  1435. iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
  1436. }
  1437. void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
  1438. {
  1439. lockdep_assert_held(&mvm->mutex);
  1440. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  1441. }
  1442. /*
  1443. * Send the add station command for the vif's broadcast station.
  1444. * Assumes that the station was already allocated.
  1445. *
  1446. * @mvm: the mvm component
  1447. * @vif: the interface to which the broadcast station is added
  1448. * @bsta: the broadcast station to add.
  1449. */
  1450. int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1451. {
  1452. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1453. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  1454. static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  1455. const u8 *baddr = _baddr;
  1456. lockdep_assert_held(&mvm->mutex);
  1457. if (iwl_mvm_is_dqa_supported(mvm)) {
  1458. struct iwl_trans_txq_scd_cfg cfg = {
  1459. .fifo = IWL_MVM_TX_FIFO_VO,
  1460. .sta_id = mvmvif->bcast_sta.sta_id,
  1461. .tid = IWL_MAX_TID_COUNT,
  1462. .aggregate = false,
  1463. .frame_limit = IWL_FRAME_LIMIT,
  1464. };
  1465. unsigned int wdg_timeout =
  1466. iwl_mvm_get_wd_timeout(mvm, vif, false, false);
  1467. int queue;
  1468. if ((vif->type == NL80211_IFTYPE_AP) &&
  1469. (mvmvif->bcast_sta.tfd_queue_msk &
  1470. BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
  1471. queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
  1472. else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
  1473. (mvmvif->bcast_sta.tfd_queue_msk &
  1474. BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
  1475. queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
  1476. else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
  1477. return -EINVAL;
  1478. iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
  1479. wdg_timeout);
  1480. }
  1481. if (vif->type == NL80211_IFTYPE_ADHOC)
  1482. baddr = vif->bss_conf.bssid;
  1483. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  1484. return -ENOSPC;
  1485. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  1486. mvmvif->id, mvmvif->color);
  1487. }
  1488. /* Send the FW a request to remove the station from it's internal data
  1489. * structures, but DO NOT remove the entry from the local data structures. */
  1490. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1491. {
  1492. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1493. int ret;
  1494. lockdep_assert_held(&mvm->mutex);
  1495. ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
  1496. if (ret)
  1497. IWL_WARN(mvm, "Failed sending remove station\n");
  1498. return ret;
  1499. }
  1500. int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1501. {
  1502. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1503. u32 qmask = 0;
  1504. lockdep_assert_held(&mvm->mutex);
  1505. if (!iwl_mvm_is_dqa_supported(mvm))
  1506. qmask = iwl_mvm_mac_get_queues_mask(vif);
  1507. if (vif->type == NL80211_IFTYPE_AP) {
  1508. /*
  1509. * The firmware defines the TFD queue mask to only be relevant
  1510. * for *unicast* queues, so the multicast (CAB) queue shouldn't
  1511. * be included.
  1512. */
  1513. qmask &= ~BIT(vif->cab_queue);
  1514. if (iwl_mvm_is_dqa_supported(mvm))
  1515. qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
  1516. } else if (iwl_mvm_is_dqa_supported(mvm) &&
  1517. vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1518. qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
  1519. }
  1520. return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
  1521. ieee80211_vif_type_p2p(vif));
  1522. }
  1523. /* Allocate a new station entry for the broadcast station to the given vif,
  1524. * and send it to the FW.
  1525. * Note that each P2P mac should have its own broadcast station.
  1526. *
  1527. * @mvm: the mvm component
  1528. * @vif: the interface to which the broadcast station is added
  1529. * @bsta: the broadcast station to add. */
  1530. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1531. {
  1532. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1533. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  1534. int ret;
  1535. lockdep_assert_held(&mvm->mutex);
  1536. ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
  1537. if (ret)
  1538. return ret;
  1539. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  1540. if (ret)
  1541. iwl_mvm_dealloc_int_sta(mvm, bsta);
  1542. return ret;
  1543. }
  1544. void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1545. {
  1546. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1547. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  1548. }
  1549. /*
  1550. * Send the FW a request to remove the station from it's internal data
  1551. * structures, and in addition remove it from the local data structure.
  1552. */
  1553. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1554. {
  1555. int ret;
  1556. lockdep_assert_held(&mvm->mutex);
  1557. ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
  1558. iwl_mvm_dealloc_bcast_sta(mvm, vif);
  1559. return ret;
  1560. }
  1561. #define IWL_MAX_RX_BA_SESSIONS 16
  1562. static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
  1563. {
  1564. struct iwl_mvm_delba_notif notif = {
  1565. .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
  1566. .metadata.sync = 1,
  1567. .delba.baid = baid,
  1568. };
  1569. iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
  1570. };
  1571. static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
  1572. struct iwl_mvm_baid_data *data)
  1573. {
  1574. int i;
  1575. iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
  1576. for (i = 0; i < mvm->trans->num_rx_queues; i++) {
  1577. int j;
  1578. struct iwl_mvm_reorder_buffer *reorder_buf =
  1579. &data->reorder_buf[i];
  1580. spin_lock_bh(&reorder_buf->lock);
  1581. if (likely(!reorder_buf->num_stored)) {
  1582. spin_unlock_bh(&reorder_buf->lock);
  1583. continue;
  1584. }
  1585. /*
  1586. * This shouldn't happen in regular DELBA since the internal
  1587. * delBA notification should trigger a release of all frames in
  1588. * the reorder buffer.
  1589. */
  1590. WARN_ON(1);
  1591. for (j = 0; j < reorder_buf->buf_size; j++)
  1592. __skb_queue_purge(&reorder_buf->entries[j]);
  1593. /*
  1594. * Prevent timer re-arm. This prevents a very far fetched case
  1595. * where we timed out on the notification. There may be prior
  1596. * RX frames pending in the RX queue before the notification
  1597. * that might get processed between now and the actual deletion
  1598. * and we would re-arm the timer although we are deleting the
  1599. * reorder buffer.
  1600. */
  1601. reorder_buf->removed = true;
  1602. spin_unlock_bh(&reorder_buf->lock);
  1603. del_timer_sync(&reorder_buf->reorder_timer);
  1604. }
  1605. }
  1606. static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
  1607. u32 sta_id,
  1608. struct iwl_mvm_baid_data *data,
  1609. u16 ssn, u8 buf_size)
  1610. {
  1611. int i;
  1612. for (i = 0; i < mvm->trans->num_rx_queues; i++) {
  1613. struct iwl_mvm_reorder_buffer *reorder_buf =
  1614. &data->reorder_buf[i];
  1615. int j;
  1616. reorder_buf->num_stored = 0;
  1617. reorder_buf->head_sn = ssn;
  1618. reorder_buf->buf_size = buf_size;
  1619. /* rx reorder timer */
  1620. reorder_buf->reorder_timer.function =
  1621. iwl_mvm_reorder_timer_expired;
  1622. reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
  1623. init_timer(&reorder_buf->reorder_timer);
  1624. spin_lock_init(&reorder_buf->lock);
  1625. reorder_buf->mvm = mvm;
  1626. reorder_buf->queue = i;
  1627. reorder_buf->sta_id = sta_id;
  1628. for (j = 0; j < reorder_buf->buf_size; j++)
  1629. __skb_queue_head_init(&reorder_buf->entries[j]);
  1630. }
  1631. }
  1632. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1633. int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
  1634. {
  1635. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1636. struct iwl_mvm_add_sta_cmd cmd = {};
  1637. struct iwl_mvm_baid_data *baid_data = NULL;
  1638. int ret;
  1639. u32 status;
  1640. lockdep_assert_held(&mvm->mutex);
  1641. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  1642. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  1643. return -ENOSPC;
  1644. }
  1645. if (iwl_mvm_has_new_rx_api(mvm) && start) {
  1646. /*
  1647. * Allocate here so if allocation fails we can bail out early
  1648. * before starting the BA session in the firmware
  1649. */
  1650. baid_data = kzalloc(sizeof(*baid_data) +
  1651. mvm->trans->num_rx_queues *
  1652. sizeof(baid_data->reorder_buf[0]),
  1653. GFP_KERNEL);
  1654. if (!baid_data)
  1655. return -ENOMEM;
  1656. }
  1657. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  1658. cmd.sta_id = mvm_sta->sta_id;
  1659. cmd.add_modify = STA_MODE_MODIFY;
  1660. if (start) {
  1661. cmd.add_immediate_ba_tid = (u8) tid;
  1662. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1663. cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
  1664. } else {
  1665. cmd.remove_immediate_ba_tid = (u8) tid;
  1666. }
  1667. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  1668. STA_MODIFY_REMOVE_BA_TID;
  1669. status = ADD_STA_SUCCESS;
  1670. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1671. iwl_mvm_add_sta_cmd_size(mvm),
  1672. &cmd, &status);
  1673. if (ret)
  1674. goto out_free;
  1675. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1676. case ADD_STA_SUCCESS:
  1677. IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
  1678. start ? "start" : "stopp");
  1679. break;
  1680. case ADD_STA_IMMEDIATE_BA_FAILURE:
  1681. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  1682. ret = -ENOSPC;
  1683. break;
  1684. default:
  1685. ret = -EIO;
  1686. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  1687. start ? "start" : "stopp", status);
  1688. break;
  1689. }
  1690. if (ret)
  1691. goto out_free;
  1692. if (start) {
  1693. u8 baid;
  1694. mvm->rx_ba_sessions++;
  1695. if (!iwl_mvm_has_new_rx_api(mvm))
  1696. return 0;
  1697. if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
  1698. ret = -EINVAL;
  1699. goto out_free;
  1700. }
  1701. baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
  1702. IWL_ADD_STA_BAID_SHIFT);
  1703. baid_data->baid = baid;
  1704. baid_data->timeout = timeout;
  1705. baid_data->last_rx = jiffies;
  1706. setup_timer(&baid_data->session_timer,
  1707. iwl_mvm_rx_agg_session_expired,
  1708. (unsigned long)&mvm->baid_map[baid]);
  1709. baid_data->mvm = mvm;
  1710. baid_data->tid = tid;
  1711. baid_data->sta_id = mvm_sta->sta_id;
  1712. mvm_sta->tid_to_baid[tid] = baid;
  1713. if (timeout)
  1714. mod_timer(&baid_data->session_timer,
  1715. TU_TO_EXP_TIME(timeout * 2));
  1716. iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
  1717. baid_data, ssn, buf_size);
  1718. /*
  1719. * protect the BA data with RCU to cover a case where our
  1720. * internal RX sync mechanism will timeout (not that it's
  1721. * supposed to happen) and we will free the session data while
  1722. * RX is being processed in parallel
  1723. */
  1724. IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
  1725. mvm_sta->sta_id, tid, baid);
  1726. WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
  1727. rcu_assign_pointer(mvm->baid_map[baid], baid_data);
  1728. } else {
  1729. u8 baid = mvm_sta->tid_to_baid[tid];
  1730. if (mvm->rx_ba_sessions > 0)
  1731. /* check that restart flow didn't zero the counter */
  1732. mvm->rx_ba_sessions--;
  1733. if (!iwl_mvm_has_new_rx_api(mvm))
  1734. return 0;
  1735. if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
  1736. return -EINVAL;
  1737. baid_data = rcu_access_pointer(mvm->baid_map[baid]);
  1738. if (WARN_ON(!baid_data))
  1739. return -EINVAL;
  1740. /* synchronize all rx queues so we can safely delete */
  1741. iwl_mvm_free_reorder(mvm, baid_data);
  1742. del_timer_sync(&baid_data->session_timer);
  1743. RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
  1744. kfree_rcu(baid_data, rcu_head);
  1745. IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
  1746. }
  1747. return 0;
  1748. out_free:
  1749. kfree(baid_data);
  1750. return ret;
  1751. }
  1752. int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1753. int tid, u8 queue, bool start)
  1754. {
  1755. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1756. struct iwl_mvm_add_sta_cmd cmd = {};
  1757. int ret;
  1758. u32 status;
  1759. lockdep_assert_held(&mvm->mutex);
  1760. if (start) {
  1761. mvm_sta->tfd_queue_msk |= BIT(queue);
  1762. mvm_sta->tid_disable_agg &= ~BIT(tid);
  1763. } else {
  1764. /* In DQA-mode the queue isn't removed on agg termination */
  1765. if (!iwl_mvm_is_dqa_supported(mvm))
  1766. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  1767. mvm_sta->tid_disable_agg |= BIT(tid);
  1768. }
  1769. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  1770. cmd.sta_id = mvm_sta->sta_id;
  1771. cmd.add_modify = STA_MODE_MODIFY;
  1772. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  1773. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  1774. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  1775. status = ADD_STA_SUCCESS;
  1776. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1777. iwl_mvm_add_sta_cmd_size(mvm),
  1778. &cmd, &status);
  1779. if (ret)
  1780. return ret;
  1781. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1782. case ADD_STA_SUCCESS:
  1783. break;
  1784. default:
  1785. ret = -EIO;
  1786. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  1787. start ? "start" : "stopp", status);
  1788. break;
  1789. }
  1790. return ret;
  1791. }
  1792. const u8 tid_to_mac80211_ac[] = {
  1793. IEEE80211_AC_BE,
  1794. IEEE80211_AC_BK,
  1795. IEEE80211_AC_BK,
  1796. IEEE80211_AC_BE,
  1797. IEEE80211_AC_VI,
  1798. IEEE80211_AC_VI,
  1799. IEEE80211_AC_VO,
  1800. IEEE80211_AC_VO,
  1801. IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
  1802. };
  1803. static const u8 tid_to_ucode_ac[] = {
  1804. AC_BE,
  1805. AC_BK,
  1806. AC_BK,
  1807. AC_BE,
  1808. AC_VI,
  1809. AC_VI,
  1810. AC_VO,
  1811. AC_VO,
  1812. };
  1813. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1814. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  1815. {
  1816. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1817. struct iwl_mvm_tid_data *tid_data;
  1818. int txq_id;
  1819. int ret;
  1820. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  1821. return -EINVAL;
  1822. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  1823. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  1824. mvmsta->tid_data[tid].state);
  1825. return -ENXIO;
  1826. }
  1827. lockdep_assert_held(&mvm->mutex);
  1828. spin_lock_bh(&mvmsta->lock);
  1829. /* possible race condition - we entered D0i3 while starting agg */
  1830. if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
  1831. spin_unlock_bh(&mvmsta->lock);
  1832. IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
  1833. return -EIO;
  1834. }
  1835. spin_lock(&mvm->queue_info_lock);
  1836. /*
  1837. * Note the possible cases:
  1838. * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
  1839. * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
  1840. * one and mark it as reserved
  1841. * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
  1842. * non-DQA mode, since the TXQ hasn't yet been allocated
  1843. */
  1844. txq_id = mvmsta->tid_data[tid].txq_id;
  1845. if (iwl_mvm_is_dqa_supported(mvm) &&
  1846. unlikely(mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_SHARED)) {
  1847. ret = -ENXIO;
  1848. IWL_DEBUG_TX_QUEUES(mvm,
  1849. "Can't start tid %d agg on shared queue!\n",
  1850. tid);
  1851. goto release_locks;
  1852. } else if (!iwl_mvm_is_dqa_supported(mvm) ||
  1853. mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
  1854. txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  1855. mvm->first_agg_queue,
  1856. mvm->last_agg_queue);
  1857. if (txq_id < 0) {
  1858. ret = txq_id;
  1859. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  1860. goto release_locks;
  1861. }
  1862. /* TXQ hasn't yet been enabled, so mark it only as reserved */
  1863. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
  1864. }
  1865. spin_unlock(&mvm->queue_info_lock);
  1866. IWL_DEBUG_TX_QUEUES(mvm,
  1867. "AGG for tid %d will be on queue #%d\n",
  1868. tid, txq_id);
  1869. tid_data = &mvmsta->tid_data[tid];
  1870. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  1871. tid_data->txq_id = txq_id;
  1872. *ssn = tid_data->ssn;
  1873. IWL_DEBUG_TX_QUEUES(mvm,
  1874. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  1875. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  1876. tid_data->next_reclaimed);
  1877. if (tid_data->ssn == tid_data->next_reclaimed) {
  1878. tid_data->state = IWL_AGG_STARTING;
  1879. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1880. } else {
  1881. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  1882. }
  1883. ret = 0;
  1884. goto out;
  1885. release_locks:
  1886. spin_unlock(&mvm->queue_info_lock);
  1887. out:
  1888. spin_unlock_bh(&mvmsta->lock);
  1889. return ret;
  1890. }
  1891. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1892. struct ieee80211_sta *sta, u16 tid, u8 buf_size,
  1893. bool amsdu)
  1894. {
  1895. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1896. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  1897. unsigned int wdg_timeout =
  1898. iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
  1899. int queue, ret;
  1900. bool alloc_queue = true;
  1901. enum iwl_mvm_queue_status queue_status;
  1902. u16 ssn;
  1903. struct iwl_trans_txq_scd_cfg cfg = {
  1904. .sta_id = mvmsta->sta_id,
  1905. .tid = tid,
  1906. .frame_limit = buf_size,
  1907. .aggregate = true,
  1908. };
  1909. BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
  1910. != IWL_MAX_TID_COUNT);
  1911. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  1912. spin_lock_bh(&mvmsta->lock);
  1913. ssn = tid_data->ssn;
  1914. queue = tid_data->txq_id;
  1915. tid_data->state = IWL_AGG_ON;
  1916. mvmsta->agg_tids |= BIT(tid);
  1917. tid_data->ssn = 0xffff;
  1918. tid_data->amsdu_in_ampdu_allowed = amsdu;
  1919. spin_unlock_bh(&mvmsta->lock);
  1920. cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  1921. spin_lock_bh(&mvm->queue_info_lock);
  1922. queue_status = mvm->queue_info[queue].status;
  1923. spin_unlock_bh(&mvm->queue_info_lock);
  1924. /* In DQA mode, the existing queue might need to be reconfigured */
  1925. if (iwl_mvm_is_dqa_supported(mvm)) {
  1926. /* Maybe there is no need to even alloc a queue... */
  1927. if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
  1928. alloc_queue = false;
  1929. /*
  1930. * Only reconfig the SCD for the queue if the window size has
  1931. * changed from current (become smaller)
  1932. */
  1933. if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
  1934. /*
  1935. * If reconfiguring an existing queue, it first must be
  1936. * drained
  1937. */
  1938. ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
  1939. BIT(queue));
  1940. if (ret) {
  1941. IWL_ERR(mvm,
  1942. "Error draining queue before reconfig\n");
  1943. return ret;
  1944. }
  1945. ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
  1946. mvmsta->sta_id, tid,
  1947. buf_size, ssn);
  1948. if (ret) {
  1949. IWL_ERR(mvm,
  1950. "Error reconfiguring TXQ #%d\n", queue);
  1951. return ret;
  1952. }
  1953. }
  1954. }
  1955. if (alloc_queue)
  1956. iwl_mvm_enable_txq(mvm, queue,
  1957. vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
  1958. &cfg, wdg_timeout);
  1959. /* Send ADD_STA command to enable aggs only if the queue isn't shared */
  1960. if (queue_status != IWL_MVM_QUEUE_SHARED) {
  1961. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  1962. if (ret)
  1963. return -EIO;
  1964. }
  1965. /* No need to mark as reserved */
  1966. spin_lock_bh(&mvm->queue_info_lock);
  1967. mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
  1968. spin_unlock_bh(&mvm->queue_info_lock);
  1969. /*
  1970. * Even though in theory the peer could have different
  1971. * aggregation reorder buffer sizes for different sessions,
  1972. * our ucode doesn't allow for that and has a global limit
  1973. * for each station. Therefore, use the minimum of all the
  1974. * aggregation sessions and our default value.
  1975. */
  1976. mvmsta->max_agg_bufsize =
  1977. min(mvmsta->max_agg_bufsize, buf_size);
  1978. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  1979. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  1980. sta->addr, tid);
  1981. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  1982. }
  1983. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1984. struct ieee80211_sta *sta, u16 tid)
  1985. {
  1986. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1987. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  1988. u16 txq_id;
  1989. int err;
  1990. /*
  1991. * If mac80211 is cleaning its state, then say that we finished since
  1992. * our state has been cleared anyway.
  1993. */
  1994. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  1995. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1996. return 0;
  1997. }
  1998. spin_lock_bh(&mvmsta->lock);
  1999. txq_id = tid_data->txq_id;
  2000. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  2001. mvmsta->sta_id, tid, txq_id, tid_data->state);
  2002. mvmsta->agg_tids &= ~BIT(tid);
  2003. spin_lock_bh(&mvm->queue_info_lock);
  2004. /*
  2005. * The TXQ is marked as reserved only if no traffic came through yet
  2006. * This means no traffic has been sent on this TID (agg'd or not), so
  2007. * we no longer have use for the queue. Since it hasn't even been
  2008. * allocated through iwl_mvm_enable_txq, so we can just mark it back as
  2009. * free.
  2010. */
  2011. if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
  2012. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
  2013. spin_unlock_bh(&mvm->queue_info_lock);
  2014. switch (tid_data->state) {
  2015. case IWL_AGG_ON:
  2016. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  2017. IWL_DEBUG_TX_QUEUES(mvm,
  2018. "ssn = %d, next_recl = %d\n",
  2019. tid_data->ssn, tid_data->next_reclaimed);
  2020. /* There are still packets for this RA / TID in the HW */
  2021. if (tid_data->ssn != tid_data->next_reclaimed) {
  2022. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  2023. err = 0;
  2024. break;
  2025. }
  2026. tid_data->ssn = 0xffff;
  2027. tid_data->state = IWL_AGG_OFF;
  2028. spin_unlock_bh(&mvmsta->lock);
  2029. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2030. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  2031. if (!iwl_mvm_is_dqa_supported(mvm)) {
  2032. int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
  2033. iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
  2034. }
  2035. return 0;
  2036. case IWL_AGG_STARTING:
  2037. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  2038. /*
  2039. * The agg session has been stopped before it was set up. This
  2040. * can happen when the AddBA timer times out for example.
  2041. */
  2042. /* No barriers since we are under mutex */
  2043. lockdep_assert_held(&mvm->mutex);
  2044. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2045. tid_data->state = IWL_AGG_OFF;
  2046. err = 0;
  2047. break;
  2048. default:
  2049. IWL_ERR(mvm,
  2050. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  2051. mvmsta->sta_id, tid, tid_data->state);
  2052. IWL_ERR(mvm,
  2053. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  2054. err = -EINVAL;
  2055. }
  2056. spin_unlock_bh(&mvmsta->lock);
  2057. return err;
  2058. }
  2059. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  2060. struct ieee80211_sta *sta, u16 tid)
  2061. {
  2062. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2063. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  2064. u16 txq_id;
  2065. enum iwl_mvm_agg_state old_state;
  2066. /*
  2067. * First set the agg state to OFF to avoid calling
  2068. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  2069. */
  2070. spin_lock_bh(&mvmsta->lock);
  2071. txq_id = tid_data->txq_id;
  2072. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  2073. mvmsta->sta_id, tid, txq_id, tid_data->state);
  2074. old_state = tid_data->state;
  2075. tid_data->state = IWL_AGG_OFF;
  2076. mvmsta->agg_tids &= ~BIT(tid);
  2077. spin_unlock_bh(&mvmsta->lock);
  2078. spin_lock_bh(&mvm->queue_info_lock);
  2079. /*
  2080. * The TXQ is marked as reserved only if no traffic came through yet
  2081. * This means no traffic has been sent on this TID (agg'd or not), so
  2082. * we no longer have use for the queue. Since it hasn't even been
  2083. * allocated through iwl_mvm_enable_txq, so we can just mark it back as
  2084. * free.
  2085. */
  2086. if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
  2087. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
  2088. spin_unlock_bh(&mvm->queue_info_lock);
  2089. if (old_state >= IWL_AGG_ON) {
  2090. iwl_mvm_drain_sta(mvm, mvmsta, true);
  2091. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
  2092. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  2093. iwl_trans_wait_tx_queue_empty(mvm->trans,
  2094. mvmsta->tfd_queue_msk);
  2095. iwl_mvm_drain_sta(mvm, mvmsta, false);
  2096. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  2097. if (!iwl_mvm_is_dqa_supported(mvm)) {
  2098. int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
  2099. iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
  2100. tid, 0);
  2101. }
  2102. }
  2103. return 0;
  2104. }
  2105. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  2106. {
  2107. int i, max = -1, max_offs = -1;
  2108. lockdep_assert_held(&mvm->mutex);
  2109. /* Pick the unused key offset with the highest 'deleted'
  2110. * counter. Every time a key is deleted, all the counters
  2111. * are incremented and the one that was just deleted is
  2112. * reset to zero. Thus, the highest counter is the one
  2113. * that was deleted longest ago. Pick that one.
  2114. */
  2115. for (i = 0; i < STA_KEY_MAX_NUM; i++) {
  2116. if (test_bit(i, mvm->fw_key_table))
  2117. continue;
  2118. if (mvm->fw_key_deleted[i] > max) {
  2119. max = mvm->fw_key_deleted[i];
  2120. max_offs = i;
  2121. }
  2122. }
  2123. if (max_offs < 0)
  2124. return STA_KEY_IDX_INVALID;
  2125. return max_offs;
  2126. }
  2127. static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
  2128. struct ieee80211_vif *vif,
  2129. struct ieee80211_sta *sta)
  2130. {
  2131. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2132. if (sta)
  2133. return iwl_mvm_sta_from_mac80211(sta);
  2134. /*
  2135. * The device expects GTKs for station interfaces to be
  2136. * installed as GTKs for the AP station. If we have no
  2137. * station ID, then use AP's station ID.
  2138. */
  2139. if (vif->type == NL80211_IFTYPE_STATION &&
  2140. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  2141. u8 sta_id = mvmvif->ap_sta_id;
  2142. sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
  2143. lockdep_is_held(&mvm->mutex));
  2144. /*
  2145. * It is possible that the 'sta' parameter is NULL,
  2146. * for example when a GTK is removed - the sta_id will then
  2147. * be the AP ID, and no station was passed by mac80211.
  2148. */
  2149. if (IS_ERR_OR_NULL(sta))
  2150. return NULL;
  2151. return iwl_mvm_sta_from_mac80211(sta);
  2152. }
  2153. return NULL;
  2154. }
  2155. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  2156. struct iwl_mvm_sta *mvm_sta,
  2157. struct ieee80211_key_conf *keyconf, bool mcast,
  2158. u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
  2159. u8 key_offset)
  2160. {
  2161. struct iwl_mvm_add_sta_key_cmd cmd = {};
  2162. __le16 key_flags;
  2163. int ret;
  2164. u32 status;
  2165. u16 keyidx;
  2166. int i;
  2167. u8 sta_id = mvm_sta->sta_id;
  2168. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  2169. STA_KEY_FLG_KEYID_MSK;
  2170. key_flags = cpu_to_le16(keyidx);
  2171. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  2172. switch (keyconf->cipher) {
  2173. case WLAN_CIPHER_SUITE_TKIP:
  2174. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  2175. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  2176. for (i = 0; i < 5; i++)
  2177. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  2178. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  2179. break;
  2180. case WLAN_CIPHER_SUITE_CCMP:
  2181. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  2182. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  2183. break;
  2184. case WLAN_CIPHER_SUITE_WEP104:
  2185. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
  2186. /* fall through */
  2187. case WLAN_CIPHER_SUITE_WEP40:
  2188. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
  2189. memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
  2190. break;
  2191. case WLAN_CIPHER_SUITE_GCMP_256:
  2192. key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
  2193. /* fall through */
  2194. case WLAN_CIPHER_SUITE_GCMP:
  2195. key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
  2196. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  2197. break;
  2198. default:
  2199. key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
  2200. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  2201. }
  2202. if (mcast)
  2203. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  2204. cmd.key_offset = key_offset;
  2205. cmd.key_flags = key_flags;
  2206. cmd.sta_id = sta_id;
  2207. status = ADD_STA_SUCCESS;
  2208. if (cmd_flags & CMD_ASYNC)
  2209. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
  2210. sizeof(cmd), &cmd);
  2211. else
  2212. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  2213. &cmd, &status);
  2214. switch (status) {
  2215. case ADD_STA_SUCCESS:
  2216. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  2217. break;
  2218. default:
  2219. ret = -EIO;
  2220. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  2221. break;
  2222. }
  2223. return ret;
  2224. }
  2225. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  2226. struct ieee80211_key_conf *keyconf,
  2227. u8 sta_id, bool remove_key)
  2228. {
  2229. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  2230. /* verify the key details match the required command's expectations */
  2231. if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  2232. (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
  2233. (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
  2234. keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
  2235. keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
  2236. return -EINVAL;
  2237. if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
  2238. keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
  2239. return -EINVAL;
  2240. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  2241. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  2242. if (remove_key) {
  2243. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  2244. } else {
  2245. struct ieee80211_key_seq seq;
  2246. const u8 *pn;
  2247. switch (keyconf->cipher) {
  2248. case WLAN_CIPHER_SUITE_AES_CMAC:
  2249. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
  2250. break;
  2251. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  2252. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  2253. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
  2254. break;
  2255. default:
  2256. return -EINVAL;
  2257. }
  2258. memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
  2259. if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
  2260. igtk_cmd.ctrl_flags |=
  2261. cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
  2262. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  2263. pn = seq.aes_cmac.pn;
  2264. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  2265. ((u64) pn[4] << 8) |
  2266. ((u64) pn[3] << 16) |
  2267. ((u64) pn[2] << 24) |
  2268. ((u64) pn[1] << 32) |
  2269. ((u64) pn[0] << 40));
  2270. }
  2271. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  2272. remove_key ? "removing" : "installing",
  2273. igtk_cmd.sta_id);
  2274. if (!iwl_mvm_has_new_rx_api(mvm)) {
  2275. struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
  2276. .ctrl_flags = igtk_cmd.ctrl_flags,
  2277. .key_id = igtk_cmd.key_id,
  2278. .sta_id = igtk_cmd.sta_id,
  2279. .receive_seq_cnt = igtk_cmd.receive_seq_cnt
  2280. };
  2281. memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
  2282. ARRAY_SIZE(igtk_cmd_v1.igtk));
  2283. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
  2284. sizeof(igtk_cmd_v1), &igtk_cmd_v1);
  2285. }
  2286. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
  2287. sizeof(igtk_cmd), &igtk_cmd);
  2288. }
  2289. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  2290. struct ieee80211_vif *vif,
  2291. struct ieee80211_sta *sta)
  2292. {
  2293. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2294. if (sta)
  2295. return sta->addr;
  2296. if (vif->type == NL80211_IFTYPE_STATION &&
  2297. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  2298. u8 sta_id = mvmvif->ap_sta_id;
  2299. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  2300. lockdep_is_held(&mvm->mutex));
  2301. return sta->addr;
  2302. }
  2303. return NULL;
  2304. }
  2305. static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  2306. struct ieee80211_vif *vif,
  2307. struct ieee80211_sta *sta,
  2308. struct ieee80211_key_conf *keyconf,
  2309. u8 key_offset,
  2310. bool mcast)
  2311. {
  2312. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2313. int ret;
  2314. const u8 *addr;
  2315. struct ieee80211_key_seq seq;
  2316. u16 p1k[5];
  2317. switch (keyconf->cipher) {
  2318. case WLAN_CIPHER_SUITE_TKIP:
  2319. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  2320. /* get phase 1 key from mac80211 */
  2321. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  2322. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  2323. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2324. seq.tkip.iv32, p1k, 0, key_offset);
  2325. break;
  2326. case WLAN_CIPHER_SUITE_CCMP:
  2327. case WLAN_CIPHER_SUITE_WEP40:
  2328. case WLAN_CIPHER_SUITE_WEP104:
  2329. case WLAN_CIPHER_SUITE_GCMP:
  2330. case WLAN_CIPHER_SUITE_GCMP_256:
  2331. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2332. 0, NULL, 0, key_offset);
  2333. break;
  2334. default:
  2335. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2336. 0, NULL, 0, key_offset);
  2337. }
  2338. return ret;
  2339. }
  2340. static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
  2341. struct ieee80211_key_conf *keyconf,
  2342. bool mcast)
  2343. {
  2344. struct iwl_mvm_add_sta_key_cmd cmd = {};
  2345. __le16 key_flags;
  2346. int ret;
  2347. u32 status;
  2348. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  2349. STA_KEY_FLG_KEYID_MSK);
  2350. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  2351. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  2352. if (mcast)
  2353. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  2354. cmd.key_flags = key_flags;
  2355. cmd.key_offset = keyconf->hw_key_idx;
  2356. cmd.sta_id = sta_id;
  2357. status = ADD_STA_SUCCESS;
  2358. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  2359. &cmd, &status);
  2360. switch (status) {
  2361. case ADD_STA_SUCCESS:
  2362. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  2363. break;
  2364. default:
  2365. ret = -EIO;
  2366. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  2367. break;
  2368. }
  2369. return ret;
  2370. }
  2371. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  2372. struct ieee80211_vif *vif,
  2373. struct ieee80211_sta *sta,
  2374. struct ieee80211_key_conf *keyconf,
  2375. u8 key_offset)
  2376. {
  2377. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  2378. struct iwl_mvm_sta *mvm_sta;
  2379. u8 sta_id;
  2380. int ret;
  2381. static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
  2382. lockdep_assert_held(&mvm->mutex);
  2383. /* Get the station id from the mvm local station table */
  2384. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  2385. if (!mvm_sta) {
  2386. IWL_ERR(mvm, "Failed to find station\n");
  2387. return -EINVAL;
  2388. }
  2389. sta_id = mvm_sta->sta_id;
  2390. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
  2391. keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
  2392. keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
  2393. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  2394. goto end;
  2395. }
  2396. /*
  2397. * It is possible that the 'sta' parameter is NULL, and thus
  2398. * there is a need to retrieve the sta from the local station table.
  2399. */
  2400. if (!sta) {
  2401. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  2402. lockdep_is_held(&mvm->mutex));
  2403. if (IS_ERR_OR_NULL(sta)) {
  2404. IWL_ERR(mvm, "Invalid station id\n");
  2405. return -EINVAL;
  2406. }
  2407. }
  2408. if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
  2409. return -EINVAL;
  2410. /* If the key_offset is not pre-assigned, we need to find a
  2411. * new offset to use. In normal cases, the offset is not
  2412. * pre-assigned, but during HW_RESTART we want to reuse the
  2413. * same indices, so we pass them when this function is called.
  2414. *
  2415. * In D3 entry, we need to hardcoded the indices (because the
  2416. * firmware hardcodes the PTK offset to 0). In this case, we
  2417. * need to make sure we don't overwrite the hw_key_idx in the
  2418. * keyconf structure, because otherwise we cannot configure
  2419. * the original ones back when resuming.
  2420. */
  2421. if (key_offset == STA_KEY_IDX_INVALID) {
  2422. key_offset = iwl_mvm_set_fw_key_idx(mvm);
  2423. if (key_offset == STA_KEY_IDX_INVALID)
  2424. return -ENOSPC;
  2425. keyconf->hw_key_idx = key_offset;
  2426. }
  2427. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
  2428. if (ret)
  2429. goto end;
  2430. /*
  2431. * For WEP, the same key is used for multicast and unicast. Upload it
  2432. * again, using the same key offset, and now pointing the other one
  2433. * to the same key slot (offset).
  2434. * If this fails, remove the original as well.
  2435. */
  2436. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2437. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
  2438. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
  2439. key_offset, !mcast);
  2440. if (ret) {
  2441. __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  2442. goto end;
  2443. }
  2444. }
  2445. __set_bit(key_offset, mvm->fw_key_table);
  2446. end:
  2447. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  2448. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  2449. sta ? sta->addr : zero_addr, ret);
  2450. return ret;
  2451. }
  2452. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  2453. struct ieee80211_vif *vif,
  2454. struct ieee80211_sta *sta,
  2455. struct ieee80211_key_conf *keyconf)
  2456. {
  2457. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  2458. struct iwl_mvm_sta *mvm_sta;
  2459. u8 sta_id = IWL_MVM_STATION_COUNT;
  2460. int ret, i;
  2461. lockdep_assert_held(&mvm->mutex);
  2462. /* Get the station from the mvm local station table */
  2463. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  2464. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  2465. keyconf->keyidx, sta_id);
  2466. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
  2467. keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
  2468. keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
  2469. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  2470. if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
  2471. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  2472. keyconf->hw_key_idx);
  2473. return -ENOENT;
  2474. }
  2475. /* track which key was deleted last */
  2476. for (i = 0; i < STA_KEY_MAX_NUM; i++) {
  2477. if (mvm->fw_key_deleted[i] < U8_MAX)
  2478. mvm->fw_key_deleted[i]++;
  2479. }
  2480. mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
  2481. if (!mvm_sta) {
  2482. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  2483. return 0;
  2484. }
  2485. sta_id = mvm_sta->sta_id;
  2486. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  2487. if (ret)
  2488. return ret;
  2489. /* delete WEP key twice to get rid of (now useless) offset */
  2490. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2491. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
  2492. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
  2493. return ret;
  2494. }
  2495. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  2496. struct ieee80211_vif *vif,
  2497. struct ieee80211_key_conf *keyconf,
  2498. struct ieee80211_sta *sta, u32 iv32,
  2499. u16 *phase1key)
  2500. {
  2501. struct iwl_mvm_sta *mvm_sta;
  2502. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  2503. rcu_read_lock();
  2504. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  2505. if (WARN_ON_ONCE(!mvm_sta))
  2506. goto unlock;
  2507. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2508. iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
  2509. unlock:
  2510. rcu_read_unlock();
  2511. }
  2512. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  2513. struct ieee80211_sta *sta)
  2514. {
  2515. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2516. struct iwl_mvm_add_sta_cmd cmd = {
  2517. .add_modify = STA_MODE_MODIFY,
  2518. .sta_id = mvmsta->sta_id,
  2519. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  2520. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  2521. };
  2522. int ret;
  2523. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  2524. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  2525. if (ret)
  2526. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  2527. }
  2528. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  2529. struct ieee80211_sta *sta,
  2530. enum ieee80211_frame_release_type reason,
  2531. u16 cnt, u16 tids, bool more_data,
  2532. bool agg)
  2533. {
  2534. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2535. struct iwl_mvm_add_sta_cmd cmd = {
  2536. .add_modify = STA_MODE_MODIFY,
  2537. .sta_id = mvmsta->sta_id,
  2538. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  2539. .sleep_tx_count = cpu_to_le16(cnt),
  2540. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  2541. };
  2542. int tid, ret;
  2543. unsigned long _tids = tids;
  2544. /* convert TIDs to ACs - we don't support TSPEC so that's OK
  2545. * Note that this field is reserved and unused by firmware not
  2546. * supporting GO uAPSD, so it's safe to always do this.
  2547. */
  2548. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
  2549. cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
  2550. /* If we're releasing frames from aggregation queues then check if the
  2551. * all queues combined that we're releasing frames from have
  2552. * - more frames than the service period, in which case more_data
  2553. * needs to be set
  2554. * - fewer than 'cnt' frames, in which case we need to adjust the
  2555. * firmware command (but do that unconditionally)
  2556. */
  2557. if (agg) {
  2558. int remaining = cnt;
  2559. int sleep_tx_count;
  2560. spin_lock_bh(&mvmsta->lock);
  2561. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
  2562. struct iwl_mvm_tid_data *tid_data;
  2563. u16 n_queued;
  2564. tid_data = &mvmsta->tid_data[tid];
  2565. if (WARN(tid_data->state != IWL_AGG_ON &&
  2566. tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
  2567. "TID %d state is %d\n",
  2568. tid, tid_data->state)) {
  2569. spin_unlock_bh(&mvmsta->lock);
  2570. ieee80211_sta_eosp(sta);
  2571. return;
  2572. }
  2573. n_queued = iwl_mvm_tid_queued(tid_data);
  2574. if (n_queued > remaining) {
  2575. more_data = true;
  2576. remaining = 0;
  2577. break;
  2578. }
  2579. remaining -= n_queued;
  2580. }
  2581. sleep_tx_count = cnt - remaining;
  2582. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  2583. mvmsta->sleep_tx_count = sleep_tx_count;
  2584. spin_unlock_bh(&mvmsta->lock);
  2585. cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
  2586. if (WARN_ON(cnt - remaining == 0)) {
  2587. ieee80211_sta_eosp(sta);
  2588. return;
  2589. }
  2590. }
  2591. /* Note: this is ignored by firmware not supporting GO uAPSD */
  2592. if (more_data)
  2593. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
  2594. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
  2595. mvmsta->next_status_eosp = true;
  2596. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
  2597. } else {
  2598. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
  2599. }
  2600. /* block the Tx queues until the FW updated the sleep Tx count */
  2601. iwl_trans_block_txq_ptrs(mvm->trans, true);
  2602. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
  2603. CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
  2604. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  2605. if (ret)
  2606. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  2607. }
  2608. void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
  2609. struct iwl_rx_cmd_buffer *rxb)
  2610. {
  2611. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  2612. struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
  2613. struct ieee80211_sta *sta;
  2614. u32 sta_id = le32_to_cpu(notif->sta_id);
  2615. if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
  2616. return;
  2617. rcu_read_lock();
  2618. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  2619. if (!IS_ERR_OR_NULL(sta))
  2620. ieee80211_sta_eosp(sta);
  2621. rcu_read_unlock();
  2622. }
  2623. void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
  2624. struct iwl_mvm_sta *mvmsta, bool disable)
  2625. {
  2626. struct iwl_mvm_add_sta_cmd cmd = {
  2627. .add_modify = STA_MODE_MODIFY,
  2628. .sta_id = mvmsta->sta_id,
  2629. .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
  2630. .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
  2631. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  2632. };
  2633. int ret;
  2634. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  2635. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  2636. if (ret)
  2637. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  2638. }
  2639. void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
  2640. struct ieee80211_sta *sta,
  2641. bool disable)
  2642. {
  2643. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2644. spin_lock_bh(&mvm_sta->lock);
  2645. if (mvm_sta->disable_tx == disable) {
  2646. spin_unlock_bh(&mvm_sta->lock);
  2647. return;
  2648. }
  2649. mvm_sta->disable_tx = disable;
  2650. /*
  2651. * Tell mac80211 to start/stop queuing tx for this station,
  2652. * but don't stop queuing if there are still pending frames
  2653. * for this station.
  2654. */
  2655. if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
  2656. ieee80211_sta_block_awake(mvm->hw, sta, disable);
  2657. iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
  2658. spin_unlock_bh(&mvm_sta->lock);
  2659. }
  2660. void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
  2661. struct iwl_mvm_vif *mvmvif,
  2662. bool disable)
  2663. {
  2664. struct ieee80211_sta *sta;
  2665. struct iwl_mvm_sta *mvm_sta;
  2666. int i;
  2667. lockdep_assert_held(&mvm->mutex);
  2668. /* Block/unblock all the stations of the given mvmvif */
  2669. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  2670. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  2671. lockdep_is_held(&mvm->mutex));
  2672. if (IS_ERR_OR_NULL(sta))
  2673. continue;
  2674. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2675. if (mvm_sta->mac_id_n_color !=
  2676. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
  2677. continue;
  2678. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
  2679. }
  2680. }
  2681. void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  2682. {
  2683. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2684. struct iwl_mvm_sta *mvmsta;
  2685. rcu_read_lock();
  2686. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
  2687. if (!WARN_ON(!mvmsta))
  2688. iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
  2689. rcu_read_unlock();
  2690. }