iwl-agn-rxon.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include "iwl-dev.h"
  27. #include "iwl-agn.h"
  28. #include "iwl-core.h"
  29. #include "iwl-agn-calib.h"
  30. #include "iwl-trans.h"
  31. #include "iwl-shared.h"
  32. static int iwlagn_disable_bss(struct iwl_priv *priv,
  33. struct iwl_rxon_context *ctx,
  34. struct iwl_rxon_cmd *send)
  35. {
  36. __le32 old_filter = send->filter_flags;
  37. int ret;
  38. send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  39. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
  40. CMD_SYNC, sizeof(*send), send);
  41. send->filter_flags = old_filter;
  42. if (ret)
  43. IWL_DEBUG_QUIET_RFKILL(priv,
  44. "Error clearing ASSOC_MSK on BSS (%d)\n", ret);
  45. return ret;
  46. }
  47. static int iwlagn_disable_pan(struct iwl_priv *priv,
  48. struct iwl_rxon_context *ctx,
  49. struct iwl_rxon_cmd *send)
  50. {
  51. struct iwl_notification_wait disable_wait;
  52. __le32 old_filter = send->filter_flags;
  53. u8 old_dev_type = send->dev_type;
  54. int ret;
  55. static const u8 deactivate_cmd[] = {
  56. REPLY_WIPAN_DEACTIVATION_COMPLETE
  57. };
  58. iwl_init_notification_wait(&priv->notif_wait, &disable_wait,
  59. deactivate_cmd, ARRAY_SIZE(deactivate_cmd),
  60. NULL, NULL);
  61. send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  62. send->dev_type = RXON_DEV_TYPE_P2P;
  63. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
  64. CMD_SYNC, sizeof(*send), send);
  65. send->filter_flags = old_filter;
  66. send->dev_type = old_dev_type;
  67. if (ret) {
  68. IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
  69. iwl_remove_notification(&priv->notif_wait, &disable_wait);
  70. } else {
  71. ret = iwl_wait_notification(&priv->notif_wait,
  72. &disable_wait, HZ);
  73. if (ret)
  74. IWL_ERR(priv, "Timed out waiting for PAN disable\n");
  75. }
  76. return ret;
  77. }
  78. static int iwlagn_disconn_pan(struct iwl_priv *priv,
  79. struct iwl_rxon_context *ctx,
  80. struct iwl_rxon_cmd *send)
  81. {
  82. __le32 old_filter = send->filter_flags;
  83. int ret;
  84. send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  85. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, CMD_SYNC,
  86. sizeof(*send), send);
  87. send->filter_flags = old_filter;
  88. return ret;
  89. }
  90. static void iwlagn_update_qos(struct iwl_priv *priv,
  91. struct iwl_rxon_context *ctx)
  92. {
  93. int ret;
  94. if (!ctx->is_active)
  95. return;
  96. ctx->qos_data.def_qos_parm.qos_flags = 0;
  97. if (ctx->qos_data.qos_active)
  98. ctx->qos_data.def_qos_parm.qos_flags |=
  99. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  100. if (ctx->ht.enabled)
  101. ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  102. IWL_DEBUG_INFO(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  103. ctx->qos_data.qos_active,
  104. ctx->qos_data.def_qos_parm.qos_flags);
  105. ret = iwl_dvm_send_cmd_pdu(priv, ctx->qos_cmd, CMD_SYNC,
  106. sizeof(struct iwl_qosparam_cmd),
  107. &ctx->qos_data.def_qos_parm);
  108. if (ret)
  109. IWL_DEBUG_QUIET_RFKILL(priv, "Failed to update QoS\n");
  110. }
  111. static int iwlagn_update_beacon(struct iwl_priv *priv,
  112. struct ieee80211_vif *vif)
  113. {
  114. lockdep_assert_held(&priv->mutex);
  115. dev_kfree_skb(priv->beacon_skb);
  116. priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
  117. if (!priv->beacon_skb)
  118. return -ENOMEM;
  119. return iwlagn_send_beacon_cmd(priv);
  120. }
  121. static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
  122. struct iwl_rxon_context *ctx)
  123. {
  124. int ret = 0;
  125. struct iwl_rxon_assoc_cmd rxon_assoc;
  126. const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
  127. const struct iwl_rxon_cmd *rxon2 = &ctx->active;
  128. if ((rxon1->flags == rxon2->flags) &&
  129. (rxon1->filter_flags == rxon2->filter_flags) &&
  130. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  131. (rxon1->ofdm_ht_single_stream_basic_rates ==
  132. rxon2->ofdm_ht_single_stream_basic_rates) &&
  133. (rxon1->ofdm_ht_dual_stream_basic_rates ==
  134. rxon2->ofdm_ht_dual_stream_basic_rates) &&
  135. (rxon1->ofdm_ht_triple_stream_basic_rates ==
  136. rxon2->ofdm_ht_triple_stream_basic_rates) &&
  137. (rxon1->acquisition_data == rxon2->acquisition_data) &&
  138. (rxon1->rx_chain == rxon2->rx_chain) &&
  139. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  140. IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
  141. return 0;
  142. }
  143. rxon_assoc.flags = ctx->staging.flags;
  144. rxon_assoc.filter_flags = ctx->staging.filter_flags;
  145. rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
  146. rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
  147. rxon_assoc.reserved1 = 0;
  148. rxon_assoc.reserved2 = 0;
  149. rxon_assoc.reserved3 = 0;
  150. rxon_assoc.ofdm_ht_single_stream_basic_rates =
  151. ctx->staging.ofdm_ht_single_stream_basic_rates;
  152. rxon_assoc.ofdm_ht_dual_stream_basic_rates =
  153. ctx->staging.ofdm_ht_dual_stream_basic_rates;
  154. rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
  155. rxon_assoc.ofdm_ht_triple_stream_basic_rates =
  156. ctx->staging.ofdm_ht_triple_stream_basic_rates;
  157. rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
  158. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_assoc_cmd,
  159. CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
  160. return ret;
  161. }
  162. static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  163. {
  164. u16 new_val;
  165. u16 beacon_factor;
  166. /*
  167. * If mac80211 hasn't given us a beacon interval, program
  168. * the default into the device (not checking this here
  169. * would cause the adjustment below to return the maximum
  170. * value, which may break PAN.)
  171. */
  172. if (!beacon_val)
  173. return DEFAULT_BEACON_INTERVAL;
  174. /*
  175. * If the beacon interval we obtained from the peer
  176. * is too large, we'll have to wake up more often
  177. * (and in IBSS case, we'll beacon too much)
  178. *
  179. * For example, if max_beacon_val is 4096, and the
  180. * requested beacon interval is 7000, we'll have to
  181. * use 3500 to be able to wake up on the beacons.
  182. *
  183. * This could badly influence beacon detection stats.
  184. */
  185. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  186. new_val = beacon_val / beacon_factor;
  187. if (!new_val)
  188. new_val = max_beacon_val;
  189. return new_val;
  190. }
  191. static int iwl_send_rxon_timing(struct iwl_priv *priv,
  192. struct iwl_rxon_context *ctx)
  193. {
  194. u64 tsf;
  195. s32 interval_tm, rem;
  196. struct ieee80211_conf *conf = NULL;
  197. u16 beacon_int;
  198. struct ieee80211_vif *vif = ctx->vif;
  199. conf = &priv->hw->conf;
  200. lockdep_assert_held(&priv->mutex);
  201. memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
  202. ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
  203. ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  204. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  205. /*
  206. * TODO: For IBSS we need to get atim_window from mac80211,
  207. * for now just always use 0
  208. */
  209. ctx->timing.atim_window = 0;
  210. if (ctx->ctxid == IWL_RXON_CTX_PAN &&
  211. (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
  212. iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
  213. priv->contexts[IWL_RXON_CTX_BSS].vif &&
  214. priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
  215. ctx->timing.beacon_interval =
  216. priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
  217. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  218. } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
  219. iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
  220. priv->contexts[IWL_RXON_CTX_PAN].vif &&
  221. priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
  222. (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
  223. !ctx->vif->bss_conf.beacon_int)) {
  224. ctx->timing.beacon_interval =
  225. priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
  226. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  227. } else {
  228. beacon_int = iwl_adjust_beacon_interval(beacon_int,
  229. IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
  230. ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
  231. }
  232. ctx->beacon_int = beacon_int;
  233. tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
  234. interval_tm = beacon_int * TIME_UNIT;
  235. rem = do_div(tsf, interval_tm);
  236. ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  237. ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
  238. IWL_DEBUG_ASSOC(priv,
  239. "beacon interval %d beacon timer %d beacon tim %d\n",
  240. le16_to_cpu(ctx->timing.beacon_interval),
  241. le32_to_cpu(ctx->timing.beacon_init_val),
  242. le16_to_cpu(ctx->timing.atim_window));
  243. return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
  244. CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
  245. }
  246. static int iwlagn_rxon_disconn(struct iwl_priv *priv,
  247. struct iwl_rxon_context *ctx)
  248. {
  249. int ret;
  250. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  251. if (ctx->ctxid == IWL_RXON_CTX_BSS) {
  252. ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
  253. } else {
  254. ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
  255. if (ret)
  256. return ret;
  257. if (ctx->vif) {
  258. ret = iwl_send_rxon_timing(priv, ctx);
  259. if (ret) {
  260. IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
  261. return ret;
  262. }
  263. ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
  264. }
  265. }
  266. if (ret)
  267. return ret;
  268. /*
  269. * Un-assoc RXON clears the station table and WEP
  270. * keys, so we have to restore those afterwards.
  271. */
  272. iwl_clear_ucode_stations(priv, ctx);
  273. /* update -- might need P2P now */
  274. iwl_update_bcast_station(priv, ctx);
  275. iwl_restore_stations(priv, ctx);
  276. ret = iwl_restore_default_wep_keys(priv, ctx);
  277. if (ret) {
  278. IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
  279. return ret;
  280. }
  281. memcpy(active, &ctx->staging, sizeof(*active));
  282. return 0;
  283. }
  284. static int iwlagn_rxon_connect(struct iwl_priv *priv,
  285. struct iwl_rxon_context *ctx)
  286. {
  287. int ret;
  288. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  289. /* RXON timing must be before associated RXON */
  290. if (ctx->ctxid == IWL_RXON_CTX_BSS) {
  291. ret = iwl_send_rxon_timing(priv, ctx);
  292. if (ret) {
  293. IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
  294. return ret;
  295. }
  296. }
  297. /* QoS info may be cleared by previous un-assoc RXON */
  298. iwlagn_update_qos(priv, ctx);
  299. /*
  300. * We'll run into this code path when beaconing is
  301. * enabled, but then we also need to send the beacon
  302. * to the device.
  303. */
  304. if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
  305. ret = iwlagn_update_beacon(priv, ctx->vif);
  306. if (ret) {
  307. IWL_ERR(priv,
  308. "Error sending required beacon (%d)!\n",
  309. ret);
  310. return ret;
  311. }
  312. }
  313. priv->start_calib = 0;
  314. /*
  315. * Apply the new configuration.
  316. *
  317. * Associated RXON doesn't clear the station table in uCode,
  318. * so we don't need to restore stations etc. after this.
  319. */
  320. ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, CMD_SYNC,
  321. sizeof(struct iwl_rxon_cmd), &ctx->staging);
  322. if (ret) {
  323. IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
  324. return ret;
  325. }
  326. memcpy(active, &ctx->staging, sizeof(*active));
  327. /* IBSS beacon needs to be sent after setting assoc */
  328. if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
  329. if (iwlagn_update_beacon(priv, ctx->vif))
  330. IWL_ERR(priv, "Error sending IBSS beacon\n");
  331. iwl_init_sensitivity(priv);
  332. /*
  333. * If we issue a new RXON command which required a tune then
  334. * we must send a new TXPOWER command or we won't be able to
  335. * Tx any frames.
  336. *
  337. * It's expected we set power here if channel is changing.
  338. */
  339. ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
  340. if (ret) {
  341. IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
  342. return ret;
  343. }
  344. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
  345. cfg(priv)->ht_params && cfg(priv)->ht_params->smps_mode)
  346. ieee80211_request_smps(ctx->vif,
  347. cfg(priv)->ht_params->smps_mode);
  348. return 0;
  349. }
  350. int iwlagn_set_pan_params(struct iwl_priv *priv)
  351. {
  352. struct iwl_wipan_params_cmd cmd;
  353. struct iwl_rxon_context *ctx_bss, *ctx_pan;
  354. int slot0 = 300, slot1 = 0;
  355. int ret;
  356. if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
  357. return 0;
  358. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  359. lockdep_assert_held(&priv->mutex);
  360. ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
  361. ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
  362. /*
  363. * If the PAN context is inactive, then we don't need
  364. * to update the PAN parameters, the last thing we'll
  365. * have done before it goes inactive is making the PAN
  366. * parameters be WLAN-only.
  367. */
  368. if (!ctx_pan->is_active)
  369. return 0;
  370. memset(&cmd, 0, sizeof(cmd));
  371. /* only 2 slots are currently allowed */
  372. cmd.num_slots = 2;
  373. cmd.slots[0].type = 0; /* BSS */
  374. cmd.slots[1].type = 1; /* PAN */
  375. if (priv->hw_roc_setup) {
  376. /* both contexts must be used for this to happen */
  377. slot1 = IWL_MIN_SLOT_TIME;
  378. slot0 = 3000;
  379. } else if (ctx_bss->vif && ctx_pan->vif) {
  380. int bcnint = ctx_pan->beacon_int;
  381. int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
  382. /* should be set, but seems unused?? */
  383. cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
  384. if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
  385. bcnint &&
  386. bcnint != ctx_bss->beacon_int) {
  387. IWL_ERR(priv,
  388. "beacon intervals don't match (%d, %d)\n",
  389. ctx_bss->beacon_int, ctx_pan->beacon_int);
  390. } else
  391. bcnint = max_t(int, bcnint,
  392. ctx_bss->beacon_int);
  393. if (!bcnint)
  394. bcnint = DEFAULT_BEACON_INTERVAL;
  395. slot0 = bcnint / 2;
  396. slot1 = bcnint - slot0;
  397. if (test_bit(STATUS_SCAN_HW, &priv->status) ||
  398. (!ctx_bss->vif->bss_conf.idle &&
  399. !ctx_bss->vif->bss_conf.assoc)) {
  400. slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  401. slot1 = IWL_MIN_SLOT_TIME;
  402. } else if (!ctx_pan->vif->bss_conf.idle &&
  403. !ctx_pan->vif->bss_conf.assoc) {
  404. slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  405. slot0 = IWL_MIN_SLOT_TIME;
  406. }
  407. } else if (ctx_pan->vif) {
  408. slot0 = 0;
  409. slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
  410. ctx_pan->beacon_int;
  411. slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
  412. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  413. slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
  414. slot1 = IWL_MIN_SLOT_TIME;
  415. }
  416. }
  417. cmd.slots[0].width = cpu_to_le16(slot0);
  418. cmd.slots[1].width = cpu_to_le16(slot1);
  419. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
  420. sizeof(cmd), &cmd);
  421. if (ret)
  422. IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
  423. return ret;
  424. }
  425. static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
  426. struct iwl_rxon_context *ctx, int hw_decrypt)
  427. {
  428. struct iwl_rxon_cmd *rxon = &ctx->staging;
  429. if (hw_decrypt)
  430. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  431. else
  432. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  433. }
  434. /**
  435. * iwlagn_commit_rxon - commit staging_rxon to hardware
  436. *
  437. * The RXON command in staging_rxon is committed to the hardware and
  438. * the active_rxon structure is updated with the new data. This
  439. * function correctly transitions out of the RXON_ASSOC_MSK state if
  440. * a HW tune is required based on the RXON structure changes.
  441. *
  442. * The connect/disconnect flow should be as the following:
  443. *
  444. * 1. make sure send RXON command with association bit unset if not connect
  445. * this should include the channel and the band for the candidate
  446. * to be connected to
  447. * 2. Add Station before RXON association with the AP
  448. * 3. RXON_timing has to send before RXON for connection
  449. * 4. full RXON command - associated bit set
  450. * 5. use RXON_ASSOC command to update any flags changes
  451. */
  452. int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  453. {
  454. /* cast away the const for active_rxon in this function */
  455. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  456. bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
  457. int ret;
  458. lockdep_assert_held(&priv->mutex);
  459. if (!iwl_is_alive(priv))
  460. return -EBUSY;
  461. /* This function hardcodes a bunch of dual-mode assumptions */
  462. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  463. if (!ctx->is_active)
  464. return 0;
  465. /* always get timestamp with Rx frame */
  466. ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
  467. /*
  468. * force CTS-to-self frames protection if RTS-CTS is not preferred
  469. * one aggregation protection method
  470. */
  471. if (!hw_params(priv).use_rts_for_aggregation)
  472. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  473. if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
  474. !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
  475. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  476. else
  477. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  478. iwl_print_rx_config_cmd(priv, ctx->ctxid);
  479. ret = iwl_check_rxon_cmd(priv, ctx);
  480. if (ret) {
  481. IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
  482. return -EINVAL;
  483. }
  484. /*
  485. * receive commit_rxon request
  486. * abort any previous channel switch if still in process
  487. */
  488. if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
  489. (priv->switch_channel != ctx->staging.channel)) {
  490. IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
  491. le16_to_cpu(priv->switch_channel));
  492. iwl_chswitch_done(priv, false);
  493. }
  494. /*
  495. * If we don't need to send a full RXON, we can use
  496. * iwl_rxon_assoc_cmd which is used to reconfigure filter
  497. * and other flags for the current radio configuration.
  498. */
  499. if (!iwl_full_rxon_required(priv, ctx)) {
  500. ret = iwlagn_send_rxon_assoc(priv, ctx);
  501. if (ret) {
  502. IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
  503. return ret;
  504. }
  505. memcpy(active, &ctx->staging, sizeof(*active));
  506. /*
  507. * We do not commit tx power settings while channel changing,
  508. * do it now if after settings changed.
  509. */
  510. iwl_set_tx_power(priv, priv->tx_power_next, false);
  511. /* make sure we are in the right PS state */
  512. iwl_power_update_mode(priv, true);
  513. return 0;
  514. }
  515. iwl_set_rxon_hwcrypto(priv, ctx, !iwlagn_mod_params.sw_crypto);
  516. IWL_DEBUG_INFO(priv,
  517. "Going to commit RXON\n"
  518. " * with%s RXON_FILTER_ASSOC_MSK\n"
  519. " * channel = %d\n"
  520. " * bssid = %pM\n",
  521. (new_assoc ? "" : "out"),
  522. le16_to_cpu(ctx->staging.channel),
  523. ctx->staging.bssid_addr);
  524. /*
  525. * Always clear associated first, but with the correct config.
  526. * This is required as for example station addition for the
  527. * AP station must be done after the BSSID is set to correctly
  528. * set up filters in the device.
  529. */
  530. ret = iwlagn_rxon_disconn(priv, ctx);
  531. if (ret)
  532. return ret;
  533. ret = iwlagn_set_pan_params(priv);
  534. if (ret)
  535. return ret;
  536. if (new_assoc)
  537. return iwlagn_rxon_connect(priv, ctx);
  538. return 0;
  539. }
  540. void iwlagn_config_ht40(struct ieee80211_conf *conf,
  541. struct iwl_rxon_context *ctx)
  542. {
  543. if (conf_is_ht40_minus(conf)) {
  544. ctx->ht.extension_chan_offset =
  545. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  546. ctx->ht.is_40mhz = true;
  547. } else if (conf_is_ht40_plus(conf)) {
  548. ctx->ht.extension_chan_offset =
  549. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  550. ctx->ht.is_40mhz = true;
  551. } else {
  552. ctx->ht.extension_chan_offset =
  553. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  554. ctx->ht.is_40mhz = false;
  555. }
  556. }
  557. int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
  558. {
  559. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  560. struct iwl_rxon_context *ctx;
  561. struct ieee80211_conf *conf = &hw->conf;
  562. struct ieee80211_channel *channel = conf->channel;
  563. const struct iwl_channel_info *ch_info;
  564. int ret = 0;
  565. IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
  566. mutex_lock(&priv->mutex);
  567. if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
  568. IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
  569. goto out;
  570. }
  571. if (!iwl_is_ready(priv)) {
  572. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  573. goto out;
  574. }
  575. if (changed & (IEEE80211_CONF_CHANGE_SMPS |
  576. IEEE80211_CONF_CHANGE_CHANNEL)) {
  577. /* mac80211 uses static for non-HT which is what we want */
  578. priv->current_ht_config.smps = conf->smps_mode;
  579. /*
  580. * Recalculate chain counts.
  581. *
  582. * If monitor mode is enabled then mac80211 will
  583. * set up the SM PS mode to OFF if an HT channel is
  584. * configured.
  585. */
  586. for_each_context(priv, ctx)
  587. iwlagn_set_rxon_chain(priv, ctx);
  588. }
  589. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  590. ch_info = iwl_get_channel_info(priv, channel->band,
  591. channel->hw_value);
  592. if (!is_channel_valid(ch_info)) {
  593. IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
  594. ret = -EINVAL;
  595. goto out;
  596. }
  597. for_each_context(priv, ctx) {
  598. /* Configure HT40 channels */
  599. if (ctx->ht.enabled != conf_is_ht(conf))
  600. ctx->ht.enabled = conf_is_ht(conf);
  601. if (ctx->ht.enabled) {
  602. /* if HT40 is used, it should not change
  603. * after associated except channel switch */
  604. if (!ctx->ht.is_40mhz ||
  605. !iwl_is_associated_ctx(ctx))
  606. iwlagn_config_ht40(conf, ctx);
  607. } else
  608. ctx->ht.is_40mhz = false;
  609. /*
  610. * Default to no protection. Protection mode will
  611. * later be set from BSS config in iwl_ht_conf
  612. */
  613. ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  614. /* if we are switching from ht to 2.4 clear flags
  615. * from any ht related info since 2.4 does not
  616. * support ht */
  617. if (le16_to_cpu(ctx->staging.channel) !=
  618. channel->hw_value)
  619. ctx->staging.flags = 0;
  620. iwl_set_rxon_channel(priv, channel, ctx);
  621. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  622. iwl_set_flags_for_band(priv, ctx, channel->band,
  623. ctx->vif);
  624. }
  625. iwl_update_bcast_stations(priv);
  626. /*
  627. * The list of supported rates and rate mask can be different
  628. * for each band; since the band may have changed, reset
  629. * the rate mask to what mac80211 lists.
  630. */
  631. iwl_set_rate(priv);
  632. }
  633. if (changed & (IEEE80211_CONF_CHANGE_PS |
  634. IEEE80211_CONF_CHANGE_IDLE)) {
  635. ret = iwl_power_update_mode(priv, false);
  636. if (ret)
  637. IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
  638. }
  639. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  640. IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
  641. priv->tx_power_user_lmt, conf->power_level);
  642. iwl_set_tx_power(priv, conf->power_level, false);
  643. }
  644. for_each_context(priv, ctx) {
  645. if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  646. continue;
  647. iwlagn_commit_rxon(priv, ctx);
  648. }
  649. out:
  650. mutex_unlock(&priv->mutex);
  651. IWL_DEBUG_MAC80211(priv, "leave\n");
  652. return ret;
  653. }
  654. static void iwlagn_check_needed_chains(struct iwl_priv *priv,
  655. struct iwl_rxon_context *ctx,
  656. struct ieee80211_bss_conf *bss_conf)
  657. {
  658. struct ieee80211_vif *vif = ctx->vif;
  659. struct iwl_rxon_context *tmp;
  660. struct ieee80211_sta *sta;
  661. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  662. struct ieee80211_sta_ht_cap *ht_cap;
  663. bool need_multiple;
  664. lockdep_assert_held(&priv->mutex);
  665. switch (vif->type) {
  666. case NL80211_IFTYPE_STATION:
  667. rcu_read_lock();
  668. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  669. if (!sta) {
  670. /*
  671. * If at all, this can only happen through a race
  672. * when the AP disconnects us while we're still
  673. * setting up the connection, in that case mac80211
  674. * will soon tell us about that.
  675. */
  676. need_multiple = false;
  677. rcu_read_unlock();
  678. break;
  679. }
  680. ht_cap = &sta->ht_cap;
  681. need_multiple = true;
  682. /*
  683. * If the peer advertises no support for receiving 2 and 3
  684. * stream MCS rates, it can't be transmitting them either.
  685. */
  686. if (ht_cap->mcs.rx_mask[1] == 0 &&
  687. ht_cap->mcs.rx_mask[2] == 0) {
  688. need_multiple = false;
  689. } else if (!(ht_cap->mcs.tx_params &
  690. IEEE80211_HT_MCS_TX_DEFINED)) {
  691. /* If it can't TX MCS at all ... */
  692. need_multiple = false;
  693. } else if (ht_cap->mcs.tx_params &
  694. IEEE80211_HT_MCS_TX_RX_DIFF) {
  695. int maxstreams;
  696. /*
  697. * But if it can receive them, it might still not
  698. * be able to transmit them, which is what we need
  699. * to check here -- so check the number of streams
  700. * it advertises for TX (if different from RX).
  701. */
  702. maxstreams = (ht_cap->mcs.tx_params &
  703. IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
  704. maxstreams >>=
  705. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  706. maxstreams += 1;
  707. if (maxstreams <= 1)
  708. need_multiple = false;
  709. }
  710. rcu_read_unlock();
  711. break;
  712. case NL80211_IFTYPE_ADHOC:
  713. /* currently */
  714. need_multiple = false;
  715. break;
  716. default:
  717. /* only AP really */
  718. need_multiple = true;
  719. break;
  720. }
  721. ctx->ht_need_multiple_chains = need_multiple;
  722. if (!need_multiple) {
  723. /* check all contexts */
  724. for_each_context(priv, tmp) {
  725. if (!tmp->vif)
  726. continue;
  727. if (tmp->ht_need_multiple_chains) {
  728. need_multiple = true;
  729. break;
  730. }
  731. }
  732. }
  733. ht_conf->single_chain_sufficient = !need_multiple;
  734. }
  735. static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
  736. {
  737. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  738. int ret;
  739. if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
  740. iwl_is_any_associated(priv)) {
  741. struct iwl_calib_chain_noise_reset_cmd cmd;
  742. /* clear data for chain noise calibration algorithm */
  743. data->chain_noise_a = 0;
  744. data->chain_noise_b = 0;
  745. data->chain_noise_c = 0;
  746. data->chain_signal_a = 0;
  747. data->chain_signal_b = 0;
  748. data->chain_signal_c = 0;
  749. data->beacon_count = 0;
  750. memset(&cmd, 0, sizeof(cmd));
  751. iwl_set_calib_hdr(&cmd.hdr,
  752. priv->phy_calib_chain_noise_reset_cmd);
  753. ret = iwl_dvm_send_cmd_pdu(priv,
  754. REPLY_PHY_CALIBRATION_CMD,
  755. CMD_SYNC, sizeof(cmd), &cmd);
  756. if (ret)
  757. IWL_ERR(priv,
  758. "Could not send REPLY_PHY_CALIBRATION_CMD\n");
  759. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  760. IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
  761. }
  762. }
  763. void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
  764. struct ieee80211_vif *vif,
  765. struct ieee80211_bss_conf *bss_conf,
  766. u32 changes)
  767. {
  768. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  769. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  770. int ret;
  771. bool force = false;
  772. mutex_lock(&priv->mutex);
  773. if (unlikely(!iwl_is_ready(priv))) {
  774. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  775. mutex_unlock(&priv->mutex);
  776. return;
  777. }
  778. if (unlikely(!ctx->vif)) {
  779. IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
  780. mutex_unlock(&priv->mutex);
  781. return;
  782. }
  783. if (changes & BSS_CHANGED_BEACON_INT)
  784. force = true;
  785. if (changes & BSS_CHANGED_QOS) {
  786. ctx->qos_data.qos_active = bss_conf->qos;
  787. iwlagn_update_qos(priv, ctx);
  788. }
  789. ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
  790. if (vif->bss_conf.use_short_preamble)
  791. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  792. else
  793. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  794. if (changes & BSS_CHANGED_ASSOC) {
  795. if (bss_conf->assoc) {
  796. priv->timestamp = bss_conf->last_tsf;
  797. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  798. } else {
  799. /*
  800. * If we disassociate while there are pending
  801. * frames, just wake up the queues and let the
  802. * frames "escape" ... This shouldn't really
  803. * be happening to start with, but we should
  804. * not get stuck in this case either since it
  805. * can happen if userspace gets confused.
  806. */
  807. iwlagn_lift_passive_no_rx(priv);
  808. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  809. if (ctx->ctxid == IWL_RXON_CTX_BSS)
  810. priv->have_rekey_data = false;
  811. }
  812. iwlagn_bt_coex_rssi_monitor(priv);
  813. }
  814. if (ctx->ht.enabled) {
  815. ctx->ht.protection = bss_conf->ht_operation_mode &
  816. IEEE80211_HT_OP_MODE_PROTECTION;
  817. ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
  818. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  819. iwlagn_check_needed_chains(priv, ctx, bss_conf);
  820. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  821. }
  822. iwlagn_set_rxon_chain(priv, ctx);
  823. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  824. ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  825. else
  826. ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  827. if (bss_conf->use_cts_prot)
  828. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  829. else
  830. ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  831. memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
  832. if (vif->type == NL80211_IFTYPE_AP ||
  833. vif->type == NL80211_IFTYPE_ADHOC) {
  834. if (vif->bss_conf.enable_beacon) {
  835. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  836. priv->beacon_ctx = ctx;
  837. } else {
  838. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  839. priv->beacon_ctx = NULL;
  840. }
  841. }
  842. /*
  843. * If the ucode decides to do beacon filtering before
  844. * association, it will lose beacons that are needed
  845. * before sending frames out on passive channels. This
  846. * causes association failures on those channels. Enable
  847. * receiving beacons in such cases.
  848. */
  849. if (vif->type == NL80211_IFTYPE_STATION) {
  850. if (!bss_conf->assoc)
  851. ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
  852. else
  853. ctx->staging.filter_flags &=
  854. ~RXON_FILTER_BCON_AWARE_MSK;
  855. }
  856. if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  857. iwlagn_commit_rxon(priv, ctx);
  858. if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
  859. /*
  860. * The chain noise calibration will enable PM upon
  861. * completion. If calibration has already been run
  862. * then we need to enable power management here.
  863. */
  864. if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
  865. iwl_power_update_mode(priv, false);
  866. /* Enable RX differential gain and sensitivity calibrations */
  867. if (!priv->disable_chain_noise_cal)
  868. iwlagn_chain_noise_reset(priv);
  869. priv->start_calib = 1;
  870. }
  871. if (changes & BSS_CHANGED_IBSS) {
  872. ret = iwlagn_manage_ibss_station(priv, vif,
  873. bss_conf->ibss_joined);
  874. if (ret)
  875. IWL_ERR(priv, "failed to %s IBSS station %pM\n",
  876. bss_conf->ibss_joined ? "add" : "remove",
  877. bss_conf->bssid);
  878. }
  879. if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_ADHOC &&
  880. priv->beacon_ctx) {
  881. if (iwlagn_update_beacon(priv, vif))
  882. IWL_ERR(priv, "Error sending IBSS beacon\n");
  883. }
  884. mutex_unlock(&priv->mutex);
  885. }
  886. void iwlagn_post_scan(struct iwl_priv *priv)
  887. {
  888. struct iwl_rxon_context *ctx;
  889. /*
  890. * We do not commit power settings while scan is pending,
  891. * do it now if the settings changed.
  892. */
  893. iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
  894. iwl_set_tx_power(priv, priv->tx_power_next, false);
  895. /*
  896. * Since setting the RXON may have been deferred while
  897. * performing the scan, fire one off if needed
  898. */
  899. for_each_context(priv, ctx)
  900. if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  901. iwlagn_commit_rxon(priv, ctx);
  902. iwlagn_set_pan_params(priv);
  903. }