rxon.c 45 KB

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