iwl-agn-sta.c 40 KB

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