sta.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <net/mac80211.h>
  66. #include "mvm.h"
  67. #include "sta.h"
  68. #include "rs.h"
  69. static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
  70. enum nl80211_iftype iftype)
  71. {
  72. int sta_id;
  73. u32 reserved_ids = 0;
  74. BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
  75. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  76. lockdep_assert_held(&mvm->mutex);
  77. /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
  78. if (iftype != NL80211_IFTYPE_STATION)
  79. reserved_ids = BIT(0);
  80. /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
  81. for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
  82. if (BIT(sta_id) & reserved_ids)
  83. continue;
  84. if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  85. lockdep_is_held(&mvm->mutex)))
  86. return sta_id;
  87. }
  88. return IWL_MVM_STATION_COUNT;
  89. }
  90. /* send station add/update command to firmware */
  91. int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  92. bool update)
  93. {
  94. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  95. struct iwl_mvm_add_sta_cmd add_sta_cmd = {
  96. .sta_id = mvm_sta->sta_id,
  97. .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
  98. .add_modify = update ? 1 : 0,
  99. .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
  100. STA_FLG_MIMO_EN_MSK),
  101. };
  102. int ret;
  103. u32 status;
  104. u32 agg_size = 0, mpdu_dens = 0;
  105. if (!update) {
  106. add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  107. memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
  108. }
  109. switch (sta->bandwidth) {
  110. case IEEE80211_STA_RX_BW_160:
  111. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
  112. /* fall through */
  113. case IEEE80211_STA_RX_BW_80:
  114. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
  115. /* fall through */
  116. case IEEE80211_STA_RX_BW_40:
  117. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
  118. /* fall through */
  119. case IEEE80211_STA_RX_BW_20:
  120. if (sta->ht_cap.ht_supported)
  121. add_sta_cmd.station_flags |=
  122. cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
  123. break;
  124. }
  125. switch (sta->rx_nss) {
  126. case 1:
  127. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  128. break;
  129. case 2:
  130. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
  131. break;
  132. case 3 ... 8:
  133. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
  134. break;
  135. }
  136. switch (sta->smps_mode) {
  137. case IEEE80211_SMPS_AUTOMATIC:
  138. case IEEE80211_SMPS_NUM_MODES:
  139. WARN_ON(1);
  140. break;
  141. case IEEE80211_SMPS_STATIC:
  142. /* override NSS */
  143. add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
  144. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  145. break;
  146. case IEEE80211_SMPS_DYNAMIC:
  147. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
  148. break;
  149. case IEEE80211_SMPS_OFF:
  150. /* nothing */
  151. break;
  152. }
  153. if (sta->ht_cap.ht_supported) {
  154. add_sta_cmd.station_flags_msk |=
  155. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  156. STA_FLG_AGG_MPDU_DENS_MSK);
  157. mpdu_dens = sta->ht_cap.ampdu_density;
  158. }
  159. if (sta->vht_cap.vht_supported) {
  160. agg_size = sta->vht_cap.cap &
  161. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  162. agg_size >>=
  163. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  164. } else if (sta->ht_cap.ht_supported) {
  165. agg_size = sta->ht_cap.ampdu_factor;
  166. }
  167. add_sta_cmd.station_flags |=
  168. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  169. add_sta_cmd.station_flags |=
  170. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  171. status = ADD_STA_SUCCESS;
  172. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
  173. &add_sta_cmd, &status);
  174. if (ret)
  175. return ret;
  176. switch (status) {
  177. case ADD_STA_SUCCESS:
  178. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  179. break;
  180. default:
  181. ret = -EIO;
  182. IWL_ERR(mvm, "ADD_STA failed\n");
  183. break;
  184. }
  185. return ret;
  186. }
  187. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  188. struct ieee80211_vif *vif,
  189. struct ieee80211_sta *sta)
  190. {
  191. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  192. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  193. int i, ret, sta_id;
  194. lockdep_assert_held(&mvm->mutex);
  195. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  196. sta_id = iwl_mvm_find_free_sta_id(mvm,
  197. ieee80211_vif_type_p2p(vif));
  198. else
  199. sta_id = mvm_sta->sta_id;
  200. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  201. return -ENOSPC;
  202. spin_lock_init(&mvm_sta->lock);
  203. mvm_sta->sta_id = sta_id;
  204. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  205. mvmvif->color);
  206. mvm_sta->vif = vif;
  207. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  208. mvm_sta->tx_protection = 0;
  209. mvm_sta->tt_tx_protection = false;
  210. /* HW restart, don't assume the memory has been zeroed */
  211. atomic_set(&mvm->pending_frames[sta_id], 0);
  212. mvm_sta->tid_disable_agg = 0;
  213. mvm_sta->tfd_queue_msk = 0;
  214. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  215. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  216. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  217. /* for HW restart - reset everything but the sequence number */
  218. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  219. u16 seq = mvm_sta->tid_data[i].seq_number;
  220. memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
  221. mvm_sta->tid_data[i].seq_number = seq;
  222. }
  223. mvm_sta->agg_tids = 0;
  224. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
  225. if (ret)
  226. return ret;
  227. if (vif->type == NL80211_IFTYPE_STATION) {
  228. if (!sta->tdls) {
  229. WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
  230. mvmvif->ap_sta_id = sta_id;
  231. } else {
  232. WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
  233. }
  234. }
  235. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  236. return 0;
  237. }
  238. int iwl_mvm_update_sta(struct iwl_mvm *mvm,
  239. struct ieee80211_vif *vif,
  240. struct ieee80211_sta *sta)
  241. {
  242. return iwl_mvm_sta_send_to_fw(mvm, sta, true);
  243. }
  244. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  245. bool drain)
  246. {
  247. struct iwl_mvm_add_sta_cmd cmd = {};
  248. int ret;
  249. u32 status;
  250. lockdep_assert_held(&mvm->mutex);
  251. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  252. cmd.sta_id = mvmsta->sta_id;
  253. cmd.add_modify = STA_MODE_MODIFY;
  254. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  255. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  256. status = ADD_STA_SUCCESS;
  257. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  258. &cmd, &status);
  259. if (ret)
  260. return ret;
  261. switch (status) {
  262. case ADD_STA_SUCCESS:
  263. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  264. mvmsta->sta_id);
  265. break;
  266. default:
  267. ret = -EIO;
  268. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  269. mvmsta->sta_id);
  270. break;
  271. }
  272. return ret;
  273. }
  274. /*
  275. * Remove a station from the FW table. Before sending the command to remove
  276. * the station validate that the station is indeed known to the driver (sanity
  277. * only).
  278. */
  279. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  280. {
  281. struct ieee80211_sta *sta;
  282. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  283. .sta_id = sta_id,
  284. };
  285. int ret;
  286. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  287. lockdep_is_held(&mvm->mutex));
  288. /* Note: internal stations are marked as error values */
  289. if (!sta) {
  290. IWL_ERR(mvm, "Invalid station id\n");
  291. return -EINVAL;
  292. }
  293. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
  294. sizeof(rm_sta_cmd), &rm_sta_cmd);
  295. if (ret) {
  296. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  302. {
  303. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  304. u8 sta_id;
  305. /*
  306. * The mutex is needed because of the SYNC cmd, but not only: if the
  307. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  308. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  309. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  310. * that later.
  311. */
  312. mutex_lock(&mvm->mutex);
  313. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  314. int ret;
  315. struct ieee80211_sta *sta =
  316. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  317. lockdep_is_held(&mvm->mutex));
  318. /*
  319. * This station is in use or RCU-removed; the latter happens in
  320. * managed mode, where mac80211 removes the station before we
  321. * can remove it from firmware (we can only do that after the
  322. * MAC is marked unassociated), and possibly while the deauth
  323. * frame to disconnect from the AP is still queued. Then, the
  324. * station pointer is -ENOENT when the last skb is reclaimed.
  325. */
  326. if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
  327. continue;
  328. if (PTR_ERR(sta) == -EINVAL) {
  329. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  330. sta_id);
  331. continue;
  332. }
  333. if (!sta) {
  334. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  335. sta_id);
  336. continue;
  337. }
  338. WARN_ON(PTR_ERR(sta) != -EBUSY);
  339. /* This station was removed and we waited until it got drained,
  340. * we can now proceed and remove it.
  341. */
  342. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  343. if (ret) {
  344. IWL_ERR(mvm,
  345. "Couldn't remove sta %d after it was drained\n",
  346. sta_id);
  347. continue;
  348. }
  349. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  350. clear_bit(sta_id, mvm->sta_drained);
  351. }
  352. mutex_unlock(&mvm->mutex);
  353. }
  354. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  355. struct ieee80211_vif *vif,
  356. struct ieee80211_sta *sta)
  357. {
  358. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  359. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  360. int ret;
  361. lockdep_assert_held(&mvm->mutex);
  362. if (vif->type == NL80211_IFTYPE_STATION &&
  363. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  364. /* flush its queues here since we are freeing mvm_sta */
  365. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
  366. /* if we are associated - we can't remove the AP STA now */
  367. if (vif->bss_conf.assoc)
  368. return ret;
  369. /* unassoc - go ahead - remove the AP STA now */
  370. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  371. /* clear d0i3_ap_sta_id if no longer relevant */
  372. if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
  373. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  374. }
  375. /*
  376. * Make sure that the tx response code sees the station as -EBUSY and
  377. * calls the drain worker.
  378. */
  379. spin_lock_bh(&mvm_sta->lock);
  380. /*
  381. * There are frames pending on the AC queues for this station.
  382. * We need to wait until all the frames are drained...
  383. */
  384. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  385. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  386. ERR_PTR(-EBUSY));
  387. spin_unlock_bh(&mvm_sta->lock);
  388. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  389. } else {
  390. spin_unlock_bh(&mvm_sta->lock);
  391. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  392. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  393. }
  394. return ret;
  395. }
  396. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  397. struct ieee80211_vif *vif,
  398. u8 sta_id)
  399. {
  400. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  401. lockdep_assert_held(&mvm->mutex);
  402. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  403. return ret;
  404. }
  405. static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
  406. struct iwl_mvm_int_sta *sta,
  407. u32 qmask, enum nl80211_iftype iftype)
  408. {
  409. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  410. sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
  411. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  412. return -ENOSPC;
  413. }
  414. sta->tfd_queue_msk = qmask;
  415. /* put a non-NULL value so iterating over the stations won't stop */
  416. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  417. return 0;
  418. }
  419. static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
  420. struct iwl_mvm_int_sta *sta)
  421. {
  422. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  423. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  424. sta->sta_id = IWL_MVM_STATION_COUNT;
  425. }
  426. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  427. struct iwl_mvm_int_sta *sta,
  428. const u8 *addr,
  429. u16 mac_id, u16 color)
  430. {
  431. struct iwl_mvm_add_sta_cmd cmd;
  432. int ret;
  433. u32 status;
  434. lockdep_assert_held(&mvm->mutex);
  435. memset(&cmd, 0, sizeof(cmd));
  436. cmd.sta_id = sta->sta_id;
  437. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  438. color));
  439. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  440. if (addr)
  441. memcpy(cmd.addr, addr, ETH_ALEN);
  442. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  443. &cmd, &status);
  444. if (ret)
  445. return ret;
  446. switch (status) {
  447. case ADD_STA_SUCCESS:
  448. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  449. return 0;
  450. default:
  451. ret = -EIO;
  452. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  453. status);
  454. break;
  455. }
  456. return ret;
  457. }
  458. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  459. {
  460. int ret;
  461. lockdep_assert_held(&mvm->mutex);
  462. /* Map Aux queue to fifo - needs to happen before adding Aux station */
  463. iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue,
  464. IWL_MVM_TX_FIFO_MCAST);
  465. /* Allocate aux station and assign to it the aux queue */
  466. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
  467. NL80211_IFTYPE_UNSPECIFIED);
  468. if (ret)
  469. return ret;
  470. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  471. MAC_INDEX_AUX, 0);
  472. if (ret)
  473. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  474. return ret;
  475. }
  476. void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
  477. {
  478. lockdep_assert_held(&mvm->mutex);
  479. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  480. }
  481. /*
  482. * Send the add station command for the vif's broadcast station.
  483. * Assumes that the station was already allocated.
  484. *
  485. * @mvm: the mvm component
  486. * @vif: the interface to which the broadcast station is added
  487. * @bsta: the broadcast station to add.
  488. */
  489. int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  490. {
  491. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  492. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  493. static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  494. const u8 *baddr = _baddr;
  495. lockdep_assert_held(&mvm->mutex);
  496. if (vif->type == NL80211_IFTYPE_ADHOC)
  497. baddr = vif->bss_conf.bssid;
  498. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  499. return -ENOSPC;
  500. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  501. mvmvif->id, mvmvif->color);
  502. }
  503. /* Send the FW a request to remove the station from it's internal data
  504. * structures, but DO NOT remove the entry from the local data structures. */
  505. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  506. {
  507. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  508. int ret;
  509. lockdep_assert_held(&mvm->mutex);
  510. ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
  511. if (ret)
  512. IWL_WARN(mvm, "Failed sending remove station\n");
  513. return ret;
  514. }
  515. int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  516. {
  517. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  518. u32 qmask;
  519. lockdep_assert_held(&mvm->mutex);
  520. qmask = iwl_mvm_mac_get_queues_mask(vif);
  521. /*
  522. * The firmware defines the TFD queue mask to only be relevant
  523. * for *unicast* queues, so the multicast (CAB) queue shouldn't
  524. * be included.
  525. */
  526. if (vif->type == NL80211_IFTYPE_AP)
  527. qmask &= ~BIT(vif->cab_queue);
  528. return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
  529. ieee80211_vif_type_p2p(vif));
  530. }
  531. /* Allocate a new station entry for the broadcast station to the given vif,
  532. * and send it to the FW.
  533. * Note that each P2P mac should have its own broadcast station.
  534. *
  535. * @mvm: the mvm component
  536. * @vif: the interface to which the broadcast station is added
  537. * @bsta: the broadcast station to add. */
  538. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  539. {
  540. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  541. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  542. int ret;
  543. lockdep_assert_held(&mvm->mutex);
  544. ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
  545. if (ret)
  546. return ret;
  547. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  548. if (ret)
  549. iwl_mvm_dealloc_int_sta(mvm, bsta);
  550. return ret;
  551. }
  552. void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  553. {
  554. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  555. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  556. }
  557. /*
  558. * Send the FW a request to remove the station from it's internal data
  559. * structures, and in addition remove it from the local data structure.
  560. */
  561. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  562. {
  563. int ret;
  564. lockdep_assert_held(&mvm->mutex);
  565. ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
  566. iwl_mvm_dealloc_bcast_sta(mvm, vif);
  567. return ret;
  568. }
  569. #define IWL_MAX_RX_BA_SESSIONS 16
  570. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  571. int tid, u16 ssn, bool start)
  572. {
  573. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  574. struct iwl_mvm_add_sta_cmd cmd = {};
  575. int ret;
  576. u32 status;
  577. lockdep_assert_held(&mvm->mutex);
  578. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  579. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  580. return -ENOSPC;
  581. }
  582. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  583. cmd.sta_id = mvm_sta->sta_id;
  584. cmd.add_modify = STA_MODE_MODIFY;
  585. if (start) {
  586. cmd.add_immediate_ba_tid = (u8) tid;
  587. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  588. } else {
  589. cmd.remove_immediate_ba_tid = (u8) tid;
  590. }
  591. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  592. STA_MODIFY_REMOVE_BA_TID;
  593. status = ADD_STA_SUCCESS;
  594. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  595. &cmd, &status);
  596. if (ret)
  597. return ret;
  598. switch (status) {
  599. case ADD_STA_SUCCESS:
  600. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  601. start ? "start" : "stopp");
  602. break;
  603. case ADD_STA_IMMEDIATE_BA_FAILURE:
  604. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  605. ret = -ENOSPC;
  606. break;
  607. default:
  608. ret = -EIO;
  609. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  610. start ? "start" : "stopp", status);
  611. break;
  612. }
  613. if (!ret) {
  614. if (start)
  615. mvm->rx_ba_sessions++;
  616. else if (mvm->rx_ba_sessions > 0)
  617. /* check that restart flow didn't zero the counter */
  618. mvm->rx_ba_sessions--;
  619. }
  620. return ret;
  621. }
  622. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  623. int tid, u8 queue, bool start)
  624. {
  625. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  626. struct iwl_mvm_add_sta_cmd cmd = {};
  627. int ret;
  628. u32 status;
  629. lockdep_assert_held(&mvm->mutex);
  630. if (start) {
  631. mvm_sta->tfd_queue_msk |= BIT(queue);
  632. mvm_sta->tid_disable_agg &= ~BIT(tid);
  633. } else {
  634. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  635. mvm_sta->tid_disable_agg |= BIT(tid);
  636. }
  637. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  638. cmd.sta_id = mvm_sta->sta_id;
  639. cmd.add_modify = STA_MODE_MODIFY;
  640. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  641. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  642. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  643. status = ADD_STA_SUCCESS;
  644. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  645. &cmd, &status);
  646. if (ret)
  647. return ret;
  648. switch (status) {
  649. case ADD_STA_SUCCESS:
  650. break;
  651. default:
  652. ret = -EIO;
  653. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  654. start ? "start" : "stopp", status);
  655. break;
  656. }
  657. return ret;
  658. }
  659. const u8 tid_to_mac80211_ac[] = {
  660. IEEE80211_AC_BE,
  661. IEEE80211_AC_BK,
  662. IEEE80211_AC_BK,
  663. IEEE80211_AC_BE,
  664. IEEE80211_AC_VI,
  665. IEEE80211_AC_VI,
  666. IEEE80211_AC_VO,
  667. IEEE80211_AC_VO,
  668. };
  669. static const u8 tid_to_ucode_ac[] = {
  670. AC_BE,
  671. AC_BK,
  672. AC_BK,
  673. AC_BE,
  674. AC_VI,
  675. AC_VI,
  676. AC_VO,
  677. AC_VO,
  678. };
  679. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  680. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  681. {
  682. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  683. struct iwl_mvm_tid_data *tid_data;
  684. int txq_id;
  685. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  686. return -EINVAL;
  687. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  688. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  689. mvmsta->tid_data[tid].state);
  690. return -ENXIO;
  691. }
  692. lockdep_assert_held(&mvm->mutex);
  693. for (txq_id = mvm->first_agg_queue;
  694. txq_id <= mvm->last_agg_queue; txq_id++)
  695. if (mvm->queue_to_mac80211[txq_id] ==
  696. IWL_INVALID_MAC80211_QUEUE)
  697. break;
  698. if (txq_id > mvm->last_agg_queue) {
  699. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  700. return -EIO;
  701. }
  702. spin_lock_bh(&mvmsta->lock);
  703. /* possible race condition - we entered D0i3 while starting agg */
  704. if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
  705. spin_unlock_bh(&mvmsta->lock);
  706. IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
  707. return -EIO;
  708. }
  709. /* the new tx queue is still connected to the same mac80211 queue */
  710. mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
  711. tid_data = &mvmsta->tid_data[tid];
  712. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  713. tid_data->txq_id = txq_id;
  714. *ssn = tid_data->ssn;
  715. IWL_DEBUG_TX_QUEUES(mvm,
  716. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  717. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  718. tid_data->next_reclaimed);
  719. if (tid_data->ssn == tid_data->next_reclaimed) {
  720. tid_data->state = IWL_AGG_STARTING;
  721. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  722. } else {
  723. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  724. }
  725. spin_unlock_bh(&mvmsta->lock);
  726. return 0;
  727. }
  728. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  729. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  730. {
  731. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  732. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  733. int queue, fifo, ret;
  734. u16 ssn;
  735. BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
  736. != IWL_MAX_TID_COUNT);
  737. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  738. spin_lock_bh(&mvmsta->lock);
  739. ssn = tid_data->ssn;
  740. queue = tid_data->txq_id;
  741. tid_data->state = IWL_AGG_ON;
  742. mvmsta->agg_tids |= BIT(tid);
  743. tid_data->ssn = 0xffff;
  744. spin_unlock_bh(&mvmsta->lock);
  745. fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  746. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  747. if (ret)
  748. return -EIO;
  749. iwl_mvm_enable_agg_txq(mvm, queue, fifo, mvmsta->sta_id, tid,
  750. buf_size, ssn);
  751. /*
  752. * Even though in theory the peer could have different
  753. * aggregation reorder buffer sizes for different sessions,
  754. * our ucode doesn't allow for that and has a global limit
  755. * for each station. Therefore, use the minimum of all the
  756. * aggregation sessions and our default value.
  757. */
  758. mvmsta->max_agg_bufsize =
  759. min(mvmsta->max_agg_bufsize, buf_size);
  760. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  761. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  762. sta->addr, tid);
  763. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  764. }
  765. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  766. struct ieee80211_sta *sta, u16 tid)
  767. {
  768. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  769. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  770. u16 txq_id;
  771. int err;
  772. /*
  773. * If mac80211 is cleaning its state, then say that we finished since
  774. * our state has been cleared anyway.
  775. */
  776. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  777. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  778. return 0;
  779. }
  780. spin_lock_bh(&mvmsta->lock);
  781. txq_id = tid_data->txq_id;
  782. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  783. mvmsta->sta_id, tid, txq_id, tid_data->state);
  784. mvmsta->agg_tids &= ~BIT(tid);
  785. switch (tid_data->state) {
  786. case IWL_AGG_ON:
  787. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  788. IWL_DEBUG_TX_QUEUES(mvm,
  789. "ssn = %d, next_recl = %d\n",
  790. tid_data->ssn, tid_data->next_reclaimed);
  791. /* There are still packets for this RA / TID in the HW */
  792. if (tid_data->ssn != tid_data->next_reclaimed) {
  793. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  794. err = 0;
  795. break;
  796. }
  797. tid_data->ssn = 0xffff;
  798. tid_data->state = IWL_AGG_OFF;
  799. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  800. spin_unlock_bh(&mvmsta->lock);
  801. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  802. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  803. iwl_mvm_disable_txq(mvm, txq_id);
  804. return 0;
  805. case IWL_AGG_STARTING:
  806. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  807. /*
  808. * The agg session has been stopped before it was set up. This
  809. * can happen when the AddBA timer times out for example.
  810. */
  811. /* No barriers since we are under mutex */
  812. lockdep_assert_held(&mvm->mutex);
  813. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  814. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  815. tid_data->state = IWL_AGG_OFF;
  816. err = 0;
  817. break;
  818. default:
  819. IWL_ERR(mvm,
  820. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  821. mvmsta->sta_id, tid, tid_data->state);
  822. IWL_ERR(mvm,
  823. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  824. err = -EINVAL;
  825. }
  826. spin_unlock_bh(&mvmsta->lock);
  827. return err;
  828. }
  829. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  830. struct ieee80211_sta *sta, u16 tid)
  831. {
  832. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  833. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  834. u16 txq_id;
  835. enum iwl_mvm_agg_state old_state;
  836. /*
  837. * First set the agg state to OFF to avoid calling
  838. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  839. */
  840. spin_lock_bh(&mvmsta->lock);
  841. txq_id = tid_data->txq_id;
  842. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  843. mvmsta->sta_id, tid, txq_id, tid_data->state);
  844. old_state = tid_data->state;
  845. tid_data->state = IWL_AGG_OFF;
  846. mvmsta->agg_tids &= ~BIT(tid);
  847. spin_unlock_bh(&mvmsta->lock);
  848. if (old_state >= IWL_AGG_ON) {
  849. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
  850. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  851. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  852. iwl_mvm_disable_txq(mvm, tid_data->txq_id);
  853. }
  854. mvm->queue_to_mac80211[tid_data->txq_id] =
  855. IWL_INVALID_MAC80211_QUEUE;
  856. return 0;
  857. }
  858. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  859. {
  860. int i;
  861. lockdep_assert_held(&mvm->mutex);
  862. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  863. if (i == STA_KEY_MAX_NUM)
  864. return STA_KEY_IDX_INVALID;
  865. __set_bit(i, mvm->fw_key_table);
  866. return i;
  867. }
  868. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  869. struct ieee80211_sta *sta)
  870. {
  871. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  872. if (sta) {
  873. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  874. return mvm_sta->sta_id;
  875. }
  876. /*
  877. * The device expects GTKs for station interfaces to be
  878. * installed as GTKs for the AP station. If we have no
  879. * station ID, then use AP's station ID.
  880. */
  881. if (vif->type == NL80211_IFTYPE_STATION &&
  882. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  883. return mvmvif->ap_sta_id;
  884. return IWL_MVM_STATION_COUNT;
  885. }
  886. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  887. struct iwl_mvm_sta *mvm_sta,
  888. struct ieee80211_key_conf *keyconf,
  889. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  890. u32 cmd_flags)
  891. {
  892. struct iwl_mvm_add_sta_key_cmd cmd = {};
  893. __le16 key_flags;
  894. int ret, status;
  895. u16 keyidx;
  896. int i;
  897. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  898. STA_KEY_FLG_KEYID_MSK;
  899. key_flags = cpu_to_le16(keyidx);
  900. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  901. switch (keyconf->cipher) {
  902. case WLAN_CIPHER_SUITE_TKIP:
  903. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  904. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  905. for (i = 0; i < 5; i++)
  906. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  907. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  908. break;
  909. case WLAN_CIPHER_SUITE_CCMP:
  910. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  911. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  912. break;
  913. default:
  914. key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
  915. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  916. }
  917. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  918. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  919. cmd.key_offset = keyconf->hw_key_idx;
  920. cmd.key_flags = key_flags;
  921. cmd.sta_id = sta_id;
  922. status = ADD_STA_SUCCESS;
  923. if (cmd_flags & CMD_ASYNC)
  924. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
  925. sizeof(cmd), &cmd);
  926. else
  927. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  928. &cmd, &status);
  929. switch (status) {
  930. case ADD_STA_SUCCESS:
  931. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  932. break;
  933. default:
  934. ret = -EIO;
  935. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  936. break;
  937. }
  938. return ret;
  939. }
  940. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  941. struct ieee80211_key_conf *keyconf,
  942. u8 sta_id, bool remove_key)
  943. {
  944. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  945. /* verify the key details match the required command's expectations */
  946. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  947. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  948. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  949. return -EINVAL;
  950. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  951. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  952. if (remove_key) {
  953. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  954. } else {
  955. struct ieee80211_key_seq seq;
  956. const u8 *pn;
  957. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  958. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  959. igtk_cmd.K1, igtk_cmd.K2);
  960. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  961. pn = seq.aes_cmac.pn;
  962. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  963. ((u64) pn[4] << 8) |
  964. ((u64) pn[3] << 16) |
  965. ((u64) pn[2] << 24) |
  966. ((u64) pn[1] << 32) |
  967. ((u64) pn[0] << 40));
  968. }
  969. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  970. remove_key ? "removing" : "installing",
  971. igtk_cmd.sta_id);
  972. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
  973. sizeof(igtk_cmd), &igtk_cmd);
  974. }
  975. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  976. struct ieee80211_vif *vif,
  977. struct ieee80211_sta *sta)
  978. {
  979. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  980. if (sta)
  981. return sta->addr;
  982. if (vif->type == NL80211_IFTYPE_STATION &&
  983. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  984. u8 sta_id = mvmvif->ap_sta_id;
  985. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  986. lockdep_is_held(&mvm->mutex));
  987. return sta->addr;
  988. }
  989. return NULL;
  990. }
  991. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  992. struct ieee80211_vif *vif,
  993. struct ieee80211_sta *sta,
  994. struct ieee80211_key_conf *keyconf,
  995. bool have_key_offset)
  996. {
  997. struct iwl_mvm_sta *mvm_sta;
  998. int ret;
  999. u8 *addr, sta_id;
  1000. struct ieee80211_key_seq seq;
  1001. u16 p1k[5];
  1002. lockdep_assert_held(&mvm->mutex);
  1003. /* Get the station id from the mvm local station table */
  1004. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1005. if (sta_id == IWL_MVM_STATION_COUNT) {
  1006. IWL_ERR(mvm, "Failed to find station id\n");
  1007. return -EINVAL;
  1008. }
  1009. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  1010. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  1011. goto end;
  1012. }
  1013. /*
  1014. * It is possible that the 'sta' parameter is NULL, and thus
  1015. * there is a need to retrieve the sta from the local station table.
  1016. */
  1017. if (!sta) {
  1018. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1019. lockdep_is_held(&mvm->mutex));
  1020. if (IS_ERR_OR_NULL(sta)) {
  1021. IWL_ERR(mvm, "Invalid station id\n");
  1022. return -EINVAL;
  1023. }
  1024. }
  1025. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1026. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1027. return -EINVAL;
  1028. if (!have_key_offset) {
  1029. /*
  1030. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  1031. * configure it there. As a result, this workaround exists to
  1032. * let the caller set the key offset (hw_key_idx), see d3.c.
  1033. */
  1034. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  1035. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  1036. return -ENOSPC;
  1037. }
  1038. switch (keyconf->cipher) {
  1039. case WLAN_CIPHER_SUITE_TKIP:
  1040. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  1041. /* get phase 1 key from mac80211 */
  1042. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1043. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1044. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1045. seq.tkip.iv32, p1k, 0);
  1046. break;
  1047. case WLAN_CIPHER_SUITE_CCMP:
  1048. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1049. 0, NULL, 0);
  1050. break;
  1051. default:
  1052. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
  1053. sta_id, 0, NULL, 0);
  1054. }
  1055. if (ret)
  1056. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1057. end:
  1058. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1059. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1060. sta->addr, ret);
  1061. return ret;
  1062. }
  1063. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  1064. struct ieee80211_vif *vif,
  1065. struct ieee80211_sta *sta,
  1066. struct ieee80211_key_conf *keyconf)
  1067. {
  1068. struct iwl_mvm_sta *mvm_sta;
  1069. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1070. __le16 key_flags;
  1071. int ret, status;
  1072. u8 sta_id;
  1073. lockdep_assert_held(&mvm->mutex);
  1074. /* Get the station id from the mvm local station table */
  1075. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1076. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  1077. keyconf->keyidx, sta_id);
  1078. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  1079. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1080. ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1081. if (!ret) {
  1082. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1083. keyconf->hw_key_idx);
  1084. return -ENOENT;
  1085. }
  1086. if (sta_id == IWL_MVM_STATION_COUNT) {
  1087. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1088. return 0;
  1089. }
  1090. /*
  1091. * It is possible that the 'sta' parameter is NULL, and thus
  1092. * there is a need to retrieve the sta from the local station table,
  1093. * for example when a GTK is removed (where the sta_id will then be
  1094. * the AP ID, and no station was passed by mac80211.)
  1095. */
  1096. if (!sta) {
  1097. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1098. lockdep_is_held(&mvm->mutex));
  1099. if (!sta) {
  1100. IWL_ERR(mvm, "Invalid station id\n");
  1101. return -EINVAL;
  1102. }
  1103. }
  1104. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1105. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1106. return -EINVAL;
  1107. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1108. STA_KEY_FLG_KEYID_MSK);
  1109. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1110. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1111. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1112. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1113. cmd.key_flags = key_flags;
  1114. cmd.key_offset = keyconf->hw_key_idx;
  1115. cmd.sta_id = sta_id;
  1116. status = ADD_STA_SUCCESS;
  1117. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  1118. &cmd, &status);
  1119. switch (status) {
  1120. case ADD_STA_SUCCESS:
  1121. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1122. break;
  1123. default:
  1124. ret = -EIO;
  1125. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1126. break;
  1127. }
  1128. return ret;
  1129. }
  1130. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1131. struct ieee80211_vif *vif,
  1132. struct ieee80211_key_conf *keyconf,
  1133. struct ieee80211_sta *sta, u32 iv32,
  1134. u16 *phase1key)
  1135. {
  1136. struct iwl_mvm_sta *mvm_sta;
  1137. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1138. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  1139. return;
  1140. rcu_read_lock();
  1141. if (!sta) {
  1142. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1143. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  1144. rcu_read_unlock();
  1145. return;
  1146. }
  1147. }
  1148. mvm_sta = (void *)sta->drv_priv;
  1149. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1150. iv32, phase1key, CMD_ASYNC);
  1151. rcu_read_unlock();
  1152. }
  1153. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1154. struct ieee80211_sta *sta)
  1155. {
  1156. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1157. struct iwl_mvm_add_sta_cmd cmd = {
  1158. .add_modify = STA_MODE_MODIFY,
  1159. .sta_id = mvmsta->sta_id,
  1160. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1161. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1162. };
  1163. int ret;
  1164. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1165. if (ret)
  1166. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1167. }
  1168. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1169. struct ieee80211_sta *sta,
  1170. enum ieee80211_frame_release_type reason,
  1171. u16 cnt, u16 tids, bool more_data,
  1172. bool agg)
  1173. {
  1174. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1175. struct iwl_mvm_add_sta_cmd cmd = {
  1176. .add_modify = STA_MODE_MODIFY,
  1177. .sta_id = mvmsta->sta_id,
  1178. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1179. .sleep_tx_count = cpu_to_le16(cnt),
  1180. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1181. };
  1182. int tid, ret;
  1183. unsigned long _tids = tids;
  1184. /* convert TIDs to ACs - we don't support TSPEC so that's OK
  1185. * Note that this field is reserved and unused by firmware not
  1186. * supporting GO uAPSD, so it's safe to always do this.
  1187. */
  1188. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
  1189. cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
  1190. /* If we're releasing frames from aggregation queues then check if the
  1191. * all queues combined that we're releasing frames from have
  1192. * - more frames than the service period, in which case more_data
  1193. * needs to be set
  1194. * - fewer than 'cnt' frames, in which case we need to adjust the
  1195. * firmware command (but do that unconditionally)
  1196. */
  1197. if (agg) {
  1198. int remaining = cnt;
  1199. spin_lock_bh(&mvmsta->lock);
  1200. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
  1201. struct iwl_mvm_tid_data *tid_data;
  1202. u16 n_queued;
  1203. tid_data = &mvmsta->tid_data[tid];
  1204. if (WARN(tid_data->state != IWL_AGG_ON &&
  1205. tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
  1206. "TID %d state is %d\n",
  1207. tid, tid_data->state)) {
  1208. spin_unlock_bh(&mvmsta->lock);
  1209. ieee80211_sta_eosp(sta);
  1210. return;
  1211. }
  1212. n_queued = iwl_mvm_tid_queued(tid_data);
  1213. if (n_queued > remaining) {
  1214. more_data = true;
  1215. remaining = 0;
  1216. break;
  1217. }
  1218. remaining -= n_queued;
  1219. }
  1220. spin_unlock_bh(&mvmsta->lock);
  1221. cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
  1222. if (WARN_ON(cnt - remaining == 0)) {
  1223. ieee80211_sta_eosp(sta);
  1224. return;
  1225. }
  1226. }
  1227. /* Note: this is ignored by firmware not supporting GO uAPSD */
  1228. if (more_data)
  1229. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
  1230. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
  1231. mvmsta->next_status_eosp = true;
  1232. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
  1233. } else {
  1234. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
  1235. }
  1236. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1237. if (ret)
  1238. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1239. }
  1240. int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
  1241. struct iwl_rx_cmd_buffer *rxb,
  1242. struct iwl_device_cmd *cmd)
  1243. {
  1244. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1245. struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
  1246. struct ieee80211_sta *sta;
  1247. u32 sta_id = le32_to_cpu(notif->sta_id);
  1248. if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
  1249. return 0;
  1250. rcu_read_lock();
  1251. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1252. if (!IS_ERR_OR_NULL(sta))
  1253. ieee80211_sta_eosp(sta);
  1254. rcu_read_unlock();
  1255. return 0;
  1256. }
  1257. void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
  1258. struct iwl_mvm_sta *mvmsta, bool disable)
  1259. {
  1260. struct iwl_mvm_add_sta_cmd cmd = {
  1261. .add_modify = STA_MODE_MODIFY,
  1262. .sta_id = mvmsta->sta_id,
  1263. .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
  1264. .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
  1265. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1266. };
  1267. int ret;
  1268. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_DISABLE_STA_TX))
  1269. return;
  1270. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1271. if (ret)
  1272. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1273. }
  1274. void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
  1275. struct ieee80211_sta *sta,
  1276. bool disable)
  1277. {
  1278. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1279. spin_lock_bh(&mvm_sta->lock);
  1280. if (mvm_sta->disable_tx == disable) {
  1281. spin_unlock_bh(&mvm_sta->lock);
  1282. return;
  1283. }
  1284. mvm_sta->disable_tx = disable;
  1285. /*
  1286. * Tell mac80211 to start/stop queueing tx for this station,
  1287. * but don't stop queueing if there are still pending frames
  1288. * for this station.
  1289. */
  1290. if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
  1291. ieee80211_sta_block_awake(mvm->hw, sta, disable);
  1292. iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
  1293. spin_unlock_bh(&mvm_sta->lock);
  1294. }
  1295. void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
  1296. struct iwl_mvm_vif *mvmvif,
  1297. bool disable)
  1298. {
  1299. struct ieee80211_sta *sta;
  1300. struct iwl_mvm_sta *mvm_sta;
  1301. int i;
  1302. lockdep_assert_held(&mvm->mutex);
  1303. /* Block/unblock all the stations of the given mvmvif */
  1304. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  1305. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  1306. lockdep_is_held(&mvm->mutex));
  1307. if (IS_ERR_OR_NULL(sta))
  1308. continue;
  1309. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1310. if (mvm_sta->mac_id_n_color !=
  1311. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
  1312. continue;
  1313. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
  1314. }
  1315. }