sta.c 99 KB

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