iwl-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. #ifdef CONFIG_IWLWIFI_DEBUG
  43. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  44. enum iwl_rxon_context_id ctxid)
  45. {
  46. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  47. struct iwl_rxon_cmd *rxon = &ctx->staging;
  48. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  49. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  50. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  51. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  52. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  53. le32_to_cpu(rxon->filter_flags));
  54. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  55. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  56. rxon->ofdm_basic_rates);
  57. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  58. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  59. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  60. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  61. }
  62. #endif
  63. void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  64. {
  65. unsigned int reload_msec;
  66. unsigned long reload_jiffies;
  67. #ifdef CONFIG_IWLWIFI_DEBUG
  68. if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
  69. iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
  70. #endif
  71. /* uCode is no longer loaded. */
  72. priv->ucode_loaded = false;
  73. /* Set the FW error flag -- cleared on iwl_down */
  74. set_bit(STATUS_FW_ERROR, &priv->status);
  75. /* Cancel currently queued command. */
  76. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  77. iwl_abort_notification_waits(&priv->notif_wait);
  78. /* Keep the restart process from trying to send host
  79. * commands by clearing the ready bit */
  80. clear_bit(STATUS_READY, &priv->status);
  81. wake_up(&trans(priv)->wait_command_queue);
  82. if (!ondemand) {
  83. /*
  84. * If firmware keep reloading, then it indicate something
  85. * serious wrong and firmware having problem to recover
  86. * from it. Instead of keep trying which will fill the syslog
  87. * and hang the system, let's just stop it
  88. */
  89. reload_jiffies = jiffies;
  90. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  91. (long) priv->reload_jiffies);
  92. priv->reload_jiffies = reload_jiffies;
  93. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  94. priv->reload_count++;
  95. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  96. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  97. return;
  98. }
  99. } else
  100. priv->reload_count = 0;
  101. }
  102. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  103. if (iwlagn_mod_params.restart_fw) {
  104. IWL_DEBUG_FW_ERRORS(priv,
  105. "Restarting adapter due to uCode error.\n");
  106. queue_work(priv->workqueue, &priv->restart);
  107. } else
  108. IWL_DEBUG_FW_ERRORS(priv,
  109. "Detected FW error, but not restarting\n");
  110. }
  111. }
  112. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  113. {
  114. int ret;
  115. s8 prev_tx_power;
  116. bool defer;
  117. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  118. lockdep_assert_held(&priv->mutex);
  119. if (priv->tx_power_user_lmt == tx_power && !force)
  120. return 0;
  121. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  122. IWL_WARN(priv,
  123. "Requested user TXPOWER %d below lower limit %d.\n",
  124. tx_power,
  125. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  126. return -EINVAL;
  127. }
  128. if (tx_power > priv->tx_power_device_lmt) {
  129. IWL_WARN(priv,
  130. "Requested user TXPOWER %d above upper limit %d.\n",
  131. tx_power, priv->tx_power_device_lmt);
  132. return -EINVAL;
  133. }
  134. if (!iwl_is_ready_rf(priv))
  135. return -EIO;
  136. /* scan complete and commit_rxon use tx_power_next value,
  137. * it always need to be updated for newest request */
  138. priv->tx_power_next = tx_power;
  139. /* do not set tx power when scanning or channel changing */
  140. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  141. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  142. if (defer && !force) {
  143. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  144. return 0;
  145. }
  146. prev_tx_power = priv->tx_power_user_lmt;
  147. priv->tx_power_user_lmt = tx_power;
  148. ret = iwlagn_send_tx_power(priv);
  149. /* if fail to set tx_power, restore the orig. tx power */
  150. if (ret) {
  151. priv->tx_power_user_lmt = prev_tx_power;
  152. priv->tx_power_next = prev_tx_power;
  153. }
  154. return ret;
  155. }
  156. void iwl_send_bt_config(struct iwl_priv *priv)
  157. {
  158. struct iwl_bt_cmd bt_cmd = {
  159. .lead_time = BT_LEAD_TIME_DEF,
  160. .max_kill = BT_MAX_KILL_DEF,
  161. .kill_ack_mask = 0,
  162. .kill_cts_mask = 0,
  163. };
  164. if (!iwlagn_mod_params.bt_coex_active)
  165. bt_cmd.flags = BT_COEX_DISABLE;
  166. else
  167. bt_cmd.flags = BT_COEX_ENABLE;
  168. priv->bt_enable_flag = bt_cmd.flags;
  169. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  170. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  171. if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  172. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  173. IWL_ERR(priv, "failed to send BT Coex Config\n");
  174. }
  175. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  176. {
  177. struct iwl_statistics_cmd statistics_cmd = {
  178. .configuration_flags =
  179. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  180. };
  181. if (flags & CMD_ASYNC)
  182. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  183. CMD_ASYNC,
  184. sizeof(struct iwl_statistics_cmd),
  185. &statistics_cmd);
  186. else
  187. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  188. CMD_SYNC,
  189. sizeof(struct iwl_statistics_cmd),
  190. &statistics_cmd);
  191. }
  192. #ifdef CONFIG_IWLWIFI_DEBUGFS
  193. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  194. void iwl_reset_traffic_log(struct iwl_priv *priv)
  195. {
  196. priv->tx_traffic_idx = 0;
  197. priv->rx_traffic_idx = 0;
  198. if (priv->tx_traffic)
  199. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  200. if (priv->rx_traffic)
  201. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  202. }
  203. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  204. {
  205. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  206. if (iwl_have_debug_level(IWL_DL_TX)) {
  207. if (!priv->tx_traffic) {
  208. priv->tx_traffic =
  209. kzalloc(traffic_size, GFP_KERNEL);
  210. if (!priv->tx_traffic)
  211. return -ENOMEM;
  212. }
  213. }
  214. if (iwl_have_debug_level(IWL_DL_RX)) {
  215. if (!priv->rx_traffic) {
  216. priv->rx_traffic =
  217. kzalloc(traffic_size, GFP_KERNEL);
  218. if (!priv->rx_traffic)
  219. return -ENOMEM;
  220. }
  221. }
  222. iwl_reset_traffic_log(priv);
  223. return 0;
  224. }
  225. void iwl_free_traffic_mem(struct iwl_priv *priv)
  226. {
  227. kfree(priv->tx_traffic);
  228. priv->tx_traffic = NULL;
  229. kfree(priv->rx_traffic);
  230. priv->rx_traffic = NULL;
  231. }
  232. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  233. u16 length, struct ieee80211_hdr *header)
  234. {
  235. __le16 fc;
  236. u16 len;
  237. if (likely(!iwl_have_debug_level(IWL_DL_TX)))
  238. return;
  239. if (!priv->tx_traffic)
  240. return;
  241. fc = header->frame_control;
  242. if (ieee80211_is_data(fc)) {
  243. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  244. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  245. memcpy((priv->tx_traffic +
  246. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  247. header, len);
  248. priv->tx_traffic_idx =
  249. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  250. }
  251. }
  252. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  253. u16 length, struct ieee80211_hdr *header)
  254. {
  255. __le16 fc;
  256. u16 len;
  257. if (likely(!iwl_have_debug_level(IWL_DL_RX)))
  258. return;
  259. if (!priv->rx_traffic)
  260. return;
  261. fc = header->frame_control;
  262. if (ieee80211_is_data(fc)) {
  263. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  264. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  265. memcpy((priv->rx_traffic +
  266. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  267. header, len);
  268. priv->rx_traffic_idx =
  269. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  270. }
  271. }
  272. const char *get_mgmt_string(int cmd)
  273. {
  274. switch (cmd) {
  275. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  276. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  277. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  278. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  279. IWL_CMD(MANAGEMENT_PROBE_REQ);
  280. IWL_CMD(MANAGEMENT_PROBE_RESP);
  281. IWL_CMD(MANAGEMENT_BEACON);
  282. IWL_CMD(MANAGEMENT_ATIM);
  283. IWL_CMD(MANAGEMENT_DISASSOC);
  284. IWL_CMD(MANAGEMENT_AUTH);
  285. IWL_CMD(MANAGEMENT_DEAUTH);
  286. IWL_CMD(MANAGEMENT_ACTION);
  287. default:
  288. return "UNKNOWN";
  289. }
  290. }
  291. const char *get_ctrl_string(int cmd)
  292. {
  293. switch (cmd) {
  294. IWL_CMD(CONTROL_BACK_REQ);
  295. IWL_CMD(CONTROL_BACK);
  296. IWL_CMD(CONTROL_PSPOLL);
  297. IWL_CMD(CONTROL_RTS);
  298. IWL_CMD(CONTROL_CTS);
  299. IWL_CMD(CONTROL_ACK);
  300. IWL_CMD(CONTROL_CFEND);
  301. IWL_CMD(CONTROL_CFENDACK);
  302. default:
  303. return "UNKNOWN";
  304. }
  305. }
  306. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  307. {
  308. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  309. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  310. }
  311. /*
  312. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  313. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  314. * Use debugFs to display the rx/rx_statistics
  315. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  316. * information will be recorded, but DATA pkt still will be recorded
  317. * for the reason of iwl_led.c need to control the led blinking based on
  318. * number of tx and rx data.
  319. *
  320. */
  321. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  322. {
  323. struct traffic_stats *stats;
  324. if (is_tx)
  325. stats = &priv->tx_stats;
  326. else
  327. stats = &priv->rx_stats;
  328. if (ieee80211_is_mgmt(fc)) {
  329. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  330. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  331. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  332. break;
  333. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  334. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  335. break;
  336. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  337. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  338. break;
  339. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  340. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  341. break;
  342. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  343. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  344. break;
  345. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  346. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  347. break;
  348. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  349. stats->mgmt[MANAGEMENT_BEACON]++;
  350. break;
  351. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  352. stats->mgmt[MANAGEMENT_ATIM]++;
  353. break;
  354. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  355. stats->mgmt[MANAGEMENT_DISASSOC]++;
  356. break;
  357. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  358. stats->mgmt[MANAGEMENT_AUTH]++;
  359. break;
  360. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  361. stats->mgmt[MANAGEMENT_DEAUTH]++;
  362. break;
  363. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  364. stats->mgmt[MANAGEMENT_ACTION]++;
  365. break;
  366. }
  367. } else if (ieee80211_is_ctl(fc)) {
  368. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  369. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  370. stats->ctrl[CONTROL_BACK_REQ]++;
  371. break;
  372. case cpu_to_le16(IEEE80211_STYPE_BACK):
  373. stats->ctrl[CONTROL_BACK]++;
  374. break;
  375. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  376. stats->ctrl[CONTROL_PSPOLL]++;
  377. break;
  378. case cpu_to_le16(IEEE80211_STYPE_RTS):
  379. stats->ctrl[CONTROL_RTS]++;
  380. break;
  381. case cpu_to_le16(IEEE80211_STYPE_CTS):
  382. stats->ctrl[CONTROL_CTS]++;
  383. break;
  384. case cpu_to_le16(IEEE80211_STYPE_ACK):
  385. stats->ctrl[CONTROL_ACK]++;
  386. break;
  387. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  388. stats->ctrl[CONTROL_CFEND]++;
  389. break;
  390. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  391. stats->ctrl[CONTROL_CFENDACK]++;
  392. break;
  393. }
  394. } else {
  395. /* data */
  396. stats->data_cnt++;
  397. stats->data_bytes += len;
  398. }
  399. }
  400. #endif
  401. int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
  402. {
  403. struct iwl_rf_reset *rf_reset;
  404. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  405. return -EAGAIN;
  406. if (!iwl_is_any_associated(priv)) {
  407. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  408. return -ENOLINK;
  409. }
  410. rf_reset = &priv->rf_reset;
  411. rf_reset->reset_request_count++;
  412. if (!external && rf_reset->last_reset_jiffies &&
  413. time_after(rf_reset->last_reset_jiffies +
  414. IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
  415. IWL_DEBUG_INFO(priv, "RF reset rejected\n");
  416. rf_reset->reset_reject_count++;
  417. return -EAGAIN;
  418. }
  419. rf_reset->reset_success_count++;
  420. rf_reset->last_reset_jiffies = jiffies;
  421. /*
  422. * There is no easy and better way to force reset the radio,
  423. * the only known method is switching channel which will force to
  424. * reset and tune the radio.
  425. * Use internal short scan (single channel) operation to should
  426. * achieve this objective.
  427. * Driver should reset the radio when number of consecutive missed
  428. * beacon, or any other uCode error condition detected.
  429. */
  430. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  431. iwl_internal_short_hw_scan(priv);
  432. return 0;
  433. }
  434. int iwl_cmd_echo_test(struct iwl_priv *priv)
  435. {
  436. int ret;
  437. struct iwl_host_cmd cmd = {
  438. .id = REPLY_ECHO,
  439. .len = { 0 },
  440. .flags = CMD_SYNC,
  441. };
  442. ret = iwl_dvm_send_cmd(priv, &cmd);
  443. if (ret)
  444. IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
  445. else
  446. IWL_DEBUG_INFO(priv, "echo testing pass\n");
  447. return ret;
  448. }