iwl-sta.c 42 KB

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