iwl-agn-sta.c 39 KB

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