iwl-agn-rxon.c 45 KB

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