rxon.c 45 KB

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