iwl-agn-sta.c 22 KB

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