sta.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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)
  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) {
  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. }
  123. switch (sta->bandwidth) {
  124. case IEEE80211_STA_RX_BW_160:
  125. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
  126. /* fall through */
  127. case IEEE80211_STA_RX_BW_80:
  128. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
  129. /* fall through */
  130. case IEEE80211_STA_RX_BW_40:
  131. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
  132. /* fall through */
  133. case IEEE80211_STA_RX_BW_20:
  134. if (sta->ht_cap.ht_supported)
  135. add_sta_cmd.station_flags |=
  136. cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
  137. break;
  138. }
  139. switch (sta->rx_nss) {
  140. case 1:
  141. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  142. break;
  143. case 2:
  144. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
  145. break;
  146. case 3 ... 8:
  147. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
  148. break;
  149. }
  150. switch (sta->smps_mode) {
  151. case IEEE80211_SMPS_AUTOMATIC:
  152. case IEEE80211_SMPS_NUM_MODES:
  153. WARN_ON(1);
  154. break;
  155. case IEEE80211_SMPS_STATIC:
  156. /* override NSS */
  157. add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
  158. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  159. break;
  160. case IEEE80211_SMPS_DYNAMIC:
  161. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
  162. break;
  163. case IEEE80211_SMPS_OFF:
  164. /* nothing */
  165. break;
  166. }
  167. if (sta->ht_cap.ht_supported) {
  168. add_sta_cmd.station_flags_msk |=
  169. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  170. STA_FLG_AGG_MPDU_DENS_MSK);
  171. mpdu_dens = sta->ht_cap.ampdu_density;
  172. }
  173. if (sta->vht_cap.vht_supported) {
  174. agg_size = sta->vht_cap.cap &
  175. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  176. agg_size >>=
  177. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  178. } else if (sta->ht_cap.ht_supported) {
  179. agg_size = sta->ht_cap.ampdu_factor;
  180. }
  181. add_sta_cmd.station_flags |=
  182. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  183. add_sta_cmd.station_flags |=
  184. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  185. status = ADD_STA_SUCCESS;
  186. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  187. iwl_mvm_add_sta_cmd_size(mvm),
  188. &add_sta_cmd, &status);
  189. if (ret)
  190. return ret;
  191. switch (status & IWL_ADD_STA_STATUS_MASK) {
  192. case ADD_STA_SUCCESS:
  193. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  194. break;
  195. default:
  196. ret = -EIO;
  197. IWL_ERR(mvm, "ADD_STA failed\n");
  198. break;
  199. }
  200. return ret;
  201. }
  202. static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
  203. struct ieee80211_sta *sta)
  204. {
  205. unsigned long used_hw_queues;
  206. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  207. unsigned int wdg_timeout =
  208. iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
  209. u32 ac;
  210. lockdep_assert_held(&mvm->mutex);
  211. used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
  212. /* Find available queues, and allocate them to the ACs */
  213. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  214. u8 queue = find_first_zero_bit(&used_hw_queues,
  215. mvm->first_agg_queue);
  216. if (queue >= mvm->first_agg_queue) {
  217. IWL_ERR(mvm, "Failed to allocate STA queue\n");
  218. return -EBUSY;
  219. }
  220. __set_bit(queue, &used_hw_queues);
  221. mvmsta->hw_queue[ac] = queue;
  222. }
  223. /* Found a place for all queues - enable them */
  224. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  225. iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
  226. mvmsta->hw_queue[ac],
  227. iwl_mvm_ac_to_tx_fifo[ac], 0,
  228. wdg_timeout);
  229. mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
  230. }
  231. return 0;
  232. }
  233. static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
  234. struct ieee80211_sta *sta)
  235. {
  236. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  237. unsigned long sta_msk;
  238. int i;
  239. lockdep_assert_held(&mvm->mutex);
  240. /* disable the TDLS STA-specific queues */
  241. sta_msk = mvmsta->tfd_queue_msk;
  242. for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
  243. iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
  244. }
  245. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  246. struct ieee80211_vif *vif,
  247. struct ieee80211_sta *sta)
  248. {
  249. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  250. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  251. struct iwl_mvm_rxq_dup_data *dup_data;
  252. int i, ret, sta_id;
  253. lockdep_assert_held(&mvm->mutex);
  254. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  255. sta_id = iwl_mvm_find_free_sta_id(mvm,
  256. ieee80211_vif_type_p2p(vif));
  257. else
  258. sta_id = mvm_sta->sta_id;
  259. if (sta_id == IWL_MVM_STATION_COUNT)
  260. return -ENOSPC;
  261. spin_lock_init(&mvm_sta->lock);
  262. mvm_sta->sta_id = sta_id;
  263. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  264. mvmvif->color);
  265. mvm_sta->vif = vif;
  266. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  267. mvm_sta->tx_protection = 0;
  268. mvm_sta->tt_tx_protection = false;
  269. /* HW restart, don't assume the memory has been zeroed */
  270. atomic_set(&mvm->pending_frames[sta_id], 0);
  271. mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
  272. mvm_sta->tfd_queue_msk = 0;
  273. /* allocate new queues for a TDLS station */
  274. if (sta->tdls) {
  275. ret = iwl_mvm_tdls_sta_init(mvm, sta);
  276. if (ret)
  277. return ret;
  278. } else {
  279. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  280. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  281. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  282. }
  283. /* for HW restart - reset everything but the sequence number */
  284. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  285. u16 seq = mvm_sta->tid_data[i].seq_number;
  286. memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
  287. mvm_sta->tid_data[i].seq_number = seq;
  288. }
  289. mvm_sta->agg_tids = 0;
  290. if (iwl_mvm_has_new_rx_api(mvm) &&
  291. !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  292. dup_data = kcalloc(mvm->trans->num_rx_queues,
  293. sizeof(*dup_data),
  294. GFP_KERNEL);
  295. if (!dup_data)
  296. return -ENOMEM;
  297. mvm_sta->dup_data = dup_data;
  298. }
  299. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
  300. if (ret)
  301. goto err;
  302. if (vif->type == NL80211_IFTYPE_STATION) {
  303. if (!sta->tdls) {
  304. WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
  305. mvmvif->ap_sta_id = sta_id;
  306. } else {
  307. WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
  308. }
  309. }
  310. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  311. return 0;
  312. err:
  313. iwl_mvm_tdls_sta_deinit(mvm, sta);
  314. return ret;
  315. }
  316. int iwl_mvm_update_sta(struct iwl_mvm *mvm,
  317. struct ieee80211_vif *vif,
  318. struct ieee80211_sta *sta)
  319. {
  320. return iwl_mvm_sta_send_to_fw(mvm, sta, true);
  321. }
  322. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  323. bool drain)
  324. {
  325. struct iwl_mvm_add_sta_cmd cmd = {};
  326. int ret;
  327. u32 status;
  328. lockdep_assert_held(&mvm->mutex);
  329. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  330. cmd.sta_id = mvmsta->sta_id;
  331. cmd.add_modify = STA_MODE_MODIFY;
  332. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  333. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  334. status = ADD_STA_SUCCESS;
  335. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  336. iwl_mvm_add_sta_cmd_size(mvm),
  337. &cmd, &status);
  338. if (ret)
  339. return ret;
  340. switch (status & IWL_ADD_STA_STATUS_MASK) {
  341. case ADD_STA_SUCCESS:
  342. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  343. mvmsta->sta_id);
  344. break;
  345. default:
  346. ret = -EIO;
  347. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  348. mvmsta->sta_id);
  349. break;
  350. }
  351. return ret;
  352. }
  353. /*
  354. * Remove a station from the FW table. Before sending the command to remove
  355. * the station validate that the station is indeed known to the driver (sanity
  356. * only).
  357. */
  358. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  359. {
  360. struct ieee80211_sta *sta;
  361. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  362. .sta_id = sta_id,
  363. };
  364. int ret;
  365. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  366. lockdep_is_held(&mvm->mutex));
  367. /* Note: internal stations are marked as error values */
  368. if (!sta) {
  369. IWL_ERR(mvm, "Invalid station id\n");
  370. return -EINVAL;
  371. }
  372. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
  373. sizeof(rm_sta_cmd), &rm_sta_cmd);
  374. if (ret) {
  375. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  376. return ret;
  377. }
  378. return 0;
  379. }
  380. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  381. {
  382. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  383. u8 sta_id;
  384. /*
  385. * The mutex is needed because of the SYNC cmd, but not only: if the
  386. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  387. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  388. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  389. * that later.
  390. */
  391. mutex_lock(&mvm->mutex);
  392. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  393. int ret;
  394. struct ieee80211_sta *sta =
  395. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  396. lockdep_is_held(&mvm->mutex));
  397. /*
  398. * This station is in use or RCU-removed; the latter happens in
  399. * managed mode, where mac80211 removes the station before we
  400. * can remove it from firmware (we can only do that after the
  401. * MAC is marked unassociated), and possibly while the deauth
  402. * frame to disconnect from the AP is still queued. Then, the
  403. * station pointer is -ENOENT when the last skb is reclaimed.
  404. */
  405. if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
  406. continue;
  407. if (PTR_ERR(sta) == -EINVAL) {
  408. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  409. sta_id);
  410. continue;
  411. }
  412. if (!sta) {
  413. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  414. sta_id);
  415. continue;
  416. }
  417. WARN_ON(PTR_ERR(sta) != -EBUSY);
  418. /* This station was removed and we waited until it got drained,
  419. * we can now proceed and remove it.
  420. */
  421. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  422. if (ret) {
  423. IWL_ERR(mvm,
  424. "Couldn't remove sta %d after it was drained\n",
  425. sta_id);
  426. continue;
  427. }
  428. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  429. clear_bit(sta_id, mvm->sta_drained);
  430. if (mvm->tfd_drained[sta_id]) {
  431. unsigned long i, msk = mvm->tfd_drained[sta_id];
  432. for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
  433. iwl_mvm_disable_txq(mvm, i, i,
  434. IWL_MAX_TID_COUNT, 0);
  435. mvm->tfd_drained[sta_id] = 0;
  436. IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
  437. sta_id, msk);
  438. }
  439. }
  440. mutex_unlock(&mvm->mutex);
  441. }
  442. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  443. struct ieee80211_vif *vif,
  444. struct ieee80211_sta *sta)
  445. {
  446. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  447. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  448. int ret;
  449. lockdep_assert_held(&mvm->mutex);
  450. if (iwl_mvm_has_new_rx_api(mvm))
  451. kfree(mvm_sta->dup_data);
  452. if (vif->type == NL80211_IFTYPE_STATION &&
  453. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  454. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  455. if (ret)
  456. return ret;
  457. /* flush its queues here since we are freeing mvm_sta */
  458. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
  459. if (ret)
  460. return ret;
  461. ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
  462. mvm_sta->tfd_queue_msk);
  463. if (ret)
  464. return ret;
  465. ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
  466. /* if we are associated - we can't remove the AP STA now */
  467. if (vif->bss_conf.assoc)
  468. return ret;
  469. /* unassoc - go ahead - remove the AP STA now */
  470. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  471. /* clear d0i3_ap_sta_id if no longer relevant */
  472. if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
  473. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  474. }
  475. /*
  476. * This shouldn't happen - the TDLS channel switch should be canceled
  477. * before the STA is removed.
  478. */
  479. if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
  480. mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
  481. cancel_delayed_work(&mvm->tdls_cs.dwork);
  482. }
  483. /*
  484. * Make sure that the tx response code sees the station as -EBUSY and
  485. * calls the drain worker.
  486. */
  487. spin_lock_bh(&mvm_sta->lock);
  488. /*
  489. * There are frames pending on the AC queues for this station.
  490. * We need to wait until all the frames are drained...
  491. */
  492. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  493. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  494. ERR_PTR(-EBUSY));
  495. spin_unlock_bh(&mvm_sta->lock);
  496. /* disable TDLS sta queues on drain complete */
  497. if (sta->tdls) {
  498. mvm->tfd_drained[mvm_sta->sta_id] =
  499. mvm_sta->tfd_queue_msk;
  500. IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
  501. mvm_sta->sta_id);
  502. }
  503. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  504. } else {
  505. spin_unlock_bh(&mvm_sta->lock);
  506. if (sta->tdls)
  507. iwl_mvm_tdls_sta_deinit(mvm, sta);
  508. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  509. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  510. }
  511. return ret;
  512. }
  513. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  514. struct ieee80211_vif *vif,
  515. u8 sta_id)
  516. {
  517. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  518. lockdep_assert_held(&mvm->mutex);
  519. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  520. return ret;
  521. }
  522. int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
  523. struct iwl_mvm_int_sta *sta,
  524. u32 qmask, enum nl80211_iftype iftype)
  525. {
  526. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  527. sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
  528. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  529. return -ENOSPC;
  530. }
  531. sta->tfd_queue_msk = qmask;
  532. /* put a non-NULL value so iterating over the stations won't stop */
  533. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  534. return 0;
  535. }
  536. static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
  537. struct iwl_mvm_int_sta *sta)
  538. {
  539. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  540. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  541. sta->sta_id = IWL_MVM_STATION_COUNT;
  542. }
  543. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  544. struct iwl_mvm_int_sta *sta,
  545. const u8 *addr,
  546. u16 mac_id, u16 color)
  547. {
  548. struct iwl_mvm_add_sta_cmd cmd;
  549. int ret;
  550. u32 status;
  551. lockdep_assert_held(&mvm->mutex);
  552. memset(&cmd, 0, sizeof(cmd));
  553. cmd.sta_id = sta->sta_id;
  554. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  555. color));
  556. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  557. cmd.tid_disable_tx = cpu_to_le16(0xffff);
  558. if (addr)
  559. memcpy(cmd.addr, addr, ETH_ALEN);
  560. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  561. iwl_mvm_add_sta_cmd_size(mvm),
  562. &cmd, &status);
  563. if (ret)
  564. return ret;
  565. switch (status & IWL_ADD_STA_STATUS_MASK) {
  566. case ADD_STA_SUCCESS:
  567. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  568. return 0;
  569. default:
  570. ret = -EIO;
  571. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  572. status);
  573. break;
  574. }
  575. return ret;
  576. }
  577. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  578. {
  579. unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
  580. mvm->cfg->base_params->wd_timeout :
  581. IWL_WATCHDOG_DISABLED;
  582. int ret;
  583. lockdep_assert_held(&mvm->mutex);
  584. /* Map Aux queue to fifo - needs to happen before adding Aux station */
  585. iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
  586. IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
  587. /* Allocate aux station and assign to it the aux queue */
  588. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
  589. NL80211_IFTYPE_UNSPECIFIED);
  590. if (ret)
  591. return ret;
  592. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  593. MAC_INDEX_AUX, 0);
  594. if (ret)
  595. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  596. return ret;
  597. }
  598. int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  599. {
  600. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  601. lockdep_assert_held(&mvm->mutex);
  602. return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
  603. mvmvif->id, 0);
  604. }
  605. int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  606. {
  607. int ret;
  608. lockdep_assert_held(&mvm->mutex);
  609. ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
  610. if (ret)
  611. IWL_WARN(mvm, "Failed sending remove station\n");
  612. return ret;
  613. }
  614. void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
  615. {
  616. iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
  617. }
  618. void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
  619. {
  620. lockdep_assert_held(&mvm->mutex);
  621. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  622. }
  623. /*
  624. * Send the add station command for the vif's broadcast station.
  625. * Assumes that the station was already allocated.
  626. *
  627. * @mvm: the mvm component
  628. * @vif: the interface to which the broadcast station is added
  629. * @bsta: the broadcast station to add.
  630. */
  631. int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  632. {
  633. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  634. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  635. static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  636. const u8 *baddr = _baddr;
  637. lockdep_assert_held(&mvm->mutex);
  638. if (vif->type == NL80211_IFTYPE_ADHOC)
  639. baddr = vif->bss_conf.bssid;
  640. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  641. return -ENOSPC;
  642. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  643. mvmvif->id, mvmvif->color);
  644. }
  645. /* Send the FW a request to remove the station from it's internal data
  646. * structures, but DO NOT remove the entry from the local data structures. */
  647. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  648. {
  649. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  650. int ret;
  651. lockdep_assert_held(&mvm->mutex);
  652. ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
  653. if (ret)
  654. IWL_WARN(mvm, "Failed sending remove station\n");
  655. return ret;
  656. }
  657. int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  658. {
  659. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  660. u32 qmask;
  661. lockdep_assert_held(&mvm->mutex);
  662. qmask = iwl_mvm_mac_get_queues_mask(vif);
  663. /*
  664. * The firmware defines the TFD queue mask to only be relevant
  665. * for *unicast* queues, so the multicast (CAB) queue shouldn't
  666. * be included.
  667. */
  668. if (vif->type == NL80211_IFTYPE_AP)
  669. qmask &= ~BIT(vif->cab_queue);
  670. return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
  671. ieee80211_vif_type_p2p(vif));
  672. }
  673. /* Allocate a new station entry for the broadcast station to the given vif,
  674. * and send it to the FW.
  675. * Note that each P2P mac should have its own broadcast station.
  676. *
  677. * @mvm: the mvm component
  678. * @vif: the interface to which the broadcast station is added
  679. * @bsta: the broadcast station to add. */
  680. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  681. {
  682. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  683. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  684. int ret;
  685. lockdep_assert_held(&mvm->mutex);
  686. ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
  687. if (ret)
  688. return ret;
  689. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  690. if (ret)
  691. iwl_mvm_dealloc_int_sta(mvm, bsta);
  692. return ret;
  693. }
  694. void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  695. {
  696. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  697. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  698. }
  699. /*
  700. * Send the FW a request to remove the station from it's internal data
  701. * structures, and in addition remove it from the local data structure.
  702. */
  703. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  704. {
  705. int ret;
  706. lockdep_assert_held(&mvm->mutex);
  707. ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
  708. iwl_mvm_dealloc_bcast_sta(mvm, vif);
  709. return ret;
  710. }
  711. #define IWL_MAX_RX_BA_SESSIONS 16
  712. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  713. int tid, u16 ssn, bool start, u8 buf_size)
  714. {
  715. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  716. struct iwl_mvm_add_sta_cmd cmd = {};
  717. int ret;
  718. u32 status;
  719. lockdep_assert_held(&mvm->mutex);
  720. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  721. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  722. return -ENOSPC;
  723. }
  724. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  725. cmd.sta_id = mvm_sta->sta_id;
  726. cmd.add_modify = STA_MODE_MODIFY;
  727. if (start) {
  728. cmd.add_immediate_ba_tid = (u8) tid;
  729. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  730. cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
  731. } else {
  732. cmd.remove_immediate_ba_tid = (u8) tid;
  733. }
  734. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  735. STA_MODIFY_REMOVE_BA_TID;
  736. status = ADD_STA_SUCCESS;
  737. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  738. iwl_mvm_add_sta_cmd_size(mvm),
  739. &cmd, &status);
  740. if (ret)
  741. return ret;
  742. switch (status & IWL_ADD_STA_STATUS_MASK) {
  743. case ADD_STA_SUCCESS:
  744. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  745. start ? "start" : "stopp");
  746. break;
  747. case ADD_STA_IMMEDIATE_BA_FAILURE:
  748. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  749. ret = -ENOSPC;
  750. break;
  751. default:
  752. ret = -EIO;
  753. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  754. start ? "start" : "stopp", status);
  755. break;
  756. }
  757. if (!ret) {
  758. if (start)
  759. mvm->rx_ba_sessions++;
  760. else if (mvm->rx_ba_sessions > 0)
  761. /* check that restart flow didn't zero the counter */
  762. mvm->rx_ba_sessions--;
  763. }
  764. return ret;
  765. }
  766. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  767. int tid, u8 queue, bool start)
  768. {
  769. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  770. struct iwl_mvm_add_sta_cmd cmd = {};
  771. int ret;
  772. u32 status;
  773. lockdep_assert_held(&mvm->mutex);
  774. if (start) {
  775. mvm_sta->tfd_queue_msk |= BIT(queue);
  776. mvm_sta->tid_disable_agg &= ~BIT(tid);
  777. } else {
  778. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  779. mvm_sta->tid_disable_agg |= BIT(tid);
  780. }
  781. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  782. cmd.sta_id = mvm_sta->sta_id;
  783. cmd.add_modify = STA_MODE_MODIFY;
  784. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  785. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  786. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  787. status = ADD_STA_SUCCESS;
  788. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  789. iwl_mvm_add_sta_cmd_size(mvm),
  790. &cmd, &status);
  791. if (ret)
  792. return ret;
  793. switch (status & IWL_ADD_STA_STATUS_MASK) {
  794. case ADD_STA_SUCCESS:
  795. break;
  796. default:
  797. ret = -EIO;
  798. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  799. start ? "start" : "stopp", status);
  800. break;
  801. }
  802. return ret;
  803. }
  804. const u8 tid_to_mac80211_ac[] = {
  805. IEEE80211_AC_BE,
  806. IEEE80211_AC_BK,
  807. IEEE80211_AC_BK,
  808. IEEE80211_AC_BE,
  809. IEEE80211_AC_VI,
  810. IEEE80211_AC_VI,
  811. IEEE80211_AC_VO,
  812. IEEE80211_AC_VO,
  813. };
  814. static const u8 tid_to_ucode_ac[] = {
  815. AC_BE,
  816. AC_BK,
  817. AC_BK,
  818. AC_BE,
  819. AC_VI,
  820. AC_VI,
  821. AC_VO,
  822. AC_VO,
  823. };
  824. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  825. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  826. {
  827. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  828. struct iwl_mvm_tid_data *tid_data;
  829. int txq_id;
  830. int ret;
  831. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  832. return -EINVAL;
  833. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  834. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  835. mvmsta->tid_data[tid].state);
  836. return -ENXIO;
  837. }
  838. lockdep_assert_held(&mvm->mutex);
  839. spin_lock_bh(&mvmsta->lock);
  840. /* possible race condition - we entered D0i3 while starting agg */
  841. if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
  842. spin_unlock_bh(&mvmsta->lock);
  843. IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
  844. return -EIO;
  845. }
  846. spin_lock_bh(&mvm->queue_info_lock);
  847. txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
  848. mvm->last_agg_queue);
  849. if (txq_id < 0) {
  850. ret = txq_id;
  851. spin_unlock_bh(&mvm->queue_info_lock);
  852. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  853. goto release_locks;
  854. }
  855. mvm->queue_info[txq_id].setup_reserved = true;
  856. spin_unlock_bh(&mvm->queue_info_lock);
  857. tid_data = &mvmsta->tid_data[tid];
  858. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  859. tid_data->txq_id = txq_id;
  860. *ssn = tid_data->ssn;
  861. IWL_DEBUG_TX_QUEUES(mvm,
  862. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  863. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  864. tid_data->next_reclaimed);
  865. if (tid_data->ssn == tid_data->next_reclaimed) {
  866. tid_data->state = IWL_AGG_STARTING;
  867. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  868. } else {
  869. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  870. }
  871. ret = 0;
  872. release_locks:
  873. spin_unlock_bh(&mvmsta->lock);
  874. return ret;
  875. }
  876. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  877. struct ieee80211_sta *sta, u16 tid, u8 buf_size,
  878. bool amsdu)
  879. {
  880. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  881. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  882. unsigned int wdg_timeout =
  883. iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
  884. int queue, ret;
  885. u16 ssn;
  886. struct iwl_trans_txq_scd_cfg cfg = {
  887. .sta_id = mvmsta->sta_id,
  888. .tid = tid,
  889. .frame_limit = buf_size,
  890. .aggregate = true,
  891. };
  892. BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
  893. != IWL_MAX_TID_COUNT);
  894. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  895. spin_lock_bh(&mvmsta->lock);
  896. ssn = tid_data->ssn;
  897. queue = tid_data->txq_id;
  898. tid_data->state = IWL_AGG_ON;
  899. mvmsta->agg_tids |= BIT(tid);
  900. tid_data->ssn = 0xffff;
  901. tid_data->amsdu_in_ampdu_allowed = amsdu;
  902. spin_unlock_bh(&mvmsta->lock);
  903. cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  904. iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[tid_to_mac80211_ac[tid]],
  905. ssn, &cfg, wdg_timeout);
  906. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  907. if (ret)
  908. return -EIO;
  909. /* No need to mark as reserved */
  910. spin_lock_bh(&mvm->queue_info_lock);
  911. mvm->queue_info[queue].setup_reserved = false;
  912. spin_unlock_bh(&mvm->queue_info_lock);
  913. /*
  914. * Even though in theory the peer could have different
  915. * aggregation reorder buffer sizes for different sessions,
  916. * our ucode doesn't allow for that and has a global limit
  917. * for each station. Therefore, use the minimum of all the
  918. * aggregation sessions and our default value.
  919. */
  920. mvmsta->max_agg_bufsize =
  921. min(mvmsta->max_agg_bufsize, buf_size);
  922. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  923. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  924. sta->addr, tid);
  925. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  926. }
  927. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  928. struct ieee80211_sta *sta, u16 tid)
  929. {
  930. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  931. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  932. u16 txq_id;
  933. int err;
  934. /*
  935. * If mac80211 is cleaning its state, then say that we finished since
  936. * our state has been cleared anyway.
  937. */
  938. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  939. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  940. return 0;
  941. }
  942. spin_lock_bh(&mvmsta->lock);
  943. txq_id = tid_data->txq_id;
  944. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  945. mvmsta->sta_id, tid, txq_id, tid_data->state);
  946. mvmsta->agg_tids &= ~BIT(tid);
  947. /* No need to mark as reserved anymore */
  948. spin_lock_bh(&mvm->queue_info_lock);
  949. mvm->queue_info[txq_id].setup_reserved = false;
  950. spin_unlock_bh(&mvm->queue_info_lock);
  951. switch (tid_data->state) {
  952. case IWL_AGG_ON:
  953. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  954. IWL_DEBUG_TX_QUEUES(mvm,
  955. "ssn = %d, next_recl = %d\n",
  956. tid_data->ssn, tid_data->next_reclaimed);
  957. /* There are still packets for this RA / TID in the HW */
  958. if (tid_data->ssn != tid_data->next_reclaimed) {
  959. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  960. err = 0;
  961. break;
  962. }
  963. tid_data->ssn = 0xffff;
  964. tid_data->state = IWL_AGG_OFF;
  965. spin_unlock_bh(&mvmsta->lock);
  966. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  967. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  968. iwl_mvm_disable_txq(mvm, txq_id,
  969. vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
  970. 0);
  971. return 0;
  972. case IWL_AGG_STARTING:
  973. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  974. /*
  975. * The agg session has been stopped before it was set up. This
  976. * can happen when the AddBA timer times out for example.
  977. */
  978. /* No barriers since we are under mutex */
  979. lockdep_assert_held(&mvm->mutex);
  980. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  981. tid_data->state = IWL_AGG_OFF;
  982. err = 0;
  983. break;
  984. default:
  985. IWL_ERR(mvm,
  986. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  987. mvmsta->sta_id, tid, tid_data->state);
  988. IWL_ERR(mvm,
  989. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  990. err = -EINVAL;
  991. }
  992. spin_unlock_bh(&mvmsta->lock);
  993. return err;
  994. }
  995. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  996. struct ieee80211_sta *sta, u16 tid)
  997. {
  998. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  999. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  1000. u16 txq_id;
  1001. enum iwl_mvm_agg_state old_state;
  1002. /*
  1003. * First set the agg state to OFF to avoid calling
  1004. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  1005. */
  1006. spin_lock_bh(&mvmsta->lock);
  1007. txq_id = tid_data->txq_id;
  1008. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  1009. mvmsta->sta_id, tid, txq_id, tid_data->state);
  1010. old_state = tid_data->state;
  1011. tid_data->state = IWL_AGG_OFF;
  1012. mvmsta->agg_tids &= ~BIT(tid);
  1013. spin_unlock_bh(&mvmsta->lock);
  1014. /* No need to mark as reserved */
  1015. spin_lock_bh(&mvm->queue_info_lock);
  1016. mvm->queue_info[txq_id].setup_reserved = false;
  1017. spin_unlock_bh(&mvm->queue_info_lock);
  1018. if (old_state >= IWL_AGG_ON) {
  1019. iwl_mvm_drain_sta(mvm, mvmsta, true);
  1020. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
  1021. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  1022. iwl_trans_wait_tx_queue_empty(mvm->trans,
  1023. mvmsta->tfd_queue_msk);
  1024. iwl_mvm_drain_sta(mvm, mvmsta, false);
  1025. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  1026. iwl_mvm_disable_txq(mvm, tid_data->txq_id,
  1027. vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
  1028. 0);
  1029. }
  1030. return 0;
  1031. }
  1032. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  1033. {
  1034. int i, max = -1, max_offs = -1;
  1035. lockdep_assert_held(&mvm->mutex);
  1036. /* Pick the unused key offset with the highest 'deleted'
  1037. * counter. Every time a key is deleted, all the counters
  1038. * are incremented and the one that was just deleted is
  1039. * reset to zero. Thus, the highest counter is the one
  1040. * that was deleted longest ago. Pick that one.
  1041. */
  1042. for (i = 0; i < STA_KEY_MAX_NUM; i++) {
  1043. if (test_bit(i, mvm->fw_key_table))
  1044. continue;
  1045. if (mvm->fw_key_deleted[i] > max) {
  1046. max = mvm->fw_key_deleted[i];
  1047. max_offs = i;
  1048. }
  1049. }
  1050. if (max_offs < 0)
  1051. return STA_KEY_IDX_INVALID;
  1052. return max_offs;
  1053. }
  1054. static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
  1055. struct ieee80211_vif *vif,
  1056. struct ieee80211_sta *sta)
  1057. {
  1058. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1059. if (sta)
  1060. return iwl_mvm_sta_from_mac80211(sta);
  1061. /*
  1062. * The device expects GTKs for station interfaces to be
  1063. * installed as GTKs for the AP station. If we have no
  1064. * station ID, then use AP's station ID.
  1065. */
  1066. if (vif->type == NL80211_IFTYPE_STATION &&
  1067. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1068. u8 sta_id = mvmvif->ap_sta_id;
  1069. sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
  1070. lockdep_is_held(&mvm->mutex));
  1071. /*
  1072. * It is possible that the 'sta' parameter is NULL,
  1073. * for example when a GTK is removed - the sta_id will then
  1074. * be the AP ID, and no station was passed by mac80211.
  1075. */
  1076. if (IS_ERR_OR_NULL(sta))
  1077. return NULL;
  1078. return iwl_mvm_sta_from_mac80211(sta);
  1079. }
  1080. return NULL;
  1081. }
  1082. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  1083. struct iwl_mvm_sta *mvm_sta,
  1084. struct ieee80211_key_conf *keyconf, bool mcast,
  1085. u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
  1086. u8 key_offset)
  1087. {
  1088. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1089. __le16 key_flags;
  1090. int ret;
  1091. u32 status;
  1092. u16 keyidx;
  1093. int i;
  1094. u8 sta_id = mvm_sta->sta_id;
  1095. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1096. STA_KEY_FLG_KEYID_MSK;
  1097. key_flags = cpu_to_le16(keyidx);
  1098. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  1099. switch (keyconf->cipher) {
  1100. case WLAN_CIPHER_SUITE_TKIP:
  1101. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  1102. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  1103. for (i = 0; i < 5; i++)
  1104. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  1105. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1106. break;
  1107. case WLAN_CIPHER_SUITE_CCMP:
  1108. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  1109. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1110. break;
  1111. case WLAN_CIPHER_SUITE_WEP104:
  1112. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
  1113. /* fall through */
  1114. case WLAN_CIPHER_SUITE_WEP40:
  1115. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
  1116. memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
  1117. break;
  1118. default:
  1119. key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
  1120. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1121. }
  1122. if (mcast)
  1123. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1124. cmd.key_offset = key_offset;
  1125. cmd.key_flags = key_flags;
  1126. cmd.sta_id = sta_id;
  1127. status = ADD_STA_SUCCESS;
  1128. if (cmd_flags & CMD_ASYNC)
  1129. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
  1130. sizeof(cmd), &cmd);
  1131. else
  1132. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  1133. &cmd, &status);
  1134. switch (status) {
  1135. case ADD_STA_SUCCESS:
  1136. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  1137. break;
  1138. default:
  1139. ret = -EIO;
  1140. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  1141. break;
  1142. }
  1143. return ret;
  1144. }
  1145. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  1146. struct ieee80211_key_conf *keyconf,
  1147. u8 sta_id, bool remove_key)
  1148. {
  1149. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  1150. /* verify the key details match the required command's expectations */
  1151. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  1152. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  1153. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  1154. return -EINVAL;
  1155. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  1156. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  1157. if (remove_key) {
  1158. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  1159. } else {
  1160. struct ieee80211_key_seq seq;
  1161. const u8 *pn;
  1162. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  1163. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1164. pn = seq.aes_cmac.pn;
  1165. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  1166. ((u64) pn[4] << 8) |
  1167. ((u64) pn[3] << 16) |
  1168. ((u64) pn[2] << 24) |
  1169. ((u64) pn[1] << 32) |
  1170. ((u64) pn[0] << 40));
  1171. }
  1172. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  1173. remove_key ? "removing" : "installing",
  1174. igtk_cmd.sta_id);
  1175. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
  1176. sizeof(igtk_cmd), &igtk_cmd);
  1177. }
  1178. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  1179. struct ieee80211_vif *vif,
  1180. struct ieee80211_sta *sta)
  1181. {
  1182. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1183. if (sta)
  1184. return sta->addr;
  1185. if (vif->type == NL80211_IFTYPE_STATION &&
  1186. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1187. u8 sta_id = mvmvif->ap_sta_id;
  1188. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1189. lockdep_is_held(&mvm->mutex));
  1190. return sta->addr;
  1191. }
  1192. return NULL;
  1193. }
  1194. static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  1195. struct ieee80211_vif *vif,
  1196. struct ieee80211_sta *sta,
  1197. struct ieee80211_key_conf *keyconf,
  1198. u8 key_offset,
  1199. bool mcast)
  1200. {
  1201. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1202. int ret;
  1203. const u8 *addr;
  1204. struct ieee80211_key_seq seq;
  1205. u16 p1k[5];
  1206. switch (keyconf->cipher) {
  1207. case WLAN_CIPHER_SUITE_TKIP:
  1208. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  1209. /* get phase 1 key from mac80211 */
  1210. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1211. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1212. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1213. seq.tkip.iv32, p1k, 0, key_offset);
  1214. break;
  1215. case WLAN_CIPHER_SUITE_CCMP:
  1216. case WLAN_CIPHER_SUITE_WEP40:
  1217. case WLAN_CIPHER_SUITE_WEP104:
  1218. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1219. 0, NULL, 0, key_offset);
  1220. break;
  1221. default:
  1222. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1223. 0, NULL, 0, key_offset);
  1224. }
  1225. return ret;
  1226. }
  1227. static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
  1228. struct ieee80211_key_conf *keyconf,
  1229. bool mcast)
  1230. {
  1231. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1232. __le16 key_flags;
  1233. int ret;
  1234. u32 status;
  1235. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1236. STA_KEY_FLG_KEYID_MSK);
  1237. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1238. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1239. if (mcast)
  1240. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1241. cmd.key_flags = key_flags;
  1242. cmd.key_offset = keyconf->hw_key_idx;
  1243. cmd.sta_id = sta_id;
  1244. status = ADD_STA_SUCCESS;
  1245. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  1246. &cmd, &status);
  1247. switch (status) {
  1248. case ADD_STA_SUCCESS:
  1249. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1250. break;
  1251. default:
  1252. ret = -EIO;
  1253. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1254. break;
  1255. }
  1256. return ret;
  1257. }
  1258. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  1259. struct ieee80211_vif *vif,
  1260. struct ieee80211_sta *sta,
  1261. struct ieee80211_key_conf *keyconf,
  1262. u8 key_offset)
  1263. {
  1264. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1265. struct iwl_mvm_sta *mvm_sta;
  1266. u8 sta_id;
  1267. int ret;
  1268. static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
  1269. lockdep_assert_held(&mvm->mutex);
  1270. /* Get the station id from the mvm local station table */
  1271. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  1272. if (!mvm_sta) {
  1273. IWL_ERR(mvm, "Failed to find station\n");
  1274. return -EINVAL;
  1275. }
  1276. sta_id = mvm_sta->sta_id;
  1277. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  1278. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  1279. goto end;
  1280. }
  1281. /*
  1282. * It is possible that the 'sta' parameter is NULL, and thus
  1283. * there is a need to retrieve the sta from the local station table.
  1284. */
  1285. if (!sta) {
  1286. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1287. lockdep_is_held(&mvm->mutex));
  1288. if (IS_ERR_OR_NULL(sta)) {
  1289. IWL_ERR(mvm, "Invalid station id\n");
  1290. return -EINVAL;
  1291. }
  1292. }
  1293. if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
  1294. return -EINVAL;
  1295. /* If the key_offset is not pre-assigned, we need to find a
  1296. * new offset to use. In normal cases, the offset is not
  1297. * pre-assigned, but during HW_RESTART we want to reuse the
  1298. * same indices, so we pass them when this function is called.
  1299. *
  1300. * In D3 entry, we need to hardcoded the indices (because the
  1301. * firmware hardcodes the PTK offset to 0). In this case, we
  1302. * need to make sure we don't overwrite the hw_key_idx in the
  1303. * keyconf structure, because otherwise we cannot configure
  1304. * the original ones back when resuming.
  1305. */
  1306. if (key_offset == STA_KEY_IDX_INVALID) {
  1307. key_offset = iwl_mvm_set_fw_key_idx(mvm);
  1308. if (key_offset == STA_KEY_IDX_INVALID)
  1309. return -ENOSPC;
  1310. keyconf->hw_key_idx = key_offset;
  1311. }
  1312. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
  1313. if (ret)
  1314. goto end;
  1315. /*
  1316. * For WEP, the same key is used for multicast and unicast. Upload it
  1317. * again, using the same key offset, and now pointing the other one
  1318. * to the same key slot (offset).
  1319. * If this fails, remove the original as well.
  1320. */
  1321. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1322. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
  1323. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
  1324. key_offset, !mcast);
  1325. if (ret) {
  1326. __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  1327. goto end;
  1328. }
  1329. }
  1330. __set_bit(key_offset, mvm->fw_key_table);
  1331. end:
  1332. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1333. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1334. sta ? sta->addr : zero_addr, ret);
  1335. return ret;
  1336. }
  1337. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  1338. struct ieee80211_vif *vif,
  1339. struct ieee80211_sta *sta,
  1340. struct ieee80211_key_conf *keyconf)
  1341. {
  1342. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1343. struct iwl_mvm_sta *mvm_sta;
  1344. u8 sta_id = IWL_MVM_STATION_COUNT;
  1345. int ret, i;
  1346. lockdep_assert_held(&mvm->mutex);
  1347. /* Get the station from the mvm local station table */
  1348. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  1349. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  1350. keyconf->keyidx, sta_id);
  1351. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  1352. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1353. if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
  1354. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1355. keyconf->hw_key_idx);
  1356. return -ENOENT;
  1357. }
  1358. /* track which key was deleted last */
  1359. for (i = 0; i < STA_KEY_MAX_NUM; i++) {
  1360. if (mvm->fw_key_deleted[i] < U8_MAX)
  1361. mvm->fw_key_deleted[i]++;
  1362. }
  1363. mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
  1364. if (!mvm_sta) {
  1365. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1366. return 0;
  1367. }
  1368. sta_id = mvm_sta->sta_id;
  1369. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  1370. if (ret)
  1371. return ret;
  1372. /* delete WEP key twice to get rid of (now useless) offset */
  1373. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1374. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
  1375. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
  1376. return ret;
  1377. }
  1378. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1379. struct ieee80211_vif *vif,
  1380. struct ieee80211_key_conf *keyconf,
  1381. struct ieee80211_sta *sta, u32 iv32,
  1382. u16 *phase1key)
  1383. {
  1384. struct iwl_mvm_sta *mvm_sta;
  1385. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1386. rcu_read_lock();
  1387. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  1388. if (WARN_ON_ONCE(!mvm_sta))
  1389. goto unlock;
  1390. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1391. iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
  1392. unlock:
  1393. rcu_read_unlock();
  1394. }
  1395. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1396. struct ieee80211_sta *sta)
  1397. {
  1398. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1399. struct iwl_mvm_add_sta_cmd cmd = {
  1400. .add_modify = STA_MODE_MODIFY,
  1401. .sta_id = mvmsta->sta_id,
  1402. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1403. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1404. };
  1405. int ret;
  1406. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  1407. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  1408. if (ret)
  1409. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1410. }
  1411. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1412. struct ieee80211_sta *sta,
  1413. enum ieee80211_frame_release_type reason,
  1414. u16 cnt, u16 tids, bool more_data,
  1415. bool agg)
  1416. {
  1417. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1418. struct iwl_mvm_add_sta_cmd cmd = {
  1419. .add_modify = STA_MODE_MODIFY,
  1420. .sta_id = mvmsta->sta_id,
  1421. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1422. .sleep_tx_count = cpu_to_le16(cnt),
  1423. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1424. };
  1425. int tid, ret;
  1426. unsigned long _tids = tids;
  1427. /* convert TIDs to ACs - we don't support TSPEC so that's OK
  1428. * Note that this field is reserved and unused by firmware not
  1429. * supporting GO uAPSD, so it's safe to always do this.
  1430. */
  1431. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
  1432. cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
  1433. /* If we're releasing frames from aggregation queues then check if the
  1434. * all queues combined that we're releasing frames from have
  1435. * - more frames than the service period, in which case more_data
  1436. * needs to be set
  1437. * - fewer than 'cnt' frames, in which case we need to adjust the
  1438. * firmware command (but do that unconditionally)
  1439. */
  1440. if (agg) {
  1441. int remaining = cnt;
  1442. int sleep_tx_count;
  1443. spin_lock_bh(&mvmsta->lock);
  1444. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
  1445. struct iwl_mvm_tid_data *tid_data;
  1446. u16 n_queued;
  1447. tid_data = &mvmsta->tid_data[tid];
  1448. if (WARN(tid_data->state != IWL_AGG_ON &&
  1449. tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
  1450. "TID %d state is %d\n",
  1451. tid, tid_data->state)) {
  1452. spin_unlock_bh(&mvmsta->lock);
  1453. ieee80211_sta_eosp(sta);
  1454. return;
  1455. }
  1456. n_queued = iwl_mvm_tid_queued(tid_data);
  1457. if (n_queued > remaining) {
  1458. more_data = true;
  1459. remaining = 0;
  1460. break;
  1461. }
  1462. remaining -= n_queued;
  1463. }
  1464. sleep_tx_count = cnt - remaining;
  1465. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  1466. mvmsta->sleep_tx_count = sleep_tx_count;
  1467. spin_unlock_bh(&mvmsta->lock);
  1468. cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
  1469. if (WARN_ON(cnt - remaining == 0)) {
  1470. ieee80211_sta_eosp(sta);
  1471. return;
  1472. }
  1473. }
  1474. /* Note: this is ignored by firmware not supporting GO uAPSD */
  1475. if (more_data)
  1476. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
  1477. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
  1478. mvmsta->next_status_eosp = true;
  1479. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
  1480. } else {
  1481. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
  1482. }
  1483. /* block the Tx queues until the FW updated the sleep Tx count */
  1484. iwl_trans_block_txq_ptrs(mvm->trans, true);
  1485. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
  1486. CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
  1487. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  1488. if (ret)
  1489. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1490. }
  1491. void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
  1492. struct iwl_rx_cmd_buffer *rxb)
  1493. {
  1494. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1495. struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
  1496. struct ieee80211_sta *sta;
  1497. u32 sta_id = le32_to_cpu(notif->sta_id);
  1498. if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
  1499. return;
  1500. rcu_read_lock();
  1501. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1502. if (!IS_ERR_OR_NULL(sta))
  1503. ieee80211_sta_eosp(sta);
  1504. rcu_read_unlock();
  1505. }
  1506. void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
  1507. struct iwl_mvm_sta *mvmsta, bool disable)
  1508. {
  1509. struct iwl_mvm_add_sta_cmd cmd = {
  1510. .add_modify = STA_MODE_MODIFY,
  1511. .sta_id = mvmsta->sta_id,
  1512. .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
  1513. .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
  1514. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1515. };
  1516. int ret;
  1517. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  1518. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  1519. if (ret)
  1520. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1521. }
  1522. void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
  1523. struct ieee80211_sta *sta,
  1524. bool disable)
  1525. {
  1526. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1527. spin_lock_bh(&mvm_sta->lock);
  1528. if (mvm_sta->disable_tx == disable) {
  1529. spin_unlock_bh(&mvm_sta->lock);
  1530. return;
  1531. }
  1532. mvm_sta->disable_tx = disable;
  1533. /*
  1534. * Tell mac80211 to start/stop queuing tx for this station,
  1535. * but don't stop queuing if there are still pending frames
  1536. * for this station.
  1537. */
  1538. if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
  1539. ieee80211_sta_block_awake(mvm->hw, sta, disable);
  1540. iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
  1541. spin_unlock_bh(&mvm_sta->lock);
  1542. }
  1543. void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
  1544. struct iwl_mvm_vif *mvmvif,
  1545. bool disable)
  1546. {
  1547. struct ieee80211_sta *sta;
  1548. struct iwl_mvm_sta *mvm_sta;
  1549. int i;
  1550. lockdep_assert_held(&mvm->mutex);
  1551. /* Block/unblock all the stations of the given mvmvif */
  1552. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  1553. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  1554. lockdep_is_held(&mvm->mutex));
  1555. if (IS_ERR_OR_NULL(sta))
  1556. continue;
  1557. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1558. if (mvm_sta->mac_id_n_color !=
  1559. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
  1560. continue;
  1561. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
  1562. }
  1563. }
  1564. void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1565. {
  1566. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1567. struct iwl_mvm_sta *mvmsta;
  1568. rcu_read_lock();
  1569. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
  1570. if (!WARN_ON(!mvmsta))
  1571. iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
  1572. rcu_read_unlock();
  1573. }