sta.c 36 KB

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