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