sta.c 35 KB

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