sta.c 99 KB

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