iwl-agn-sta.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <net/mac80211.h>
  30. #include "iwl-dev.h"
  31. #include "iwl-core.h"
  32. #include "iwl-sta.h"
  33. #include "iwl-agn.h"
  34. #include "iwl-trans.h"
  35. static struct iwl_link_quality_cmd *
  36. iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx, u8 sta_id)
  37. {
  38. int i, r;
  39. struct iwl_link_quality_cmd *link_cmd;
  40. u32 rate_flags = 0;
  41. __le32 rate_n_flags;
  42. link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
  43. if (!link_cmd) {
  44. IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
  45. return NULL;
  46. }
  47. lockdep_assert_held(&priv->mutex);
  48. /* Set up the rate scaling to start at selected rate, fall back
  49. * all the way down to 1M in IEEE order, and then spin on 1M */
  50. if (priv->band == IEEE80211_BAND_5GHZ)
  51. r = IWL_RATE_6M_INDEX;
  52. else if (ctx && ctx->vif && ctx->vif->p2p)
  53. r = IWL_RATE_6M_INDEX;
  54. else
  55. r = IWL_RATE_1M_INDEX;
  56. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  57. rate_flags |= RATE_MCS_CCK_MSK;
  58. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  59. RATE_MCS_ANT_POS;
  60. rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  61. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  62. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  63. link_cmd->general_params.single_stream_ant_msk =
  64. first_antenna(priv->hw_params.valid_tx_ant);
  65. link_cmd->general_params.dual_stream_ant_msk =
  66. priv->hw_params.valid_tx_ant &
  67. ~first_antenna(priv->hw_params.valid_tx_ant);
  68. if (!link_cmd->general_params.dual_stream_ant_msk) {
  69. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  70. } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
  71. link_cmd->general_params.dual_stream_ant_msk =
  72. priv->hw_params.valid_tx_ant;
  73. }
  74. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  75. link_cmd->agg_params.agg_time_limit =
  76. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  77. link_cmd->sta_id = sta_id;
  78. return link_cmd;
  79. }
  80. /*
  81. * iwlagn_add_bssid_station - Add the special IBSS BSSID station
  82. *
  83. * Function sleeps.
  84. */
  85. int iwlagn_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  86. const u8 *addr, u8 *sta_id_r)
  87. {
  88. int ret;
  89. u8 sta_id;
  90. struct iwl_link_quality_cmd *link_cmd;
  91. unsigned long flags;
  92. if (sta_id_r)
  93. *sta_id_r = IWL_INVALID_STATION;
  94. ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
  95. if (ret) {
  96. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  97. return ret;
  98. }
  99. if (sta_id_r)
  100. *sta_id_r = sta_id;
  101. spin_lock_irqsave(&priv->sta_lock, flags);
  102. priv->stations[sta_id].used |= IWL_STA_LOCAL;
  103. spin_unlock_irqrestore(&priv->sta_lock, flags);
  104. /* Set up default rate scaling table in device's station table */
  105. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  106. if (!link_cmd) {
  107. IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
  108. addr);
  109. return -ENOMEM;
  110. }
  111. ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
  112. if (ret)
  113. IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
  114. spin_lock_irqsave(&priv->sta_lock, flags);
  115. priv->stations[sta_id].lq = link_cmd;
  116. spin_unlock_irqrestore(&priv->sta_lock, flags);
  117. return 0;
  118. }
  119. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
  120. struct iwl_rxon_context *ctx,
  121. bool send_if_empty)
  122. {
  123. int i, not_empty = 0;
  124. u8 buff[sizeof(struct iwl_wep_cmd) +
  125. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  126. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  127. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  128. struct iwl_host_cmd cmd = {
  129. .id = ctx->wep_key_cmd,
  130. .data = { wep_cmd, },
  131. .flags = CMD_SYNC,
  132. };
  133. might_sleep();
  134. memset(wep_cmd, 0, cmd_size +
  135. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  136. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  137. wep_cmd->key[i].key_index = i;
  138. if (ctx->wep_keys[i].key_size) {
  139. wep_cmd->key[i].key_offset = i;
  140. not_empty = 1;
  141. } else {
  142. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  143. }
  144. wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
  145. memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
  146. ctx->wep_keys[i].key_size);
  147. }
  148. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  149. wep_cmd->num_keys = WEP_KEYS_MAX;
  150. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  151. cmd.len[0] = cmd_size;
  152. if (not_empty || send_if_empty)
  153. return trans_send_cmd(priv, &cmd);
  154. else
  155. return 0;
  156. }
  157. int iwl_restore_default_wep_keys(struct iwl_priv *priv,
  158. struct iwl_rxon_context *ctx)
  159. {
  160. lockdep_assert_held(&priv->mutex);
  161. return iwl_send_static_wepkey_cmd(priv, ctx, false);
  162. }
  163. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  164. struct iwl_rxon_context *ctx,
  165. struct ieee80211_key_conf *keyconf)
  166. {
  167. int ret;
  168. lockdep_assert_held(&priv->mutex);
  169. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  170. keyconf->keyidx);
  171. memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
  172. if (iwl_is_rfkill(priv)) {
  173. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  174. /* but keys in device are clear anyway so return success */
  175. return 0;
  176. }
  177. ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
  178. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  179. keyconf->keyidx, ret);
  180. return ret;
  181. }
  182. int iwl_set_default_wep_key(struct iwl_priv *priv,
  183. struct iwl_rxon_context *ctx,
  184. struct ieee80211_key_conf *keyconf)
  185. {
  186. int ret;
  187. lockdep_assert_held(&priv->mutex);
  188. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  189. keyconf->keylen != WEP_KEY_LEN_64) {
  190. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  191. return -EINVAL;
  192. }
  193. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  194. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  195. priv->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
  196. ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  197. memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
  198. keyconf->keylen);
  199. ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
  200. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  201. keyconf->keylen, keyconf->keyidx, ret);
  202. return ret;
  203. }
  204. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  205. struct iwl_rxon_context *ctx,
  206. struct ieee80211_key_conf *keyconf,
  207. u8 sta_id)
  208. {
  209. unsigned long flags;
  210. __le16 key_flags = 0;
  211. struct iwl_addsta_cmd sta_cmd;
  212. lockdep_assert_held(&priv->mutex);
  213. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  214. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  215. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  216. key_flags &= ~STA_KEY_FLG_INVALID;
  217. if (keyconf->keylen == WEP_KEY_LEN_128)
  218. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  219. if (sta_id == ctx->bcast_sta_id)
  220. key_flags |= STA_KEY_MULTICAST_MSK;
  221. spin_lock_irqsave(&priv->sta_lock, flags);
  222. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  223. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  224. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  225. memcpy(priv->stations[sta_id].keyinfo.key,
  226. keyconf->key, keyconf->keylen);
  227. memcpy(&priv->stations[sta_id].sta.key.key[3],
  228. keyconf->key, keyconf->keylen);
  229. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  230. == STA_KEY_FLG_NO_ENC)
  231. priv->stations[sta_id].sta.key.key_offset =
  232. iwl_get_free_ucode_key_index(priv);
  233. /* else, we are overriding an existing key => no need to allocated room
  234. * in uCode. */
  235. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  236. "no space for a new key");
  237. priv->stations[sta_id].sta.key.key_flags = key_flags;
  238. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  239. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  240. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  241. spin_unlock_irqrestore(&priv->sta_lock, flags);
  242. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  243. }
  244. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  245. struct iwl_rxon_context *ctx,
  246. struct ieee80211_key_conf *keyconf,
  247. u8 sta_id)
  248. {
  249. unsigned long flags;
  250. __le16 key_flags = 0;
  251. struct iwl_addsta_cmd sta_cmd;
  252. lockdep_assert_held(&priv->mutex);
  253. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  254. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  255. key_flags &= ~STA_KEY_FLG_INVALID;
  256. if (sta_id == ctx->bcast_sta_id)
  257. key_flags |= STA_KEY_MULTICAST_MSK;
  258. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  259. spin_lock_irqsave(&priv->sta_lock, flags);
  260. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  261. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  262. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  263. keyconf->keylen);
  264. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  265. keyconf->keylen);
  266. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  267. == STA_KEY_FLG_NO_ENC)
  268. priv->stations[sta_id].sta.key.key_offset =
  269. iwl_get_free_ucode_key_index(priv);
  270. /* else, we are overriding an existing key => no need to allocated room
  271. * in uCode. */
  272. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  273. "no space for a new key");
  274. priv->stations[sta_id].sta.key.key_flags = key_flags;
  275. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  276. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  277. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  278. spin_unlock_irqrestore(&priv->sta_lock, flags);
  279. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  280. }
  281. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  282. struct iwl_rxon_context *ctx,
  283. struct ieee80211_key_conf *keyconf,
  284. u8 sta_id)
  285. {
  286. unsigned long flags;
  287. int ret = 0;
  288. __le16 key_flags = 0;
  289. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  290. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  291. key_flags &= ~STA_KEY_FLG_INVALID;
  292. if (sta_id == ctx->bcast_sta_id)
  293. key_flags |= STA_KEY_MULTICAST_MSK;
  294. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  295. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  296. spin_lock_irqsave(&priv->sta_lock, flags);
  297. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  298. priv->stations[sta_id].keyinfo.keylen = 16;
  299. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  300. == STA_KEY_FLG_NO_ENC)
  301. priv->stations[sta_id].sta.key.key_offset =
  302. iwl_get_free_ucode_key_index(priv);
  303. /* else, we are overriding an existing key => no need to allocated room
  304. * in uCode. */
  305. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  306. "no space for a new key");
  307. priv->stations[sta_id].sta.key.key_flags = key_flags;
  308. /* This copy is acutally not needed: we get the key with each TX */
  309. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  310. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  311. spin_unlock_irqrestore(&priv->sta_lock, flags);
  312. return ret;
  313. }
  314. void iwl_update_tkip_key(struct iwl_priv *priv,
  315. struct iwl_rxon_context *ctx,
  316. struct ieee80211_key_conf *keyconf,
  317. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  318. {
  319. u8 sta_id;
  320. unsigned long flags;
  321. int i;
  322. if (iwl_scan_cancel(priv)) {
  323. /* cancel scan failed, just live w/ bad key and rely
  324. briefly on SW decryption */
  325. return;
  326. }
  327. sta_id = iwl_sta_id_or_broadcast(priv, ctx, sta);
  328. if (sta_id == IWL_INVALID_STATION)
  329. return;
  330. spin_lock_irqsave(&priv->sta_lock, flags);
  331. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  332. for (i = 0; i < 5; i++)
  333. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  334. cpu_to_le16(phase1key[i]);
  335. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  336. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  337. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  338. spin_unlock_irqrestore(&priv->sta_lock, flags);
  339. }
  340. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  341. struct iwl_rxon_context *ctx,
  342. struct ieee80211_key_conf *keyconf,
  343. u8 sta_id)
  344. {
  345. unsigned long flags;
  346. u16 key_flags;
  347. u8 keyidx;
  348. struct iwl_addsta_cmd sta_cmd;
  349. lockdep_assert_held(&priv->mutex);
  350. ctx->key_mapping_keys--;
  351. spin_lock_irqsave(&priv->sta_lock, flags);
  352. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  353. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  354. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  355. keyconf->keyidx, sta_id);
  356. if (keyconf->keyidx != keyidx) {
  357. /* We need to remove a key with index different that the one
  358. * in the uCode. This means that the key we need to remove has
  359. * been replaced by another one with different index.
  360. * Don't do anything and return ok
  361. */
  362. spin_unlock_irqrestore(&priv->sta_lock, flags);
  363. return 0;
  364. }
  365. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  366. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  367. keyconf->keyidx, key_flags);
  368. spin_unlock_irqrestore(&priv->sta_lock, flags);
  369. return 0;
  370. }
  371. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  372. &priv->ucode_key_table))
  373. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  374. priv->stations[sta_id].sta.key.key_offset);
  375. memset(&priv->stations[sta_id].keyinfo, 0,
  376. sizeof(struct iwl_hw_key));
  377. memset(&priv->stations[sta_id].sta.key, 0,
  378. sizeof(struct iwl_keyinfo));
  379. priv->stations[sta_id].sta.key.key_flags =
  380. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  381. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  382. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  383. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  384. if (iwl_is_rfkill(priv)) {
  385. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
  386. spin_unlock_irqrestore(&priv->sta_lock, flags);
  387. return 0;
  388. }
  389. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  390. spin_unlock_irqrestore(&priv->sta_lock, flags);
  391. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  392. }
  393. int iwl_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  394. struct ieee80211_key_conf *keyconf, u8 sta_id)
  395. {
  396. int ret;
  397. lockdep_assert_held(&priv->mutex);
  398. ctx->key_mapping_keys++;
  399. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  400. switch (keyconf->cipher) {
  401. case WLAN_CIPHER_SUITE_CCMP:
  402. ret = iwl_set_ccmp_dynamic_key_info(priv, ctx, keyconf, sta_id);
  403. break;
  404. case WLAN_CIPHER_SUITE_TKIP:
  405. ret = iwl_set_tkip_dynamic_key_info(priv, ctx, keyconf, sta_id);
  406. break;
  407. case WLAN_CIPHER_SUITE_WEP40:
  408. case WLAN_CIPHER_SUITE_WEP104:
  409. ret = iwl_set_wep_dynamic_key_info(priv, ctx, keyconf, sta_id);
  410. break;
  411. default:
  412. IWL_ERR(priv,
  413. "Unknown alg: %s cipher = %x\n", __func__,
  414. keyconf->cipher);
  415. ret = -EINVAL;
  416. }
  417. IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
  418. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  419. sta_id, ret);
  420. return ret;
  421. }
  422. /**
  423. * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
  424. *
  425. * This adds the broadcast station into the driver's station table
  426. * and marks it driver active, so that it will be restored to the
  427. * device at the next best time.
  428. */
  429. int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
  430. struct iwl_rxon_context *ctx)
  431. {
  432. struct iwl_link_quality_cmd *link_cmd;
  433. unsigned long flags;
  434. u8 sta_id;
  435. spin_lock_irqsave(&priv->sta_lock, flags);
  436. sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
  437. if (sta_id == IWL_INVALID_STATION) {
  438. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  439. spin_unlock_irqrestore(&priv->sta_lock, flags);
  440. return -EINVAL;
  441. }
  442. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  443. priv->stations[sta_id].used |= IWL_STA_BCAST;
  444. spin_unlock_irqrestore(&priv->sta_lock, flags);
  445. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  446. if (!link_cmd) {
  447. IWL_ERR(priv,
  448. "Unable to initialize rate scaling for bcast station.\n");
  449. return -ENOMEM;
  450. }
  451. spin_lock_irqsave(&priv->sta_lock, flags);
  452. priv->stations[sta_id].lq = link_cmd;
  453. spin_unlock_irqrestore(&priv->sta_lock, flags);
  454. return 0;
  455. }
  456. /**
  457. * iwl_update_bcast_station - update broadcast station's LQ command
  458. *
  459. * Only used by iwlagn. Placed here to have all bcast station management
  460. * code together.
  461. */
  462. int iwl_update_bcast_station(struct iwl_priv *priv,
  463. struct iwl_rxon_context *ctx)
  464. {
  465. unsigned long flags;
  466. struct iwl_link_quality_cmd *link_cmd;
  467. u8 sta_id = ctx->bcast_sta_id;
  468. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  469. if (!link_cmd) {
  470. IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
  471. return -ENOMEM;
  472. }
  473. spin_lock_irqsave(&priv->sta_lock, flags);
  474. if (priv->stations[sta_id].lq)
  475. kfree(priv->stations[sta_id].lq);
  476. else
  477. IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
  478. priv->stations[sta_id].lq = link_cmd;
  479. spin_unlock_irqrestore(&priv->sta_lock, flags);
  480. return 0;
  481. }
  482. int iwl_update_bcast_stations(struct iwl_priv *priv)
  483. {
  484. struct iwl_rxon_context *ctx;
  485. int ret = 0;
  486. for_each_context(priv, ctx) {
  487. ret = iwl_update_bcast_station(priv, ctx);
  488. if (ret)
  489. break;
  490. }
  491. return ret;
  492. }
  493. /**
  494. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  495. */
  496. int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  497. {
  498. unsigned long flags;
  499. struct iwl_addsta_cmd sta_cmd;
  500. lockdep_assert_held(&priv->mutex);
  501. /* Remove "disable" flag, to enable Tx for this TID */
  502. spin_lock_irqsave(&priv->sta_lock, flags);
  503. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  504. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  505. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  506. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  507. spin_unlock_irqrestore(&priv->sta_lock, flags);
  508. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  509. }
  510. int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
  511. int tid, u16 ssn)
  512. {
  513. unsigned long flags;
  514. int sta_id;
  515. struct iwl_addsta_cmd sta_cmd;
  516. lockdep_assert_held(&priv->mutex);
  517. sta_id = iwl_sta_id(sta);
  518. if (sta_id == IWL_INVALID_STATION)
  519. return -ENXIO;
  520. spin_lock_irqsave(&priv->sta_lock, flags);
  521. priv->stations[sta_id].sta.station_flags_msk = 0;
  522. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  523. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  524. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  525. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  526. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  527. spin_unlock_irqrestore(&priv->sta_lock, flags);
  528. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  529. }
  530. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
  531. int tid)
  532. {
  533. unsigned long flags;
  534. int sta_id;
  535. struct iwl_addsta_cmd sta_cmd;
  536. lockdep_assert_held(&priv->mutex);
  537. sta_id = iwl_sta_id(sta);
  538. if (sta_id == IWL_INVALID_STATION) {
  539. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  540. return -ENXIO;
  541. }
  542. spin_lock_irqsave(&priv->sta_lock, flags);
  543. priv->stations[sta_id].sta.station_flags_msk = 0;
  544. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  545. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  546. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  547. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  548. spin_unlock_irqrestore(&priv->sta_lock, flags);
  549. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  550. }
  551. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  552. {
  553. unsigned long flags;
  554. spin_lock_irqsave(&priv->sta_lock, flags);
  555. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  556. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  557. priv->stations[sta_id].sta.sta.modify_mask = 0;
  558. priv->stations[sta_id].sta.sleep_tx_count = 0;
  559. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  560. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  561. spin_unlock_irqrestore(&priv->sta_lock, flags);
  562. }
  563. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  564. {
  565. unsigned long flags;
  566. spin_lock_irqsave(&priv->sta_lock, flags);
  567. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  568. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  569. priv->stations[sta_id].sta.sta.modify_mask =
  570. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  571. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  572. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  573. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  574. spin_unlock_irqrestore(&priv->sta_lock, flags);
  575. }
  576. void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  577. struct ieee80211_vif *vif,
  578. enum sta_notify_cmd cmd,
  579. struct ieee80211_sta *sta)
  580. {
  581. struct iwl_priv *priv = hw->priv;
  582. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  583. int sta_id;
  584. switch (cmd) {
  585. case STA_NOTIFY_SLEEP:
  586. WARN_ON(!sta_priv->client);
  587. sta_priv->asleep = true;
  588. if (atomic_read(&sta_priv->pending_frames) > 0)
  589. ieee80211_sta_block_awake(hw, sta, true);
  590. break;
  591. case STA_NOTIFY_AWAKE:
  592. WARN_ON(!sta_priv->client);
  593. if (!sta_priv->asleep)
  594. break;
  595. sta_priv->asleep = false;
  596. sta_id = iwl_sta_id(sta);
  597. if (sta_id != IWL_INVALID_STATION)
  598. iwl_sta_modify_ps_wake(priv, sta_id);
  599. break;
  600. default:
  601. break;
  602. }
  603. }