iwl-sta.c 42 KB

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