iwl-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2003-2014, 2018-2021 Intel Corporation
  4. * Copyright (C) 2015-2016 Intel Deutschland GmbH
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/export.h>
  9. #include "iwl-drv.h"
  10. #include "iwl-io.h"
  11. #include "iwl-csr.h"
  12. #include "iwl-debug.h"
  13. #include "iwl-prph.h"
  14. #include "iwl-fh.h"
  15. void iwl_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  16. {
  17. trace_iwlwifi_dev_iowrite8(trans->dev, ofs, val);
  18. iwl_trans_write8(trans, ofs, val);
  19. }
  20. IWL_EXPORT_SYMBOL(iwl_write8);
  21. void iwl_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  22. {
  23. trace_iwlwifi_dev_iowrite32(trans->dev, ofs, val);
  24. iwl_trans_write32(trans, ofs, val);
  25. }
  26. IWL_EXPORT_SYMBOL(iwl_write32);
  27. void iwl_write64(struct iwl_trans *trans, u64 ofs, u64 val)
  28. {
  29. trace_iwlwifi_dev_iowrite64(trans->dev, ofs, val);
  30. iwl_trans_write32(trans, ofs, lower_32_bits(val));
  31. iwl_trans_write32(trans, ofs + 4, upper_32_bits(val));
  32. }
  33. IWL_EXPORT_SYMBOL(iwl_write64);
  34. u32 iwl_read32(struct iwl_trans *trans, u32 ofs)
  35. {
  36. u32 val = iwl_trans_read32(trans, ofs);
  37. trace_iwlwifi_dev_ioread32(trans->dev, ofs, val);
  38. return val;
  39. }
  40. IWL_EXPORT_SYMBOL(iwl_read32);
  41. #define IWL_POLL_INTERVAL 10 /* microseconds */
  42. int iwl_poll_bit(struct iwl_trans *trans, u32 addr,
  43. u32 bits, u32 mask, int timeout)
  44. {
  45. int t = 0;
  46. do {
  47. if ((iwl_read32(trans, addr) & mask) == (bits & mask))
  48. return t;
  49. udelay(IWL_POLL_INTERVAL);
  50. t += IWL_POLL_INTERVAL;
  51. } while (t < timeout);
  52. return -ETIMEDOUT;
  53. }
  54. IWL_EXPORT_SYMBOL(iwl_poll_bit);
  55. u32 iwl_read_direct32(struct iwl_trans *trans, u32 reg)
  56. {
  57. if (iwl_trans_grab_nic_access(trans)) {
  58. u32 value = iwl_read32(trans, reg);
  59. iwl_trans_release_nic_access(trans);
  60. return value;
  61. }
  62. return 0x5a5a5a5a;
  63. }
  64. IWL_EXPORT_SYMBOL(iwl_read_direct32);
  65. void iwl_write_direct32(struct iwl_trans *trans, u32 reg, u32 value)
  66. {
  67. if (iwl_trans_grab_nic_access(trans)) {
  68. iwl_write32(trans, reg, value);
  69. iwl_trans_release_nic_access(trans);
  70. }
  71. }
  72. IWL_EXPORT_SYMBOL(iwl_write_direct32);
  73. void iwl_write_direct64(struct iwl_trans *trans, u64 reg, u64 value)
  74. {
  75. if (iwl_trans_grab_nic_access(trans)) {
  76. iwl_write64(trans, reg, value);
  77. iwl_trans_release_nic_access(trans);
  78. }
  79. }
  80. IWL_EXPORT_SYMBOL(iwl_write_direct64);
  81. int iwl_poll_direct_bit(struct iwl_trans *trans, u32 addr, u32 mask,
  82. int timeout)
  83. {
  84. int t = 0;
  85. do {
  86. if ((iwl_read_direct32(trans, addr) & mask) == mask)
  87. return t;
  88. udelay(IWL_POLL_INTERVAL);
  89. t += IWL_POLL_INTERVAL;
  90. } while (t < timeout);
  91. return -ETIMEDOUT;
  92. }
  93. IWL_EXPORT_SYMBOL(iwl_poll_direct_bit);
  94. u32 iwl_read_prph_no_grab(struct iwl_trans *trans, u32 ofs)
  95. {
  96. u32 val = iwl_trans_read_prph(trans, ofs);
  97. trace_iwlwifi_dev_ioread_prph32(trans->dev, ofs, val);
  98. return val;
  99. }
  100. IWL_EXPORT_SYMBOL(iwl_read_prph_no_grab);
  101. void iwl_write_prph_no_grab(struct iwl_trans *trans, u32 ofs, u32 val)
  102. {
  103. trace_iwlwifi_dev_iowrite_prph32(trans->dev, ofs, val);
  104. iwl_trans_write_prph(trans, ofs, val);
  105. }
  106. IWL_EXPORT_SYMBOL(iwl_write_prph_no_grab);
  107. void iwl_write_prph64_no_grab(struct iwl_trans *trans, u64 ofs, u64 val)
  108. {
  109. trace_iwlwifi_dev_iowrite_prph64(trans->dev, ofs, val);
  110. iwl_write_prph_no_grab(trans, ofs, val & 0xffffffff);
  111. iwl_write_prph_no_grab(trans, ofs + 4, val >> 32);
  112. }
  113. IWL_EXPORT_SYMBOL(iwl_write_prph64_no_grab);
  114. u32 iwl_read_prph(struct iwl_trans *trans, u32 ofs)
  115. {
  116. if (iwl_trans_grab_nic_access(trans)) {
  117. u32 val = iwl_read_prph_no_grab(trans, ofs);
  118. iwl_trans_release_nic_access(trans);
  119. return val;
  120. }
  121. return 0x5a5a5a5a;
  122. }
  123. IWL_EXPORT_SYMBOL(iwl_read_prph);
  124. void iwl_write_prph_delay(struct iwl_trans *trans, u32 ofs, u32 val, u32 delay_ms)
  125. {
  126. if (iwl_trans_grab_nic_access(trans)) {
  127. mdelay(delay_ms);
  128. iwl_write_prph_no_grab(trans, ofs, val);
  129. iwl_trans_release_nic_access(trans);
  130. }
  131. }
  132. IWL_EXPORT_SYMBOL(iwl_write_prph_delay);
  133. int iwl_poll_prph_bit(struct iwl_trans *trans, u32 addr,
  134. u32 bits, u32 mask, int timeout)
  135. {
  136. int t = 0;
  137. do {
  138. if ((iwl_read_prph(trans, addr) & mask) == (bits & mask))
  139. return t;
  140. udelay(IWL_POLL_INTERVAL);
  141. t += IWL_POLL_INTERVAL;
  142. } while (t < timeout);
  143. return -ETIMEDOUT;
  144. }
  145. void iwl_set_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  146. {
  147. if (iwl_trans_grab_nic_access(trans)) {
  148. iwl_write_prph_no_grab(trans, ofs,
  149. iwl_read_prph_no_grab(trans, ofs) |
  150. mask);
  151. iwl_trans_release_nic_access(trans);
  152. }
  153. }
  154. IWL_EXPORT_SYMBOL(iwl_set_bits_prph);
  155. void iwl_set_bits_mask_prph(struct iwl_trans *trans, u32 ofs,
  156. u32 bits, u32 mask)
  157. {
  158. if (iwl_trans_grab_nic_access(trans)) {
  159. iwl_write_prph_no_grab(trans, ofs,
  160. (iwl_read_prph_no_grab(trans, ofs) &
  161. mask) | bits);
  162. iwl_trans_release_nic_access(trans);
  163. }
  164. }
  165. IWL_EXPORT_SYMBOL(iwl_set_bits_mask_prph);
  166. void iwl_clear_bits_prph(struct iwl_trans *trans, u32 ofs, u32 mask)
  167. {
  168. u32 val;
  169. if (iwl_trans_grab_nic_access(trans)) {
  170. val = iwl_read_prph_no_grab(trans, ofs);
  171. iwl_write_prph_no_grab(trans, ofs, (val & ~mask));
  172. iwl_trans_release_nic_access(trans);
  173. }
  174. }
  175. IWL_EXPORT_SYMBOL(iwl_clear_bits_prph);
  176. void iwl_force_nmi(struct iwl_trans *trans)
  177. {
  178. if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
  179. iwl_write_prph_delay(trans, DEVICE_SET_NMI_REG,
  180. DEVICE_SET_NMI_VAL_DRV, 1);
  181. else if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
  182. iwl_write_umac_prph(trans, UREG_NIC_SET_NMI_DRIVER,
  183. UREG_NIC_SET_NMI_DRIVER_NMI_FROM_DRIVER);
  184. else if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_BZ)
  185. iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
  186. UREG_DOORBELL_TO_ISR6_NMI_BIT);
  187. else
  188. iwl_write32(trans, CSR_DOORBELL_VECTOR,
  189. UREG_DOORBELL_TO_ISR6_NMI_BIT);
  190. }
  191. IWL_EXPORT_SYMBOL(iwl_force_nmi);
  192. static const char *get_rfh_string(int cmd)
  193. {
  194. #define IWL_CMD(x) case x: return #x
  195. #define IWL_CMD_MQ(arg, reg, q) { if (arg == reg(q)) return #reg; }
  196. int i;
  197. for (i = 0; i < IWL_MAX_RX_HW_QUEUES; i++) {
  198. IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_BA_LSB, i);
  199. IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_WIDX, i);
  200. IWL_CMD_MQ(cmd, RFH_Q_FRBDCB_RIDX, i);
  201. IWL_CMD_MQ(cmd, RFH_Q_URBD_STTS_WPTR_LSB, i);
  202. }
  203. switch (cmd) {
  204. IWL_CMD(RFH_RXF_DMA_CFG);
  205. IWL_CMD(RFH_GEN_CFG);
  206. IWL_CMD(RFH_GEN_STATUS);
  207. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  208. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  209. default:
  210. return "UNKNOWN";
  211. }
  212. #undef IWL_CMD_MQ
  213. }
  214. struct reg {
  215. u32 addr;
  216. bool is64;
  217. };
  218. static int iwl_dump_rfh(struct iwl_trans *trans, char **buf)
  219. {
  220. int i, q;
  221. int num_q = trans->num_rx_queues;
  222. static const u32 rfh_tbl[] = {
  223. RFH_RXF_DMA_CFG,
  224. RFH_GEN_CFG,
  225. RFH_GEN_STATUS,
  226. FH_TSSR_TX_STATUS_REG,
  227. FH_TSSR_TX_ERROR_REG,
  228. };
  229. static const struct reg rfh_mq_tbl[] = {
  230. { RFH_Q0_FRBDCB_BA_LSB, true },
  231. { RFH_Q0_FRBDCB_WIDX, false },
  232. { RFH_Q0_FRBDCB_RIDX, false },
  233. { RFH_Q0_URBD_STTS_WPTR_LSB, true },
  234. };
  235. #ifdef CONFIG_IWLWIFI_DEBUGFS
  236. if (buf) {
  237. int pos = 0;
  238. /*
  239. * Register (up to 34 for name + 8 blank/q for MQ): 40 chars
  240. * Colon + space: 2 characters
  241. * 0X%08x: 10 characters
  242. * New line: 1 character
  243. * Total of 53 characters
  244. */
  245. size_t bufsz = ARRAY_SIZE(rfh_tbl) * 53 +
  246. ARRAY_SIZE(rfh_mq_tbl) * 53 * num_q + 40;
  247. *buf = kmalloc(bufsz, GFP_KERNEL);
  248. if (!*buf)
  249. return -ENOMEM;
  250. pos += scnprintf(*buf + pos, bufsz - pos,
  251. "RFH register values:\n");
  252. for (i = 0; i < ARRAY_SIZE(rfh_tbl); i++)
  253. pos += scnprintf(*buf + pos, bufsz - pos,
  254. "%40s: 0X%08x\n",
  255. get_rfh_string(rfh_tbl[i]),
  256. iwl_read_prph(trans, rfh_tbl[i]));
  257. for (i = 0; i < ARRAY_SIZE(rfh_mq_tbl); i++)
  258. for (q = 0; q < num_q; q++) {
  259. u32 addr = rfh_mq_tbl[i].addr;
  260. addr += q * (rfh_mq_tbl[i].is64 ? 8 : 4);
  261. pos += scnprintf(*buf + pos, bufsz - pos,
  262. "%34s(q %2d): 0X%08x\n",
  263. get_rfh_string(addr), q,
  264. iwl_read_prph(trans, addr));
  265. }
  266. return pos;
  267. }
  268. #endif
  269. IWL_ERR(trans, "RFH register values:\n");
  270. for (i = 0; i < ARRAY_SIZE(rfh_tbl); i++)
  271. IWL_ERR(trans, " %34s: 0X%08x\n",
  272. get_rfh_string(rfh_tbl[i]),
  273. iwl_read_prph(trans, rfh_tbl[i]));
  274. for (i = 0; i < ARRAY_SIZE(rfh_mq_tbl); i++)
  275. for (q = 0; q < num_q; q++) {
  276. u32 addr = rfh_mq_tbl[i].addr;
  277. addr += q * (rfh_mq_tbl[i].is64 ? 8 : 4);
  278. IWL_ERR(trans, " %34s(q %d): 0X%08x\n",
  279. get_rfh_string(addr), q,
  280. iwl_read_prph(trans, addr));
  281. }
  282. return 0;
  283. }
  284. static const char *get_fh_string(int cmd)
  285. {
  286. switch (cmd) {
  287. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  288. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  289. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  290. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  291. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  292. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  293. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  294. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  295. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  296. default:
  297. return "UNKNOWN";
  298. }
  299. #undef IWL_CMD
  300. }
  301. int iwl_dump_fh(struct iwl_trans *trans, char **buf)
  302. {
  303. int i;
  304. static const u32 fh_tbl[] = {
  305. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  306. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  307. FH_RSCSR_CHNL0_WPTR,
  308. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  309. FH_MEM_RSSR_SHARED_CTRL_REG,
  310. FH_MEM_RSSR_RX_STATUS_REG,
  311. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  312. FH_TSSR_TX_STATUS_REG,
  313. FH_TSSR_TX_ERROR_REG
  314. };
  315. if (trans->trans_cfg->mq_rx_supported)
  316. return iwl_dump_rfh(trans, buf);
  317. #ifdef CONFIG_IWLWIFI_DEBUGFS
  318. if (buf) {
  319. int pos = 0;
  320. size_t bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  321. *buf = kmalloc(bufsz, GFP_KERNEL);
  322. if (!*buf)
  323. return -ENOMEM;
  324. pos += scnprintf(*buf + pos, bufsz - pos,
  325. "FH register values:\n");
  326. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  327. pos += scnprintf(*buf + pos, bufsz - pos,
  328. " %34s: 0X%08x\n",
  329. get_fh_string(fh_tbl[i]),
  330. iwl_read_direct32(trans, fh_tbl[i]));
  331. return pos;
  332. }
  333. #endif
  334. IWL_ERR(trans, "FH register values:\n");
  335. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++)
  336. IWL_ERR(trans, " %34s: 0X%08x\n",
  337. get_fh_string(fh_tbl[i]),
  338. iwl_read_direct32(trans, fh_tbl[i]));
  339. return 0;
  340. }
  341. #define IWL_HOST_MON_BLOCK_PEMON 0x00
  342. #define IWL_HOST_MON_BLOCK_HIPM 0x22
  343. #define IWL_HOST_MON_BLOCK_PEMON_VEC0 0x00
  344. #define IWL_HOST_MON_BLOCK_PEMON_VEC1 0x01
  345. #define IWL_HOST_MON_BLOCK_PEMON_WFPM 0x06
  346. static void iwl_dump_host_monitor_block(struct iwl_trans *trans,
  347. u32 block, u32 vec, u32 iter)
  348. {
  349. int i;
  350. IWL_ERR(trans, "Host monitor block 0x%x vector 0x%x\n", block, vec);
  351. iwl_write32(trans, CSR_MONITOR_CFG_REG, (block << 8) | vec);
  352. for (i = 0; i < iter; i++)
  353. IWL_ERR(trans, " value [iter %d]: 0x%08x\n",
  354. i, iwl_read32(trans, CSR_MONITOR_STATUS_REG));
  355. }
  356. static void iwl_dump_host_monitor(struct iwl_trans *trans)
  357. {
  358. switch (trans->trans_cfg->device_family) {
  359. case IWL_DEVICE_FAMILY_22000:
  360. case IWL_DEVICE_FAMILY_AX210:
  361. IWL_ERR(trans, "CSR_RESET = 0x%x\n",
  362. iwl_read32(trans, CSR_RESET));
  363. iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_PEMON,
  364. IWL_HOST_MON_BLOCK_PEMON_VEC0, 15);
  365. iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_PEMON,
  366. IWL_HOST_MON_BLOCK_PEMON_VEC1, 15);
  367. iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_PEMON,
  368. IWL_HOST_MON_BLOCK_PEMON_WFPM, 15);
  369. iwl_dump_host_monitor_block(trans, IWL_HOST_MON_BLOCK_HIPM,
  370. IWL_HOST_MON_BLOCK_PEMON_VEC0, 1);
  371. break;
  372. default:
  373. /* not supported yet */
  374. return;
  375. }
  376. }
  377. int iwl_finish_nic_init(struct iwl_trans *trans)
  378. {
  379. const struct iwl_cfg_trans_params *cfg_trans = trans->trans_cfg;
  380. u32 poll_ready;
  381. int err;
  382. if (cfg_trans->bisr_workaround) {
  383. /* ensure the TOP FSM isn't still in previous reset */
  384. mdelay(2);
  385. }
  386. /*
  387. * Set "initialization complete" bit to move adapter from
  388. * D0U* --> D0A* (powered-up active) state.
  389. */
  390. if (cfg_trans->device_family >= IWL_DEVICE_FAMILY_BZ) {
  391. iwl_set_bit(trans, CSR_GP_CNTRL,
  392. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  393. CSR_GP_CNTRL_REG_FLAG_MAC_INIT);
  394. poll_ready = CSR_GP_CNTRL_REG_FLAG_MAC_STATUS;
  395. } else {
  396. iwl_set_bit(trans, CSR_GP_CNTRL,
  397. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  398. poll_ready = CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY;
  399. }
  400. if (cfg_trans->device_family == IWL_DEVICE_FAMILY_8000)
  401. udelay(2);
  402. /*
  403. * Wait for clock stabilization; once stabilized, access to
  404. * device-internal resources is supported, e.g. iwl_write_prph()
  405. * and accesses to uCode SRAM.
  406. */
  407. err = iwl_poll_bit(trans, CSR_GP_CNTRL, poll_ready, poll_ready, 25000);
  408. if (err < 0) {
  409. IWL_DEBUG_INFO(trans, "Failed to wake NIC\n");
  410. iwl_dump_host_monitor(trans);
  411. }
  412. if (cfg_trans->bisr_workaround) {
  413. /* ensure BISR shift has finished */
  414. udelay(200);
  415. }
  416. return err < 0 ? err : 0;
  417. }
  418. IWL_EXPORT_SYMBOL(iwl_finish_nic_init);
  419. void iwl_trans_sync_nmi_with_addr(struct iwl_trans *trans, u32 inta_addr,
  420. u32 sw_err_bit)
  421. {
  422. unsigned long timeout = jiffies + IWL_TRANS_NMI_TIMEOUT;
  423. bool interrupts_enabled = test_bit(STATUS_INT_ENABLED, &trans->status);
  424. /* if the interrupts were already disabled, there is no point in
  425. * calling iwl_disable_interrupts
  426. */
  427. if (interrupts_enabled)
  428. iwl_trans_interrupts(trans, false);
  429. iwl_force_nmi(trans);
  430. while (time_after(timeout, jiffies)) {
  431. u32 inta_hw = iwl_read32(trans, inta_addr);
  432. /* Error detected by uCode */
  433. if (inta_hw & sw_err_bit) {
  434. /* Clear causes register */
  435. iwl_write32(trans, inta_addr, inta_hw & sw_err_bit);
  436. break;
  437. }
  438. mdelay(1);
  439. }
  440. /* enable interrupts only if there were already enabled before this
  441. * function to avoid a case were the driver enable interrupts before
  442. * proper configurations were made
  443. */
  444. if (interrupts_enabled)
  445. iwl_trans_interrupts(trans, true);
  446. iwl_trans_fw_error(trans, false);
  447. }