iwl-sta.c 27 KB

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