sta.c 40 KB

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