sta.c 40 KB

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