sta.c 39 KB

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