sta.c 91 KB

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