rxon.c 45 KB

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