iwl-core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <net/mac80211.h>
  33. #include "iwl-eeprom.h"
  34. #include "iwl-debug.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-power.h"
  38. #include "iwl-shared.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-trans.h"
  41. const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  42. static bool iwl_is_channel_extension(struct iwl_priv *priv,
  43. enum ieee80211_band band,
  44. u16 channel, u8 extension_chan_offset)
  45. {
  46. const struct iwl_channel_info *ch_info;
  47. ch_info = iwl_get_channel_info(priv, band, channel);
  48. if (!is_channel_valid(ch_info))
  49. return false;
  50. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  51. return !(ch_info->ht40_extension_channel &
  52. IEEE80211_CHAN_NO_HT40PLUS);
  53. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  54. return !(ch_info->ht40_extension_channel &
  55. IEEE80211_CHAN_NO_HT40MINUS);
  56. return false;
  57. }
  58. bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
  59. struct iwl_rxon_context *ctx,
  60. struct ieee80211_sta_ht_cap *ht_cap)
  61. {
  62. if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
  63. return false;
  64. /*
  65. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  66. * the bit will not set if it is pure 40MHz case
  67. */
  68. if (ht_cap && !ht_cap->ht_supported)
  69. return false;
  70. #ifdef CONFIG_IWLWIFI_DEBUGFS
  71. if (priv->disable_ht40)
  72. return false;
  73. #endif
  74. return iwl_is_channel_extension(priv, priv->band,
  75. le16_to_cpu(ctx->staging.channel),
  76. ctx->ht.extension_chan_offset);
  77. }
  78. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  79. struct iwl_ht_config *ht_conf,
  80. struct iwl_rxon_context *ctx)
  81. {
  82. struct iwl_rxon_cmd *rxon = &ctx->staging;
  83. if (!ctx->ht.enabled) {
  84. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  85. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  86. RXON_FLG_HT40_PROT_MSK |
  87. RXON_FLG_HT_PROT_MSK);
  88. return;
  89. }
  90. /* FIXME: if the definition of ht.protection changed, the "translation"
  91. * will be needed for rxon->flags
  92. */
  93. rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  94. /* Set up channel bandwidth:
  95. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  96. /* clear the HT channel mode before set the mode */
  97. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  98. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  99. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  100. /* pure ht40 */
  101. if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  102. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  103. /* Note: control channel is opposite of extension channel */
  104. switch (ctx->ht.extension_chan_offset) {
  105. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  106. rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  107. break;
  108. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  109. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  110. break;
  111. }
  112. } else {
  113. /* Note: control channel is opposite of extension channel */
  114. switch (ctx->ht.extension_chan_offset) {
  115. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  116. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  117. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  118. break;
  119. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  120. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  121. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  122. break;
  123. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  124. default:
  125. /* channel location only valid if in Mixed mode */
  126. IWL_ERR(priv, "invalid extension channel offset\n");
  127. break;
  128. }
  129. }
  130. } else {
  131. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  132. }
  133. iwlagn_set_rxon_chain(priv, ctx);
  134. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  135. "extension channel offset 0x%x\n",
  136. le32_to_cpu(rxon->flags), ctx->ht.protection,
  137. ctx->ht.extension_chan_offset);
  138. }
  139. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  140. {
  141. struct iwl_rxon_context *ctx;
  142. for_each_context(priv, ctx)
  143. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  144. }
  145. /* Return valid, unused, channel for a passive scan to reset the RF */
  146. u8 iwl_get_single_channel_number(struct iwl_priv *priv,
  147. enum ieee80211_band band)
  148. {
  149. const struct iwl_channel_info *ch_info;
  150. int i;
  151. u8 channel = 0;
  152. u8 min, max;
  153. struct iwl_rxon_context *ctx;
  154. if (band == IEEE80211_BAND_5GHZ) {
  155. min = 14;
  156. max = priv->channel_count;
  157. } else {
  158. min = 0;
  159. max = 14;
  160. }
  161. for (i = min; i < max; i++) {
  162. bool busy = false;
  163. for_each_context(priv, ctx) {
  164. busy = priv->channel_info[i].channel ==
  165. le16_to_cpu(ctx->staging.channel);
  166. if (busy)
  167. break;
  168. }
  169. if (busy)
  170. continue;
  171. channel = priv->channel_info[i].channel;
  172. ch_info = iwl_get_channel_info(priv, band, channel);
  173. if (is_channel_valid(ch_info))
  174. break;
  175. }
  176. return channel;
  177. }
  178. /**
  179. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  180. * @ch: requested channel as a pointer to struct ieee80211_channel
  181. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  182. * in the staging RXON flag structure based on the ch->band
  183. */
  184. void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  185. struct iwl_rxon_context *ctx)
  186. {
  187. enum ieee80211_band band = ch->band;
  188. u16 channel = ch->hw_value;
  189. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  190. (priv->band == band))
  191. return;
  192. ctx->staging.channel = cpu_to_le16(channel);
  193. if (band == IEEE80211_BAND_5GHZ)
  194. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  195. else
  196. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  197. priv->band = band;
  198. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  199. }
  200. void iwl_set_flags_for_band(struct iwl_priv *priv,
  201. struct iwl_rxon_context *ctx,
  202. enum ieee80211_band band,
  203. struct ieee80211_vif *vif)
  204. {
  205. if (band == IEEE80211_BAND_5GHZ) {
  206. ctx->staging.flags &=
  207. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  208. | RXON_FLG_CCK_MSK);
  209. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  210. } else {
  211. /* Copied from iwl_post_associate() */
  212. if (vif && vif->bss_conf.use_short_slot)
  213. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  214. else
  215. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  216. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  217. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  218. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  219. }
  220. }
  221. /*
  222. * initialize rxon structure with default values from eeprom
  223. */
  224. void iwl_connection_init_rx_config(struct iwl_priv *priv,
  225. struct iwl_rxon_context *ctx)
  226. {
  227. const struct iwl_channel_info *ch_info;
  228. memset(&ctx->staging, 0, sizeof(ctx->staging));
  229. if (!ctx->vif) {
  230. ctx->staging.dev_type = ctx->unused_devtype;
  231. } else switch (ctx->vif->type) {
  232. case NL80211_IFTYPE_AP:
  233. ctx->staging.dev_type = ctx->ap_devtype;
  234. break;
  235. case NL80211_IFTYPE_STATION:
  236. ctx->staging.dev_type = ctx->station_devtype;
  237. ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  238. break;
  239. case NL80211_IFTYPE_ADHOC:
  240. ctx->staging.dev_type = ctx->ibss_devtype;
  241. ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  242. ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  243. RXON_FILTER_ACCEPT_GRP_MSK;
  244. break;
  245. default:
  246. IWL_ERR(priv, "Unsupported interface type %d\n",
  247. ctx->vif->type);
  248. break;
  249. }
  250. #if 0
  251. /* TODO: Figure out when short_preamble would be set and cache from
  252. * that */
  253. if (!hw_to_local(priv->hw)->short_preamble)
  254. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  255. else
  256. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  257. #endif
  258. ch_info = iwl_get_channel_info(priv, priv->band,
  259. le16_to_cpu(ctx->active.channel));
  260. if (!ch_info)
  261. ch_info = &priv->channel_info[0];
  262. ctx->staging.channel = cpu_to_le16(ch_info->channel);
  263. priv->band = ch_info->band;
  264. iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  265. ctx->staging.ofdm_basic_rates =
  266. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  267. ctx->staging.cck_basic_rates =
  268. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  269. /* clear both MIX and PURE40 mode flag */
  270. ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  271. RXON_FLG_CHANNEL_MODE_PURE_40);
  272. if (ctx->vif)
  273. memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  274. ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  275. ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  276. ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  277. }
  278. void iwl_set_rate(struct iwl_priv *priv)
  279. {
  280. const struct ieee80211_supported_band *hw = NULL;
  281. struct ieee80211_rate *rate;
  282. struct iwl_rxon_context *ctx;
  283. int i;
  284. hw = iwl_get_hw_mode(priv, priv->band);
  285. if (!hw) {
  286. IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
  287. return;
  288. }
  289. priv->active_rate = 0;
  290. for (i = 0; i < hw->n_bitrates; i++) {
  291. rate = &(hw->bitrates[i]);
  292. if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
  293. priv->active_rate |= (1 << rate->hw_value);
  294. }
  295. IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
  296. for_each_context(priv, ctx) {
  297. ctx->staging.cck_basic_rates =
  298. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  299. ctx->staging.ofdm_basic_rates =
  300. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  301. }
  302. }
  303. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  304. {
  305. /*
  306. * MULTI-FIXME
  307. * See iwlagn_mac_channel_switch.
  308. */
  309. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  310. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  311. return;
  312. if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  313. ieee80211_chswitch_done(ctx->vif, is_success);
  314. }
  315. #ifdef CONFIG_IWLWIFI_DEBUG
  316. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  317. enum iwl_rxon_context_id ctxid)
  318. {
  319. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  320. struct iwl_rxon_cmd *rxon = &ctx->staging;
  321. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  322. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  323. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  324. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  325. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  326. le32_to_cpu(rxon->filter_flags));
  327. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  328. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  329. rxon->ofdm_basic_rates);
  330. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  331. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  332. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  333. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  334. }
  335. #endif
  336. void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  337. {
  338. unsigned int reload_msec;
  339. unsigned long reload_jiffies;
  340. #ifdef CONFIG_IWLWIFI_DEBUG
  341. if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
  342. iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
  343. #endif
  344. /* uCode is no longer loaded. */
  345. priv->ucode_loaded = false;
  346. /* Set the FW error flag -- cleared on iwl_down */
  347. set_bit(STATUS_FW_ERROR, &priv->shrd->status);
  348. /* Cancel currently queued command. */
  349. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  350. iwl_abort_notification_waits(&priv->notif_wait);
  351. /* Keep the restart process from trying to send host
  352. * commands by clearing the ready bit */
  353. clear_bit(STATUS_READY, &priv->status);
  354. wake_up(&trans(priv)->wait_command_queue);
  355. if (!ondemand) {
  356. /*
  357. * If firmware keep reloading, then it indicate something
  358. * serious wrong and firmware having problem to recover
  359. * from it. Instead of keep trying which will fill the syslog
  360. * and hang the system, let's just stop it
  361. */
  362. reload_jiffies = jiffies;
  363. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  364. (long) priv->reload_jiffies);
  365. priv->reload_jiffies = reload_jiffies;
  366. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  367. priv->reload_count++;
  368. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  369. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  370. return;
  371. }
  372. } else
  373. priv->reload_count = 0;
  374. }
  375. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  376. if (iwlagn_mod_params.restart_fw) {
  377. IWL_DEBUG_FW_ERRORS(priv,
  378. "Restarting adapter due to uCode error.\n");
  379. queue_work(priv->workqueue, &priv->restart);
  380. } else
  381. IWL_DEBUG_FW_ERRORS(priv,
  382. "Detected FW error, but not restarting\n");
  383. }
  384. }
  385. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  386. {
  387. int ret;
  388. s8 prev_tx_power;
  389. bool defer;
  390. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  391. lockdep_assert_held(&priv->mutex);
  392. if (priv->tx_power_user_lmt == tx_power && !force)
  393. return 0;
  394. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  395. IWL_WARN(priv,
  396. "Requested user TXPOWER %d below lower limit %d.\n",
  397. tx_power,
  398. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  399. return -EINVAL;
  400. }
  401. if (tx_power > priv->tx_power_device_lmt) {
  402. IWL_WARN(priv,
  403. "Requested user TXPOWER %d above upper limit %d.\n",
  404. tx_power, priv->tx_power_device_lmt);
  405. return -EINVAL;
  406. }
  407. if (!iwl_is_ready_rf(priv))
  408. return -EIO;
  409. /* scan complete and commit_rxon use tx_power_next value,
  410. * it always need to be updated for newest request */
  411. priv->tx_power_next = tx_power;
  412. /* do not set tx power when scanning or channel changing */
  413. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  414. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  415. if (defer && !force) {
  416. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  417. return 0;
  418. }
  419. prev_tx_power = priv->tx_power_user_lmt;
  420. priv->tx_power_user_lmt = tx_power;
  421. ret = iwlagn_send_tx_power(priv);
  422. /* if fail to set tx_power, restore the orig. tx power */
  423. if (ret) {
  424. priv->tx_power_user_lmt = prev_tx_power;
  425. priv->tx_power_next = prev_tx_power;
  426. }
  427. return ret;
  428. }
  429. void iwl_send_bt_config(struct iwl_priv *priv)
  430. {
  431. struct iwl_bt_cmd bt_cmd = {
  432. .lead_time = BT_LEAD_TIME_DEF,
  433. .max_kill = BT_MAX_KILL_DEF,
  434. .kill_ack_mask = 0,
  435. .kill_cts_mask = 0,
  436. };
  437. if (!iwlagn_mod_params.bt_coex_active)
  438. bt_cmd.flags = BT_COEX_DISABLE;
  439. else
  440. bt_cmd.flags = BT_COEX_ENABLE;
  441. priv->bt_enable_flag = bt_cmd.flags;
  442. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  443. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  444. if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  445. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  446. IWL_ERR(priv, "failed to send BT Coex Config\n");
  447. }
  448. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  449. {
  450. struct iwl_statistics_cmd statistics_cmd = {
  451. .configuration_flags =
  452. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  453. };
  454. if (flags & CMD_ASYNC)
  455. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  456. CMD_ASYNC,
  457. sizeof(struct iwl_statistics_cmd),
  458. &statistics_cmd);
  459. else
  460. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  461. CMD_SYNC,
  462. sizeof(struct iwl_statistics_cmd),
  463. &statistics_cmd);
  464. }
  465. #ifdef CONFIG_IWLWIFI_DEBUGFS
  466. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  467. void iwl_reset_traffic_log(struct iwl_priv *priv)
  468. {
  469. priv->tx_traffic_idx = 0;
  470. priv->rx_traffic_idx = 0;
  471. if (priv->tx_traffic)
  472. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  473. if (priv->rx_traffic)
  474. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  475. }
  476. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  477. {
  478. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  479. if (iwl_have_debug_level(IWL_DL_TX)) {
  480. if (!priv->tx_traffic) {
  481. priv->tx_traffic =
  482. kzalloc(traffic_size, GFP_KERNEL);
  483. if (!priv->tx_traffic)
  484. return -ENOMEM;
  485. }
  486. }
  487. if (iwl_have_debug_level(IWL_DL_RX)) {
  488. if (!priv->rx_traffic) {
  489. priv->rx_traffic =
  490. kzalloc(traffic_size, GFP_KERNEL);
  491. if (!priv->rx_traffic)
  492. return -ENOMEM;
  493. }
  494. }
  495. iwl_reset_traffic_log(priv);
  496. return 0;
  497. }
  498. void iwl_free_traffic_mem(struct iwl_priv *priv)
  499. {
  500. kfree(priv->tx_traffic);
  501. priv->tx_traffic = NULL;
  502. kfree(priv->rx_traffic);
  503. priv->rx_traffic = NULL;
  504. }
  505. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  506. u16 length, struct ieee80211_hdr *header)
  507. {
  508. __le16 fc;
  509. u16 len;
  510. if (likely(!iwl_have_debug_level(IWL_DL_TX)))
  511. return;
  512. if (!priv->tx_traffic)
  513. return;
  514. fc = header->frame_control;
  515. if (ieee80211_is_data(fc)) {
  516. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  517. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  518. memcpy((priv->tx_traffic +
  519. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  520. header, len);
  521. priv->tx_traffic_idx =
  522. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  523. }
  524. }
  525. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  526. u16 length, struct ieee80211_hdr *header)
  527. {
  528. __le16 fc;
  529. u16 len;
  530. if (likely(!iwl_have_debug_level(IWL_DL_RX)))
  531. return;
  532. if (!priv->rx_traffic)
  533. return;
  534. fc = header->frame_control;
  535. if (ieee80211_is_data(fc)) {
  536. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  537. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  538. memcpy((priv->rx_traffic +
  539. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  540. header, len);
  541. priv->rx_traffic_idx =
  542. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  543. }
  544. }
  545. const char *get_mgmt_string(int cmd)
  546. {
  547. switch (cmd) {
  548. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  549. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  550. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  551. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  552. IWL_CMD(MANAGEMENT_PROBE_REQ);
  553. IWL_CMD(MANAGEMENT_PROBE_RESP);
  554. IWL_CMD(MANAGEMENT_BEACON);
  555. IWL_CMD(MANAGEMENT_ATIM);
  556. IWL_CMD(MANAGEMENT_DISASSOC);
  557. IWL_CMD(MANAGEMENT_AUTH);
  558. IWL_CMD(MANAGEMENT_DEAUTH);
  559. IWL_CMD(MANAGEMENT_ACTION);
  560. default:
  561. return "UNKNOWN";
  562. }
  563. }
  564. const char *get_ctrl_string(int cmd)
  565. {
  566. switch (cmd) {
  567. IWL_CMD(CONTROL_BACK_REQ);
  568. IWL_CMD(CONTROL_BACK);
  569. IWL_CMD(CONTROL_PSPOLL);
  570. IWL_CMD(CONTROL_RTS);
  571. IWL_CMD(CONTROL_CTS);
  572. IWL_CMD(CONTROL_ACK);
  573. IWL_CMD(CONTROL_CFEND);
  574. IWL_CMD(CONTROL_CFENDACK);
  575. default:
  576. return "UNKNOWN";
  577. }
  578. }
  579. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  580. {
  581. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  582. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  583. }
  584. /*
  585. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  586. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  587. * Use debugFs to display the rx/rx_statistics
  588. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  589. * information will be recorded, but DATA pkt still will be recorded
  590. * for the reason of iwl_led.c need to control the led blinking based on
  591. * number of tx and rx data.
  592. *
  593. */
  594. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  595. {
  596. struct traffic_stats *stats;
  597. if (is_tx)
  598. stats = &priv->tx_stats;
  599. else
  600. stats = &priv->rx_stats;
  601. if (ieee80211_is_mgmt(fc)) {
  602. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  603. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  604. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  605. break;
  606. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  607. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  608. break;
  609. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  610. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  611. break;
  612. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  613. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  614. break;
  615. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  616. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  617. break;
  618. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  619. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  620. break;
  621. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  622. stats->mgmt[MANAGEMENT_BEACON]++;
  623. break;
  624. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  625. stats->mgmt[MANAGEMENT_ATIM]++;
  626. break;
  627. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  628. stats->mgmt[MANAGEMENT_DISASSOC]++;
  629. break;
  630. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  631. stats->mgmt[MANAGEMENT_AUTH]++;
  632. break;
  633. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  634. stats->mgmt[MANAGEMENT_DEAUTH]++;
  635. break;
  636. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  637. stats->mgmt[MANAGEMENT_ACTION]++;
  638. break;
  639. }
  640. } else if (ieee80211_is_ctl(fc)) {
  641. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  642. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  643. stats->ctrl[CONTROL_BACK_REQ]++;
  644. break;
  645. case cpu_to_le16(IEEE80211_STYPE_BACK):
  646. stats->ctrl[CONTROL_BACK]++;
  647. break;
  648. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  649. stats->ctrl[CONTROL_PSPOLL]++;
  650. break;
  651. case cpu_to_le16(IEEE80211_STYPE_RTS):
  652. stats->ctrl[CONTROL_RTS]++;
  653. break;
  654. case cpu_to_le16(IEEE80211_STYPE_CTS):
  655. stats->ctrl[CONTROL_CTS]++;
  656. break;
  657. case cpu_to_le16(IEEE80211_STYPE_ACK):
  658. stats->ctrl[CONTROL_ACK]++;
  659. break;
  660. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  661. stats->ctrl[CONTROL_CFEND]++;
  662. break;
  663. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  664. stats->ctrl[CONTROL_CFENDACK]++;
  665. break;
  666. }
  667. } else {
  668. /* data */
  669. stats->data_cnt++;
  670. stats->data_bytes += len;
  671. }
  672. }
  673. #endif
  674. static void iwl_force_rf_reset(struct iwl_priv *priv)
  675. {
  676. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  677. return;
  678. if (!iwl_is_any_associated(priv)) {
  679. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  680. return;
  681. }
  682. /*
  683. * There is no easy and better way to force reset the radio,
  684. * the only known method is switching channel which will force to
  685. * reset and tune the radio.
  686. * Use internal short scan (single channel) operation to should
  687. * achieve this objective.
  688. * Driver should reset the radio when number of consecutive missed
  689. * beacon, or any other uCode error condition detected.
  690. */
  691. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  692. iwl_internal_short_hw_scan(priv);
  693. }
  694. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  695. {
  696. struct iwl_force_reset *force_reset;
  697. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  698. return -EINVAL;
  699. if (mode >= IWL_MAX_FORCE_RESET) {
  700. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  701. return -EINVAL;
  702. }
  703. force_reset = &priv->force_reset[mode];
  704. force_reset->reset_request_count++;
  705. if (!external) {
  706. if (force_reset->last_force_reset_jiffies &&
  707. time_after(force_reset->last_force_reset_jiffies +
  708. force_reset->reset_duration, jiffies)) {
  709. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  710. force_reset->reset_reject_count++;
  711. return -EAGAIN;
  712. }
  713. }
  714. force_reset->reset_success_count++;
  715. force_reset->last_force_reset_jiffies = jiffies;
  716. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  717. switch (mode) {
  718. case IWL_RF_RESET:
  719. iwl_force_rf_reset(priv);
  720. break;
  721. case IWL_FW_RESET:
  722. /*
  723. * if the request is from external(ex: debugfs),
  724. * then always perform the request in regardless the module
  725. * parameter setting
  726. * if the request is from internal (uCode error or driver
  727. * detect failure), then fw_restart module parameter
  728. * need to be check before performing firmware reload
  729. */
  730. if (!external && !iwlagn_mod_params.restart_fw) {
  731. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  732. "module parameter setting\n");
  733. break;
  734. }
  735. IWL_ERR(priv, "On demand firmware reload\n");
  736. iwlagn_fw_error(priv, true);
  737. break;
  738. }
  739. return 0;
  740. }
  741. int iwl_cmd_echo_test(struct iwl_priv *priv)
  742. {
  743. int ret;
  744. struct iwl_host_cmd cmd = {
  745. .id = REPLY_ECHO,
  746. .len = { 0 },
  747. .flags = CMD_SYNC,
  748. };
  749. ret = iwl_dvm_send_cmd(priv, &cmd);
  750. if (ret)
  751. IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
  752. else
  753. IWL_DEBUG_INFO(priv, "echo testing pass\n");
  754. return ret;
  755. }
  756. static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
  757. {
  758. if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
  759. int ret;
  760. ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  761. return (ret == -EAGAIN) ? 0 : 1;
  762. }
  763. return 0;
  764. }
  765. /*
  766. * Making watchdog tick be a quarter of timeout assure we will
  767. * discover the queue hung between timeout and 1.25*timeout
  768. */
  769. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  770. /*
  771. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  772. * we reset the firmware. If everything is fine just rearm the timer.
  773. */
  774. void iwl_bg_watchdog(unsigned long data)
  775. {
  776. struct iwl_priv *priv = (struct iwl_priv *)data;
  777. int cnt;
  778. unsigned long timeout;
  779. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  780. return;
  781. if (iwl_is_rfkill(priv))
  782. return;
  783. timeout = hw_params(priv).wd_timeout;
  784. if (timeout == 0)
  785. return;
  786. /* monitor and check for stuck queues */
  787. for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
  788. if (iwl_check_stuck_queue(priv, cnt))
  789. return;
  790. mod_timer(&priv->watchdog, jiffies +
  791. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  792. }
  793. void iwl_setup_watchdog(struct iwl_priv *priv)
  794. {
  795. unsigned int timeout = hw_params(priv).wd_timeout;
  796. if (!iwlagn_mod_params.wd_disable) {
  797. /* use system default */
  798. if (timeout && !cfg(priv)->base_params->wd_disable)
  799. mod_timer(&priv->watchdog,
  800. jiffies +
  801. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  802. else
  803. del_timer(&priv->watchdog);
  804. } else {
  805. /* module parameter overwrite default configuration */
  806. if (timeout && iwlagn_mod_params.wd_disable == 2)
  807. mod_timer(&priv->watchdog,
  808. jiffies +
  809. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  810. else
  811. del_timer(&priv->watchdog);
  812. }
  813. }
  814. /**
  815. * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
  816. * @priv -- pointer to iwl_priv data structure
  817. * @tsf_bits -- number of bits need to shift for masking)
  818. */
  819. static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
  820. u16 tsf_bits)
  821. {
  822. return (1 << tsf_bits) - 1;
  823. }
  824. /**
  825. * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
  826. * @priv -- pointer to iwl_priv data structure
  827. * @tsf_bits -- number of bits need to shift for masking)
  828. */
  829. static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
  830. u16 tsf_bits)
  831. {
  832. return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
  833. }
  834. /*
  835. * extended beacon time format
  836. * time in usec will be changed into a 32-bit value in extended:internal format
  837. * the extended part is the beacon counts
  838. * the internal part is the time in usec within one beacon interval
  839. */
  840. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  841. {
  842. u32 quot;
  843. u32 rem;
  844. u32 interval = beacon_interval * TIME_UNIT;
  845. if (!interval || !usec)
  846. return 0;
  847. quot = (usec / interval) &
  848. (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
  849. IWLAGN_EXT_BEACON_TIME_POS);
  850. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  851. IWLAGN_EXT_BEACON_TIME_POS);
  852. return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
  853. }
  854. /* base is usually what we get from ucode with each received frame,
  855. * the same as HW timer counter counting down
  856. */
  857. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  858. u32 addon, u32 beacon_interval)
  859. {
  860. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  861. IWLAGN_EXT_BEACON_TIME_POS);
  862. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  863. IWLAGN_EXT_BEACON_TIME_POS);
  864. u32 interval = beacon_interval * TIME_UNIT;
  865. u32 res = (base & iwl_beacon_time_mask_high(priv,
  866. IWLAGN_EXT_BEACON_TIME_POS)) +
  867. (addon & iwl_beacon_time_mask_high(priv,
  868. IWLAGN_EXT_BEACON_TIME_POS));
  869. if (base_low > addon_low)
  870. res += base_low - addon_low;
  871. else if (base_low < addon_low) {
  872. res += interval + base_low - addon_low;
  873. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  874. } else
  875. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  876. return cpu_to_le32(res);
  877. }
  878. void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
  879. {
  880. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  881. if (state)
  882. set_bit(STATUS_RF_KILL_HW, &priv->status);
  883. else
  884. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  885. wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
  886. }
  887. void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  888. {
  889. struct ieee80211_tx_info *info;
  890. info = IEEE80211_SKB_CB(skb);
  891. kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
  892. dev_kfree_skb_any(skb);
  893. }