sta.c 102 KB

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