utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <net/mac80211.h>
  64. #include "iwl-debug.h"
  65. #include "iwl-io.h"
  66. #include "mvm.h"
  67. #include "fw-api-rs.h"
  68. /*
  69. * Will return 0 even if the cmd failed when RFKILL is asserted unless
  70. * CMD_WANT_SKB is set in cmd->flags.
  71. */
  72. int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
  73. {
  74. int ret;
  75. /*
  76. * Synchronous commands from this op-mode must hold
  77. * the mutex, this ensures we don't try to send two
  78. * (or more) synchronous commands at a time.
  79. */
  80. if (!(cmd->flags & CMD_ASYNC))
  81. lockdep_assert_held(&mvm->mutex);
  82. ret = iwl_trans_send_cmd(mvm->trans, cmd);
  83. /*
  84. * If the caller wants the SKB, then don't hide any problems, the
  85. * caller might access the response buffer which will be NULL if
  86. * the command failed.
  87. */
  88. if (cmd->flags & CMD_WANT_SKB)
  89. return ret;
  90. /* Silently ignore failures if RFKILL is asserted */
  91. if (!ret || ret == -ERFKILL)
  92. return 0;
  93. return ret;
  94. }
  95. int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u8 id,
  96. u32 flags, u16 len, const void *data)
  97. {
  98. struct iwl_host_cmd cmd = {
  99. .id = id,
  100. .len = { len, },
  101. .data = { data, },
  102. .flags = flags,
  103. };
  104. return iwl_mvm_send_cmd(mvm, &cmd);
  105. }
  106. /*
  107. * We assume that the caller set the status to the sucess value
  108. */
  109. int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
  110. u32 *status)
  111. {
  112. struct iwl_rx_packet *pkt;
  113. struct iwl_cmd_response *resp;
  114. int ret, resp_len;
  115. lockdep_assert_held(&mvm->mutex);
  116. /*
  117. * Only synchronous commands can wait for status,
  118. * we use WANT_SKB so the caller can't.
  119. */
  120. if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
  121. "cmd flags %x", cmd->flags))
  122. return -EINVAL;
  123. cmd->flags |= CMD_SYNC | CMD_WANT_SKB;
  124. ret = iwl_trans_send_cmd(mvm->trans, cmd);
  125. if (ret == -ERFKILL) {
  126. /*
  127. * The command failed because of RFKILL, don't update
  128. * the status, leave it as success and return 0.
  129. */
  130. return 0;
  131. } else if (ret) {
  132. return ret;
  133. }
  134. pkt = cmd->resp_pkt;
  135. /* Can happen if RFKILL is asserted */
  136. if (!pkt) {
  137. ret = 0;
  138. goto out_free_resp;
  139. }
  140. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  141. ret = -EIO;
  142. goto out_free_resp;
  143. }
  144. resp_len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  145. if (WARN_ON_ONCE(resp_len != sizeof(pkt->hdr) + sizeof(*resp))) {
  146. ret = -EIO;
  147. goto out_free_resp;
  148. }
  149. resp = (void *)pkt->data;
  150. *status = le32_to_cpu(resp->status);
  151. out_free_resp:
  152. iwl_free_resp(cmd);
  153. return ret;
  154. }
  155. /*
  156. * We assume that the caller set the status to the sucess value
  157. */
  158. int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u8 id, u16 len,
  159. const void *data, u32 *status)
  160. {
  161. struct iwl_host_cmd cmd = {
  162. .id = id,
  163. .len = { len, },
  164. .data = { data, },
  165. };
  166. return iwl_mvm_send_cmd_status(mvm, &cmd, status);
  167. }
  168. #define IWL_DECLARE_RATE_INFO(r) \
  169. [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
  170. /*
  171. * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
  172. */
  173. static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
  174. IWL_DECLARE_RATE_INFO(1),
  175. IWL_DECLARE_RATE_INFO(2),
  176. IWL_DECLARE_RATE_INFO(5),
  177. IWL_DECLARE_RATE_INFO(11),
  178. IWL_DECLARE_RATE_INFO(6),
  179. IWL_DECLARE_RATE_INFO(9),
  180. IWL_DECLARE_RATE_INFO(12),
  181. IWL_DECLARE_RATE_INFO(18),
  182. IWL_DECLARE_RATE_INFO(24),
  183. IWL_DECLARE_RATE_INFO(36),
  184. IWL_DECLARE_RATE_INFO(48),
  185. IWL_DECLARE_RATE_INFO(54),
  186. };
  187. int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
  188. enum ieee80211_band band)
  189. {
  190. int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
  191. int idx;
  192. int band_offset = 0;
  193. /* Legacy rate format, search for match in table */
  194. if (band == IEEE80211_BAND_5GHZ)
  195. band_offset = IWL_FIRST_OFDM_RATE;
  196. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  197. if (fw_rate_idx_to_plcp[idx] == rate)
  198. return idx - band_offset;
  199. return -1;
  200. }
  201. u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
  202. {
  203. /* Get PLCP rate for tx_cmd->rate_n_flags */
  204. return fw_rate_idx_to_plcp[rate_idx];
  205. }
  206. int iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  207. struct iwl_device_cmd *cmd)
  208. {
  209. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  210. struct iwl_error_resp *err_resp = (void *)pkt->data;
  211. IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
  212. le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
  213. IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
  214. le16_to_cpu(err_resp->bad_cmd_seq_num),
  215. le32_to_cpu(err_resp->error_service));
  216. IWL_ERR(mvm, "FW Error notification: timestamp 0x%16llX\n",
  217. le64_to_cpu(err_resp->timestamp));
  218. return 0;
  219. }
  220. /*
  221. * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
  222. * The parameter should also be a combination of ANT_[ABC].
  223. */
  224. u8 first_antenna(u8 mask)
  225. {
  226. BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
  227. WARN_ON_ONCE(!mask); /* ffs will return 0 if mask is zeroed */
  228. return (u8)(BIT(ffs(mask)));
  229. }
  230. /*
  231. * Toggles between TX antennas to send the probe request on.
  232. * Receives the bitmask of valid TX antennas and the *index* used
  233. * for the last TX, and returns the next valid *index* to use.
  234. * In order to set it in the tx_cmd, must do BIT(idx).
  235. */
  236. u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
  237. {
  238. u8 ind = last_idx;
  239. int i;
  240. for (i = 0; i < RATE_MCS_ANT_NUM; i++) {
  241. ind = (ind + 1) % RATE_MCS_ANT_NUM;
  242. if (valid & BIT(ind))
  243. return ind;
  244. }
  245. WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
  246. return last_idx;
  247. }
  248. static struct {
  249. char *name;
  250. u8 num;
  251. } advanced_lookup[] = {
  252. { "NMI_INTERRUPT_WDG", 0x34 },
  253. { "SYSASSERT", 0x35 },
  254. { "UCODE_VERSION_MISMATCH", 0x37 },
  255. { "BAD_COMMAND", 0x38 },
  256. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  257. { "FATAL_ERROR", 0x3D },
  258. { "NMI_TRM_HW_ERR", 0x46 },
  259. { "NMI_INTERRUPT_TRM", 0x4C },
  260. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  261. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  262. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  263. { "NMI_INTERRUPT_HOST", 0x66 },
  264. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  265. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  266. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  267. { "ADVANCED_SYSASSERT", 0 },
  268. };
  269. static const char *desc_lookup(u32 num)
  270. {
  271. int i;
  272. for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
  273. if (advanced_lookup[i].num == num)
  274. return advanced_lookup[i].name;
  275. /* No entry matches 'num', so it is the last: ADVANCED_SYSASSERT */
  276. return advanced_lookup[i].name;
  277. }
  278. /*
  279. * Note: This structure is read from the device with IO accesses,
  280. * and the reading already does the endian conversion. As it is
  281. * read with u32-sized accesses, any members with a different size
  282. * need to be ordered correctly though!
  283. */
  284. struct iwl_error_event_table {
  285. u32 valid; /* (nonzero) valid, (0) log is empty */
  286. u32 error_id; /* type of error */
  287. u32 pc; /* program counter */
  288. u32 blink1; /* branch link */
  289. u32 blink2; /* branch link */
  290. u32 ilink1; /* interrupt link */
  291. u32 ilink2; /* interrupt link */
  292. u32 data1; /* error-specific data */
  293. u32 data2; /* error-specific data */
  294. u32 data3; /* error-specific data */
  295. u32 bcon_time; /* beacon timer */
  296. u32 tsf_low; /* network timestamp function timer */
  297. u32 tsf_hi; /* network timestamp function timer */
  298. u32 gp1; /* GP1 timer register */
  299. u32 gp2; /* GP2 timer register */
  300. u32 gp3; /* GP3 timer register */
  301. u32 ucode_ver; /* uCode version */
  302. u32 hw_ver; /* HW Silicon version */
  303. u32 brd_ver; /* HW board version */
  304. u32 log_pc; /* log program counter */
  305. u32 frame_ptr; /* frame pointer */
  306. u32 stack_ptr; /* stack pointer */
  307. u32 hcmd; /* last host command header */
  308. u32 isr0; /* isr status register LMPM_NIC_ISR0:
  309. * rxtx_flag */
  310. u32 isr1; /* isr status register LMPM_NIC_ISR1:
  311. * host_flag */
  312. u32 isr2; /* isr status register LMPM_NIC_ISR2:
  313. * enc_flag */
  314. u32 isr3; /* isr status register LMPM_NIC_ISR3:
  315. * time_flag */
  316. u32 isr4; /* isr status register LMPM_NIC_ISR4:
  317. * wico interrupt */
  318. u32 isr_pref; /* isr status register LMPM_NIC_PREF_STAT */
  319. u32 wait_event; /* wait event() caller address */
  320. u32 l2p_control; /* L2pControlField */
  321. u32 l2p_duration; /* L2pDurationField */
  322. u32 l2p_mhvalid; /* L2pMhValidBits */
  323. u32 l2p_addr_match; /* L2pAddrMatchStat */
  324. u32 lmpm_pmg_sel; /* indicate which clocks are turned on
  325. * (LMPM_PMG_SEL) */
  326. u32 u_timestamp; /* indicate when the date and time of the
  327. * compilation */
  328. u32 flow_handler; /* FH read/write pointers, RX credit */
  329. } __packed;
  330. #define ERROR_START_OFFSET (1 * sizeof(u32))
  331. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  332. void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
  333. {
  334. struct iwl_trans *trans = mvm->trans;
  335. struct iwl_error_event_table table;
  336. u32 base;
  337. base = mvm->error_event_table;
  338. if (mvm->cur_ucode == IWL_UCODE_INIT) {
  339. if (!base)
  340. base = mvm->fw->init_errlog_ptr;
  341. } else {
  342. if (!base)
  343. base = mvm->fw->inst_errlog_ptr;
  344. }
  345. if (base < 0x800000 || base >= 0x80C000) {
  346. IWL_ERR(mvm,
  347. "Not valid error log pointer 0x%08X for %s uCode\n",
  348. base,
  349. (mvm->cur_ucode == IWL_UCODE_INIT)
  350. ? "Init" : "RT");
  351. return;
  352. }
  353. iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
  354. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  355. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  356. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  357. mvm->status, table.valid);
  358. }
  359. trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
  360. table.data1, table.data2, table.data3,
  361. table.blink1, table.blink2, table.ilink1,
  362. table.ilink2, table.bcon_time, table.gp1,
  363. table.gp2, table.gp3, table.ucode_ver,
  364. table.hw_ver, table.brd_ver);
  365. IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
  366. desc_lookup(table.error_id));
  367. IWL_ERR(mvm, "0x%08X | uPc\n", table.pc);
  368. IWL_ERR(mvm, "0x%08X | branchlink1\n", table.blink1);
  369. IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
  370. IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
  371. IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
  372. IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
  373. IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
  374. IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
  375. IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
  376. IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
  377. IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
  378. IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
  379. IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
  380. IWL_ERR(mvm, "0x%08X | time gp3\n", table.gp3);
  381. IWL_ERR(mvm, "0x%08X | uCode version\n", table.ucode_ver);
  382. IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
  383. IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
  384. IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
  385. IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
  386. IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
  387. IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
  388. IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
  389. IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
  390. IWL_ERR(mvm, "0x%08X | isr_pref\n", table.isr_pref);
  391. IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
  392. IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
  393. IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
  394. IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
  395. IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
  396. IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
  397. IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
  398. IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
  399. }
  400. /**
  401. * iwl_mvm_send_lq_cmd() - Send link quality command
  402. * @init: This command is sent as part of station initialization right
  403. * after station has been added.
  404. *
  405. * The link quality command is sent as the last step of station creation.
  406. * This is the special case in which init is set and we call a callback in
  407. * this case to clear the state indicating that station creation is in
  408. * progress.
  409. */
  410. int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq,
  411. u8 flags, bool init)
  412. {
  413. struct iwl_host_cmd cmd = {
  414. .id = LQ_CMD,
  415. .len = { sizeof(struct iwl_lq_cmd), },
  416. .flags = flags,
  417. .data = { lq, },
  418. };
  419. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  420. return -EINVAL;
  421. if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
  422. return -EINVAL;
  423. return iwl_mvm_send_cmd(mvm, &cmd);
  424. }