rxon.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2015 Intel Deutschland GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  18. *
  19. * The full GNU General Public License is included in this distribution in the
  20. * file called LICENSE.
  21. *
  22. * Contact Information:
  23. * Intel Linux Wireless <linuxwifi@intel.com>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. *****************************************************************************/
  27. #include <linux/etherdevice.h>
  28. #include "iwl-trans.h"
  29. #include "iwl-modparams.h"
  30. #include "dev.h"
  31. #include "agn.h"
  32. #include "calib.h"
  33. /*
  34. * initialize rxon structure with default values from eeprom
  35. */
  36. void iwl_connection_init_rx_config(struct iwl_priv *priv,
  37. struct iwl_rxon_context *ctx)
  38. {
  39. memset(&ctx->staging, 0, sizeof(ctx->staging));
  40. if (!ctx->vif) {
  41. ctx->staging.dev_type = ctx->unused_devtype;
  42. } else
  43. switch (ctx->vif->type) {
  44. case NL80211_IFTYPE_AP:
  45. ctx->staging.dev_type = ctx->ap_devtype;
  46. break;
  47. case NL80211_IFTYPE_STATION:
  48. ctx->staging.dev_type = ctx->station_devtype;
  49. ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  50. break;
  51. case NL80211_IFTYPE_ADHOC:
  52. ctx->staging.dev_type = ctx->ibss_devtype;
  53. ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  54. ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  55. RXON_FILTER_ACCEPT_GRP_MSK;
  56. break;
  57. case NL80211_IFTYPE_MONITOR:
  58. ctx->staging.dev_type = RXON_DEV_TYPE_SNIFFER;
  59. break;
  60. default:
  61. IWL_ERR(priv, "Unsupported interface type %d\n",
  62. ctx->vif->type);
  63. break;
  64. }
  65. #if 0
  66. /* TODO: Figure out when short_preamble would be set and cache from
  67. * that */
  68. if (!hw_to_local(priv->hw)->short_preamble)
  69. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  70. else
  71. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  72. #endif
  73. ctx->staging.channel =
  74. cpu_to_le16(priv->hw->conf.chandef.chan->hw_value);
  75. priv->band = priv->hw->conf.chandef.chan->band;
  76. iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  77. /* clear both MIX and PURE40 mode flag */
  78. ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  79. RXON_FLG_CHANNEL_MODE_PURE_40);
  80. if (ctx->vif)
  81. memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  82. ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  83. ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  84. ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  85. }
  86. static int iwlagn_disable_bss(struct iwl_priv *priv,
  87. struct iwl_rxon_context *ctx,
  88. struct iwl_rxon_cmd *send)
  89. {
  90. __le32 old_filter = send->filter_flags;
  91. int ret;
  92. send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  93. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
  94. 0, sizeof(*send), send);
  95. send->filter_flags = old_filter;
  96. if (ret)
  97. IWL_DEBUG_QUIET_RFKILL(priv,
  98. "Error clearing ASSOC_MSK on BSS (%d)\n", ret);
  99. return ret;
  100. }
  101. static int iwlagn_disable_pan(struct iwl_priv *priv,
  102. struct iwl_rxon_context *ctx,
  103. struct iwl_rxon_cmd *send)
  104. {
  105. struct iwl_notification_wait disable_wait;
  106. __le32 old_filter = send->filter_flags;
  107. u8 old_dev_type = send->dev_type;
  108. int ret;
  109. static const u16 deactivate_cmd[] = {
  110. REPLY_WIPAN_DEACTIVATION_COMPLETE
  111. };
  112. iwl_init_notification_wait(&priv->notif_wait, &disable_wait,
  113. deactivate_cmd, ARRAY_SIZE(deactivate_cmd),
  114. NULL, NULL);
  115. send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  116. send->dev_type = RXON_DEV_TYPE_P2P;
  117. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
  118. 0, sizeof(*send), send);
  119. send->filter_flags = old_filter;
  120. send->dev_type = old_dev_type;
  121. if (ret) {
  122. IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
  123. iwl_remove_notification(&priv->notif_wait, &disable_wait);
  124. } else {
  125. ret = iwl_wait_notification(&priv->notif_wait,
  126. &disable_wait, HZ);
  127. if (ret)
  128. IWL_ERR(priv, "Timed out waiting for PAN disable\n");
  129. }
  130. return ret;
  131. }
  132. static int iwlagn_disconn_pan(struct iwl_priv *priv,
  133. struct iwl_rxon_context *ctx,
  134. struct iwl_rxon_cmd *send)
  135. {
  136. __le32 old_filter = send->filter_flags;
  137. int ret;
  138. send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  139. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, 0,
  140. sizeof(*send), send);
  141. send->filter_flags = old_filter;
  142. return ret;
  143. }
  144. static void iwlagn_update_qos(struct iwl_priv *priv,
  145. struct iwl_rxon_context *ctx)
  146. {
  147. int ret;
  148. if (!ctx->is_active)
  149. return;
  150. ctx->qos_data.def_qos_parm.qos_flags = 0;
  151. if (ctx->qos_data.qos_active)
  152. ctx->qos_data.def_qos_parm.qos_flags |=
  153. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  154. if (ctx->ht.enabled)
  155. ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  156. IWL_DEBUG_INFO(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  157. ctx->qos_data.qos_active,
  158. ctx->qos_data.def_qos_parm.qos_flags);
  159. ret = iwl_dvm_send_cmd_pdu(priv, ctx->qos_cmd, 0,
  160. sizeof(struct iwl_qosparam_cmd),
  161. &ctx->qos_data.def_qos_parm);
  162. if (ret)
  163. IWL_DEBUG_QUIET_RFKILL(priv, "Failed to update QoS\n");
  164. }
  165. static int iwlagn_update_beacon(struct iwl_priv *priv,
  166. struct ieee80211_vif *vif)
  167. {
  168. lockdep_assert_held(&priv->mutex);
  169. dev_kfree_skb(priv->beacon_skb);
  170. priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
  171. if (!priv->beacon_skb)
  172. return -ENOMEM;
  173. return iwlagn_send_beacon_cmd(priv);
  174. }
  175. static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
  176. struct iwl_rxon_context *ctx)
  177. {
  178. int ret = 0;
  179. struct iwl_rxon_assoc_cmd rxon_assoc;
  180. const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
  181. const struct iwl_rxon_cmd *rxon2 = &ctx->active;
  182. if ((rxon1->flags == rxon2->flags) &&
  183. (rxon1->filter_flags == rxon2->filter_flags) &&
  184. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  185. (rxon1->ofdm_ht_single_stream_basic_rates ==
  186. rxon2->ofdm_ht_single_stream_basic_rates) &&
  187. (rxon1->ofdm_ht_dual_stream_basic_rates ==
  188. rxon2->ofdm_ht_dual_stream_basic_rates) &&
  189. (rxon1->ofdm_ht_triple_stream_basic_rates ==
  190. rxon2->ofdm_ht_triple_stream_basic_rates) &&
  191. (rxon1->acquisition_data == rxon2->acquisition_data) &&
  192. (rxon1->rx_chain == rxon2->rx_chain) &&
  193. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  194. IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
  195. return 0;
  196. }
  197. rxon_assoc.flags = ctx->staging.flags;
  198. rxon_assoc.filter_flags = ctx->staging.filter_flags;
  199. rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
  200. rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
  201. rxon_assoc.reserved1 = 0;
  202. rxon_assoc.reserved2 = 0;
  203. rxon_assoc.reserved3 = 0;
  204. rxon_assoc.ofdm_ht_single_stream_basic_rates =
  205. ctx->staging.ofdm_ht_single_stream_basic_rates;
  206. rxon_assoc.ofdm_ht_dual_stream_basic_rates =
  207. ctx->staging.ofdm_ht_dual_stream_basic_rates;
  208. rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
  209. rxon_assoc.ofdm_ht_triple_stream_basic_rates =
  210. ctx->staging.ofdm_ht_triple_stream_basic_rates;
  211. rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
  212. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_assoc_cmd,
  213. CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
  214. return ret;
  215. }
  216. static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  217. {
  218. u16 new_val;
  219. u16 beacon_factor;
  220. /*
  221. * If mac80211 hasn't given us a beacon interval, program
  222. * the default into the device (not checking this here
  223. * would cause the adjustment below to return the maximum
  224. * value, which may break PAN.)
  225. */
  226. if (!beacon_val)
  227. return DEFAULT_BEACON_INTERVAL;
  228. /*
  229. * If the beacon interval we obtained from the peer
  230. * is too large, we'll have to wake up more often
  231. * (and in IBSS case, we'll beacon too much)
  232. *
  233. * For example, if max_beacon_val is 4096, and the
  234. * requested beacon interval is 7000, we'll have to
  235. * use 3500 to be able to wake up on the beacons.
  236. *
  237. * This could badly influence beacon detection stats.
  238. */
  239. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  240. new_val = beacon_val / beacon_factor;
  241. if (!new_val)
  242. new_val = max_beacon_val;
  243. return new_val;
  244. }
  245. static int iwl_send_rxon_timing(struct iwl_priv *priv,
  246. struct iwl_rxon_context *ctx)
  247. {
  248. u64 tsf;
  249. s32 interval_tm, rem;
  250. struct ieee80211_conf *conf = NULL;
  251. u16 beacon_int;
  252. struct ieee80211_vif *vif = ctx->vif;
  253. conf = &priv->hw->conf;
  254. lockdep_assert_held(&priv->mutex);
  255. memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
  256. ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
  257. ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  258. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  259. /*
  260. * TODO: For IBSS we need to get atim_window from mac80211,
  261. * for now just always use 0
  262. */
  263. ctx->timing.atim_window = 0;
  264. if (ctx->ctxid == IWL_RXON_CTX_PAN &&
  265. (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
  266. iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
  267. priv->contexts[IWL_RXON_CTX_BSS].vif &&
  268. priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
  269. ctx->timing.beacon_interval =
  270. priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
  271. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  272. } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
  273. iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
  274. priv->contexts[IWL_RXON_CTX_PAN].vif &&
  275. priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
  276. (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
  277. !ctx->vif->bss_conf.beacon_int)) {
  278. ctx->timing.beacon_interval =
  279. priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
  280. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  281. } else {
  282. beacon_int = iwl_adjust_beacon_interval(beacon_int,
  283. IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
  284. ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
  285. }
  286. ctx->beacon_int = beacon_int;
  287. tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
  288. interval_tm = beacon_int * TIME_UNIT;
  289. rem = do_div(tsf, interval_tm);
  290. ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  291. ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
  292. IWL_DEBUG_ASSOC(priv,
  293. "beacon interval %d beacon timer %d beacon tim %d\n",
  294. le16_to_cpu(ctx->timing.beacon_interval),
  295. le32_to_cpu(ctx->timing.beacon_init_val),
  296. le16_to_cpu(ctx->timing.atim_window));
  297. return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
  298. 0, sizeof(ctx->timing), &ctx->timing);
  299. }
  300. static int iwlagn_rxon_disconn(struct iwl_priv *priv,
  301. struct iwl_rxon_context *ctx)
  302. {
  303. int ret;
  304. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  305. if (ctx->ctxid == IWL_RXON_CTX_BSS) {
  306. ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
  307. } else {
  308. ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
  309. if (ret)
  310. return ret;
  311. if (ctx->vif) {
  312. ret = iwl_send_rxon_timing(priv, ctx);
  313. if (ret) {
  314. IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
  315. return ret;
  316. }
  317. ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
  318. }
  319. }
  320. if (ret)
  321. return ret;
  322. /*
  323. * Un-assoc RXON clears the station table and WEP
  324. * keys, so we have to restore those afterwards.
  325. */
  326. iwl_clear_ucode_stations(priv, ctx);
  327. /* update -- might need P2P now */
  328. iwl_update_bcast_station(priv, ctx);
  329. iwl_restore_stations(priv, ctx);
  330. ret = iwl_restore_default_wep_keys(priv, ctx);
  331. if (ret) {
  332. IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
  333. return ret;
  334. }
  335. memcpy(active, &ctx->staging, sizeof(*active));
  336. return 0;
  337. }
  338. static int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  339. {
  340. int ret;
  341. s8 prev_tx_power;
  342. bool defer;
  343. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  344. if (priv->calib_disabled & IWL_TX_POWER_CALIB_DISABLED)
  345. return 0;
  346. lockdep_assert_held(&priv->mutex);
  347. if (priv->tx_power_user_lmt == tx_power && !force)
  348. return 0;
  349. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  350. IWL_WARN(priv,
  351. "Requested user TXPOWER %d below lower limit %d.\n",
  352. tx_power,
  353. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  354. return -EINVAL;
  355. }
  356. if (tx_power > DIV_ROUND_UP(priv->nvm_data->max_tx_pwr_half_dbm, 2)) {
  357. IWL_WARN(priv,
  358. "Requested user TXPOWER %d above upper limit %d.\n",
  359. tx_power, priv->nvm_data->max_tx_pwr_half_dbm);
  360. return -EINVAL;
  361. }
  362. if (!iwl_is_ready_rf(priv))
  363. return -EIO;
  364. /* scan complete and commit_rxon use tx_power_next value,
  365. * it always need to be updated for newest request */
  366. priv->tx_power_next = tx_power;
  367. /* do not set tx power when scanning or channel changing */
  368. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  369. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  370. if (defer && !force) {
  371. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  372. return 0;
  373. }
  374. prev_tx_power = priv->tx_power_user_lmt;
  375. priv->tx_power_user_lmt = tx_power;
  376. ret = iwlagn_send_tx_power(priv);
  377. /* if fail to set tx_power, restore the orig. tx power */
  378. if (ret) {
  379. priv->tx_power_user_lmt = prev_tx_power;
  380. priv->tx_power_next = prev_tx_power;
  381. }
  382. return ret;
  383. }
  384. static int iwlagn_rxon_connect(struct iwl_priv *priv,
  385. struct iwl_rxon_context *ctx)
  386. {
  387. int ret;
  388. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  389. /* RXON timing must be before associated RXON */
  390. if (ctx->ctxid == IWL_RXON_CTX_BSS) {
  391. ret = iwl_send_rxon_timing(priv, ctx);
  392. if (ret) {
  393. IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
  394. return ret;
  395. }
  396. }
  397. /* QoS info may be cleared by previous un-assoc RXON */
  398. iwlagn_update_qos(priv, ctx);
  399. /*
  400. * We'll run into this code path when beaconing is
  401. * enabled, but then we also need to send the beacon
  402. * to the device.
  403. */
  404. if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
  405. ret = iwlagn_update_beacon(priv, ctx->vif);
  406. if (ret) {
  407. IWL_ERR(priv,
  408. "Error sending required beacon (%d)!\n",
  409. ret);
  410. return ret;
  411. }
  412. }
  413. priv->start_calib = 0;
  414. /*
  415. * Apply the new configuration.
  416. *
  417. * Associated RXON doesn't clear the station table in uCode,
  418. * so we don't need to restore stations etc. after this.
  419. */
  420. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, 0,
  421. sizeof(struct iwl_rxon_cmd), &ctx->staging);
  422. if (ret) {
  423. IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
  424. return ret;
  425. }
  426. memcpy(active, &ctx->staging, sizeof(*active));
  427. /* IBSS beacon needs to be sent after setting assoc */
  428. if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
  429. if (iwlagn_update_beacon(priv, ctx->vif))
  430. IWL_ERR(priv, "Error sending IBSS beacon\n");
  431. iwl_init_sensitivity(priv);
  432. /*
  433. * If we issue a new RXON command which required a tune then
  434. * we must send a new TXPOWER command or we won't be able to
  435. * Tx any frames.
  436. *
  437. * It's expected we set power here if channel is changing.
  438. */
  439. ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
  440. if (ret) {
  441. IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
  442. return ret;
  443. }
  444. return 0;
  445. }
  446. int iwlagn_set_pan_params(struct iwl_priv *priv)
  447. {
  448. struct iwl_wipan_params_cmd cmd;
  449. struct iwl_rxon_context *ctx_bss, *ctx_pan;
  450. int slot0 = 300, slot1 = 0;
  451. int ret;
  452. if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
  453. return 0;
  454. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  455. lockdep_assert_held(&priv->mutex);
  456. ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
  457. ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
  458. /*
  459. * If the PAN context is inactive, then we don't need
  460. * to update the PAN parameters, the last thing we'll
  461. * have done before it goes inactive is making the PAN
  462. * parameters be WLAN-only.
  463. */
  464. if (!ctx_pan->is_active)
  465. return 0;
  466. memset(&cmd, 0, sizeof(cmd));
  467. /* only 2 slots are currently allowed */
  468. cmd.num_slots = 2;
  469. cmd.slots[0].type = 0; /* BSS */
  470. cmd.slots[1].type = 1; /* PAN */
  471. if (ctx_bss->vif && ctx_pan->vif) {
  472. int bcnint = ctx_pan->beacon_int;
  473. int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
  474. /* should be set, but seems unused?? */
  475. cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
  476. if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
  477. bcnint &&
  478. bcnint != ctx_bss->beacon_int) {
  479. IWL_ERR(priv,
  480. "beacon intervals don't match (%d, %d)\n",
  481. ctx_bss->beacon_int, ctx_pan->beacon_int);
  482. } else
  483. bcnint = max_t(int, bcnint,
  484. ctx_bss->beacon_int);
  485. if (!bcnint)
  486. bcnint = DEFAULT_BEACON_INTERVAL;
  487. slot0 = bcnint / 2;
  488. slot1 = bcnint - slot0;
  489. if (test_bit(STATUS_SCAN_HW, &priv->status) ||
  490. (!ctx_bss->vif->bss_conf.idle &&
  491. !ctx_bss->vif->bss_conf.assoc)) {
  492. slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  493. slot1 = IWL_MIN_SLOT_TIME;
  494. } else if (!ctx_pan->vif->bss_conf.idle &&
  495. !ctx_pan->vif->bss_conf.assoc) {
  496. slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  497. slot0 = IWL_MIN_SLOT_TIME;
  498. }
  499. } else if (ctx_pan->vif) {
  500. slot0 = 0;
  501. slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
  502. ctx_pan->beacon_int;
  503. slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
  504. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  505. slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
  506. slot1 = IWL_MIN_SLOT_TIME;
  507. }
  508. }
  509. cmd.slots[0].width = cpu_to_le16(slot0);
  510. cmd.slots[1].width = cpu_to_le16(slot1);
  511. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, 0,
  512. sizeof(cmd), &cmd);
  513. if (ret)
  514. IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
  515. return ret;
  516. }
  517. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  518. struct iwl_ht_config *ht_conf,
  519. struct iwl_rxon_context *ctx)
  520. {
  521. struct iwl_rxon_cmd *rxon = &ctx->staging;
  522. if (!ctx->ht.enabled) {
  523. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  524. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  525. RXON_FLG_HT40_PROT_MSK |
  526. RXON_FLG_HT_PROT_MSK);
  527. return;
  528. }
  529. /* FIXME: if the definition of ht.protection changed, the "translation"
  530. * will be needed for rxon->flags
  531. */
  532. rxon->flags |= cpu_to_le32(ctx->ht.protection <<
  533. RXON_FLG_HT_OPERATING_MODE_POS);
  534. /* Set up channel bandwidth:
  535. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  536. /* clear the HT channel mode before set the mode */
  537. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  538. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  539. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  540. /* pure ht40 */
  541. if (ctx->ht.protection ==
  542. IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  543. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  544. /*
  545. * Note: control channel is opposite of extension
  546. * channel
  547. */
  548. switch (ctx->ht.extension_chan_offset) {
  549. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  550. rxon->flags &=
  551. ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  552. break;
  553. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  554. rxon->flags |=
  555. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  556. break;
  557. }
  558. } else {
  559. /*
  560. * Note: control channel is opposite of extension
  561. * channel
  562. */
  563. switch (ctx->ht.extension_chan_offset) {
  564. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  565. rxon->flags &=
  566. ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  567. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  568. break;
  569. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  570. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  571. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  572. break;
  573. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  574. default:
  575. /*
  576. * channel location only valid if in Mixed
  577. * mode
  578. */
  579. IWL_ERR(priv,
  580. "invalid extension channel offset\n");
  581. break;
  582. }
  583. }
  584. } else {
  585. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  586. }
  587. iwlagn_set_rxon_chain(priv, ctx);
  588. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  589. "extension channel offset 0x%x\n",
  590. le32_to_cpu(rxon->flags), ctx->ht.protection,
  591. ctx->ht.extension_chan_offset);
  592. }
  593. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  594. {
  595. struct iwl_rxon_context *ctx;
  596. for_each_context(priv, ctx)
  597. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  598. }
  599. /**
  600. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  601. * @ch: requested channel as a pointer to struct ieee80211_channel
  602. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  603. * in the staging RXON flag structure based on the ch->band
  604. */
  605. void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  606. struct iwl_rxon_context *ctx)
  607. {
  608. enum nl80211_band band = ch->band;
  609. u16 channel = ch->hw_value;
  610. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  611. (priv->band == band))
  612. return;
  613. ctx->staging.channel = cpu_to_le16(channel);
  614. if (band == NL80211_BAND_5GHZ)
  615. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  616. else
  617. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  618. priv->band = band;
  619. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  620. }
  621. void iwl_set_flags_for_band(struct iwl_priv *priv,
  622. struct iwl_rxon_context *ctx,
  623. enum nl80211_band band,
  624. struct ieee80211_vif *vif)
  625. {
  626. if (band == NL80211_BAND_5GHZ) {
  627. ctx->staging.flags &=
  628. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  629. | RXON_FLG_CCK_MSK);
  630. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  631. } else {
  632. /* Copied from iwl_post_associate() */
  633. if (vif && vif->bss_conf.use_short_slot)
  634. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  635. else
  636. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  637. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  638. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  639. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  640. }
  641. }
  642. static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
  643. struct iwl_rxon_context *ctx, int hw_decrypt)
  644. {
  645. struct iwl_rxon_cmd *rxon = &ctx->staging;
  646. if (hw_decrypt)
  647. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  648. else
  649. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  650. }
  651. /* validate RXON structure is valid */
  652. static int iwl_check_rxon_cmd(struct iwl_priv *priv,
  653. struct iwl_rxon_context *ctx)
  654. {
  655. struct iwl_rxon_cmd *rxon = &ctx->staging;
  656. u32 errors = 0;
  657. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  658. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  659. IWL_WARN(priv, "check 2.4G: wrong narrow\n");
  660. errors |= BIT(0);
  661. }
  662. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  663. IWL_WARN(priv, "check 2.4G: wrong radar\n");
  664. errors |= BIT(1);
  665. }
  666. } else {
  667. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  668. IWL_WARN(priv, "check 5.2G: not short slot!\n");
  669. errors |= BIT(2);
  670. }
  671. if (rxon->flags & RXON_FLG_CCK_MSK) {
  672. IWL_WARN(priv, "check 5.2G: CCK!\n");
  673. errors |= BIT(3);
  674. }
  675. }
  676. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  677. IWL_WARN(priv, "mac/bssid mcast!\n");
  678. errors |= BIT(4);
  679. }
  680. /* make sure basic rates 6Mbps and 1Mbps are supported */
  681. if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
  682. (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
  683. IWL_WARN(priv, "neither 1 nor 6 are basic\n");
  684. errors |= BIT(5);
  685. }
  686. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  687. IWL_WARN(priv, "aid > 2007\n");
  688. errors |= BIT(6);
  689. }
  690. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  691. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  692. IWL_WARN(priv, "CCK and short slot\n");
  693. errors |= BIT(7);
  694. }
  695. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  696. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  697. IWL_WARN(priv, "CCK and auto detect\n");
  698. errors |= BIT(8);
  699. }
  700. if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  701. RXON_FLG_TGG_PROTECT_MSK)) ==
  702. RXON_FLG_TGG_PROTECT_MSK) {
  703. IWL_WARN(priv, "TGg but no auto-detect\n");
  704. errors |= BIT(9);
  705. }
  706. if (rxon->channel == 0) {
  707. IWL_WARN(priv, "zero channel is invalid\n");
  708. errors |= BIT(10);
  709. }
  710. WARN(errors, "Invalid RXON (%#x), channel %d",
  711. errors, le16_to_cpu(rxon->channel));
  712. return errors ? -EINVAL : 0;
  713. }
  714. /**
  715. * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  716. * @priv: staging_rxon is compared to active_rxon
  717. *
  718. * If the RXON structure is changing enough to require a new tune,
  719. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  720. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  721. */
  722. static int iwl_full_rxon_required(struct iwl_priv *priv,
  723. struct iwl_rxon_context *ctx)
  724. {
  725. const struct iwl_rxon_cmd *staging = &ctx->staging;
  726. const struct iwl_rxon_cmd *active = &ctx->active;
  727. #define CHK(cond) \
  728. if ((cond)) { \
  729. IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
  730. return 1; \
  731. }
  732. #define CHK_NEQ(c1, c2) \
  733. if ((c1) != (c2)) { \
  734. IWL_DEBUG_INFO(priv, "need full RXON - " \
  735. #c1 " != " #c2 " - %d != %d\n", \
  736. (c1), (c2)); \
  737. return 1; \
  738. }
  739. /* These items are only settable from the full RXON command */
  740. CHK(!iwl_is_associated_ctx(ctx));
  741. CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
  742. CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
  743. CHK(!ether_addr_equal(staging->wlap_bssid_addr,
  744. active->wlap_bssid_addr));
  745. CHK_NEQ(staging->dev_type, active->dev_type);
  746. CHK_NEQ(staging->channel, active->channel);
  747. CHK_NEQ(staging->air_propagation, active->air_propagation);
  748. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  749. active->ofdm_ht_single_stream_basic_rates);
  750. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  751. active->ofdm_ht_dual_stream_basic_rates);
  752. CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
  753. active->ofdm_ht_triple_stream_basic_rates);
  754. CHK_NEQ(staging->assoc_id, active->assoc_id);
  755. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  756. * be updated with the RXON_ASSOC command -- however only some
  757. * flag transitions are allowed using RXON_ASSOC */
  758. /* Check if we are not switching bands */
  759. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  760. active->flags & RXON_FLG_BAND_24G_MSK);
  761. /* Check if we are switching association toggle */
  762. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  763. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  764. #undef CHK
  765. #undef CHK_NEQ
  766. return 0;
  767. }
  768. #ifdef CONFIG_IWLWIFI_DEBUG
  769. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  770. enum iwl_rxon_context_id ctxid)
  771. {
  772. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  773. struct iwl_rxon_cmd *rxon = &ctx->staging;
  774. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  775. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  776. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
  777. le16_to_cpu(rxon->channel));
  778. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n",
  779. le32_to_cpu(rxon->flags));
  780. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  781. le32_to_cpu(rxon->filter_flags));
  782. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  783. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  784. rxon->ofdm_basic_rates);
  785. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
  786. rxon->cck_basic_rates);
  787. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  788. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  789. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
  790. le16_to_cpu(rxon->assoc_id));
  791. }
  792. #endif
  793. static void iwl_calc_basic_rates(struct iwl_priv *priv,
  794. struct iwl_rxon_context *ctx)
  795. {
  796. int lowest_present_ofdm = 100;
  797. int lowest_present_cck = 100;
  798. u8 cck = 0;
  799. u8 ofdm = 0;
  800. if (ctx->vif) {
  801. struct ieee80211_supported_band *sband;
  802. unsigned long basic = ctx->vif->bss_conf.basic_rates;
  803. int i;
  804. sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
  805. for_each_set_bit(i, &basic, BITS_PER_LONG) {
  806. int hw = sband->bitrates[i].hw_value;
  807. if (hw >= IWL_FIRST_OFDM_RATE) {
  808. ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
  809. if (lowest_present_ofdm > hw)
  810. lowest_present_ofdm = hw;
  811. } else {
  812. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  813. cck |= BIT(hw);
  814. if (lowest_present_cck > hw)
  815. lowest_present_cck = hw;
  816. }
  817. }
  818. }
  819. /*
  820. * Now we've got the basic rates as bitmaps in the ofdm and cck
  821. * variables. This isn't sufficient though, as there might not
  822. * be all the right rates in the bitmap. E.g. if the only basic
  823. * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
  824. * and 6 Mbps because the 802.11-2007 standard says in 9.6:
  825. *
  826. * [...] a STA responding to a received frame shall transmit
  827. * its Control Response frame [...] at the highest rate in the
  828. * BSSBasicRateSet parameter that is less than or equal to the
  829. * rate of the immediately previous frame in the frame exchange
  830. * sequence ([...]) and that is of the same modulation class
  831. * ([...]) as the received frame. If no rate contained in the
  832. * BSSBasicRateSet parameter meets these conditions, then the
  833. * control frame sent in response to a received frame shall be
  834. * transmitted at the highest mandatory rate of the PHY that is
  835. * less than or equal to the rate of the received frame, and
  836. * that is of the same modulation class as the received frame.
  837. *
  838. * As a consequence, we need to add all mandatory rates that are
  839. * lower than all of the basic rates to these bitmaps.
  840. */
  841. if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
  842. ofdm |= IWL_RATE_24M_MASK >> IWL_FIRST_OFDM_RATE;
  843. if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
  844. ofdm |= IWL_RATE_12M_MASK >> IWL_FIRST_OFDM_RATE;
  845. /* 6M already there or needed so always add */
  846. ofdm |= IWL_RATE_6M_MASK >> IWL_FIRST_OFDM_RATE;
  847. /*
  848. * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
  849. * Note, however:
  850. * - if no CCK rates are basic, it must be ERP since there must
  851. * be some basic rates at all, so they're OFDM => ERP PHY
  852. * (or we're in 5 GHz, and the cck bitmap will never be used)
  853. * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
  854. * - if 5.5M is basic, 1M and 2M are mandatory
  855. * - if 2M is basic, 1M is mandatory
  856. * - if 1M is basic, that's the only valid ACK rate.
  857. * As a consequence, it's not as complicated as it sounds, just add
  858. * any lower rates to the ACK rate bitmap.
  859. */
  860. if (IWL_RATE_11M_INDEX < lowest_present_cck)
  861. cck |= IWL_RATE_11M_MASK >> IWL_FIRST_CCK_RATE;
  862. if (IWL_RATE_5M_INDEX < lowest_present_cck)
  863. cck |= IWL_RATE_5M_MASK >> IWL_FIRST_CCK_RATE;
  864. if (IWL_RATE_2M_INDEX < lowest_present_cck)
  865. cck |= IWL_RATE_2M_MASK >> IWL_FIRST_CCK_RATE;
  866. /* 1M already there or needed so always add */
  867. cck |= IWL_RATE_1M_MASK >> IWL_FIRST_CCK_RATE;
  868. IWL_DEBUG_RATE(priv, "Set basic rates cck:0x%.2x ofdm:0x%.2x\n",
  869. cck, ofdm);
  870. /* "basic_rates" is a misnomer here -- should be called ACK rates */
  871. ctx->staging.cck_basic_rates = cck;
  872. ctx->staging.ofdm_basic_rates = ofdm;
  873. }
  874. /**
  875. * iwlagn_commit_rxon - commit staging_rxon to hardware
  876. *
  877. * The RXON command in staging_rxon is committed to the hardware and
  878. * the active_rxon structure is updated with the new data. This
  879. * function correctly transitions out of the RXON_ASSOC_MSK state if
  880. * a HW tune is required based on the RXON structure changes.
  881. *
  882. * The connect/disconnect flow should be as the following:
  883. *
  884. * 1. make sure send RXON command with association bit unset if not connect
  885. * this should include the channel and the band for the candidate
  886. * to be connected to
  887. * 2. Add Station before RXON association with the AP
  888. * 3. RXON_timing has to send before RXON for connection
  889. * 4. full RXON command - associated bit set
  890. * 5. use RXON_ASSOC command to update any flags changes
  891. */
  892. int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  893. {
  894. /* cast away the const for active_rxon in this function */
  895. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  896. bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
  897. int ret;
  898. lockdep_assert_held(&priv->mutex);
  899. if (!iwl_is_alive(priv))
  900. return -EBUSY;
  901. /* This function hardcodes a bunch of dual-mode assumptions */
  902. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  903. if (!ctx->is_active)
  904. return 0;
  905. /* always get timestamp with Rx frame */
  906. ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
  907. /* recalculate basic rates */
  908. iwl_calc_basic_rates(priv, ctx);
  909. /*
  910. * force CTS-to-self frames protection if RTS-CTS is not preferred
  911. * one aggregation protection method
  912. */
  913. if (!priv->hw_params.use_rts_for_aggregation)
  914. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  915. if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
  916. !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
  917. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  918. else
  919. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  920. iwl_print_rx_config_cmd(priv, ctx->ctxid);
  921. ret = iwl_check_rxon_cmd(priv, ctx);
  922. if (ret) {
  923. IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
  924. return -EINVAL;
  925. }
  926. /*
  927. * receive commit_rxon request
  928. * abort any previous channel switch if still in process
  929. */
  930. if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
  931. (priv->switch_channel != ctx->staging.channel)) {
  932. IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
  933. le16_to_cpu(priv->switch_channel));
  934. iwl_chswitch_done(priv, false);
  935. }
  936. /*
  937. * If we don't need to send a full RXON, we can use
  938. * iwl_rxon_assoc_cmd which is used to reconfigure filter
  939. * and other flags for the current radio configuration.
  940. */
  941. if (!iwl_full_rxon_required(priv, ctx)) {
  942. ret = iwlagn_send_rxon_assoc(priv, ctx);
  943. if (ret) {
  944. IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
  945. return ret;
  946. }
  947. memcpy(active, &ctx->staging, sizeof(*active));
  948. /*
  949. * We do not commit tx power settings while channel changing,
  950. * do it now if after settings changed.
  951. */
  952. iwl_set_tx_power(priv, priv->tx_power_next, false);
  953. /* make sure we are in the right PS state */
  954. iwl_power_update_mode(priv, true);
  955. return 0;
  956. }
  957. iwl_set_rxon_hwcrypto(priv, ctx, !iwlwifi_mod_params.swcrypto);
  958. IWL_DEBUG_INFO(priv,
  959. "Going to commit RXON\n"
  960. " * with%s RXON_FILTER_ASSOC_MSK\n"
  961. " * channel = %d\n"
  962. " * bssid = %pM\n",
  963. (new_assoc ? "" : "out"),
  964. le16_to_cpu(ctx->staging.channel),
  965. ctx->staging.bssid_addr);
  966. /*
  967. * Always clear associated first, but with the correct config.
  968. * This is required as for example station addition for the
  969. * AP station must be done after the BSSID is set to correctly
  970. * set up filters in the device.
  971. */
  972. ret = iwlagn_rxon_disconn(priv, ctx);
  973. if (ret)
  974. return ret;
  975. ret = iwlagn_set_pan_params(priv);
  976. if (ret)
  977. return ret;
  978. if (new_assoc)
  979. return iwlagn_rxon_connect(priv, ctx);
  980. return 0;
  981. }
  982. void iwlagn_config_ht40(struct ieee80211_conf *conf,
  983. struct iwl_rxon_context *ctx)
  984. {
  985. if (conf_is_ht40_minus(conf)) {
  986. ctx->ht.extension_chan_offset =
  987. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  988. ctx->ht.is_40mhz = true;
  989. } else if (conf_is_ht40_plus(conf)) {
  990. ctx->ht.extension_chan_offset =
  991. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  992. ctx->ht.is_40mhz = true;
  993. } else {
  994. ctx->ht.extension_chan_offset =
  995. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  996. ctx->ht.is_40mhz = false;
  997. }
  998. }
  999. int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
  1000. {
  1001. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1002. struct iwl_rxon_context *ctx;
  1003. struct ieee80211_conf *conf = &hw->conf;
  1004. struct ieee80211_channel *channel = conf->chandef.chan;
  1005. int ret = 0;
  1006. IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
  1007. mutex_lock(&priv->mutex);
  1008. if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
  1009. IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
  1010. goto out;
  1011. }
  1012. if (!iwl_is_ready(priv)) {
  1013. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  1014. goto out;
  1015. }
  1016. if (changed & (IEEE80211_CONF_CHANGE_SMPS |
  1017. IEEE80211_CONF_CHANGE_CHANNEL)) {
  1018. /* mac80211 uses static for non-HT which is what we want */
  1019. priv->current_ht_config.smps = conf->smps_mode;
  1020. /*
  1021. * Recalculate chain counts.
  1022. *
  1023. * If monitor mode is enabled then mac80211 will
  1024. * set up the SM PS mode to OFF if an HT channel is
  1025. * configured.
  1026. */
  1027. for_each_context(priv, ctx)
  1028. iwlagn_set_rxon_chain(priv, ctx);
  1029. }
  1030. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1031. for_each_context(priv, ctx) {
  1032. /* Configure HT40 channels */
  1033. if (ctx->ht.enabled != conf_is_ht(conf))
  1034. ctx->ht.enabled = conf_is_ht(conf);
  1035. if (ctx->ht.enabled) {
  1036. /* if HT40 is used, it should not change
  1037. * after associated except channel switch */
  1038. if (!ctx->ht.is_40mhz ||
  1039. !iwl_is_associated_ctx(ctx))
  1040. iwlagn_config_ht40(conf, ctx);
  1041. } else
  1042. ctx->ht.is_40mhz = false;
  1043. /*
  1044. * Default to no protection. Protection mode will
  1045. * later be set from BSS config in iwl_ht_conf
  1046. */
  1047. ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  1048. /* if we are switching from ht to 2.4 clear flags
  1049. * from any ht related info since 2.4 does not
  1050. * support ht */
  1051. if (le16_to_cpu(ctx->staging.channel) !=
  1052. channel->hw_value)
  1053. ctx->staging.flags = 0;
  1054. iwl_set_rxon_channel(priv, channel, ctx);
  1055. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  1056. iwl_set_flags_for_band(priv, ctx, channel->band,
  1057. ctx->vif);
  1058. }
  1059. iwl_update_bcast_stations(priv);
  1060. }
  1061. if (changed & (IEEE80211_CONF_CHANGE_PS |
  1062. IEEE80211_CONF_CHANGE_IDLE)) {
  1063. ret = iwl_power_update_mode(priv, false);
  1064. if (ret)
  1065. IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
  1066. }
  1067. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1068. IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
  1069. priv->tx_power_user_lmt, conf->power_level);
  1070. iwl_set_tx_power(priv, conf->power_level, false);
  1071. }
  1072. for_each_context(priv, ctx) {
  1073. if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  1074. continue;
  1075. iwlagn_commit_rxon(priv, ctx);
  1076. }
  1077. out:
  1078. mutex_unlock(&priv->mutex);
  1079. IWL_DEBUG_MAC80211(priv, "leave\n");
  1080. return ret;
  1081. }
  1082. static void iwlagn_check_needed_chains(struct iwl_priv *priv,
  1083. struct iwl_rxon_context *ctx,
  1084. struct ieee80211_bss_conf *bss_conf)
  1085. {
  1086. struct ieee80211_vif *vif = ctx->vif;
  1087. struct iwl_rxon_context *tmp;
  1088. struct ieee80211_sta *sta;
  1089. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  1090. struct ieee80211_sta_ht_cap *ht_cap;
  1091. bool need_multiple;
  1092. lockdep_assert_held(&priv->mutex);
  1093. switch (vif->type) {
  1094. case NL80211_IFTYPE_STATION:
  1095. rcu_read_lock();
  1096. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  1097. if (!sta) {
  1098. /*
  1099. * If at all, this can only happen through a race
  1100. * when the AP disconnects us while we're still
  1101. * setting up the connection, in that case mac80211
  1102. * will soon tell us about that.
  1103. */
  1104. need_multiple = false;
  1105. rcu_read_unlock();
  1106. break;
  1107. }
  1108. ht_cap = &sta->ht_cap;
  1109. need_multiple = true;
  1110. /*
  1111. * If the peer advertises no support for receiving 2 and 3
  1112. * stream MCS rates, it can't be transmitting them either.
  1113. */
  1114. if (ht_cap->mcs.rx_mask[1] == 0 &&
  1115. ht_cap->mcs.rx_mask[2] == 0) {
  1116. need_multiple = false;
  1117. } else if (!(ht_cap->mcs.tx_params &
  1118. IEEE80211_HT_MCS_TX_DEFINED)) {
  1119. /* If it can't TX MCS at all ... */
  1120. need_multiple = false;
  1121. } else if (ht_cap->mcs.tx_params &
  1122. IEEE80211_HT_MCS_TX_RX_DIFF) {
  1123. int maxstreams;
  1124. /*
  1125. * But if it can receive them, it might still not
  1126. * be able to transmit them, which is what we need
  1127. * to check here -- so check the number of streams
  1128. * it advertises for TX (if different from RX).
  1129. */
  1130. maxstreams = (ht_cap->mcs.tx_params &
  1131. IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
  1132. maxstreams >>=
  1133. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1134. maxstreams += 1;
  1135. if (maxstreams <= 1)
  1136. need_multiple = false;
  1137. }
  1138. rcu_read_unlock();
  1139. break;
  1140. case NL80211_IFTYPE_ADHOC:
  1141. /* currently */
  1142. need_multiple = false;
  1143. break;
  1144. default:
  1145. /* only AP really */
  1146. need_multiple = true;
  1147. break;
  1148. }
  1149. ctx->ht_need_multiple_chains = need_multiple;
  1150. if (!need_multiple) {
  1151. /* check all contexts */
  1152. for_each_context(priv, tmp) {
  1153. if (!tmp->vif)
  1154. continue;
  1155. if (tmp->ht_need_multiple_chains) {
  1156. need_multiple = true;
  1157. break;
  1158. }
  1159. }
  1160. }
  1161. ht_conf->single_chain_sufficient = !need_multiple;
  1162. }
  1163. static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
  1164. {
  1165. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  1166. int ret;
  1167. if (priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED)
  1168. return;
  1169. if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
  1170. iwl_is_any_associated(priv)) {
  1171. struct iwl_calib_chain_noise_reset_cmd cmd;
  1172. /* clear data for chain noise calibration algorithm */
  1173. data->chain_noise_a = 0;
  1174. data->chain_noise_b = 0;
  1175. data->chain_noise_c = 0;
  1176. data->chain_signal_a = 0;
  1177. data->chain_signal_b = 0;
  1178. data->chain_signal_c = 0;
  1179. data->beacon_count = 0;
  1180. memset(&cmd, 0, sizeof(cmd));
  1181. iwl_set_calib_hdr(&cmd.hdr,
  1182. priv->phy_calib_chain_noise_reset_cmd);
  1183. ret = iwl_dvm_send_cmd_pdu(priv,
  1184. REPLY_PHY_CALIBRATION_CMD,
  1185. 0, sizeof(cmd), &cmd);
  1186. if (ret)
  1187. IWL_ERR(priv,
  1188. "Could not send REPLY_PHY_CALIBRATION_CMD\n");
  1189. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  1190. IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
  1191. }
  1192. }
  1193. void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
  1194. struct ieee80211_vif *vif,
  1195. struct ieee80211_bss_conf *bss_conf,
  1196. u32 changes)
  1197. {
  1198. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1199. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1200. int ret;
  1201. bool force = false;
  1202. mutex_lock(&priv->mutex);
  1203. if (changes & BSS_CHANGED_IDLE && bss_conf->idle) {
  1204. /*
  1205. * If we go idle, then clearly no "passive-no-rx"
  1206. * workaround is needed any more, this is a reset.
  1207. */
  1208. iwlagn_lift_passive_no_rx(priv);
  1209. }
  1210. if (unlikely(!iwl_is_ready(priv))) {
  1211. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  1212. mutex_unlock(&priv->mutex);
  1213. return;
  1214. }
  1215. if (unlikely(!ctx->vif)) {
  1216. IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
  1217. mutex_unlock(&priv->mutex);
  1218. return;
  1219. }
  1220. if (changes & BSS_CHANGED_BEACON_INT)
  1221. force = true;
  1222. if (changes & BSS_CHANGED_QOS) {
  1223. ctx->qos_data.qos_active = bss_conf->qos;
  1224. iwlagn_update_qos(priv, ctx);
  1225. }
  1226. ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
  1227. if (vif->bss_conf.use_short_preamble)
  1228. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  1229. else
  1230. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  1231. if (changes & BSS_CHANGED_ASSOC) {
  1232. if (bss_conf->assoc) {
  1233. priv->timestamp = bss_conf->sync_tsf;
  1234. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1235. } else {
  1236. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1237. if (ctx->ctxid == IWL_RXON_CTX_BSS)
  1238. priv->have_rekey_data = false;
  1239. }
  1240. iwlagn_bt_coex_rssi_monitor(priv);
  1241. }
  1242. if (ctx->ht.enabled) {
  1243. ctx->ht.protection = bss_conf->ht_operation_mode &
  1244. IEEE80211_HT_OP_MODE_PROTECTION;
  1245. ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
  1246. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  1247. iwlagn_check_needed_chains(priv, ctx, bss_conf);
  1248. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  1249. }
  1250. iwlagn_set_rxon_chain(priv, ctx);
  1251. if (bss_conf->use_cts_prot && (priv->band != NL80211_BAND_5GHZ))
  1252. ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  1253. else
  1254. ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  1255. if (bss_conf->use_cts_prot)
  1256. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  1257. else
  1258. ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  1259. memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
  1260. if (vif->type == NL80211_IFTYPE_AP ||
  1261. vif->type == NL80211_IFTYPE_ADHOC) {
  1262. if (vif->bss_conf.enable_beacon) {
  1263. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1264. priv->beacon_ctx = ctx;
  1265. } else {
  1266. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1267. priv->beacon_ctx = NULL;
  1268. }
  1269. }
  1270. /*
  1271. * If the ucode decides to do beacon filtering before
  1272. * association, it will lose beacons that are needed
  1273. * before sending frames out on passive channels. This
  1274. * causes association failures on those channels. Enable
  1275. * receiving beacons in such cases.
  1276. */
  1277. if (vif->type == NL80211_IFTYPE_STATION) {
  1278. if (!bss_conf->assoc)
  1279. ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
  1280. else
  1281. ctx->staging.filter_flags &=
  1282. ~RXON_FILTER_BCON_AWARE_MSK;
  1283. }
  1284. if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  1285. iwlagn_commit_rxon(priv, ctx);
  1286. if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
  1287. /*
  1288. * The chain noise calibration will enable PM upon
  1289. * completion. If calibration has already been run
  1290. * then we need to enable power management here.
  1291. */
  1292. if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
  1293. iwl_power_update_mode(priv, false);
  1294. /* Enable RX differential gain and sensitivity calibrations */
  1295. iwlagn_chain_noise_reset(priv);
  1296. priv->start_calib = 1;
  1297. }
  1298. if (changes & BSS_CHANGED_IBSS) {
  1299. ret = iwlagn_manage_ibss_station(priv, vif,
  1300. bss_conf->ibss_joined);
  1301. if (ret)
  1302. IWL_ERR(priv, "failed to %s IBSS station %pM\n",
  1303. bss_conf->ibss_joined ? "add" : "remove",
  1304. bss_conf->bssid);
  1305. }
  1306. if (changes & BSS_CHANGED_BEACON && priv->beacon_ctx == ctx) {
  1307. if (iwlagn_update_beacon(priv, vif))
  1308. IWL_ERR(priv, "Error updating beacon\n");
  1309. }
  1310. mutex_unlock(&priv->mutex);
  1311. }
  1312. void iwlagn_post_scan(struct iwl_priv *priv)
  1313. {
  1314. struct iwl_rxon_context *ctx;
  1315. /*
  1316. * We do not commit power settings while scan is pending,
  1317. * do it now if the settings changed.
  1318. */
  1319. iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
  1320. iwl_set_tx_power(priv, priv->tx_power_next, false);
  1321. /*
  1322. * Since setting the RXON may have been deferred while
  1323. * performing the scan, fire one off if needed
  1324. */
  1325. for_each_context(priv, ctx)
  1326. if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  1327. iwlagn_commit_rxon(priv, ctx);
  1328. iwlagn_set_pan_params(priv);
  1329. }