iwl-sta.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <net/mac80211.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/sched.h>
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  36. {
  37. int i;
  38. int start = 0;
  39. int ret = IWL_INVALID_STATION;
  40. unsigned long flags;
  41. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
  42. (priv->iw_mode == NL80211_IFTYPE_AP))
  43. start = IWL_STA_ID;
  44. if (is_broadcast_ether_addr(addr))
  45. return priv->hw_params.bcast_sta_id;
  46. spin_lock_irqsave(&priv->sta_lock, flags);
  47. for (i = start; i < priv->hw_params.max_stations; i++)
  48. if (priv->stations[i].used &&
  49. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  50. addr))) {
  51. ret = i;
  52. goto out;
  53. }
  54. IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
  55. addr, priv->num_stations);
  56. out:
  57. /*
  58. * It may be possible that more commands interacting with stations
  59. * arrive before we completed processing the adding of
  60. * station
  61. */
  62. if (ret != IWL_INVALID_STATION &&
  63. (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
  64. ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
  65. (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
  66. IWL_ERR(priv, "Requested station info for sta %d before ready. \n",
  67. ret);
  68. ret = IWL_INVALID_STATION;
  69. }
  70. spin_unlock_irqrestore(&priv->sta_lock, flags);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(iwl_find_station);
  74. int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  75. {
  76. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  77. return IWL_AP_ID;
  78. } else {
  79. u8 *da = ieee80211_get_DA(hdr);
  80. return iwl_find_station(priv, da);
  81. }
  82. }
  83. EXPORT_SYMBOL(iwl_get_ra_sta_id);
  84. /* priv->sta_lock must be held */
  85. static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  86. {
  87. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  88. IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
  89. sta_id, priv->stations[sta_id].sta.sta.addr);
  90. if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  91. IWL_DEBUG_ASSOC(priv,
  92. "STA id %u addr %pM already present in uCode (according to driver)\n",
  93. sta_id, priv->stations[sta_id].sta.sta.addr);
  94. } else {
  95. priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  96. IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  97. sta_id, priv->stations[sta_id].sta.sta.addr);
  98. }
  99. }
  100. static void iwl_process_add_sta_resp(struct iwl_priv *priv,
  101. struct iwl_addsta_cmd *addsta,
  102. struct iwl_rx_packet *pkt,
  103. bool sync)
  104. {
  105. u8 sta_id = addsta->sta.sta_id;
  106. unsigned long flags;
  107. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  108. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  109. pkt->hdr.flags);
  110. return;
  111. }
  112. IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  113. sta_id);
  114. spin_lock_irqsave(&priv->sta_lock, flags);
  115. switch (pkt->u.add_sta.status) {
  116. case ADD_STA_SUCCESS_MSK:
  117. IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  118. iwl_sta_ucode_activate(priv, sta_id);
  119. break;
  120. case ADD_STA_NO_ROOM_IN_TABLE:
  121. IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  122. sta_id);
  123. break;
  124. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  125. IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
  126. sta_id);
  127. break;
  128. case ADD_STA_MODIFY_NON_EXIST_STA:
  129. IWL_ERR(priv, "Attempting to modify non-existing station %d \n",
  130. sta_id);
  131. break;
  132. default:
  133. IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
  134. pkt->u.add_sta.status);
  135. break;
  136. }
  137. IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
  138. priv->stations[sta_id].sta.mode ==
  139. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  140. sta_id, priv->stations[sta_id].sta.sta.addr);
  141. /*
  142. * XXX: The MAC address in the command buffer is often changed from
  143. * the original sent to the device. That is, the MAC address
  144. * written to the command buffer often is not the same MAC adress
  145. * read from the command buffer when the command returns. This
  146. * issue has not yet been resolved and this debugging is left to
  147. * observe the problem.
  148. */
  149. IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
  150. priv->stations[sta_id].sta.mode ==
  151. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  152. addsta->sta.addr);
  153. spin_unlock_irqrestore(&priv->sta_lock, flags);
  154. }
  155. static void iwl_add_sta_callback(struct iwl_priv *priv,
  156. struct iwl_device_cmd *cmd,
  157. struct iwl_rx_packet *pkt)
  158. {
  159. struct iwl_addsta_cmd *addsta =
  160. (struct iwl_addsta_cmd *)cmd->cmd.payload;
  161. iwl_process_add_sta_resp(priv, addsta, pkt, false);
  162. }
  163. int iwl_send_add_sta(struct iwl_priv *priv,
  164. struct iwl_addsta_cmd *sta, u8 flags)
  165. {
  166. struct iwl_rx_packet *pkt = NULL;
  167. int ret = 0;
  168. u8 data[sizeof(*sta)];
  169. struct iwl_host_cmd cmd = {
  170. .id = REPLY_ADD_STA,
  171. .flags = flags,
  172. .data = data,
  173. };
  174. u8 sta_id = sta->sta.sta_id;
  175. IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
  176. sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
  177. if (flags & CMD_ASYNC)
  178. cmd.callback = iwl_add_sta_callback;
  179. else
  180. cmd.flags |= CMD_WANT_SKB;
  181. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  182. ret = iwl_send_cmd(priv, &cmd);
  183. if (ret || (flags & CMD_ASYNC))
  184. return ret;
  185. if (ret == 0) {
  186. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  187. iwl_process_add_sta_resp(priv, sta, pkt, true);
  188. }
  189. iwl_free_pages(priv, cmd.reply_page);
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(iwl_send_add_sta);
  193. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  194. struct ieee80211_sta_ht_cap *sta_ht_inf)
  195. {
  196. __le32 sta_flags;
  197. u8 mimo_ps_mode;
  198. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  199. goto done;
  200. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  201. IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
  202. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
  203. "static" :
  204. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
  205. "dynamic" : "disabled");
  206. sta_flags = priv->stations[index].sta.station_flags;
  207. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  208. switch (mimo_ps_mode) {
  209. case WLAN_HT_CAP_SM_PS_STATIC:
  210. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  211. break;
  212. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  213. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  214. break;
  215. case WLAN_HT_CAP_SM_PS_DISABLED:
  216. break;
  217. default:
  218. IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
  219. break;
  220. }
  221. sta_flags |= cpu_to_le32(
  222. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  223. sta_flags |= cpu_to_le32(
  224. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  225. if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
  226. sta_flags |= STA_FLG_HT40_EN_MSK;
  227. else
  228. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  229. priv->stations[index].sta.station_flags = sta_flags;
  230. done:
  231. return;
  232. }
  233. /**
  234. * iwl_prep_station - Prepare station information for addition
  235. *
  236. * should be called with sta_lock held
  237. */
  238. static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
  239. bool is_ap,
  240. struct ieee80211_sta_ht_cap *ht_info)
  241. {
  242. struct iwl_station_entry *station;
  243. int i;
  244. u8 sta_id = IWL_INVALID_STATION;
  245. u16 rate;
  246. if (is_ap)
  247. sta_id = IWL_AP_ID;
  248. else if (is_broadcast_ether_addr(addr))
  249. sta_id = priv->hw_params.bcast_sta_id;
  250. else
  251. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  252. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  253. addr)) {
  254. sta_id = i;
  255. break;
  256. }
  257. if (!priv->stations[i].used &&
  258. sta_id == IWL_INVALID_STATION)
  259. sta_id = i;
  260. }
  261. /*
  262. * These two conditions have the same outcome, but keep them
  263. * separate
  264. */
  265. if (unlikely(sta_id == IWL_INVALID_STATION))
  266. return sta_id;
  267. /*
  268. * uCode is not able to deal with multiple requests to add a
  269. * station. Keep track if one is in progress so that we do not send
  270. * another.
  271. */
  272. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  273. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  274. sta_id);
  275. return sta_id;
  276. }
  277. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  278. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
  279. !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
  280. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  281. sta_id, addr);
  282. return sta_id;
  283. }
  284. station = &priv->stations[sta_id];
  285. station->used = IWL_STA_DRIVER_ACTIVE;
  286. IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
  287. sta_id, addr);
  288. priv->num_stations++;
  289. /* Set up the REPLY_ADD_STA command to send to device */
  290. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  291. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  292. station->sta.mode = 0;
  293. station->sta.sta.sta_id = sta_id;
  294. station->sta.station_flags = 0;
  295. /* BCAST station and IBSS stations do not work in HT mode */
  296. if (sta_id != priv->hw_params.bcast_sta_id &&
  297. priv->iw_mode != NL80211_IFTYPE_ADHOC)
  298. iwl_set_ht_add_station(priv, sta_id, ht_info);
  299. /* 3945 only */
  300. rate = (priv->band == IEEE80211_BAND_5GHZ) ?
  301. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
  302. /* Turn on both antennas for the station... */
  303. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  304. return sta_id;
  305. }
  306. #define STA_WAIT_TIMEOUT (HZ/2)
  307. /**
  308. * iwl_add_station_common -
  309. */
  310. int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
  311. bool is_ap,
  312. struct ieee80211_sta_ht_cap *ht_info,
  313. u8 *sta_id_r)
  314. {
  315. struct iwl_station_entry *station;
  316. unsigned long flags_spin;
  317. int ret = 0;
  318. u8 sta_id;
  319. *sta_id_r = 0;
  320. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  321. sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
  322. if (sta_id == IWL_INVALID_STATION) {
  323. IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
  324. addr);
  325. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  326. return -EINVAL;
  327. }
  328. /*
  329. * uCode is not able to deal with multiple requests to add a
  330. * station. Keep track if one is in progress so that we do not send
  331. * another.
  332. */
  333. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  334. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  335. sta_id);
  336. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  337. return -EEXIST;
  338. }
  339. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  340. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  341. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  342. sta_id, addr);
  343. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  344. return -EEXIST;
  345. }
  346. priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
  347. station = &priv->stations[sta_id];
  348. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  349. /* Add station to device's station table */
  350. ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
  351. if (ret) {
  352. IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
  353. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  354. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  355. priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  356. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  357. }
  358. *sta_id_r = sta_id;
  359. return ret;
  360. }
  361. EXPORT_SYMBOL(iwl_add_station_common);
  362. static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, bool is_ap)
  363. {
  364. int i, r;
  365. struct iwl_link_quality_cmd link_cmd = {
  366. .reserved1 = 0,
  367. };
  368. u32 rate_flags;
  369. /* Set up the rate scaling to start at selected rate, fall back
  370. * all the way down to 1M in IEEE order, and then spin on 1M */
  371. if (is_ap)
  372. r = IWL_RATE_54M_INDEX;
  373. else if (priv->band == IEEE80211_BAND_5GHZ)
  374. r = IWL_RATE_6M_INDEX;
  375. else
  376. r = IWL_RATE_1M_INDEX;
  377. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  378. rate_flags = 0;
  379. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  380. rate_flags |= RATE_MCS_CCK_MSK;
  381. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  382. RATE_MCS_ANT_POS;
  383. link_cmd.rs_table[i].rate_n_flags =
  384. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  385. r = iwl_get_prev_ieee_rate(r);
  386. }
  387. link_cmd.general_params.single_stream_ant_msk =
  388. first_antenna(priv->hw_params.valid_tx_ant);
  389. link_cmd.general_params.dual_stream_ant_msk = 3;
  390. link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  391. link_cmd.agg_params.agg_time_limit =
  392. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  393. /* Update the rate scaling for control frame Tx to AP */
  394. link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
  395. iwl_send_cmd_pdu(priv, REPLY_TX_LINK_QUALITY_CMD,
  396. sizeof(link_cmd), &link_cmd);
  397. }
  398. /*
  399. * iwl_add_local_stations - Add stations not requested by mac80211
  400. *
  401. * This will be either the broadcast station or the bssid station needed by
  402. * ad-hoc.
  403. *
  404. * Function sleeps.
  405. */
  406. int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs)
  407. {
  408. int ret;
  409. u8 sta_id;
  410. ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
  411. if (ret) {
  412. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  413. return ret;
  414. }
  415. if (init_rs)
  416. /* Set up default rate scaling table in device's station table */
  417. iwl_sta_init_lq(priv, addr, false);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL(iwl_add_local_station);
  421. /**
  422. * iwl_sta_ucode_deactivate - deactivate ucode status for a station
  423. *
  424. * priv->sta_lock must be held
  425. */
  426. static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
  427. {
  428. /* Ucode must be active and driver must be non active */
  429. if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
  430. IWL_ERR(priv, "removed non active STA %u\n", sta_id);
  431. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  432. memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
  433. IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
  434. }
  435. static int iwl_send_remove_station(struct iwl_priv *priv,
  436. struct iwl_station_entry *station)
  437. {
  438. struct iwl_rx_packet *pkt;
  439. int ret;
  440. unsigned long flags_spin;
  441. struct iwl_rem_sta_cmd rm_sta_cmd;
  442. struct iwl_host_cmd cmd = {
  443. .id = REPLY_REMOVE_STA,
  444. .len = sizeof(struct iwl_rem_sta_cmd),
  445. .flags = CMD_SYNC,
  446. .data = &rm_sta_cmd,
  447. };
  448. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  449. rm_sta_cmd.num_sta = 1;
  450. memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
  451. cmd.flags |= CMD_WANT_SKB;
  452. ret = iwl_send_cmd(priv, &cmd);
  453. if (ret)
  454. return ret;
  455. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  456. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  457. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  458. pkt->hdr.flags);
  459. ret = -EIO;
  460. }
  461. if (!ret) {
  462. switch (pkt->u.rem_sta.status) {
  463. case REM_STA_SUCCESS_MSK:
  464. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  465. iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
  466. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  467. IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
  468. break;
  469. default:
  470. ret = -EIO;
  471. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  472. break;
  473. }
  474. }
  475. iwl_free_pages(priv, cmd.reply_page);
  476. return ret;
  477. }
  478. /**
  479. * iwl_remove_station - Remove driver's knowledge of station.
  480. */
  481. static int iwl_remove_station(struct iwl_priv *priv, struct ieee80211_sta *sta)
  482. {
  483. int sta_id = IWL_INVALID_STATION;
  484. int i, ret = -EINVAL;
  485. unsigned long flags;
  486. bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
  487. struct iwl_station_entry *station;
  488. if (!iwl_is_ready(priv)) {
  489. IWL_DEBUG_INFO(priv,
  490. "Unable to remove station %pM, device not ready. \n",
  491. sta->addr);
  492. /*
  493. * It is typical for stations to be removed when we are
  494. * going down. Return success since device will be down
  495. * soon anyway
  496. */
  497. return 0;
  498. }
  499. spin_lock_irqsave(&priv->sta_lock, flags);
  500. if (is_ap)
  501. sta_id = IWL_AP_ID;
  502. else
  503. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  504. if (priv->stations[i].used &&
  505. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  506. sta->addr)) {
  507. sta_id = i;
  508. break;
  509. }
  510. if (unlikely(sta_id == IWL_INVALID_STATION))
  511. goto out;
  512. IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
  513. sta_id, sta->addr);
  514. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  515. IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
  516. sta->addr);
  517. goto out;
  518. }
  519. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  520. IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
  521. sta->addr);
  522. goto out;
  523. }
  524. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  525. priv->num_stations--;
  526. BUG_ON(priv->num_stations < 0);
  527. station = &priv->stations[sta_id];
  528. spin_unlock_irqrestore(&priv->sta_lock, flags);
  529. ret = iwl_send_remove_station(priv, station);
  530. return ret;
  531. out:
  532. spin_unlock_irqrestore(&priv->sta_lock, flags);
  533. return ret;
  534. }
  535. /**
  536. * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
  537. * @priv:
  538. * @force: If set then the uCode station table needs to be cleared here. If
  539. * not set then the uCode station table has already been cleared,
  540. * for example after sending it a RXON command without ASSOC bit
  541. * set, and we just need to change driver state here.
  542. */
  543. void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
  544. {
  545. int i;
  546. unsigned long flags_spin;
  547. bool cleared = false;
  548. IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
  549. force ? " and ucode" : "");
  550. if (force) {
  551. if (!iwl_is_ready(priv)) {
  552. /*
  553. * If device is not ready at this point the station
  554. * table is likely already empty (uCode not ready
  555. * to receive station requests) or will soon be
  556. * due to interface going down.
  557. */
  558. IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
  559. } else {
  560. iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
  561. }
  562. }
  563. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  564. if (force) {
  565. IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
  566. priv->num_stations = 0;
  567. memset(priv->stations, 0, sizeof(priv->stations));
  568. } else {
  569. for (i = 0; i < priv->hw_params.max_stations; i++) {
  570. if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
  571. IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d \n", i);
  572. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  573. cleared = true;
  574. }
  575. }
  576. }
  577. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  578. if (!cleared)
  579. IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
  580. }
  581. EXPORT_SYMBOL(iwl_clear_ucode_stations);
  582. /**
  583. * iwl_restore_stations() - Restore driver known stations to device
  584. *
  585. * All stations considered active by driver, but not present in ucode, is
  586. * restored.
  587. *
  588. * Function sleeps.
  589. */
  590. void iwl_restore_stations(struct iwl_priv *priv)
  591. {
  592. struct iwl_station_entry *station;
  593. unsigned long flags_spin;
  594. int i;
  595. bool found = false;
  596. int ret;
  597. if (!iwl_is_ready(priv)) {
  598. IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
  599. return;
  600. }
  601. IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
  602. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  603. for (i = 0; i < priv->hw_params.max_stations; i++) {
  604. if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
  605. !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
  606. IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
  607. priv->stations[i].sta.sta.addr);
  608. priv->stations[i].sta.mode = 0;
  609. priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
  610. found = true;
  611. }
  612. }
  613. for (i = 0; i < priv->hw_params.max_stations; i++) {
  614. if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
  615. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  616. station = &priv->stations[i];
  617. ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
  618. if (ret) {
  619. IWL_ERR(priv, "Adding station %pM failed.\n",
  620. station->sta.sta.addr);
  621. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  622. priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
  623. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  624. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  625. }
  626. /*
  627. * Rate scaling has already been initialized, send
  628. * current LQ command
  629. */
  630. if (station->lq)
  631. iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
  632. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  633. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  634. }
  635. }
  636. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  637. if (!found)
  638. IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
  639. else
  640. IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
  641. }
  642. EXPORT_SYMBOL(iwl_restore_stations);
  643. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  644. {
  645. int i;
  646. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  647. if (!test_and_set_bit(i, &priv->ucode_key_table))
  648. return i;
  649. return WEP_INVALID_OFFSET;
  650. }
  651. EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
  652. int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  653. {
  654. int i, not_empty = 0;
  655. u8 buff[sizeof(struct iwl_wep_cmd) +
  656. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  657. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  658. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  659. struct iwl_host_cmd cmd = {
  660. .id = REPLY_WEPKEY,
  661. .data = wep_cmd,
  662. .flags = CMD_SYNC,
  663. };
  664. might_sleep();
  665. memset(wep_cmd, 0, cmd_size +
  666. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  667. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  668. wep_cmd->key[i].key_index = i;
  669. if (priv->wep_keys[i].key_size) {
  670. wep_cmd->key[i].key_offset = i;
  671. not_empty = 1;
  672. } else {
  673. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  674. }
  675. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  676. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  677. priv->wep_keys[i].key_size);
  678. }
  679. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  680. wep_cmd->num_keys = WEP_KEYS_MAX;
  681. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  682. cmd.len = cmd_size;
  683. if (not_empty || send_if_empty)
  684. return iwl_send_cmd(priv, &cmd);
  685. else
  686. return 0;
  687. }
  688. EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
  689. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  690. struct ieee80211_key_conf *keyconf)
  691. {
  692. int ret;
  693. WARN_ON(!mutex_is_locked(&priv->mutex));
  694. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  695. keyconf->keyidx);
  696. if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
  697. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  698. keyconf->keyidx);
  699. priv->default_wep_key--;
  700. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  701. if (iwl_is_rfkill(priv)) {
  702. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  703. /* but keys in device are clear anyway so return success */
  704. return 0;
  705. }
  706. ret = iwl_send_static_wepkey_cmd(priv, 1);
  707. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  708. keyconf->keyidx, ret);
  709. return ret;
  710. }
  711. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  712. int iwl_set_default_wep_key(struct iwl_priv *priv,
  713. struct ieee80211_key_conf *keyconf)
  714. {
  715. int ret;
  716. WARN_ON(!mutex_is_locked(&priv->mutex));
  717. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  718. keyconf->keylen != WEP_KEY_LEN_64) {
  719. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  720. return -EINVAL;
  721. }
  722. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  723. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  724. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  725. priv->default_wep_key++;
  726. if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
  727. IWL_ERR(priv, "index %d already used in uCode key table.\n",
  728. keyconf->keyidx);
  729. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  730. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  731. keyconf->keylen);
  732. ret = iwl_send_static_wepkey_cmd(priv, 0);
  733. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  734. keyconf->keylen, keyconf->keyidx, ret);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL(iwl_set_default_wep_key);
  738. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  739. struct ieee80211_key_conf *keyconf,
  740. u8 sta_id)
  741. {
  742. unsigned long flags;
  743. __le16 key_flags = 0;
  744. int ret;
  745. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  746. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  747. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  748. key_flags &= ~STA_KEY_FLG_INVALID;
  749. if (keyconf->keylen == WEP_KEY_LEN_128)
  750. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  751. if (sta_id == priv->hw_params.bcast_sta_id)
  752. key_flags |= STA_KEY_MULTICAST_MSK;
  753. spin_lock_irqsave(&priv->sta_lock, flags);
  754. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  755. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  756. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  757. memcpy(priv->stations[sta_id].keyinfo.key,
  758. keyconf->key, keyconf->keylen);
  759. memcpy(&priv->stations[sta_id].sta.key.key[3],
  760. keyconf->key, keyconf->keylen);
  761. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  762. == STA_KEY_FLG_NO_ENC)
  763. priv->stations[sta_id].sta.key.key_offset =
  764. iwl_get_free_ucode_key_index(priv);
  765. /* else, we are overriding an existing key => no need to allocated room
  766. * in uCode. */
  767. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  768. "no space for a new key");
  769. priv->stations[sta_id].sta.key.key_flags = key_flags;
  770. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  771. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  772. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  773. spin_unlock_irqrestore(&priv->sta_lock, flags);
  774. return ret;
  775. }
  776. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  777. struct ieee80211_key_conf *keyconf,
  778. u8 sta_id)
  779. {
  780. unsigned long flags;
  781. __le16 key_flags = 0;
  782. int ret;
  783. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  784. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  785. key_flags &= ~STA_KEY_FLG_INVALID;
  786. if (sta_id == priv->hw_params.bcast_sta_id)
  787. key_flags |= STA_KEY_MULTICAST_MSK;
  788. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  789. spin_lock_irqsave(&priv->sta_lock, flags);
  790. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  791. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  792. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  793. keyconf->keylen);
  794. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  795. keyconf->keylen);
  796. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  797. == STA_KEY_FLG_NO_ENC)
  798. priv->stations[sta_id].sta.key.key_offset =
  799. iwl_get_free_ucode_key_index(priv);
  800. /* else, we are overriding an existing key => no need to allocated room
  801. * in uCode. */
  802. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  803. "no space for a new key");
  804. priv->stations[sta_id].sta.key.key_flags = key_flags;
  805. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  806. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  807. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  808. spin_unlock_irqrestore(&priv->sta_lock, flags);
  809. return ret;
  810. }
  811. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  812. struct ieee80211_key_conf *keyconf,
  813. u8 sta_id)
  814. {
  815. unsigned long flags;
  816. int ret = 0;
  817. __le16 key_flags = 0;
  818. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  819. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  820. key_flags &= ~STA_KEY_FLG_INVALID;
  821. if (sta_id == priv->hw_params.bcast_sta_id)
  822. key_flags |= STA_KEY_MULTICAST_MSK;
  823. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  824. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  825. spin_lock_irqsave(&priv->sta_lock, flags);
  826. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  827. priv->stations[sta_id].keyinfo.keylen = 16;
  828. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  829. == STA_KEY_FLG_NO_ENC)
  830. priv->stations[sta_id].sta.key.key_offset =
  831. iwl_get_free_ucode_key_index(priv);
  832. /* else, we are overriding an existing key => no need to allocated room
  833. * in uCode. */
  834. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  835. "no space for a new key");
  836. priv->stations[sta_id].sta.key.key_flags = key_flags;
  837. /* This copy is acutally not needed: we get the key with each TX */
  838. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  839. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  840. spin_unlock_irqrestore(&priv->sta_lock, flags);
  841. return ret;
  842. }
  843. void iwl_update_tkip_key(struct iwl_priv *priv,
  844. struct ieee80211_key_conf *keyconf,
  845. const u8 *addr, u32 iv32, u16 *phase1key)
  846. {
  847. u8 sta_id = IWL_INVALID_STATION;
  848. unsigned long flags;
  849. int i;
  850. sta_id = iwl_find_station(priv, addr);
  851. if (sta_id == IWL_INVALID_STATION) {
  852. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  853. addr);
  854. return;
  855. }
  856. if (iwl_scan_cancel(priv)) {
  857. /* cancel scan failed, just live w/ bad key and rely
  858. briefly on SW decryption */
  859. return;
  860. }
  861. spin_lock_irqsave(&priv->sta_lock, flags);
  862. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  863. for (i = 0; i < 5; i++)
  864. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  865. cpu_to_le16(phase1key[i]);
  866. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  867. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  868. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  869. spin_unlock_irqrestore(&priv->sta_lock, flags);
  870. }
  871. EXPORT_SYMBOL(iwl_update_tkip_key);
  872. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  873. struct ieee80211_key_conf *keyconf,
  874. u8 sta_id)
  875. {
  876. unsigned long flags;
  877. int ret = 0;
  878. u16 key_flags;
  879. u8 keyidx;
  880. priv->key_mapping_key--;
  881. spin_lock_irqsave(&priv->sta_lock, flags);
  882. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  883. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  884. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  885. keyconf->keyidx, sta_id);
  886. if (keyconf->keyidx != keyidx) {
  887. /* We need to remove a key with index different that the one
  888. * in the uCode. This means that the key we need to remove has
  889. * been replaced by another one with different index.
  890. * Don't do anything and return ok
  891. */
  892. spin_unlock_irqrestore(&priv->sta_lock, flags);
  893. return 0;
  894. }
  895. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  896. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  897. keyconf->keyidx, key_flags);
  898. spin_unlock_irqrestore(&priv->sta_lock, flags);
  899. return 0;
  900. }
  901. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  902. &priv->ucode_key_table))
  903. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  904. priv->stations[sta_id].sta.key.key_offset);
  905. memset(&priv->stations[sta_id].keyinfo, 0,
  906. sizeof(struct iwl_hw_key));
  907. memset(&priv->stations[sta_id].sta.key, 0,
  908. sizeof(struct iwl4965_keyinfo));
  909. priv->stations[sta_id].sta.key.key_flags =
  910. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  911. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  912. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  913. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  914. if (iwl_is_rfkill(priv)) {
  915. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled. \n");
  916. spin_unlock_irqrestore(&priv->sta_lock, flags);
  917. return 0;
  918. }
  919. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  920. spin_unlock_irqrestore(&priv->sta_lock, flags);
  921. return ret;
  922. }
  923. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  924. int iwl_set_dynamic_key(struct iwl_priv *priv,
  925. struct ieee80211_key_conf *keyconf, u8 sta_id)
  926. {
  927. int ret;
  928. priv->key_mapping_key++;
  929. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  930. switch (keyconf->alg) {
  931. case ALG_CCMP:
  932. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  933. break;
  934. case ALG_TKIP:
  935. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  936. break;
  937. case ALG_WEP:
  938. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  939. break;
  940. default:
  941. IWL_ERR(priv,
  942. "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  943. ret = -EINVAL;
  944. }
  945. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  946. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  947. sta_id, ret);
  948. return ret;
  949. }
  950. EXPORT_SYMBOL(iwl_set_dynamic_key);
  951. #ifdef CONFIG_IWLWIFI_DEBUG
  952. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  953. struct iwl_link_quality_cmd *lq)
  954. {
  955. int i;
  956. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  957. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  958. lq->general_params.single_stream_ant_msk,
  959. lq->general_params.dual_stream_ant_msk);
  960. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  961. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  962. i, lq->rs_table[i].rate_n_flags);
  963. }
  964. #else
  965. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  966. struct iwl_link_quality_cmd *lq)
  967. {
  968. }
  969. #endif
  970. /**
  971. * iwl_send_lq_cmd() - Send link quality command
  972. * @init: This command is sent as part of station initialization right
  973. * after station has been added.
  974. *
  975. * The link quality command is sent as the last step of station creation.
  976. * This is the special case in which init is set and we call a callback in
  977. * this case to clear the state indicating that station creation is in
  978. * progress.
  979. */
  980. int iwl_send_lq_cmd(struct iwl_priv *priv,
  981. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  982. {
  983. int ret = 0;
  984. unsigned long flags_spin;
  985. struct iwl_host_cmd cmd = {
  986. .id = REPLY_TX_LINK_QUALITY_CMD,
  987. .len = sizeof(struct iwl_link_quality_cmd),
  988. .flags = flags,
  989. .data = lq,
  990. };
  991. if ((lq->sta_id == 0xFF) &&
  992. (priv->iw_mode == NL80211_IFTYPE_ADHOC))
  993. return -EINVAL;
  994. if (lq->sta_id == 0xFF)
  995. lq->sta_id = IWL_AP_ID;
  996. iwl_dump_lq_cmd(priv, lq);
  997. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  998. iwl_dump_lq_cmd(priv, lq);
  999. ret = iwl_send_cmd(priv, &cmd);
  1000. if (ret || (cmd.flags & CMD_ASYNC))
  1001. return ret;
  1002. if (init) {
  1003. IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d \n",
  1004. lq->sta_id);
  1005. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  1006. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  1007. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  1008. }
  1009. return 0;
  1010. }
  1011. EXPORT_SYMBOL(iwl_send_lq_cmd);
  1012. /**
  1013. * iwl_add_bcast_station - add broadcast station into station table.
  1014. */
  1015. void iwl_add_bcast_station(struct iwl_priv *priv)
  1016. {
  1017. IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
  1018. iwl_add_local_station(priv, iwl_bcast_addr, true);
  1019. }
  1020. EXPORT_SYMBOL(iwl_add_bcast_station);
  1021. /**
  1022. * iwl3945_add_bcast_station - add broadcast station into station table.
  1023. */
  1024. void iwl3945_add_bcast_station(struct iwl_priv *priv)
  1025. {
  1026. IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
  1027. iwl_add_local_station(priv, iwl_bcast_addr, false);
  1028. /*
  1029. * It is assumed that when station is added more initialization
  1030. * needs to be done, but for 3945 it is not the case and we can
  1031. * just release station table access right here.
  1032. */
  1033. priv->stations[priv->hw_params.bcast_sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  1034. }
  1035. EXPORT_SYMBOL(iwl3945_add_bcast_station);
  1036. /**
  1037. * iwl_get_sta_id - Find station's index within station table
  1038. *
  1039. * If new IBSS station, create new entry in station table
  1040. */
  1041. int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  1042. {
  1043. int sta_id;
  1044. __le16 fc = hdr->frame_control;
  1045. /* If this frame is broadcast or management, use broadcast station id */
  1046. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
  1047. return priv->hw_params.bcast_sta_id;
  1048. switch (priv->iw_mode) {
  1049. /* If we are a client station in a BSS network, use the special
  1050. * AP station entry (that's the only station we communicate with) */
  1051. case NL80211_IFTYPE_STATION:
  1052. /*
  1053. * If addition of station not complete yet, which means
  1054. * that rate scaling has not been initialized, then return
  1055. * the broadcast station.
  1056. */
  1057. if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
  1058. return priv->hw_params.bcast_sta_id;
  1059. return IWL_AP_ID;
  1060. /* If we are an AP, then find the station, or use BCAST */
  1061. case NL80211_IFTYPE_AP:
  1062. sta_id = iwl_find_station(priv, hdr->addr1);
  1063. if (sta_id != IWL_INVALID_STATION)
  1064. return sta_id;
  1065. return priv->hw_params.bcast_sta_id;
  1066. /* If this frame is going out to an IBSS network, find the station,
  1067. * or create a new station table entry */
  1068. case NL80211_IFTYPE_ADHOC:
  1069. sta_id = iwl_find_station(priv, hdr->addr1);
  1070. if (sta_id != IWL_INVALID_STATION)
  1071. return sta_id;
  1072. IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
  1073. "Defaulting to broadcast...\n",
  1074. hdr->addr1);
  1075. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  1076. return priv->hw_params.bcast_sta_id;
  1077. default:
  1078. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  1079. priv->iw_mode);
  1080. return priv->hw_params.bcast_sta_id;
  1081. }
  1082. }
  1083. EXPORT_SYMBOL(iwl_get_sta_id);
  1084. /**
  1085. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1086. */
  1087. void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1088. {
  1089. unsigned long flags;
  1090. /* Remove "disable" flag, to enable Tx for this TID */
  1091. spin_lock_irqsave(&priv->sta_lock, flags);
  1092. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1093. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1094. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1095. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1096. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1097. }
  1098. EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
  1099. int iwl_sta_rx_agg_start(struct iwl_priv *priv,
  1100. const u8 *addr, int tid, u16 ssn)
  1101. {
  1102. unsigned long flags;
  1103. int sta_id;
  1104. sta_id = iwl_find_station(priv, addr);
  1105. if (sta_id == IWL_INVALID_STATION)
  1106. return -ENXIO;
  1107. spin_lock_irqsave(&priv->sta_lock, flags);
  1108. priv->stations[sta_id].sta.station_flags_msk = 0;
  1109. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1110. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1111. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1112. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1113. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1114. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1115. CMD_ASYNC);
  1116. }
  1117. EXPORT_SYMBOL(iwl_sta_rx_agg_start);
  1118. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
  1119. {
  1120. unsigned long flags;
  1121. int sta_id;
  1122. sta_id = iwl_find_station(priv, addr);
  1123. if (sta_id == IWL_INVALID_STATION) {
  1124. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1125. return -ENXIO;
  1126. }
  1127. spin_lock_irqsave(&priv->sta_lock, flags);
  1128. priv->stations[sta_id].sta.station_flags_msk = 0;
  1129. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1130. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1131. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1132. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1133. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1134. CMD_ASYNC);
  1135. }
  1136. EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
  1137. void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1138. {
  1139. unsigned long flags;
  1140. spin_lock_irqsave(&priv->sta_lock, flags);
  1141. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1142. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1143. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1144. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1145. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1146. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1147. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1148. }
  1149. EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
  1150. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1151. {
  1152. unsigned long flags;
  1153. spin_lock_irqsave(&priv->sta_lock, flags);
  1154. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1155. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1156. priv->stations[sta_id].sta.sta.modify_mask =
  1157. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1158. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1159. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1160. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1161. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1162. }
  1163. EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
  1164. int iwl_mac_sta_remove(struct ieee80211_hw *hw,
  1165. struct ieee80211_vif *vif,
  1166. struct ieee80211_sta *sta)
  1167. {
  1168. int ret;
  1169. struct iwl_priv *priv = hw->priv;
  1170. IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
  1171. sta->addr);
  1172. ret = iwl_remove_station(priv, sta);
  1173. if (ret)
  1174. IWL_ERR(priv, "Error removing station %pM\n",
  1175. sta->addr);
  1176. return ret;
  1177. }
  1178. EXPORT_SYMBOL(iwl_mac_sta_remove);