iwl-sta.c 25 KB

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