sta.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 - 2013 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 - 2013 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. {
  69. int sta_id;
  70. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  71. lockdep_assert_held(&mvm->mutex);
  72. /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
  73. for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++)
  74. if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  75. lockdep_is_held(&mvm->mutex)))
  76. return sta_id;
  77. return IWL_MVM_STATION_COUNT;
  78. }
  79. /* send station add/update command to firmware */
  80. int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  81. bool update)
  82. {
  83. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  84. struct iwl_mvm_add_sta_cmd add_sta_cmd;
  85. int ret;
  86. u32 status;
  87. u32 agg_size = 0, mpdu_dens = 0;
  88. memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
  89. add_sta_cmd.sta_id = mvm_sta->sta_id;
  90. add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  91. if (!update) {
  92. add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  93. memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
  94. }
  95. add_sta_cmd.add_modify = update ? 1 : 0;
  96. add_sta_cmd.station_flags_msk |= cpu_to_le32(STA_FLG_FAT_EN_MSK |
  97. STA_FLG_MIMO_EN_MSK);
  98. switch (sta->bandwidth) {
  99. case IEEE80211_STA_RX_BW_160:
  100. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
  101. /* fall through */
  102. case IEEE80211_STA_RX_BW_80:
  103. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
  104. /* fall through */
  105. case IEEE80211_STA_RX_BW_40:
  106. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
  107. /* fall through */
  108. case IEEE80211_STA_RX_BW_20:
  109. if (sta->ht_cap.ht_supported)
  110. add_sta_cmd.station_flags |=
  111. cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
  112. break;
  113. }
  114. switch (sta->rx_nss) {
  115. case 1:
  116. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  117. break;
  118. case 2:
  119. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
  120. break;
  121. case 3 ... 8:
  122. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
  123. break;
  124. }
  125. switch (sta->smps_mode) {
  126. case IEEE80211_SMPS_AUTOMATIC:
  127. case IEEE80211_SMPS_NUM_MODES:
  128. WARN_ON(1);
  129. break;
  130. case IEEE80211_SMPS_STATIC:
  131. /* override NSS */
  132. add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
  133. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  134. break;
  135. case IEEE80211_SMPS_DYNAMIC:
  136. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
  137. break;
  138. case IEEE80211_SMPS_OFF:
  139. /* nothing */
  140. break;
  141. }
  142. if (sta->ht_cap.ht_supported) {
  143. add_sta_cmd.station_flags_msk |=
  144. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  145. STA_FLG_AGG_MPDU_DENS_MSK);
  146. mpdu_dens = sta->ht_cap.ampdu_density;
  147. }
  148. if (sta->vht_cap.vht_supported) {
  149. agg_size = sta->vht_cap.cap &
  150. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  151. agg_size >>=
  152. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  153. } else if (sta->ht_cap.ht_supported) {
  154. agg_size = sta->ht_cap.ampdu_factor;
  155. }
  156. add_sta_cmd.station_flags |=
  157. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  158. add_sta_cmd.station_flags |=
  159. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  160. status = ADD_STA_SUCCESS;
  161. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
  162. &add_sta_cmd, &status);
  163. if (ret)
  164. return ret;
  165. switch (status) {
  166. case ADD_STA_SUCCESS:
  167. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  168. break;
  169. default:
  170. ret = -EIO;
  171. IWL_ERR(mvm, "ADD_STA failed\n");
  172. break;
  173. }
  174. return ret;
  175. }
  176. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  177. struct ieee80211_vif *vif,
  178. struct ieee80211_sta *sta)
  179. {
  180. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  181. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  182. int i, ret, sta_id;
  183. lockdep_assert_held(&mvm->mutex);
  184. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  185. sta_id = iwl_mvm_find_free_sta_id(mvm);
  186. else
  187. sta_id = mvm_sta->sta_id;
  188. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  189. return -ENOSPC;
  190. spin_lock_init(&mvm_sta->lock);
  191. mvm_sta->sta_id = sta_id;
  192. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  193. mvmvif->color);
  194. mvm_sta->vif = vif;
  195. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  196. mvm_sta->tx_protection = 0;
  197. mvm_sta->tt_tx_protection = false;
  198. /* HW restart, don't assume the memory has been zeroed */
  199. atomic_set(&mvm->pending_frames[sta_id], 0);
  200. mvm_sta->tid_disable_agg = 0;
  201. mvm_sta->tfd_queue_msk = 0;
  202. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  203. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  204. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  205. /* for HW restart - need to reset the seq_number etc... */
  206. memset(mvm_sta->tid_data, 0, sizeof(mvm_sta->tid_data));
  207. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
  208. if (ret)
  209. return ret;
  210. /* The first station added is the AP, the others are TDLS STAs */
  211. if (vif->type == NL80211_IFTYPE_STATION &&
  212. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  213. mvmvif->ap_sta_id = sta_id;
  214. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  215. return 0;
  216. }
  217. int iwl_mvm_update_sta(struct iwl_mvm *mvm,
  218. struct ieee80211_vif *vif,
  219. struct ieee80211_sta *sta)
  220. {
  221. return iwl_mvm_sta_send_to_fw(mvm, sta, true);
  222. }
  223. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  224. bool drain)
  225. {
  226. struct iwl_mvm_add_sta_cmd cmd = {};
  227. int ret;
  228. u32 status;
  229. lockdep_assert_held(&mvm->mutex);
  230. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  231. cmd.sta_id = mvmsta->sta_id;
  232. cmd.add_modify = STA_MODE_MODIFY;
  233. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  234. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  235. status = ADD_STA_SUCCESS;
  236. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  237. &cmd, &status);
  238. if (ret)
  239. return ret;
  240. switch (status) {
  241. case ADD_STA_SUCCESS:
  242. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  243. mvmsta->sta_id);
  244. break;
  245. default:
  246. ret = -EIO;
  247. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  248. mvmsta->sta_id);
  249. break;
  250. }
  251. return ret;
  252. }
  253. /*
  254. * Remove a station from the FW table. Before sending the command to remove
  255. * the station validate that the station is indeed known to the driver (sanity
  256. * only).
  257. */
  258. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  259. {
  260. struct ieee80211_sta *sta;
  261. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  262. .sta_id = sta_id,
  263. };
  264. int ret;
  265. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  266. lockdep_is_held(&mvm->mutex));
  267. /* Note: internal stations are marked as error values */
  268. if (!sta) {
  269. IWL_ERR(mvm, "Invalid station id\n");
  270. return -EINVAL;
  271. }
  272. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
  273. sizeof(rm_sta_cmd), &rm_sta_cmd);
  274. if (ret) {
  275. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  281. {
  282. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  283. u8 sta_id;
  284. /*
  285. * The mutex is needed because of the SYNC cmd, but not only: if the
  286. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  287. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  288. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  289. * that later.
  290. */
  291. mutex_lock(&mvm->mutex);
  292. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  293. int ret;
  294. struct ieee80211_sta *sta =
  295. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  296. lockdep_is_held(&mvm->mutex));
  297. /* This station is in use */
  298. if (!IS_ERR(sta))
  299. continue;
  300. if (PTR_ERR(sta) == -EINVAL) {
  301. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  302. sta_id);
  303. continue;
  304. }
  305. if (!sta) {
  306. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  307. sta_id);
  308. continue;
  309. }
  310. WARN_ON(PTR_ERR(sta) != -EBUSY);
  311. /* This station was removed and we waited until it got drained,
  312. * we can now proceed and remove it.
  313. */
  314. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  315. if (ret) {
  316. IWL_ERR(mvm,
  317. "Couldn't remove sta %d after it was drained\n",
  318. sta_id);
  319. continue;
  320. }
  321. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
  322. clear_bit(sta_id, mvm->sta_drained);
  323. }
  324. mutex_unlock(&mvm->mutex);
  325. }
  326. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  327. struct ieee80211_vif *vif,
  328. struct ieee80211_sta *sta)
  329. {
  330. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  331. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  332. int ret;
  333. lockdep_assert_held(&mvm->mutex);
  334. if (vif->type == NL80211_IFTYPE_STATION &&
  335. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  336. /* flush its queues here since we are freeing mvm_sta */
  337. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
  338. /*
  339. * Put a non-NULL since the fw station isn't removed.
  340. * It will be removed after the MAC will be set as
  341. * unassoc.
  342. */
  343. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  344. ERR_PTR(-EINVAL));
  345. /* if we are associated - we can't remove the AP STA now */
  346. if (vif->bss_conf.assoc)
  347. return ret;
  348. /* unassoc - go ahead - remove the AP STA now */
  349. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  350. }
  351. /*
  352. * Make sure that the tx response code sees the station as -EBUSY and
  353. * calls the drain worker.
  354. */
  355. spin_lock_bh(&mvm_sta->lock);
  356. /*
  357. * There are frames pending on the AC queues for this station.
  358. * We need to wait until all the frames are drained...
  359. */
  360. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  361. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  362. ERR_PTR(-EBUSY));
  363. spin_unlock_bh(&mvm_sta->lock);
  364. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  365. } else {
  366. spin_unlock_bh(&mvm_sta->lock);
  367. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  368. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  369. }
  370. return ret;
  371. }
  372. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  373. struct ieee80211_vif *vif,
  374. u8 sta_id)
  375. {
  376. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  377. lockdep_assert_held(&mvm->mutex);
  378. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
  379. return ret;
  380. }
  381. int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
  382. u32 qmask)
  383. {
  384. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  385. sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
  386. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  387. return -ENOSPC;
  388. }
  389. sta->tfd_queue_msk = qmask;
  390. /* put a non-NULL value so iterating over the stations won't stop */
  391. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  392. return 0;
  393. }
  394. void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
  395. {
  396. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  397. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  398. sta->sta_id = IWL_MVM_STATION_COUNT;
  399. }
  400. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  401. struct iwl_mvm_int_sta *sta,
  402. const u8 *addr,
  403. u16 mac_id, u16 color)
  404. {
  405. struct iwl_mvm_add_sta_cmd cmd;
  406. int ret;
  407. u32 status;
  408. lockdep_assert_held(&mvm->mutex);
  409. memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd));
  410. cmd.sta_id = sta->sta_id;
  411. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  412. color));
  413. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  414. if (addr)
  415. memcpy(cmd.addr, addr, ETH_ALEN);
  416. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  417. &cmd, &status);
  418. if (ret)
  419. return ret;
  420. switch (status) {
  421. case ADD_STA_SUCCESS:
  422. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  423. return 0;
  424. default:
  425. ret = -EIO;
  426. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  427. status);
  428. break;
  429. }
  430. return ret;
  431. }
  432. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  433. {
  434. int ret;
  435. lockdep_assert_held(&mvm->mutex);
  436. /* Add the aux station, but without any queues */
  437. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
  438. if (ret)
  439. return ret;
  440. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  441. MAC_INDEX_AUX, 0);
  442. if (ret)
  443. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  444. return ret;
  445. }
  446. /*
  447. * Send the add station command for the vif's broadcast station.
  448. * Assumes that the station was already allocated.
  449. *
  450. * @mvm: the mvm component
  451. * @vif: the interface to which the broadcast station is added
  452. * @bsta: the broadcast station to add.
  453. */
  454. int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  455. struct iwl_mvm_int_sta *bsta)
  456. {
  457. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  458. static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  459. lockdep_assert_held(&mvm->mutex);
  460. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  461. return -ENOSPC;
  462. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  463. mvmvif->id, mvmvif->color);
  464. }
  465. /* Send the FW a request to remove the station from it's internal data
  466. * structures, but DO NOT remove the entry from the local data structures. */
  467. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
  468. struct iwl_mvm_int_sta *bsta)
  469. {
  470. int ret;
  471. lockdep_assert_held(&mvm->mutex);
  472. ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
  473. if (ret)
  474. IWL_WARN(mvm, "Failed sending remove station\n");
  475. return ret;
  476. }
  477. /* Allocate a new station entry for the broadcast station to the given vif,
  478. * and send it to the FW.
  479. * Note that each P2P mac should have its own broadcast station.
  480. *
  481. * @mvm: the mvm component
  482. * @vif: the interface to which the broadcast station is added
  483. * @bsta: the broadcast station to add. */
  484. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  485. struct iwl_mvm_int_sta *bsta)
  486. {
  487. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  488. static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  489. u32 qmask;
  490. int ret;
  491. lockdep_assert_held(&mvm->mutex);
  492. qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
  493. ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
  494. if (ret)
  495. return ret;
  496. ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  497. mvmvif->id, mvmvif->color);
  498. if (ret)
  499. iwl_mvm_dealloc_int_sta(mvm, bsta);
  500. return ret;
  501. }
  502. /*
  503. * Send the FW a request to remove the station from it's internal data
  504. * structures, and in addition remove it from the local data structure.
  505. */
  506. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
  507. {
  508. int ret;
  509. lockdep_assert_held(&mvm->mutex);
  510. ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
  511. if (ret)
  512. return ret;
  513. iwl_mvm_dealloc_int_sta(mvm, bsta);
  514. return ret;
  515. }
  516. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  517. int tid, u16 ssn, bool start)
  518. {
  519. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  520. struct iwl_mvm_add_sta_cmd cmd = {};
  521. int ret;
  522. u32 status;
  523. lockdep_assert_held(&mvm->mutex);
  524. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  525. cmd.sta_id = mvm_sta->sta_id;
  526. cmd.add_modify = STA_MODE_MODIFY;
  527. if (start) {
  528. cmd.add_immediate_ba_tid = (u8) tid;
  529. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  530. } else {
  531. cmd.remove_immediate_ba_tid = (u8) tid;
  532. }
  533. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  534. STA_MODIFY_REMOVE_BA_TID;
  535. status = ADD_STA_SUCCESS;
  536. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  537. &cmd, &status);
  538. if (ret)
  539. return ret;
  540. switch (status) {
  541. case ADD_STA_SUCCESS:
  542. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  543. start ? "start" : "stopp");
  544. break;
  545. case ADD_STA_IMMEDIATE_BA_FAILURE:
  546. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  547. ret = -ENOSPC;
  548. break;
  549. default:
  550. ret = -EIO;
  551. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  552. start ? "start" : "stopp", status);
  553. break;
  554. }
  555. return ret;
  556. }
  557. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  558. int tid, u8 queue, bool start)
  559. {
  560. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  561. struct iwl_mvm_add_sta_cmd cmd = {};
  562. int ret;
  563. u32 status;
  564. lockdep_assert_held(&mvm->mutex);
  565. if (start) {
  566. mvm_sta->tfd_queue_msk |= BIT(queue);
  567. mvm_sta->tid_disable_agg &= ~BIT(tid);
  568. } else {
  569. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  570. mvm_sta->tid_disable_agg |= BIT(tid);
  571. }
  572. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  573. cmd.sta_id = mvm_sta->sta_id;
  574. cmd.add_modify = STA_MODE_MODIFY;
  575. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  576. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  577. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  578. status = ADD_STA_SUCCESS;
  579. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  580. &cmd, &status);
  581. if (ret)
  582. return ret;
  583. switch (status) {
  584. case ADD_STA_SUCCESS:
  585. break;
  586. default:
  587. ret = -EIO;
  588. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  589. start ? "start" : "stopp", status);
  590. break;
  591. }
  592. return ret;
  593. }
  594. static const u8 tid_to_ac[] = {
  595. IEEE80211_AC_BE,
  596. IEEE80211_AC_BK,
  597. IEEE80211_AC_BK,
  598. IEEE80211_AC_BE,
  599. IEEE80211_AC_VI,
  600. IEEE80211_AC_VI,
  601. IEEE80211_AC_VO,
  602. IEEE80211_AC_VO,
  603. };
  604. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  605. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  606. {
  607. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  608. struct iwl_mvm_tid_data *tid_data;
  609. int txq_id;
  610. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  611. return -EINVAL;
  612. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  613. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  614. mvmsta->tid_data[tid].state);
  615. return -ENXIO;
  616. }
  617. lockdep_assert_held(&mvm->mutex);
  618. for (txq_id = IWL_MVM_FIRST_AGG_QUEUE;
  619. txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++)
  620. if (mvm->queue_to_mac80211[txq_id] ==
  621. IWL_INVALID_MAC80211_QUEUE)
  622. break;
  623. if (txq_id > IWL_MVM_LAST_AGG_QUEUE) {
  624. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  625. return -EIO;
  626. }
  627. /* the new tx queue is still connected to the same mac80211 queue */
  628. mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
  629. spin_lock_bh(&mvmsta->lock);
  630. tid_data = &mvmsta->tid_data[tid];
  631. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  632. tid_data->txq_id = txq_id;
  633. *ssn = tid_data->ssn;
  634. IWL_DEBUG_TX_QUEUES(mvm,
  635. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  636. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  637. tid_data->next_reclaimed);
  638. if (tid_data->ssn == tid_data->next_reclaimed) {
  639. tid_data->state = IWL_AGG_STARTING;
  640. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  641. } else {
  642. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  643. }
  644. spin_unlock_bh(&mvmsta->lock);
  645. return 0;
  646. }
  647. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  648. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  649. {
  650. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  651. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  652. int queue, fifo, ret;
  653. u16 ssn;
  654. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  655. spin_lock_bh(&mvmsta->lock);
  656. ssn = tid_data->ssn;
  657. queue = tid_data->txq_id;
  658. tid_data->state = IWL_AGG_ON;
  659. tid_data->ssn = 0xffff;
  660. spin_unlock_bh(&mvmsta->lock);
  661. fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
  662. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  663. if (ret)
  664. return -EIO;
  665. iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
  666. buf_size, ssn);
  667. /*
  668. * Even though in theory the peer could have different
  669. * aggregation reorder buffer sizes for different sessions,
  670. * our ucode doesn't allow for that and has a global limit
  671. * for each station. Therefore, use the minimum of all the
  672. * aggregation sessions and our default value.
  673. */
  674. mvmsta->max_agg_bufsize =
  675. min(mvmsta->max_agg_bufsize, buf_size);
  676. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  677. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  678. sta->addr, tid);
  679. if (mvm->cfg->ht_params->use_rts_for_aggregation) {
  680. /*
  681. * switch to RTS/CTS if it is the prefer protection
  682. * method for HT traffic
  683. * this function also sends the LQ command
  684. */
  685. return iwl_mvm_tx_protection(mvm, &mvmsta->lq_sta.lq,
  686. mvmsta, true);
  687. /*
  688. * TODO: remove the TLC_RTS flag when we tear down the last
  689. * AGG session (agg_tids_count in DVM)
  690. */
  691. }
  692. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
  693. }
  694. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  695. struct ieee80211_sta *sta, u16 tid)
  696. {
  697. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  698. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  699. u16 txq_id;
  700. int err;
  701. /*
  702. * If mac80211 is cleaning its state, then say that we finished since
  703. * our state has been cleared anyway.
  704. */
  705. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  706. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  707. return 0;
  708. }
  709. spin_lock_bh(&mvmsta->lock);
  710. txq_id = tid_data->txq_id;
  711. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  712. mvmsta->sta_id, tid, txq_id, tid_data->state);
  713. switch (tid_data->state) {
  714. case IWL_AGG_ON:
  715. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  716. IWL_DEBUG_TX_QUEUES(mvm,
  717. "ssn = %d, next_recl = %d\n",
  718. tid_data->ssn, tid_data->next_reclaimed);
  719. /* There are still packets for this RA / TID in the HW */
  720. if (tid_data->ssn != tid_data->next_reclaimed) {
  721. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  722. err = 0;
  723. break;
  724. }
  725. tid_data->ssn = 0xffff;
  726. iwl_trans_txq_disable(mvm->trans, txq_id);
  727. /* fall through */
  728. case IWL_AGG_STARTING:
  729. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  730. /*
  731. * The agg session has been stopped before it was set up. This
  732. * can happen when the AddBA timer times out for example.
  733. */
  734. /* No barriers since we are under mutex */
  735. lockdep_assert_held(&mvm->mutex);
  736. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  737. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  738. tid_data->state = IWL_AGG_OFF;
  739. err = 0;
  740. break;
  741. default:
  742. IWL_ERR(mvm,
  743. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  744. mvmsta->sta_id, tid, tid_data->state);
  745. IWL_ERR(mvm,
  746. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  747. err = -EINVAL;
  748. }
  749. spin_unlock_bh(&mvmsta->lock);
  750. return err;
  751. }
  752. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  753. struct ieee80211_sta *sta, u16 tid)
  754. {
  755. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  756. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  757. u16 txq_id;
  758. /*
  759. * First set the agg state to OFF to avoid calling
  760. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  761. */
  762. spin_lock_bh(&mvmsta->lock);
  763. txq_id = tid_data->txq_id;
  764. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  765. mvmsta->sta_id, tid, txq_id, tid_data->state);
  766. tid_data->state = IWL_AGG_OFF;
  767. spin_unlock_bh(&mvmsta->lock);
  768. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
  769. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  770. iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
  771. mvm->queue_to_mac80211[tid_data->txq_id] =
  772. IWL_INVALID_MAC80211_QUEUE;
  773. return 0;
  774. }
  775. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  776. {
  777. int i;
  778. lockdep_assert_held(&mvm->mutex);
  779. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  780. if (i == STA_KEY_MAX_NUM)
  781. return STA_KEY_IDX_INVALID;
  782. __set_bit(i, mvm->fw_key_table);
  783. return i;
  784. }
  785. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  786. struct ieee80211_sta *sta)
  787. {
  788. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  789. if (sta) {
  790. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  791. return mvm_sta->sta_id;
  792. }
  793. /*
  794. * The device expects GTKs for station interfaces to be
  795. * installed as GTKs for the AP station. If we have no
  796. * station ID, then use AP's station ID.
  797. */
  798. if (vif->type == NL80211_IFTYPE_STATION &&
  799. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  800. return mvmvif->ap_sta_id;
  801. return IWL_MVM_STATION_COUNT;
  802. }
  803. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  804. struct iwl_mvm_sta *mvm_sta,
  805. struct ieee80211_key_conf *keyconf,
  806. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  807. u32 cmd_flags)
  808. {
  809. __le16 key_flags;
  810. struct iwl_mvm_add_sta_cmd cmd = {};
  811. int ret, status;
  812. u16 keyidx;
  813. int i;
  814. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  815. STA_KEY_FLG_KEYID_MSK;
  816. key_flags = cpu_to_le16(keyidx);
  817. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  818. switch (keyconf->cipher) {
  819. case WLAN_CIPHER_SUITE_TKIP:
  820. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  821. cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
  822. for (i = 0; i < 5; i++)
  823. cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  824. memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
  825. break;
  826. case WLAN_CIPHER_SUITE_CCMP:
  827. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  828. memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
  829. break;
  830. default:
  831. WARN_ON(1);
  832. return -EINVAL;
  833. }
  834. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  835. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  836. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  837. cmd.key.key_offset = keyconf->hw_key_idx;
  838. cmd.key.key_flags = key_flags;
  839. cmd.add_modify = STA_MODE_MODIFY;
  840. cmd.modify_mask = STA_MODIFY_KEY;
  841. cmd.sta_id = sta_id;
  842. status = ADD_STA_SUCCESS;
  843. if (cmd_flags == CMD_SYNC)
  844. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  845. &cmd, &status);
  846. else
  847. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  848. sizeof(cmd), &cmd);
  849. switch (status) {
  850. case ADD_STA_SUCCESS:
  851. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  852. break;
  853. default:
  854. ret = -EIO;
  855. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  856. break;
  857. }
  858. return ret;
  859. }
  860. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  861. struct ieee80211_key_conf *keyconf,
  862. u8 sta_id, bool remove_key)
  863. {
  864. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  865. /* verify the key details match the required command's expectations */
  866. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  867. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  868. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  869. return -EINVAL;
  870. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  871. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  872. if (remove_key) {
  873. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  874. } else {
  875. struct ieee80211_key_seq seq;
  876. const u8 *pn;
  877. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  878. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  879. igtk_cmd.K1, igtk_cmd.K2);
  880. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  881. pn = seq.aes_cmac.pn;
  882. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  883. ((u64) pn[4] << 8) |
  884. ((u64) pn[3] << 16) |
  885. ((u64) pn[2] << 24) |
  886. ((u64) pn[1] << 32) |
  887. ((u64) pn[0] << 40));
  888. }
  889. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  890. remove_key ? "removing" : "installing",
  891. igtk_cmd.sta_id);
  892. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
  893. sizeof(igtk_cmd), &igtk_cmd);
  894. }
  895. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  896. struct ieee80211_vif *vif,
  897. struct ieee80211_sta *sta)
  898. {
  899. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  900. if (sta)
  901. return sta->addr;
  902. if (vif->type == NL80211_IFTYPE_STATION &&
  903. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  904. u8 sta_id = mvmvif->ap_sta_id;
  905. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  906. lockdep_is_held(&mvm->mutex));
  907. return sta->addr;
  908. }
  909. return NULL;
  910. }
  911. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  912. struct ieee80211_vif *vif,
  913. struct ieee80211_sta *sta,
  914. struct ieee80211_key_conf *keyconf,
  915. bool have_key_offset)
  916. {
  917. struct iwl_mvm_sta *mvm_sta;
  918. int ret;
  919. u8 *addr, sta_id;
  920. struct ieee80211_key_seq seq;
  921. u16 p1k[5];
  922. lockdep_assert_held(&mvm->mutex);
  923. /* Get the station id from the mvm local station table */
  924. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  925. if (sta_id == IWL_MVM_STATION_COUNT) {
  926. IWL_ERR(mvm, "Failed to find station id\n");
  927. return -EINVAL;
  928. }
  929. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  930. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  931. goto end;
  932. }
  933. /*
  934. * It is possible that the 'sta' parameter is NULL, and thus
  935. * there is a need to retrieve the sta from the local station table.
  936. */
  937. if (!sta) {
  938. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  939. lockdep_is_held(&mvm->mutex));
  940. if (IS_ERR_OR_NULL(sta)) {
  941. IWL_ERR(mvm, "Invalid station id\n");
  942. return -EINVAL;
  943. }
  944. }
  945. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  946. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  947. return -EINVAL;
  948. if (!have_key_offset) {
  949. /*
  950. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  951. * configure it there. As a result, this workaround exists to
  952. * let the caller set the key offset (hw_key_idx), see d3.c.
  953. */
  954. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  955. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  956. return -ENOSPC;
  957. }
  958. switch (keyconf->cipher) {
  959. case WLAN_CIPHER_SUITE_TKIP:
  960. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  961. /* get phase 1 key from mac80211 */
  962. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  963. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  964. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  965. seq.tkip.iv32, p1k, CMD_SYNC);
  966. break;
  967. case WLAN_CIPHER_SUITE_CCMP:
  968. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  969. 0, NULL, CMD_SYNC);
  970. break;
  971. default:
  972. IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
  973. ret = -EINVAL;
  974. }
  975. if (ret)
  976. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  977. end:
  978. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  979. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  980. sta->addr, ret);
  981. return ret;
  982. }
  983. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  984. struct ieee80211_vif *vif,
  985. struct ieee80211_sta *sta,
  986. struct ieee80211_key_conf *keyconf)
  987. {
  988. struct iwl_mvm_sta *mvm_sta;
  989. struct iwl_mvm_add_sta_cmd cmd = {};
  990. __le16 key_flags;
  991. int ret, status;
  992. u8 sta_id;
  993. lockdep_assert_held(&mvm->mutex);
  994. /* Get the station id from the mvm local station table */
  995. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  996. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  997. keyconf->keyidx, sta_id);
  998. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  999. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1000. ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1001. if (!ret) {
  1002. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1003. keyconf->hw_key_idx);
  1004. return -ENOENT;
  1005. }
  1006. if (sta_id == IWL_MVM_STATION_COUNT) {
  1007. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1008. return 0;
  1009. }
  1010. /*
  1011. * It is possible that the 'sta' parameter is NULL, and thus
  1012. * there is a need to retrieve the sta from the local station table,
  1013. * for example when a GTK is removed (where the sta_id will then be
  1014. * the AP ID, and no station was passed by mac80211.)
  1015. */
  1016. if (!sta) {
  1017. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1018. lockdep_is_held(&mvm->mutex));
  1019. if (!sta) {
  1020. IWL_ERR(mvm, "Invalid station id\n");
  1021. return -EINVAL;
  1022. }
  1023. }
  1024. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1025. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1026. return -EINVAL;
  1027. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1028. STA_KEY_FLG_KEYID_MSK);
  1029. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1030. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1031. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1032. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1033. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  1034. cmd.key.key_flags = key_flags;
  1035. cmd.key.key_offset = keyconf->hw_key_idx;
  1036. cmd.sta_id = sta_id;
  1037. cmd.modify_mask = STA_MODIFY_KEY;
  1038. cmd.add_modify = STA_MODE_MODIFY;
  1039. status = ADD_STA_SUCCESS;
  1040. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  1041. &cmd, &status);
  1042. switch (status) {
  1043. case ADD_STA_SUCCESS:
  1044. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1045. break;
  1046. default:
  1047. ret = -EIO;
  1048. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1049. break;
  1050. }
  1051. return ret;
  1052. }
  1053. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1054. struct ieee80211_vif *vif,
  1055. struct ieee80211_key_conf *keyconf,
  1056. struct ieee80211_sta *sta, u32 iv32,
  1057. u16 *phase1key)
  1058. {
  1059. struct iwl_mvm_sta *mvm_sta;
  1060. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1061. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  1062. return;
  1063. rcu_read_lock();
  1064. if (!sta) {
  1065. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1066. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  1067. rcu_read_unlock();
  1068. return;
  1069. }
  1070. }
  1071. mvm_sta = (void *)sta->drv_priv;
  1072. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1073. iv32, phase1key, CMD_ASYNC);
  1074. rcu_read_unlock();
  1075. }
  1076. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1077. struct ieee80211_sta *sta)
  1078. {
  1079. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  1080. struct iwl_mvm_add_sta_cmd cmd = {
  1081. .add_modify = STA_MODE_MODIFY,
  1082. .sta_id = mvmsta->sta_id,
  1083. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1084. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1085. };
  1086. int ret;
  1087. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1088. if (ret)
  1089. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1090. }
  1091. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1092. struct ieee80211_sta *sta,
  1093. enum ieee80211_frame_release_type reason,
  1094. u16 cnt)
  1095. {
  1096. u16 sleep_state_flags =
  1097. (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
  1098. STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
  1099. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  1100. struct iwl_mvm_add_sta_cmd cmd = {
  1101. .add_modify = STA_MODE_MODIFY,
  1102. .sta_id = mvmsta->sta_id,
  1103. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1104. .sleep_tx_count = cpu_to_le16(cnt),
  1105. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1106. /*
  1107. * Same modify mask for sleep_tx_count and sleep_state_flags so
  1108. * we must set the sleep_state_flags too.
  1109. */
  1110. .sleep_state_flags = cpu_to_le16(sleep_state_flags),
  1111. };
  1112. int ret;
  1113. /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
  1114. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1115. if (ret)
  1116. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1117. }