iwl-agn-rxon.c 45 KB

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