iwl-sta.c 42 KB

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