iwl-agn-rxon.c 45 KB

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