rxon.c 45 KB

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