sta.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. #define IWL_MAX_RX_BA_SESSIONS 16
  517. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  518. int tid, u16 ssn, bool start)
  519. {
  520. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  521. struct iwl_mvm_add_sta_cmd cmd = {};
  522. int ret;
  523. u32 status;
  524. lockdep_assert_held(&mvm->mutex);
  525. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  526. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  527. return -ENOSPC;
  528. }
  529. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  530. cmd.sta_id = mvm_sta->sta_id;
  531. cmd.add_modify = STA_MODE_MODIFY;
  532. if (start) {
  533. cmd.add_immediate_ba_tid = (u8) tid;
  534. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  535. } else {
  536. cmd.remove_immediate_ba_tid = (u8) tid;
  537. }
  538. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  539. STA_MODIFY_REMOVE_BA_TID;
  540. status = ADD_STA_SUCCESS;
  541. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  542. &cmd, &status);
  543. if (ret)
  544. return ret;
  545. switch (status) {
  546. case ADD_STA_SUCCESS:
  547. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  548. start ? "start" : "stopp");
  549. break;
  550. case ADD_STA_IMMEDIATE_BA_FAILURE:
  551. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  552. ret = -ENOSPC;
  553. break;
  554. default:
  555. ret = -EIO;
  556. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  557. start ? "start" : "stopp", status);
  558. break;
  559. }
  560. if (!ret) {
  561. if (start)
  562. mvm->rx_ba_sessions++;
  563. else if (mvm->rx_ba_sessions > 0)
  564. /* check that restart flow didn't zero the counter */
  565. mvm->rx_ba_sessions--;
  566. }
  567. return ret;
  568. }
  569. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  570. int tid, u8 queue, bool start)
  571. {
  572. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  573. struct iwl_mvm_add_sta_cmd cmd = {};
  574. int ret;
  575. u32 status;
  576. lockdep_assert_held(&mvm->mutex);
  577. if (start) {
  578. mvm_sta->tfd_queue_msk |= BIT(queue);
  579. mvm_sta->tid_disable_agg &= ~BIT(tid);
  580. } else {
  581. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  582. mvm_sta->tid_disable_agg |= BIT(tid);
  583. }
  584. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  585. cmd.sta_id = mvm_sta->sta_id;
  586. cmd.add_modify = STA_MODE_MODIFY;
  587. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  588. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  589. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  590. status = ADD_STA_SUCCESS;
  591. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  592. &cmd, &status);
  593. if (ret)
  594. return ret;
  595. switch (status) {
  596. case ADD_STA_SUCCESS:
  597. break;
  598. default:
  599. ret = -EIO;
  600. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  601. start ? "start" : "stopp", status);
  602. break;
  603. }
  604. return ret;
  605. }
  606. static const u8 tid_to_ac[] = {
  607. IEEE80211_AC_BE,
  608. IEEE80211_AC_BK,
  609. IEEE80211_AC_BK,
  610. IEEE80211_AC_BE,
  611. IEEE80211_AC_VI,
  612. IEEE80211_AC_VI,
  613. IEEE80211_AC_VO,
  614. IEEE80211_AC_VO,
  615. };
  616. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  617. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  618. {
  619. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  620. struct iwl_mvm_tid_data *tid_data;
  621. int txq_id;
  622. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  623. return -EINVAL;
  624. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  625. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  626. mvmsta->tid_data[tid].state);
  627. return -ENXIO;
  628. }
  629. lockdep_assert_held(&mvm->mutex);
  630. for (txq_id = IWL_MVM_FIRST_AGG_QUEUE;
  631. txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++)
  632. if (mvm->queue_to_mac80211[txq_id] ==
  633. IWL_INVALID_MAC80211_QUEUE)
  634. break;
  635. if (txq_id > IWL_MVM_LAST_AGG_QUEUE) {
  636. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  637. return -EIO;
  638. }
  639. /* the new tx queue is still connected to the same mac80211 queue */
  640. mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
  641. spin_lock_bh(&mvmsta->lock);
  642. tid_data = &mvmsta->tid_data[tid];
  643. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  644. tid_data->txq_id = txq_id;
  645. *ssn = tid_data->ssn;
  646. IWL_DEBUG_TX_QUEUES(mvm,
  647. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  648. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  649. tid_data->next_reclaimed);
  650. if (tid_data->ssn == tid_data->next_reclaimed) {
  651. tid_data->state = IWL_AGG_STARTING;
  652. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  653. } else {
  654. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  655. }
  656. spin_unlock_bh(&mvmsta->lock);
  657. return 0;
  658. }
  659. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  660. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  661. {
  662. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  663. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  664. int queue, fifo, ret;
  665. u16 ssn;
  666. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  667. spin_lock_bh(&mvmsta->lock);
  668. ssn = tid_data->ssn;
  669. queue = tid_data->txq_id;
  670. tid_data->state = IWL_AGG_ON;
  671. tid_data->ssn = 0xffff;
  672. spin_unlock_bh(&mvmsta->lock);
  673. fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
  674. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  675. if (ret)
  676. return -EIO;
  677. iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
  678. buf_size, ssn);
  679. /*
  680. * Even though in theory the peer could have different
  681. * aggregation reorder buffer sizes for different sessions,
  682. * our ucode doesn't allow for that and has a global limit
  683. * for each station. Therefore, use the minimum of all the
  684. * aggregation sessions and our default value.
  685. */
  686. mvmsta->max_agg_bufsize =
  687. min(mvmsta->max_agg_bufsize, buf_size);
  688. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  689. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  690. sta->addr, tid);
  691. if (mvm->cfg->ht_params->use_rts_for_aggregation) {
  692. /*
  693. * switch to RTS/CTS if it is the prefer protection
  694. * method for HT traffic
  695. * this function also sends the LQ command
  696. */
  697. return iwl_mvm_tx_protection(mvm, &mvmsta->lq_sta.lq,
  698. mvmsta, true);
  699. /*
  700. * TODO: remove the TLC_RTS flag when we tear down the last
  701. * AGG session (agg_tids_count in DVM)
  702. */
  703. }
  704. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
  705. }
  706. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  707. struct ieee80211_sta *sta, u16 tid)
  708. {
  709. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  710. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  711. u16 txq_id;
  712. int err;
  713. /*
  714. * If mac80211 is cleaning its state, then say that we finished since
  715. * our state has been cleared anyway.
  716. */
  717. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  718. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  719. return 0;
  720. }
  721. spin_lock_bh(&mvmsta->lock);
  722. txq_id = tid_data->txq_id;
  723. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  724. mvmsta->sta_id, tid, txq_id, tid_data->state);
  725. switch (tid_data->state) {
  726. case IWL_AGG_ON:
  727. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  728. IWL_DEBUG_TX_QUEUES(mvm,
  729. "ssn = %d, next_recl = %d\n",
  730. tid_data->ssn, tid_data->next_reclaimed);
  731. /* There are still packets for this RA / TID in the HW */
  732. if (tid_data->ssn != tid_data->next_reclaimed) {
  733. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  734. err = 0;
  735. break;
  736. }
  737. tid_data->ssn = 0xffff;
  738. iwl_trans_txq_disable(mvm->trans, txq_id);
  739. /* fall through */
  740. case IWL_AGG_STARTING:
  741. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  742. /*
  743. * The agg session has been stopped before it was set up. This
  744. * can happen when the AddBA timer times out for example.
  745. */
  746. /* No barriers since we are under mutex */
  747. lockdep_assert_held(&mvm->mutex);
  748. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  749. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  750. tid_data->state = IWL_AGG_OFF;
  751. err = 0;
  752. break;
  753. default:
  754. IWL_ERR(mvm,
  755. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  756. mvmsta->sta_id, tid, tid_data->state);
  757. IWL_ERR(mvm,
  758. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  759. err = -EINVAL;
  760. }
  761. spin_unlock_bh(&mvmsta->lock);
  762. return err;
  763. }
  764. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  765. struct ieee80211_sta *sta, u16 tid)
  766. {
  767. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  768. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  769. u16 txq_id;
  770. enum iwl_mvm_agg_state old_state;
  771. /*
  772. * First set the agg state to OFF to avoid calling
  773. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  774. */
  775. spin_lock_bh(&mvmsta->lock);
  776. txq_id = tid_data->txq_id;
  777. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  778. mvmsta->sta_id, tid, txq_id, tid_data->state);
  779. old_state = tid_data->state;
  780. tid_data->state = IWL_AGG_OFF;
  781. spin_unlock_bh(&mvmsta->lock);
  782. if (old_state >= IWL_AGG_ON) {
  783. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
  784. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  785. iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
  786. }
  787. mvm->queue_to_mac80211[tid_data->txq_id] =
  788. IWL_INVALID_MAC80211_QUEUE;
  789. return 0;
  790. }
  791. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  792. {
  793. int i;
  794. lockdep_assert_held(&mvm->mutex);
  795. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  796. if (i == STA_KEY_MAX_NUM)
  797. return STA_KEY_IDX_INVALID;
  798. __set_bit(i, mvm->fw_key_table);
  799. return i;
  800. }
  801. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  802. struct ieee80211_sta *sta)
  803. {
  804. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  805. if (sta) {
  806. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  807. return mvm_sta->sta_id;
  808. }
  809. /*
  810. * The device expects GTKs for station interfaces to be
  811. * installed as GTKs for the AP station. If we have no
  812. * station ID, then use AP's station ID.
  813. */
  814. if (vif->type == NL80211_IFTYPE_STATION &&
  815. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  816. return mvmvif->ap_sta_id;
  817. return IWL_MVM_STATION_COUNT;
  818. }
  819. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  820. struct iwl_mvm_sta *mvm_sta,
  821. struct ieee80211_key_conf *keyconf,
  822. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  823. u32 cmd_flags)
  824. {
  825. __le16 key_flags;
  826. struct iwl_mvm_add_sta_cmd cmd = {};
  827. int ret, status;
  828. u16 keyidx;
  829. int i;
  830. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  831. STA_KEY_FLG_KEYID_MSK;
  832. key_flags = cpu_to_le16(keyidx);
  833. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  834. switch (keyconf->cipher) {
  835. case WLAN_CIPHER_SUITE_TKIP:
  836. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  837. cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
  838. for (i = 0; i < 5; i++)
  839. cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  840. memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
  841. break;
  842. case WLAN_CIPHER_SUITE_CCMP:
  843. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  844. memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
  845. break;
  846. default:
  847. WARN_ON(1);
  848. return -EINVAL;
  849. }
  850. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  851. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  852. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  853. cmd.key.key_offset = keyconf->hw_key_idx;
  854. cmd.key.key_flags = key_flags;
  855. cmd.add_modify = STA_MODE_MODIFY;
  856. cmd.modify_mask = STA_MODIFY_KEY;
  857. cmd.sta_id = sta_id;
  858. status = ADD_STA_SUCCESS;
  859. if (cmd_flags == CMD_SYNC)
  860. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  861. &cmd, &status);
  862. else
  863. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  864. sizeof(cmd), &cmd);
  865. switch (status) {
  866. case ADD_STA_SUCCESS:
  867. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  868. break;
  869. default:
  870. ret = -EIO;
  871. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  872. break;
  873. }
  874. return ret;
  875. }
  876. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  877. struct ieee80211_key_conf *keyconf,
  878. u8 sta_id, bool remove_key)
  879. {
  880. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  881. /* verify the key details match the required command's expectations */
  882. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  883. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  884. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  885. return -EINVAL;
  886. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  887. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  888. if (remove_key) {
  889. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  890. } else {
  891. struct ieee80211_key_seq seq;
  892. const u8 *pn;
  893. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  894. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  895. igtk_cmd.K1, igtk_cmd.K2);
  896. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  897. pn = seq.aes_cmac.pn;
  898. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  899. ((u64) pn[4] << 8) |
  900. ((u64) pn[3] << 16) |
  901. ((u64) pn[2] << 24) |
  902. ((u64) pn[1] << 32) |
  903. ((u64) pn[0] << 40));
  904. }
  905. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  906. remove_key ? "removing" : "installing",
  907. igtk_cmd.sta_id);
  908. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
  909. sizeof(igtk_cmd), &igtk_cmd);
  910. }
  911. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  912. struct ieee80211_vif *vif,
  913. struct ieee80211_sta *sta)
  914. {
  915. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  916. if (sta)
  917. return sta->addr;
  918. if (vif->type == NL80211_IFTYPE_STATION &&
  919. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  920. u8 sta_id = mvmvif->ap_sta_id;
  921. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  922. lockdep_is_held(&mvm->mutex));
  923. return sta->addr;
  924. }
  925. return NULL;
  926. }
  927. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  928. struct ieee80211_vif *vif,
  929. struct ieee80211_sta *sta,
  930. struct ieee80211_key_conf *keyconf,
  931. bool have_key_offset)
  932. {
  933. struct iwl_mvm_sta *mvm_sta;
  934. int ret;
  935. u8 *addr, sta_id;
  936. struct ieee80211_key_seq seq;
  937. u16 p1k[5];
  938. lockdep_assert_held(&mvm->mutex);
  939. /* Get the station id from the mvm local station table */
  940. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  941. if (sta_id == IWL_MVM_STATION_COUNT) {
  942. IWL_ERR(mvm, "Failed to find station id\n");
  943. return -EINVAL;
  944. }
  945. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  946. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  947. goto end;
  948. }
  949. /*
  950. * It is possible that the 'sta' parameter is NULL, and thus
  951. * there is a need to retrieve the sta from the local station table.
  952. */
  953. if (!sta) {
  954. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  955. lockdep_is_held(&mvm->mutex));
  956. if (IS_ERR_OR_NULL(sta)) {
  957. IWL_ERR(mvm, "Invalid station id\n");
  958. return -EINVAL;
  959. }
  960. }
  961. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  962. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  963. return -EINVAL;
  964. if (!have_key_offset) {
  965. /*
  966. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  967. * configure it there. As a result, this workaround exists to
  968. * let the caller set the key offset (hw_key_idx), see d3.c.
  969. */
  970. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  971. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  972. return -ENOSPC;
  973. }
  974. switch (keyconf->cipher) {
  975. case WLAN_CIPHER_SUITE_TKIP:
  976. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  977. /* get phase 1 key from mac80211 */
  978. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  979. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  980. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  981. seq.tkip.iv32, p1k, CMD_SYNC);
  982. break;
  983. case WLAN_CIPHER_SUITE_CCMP:
  984. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  985. 0, NULL, CMD_SYNC);
  986. break;
  987. default:
  988. IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
  989. ret = -EINVAL;
  990. }
  991. if (ret)
  992. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  993. end:
  994. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  995. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  996. sta->addr, ret);
  997. return ret;
  998. }
  999. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  1000. struct ieee80211_vif *vif,
  1001. struct ieee80211_sta *sta,
  1002. struct ieee80211_key_conf *keyconf)
  1003. {
  1004. struct iwl_mvm_sta *mvm_sta;
  1005. struct iwl_mvm_add_sta_cmd cmd = {};
  1006. __le16 key_flags;
  1007. int ret, status;
  1008. u8 sta_id;
  1009. lockdep_assert_held(&mvm->mutex);
  1010. /* Get the station id from the mvm local station table */
  1011. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1012. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  1013. keyconf->keyidx, sta_id);
  1014. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  1015. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1016. ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1017. if (!ret) {
  1018. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1019. keyconf->hw_key_idx);
  1020. return -ENOENT;
  1021. }
  1022. if (sta_id == IWL_MVM_STATION_COUNT) {
  1023. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1024. return 0;
  1025. }
  1026. /*
  1027. * It is possible that the 'sta' parameter is NULL, and thus
  1028. * there is a need to retrieve the sta from the local station table,
  1029. * for example when a GTK is removed (where the sta_id will then be
  1030. * the AP ID, and no station was passed by mac80211.)
  1031. */
  1032. if (!sta) {
  1033. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1034. lockdep_is_held(&mvm->mutex));
  1035. if (!sta) {
  1036. IWL_ERR(mvm, "Invalid station id\n");
  1037. return -EINVAL;
  1038. }
  1039. }
  1040. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1041. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1042. return -EINVAL;
  1043. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1044. STA_KEY_FLG_KEYID_MSK);
  1045. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1046. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1047. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1048. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1049. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  1050. cmd.key.key_flags = key_flags;
  1051. cmd.key.key_offset = keyconf->hw_key_idx;
  1052. cmd.sta_id = sta_id;
  1053. cmd.modify_mask = STA_MODIFY_KEY;
  1054. cmd.add_modify = STA_MODE_MODIFY;
  1055. status = ADD_STA_SUCCESS;
  1056. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  1057. &cmd, &status);
  1058. switch (status) {
  1059. case ADD_STA_SUCCESS:
  1060. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1061. break;
  1062. default:
  1063. ret = -EIO;
  1064. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1065. break;
  1066. }
  1067. return ret;
  1068. }
  1069. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1070. struct ieee80211_vif *vif,
  1071. struct ieee80211_key_conf *keyconf,
  1072. struct ieee80211_sta *sta, u32 iv32,
  1073. u16 *phase1key)
  1074. {
  1075. struct iwl_mvm_sta *mvm_sta;
  1076. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1077. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  1078. return;
  1079. rcu_read_lock();
  1080. if (!sta) {
  1081. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1082. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  1083. rcu_read_unlock();
  1084. return;
  1085. }
  1086. }
  1087. mvm_sta = (void *)sta->drv_priv;
  1088. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1089. iv32, phase1key, CMD_ASYNC);
  1090. rcu_read_unlock();
  1091. }
  1092. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1093. struct ieee80211_sta *sta)
  1094. {
  1095. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  1096. struct iwl_mvm_add_sta_cmd cmd = {
  1097. .add_modify = STA_MODE_MODIFY,
  1098. .sta_id = mvmsta->sta_id,
  1099. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1100. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1101. };
  1102. int ret;
  1103. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1104. if (ret)
  1105. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1106. }
  1107. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1108. struct ieee80211_sta *sta,
  1109. enum ieee80211_frame_release_type reason,
  1110. u16 cnt)
  1111. {
  1112. u16 sleep_state_flags =
  1113. (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
  1114. STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
  1115. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  1116. struct iwl_mvm_add_sta_cmd cmd = {
  1117. .add_modify = STA_MODE_MODIFY,
  1118. .sta_id = mvmsta->sta_id,
  1119. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1120. .sleep_tx_count = cpu_to_le16(cnt),
  1121. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1122. /*
  1123. * Same modify mask for sleep_tx_count and sleep_state_flags so
  1124. * we must set the sleep_state_flags too.
  1125. */
  1126. .sleep_state_flags = cpu_to_le16(sleep_state_flags),
  1127. };
  1128. int ret;
  1129. /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
  1130. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1131. if (ret)
  1132. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1133. }