iwl-agn-rxon.c 43 KB

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