sta.c 39 KB

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