sta.c 102 KB

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