rxon.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2013 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. CMD_SYNC, 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. CMD_SYNC, 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, CMD_SYNC,
  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, CMD_SYNC,
  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. CMD_SYNC, 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, CMD_SYNC,
  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 (priv->hw_roc_setup) {
  475. /* both contexts must be used for this to happen */
  476. slot1 = IWL_MIN_SLOT_TIME;
  477. slot0 = 3000;
  478. } else if (ctx_bss->vif && ctx_pan->vif) {
  479. int bcnint = ctx_pan->beacon_int;
  480. int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
  481. /* should be set, but seems unused?? */
  482. cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
  483. if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
  484. bcnint &&
  485. bcnint != ctx_bss->beacon_int) {
  486. IWL_ERR(priv,
  487. "beacon intervals don't match (%d, %d)\n",
  488. ctx_bss->beacon_int, ctx_pan->beacon_int);
  489. } else
  490. bcnint = max_t(int, bcnint,
  491. ctx_bss->beacon_int);
  492. if (!bcnint)
  493. bcnint = DEFAULT_BEACON_INTERVAL;
  494. slot0 = bcnint / 2;
  495. slot1 = bcnint - slot0;
  496. if (test_bit(STATUS_SCAN_HW, &priv->status) ||
  497. (!ctx_bss->vif->bss_conf.idle &&
  498. !ctx_bss->vif->bss_conf.assoc)) {
  499. slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  500. slot1 = IWL_MIN_SLOT_TIME;
  501. } else if (!ctx_pan->vif->bss_conf.idle &&
  502. !ctx_pan->vif->bss_conf.assoc) {
  503. slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
  504. slot0 = IWL_MIN_SLOT_TIME;
  505. }
  506. } else if (ctx_pan->vif) {
  507. slot0 = 0;
  508. slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
  509. ctx_pan->beacon_int;
  510. slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
  511. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  512. slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
  513. slot1 = IWL_MIN_SLOT_TIME;
  514. }
  515. }
  516. cmd.slots[0].width = cpu_to_le16(slot0);
  517. cmd.slots[1].width = cpu_to_le16(slot1);
  518. ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
  519. sizeof(cmd), &cmd);
  520. if (ret)
  521. IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
  522. return ret;
  523. }
  524. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  525. struct iwl_ht_config *ht_conf,
  526. struct iwl_rxon_context *ctx)
  527. {
  528. struct iwl_rxon_cmd *rxon = &ctx->staging;
  529. if (!ctx->ht.enabled) {
  530. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  531. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  532. RXON_FLG_HT40_PROT_MSK |
  533. RXON_FLG_HT_PROT_MSK);
  534. return;
  535. }
  536. /* FIXME: if the definition of ht.protection changed, the "translation"
  537. * will be needed for rxon->flags
  538. */
  539. rxon->flags |= cpu_to_le32(ctx->ht.protection <<
  540. RXON_FLG_HT_OPERATING_MODE_POS);
  541. /* Set up channel bandwidth:
  542. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  543. /* clear the HT channel mode before set the mode */
  544. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  545. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  546. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  547. /* pure ht40 */
  548. if (ctx->ht.protection ==
  549. IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  550. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  551. /*
  552. * Note: control channel is opposite of extension
  553. * channel
  554. */
  555. switch (ctx->ht.extension_chan_offset) {
  556. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  557. rxon->flags &=
  558. ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  559. break;
  560. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  561. rxon->flags |=
  562. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  563. break;
  564. }
  565. } else {
  566. /*
  567. * Note: control channel is opposite of extension
  568. * channel
  569. */
  570. switch (ctx->ht.extension_chan_offset) {
  571. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  572. rxon->flags &=
  573. ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  574. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  575. break;
  576. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  577. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  578. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  579. break;
  580. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  581. default:
  582. /*
  583. * channel location only valid if in Mixed
  584. * mode
  585. */
  586. IWL_ERR(priv,
  587. "invalid extension channel offset\n");
  588. break;
  589. }
  590. }
  591. } else {
  592. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  593. }
  594. iwlagn_set_rxon_chain(priv, ctx);
  595. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  596. "extension channel offset 0x%x\n",
  597. le32_to_cpu(rxon->flags), ctx->ht.protection,
  598. ctx->ht.extension_chan_offset);
  599. }
  600. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  601. {
  602. struct iwl_rxon_context *ctx;
  603. for_each_context(priv, ctx)
  604. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  605. }
  606. /**
  607. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  608. * @ch: requested channel as a pointer to struct ieee80211_channel
  609. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  610. * in the staging RXON flag structure based on the ch->band
  611. */
  612. void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  613. struct iwl_rxon_context *ctx)
  614. {
  615. enum ieee80211_band band = ch->band;
  616. u16 channel = ch->hw_value;
  617. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  618. (priv->band == band))
  619. return;
  620. ctx->staging.channel = cpu_to_le16(channel);
  621. if (band == IEEE80211_BAND_5GHZ)
  622. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  623. else
  624. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  625. priv->band = band;
  626. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  627. }
  628. void iwl_set_flags_for_band(struct iwl_priv *priv,
  629. struct iwl_rxon_context *ctx,
  630. enum ieee80211_band band,
  631. struct ieee80211_vif *vif)
  632. {
  633. if (band == IEEE80211_BAND_5GHZ) {
  634. ctx->staging.flags &=
  635. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  636. | RXON_FLG_CCK_MSK);
  637. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  638. } else {
  639. /* Copied from iwl_post_associate() */
  640. if (vif && vif->bss_conf.use_short_slot)
  641. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  642. else
  643. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  644. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  645. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  646. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  647. }
  648. }
  649. static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
  650. struct iwl_rxon_context *ctx, int hw_decrypt)
  651. {
  652. struct iwl_rxon_cmd *rxon = &ctx->staging;
  653. if (hw_decrypt)
  654. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  655. else
  656. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  657. }
  658. /* validate RXON structure is valid */
  659. static int iwl_check_rxon_cmd(struct iwl_priv *priv,
  660. struct iwl_rxon_context *ctx)
  661. {
  662. struct iwl_rxon_cmd *rxon = &ctx->staging;
  663. u32 errors = 0;
  664. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  665. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  666. IWL_WARN(priv, "check 2.4G: wrong narrow\n");
  667. errors |= BIT(0);
  668. }
  669. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  670. IWL_WARN(priv, "check 2.4G: wrong radar\n");
  671. errors |= BIT(1);
  672. }
  673. } else {
  674. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  675. IWL_WARN(priv, "check 5.2G: not short slot!\n");
  676. errors |= BIT(2);
  677. }
  678. if (rxon->flags & RXON_FLG_CCK_MSK) {
  679. IWL_WARN(priv, "check 5.2G: CCK!\n");
  680. errors |= BIT(3);
  681. }
  682. }
  683. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  684. IWL_WARN(priv, "mac/bssid mcast!\n");
  685. errors |= BIT(4);
  686. }
  687. /* make sure basic rates 6Mbps and 1Mbps are supported */
  688. if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
  689. (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
  690. IWL_WARN(priv, "neither 1 nor 6 are basic\n");
  691. errors |= BIT(5);
  692. }
  693. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  694. IWL_WARN(priv, "aid > 2007\n");
  695. errors |= BIT(6);
  696. }
  697. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  698. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  699. IWL_WARN(priv, "CCK and short slot\n");
  700. errors |= BIT(7);
  701. }
  702. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  703. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  704. IWL_WARN(priv, "CCK and auto detect");
  705. errors |= BIT(8);
  706. }
  707. if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  708. RXON_FLG_TGG_PROTECT_MSK)) ==
  709. RXON_FLG_TGG_PROTECT_MSK) {
  710. IWL_WARN(priv, "TGg but no auto-detect\n");
  711. errors |= BIT(9);
  712. }
  713. if (rxon->channel == 0) {
  714. IWL_WARN(priv, "zero channel is invalid\n");
  715. errors |= BIT(10);
  716. }
  717. WARN(errors, "Invalid RXON (%#x), channel %d",
  718. errors, le16_to_cpu(rxon->channel));
  719. return errors ? -EINVAL : 0;
  720. }
  721. /**
  722. * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  723. * @priv: staging_rxon is compared to active_rxon
  724. *
  725. * If the RXON structure is changing enough to require a new tune,
  726. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  727. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  728. */
  729. static int iwl_full_rxon_required(struct iwl_priv *priv,
  730. struct iwl_rxon_context *ctx)
  731. {
  732. const struct iwl_rxon_cmd *staging = &ctx->staging;
  733. const struct iwl_rxon_cmd *active = &ctx->active;
  734. #define CHK(cond) \
  735. if ((cond)) { \
  736. IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
  737. return 1; \
  738. }
  739. #define CHK_NEQ(c1, c2) \
  740. if ((c1) != (c2)) { \
  741. IWL_DEBUG_INFO(priv, "need full RXON - " \
  742. #c1 " != " #c2 " - %d != %d\n", \
  743. (c1), (c2)); \
  744. return 1; \
  745. }
  746. /* These items are only settable from the full RXON command */
  747. CHK(!iwl_is_associated_ctx(ctx));
  748. CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
  749. CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
  750. CHK(!ether_addr_equal(staging->wlap_bssid_addr,
  751. active->wlap_bssid_addr));
  752. CHK_NEQ(staging->dev_type, active->dev_type);
  753. CHK_NEQ(staging->channel, active->channel);
  754. CHK_NEQ(staging->air_propagation, active->air_propagation);
  755. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  756. active->ofdm_ht_single_stream_basic_rates);
  757. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  758. active->ofdm_ht_dual_stream_basic_rates);
  759. CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
  760. active->ofdm_ht_triple_stream_basic_rates);
  761. CHK_NEQ(staging->assoc_id, active->assoc_id);
  762. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  763. * be updated with the RXON_ASSOC command -- however only some
  764. * flag transitions are allowed using RXON_ASSOC */
  765. /* Check if we are not switching bands */
  766. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  767. active->flags & RXON_FLG_BAND_24G_MSK);
  768. /* Check if we are switching association toggle */
  769. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  770. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  771. #undef CHK
  772. #undef CHK_NEQ
  773. return 0;
  774. }
  775. #ifdef CONFIG_IWLWIFI_DEBUG
  776. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  777. enum iwl_rxon_context_id ctxid)
  778. {
  779. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  780. struct iwl_rxon_cmd *rxon = &ctx->staging;
  781. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  782. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  783. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
  784. le16_to_cpu(rxon->channel));
  785. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n",
  786. le32_to_cpu(rxon->flags));
  787. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  788. le32_to_cpu(rxon->filter_flags));
  789. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  790. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  791. rxon->ofdm_basic_rates);
  792. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
  793. rxon->cck_basic_rates);
  794. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  795. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  796. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
  797. le16_to_cpu(rxon->assoc_id));
  798. }
  799. #endif
  800. static void iwl_calc_basic_rates(struct iwl_priv *priv,
  801. struct iwl_rxon_context *ctx)
  802. {
  803. int lowest_present_ofdm = 100;
  804. int lowest_present_cck = 100;
  805. u8 cck = 0;
  806. u8 ofdm = 0;
  807. if (ctx->vif) {
  808. struct ieee80211_supported_band *sband;
  809. unsigned long basic = ctx->vif->bss_conf.basic_rates;
  810. int i;
  811. sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
  812. for_each_set_bit(i, &basic, BITS_PER_LONG) {
  813. int hw = sband->bitrates[i].hw_value;
  814. if (hw >= IWL_FIRST_OFDM_RATE) {
  815. ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
  816. if (lowest_present_ofdm > hw)
  817. lowest_present_ofdm = hw;
  818. } else {
  819. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  820. cck |= BIT(hw);
  821. if (lowest_present_cck > hw)
  822. lowest_present_cck = hw;
  823. }
  824. }
  825. }
  826. /*
  827. * Now we've got the basic rates as bitmaps in the ofdm and cck
  828. * variables. This isn't sufficient though, as there might not
  829. * be all the right rates in the bitmap. E.g. if the only basic
  830. * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
  831. * and 6 Mbps because the 802.11-2007 standard says in 9.6:
  832. *
  833. * [...] a STA responding to a received frame shall transmit
  834. * its Control Response frame [...] at the highest rate in the
  835. * BSSBasicRateSet parameter that is less than or equal to the
  836. * rate of the immediately previous frame in the frame exchange
  837. * sequence ([...]) and that is of the same modulation class
  838. * ([...]) as the received frame. If no rate contained in the
  839. * BSSBasicRateSet parameter meets these conditions, then the
  840. * control frame sent in response to a received frame shall be
  841. * transmitted at the highest mandatory rate of the PHY that is
  842. * less than or equal to the rate of the received frame, and
  843. * that is of the same modulation class as the received frame.
  844. *
  845. * As a consequence, we need to add all mandatory rates that are
  846. * lower than all of the basic rates to these bitmaps.
  847. */
  848. if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
  849. ofdm |= IWL_RATE_24M_MASK >> IWL_FIRST_OFDM_RATE;
  850. if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
  851. ofdm |= IWL_RATE_12M_MASK >> IWL_FIRST_OFDM_RATE;
  852. /* 6M already there or needed so always add */
  853. ofdm |= IWL_RATE_6M_MASK >> IWL_FIRST_OFDM_RATE;
  854. /*
  855. * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
  856. * Note, however:
  857. * - if no CCK rates are basic, it must be ERP since there must
  858. * be some basic rates at all, so they're OFDM => ERP PHY
  859. * (or we're in 5 GHz, and the cck bitmap will never be used)
  860. * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
  861. * - if 5.5M is basic, 1M and 2M are mandatory
  862. * - if 2M is basic, 1M is mandatory
  863. * - if 1M is basic, that's the only valid ACK rate.
  864. * As a consequence, it's not as complicated as it sounds, just add
  865. * any lower rates to the ACK rate bitmap.
  866. */
  867. if (IWL_RATE_11M_INDEX < lowest_present_cck)
  868. cck |= IWL_RATE_11M_MASK >> IWL_FIRST_CCK_RATE;
  869. if (IWL_RATE_5M_INDEX < lowest_present_cck)
  870. cck |= IWL_RATE_5M_MASK >> IWL_FIRST_CCK_RATE;
  871. if (IWL_RATE_2M_INDEX < lowest_present_cck)
  872. cck |= IWL_RATE_2M_MASK >> IWL_FIRST_CCK_RATE;
  873. /* 1M already there or needed so always add */
  874. cck |= IWL_RATE_1M_MASK >> IWL_FIRST_CCK_RATE;
  875. IWL_DEBUG_RATE(priv, "Set basic rates cck:0x%.2x ofdm:0x%.2x\n",
  876. cck, ofdm);
  877. /* "basic_rates" is a misnomer here -- should be called ACK rates */
  878. ctx->staging.cck_basic_rates = cck;
  879. ctx->staging.ofdm_basic_rates = ofdm;
  880. }
  881. /**
  882. * iwlagn_commit_rxon - commit staging_rxon to hardware
  883. *
  884. * The RXON command in staging_rxon is committed to the hardware and
  885. * the active_rxon structure is updated with the new data. This
  886. * function correctly transitions out of the RXON_ASSOC_MSK state if
  887. * a HW tune is required based on the RXON structure changes.
  888. *
  889. * The connect/disconnect flow should be as the following:
  890. *
  891. * 1. make sure send RXON command with association bit unset if not connect
  892. * this should include the channel and the band for the candidate
  893. * to be connected to
  894. * 2. Add Station before RXON association with the AP
  895. * 3. RXON_timing has to send before RXON for connection
  896. * 4. full RXON command - associated bit set
  897. * 5. use RXON_ASSOC command to update any flags changes
  898. */
  899. int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  900. {
  901. /* cast away the const for active_rxon in this function */
  902. struct iwl_rxon_cmd *active = (void *)&ctx->active;
  903. bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
  904. int ret;
  905. lockdep_assert_held(&priv->mutex);
  906. if (!iwl_is_alive(priv))
  907. return -EBUSY;
  908. /* This function hardcodes a bunch of dual-mode assumptions */
  909. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  910. if (!ctx->is_active)
  911. return 0;
  912. /* always get timestamp with Rx frame */
  913. ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
  914. /* recalculate basic rates */
  915. iwl_calc_basic_rates(priv, ctx);
  916. /*
  917. * force CTS-to-self frames protection if RTS-CTS is not preferred
  918. * one aggregation protection method
  919. */
  920. if (!priv->hw_params.use_rts_for_aggregation)
  921. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  922. if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
  923. !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
  924. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  925. else
  926. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  927. iwl_print_rx_config_cmd(priv, ctx->ctxid);
  928. ret = iwl_check_rxon_cmd(priv, ctx);
  929. if (ret) {
  930. IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
  931. return -EINVAL;
  932. }
  933. /*
  934. * receive commit_rxon request
  935. * abort any previous channel switch if still in process
  936. */
  937. if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
  938. (priv->switch_channel != ctx->staging.channel)) {
  939. IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
  940. le16_to_cpu(priv->switch_channel));
  941. iwl_chswitch_done(priv, false);
  942. }
  943. /*
  944. * If we don't need to send a full RXON, we can use
  945. * iwl_rxon_assoc_cmd which is used to reconfigure filter
  946. * and other flags for the current radio configuration.
  947. */
  948. if (!iwl_full_rxon_required(priv, ctx)) {
  949. ret = iwlagn_send_rxon_assoc(priv, ctx);
  950. if (ret) {
  951. IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
  952. return ret;
  953. }
  954. memcpy(active, &ctx->staging, sizeof(*active));
  955. /*
  956. * We do not commit tx power settings while channel changing,
  957. * do it now if after settings changed.
  958. */
  959. iwl_set_tx_power(priv, priv->tx_power_next, false);
  960. /* make sure we are in the right PS state */
  961. iwl_power_update_mode(priv, true);
  962. return 0;
  963. }
  964. iwl_set_rxon_hwcrypto(priv, ctx, !iwlwifi_mod_params.sw_crypto);
  965. IWL_DEBUG_INFO(priv,
  966. "Going to commit RXON\n"
  967. " * with%s RXON_FILTER_ASSOC_MSK\n"
  968. " * channel = %d\n"
  969. " * bssid = %pM\n",
  970. (new_assoc ? "" : "out"),
  971. le16_to_cpu(ctx->staging.channel),
  972. ctx->staging.bssid_addr);
  973. /*
  974. * Always clear associated first, but with the correct config.
  975. * This is required as for example station addition for the
  976. * AP station must be done after the BSSID is set to correctly
  977. * set up filters in the device.
  978. */
  979. ret = iwlagn_rxon_disconn(priv, ctx);
  980. if (ret)
  981. return ret;
  982. ret = iwlagn_set_pan_params(priv);
  983. if (ret)
  984. return ret;
  985. if (new_assoc)
  986. return iwlagn_rxon_connect(priv, ctx);
  987. return 0;
  988. }
  989. void iwlagn_config_ht40(struct ieee80211_conf *conf,
  990. struct iwl_rxon_context *ctx)
  991. {
  992. if (conf_is_ht40_minus(conf)) {
  993. ctx->ht.extension_chan_offset =
  994. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  995. ctx->ht.is_40mhz = true;
  996. } else if (conf_is_ht40_plus(conf)) {
  997. ctx->ht.extension_chan_offset =
  998. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  999. ctx->ht.is_40mhz = true;
  1000. } else {
  1001. ctx->ht.extension_chan_offset =
  1002. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1003. ctx->ht.is_40mhz = false;
  1004. }
  1005. }
  1006. int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
  1007. {
  1008. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1009. struct iwl_rxon_context *ctx;
  1010. struct ieee80211_conf *conf = &hw->conf;
  1011. struct ieee80211_channel *channel = conf->chandef.chan;
  1012. int ret = 0;
  1013. IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
  1014. mutex_lock(&priv->mutex);
  1015. if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
  1016. IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
  1017. goto out;
  1018. }
  1019. if (!iwl_is_ready(priv)) {
  1020. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  1021. goto out;
  1022. }
  1023. if (changed & (IEEE80211_CONF_CHANGE_SMPS |
  1024. IEEE80211_CONF_CHANGE_CHANNEL)) {
  1025. /* mac80211 uses static for non-HT which is what we want */
  1026. priv->current_ht_config.smps = conf->smps_mode;
  1027. /*
  1028. * Recalculate chain counts.
  1029. *
  1030. * If monitor mode is enabled then mac80211 will
  1031. * set up the SM PS mode to OFF if an HT channel is
  1032. * configured.
  1033. */
  1034. for_each_context(priv, ctx)
  1035. iwlagn_set_rxon_chain(priv, ctx);
  1036. }
  1037. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1038. for_each_context(priv, ctx) {
  1039. /* Configure HT40 channels */
  1040. if (ctx->ht.enabled != conf_is_ht(conf))
  1041. ctx->ht.enabled = conf_is_ht(conf);
  1042. if (ctx->ht.enabled) {
  1043. /* if HT40 is used, it should not change
  1044. * after associated except channel switch */
  1045. if (!ctx->ht.is_40mhz ||
  1046. !iwl_is_associated_ctx(ctx))
  1047. iwlagn_config_ht40(conf, ctx);
  1048. } else
  1049. ctx->ht.is_40mhz = false;
  1050. /*
  1051. * Default to no protection. Protection mode will
  1052. * later be set from BSS config in iwl_ht_conf
  1053. */
  1054. ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  1055. /* if we are switching from ht to 2.4 clear flags
  1056. * from any ht related info since 2.4 does not
  1057. * support ht */
  1058. if (le16_to_cpu(ctx->staging.channel) !=
  1059. channel->hw_value)
  1060. ctx->staging.flags = 0;
  1061. iwl_set_rxon_channel(priv, channel, ctx);
  1062. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  1063. iwl_set_flags_for_band(priv, ctx, channel->band,
  1064. ctx->vif);
  1065. }
  1066. iwl_update_bcast_stations(priv);
  1067. }
  1068. if (changed & (IEEE80211_CONF_CHANGE_PS |
  1069. IEEE80211_CONF_CHANGE_IDLE)) {
  1070. ret = iwl_power_update_mode(priv, false);
  1071. if (ret)
  1072. IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
  1073. }
  1074. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1075. IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
  1076. priv->tx_power_user_lmt, conf->power_level);
  1077. iwl_set_tx_power(priv, conf->power_level, false);
  1078. }
  1079. for_each_context(priv, ctx) {
  1080. if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  1081. continue;
  1082. iwlagn_commit_rxon(priv, ctx);
  1083. }
  1084. out:
  1085. mutex_unlock(&priv->mutex);
  1086. IWL_DEBUG_MAC80211(priv, "leave\n");
  1087. return ret;
  1088. }
  1089. static void iwlagn_check_needed_chains(struct iwl_priv *priv,
  1090. struct iwl_rxon_context *ctx,
  1091. struct ieee80211_bss_conf *bss_conf)
  1092. {
  1093. struct ieee80211_vif *vif = ctx->vif;
  1094. struct iwl_rxon_context *tmp;
  1095. struct ieee80211_sta *sta;
  1096. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  1097. struct ieee80211_sta_ht_cap *ht_cap;
  1098. bool need_multiple;
  1099. lockdep_assert_held(&priv->mutex);
  1100. switch (vif->type) {
  1101. case NL80211_IFTYPE_STATION:
  1102. rcu_read_lock();
  1103. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  1104. if (!sta) {
  1105. /*
  1106. * If at all, this can only happen through a race
  1107. * when the AP disconnects us while we're still
  1108. * setting up the connection, in that case mac80211
  1109. * will soon tell us about that.
  1110. */
  1111. need_multiple = false;
  1112. rcu_read_unlock();
  1113. break;
  1114. }
  1115. ht_cap = &sta->ht_cap;
  1116. need_multiple = true;
  1117. /*
  1118. * If the peer advertises no support for receiving 2 and 3
  1119. * stream MCS rates, it can't be transmitting them either.
  1120. */
  1121. if (ht_cap->mcs.rx_mask[1] == 0 &&
  1122. ht_cap->mcs.rx_mask[2] == 0) {
  1123. need_multiple = false;
  1124. } else if (!(ht_cap->mcs.tx_params &
  1125. IEEE80211_HT_MCS_TX_DEFINED)) {
  1126. /* If it can't TX MCS at all ... */
  1127. need_multiple = false;
  1128. } else if (ht_cap->mcs.tx_params &
  1129. IEEE80211_HT_MCS_TX_RX_DIFF) {
  1130. int maxstreams;
  1131. /*
  1132. * But if it can receive them, it might still not
  1133. * be able to transmit them, which is what we need
  1134. * to check here -- so check the number of streams
  1135. * it advertises for TX (if different from RX).
  1136. */
  1137. maxstreams = (ht_cap->mcs.tx_params &
  1138. IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
  1139. maxstreams >>=
  1140. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1141. maxstreams += 1;
  1142. if (maxstreams <= 1)
  1143. need_multiple = false;
  1144. }
  1145. rcu_read_unlock();
  1146. break;
  1147. case NL80211_IFTYPE_ADHOC:
  1148. /* currently */
  1149. need_multiple = false;
  1150. break;
  1151. default:
  1152. /* only AP really */
  1153. need_multiple = true;
  1154. break;
  1155. }
  1156. ctx->ht_need_multiple_chains = need_multiple;
  1157. if (!need_multiple) {
  1158. /* check all contexts */
  1159. for_each_context(priv, tmp) {
  1160. if (!tmp->vif)
  1161. continue;
  1162. if (tmp->ht_need_multiple_chains) {
  1163. need_multiple = true;
  1164. break;
  1165. }
  1166. }
  1167. }
  1168. ht_conf->single_chain_sufficient = !need_multiple;
  1169. }
  1170. static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
  1171. {
  1172. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  1173. int ret;
  1174. if (!(priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED))
  1175. return;
  1176. if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
  1177. iwl_is_any_associated(priv)) {
  1178. struct iwl_calib_chain_noise_reset_cmd cmd;
  1179. /* clear data for chain noise calibration algorithm */
  1180. data->chain_noise_a = 0;
  1181. data->chain_noise_b = 0;
  1182. data->chain_noise_c = 0;
  1183. data->chain_signal_a = 0;
  1184. data->chain_signal_b = 0;
  1185. data->chain_signal_c = 0;
  1186. data->beacon_count = 0;
  1187. memset(&cmd, 0, sizeof(cmd));
  1188. iwl_set_calib_hdr(&cmd.hdr,
  1189. priv->phy_calib_chain_noise_reset_cmd);
  1190. ret = iwl_dvm_send_cmd_pdu(priv,
  1191. REPLY_PHY_CALIBRATION_CMD,
  1192. CMD_SYNC, sizeof(cmd), &cmd);
  1193. if (ret)
  1194. IWL_ERR(priv,
  1195. "Could not send REPLY_PHY_CALIBRATION_CMD\n");
  1196. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  1197. IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
  1198. }
  1199. }
  1200. void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
  1201. struct ieee80211_vif *vif,
  1202. struct ieee80211_bss_conf *bss_conf,
  1203. u32 changes)
  1204. {
  1205. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1206. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1207. int ret;
  1208. bool force = false;
  1209. mutex_lock(&priv->mutex);
  1210. if (changes & BSS_CHANGED_IDLE && bss_conf->idle) {
  1211. /*
  1212. * If we go idle, then clearly no "passive-no-rx"
  1213. * workaround is needed any more, this is a reset.
  1214. */
  1215. iwlagn_lift_passive_no_rx(priv);
  1216. }
  1217. if (unlikely(!iwl_is_ready(priv))) {
  1218. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  1219. mutex_unlock(&priv->mutex);
  1220. return;
  1221. }
  1222. if (unlikely(!ctx->vif)) {
  1223. IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
  1224. mutex_unlock(&priv->mutex);
  1225. return;
  1226. }
  1227. if (changes & BSS_CHANGED_BEACON_INT)
  1228. force = true;
  1229. if (changes & BSS_CHANGED_QOS) {
  1230. ctx->qos_data.qos_active = bss_conf->qos;
  1231. iwlagn_update_qos(priv, ctx);
  1232. }
  1233. ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
  1234. if (vif->bss_conf.use_short_preamble)
  1235. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  1236. else
  1237. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  1238. if (changes & BSS_CHANGED_ASSOC) {
  1239. if (bss_conf->assoc) {
  1240. priv->timestamp = bss_conf->sync_tsf;
  1241. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1242. } else {
  1243. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1244. if (ctx->ctxid == IWL_RXON_CTX_BSS)
  1245. priv->have_rekey_data = false;
  1246. }
  1247. iwlagn_bt_coex_rssi_monitor(priv);
  1248. }
  1249. if (ctx->ht.enabled) {
  1250. ctx->ht.protection = bss_conf->ht_operation_mode &
  1251. IEEE80211_HT_OP_MODE_PROTECTION;
  1252. ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
  1253. IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  1254. iwlagn_check_needed_chains(priv, ctx, bss_conf);
  1255. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  1256. }
  1257. iwlagn_set_rxon_chain(priv, ctx);
  1258. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  1259. ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  1260. else
  1261. ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  1262. if (bss_conf->use_cts_prot)
  1263. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  1264. else
  1265. ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  1266. memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
  1267. if (vif->type == NL80211_IFTYPE_AP ||
  1268. vif->type == NL80211_IFTYPE_ADHOC) {
  1269. if (vif->bss_conf.enable_beacon) {
  1270. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1271. priv->beacon_ctx = ctx;
  1272. } else {
  1273. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1274. priv->beacon_ctx = NULL;
  1275. }
  1276. }
  1277. /*
  1278. * If the ucode decides to do beacon filtering before
  1279. * association, it will lose beacons that are needed
  1280. * before sending frames out on passive channels. This
  1281. * causes association failures on those channels. Enable
  1282. * receiving beacons in such cases.
  1283. */
  1284. if (vif->type == NL80211_IFTYPE_STATION) {
  1285. if (!bss_conf->assoc)
  1286. ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
  1287. else
  1288. ctx->staging.filter_flags &=
  1289. ~RXON_FILTER_BCON_AWARE_MSK;
  1290. }
  1291. if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  1292. iwlagn_commit_rxon(priv, ctx);
  1293. if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
  1294. /*
  1295. * The chain noise calibration will enable PM upon
  1296. * completion. If calibration has already been run
  1297. * then we need to enable power management here.
  1298. */
  1299. if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
  1300. iwl_power_update_mode(priv, false);
  1301. /* Enable RX differential gain and sensitivity calibrations */
  1302. iwlagn_chain_noise_reset(priv);
  1303. priv->start_calib = 1;
  1304. }
  1305. if (changes & BSS_CHANGED_IBSS) {
  1306. ret = iwlagn_manage_ibss_station(priv, vif,
  1307. bss_conf->ibss_joined);
  1308. if (ret)
  1309. IWL_ERR(priv, "failed to %s IBSS station %pM\n",
  1310. bss_conf->ibss_joined ? "add" : "remove",
  1311. bss_conf->bssid);
  1312. }
  1313. if (changes & BSS_CHANGED_BEACON && priv->beacon_ctx == ctx) {
  1314. if (iwlagn_update_beacon(priv, vif))
  1315. IWL_ERR(priv, "Error updating beacon\n");
  1316. }
  1317. mutex_unlock(&priv->mutex);
  1318. }
  1319. void iwlagn_post_scan(struct iwl_priv *priv)
  1320. {
  1321. struct iwl_rxon_context *ctx;
  1322. /*
  1323. * We do not commit power settings while scan is pending,
  1324. * do it now if the settings changed.
  1325. */
  1326. iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
  1327. iwl_set_tx_power(priv, priv->tx_power_next, false);
  1328. /*
  1329. * Since setting the RXON may have been deferred while
  1330. * performing the scan, fire one off if needed
  1331. */
  1332. for_each_context(priv, ctx)
  1333. if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
  1334. iwlagn_commit_rxon(priv, ctx);
  1335. iwlagn_set_pan_params(priv);
  1336. }