iwl-sta.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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. * James P. Ketrenos <ipw2100-admin@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 <linux/etherdevice.h>
  31. #include "iwl-eeprom.h"
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. #define IWL_STA_DRIVER_ACTIVE 0x1 /* ucode entry is active */
  38. #define IWL_STA_UCODE_ACTIVE 0x2 /* ucode entry is active */
  39. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  40. {
  41. int i;
  42. int start = 0;
  43. int ret = IWL_INVALID_STATION;
  44. unsigned long flags;
  45. DECLARE_MAC_BUF(mac);
  46. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
  47. (priv->iw_mode == IEEE80211_IF_TYPE_AP))
  48. start = IWL_STA_ID;
  49. if (is_broadcast_ether_addr(addr))
  50. return priv->hw_params.bcast_sta_id;
  51. spin_lock_irqsave(&priv->sta_lock, flags);
  52. for (i = start; i < priv->hw_params.max_stations; i++)
  53. if (priv->stations[i].used &&
  54. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  55. addr))) {
  56. ret = i;
  57. goto out;
  58. }
  59. IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
  60. print_mac(mac, addr), priv->num_stations);
  61. out:
  62. spin_unlock_irqrestore(&priv->sta_lock, flags);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL(iwl_find_station);
  66. static int iwl_add_sta_callback(struct iwl_priv *priv,
  67. struct iwl_cmd *cmd, struct sk_buff *skb)
  68. {
  69. struct iwl_rx_packet *res = NULL;
  70. if (!skb) {
  71. IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
  72. return 1;
  73. }
  74. res = (struct iwl_rx_packet *)skb->data;
  75. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  76. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  77. res->hdr.flags);
  78. return 1;
  79. }
  80. switch (res->u.add_sta.status) {
  81. case ADD_STA_SUCCESS_MSK:
  82. /* FIXME: implement iwl_sta_ucode_activate(priv, addr); */
  83. /* fail through */
  84. default:
  85. IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
  86. res->u.add_sta.status);
  87. break;
  88. }
  89. /* We didn't cache the SKB; let the caller free it */
  90. return 1;
  91. }
  92. int iwl_send_add_sta(struct iwl_priv *priv,
  93. struct iwl_addsta_cmd *sta, u8 flags)
  94. {
  95. struct iwl_rx_packet *res = NULL;
  96. int ret = 0;
  97. u8 data[sizeof(*sta)];
  98. struct iwl_host_cmd cmd = {
  99. .id = REPLY_ADD_STA,
  100. .meta.flags = flags,
  101. .data = data,
  102. };
  103. if (flags & CMD_ASYNC)
  104. cmd.meta.u.callback = iwl_add_sta_callback;
  105. else
  106. cmd.meta.flags |= CMD_WANT_SKB;
  107. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  108. ret = iwl_send_cmd(priv, &cmd);
  109. if (ret || (flags & CMD_ASYNC))
  110. return ret;
  111. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  112. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  113. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  114. res->hdr.flags);
  115. ret = -EIO;
  116. }
  117. if (ret == 0) {
  118. switch (res->u.add_sta.status) {
  119. case ADD_STA_SUCCESS_MSK:
  120. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  121. break;
  122. default:
  123. ret = -EIO;
  124. IWL_WARNING("REPLY_ADD_STA failed\n");
  125. break;
  126. }
  127. }
  128. priv->alloc_rxb_skb--;
  129. dev_kfree_skb_any(cmd.meta.u.skb);
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(iwl_send_add_sta);
  133. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  134. struct ieee80211_ht_info *sta_ht_inf)
  135. {
  136. __le32 sta_flags;
  137. u8 mimo_ps_mode;
  138. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  139. goto done;
  140. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
  141. sta_flags = priv->stations[index].sta.station_flags;
  142. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  143. switch (mimo_ps_mode) {
  144. case WLAN_HT_CAP_MIMO_PS_STATIC:
  145. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  146. break;
  147. case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
  148. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  149. break;
  150. case WLAN_HT_CAP_MIMO_PS_DISABLED:
  151. break;
  152. default:
  153. IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
  154. break;
  155. }
  156. sta_flags |= cpu_to_le32(
  157. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  158. sta_flags |= cpu_to_le32(
  159. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  160. if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
  161. sta_flags |= STA_FLG_FAT_EN_MSK;
  162. else
  163. sta_flags &= ~STA_FLG_FAT_EN_MSK;
  164. priv->stations[index].sta.station_flags = sta_flags;
  165. done:
  166. return;
  167. }
  168. /**
  169. * iwl_add_station_flags - Add station to tables in driver and device
  170. */
  171. u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
  172. u8 flags, struct ieee80211_ht_info *ht_info)
  173. {
  174. int i;
  175. int index = IWL_INVALID_STATION;
  176. struct iwl_station_entry *station;
  177. unsigned long flags_spin;
  178. DECLARE_MAC_BUF(mac);
  179. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  180. if (is_ap)
  181. index = IWL_AP_ID;
  182. else if (is_broadcast_ether_addr(addr))
  183. index = priv->hw_params.bcast_sta_id;
  184. else
  185. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  186. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  187. addr)) {
  188. index = i;
  189. break;
  190. }
  191. if (!priv->stations[i].used &&
  192. index == IWL_INVALID_STATION)
  193. index = i;
  194. }
  195. /* These two conditions have the same outcome, but keep them separate
  196. since they have different meanings */
  197. if (unlikely(index == IWL_INVALID_STATION)) {
  198. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  199. return index;
  200. }
  201. if (priv->stations[index].used &&
  202. !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
  203. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  204. return index;
  205. }
  206. IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
  207. station = &priv->stations[index];
  208. station->used = 1;
  209. priv->num_stations++;
  210. /* Set up the REPLY_ADD_STA command to send to device */
  211. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  212. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  213. station->sta.mode = 0;
  214. station->sta.sta.sta_id = index;
  215. station->sta.station_flags = 0;
  216. /* BCAST station and IBSS stations do not work in HT mode */
  217. if (index != priv->hw_params.bcast_sta_id &&
  218. priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
  219. iwl_set_ht_add_station(priv, index, ht_info);
  220. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  221. /* Add station to device's station table */
  222. iwl_send_add_sta(priv, &station->sta, flags);
  223. return index;
  224. }
  225. EXPORT_SYMBOL(iwl_add_station_flags);
  226. static int iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
  227. {
  228. unsigned long flags;
  229. u8 sta_id;
  230. DECLARE_MAC_BUF(mac);
  231. sta_id = iwl_find_station(priv, addr);
  232. if (sta_id != IWL_INVALID_STATION) {
  233. IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
  234. print_mac(mac, addr));
  235. spin_lock_irqsave(&priv->sta_lock, flags);
  236. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  237. memset(&priv->stations[sta_id], 0,
  238. sizeof(struct iwl_station_entry));
  239. spin_unlock_irqrestore(&priv->sta_lock, flags);
  240. return 0;
  241. }
  242. return -EINVAL;
  243. }
  244. static int iwl_remove_sta_callback(struct iwl_priv *priv,
  245. struct iwl_cmd *cmd, struct sk_buff *skb)
  246. {
  247. struct iwl_rx_packet *res = NULL;
  248. const char *addr = cmd->cmd.rm_sta.addr;
  249. if (!skb) {
  250. IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
  251. return 1;
  252. }
  253. res = (struct iwl_rx_packet *)skb->data;
  254. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  255. IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  256. res->hdr.flags);
  257. return 1;
  258. }
  259. switch (res->u.rem_sta.status) {
  260. case REM_STA_SUCCESS_MSK:
  261. iwl_sta_ucode_deactivate(priv, addr);
  262. break;
  263. default:
  264. break;
  265. }
  266. /* We didn't cache the SKB; let the caller free it */
  267. return 1;
  268. }
  269. static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
  270. u8 flags)
  271. {
  272. struct iwl_rx_packet *res = NULL;
  273. int ret;
  274. struct iwl_rem_sta_cmd rm_sta_cmd;
  275. struct iwl_host_cmd cmd = {
  276. .id = REPLY_REMOVE_STA,
  277. .len = sizeof(struct iwl_rem_sta_cmd),
  278. .meta.flags = flags,
  279. .data = &rm_sta_cmd,
  280. };
  281. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  282. rm_sta_cmd.num_sta = 1;
  283. memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
  284. if (flags & CMD_ASYNC)
  285. cmd.meta.u.callback = iwl_remove_sta_callback;
  286. else
  287. cmd.meta.flags |= CMD_WANT_SKB;
  288. ret = iwl_send_cmd(priv, &cmd);
  289. if (ret || (flags & CMD_ASYNC))
  290. return ret;
  291. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  292. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  293. IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  294. res->hdr.flags);
  295. ret = -EIO;
  296. }
  297. if (!ret) {
  298. switch (res->u.rem_sta.status) {
  299. case REM_STA_SUCCESS_MSK:
  300. iwl_sta_ucode_deactivate(priv, addr);
  301. IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
  302. break;
  303. default:
  304. ret = -EIO;
  305. IWL_ERROR("REPLY_REMOVE_STA failed\n");
  306. break;
  307. }
  308. }
  309. priv->alloc_rxb_skb--;
  310. dev_kfree_skb_any(cmd.meta.u.skb);
  311. return ret;
  312. }
  313. /**
  314. * iwl_remove_station - Remove driver's knowledge of station.
  315. *
  316. */
  317. u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  318. {
  319. int index = IWL_INVALID_STATION;
  320. int i;
  321. unsigned long flags;
  322. spin_lock_irqsave(&priv->sta_lock, flags);
  323. if (is_ap)
  324. index = IWL_AP_ID;
  325. else if (is_broadcast_ether_addr(addr))
  326. index = priv->hw_params.bcast_sta_id;
  327. else
  328. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  329. if (priv->stations[i].used &&
  330. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  331. addr)) {
  332. index = i;
  333. break;
  334. }
  335. if (unlikely(index == IWL_INVALID_STATION))
  336. goto out;
  337. if (priv->stations[index].used) {
  338. priv->stations[index].used = 0;
  339. priv->num_stations--;
  340. }
  341. BUG_ON(priv->num_stations < 0);
  342. spin_unlock_irqrestore(&priv->sta_lock, flags);
  343. iwl_send_remove_station(priv, addr, CMD_ASYNC);
  344. return index;
  345. out:
  346. spin_unlock_irqrestore(&priv->sta_lock, flags);
  347. return 0;
  348. }
  349. EXPORT_SYMBOL(iwl_remove_station);
  350. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  351. {
  352. int i;
  353. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  354. if (!test_and_set_bit(i, &priv->ucode_key_table))
  355. return i;
  356. return -1;
  357. }
  358. int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  359. {
  360. int i, not_empty = 0;
  361. u8 buff[sizeof(struct iwl_wep_cmd) +
  362. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  363. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  364. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  365. struct iwl_host_cmd cmd = {
  366. .id = REPLY_WEPKEY,
  367. .data = wep_cmd,
  368. .meta.flags = CMD_ASYNC,
  369. };
  370. memset(wep_cmd, 0, cmd_size +
  371. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  372. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  373. wep_cmd->key[i].key_index = i;
  374. if (priv->wep_keys[i].key_size) {
  375. wep_cmd->key[i].key_offset = i;
  376. not_empty = 1;
  377. } else {
  378. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  379. }
  380. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  381. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  382. priv->wep_keys[i].key_size);
  383. }
  384. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  385. wep_cmd->num_keys = WEP_KEYS_MAX;
  386. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  387. cmd.len = cmd_size;
  388. if (not_empty || send_if_empty)
  389. return iwl_send_cmd(priv, &cmd);
  390. else
  391. return 0;
  392. }
  393. EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
  394. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  395. struct ieee80211_key_conf *keyconf)
  396. {
  397. int ret;
  398. unsigned long flags;
  399. spin_lock_irqsave(&priv->sta_lock, flags);
  400. if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
  401. IWL_ERROR("index %d not used in uCode key table.\n",
  402. keyconf->keyidx);
  403. priv->default_wep_key--;
  404. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  405. ret = iwl_send_static_wepkey_cmd(priv, 1);
  406. IWL_DEBUG_WEP("Remove default WEP key: idx=%d ret=%d\n",
  407. keyconf->keyidx, ret);
  408. spin_unlock_irqrestore(&priv->sta_lock, flags);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  412. int iwl_set_default_wep_key(struct iwl_priv *priv,
  413. struct ieee80211_key_conf *keyconf)
  414. {
  415. int ret;
  416. unsigned long flags;
  417. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  418. keyconf->keylen != WEP_KEY_LEN_64) {
  419. IWL_DEBUG_WEP("Bad WEP key length %d\n", keyconf->keylen);
  420. return -EINVAL;
  421. }
  422. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  423. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  424. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  425. spin_lock_irqsave(&priv->sta_lock, flags);
  426. priv->default_wep_key++;
  427. if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
  428. IWL_ERROR("index %d already used in uCode key table.\n",
  429. keyconf->keyidx);
  430. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  431. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  432. keyconf->keylen);
  433. ret = iwl_send_static_wepkey_cmd(priv, 0);
  434. IWL_DEBUG_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
  435. keyconf->keylen, keyconf->keyidx, ret);
  436. spin_unlock_irqrestore(&priv->sta_lock, flags);
  437. return ret;
  438. }
  439. EXPORT_SYMBOL(iwl_set_default_wep_key);
  440. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  441. struct ieee80211_key_conf *keyconf,
  442. u8 sta_id)
  443. {
  444. unsigned long flags;
  445. __le16 key_flags = 0;
  446. int ret;
  447. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  448. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  449. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  450. key_flags &= ~STA_KEY_FLG_INVALID;
  451. if (keyconf->keylen == WEP_KEY_LEN_128)
  452. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  453. if (sta_id == priv->hw_params.bcast_sta_id)
  454. key_flags |= STA_KEY_MULTICAST_MSK;
  455. spin_lock_irqsave(&priv->sta_lock, flags);
  456. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  457. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  458. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  459. memcpy(priv->stations[sta_id].keyinfo.key,
  460. keyconf->key, keyconf->keylen);
  461. memcpy(&priv->stations[sta_id].sta.key.key[3],
  462. keyconf->key, keyconf->keylen);
  463. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  464. == STA_KEY_FLG_NO_ENC)
  465. priv->stations[sta_id].sta.key.key_offset =
  466. iwl_get_free_ucode_key_index(priv);
  467. /* else, we are overriding an existing key => no need to allocated room
  468. * in uCode. */
  469. priv->stations[sta_id].sta.key.key_flags = key_flags;
  470. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  471. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  472. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  473. spin_unlock_irqrestore(&priv->sta_lock, flags);
  474. return ret;
  475. }
  476. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  477. struct ieee80211_key_conf *keyconf,
  478. u8 sta_id)
  479. {
  480. unsigned long flags;
  481. __le16 key_flags = 0;
  482. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  483. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  484. key_flags &= ~STA_KEY_FLG_INVALID;
  485. if (sta_id == priv->hw_params.bcast_sta_id)
  486. key_flags |= STA_KEY_MULTICAST_MSK;
  487. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  488. spin_lock_irqsave(&priv->sta_lock, flags);
  489. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  490. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  491. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  492. keyconf->keylen);
  493. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  494. keyconf->keylen);
  495. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  496. == STA_KEY_FLG_NO_ENC)
  497. priv->stations[sta_id].sta.key.key_offset =
  498. iwl_get_free_ucode_key_index(priv);
  499. /* else, we are overriding an existing key => no need to allocated room
  500. * in uCode. */
  501. priv->stations[sta_id].sta.key.key_flags = key_flags;
  502. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  503. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  504. spin_unlock_irqrestore(&priv->sta_lock, flags);
  505. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  506. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  507. }
  508. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  509. struct ieee80211_key_conf *keyconf,
  510. u8 sta_id)
  511. {
  512. unsigned long flags;
  513. int ret = 0;
  514. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  515. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  516. spin_lock_irqsave(&priv->sta_lock, flags);
  517. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  518. priv->stations[sta_id].keyinfo.keylen = 16;
  519. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  520. == STA_KEY_FLG_NO_ENC)
  521. priv->stations[sta_id].sta.key.key_offset =
  522. iwl_get_free_ucode_key_index(priv);
  523. /* else, we are overriding an existing key => no need to allocated room
  524. * in uCode. */
  525. /* This copy is acutally not needed: we get the key with each TX */
  526. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  527. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  528. spin_unlock_irqrestore(&priv->sta_lock, flags);
  529. return ret;
  530. }
  531. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  532. struct ieee80211_key_conf *keyconf,
  533. u8 sta_id)
  534. {
  535. unsigned long flags;
  536. int ret = 0;
  537. u16 key_flags;
  538. u8 keyidx;
  539. priv->key_mapping_key--;
  540. spin_lock_irqsave(&priv->sta_lock, flags);
  541. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  542. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  543. IWL_DEBUG_WEP("Remove dynamic key: idx=%d sta=%d\n",
  544. keyconf->keyidx, sta_id);
  545. if (keyconf->keyidx != keyidx) {
  546. /* We need to remove a key with index different that the one
  547. * in the uCode. This means that the key we need to remove has
  548. * been replaced by another one with different index.
  549. * Don't do anything and return ok
  550. */
  551. spin_unlock_irqrestore(&priv->sta_lock, flags);
  552. return 0;
  553. }
  554. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  555. &priv->ucode_key_table))
  556. IWL_ERROR("index %d not used in uCode key table.\n",
  557. priv->stations[sta_id].sta.key.key_offset);
  558. memset(&priv->stations[sta_id].keyinfo, 0,
  559. sizeof(struct iwl_hw_key));
  560. memset(&priv->stations[sta_id].sta.key, 0,
  561. sizeof(struct iwl4965_keyinfo));
  562. priv->stations[sta_id].sta.key.key_flags =
  563. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  564. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  565. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  566. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  567. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  568. spin_unlock_irqrestore(&priv->sta_lock, flags);
  569. return ret;
  570. }
  571. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  572. int iwl_set_dynamic_key(struct iwl_priv *priv,
  573. struct ieee80211_key_conf *keyconf, u8 sta_id)
  574. {
  575. int ret;
  576. priv->key_mapping_key++;
  577. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  578. switch (keyconf->alg) {
  579. case ALG_CCMP:
  580. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  581. break;
  582. case ALG_TKIP:
  583. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  584. break;
  585. case ALG_WEP:
  586. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  587. break;
  588. default:
  589. IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  590. ret = -EINVAL;
  591. }
  592. IWL_DEBUG_WEP("Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  593. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  594. sta_id, ret);
  595. return ret;
  596. }
  597. EXPORT_SYMBOL(iwl_set_dynamic_key);
  598. #ifdef CONFIG_IWLWIFI_DEBUG
  599. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  600. struct iwl_link_quality_cmd *lq)
  601. {
  602. int i;
  603. IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
  604. IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
  605. lq->general_params.single_stream_ant_msk,
  606. lq->general_params.dual_stream_ant_msk);
  607. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  608. IWL_DEBUG_RATE("lq index %d 0x%X\n",
  609. i, lq->rs_table[i].rate_n_flags);
  610. }
  611. #else
  612. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  613. struct iwl_link_quality_cmd *lq)
  614. {
  615. }
  616. #endif
  617. int iwl_send_lq_cmd(struct iwl_priv *priv,
  618. struct iwl_link_quality_cmd *lq, u8 flags)
  619. {
  620. struct iwl_host_cmd cmd = {
  621. .id = REPLY_TX_LINK_QUALITY_CMD,
  622. .len = sizeof(struct iwl_link_quality_cmd),
  623. .meta.flags = flags,
  624. .data = lq,
  625. };
  626. if ((lq->sta_id == 0xFF) &&
  627. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
  628. return -EINVAL;
  629. if (lq->sta_id == 0xFF)
  630. lq->sta_id = IWL_AP_ID;
  631. iwl_dump_lq_cmd(priv,lq);
  632. if (iwl_is_associated(priv) && priv->assoc_station_added &&
  633. priv->lq_mngr.lq_ready)
  634. return iwl_send_cmd(priv, &cmd);
  635. return 0;
  636. }
  637. EXPORT_SYMBOL(iwl_send_lq_cmd);
  638. /**
  639. * iwl_sta_init_lq - Initialize a station's hardware rate table
  640. *
  641. * The uCode's station table contains a table of fallback rates
  642. * for automatic fallback during transmission.
  643. *
  644. * NOTE: This sets up a default set of values. These will be replaced later
  645. * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
  646. * rc80211_simple.
  647. *
  648. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  649. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  650. * which requires station table entry to exist).
  651. */
  652. static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
  653. {
  654. int i, r;
  655. struct iwl_link_quality_cmd link_cmd = {
  656. .reserved1 = 0,
  657. };
  658. u16 rate_flags;
  659. /* Set up the rate scaling to start at selected rate, fall back
  660. * all the way down to 1M in IEEE order, and then spin on 1M */
  661. if (is_ap)
  662. r = IWL_RATE_54M_INDEX;
  663. else if (priv->band == IEEE80211_BAND_5GHZ)
  664. r = IWL_RATE_6M_INDEX;
  665. else
  666. r = IWL_RATE_1M_INDEX;
  667. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  668. rate_flags = 0;
  669. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  670. rate_flags |= RATE_MCS_CCK_MSK;
  671. /* Use Tx antenna B only */
  672. rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
  673. link_cmd.rs_table[i].rate_n_flags =
  674. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  675. r = iwl4965_get_prev_ieee_rate(r);
  676. }
  677. link_cmd.general_params.single_stream_ant_msk = 2;
  678. link_cmd.general_params.dual_stream_ant_msk = 3;
  679. link_cmd.agg_params.agg_dis_start_th = 3;
  680. link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
  681. /* Update the rate scaling for control frame Tx to AP */
  682. link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
  683. iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
  684. sizeof(link_cmd), &link_cmd, NULL);
  685. }
  686. /**
  687. * iwl_rxon_add_station - add station into station table.
  688. *
  689. * there is only one AP station with id= IWL_AP_ID
  690. * NOTE: mutex must be held before calling this fnction
  691. */
  692. int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  693. {
  694. u8 sta_id;
  695. /* Add station to device's station table */
  696. struct ieee80211_conf *conf = &priv->hw->conf;
  697. struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
  698. if ((is_ap) &&
  699. (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
  700. (priv->iw_mode == IEEE80211_IF_TYPE_STA))
  701. sta_id = iwl_add_station_flags(priv, addr, is_ap,
  702. 0, cur_ht_config);
  703. else
  704. sta_id = iwl_add_station_flags(priv, addr, is_ap,
  705. 0, NULL);
  706. /* Set up default rate scaling table in device's station table */
  707. iwl_sta_init_lq(priv, addr, is_ap);
  708. return sta_id;
  709. }
  710. EXPORT_SYMBOL(iwl_rxon_add_station);
  711. /**
  712. * iwl_get_sta_id - Find station's index within station table
  713. *
  714. * If new IBSS station, create new entry in station table
  715. */
  716. int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  717. {
  718. int sta_id;
  719. u16 fc = le16_to_cpu(hdr->frame_control);
  720. DECLARE_MAC_BUF(mac);
  721. /* If this frame is broadcast or management, use broadcast station id */
  722. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  723. is_multicast_ether_addr(hdr->addr1))
  724. return priv->hw_params.bcast_sta_id;
  725. switch (priv->iw_mode) {
  726. /* If we are a client station in a BSS network, use the special
  727. * AP station entry (that's the only station we communicate with) */
  728. case IEEE80211_IF_TYPE_STA:
  729. return IWL_AP_ID;
  730. /* If we are an AP, then find the station, or use BCAST */
  731. case IEEE80211_IF_TYPE_AP:
  732. sta_id = iwl_find_station(priv, hdr->addr1);
  733. if (sta_id != IWL_INVALID_STATION)
  734. return sta_id;
  735. return priv->hw_params.bcast_sta_id;
  736. /* If this frame is going out to an IBSS network, find the station,
  737. * or create a new station table entry */
  738. case IEEE80211_IF_TYPE_IBSS:
  739. sta_id = iwl_find_station(priv, hdr->addr1);
  740. if (sta_id != IWL_INVALID_STATION)
  741. return sta_id;
  742. /* Create new station table entry */
  743. sta_id = iwl_add_station_flags(priv, hdr->addr1,
  744. 0, CMD_ASYNC, NULL);
  745. if (sta_id != IWL_INVALID_STATION)
  746. return sta_id;
  747. IWL_DEBUG_DROP("Station %s not in station map. "
  748. "Defaulting to broadcast...\n",
  749. print_mac(mac, hdr->addr1));
  750. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  751. return priv->hw_params.bcast_sta_id;
  752. default:
  753. IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
  754. return priv->hw_params.bcast_sta_id;
  755. }
  756. }
  757. EXPORT_SYMBOL(iwl_get_sta_id);
  758. /**
  759. * iwl_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
  760. */
  761. void iwl_sta_modify_enable_tid_tx(struct iwl_priv *priv, int sta_id, int tid)
  762. {
  763. unsigned long flags;
  764. /* Remove "disable" flag, to enable Tx for this TID */
  765. spin_lock_irqsave(&priv->sta_lock, flags);
  766. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  767. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  768. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  769. spin_unlock_irqrestore(&priv->sta_lock, flags);
  770. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  771. }
  772. EXPORT_SYMBOL(iwl_sta_modify_enable_tid_tx);