tx.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  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. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include "iwl-debug.h"
  33. #include "iwl-csr.h"
  34. #include "iwl-prph.h"
  35. #include "iwl-io.h"
  36. #include "iwl-op-mode.h"
  37. #include "internal.h"
  38. /* FIXME: need to abstract out TX command (once we know what it looks like) */
  39. #include "dvm/commands.h"
  40. #define IWL_TX_CRC_SIZE 4
  41. #define IWL_TX_DELIMITER_SIZE 4
  42. /*
  43. * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  44. */
  45. void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  46. struct iwl_txq *txq, u16 byte_cnt)
  47. {
  48. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  49. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  50. int write_ptr = txq->q.write_ptr;
  51. int txq_id = txq->q.id;
  52. u8 sec_ctl = 0;
  53. u8 sta_id = 0;
  54. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  55. __le16 bc_ent;
  56. struct iwl_tx_cmd *tx_cmd =
  57. (void *) txq->entries[txq->q.write_ptr].cmd->payload;
  58. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  59. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  60. sta_id = tx_cmd->sta_id;
  61. sec_ctl = tx_cmd->sec_ctl;
  62. switch (sec_ctl & TX_CMD_SEC_MSK) {
  63. case TX_CMD_SEC_CCM:
  64. len += CCMP_MIC_LEN;
  65. break;
  66. case TX_CMD_SEC_TKIP:
  67. len += TKIP_ICV_LEN;
  68. break;
  69. case TX_CMD_SEC_WEP:
  70. len += WEP_IV_LEN + WEP_ICV_LEN;
  71. break;
  72. }
  73. bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12));
  74. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  75. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  76. scd_bc_tbl[txq_id].
  77. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  78. }
  79. /*
  80. * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
  81. */
  82. void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq)
  83. {
  84. u32 reg = 0;
  85. int txq_id = txq->q.id;
  86. if (txq->need_update == 0)
  87. return;
  88. if (trans->cfg->base_params->shadow_reg_enable) {
  89. /* shadow register enabled */
  90. iwl_write32(trans, HBUS_TARG_WRPTR,
  91. txq->q.write_ptr | (txq_id << 8));
  92. } else {
  93. struct iwl_trans_pcie *trans_pcie =
  94. IWL_TRANS_GET_PCIE_TRANS(trans);
  95. /* if we're trying to save power */
  96. if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
  97. /* wake up nic if it's powered down ...
  98. * uCode will wake up, and interrupt us again, so next
  99. * time we'll skip this part. */
  100. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  101. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  102. IWL_DEBUG_INFO(trans,
  103. "Tx queue %d requesting wakeup,"
  104. " GP1 = 0x%x\n", txq_id, reg);
  105. iwl_set_bit(trans, CSR_GP_CNTRL,
  106. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  107. return;
  108. }
  109. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  110. txq->q.write_ptr | (txq_id << 8));
  111. /*
  112. * else not in power-save mode,
  113. * uCode will never sleep when we're
  114. * trying to tx (during RFKILL, we're not trying to tx).
  115. */
  116. } else
  117. iwl_write32(trans, HBUS_TARG_WRPTR,
  118. txq->q.write_ptr | (txq_id << 8));
  119. }
  120. txq->need_update = 0;
  121. }
  122. static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  123. {
  124. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  125. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  126. if (sizeof(dma_addr_t) > sizeof(u32))
  127. addr |=
  128. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  129. return addr;
  130. }
  131. static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
  132. {
  133. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  134. return le16_to_cpu(tb->hi_n_len) >> 4;
  135. }
  136. static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  137. dma_addr_t addr, u16 len)
  138. {
  139. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  140. u16 hi_n_len = len << 4;
  141. put_unaligned_le32(addr, &tb->lo);
  142. if (sizeof(dma_addr_t) > sizeof(u32))
  143. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  144. tb->hi_n_len = cpu_to_le16(hi_n_len);
  145. tfd->num_tbs = idx + 1;
  146. }
  147. static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
  148. {
  149. return tfd->num_tbs & 0x1f;
  150. }
  151. static void iwl_unmap_tfd(struct iwl_trans *trans, struct iwl_cmd_meta *meta,
  152. struct iwl_tfd *tfd, enum dma_data_direction dma_dir)
  153. {
  154. int i;
  155. int num_tbs;
  156. /* Sanity check on number of chunks */
  157. num_tbs = iwl_tfd_get_num_tbs(tfd);
  158. if (num_tbs >= IWL_NUM_OF_TBS) {
  159. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  160. /* @todo issue fatal error, it is quite serious situation */
  161. return;
  162. }
  163. /* Unmap tx_cmd */
  164. if (num_tbs)
  165. dma_unmap_single(trans->dev,
  166. dma_unmap_addr(meta, mapping),
  167. dma_unmap_len(meta, len),
  168. DMA_BIDIRECTIONAL);
  169. /* Unmap chunks, if any. */
  170. for (i = 1; i < num_tbs; i++)
  171. dma_unmap_single(trans->dev, iwl_tfd_tb_get_addr(tfd, i),
  172. iwl_tfd_tb_get_len(tfd, i), dma_dir);
  173. tfd->num_tbs = 0;
  174. }
  175. /*
  176. * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  177. * @trans - transport private data
  178. * @txq - tx queue
  179. * @dma_dir - the direction of the DMA mapping
  180. *
  181. * Does NOT advance any TFD circular buffer read/write indexes
  182. * Does NOT free the TFD itself (which is within circular buffer)
  183. */
  184. void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  185. enum dma_data_direction dma_dir)
  186. {
  187. struct iwl_tfd *tfd_tmp = txq->tfds;
  188. /* rd_ptr is bounded by n_bd and idx is bounded by n_window */
  189. int rd_ptr = txq->q.read_ptr;
  190. int idx = get_cmd_index(&txq->q, rd_ptr);
  191. lockdep_assert_held(&txq->lock);
  192. /* We have only q->n_window txq->entries, but we use q->n_bd tfds */
  193. iwl_unmap_tfd(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr],
  194. dma_dir);
  195. /* free SKB */
  196. if (txq->entries) {
  197. struct sk_buff *skb;
  198. skb = txq->entries[idx].skb;
  199. /* Can be called from irqs-disabled context
  200. * If skb is not NULL, it means that the whole queue is being
  201. * freed and that the queue is not empty - free the skb
  202. */
  203. if (skb) {
  204. iwl_op_mode_free_skb(trans->op_mode, skb);
  205. txq->entries[idx].skb = NULL;
  206. }
  207. }
  208. }
  209. int iwl_pcie_tx_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  210. dma_addr_t addr, u16 len, u8 reset)
  211. {
  212. struct iwl_queue *q;
  213. struct iwl_tfd *tfd, *tfd_tmp;
  214. u32 num_tbs;
  215. q = &txq->q;
  216. tfd_tmp = txq->tfds;
  217. tfd = &tfd_tmp[q->write_ptr];
  218. if (reset)
  219. memset(tfd, 0, sizeof(*tfd));
  220. num_tbs = iwl_tfd_get_num_tbs(tfd);
  221. /* Each TFD can point to a maximum 20 Tx buffers */
  222. if (num_tbs >= IWL_NUM_OF_TBS) {
  223. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  224. IWL_NUM_OF_TBS);
  225. return -EINVAL;
  226. }
  227. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  228. return -EINVAL;
  229. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  230. IWL_ERR(trans, "Unaligned address = %llx\n",
  231. (unsigned long long)addr);
  232. iwl_tfd_set_tb(tfd, num_tbs, addr, len);
  233. return 0;
  234. }
  235. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  236. * DMA services
  237. *
  238. * Theory of operation
  239. *
  240. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  241. * of buffer descriptors, each of which points to one or more data buffers for
  242. * the device to read from or fill. Driver and device exchange status of each
  243. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  244. * entries in each circular buffer, to protect against confusing empty and full
  245. * queue states.
  246. *
  247. * The device reads or writes the data in the queues via the device's several
  248. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  249. *
  250. * For Tx queue, there are low mark and high mark limits. If, after queuing
  251. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  252. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  253. * Tx queue resumed.
  254. *
  255. ***************************************************/
  256. int iwl_queue_space(const struct iwl_queue *q)
  257. {
  258. int s = q->read_ptr - q->write_ptr;
  259. if (q->read_ptr > q->write_ptr)
  260. s -= q->n_bd;
  261. if (s <= 0)
  262. s += q->n_window;
  263. /* keep some reserve to not confuse empty and full situations */
  264. s -= 2;
  265. if (s < 0)
  266. s = 0;
  267. return s;
  268. }
  269. /*
  270. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  271. */
  272. int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
  273. {
  274. q->n_bd = count;
  275. q->n_window = slots_num;
  276. q->id = id;
  277. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  278. * and iwl_queue_dec_wrap are broken. */
  279. if (WARN_ON(!is_power_of_2(count)))
  280. return -EINVAL;
  281. /* slots_num must be power-of-two size, otherwise
  282. * get_cmd_index is broken. */
  283. if (WARN_ON(!is_power_of_2(slots_num)))
  284. return -EINVAL;
  285. q->low_mark = q->n_window / 4;
  286. if (q->low_mark < 4)
  287. q->low_mark = 4;
  288. q->high_mark = q->n_window / 8;
  289. if (q->high_mark < 2)
  290. q->high_mark = 2;
  291. q->write_ptr = q->read_ptr = 0;
  292. return 0;
  293. }
  294. static void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  295. struct iwl_txq *txq)
  296. {
  297. struct iwl_trans_pcie *trans_pcie =
  298. IWL_TRANS_GET_PCIE_TRANS(trans);
  299. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  300. int txq_id = txq->q.id;
  301. int read_ptr = txq->q.read_ptr;
  302. u8 sta_id = 0;
  303. __le16 bc_ent;
  304. struct iwl_tx_cmd *tx_cmd =
  305. (void *)txq->entries[txq->q.read_ptr].cmd->payload;
  306. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  307. if (txq_id != trans_pcie->cmd_queue)
  308. sta_id = tx_cmd->sta_id;
  309. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  310. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  311. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  312. scd_bc_tbl[txq_id].
  313. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  314. }
  315. static int iwl_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  316. u16 txq_id)
  317. {
  318. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  319. u32 tbl_dw_addr;
  320. u32 tbl_dw;
  321. u16 scd_q2ratid;
  322. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  323. tbl_dw_addr = trans_pcie->scd_base_addr +
  324. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  325. tbl_dw = iwl_read_targ_mem(trans, tbl_dw_addr);
  326. if (txq_id & 0x1)
  327. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  328. else
  329. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  330. iwl_write_targ_mem(trans, tbl_dw_addr, tbl_dw);
  331. return 0;
  332. }
  333. static inline void iwl_txq_set_inactive(struct iwl_trans *trans, u16 txq_id)
  334. {
  335. /* Simply stop the queue, but don't change any configuration;
  336. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  337. iwl_write_prph(trans,
  338. SCD_QUEUE_STATUS_BITS(txq_id),
  339. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  340. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  341. }
  342. void iwl_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
  343. int sta_id, int tid, int frame_limit, u16 ssn)
  344. {
  345. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  346. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  347. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  348. /* Stop this Tx queue before configuring it */
  349. iwl_txq_set_inactive(trans, txq_id);
  350. /* Set this queue as a chain-building queue unless it is CMD queue */
  351. if (txq_id != trans_pcie->cmd_queue)
  352. iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
  353. /* If this queue is mapped to a certain station: it is an AGG queue */
  354. if (sta_id != IWL_INVALID_STATION) {
  355. u16 ra_tid = BUILD_RAxTID(sta_id, tid);
  356. /* Map receiver-address / traffic-ID to this queue */
  357. iwl_txq_set_ratid_map(trans, ra_tid, txq_id);
  358. /* enable aggregations for the queue */
  359. iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  360. } else {
  361. /*
  362. * disable aggregations for the queue, this will also make the
  363. * ra_tid mapping configuration irrelevant since it is now a
  364. * non-AGG queue.
  365. */
  366. iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  367. }
  368. /* Place first TFD at index corresponding to start sequence number.
  369. * Assumes that ssn_idx is valid (!= 0xFFF) */
  370. trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
  371. trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
  372. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  373. (ssn & 0xff) | (txq_id << 8));
  374. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  375. /* Set up Tx window size and frame limit for this queue */
  376. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  377. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  378. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  379. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  380. ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  381. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  382. ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  383. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  384. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  385. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  386. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  387. (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  388. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  389. SCD_QUEUE_STTS_REG_MSK);
  390. IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
  391. txq_id, fifo, ssn & 0xff);
  392. }
  393. void iwl_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
  394. {
  395. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  396. u32 stts_addr = trans_pcie->scd_base_addr +
  397. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  398. static const u32 zero_val[4] = {};
  399. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  400. WARN_ONCE(1, "queue %d not used", txq_id);
  401. return;
  402. }
  403. iwl_txq_set_inactive(trans, txq_id);
  404. _iwl_write_targ_mem_dwords(trans, stts_addr,
  405. zero_val, ARRAY_SIZE(zero_val));
  406. iwl_pcie_txq_unmap(trans, txq_id);
  407. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  408. }
  409. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  410. /*
  411. * iwl_enqueue_hcmd - enqueue a uCode command
  412. * @priv: device private data point
  413. * @cmd: a point to the ucode command structure
  414. *
  415. * The function returns < 0 values to indicate the operation is
  416. * failed. On success, it turns the index (> 0) of command in the
  417. * command queue.
  418. */
  419. static int iwl_enqueue_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  420. {
  421. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  422. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  423. struct iwl_queue *q = &txq->q;
  424. struct iwl_device_cmd *out_cmd;
  425. struct iwl_cmd_meta *out_meta;
  426. void *dup_buf = NULL;
  427. dma_addr_t phys_addr;
  428. int idx;
  429. u16 copy_size, cmd_size;
  430. bool had_nocopy = false;
  431. int i;
  432. u32 cmd_pos;
  433. copy_size = sizeof(out_cmd->hdr);
  434. cmd_size = sizeof(out_cmd->hdr);
  435. /* need one for the header if the first is NOCOPY */
  436. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  437. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  438. if (!cmd->len[i])
  439. continue;
  440. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  441. had_nocopy = true;
  442. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  443. idx = -EINVAL;
  444. goto free_dup_buf;
  445. }
  446. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  447. /*
  448. * This is also a chunk that isn't copied
  449. * to the static buffer so set had_nocopy.
  450. */
  451. had_nocopy = true;
  452. /* only allowed once */
  453. if (WARN_ON(dup_buf)) {
  454. idx = -EINVAL;
  455. goto free_dup_buf;
  456. }
  457. dup_buf = kmemdup(cmd->data[i], cmd->len[i],
  458. GFP_ATOMIC);
  459. if (!dup_buf)
  460. return -ENOMEM;
  461. } else {
  462. /* NOCOPY must not be followed by normal! */
  463. if (WARN_ON(had_nocopy)) {
  464. idx = -EINVAL;
  465. goto free_dup_buf;
  466. }
  467. copy_size += cmd->len[i];
  468. }
  469. cmd_size += cmd->len[i];
  470. }
  471. /*
  472. * If any of the command structures end up being larger than
  473. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  474. * allocated into separate TFDs, then we will need to
  475. * increase the size of the buffers.
  476. */
  477. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  478. "Command %s (%#x) is too large (%d bytes)\n",
  479. get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
  480. idx = -EINVAL;
  481. goto free_dup_buf;
  482. }
  483. spin_lock_bh(&txq->lock);
  484. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  485. spin_unlock_bh(&txq->lock);
  486. IWL_ERR(trans, "No space in command queue\n");
  487. iwl_op_mode_cmd_queue_full(trans->op_mode);
  488. idx = -ENOSPC;
  489. goto free_dup_buf;
  490. }
  491. idx = get_cmd_index(q, q->write_ptr);
  492. out_cmd = txq->entries[idx].cmd;
  493. out_meta = &txq->entries[idx].meta;
  494. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  495. if (cmd->flags & CMD_WANT_SKB)
  496. out_meta->source = cmd;
  497. /* set up the header */
  498. out_cmd->hdr.cmd = cmd->id;
  499. out_cmd->hdr.flags = 0;
  500. out_cmd->hdr.sequence =
  501. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  502. INDEX_TO_SEQ(q->write_ptr));
  503. /* and copy the data that needs to be copied */
  504. cmd_pos = offsetof(struct iwl_device_cmd, payload);
  505. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  506. if (!cmd->len[i])
  507. continue;
  508. if (cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  509. IWL_HCMD_DFL_DUP))
  510. break;
  511. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], cmd->len[i]);
  512. cmd_pos += cmd->len[i];
  513. }
  514. WARN_ON_ONCE(txq->entries[idx].copy_cmd);
  515. /*
  516. * since out_cmd will be the source address of the FH, it will write
  517. * the retry count there. So when the user needs to receivce the HCMD
  518. * that corresponds to the response in the response handler, it needs
  519. * to set CMD_WANT_HCMD.
  520. */
  521. if (cmd->flags & CMD_WANT_HCMD) {
  522. txq->entries[idx].copy_cmd =
  523. kmemdup(out_cmd, cmd_pos, GFP_ATOMIC);
  524. if (unlikely(!txq->entries[idx].copy_cmd)) {
  525. idx = -ENOMEM;
  526. goto out;
  527. }
  528. }
  529. IWL_DEBUG_HC(trans,
  530. "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  531. get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
  532. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  533. cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
  534. phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size,
  535. DMA_BIDIRECTIONAL);
  536. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  537. idx = -ENOMEM;
  538. goto out;
  539. }
  540. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  541. dma_unmap_len_set(out_meta, len, copy_size);
  542. iwl_pcie_tx_build_tfd(trans, txq, phys_addr, copy_size, 1);
  543. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  544. const void *data = cmd->data[i];
  545. if (!cmd->len[i])
  546. continue;
  547. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  548. IWL_HCMD_DFL_DUP)))
  549. continue;
  550. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  551. data = dup_buf;
  552. phys_addr = dma_map_single(trans->dev, (void *)data,
  553. cmd->len[i], DMA_BIDIRECTIONAL);
  554. if (dma_mapping_error(trans->dev, phys_addr)) {
  555. iwl_unmap_tfd(trans, out_meta,
  556. &txq->tfds[q->write_ptr],
  557. DMA_BIDIRECTIONAL);
  558. idx = -ENOMEM;
  559. goto out;
  560. }
  561. iwl_pcie_tx_build_tfd(trans, txq, phys_addr, cmd->len[i], 0);
  562. }
  563. out_meta->flags = cmd->flags;
  564. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  565. kfree(txq->entries[idx].free_buf);
  566. txq->entries[idx].free_buf = dup_buf;
  567. txq->need_update = 1;
  568. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size,
  569. &out_cmd->hdr, copy_size);
  570. /* start timer if queue currently empty */
  571. if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
  572. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  573. /* Increment and update queue's write index */
  574. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  575. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  576. out:
  577. spin_unlock_bh(&txq->lock);
  578. free_dup_buf:
  579. if (idx < 0)
  580. kfree(dup_buf);
  581. return idx;
  582. }
  583. static inline void iwl_queue_progress(struct iwl_trans_pcie *trans_pcie,
  584. struct iwl_txq *txq)
  585. {
  586. if (!trans_pcie->wd_timeout)
  587. return;
  588. /*
  589. * if empty delete timer, otherwise move timer forward
  590. * since we're making progress on this queue
  591. */
  592. if (txq->q.read_ptr == txq->q.write_ptr)
  593. del_timer(&txq->stuck_timer);
  594. else
  595. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  596. }
  597. /*
  598. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  599. *
  600. * When FW advances 'R' index, all entries between old and new 'R' index
  601. * need to be reclaimed. As result, some free space forms. If there is
  602. * enough free space (> low mark), wake the stack that feeds us.
  603. */
  604. static void iwl_hcmd_queue_reclaim(struct iwl_trans *trans, int txq_id,
  605. int idx)
  606. {
  607. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  608. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  609. struct iwl_queue *q = &txq->q;
  610. int nfreed = 0;
  611. lockdep_assert_held(&txq->lock);
  612. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  613. IWL_ERR(trans,
  614. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  615. __func__, txq_id, idx, q->n_bd,
  616. q->write_ptr, q->read_ptr);
  617. return;
  618. }
  619. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  620. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  621. if (nfreed++ > 0) {
  622. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  623. idx, q->write_ptr, q->read_ptr);
  624. iwl_op_mode_nic_error(trans->op_mode);
  625. }
  626. }
  627. iwl_queue_progress(trans_pcie, txq);
  628. }
  629. /*
  630. * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
  631. * @rxb: Rx buffer to reclaim
  632. * @handler_status: return value of the handler of the command
  633. * (put in setup_rx_handlers)
  634. *
  635. * If an Rx buffer has an async callback associated with it the callback
  636. * will be executed. The attached skb (if present) will only be freed
  637. * if the callback returns 1
  638. */
  639. void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
  640. struct iwl_rx_cmd_buffer *rxb, int handler_status)
  641. {
  642. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  643. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  644. int txq_id = SEQ_TO_QUEUE(sequence);
  645. int index = SEQ_TO_INDEX(sequence);
  646. int cmd_index;
  647. struct iwl_device_cmd *cmd;
  648. struct iwl_cmd_meta *meta;
  649. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  650. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  651. /* If a Tx command is being handled and it isn't in the actual
  652. * command queue then there a command routing bug has been introduced
  653. * in the queue management code. */
  654. if (WARN(txq_id != trans_pcie->cmd_queue,
  655. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  656. txq_id, trans_pcie->cmd_queue, sequence,
  657. trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
  658. trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
  659. iwl_print_hex_error(trans, pkt, 32);
  660. return;
  661. }
  662. spin_lock(&txq->lock);
  663. cmd_index = get_cmd_index(&txq->q, index);
  664. cmd = txq->entries[cmd_index].cmd;
  665. meta = &txq->entries[cmd_index].meta;
  666. iwl_unmap_tfd(trans, meta, &txq->tfds[index], DMA_BIDIRECTIONAL);
  667. /* Input error checking is done when commands are added to queue. */
  668. if (meta->flags & CMD_WANT_SKB) {
  669. struct page *p = rxb_steal_page(rxb);
  670. meta->source->resp_pkt = pkt;
  671. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  672. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  673. meta->source->handler_status = handler_status;
  674. }
  675. iwl_hcmd_queue_reclaim(trans, txq_id, index);
  676. if (!(meta->flags & CMD_ASYNC)) {
  677. if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  678. IWL_WARN(trans,
  679. "HCMD_ACTIVE already clear for command %s\n",
  680. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  681. }
  682. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  683. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  684. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  685. wake_up(&trans_pcie->wait_command_queue);
  686. }
  687. meta->flags = 0;
  688. spin_unlock(&txq->lock);
  689. }
  690. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  691. static int iwl_send_cmd_async(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  692. {
  693. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  694. int ret;
  695. /* An asynchronous command can not expect an SKB to be set. */
  696. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  697. return -EINVAL;
  698. ret = iwl_enqueue_hcmd(trans, cmd);
  699. if (ret < 0) {
  700. IWL_ERR(trans,
  701. "Error sending %s: enqueue_hcmd failed: %d\n",
  702. get_cmd_string(trans_pcie, cmd->id), ret);
  703. return ret;
  704. }
  705. return 0;
  706. }
  707. static int iwl_send_cmd_sync(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  708. {
  709. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  710. int cmd_idx;
  711. int ret;
  712. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  713. get_cmd_string(trans_pcie, cmd->id));
  714. if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
  715. &trans_pcie->status))) {
  716. IWL_ERR(trans, "Command %s: a command is already active!\n",
  717. get_cmd_string(trans_pcie, cmd->id));
  718. return -EIO;
  719. }
  720. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  721. get_cmd_string(trans_pcie, cmd->id));
  722. cmd_idx = iwl_enqueue_hcmd(trans, cmd);
  723. if (cmd_idx < 0) {
  724. ret = cmd_idx;
  725. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  726. IWL_ERR(trans,
  727. "Error sending %s: enqueue_hcmd failed: %d\n",
  728. get_cmd_string(trans_pcie, cmd->id), ret);
  729. return ret;
  730. }
  731. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  732. !test_bit(STATUS_HCMD_ACTIVE,
  733. &trans_pcie->status),
  734. HOST_COMPLETE_TIMEOUT);
  735. if (!ret) {
  736. if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  737. struct iwl_txq *txq =
  738. &trans_pcie->txq[trans_pcie->cmd_queue];
  739. struct iwl_queue *q = &txq->q;
  740. IWL_ERR(trans,
  741. "Error sending %s: time out after %dms.\n",
  742. get_cmd_string(trans_pcie, cmd->id),
  743. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  744. IWL_ERR(trans,
  745. "Current CMD queue read_ptr %d write_ptr %d\n",
  746. q->read_ptr, q->write_ptr);
  747. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  748. IWL_DEBUG_INFO(trans,
  749. "Clearing HCMD_ACTIVE for command %s\n",
  750. get_cmd_string(trans_pcie, cmd->id));
  751. ret = -ETIMEDOUT;
  752. goto cancel;
  753. }
  754. }
  755. if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) {
  756. IWL_ERR(trans, "FW error in SYNC CMD %s\n",
  757. get_cmd_string(trans_pcie, cmd->id));
  758. ret = -EIO;
  759. goto cancel;
  760. }
  761. if (test_bit(STATUS_RFKILL, &trans_pcie->status)) {
  762. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  763. ret = -ERFKILL;
  764. goto cancel;
  765. }
  766. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  767. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  768. get_cmd_string(trans_pcie, cmd->id));
  769. ret = -EIO;
  770. goto cancel;
  771. }
  772. return 0;
  773. cancel:
  774. if (cmd->flags & CMD_WANT_SKB) {
  775. /*
  776. * Cancel the CMD_WANT_SKB flag for the cmd in the
  777. * TX cmd queue. Otherwise in case the cmd comes
  778. * in later, it will possibly set an invalid
  779. * address (cmd->meta.source).
  780. */
  781. trans_pcie->txq[trans_pcie->cmd_queue].
  782. entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  783. }
  784. if (cmd->resp_pkt) {
  785. iwl_free_resp(cmd);
  786. cmd->resp_pkt = NULL;
  787. }
  788. return ret;
  789. }
  790. int iwl_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  791. {
  792. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  793. if (test_bit(STATUS_FW_ERROR, &trans_pcie->status))
  794. return -EIO;
  795. if (test_bit(STATUS_RFKILL, &trans_pcie->status))
  796. return -ERFKILL;
  797. if (cmd->flags & CMD_ASYNC)
  798. return iwl_send_cmd_async(trans, cmd);
  799. /* We still can fail on RFKILL that can be asserted while we wait */
  800. return iwl_send_cmd_sync(trans, cmd);
  801. }
  802. /* Frees buffers until index _not_ inclusive */
  803. int iwl_pcie_txq_reclaim(struct iwl_trans *trans, int txq_id, int index,
  804. struct sk_buff_head *skbs)
  805. {
  806. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  807. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  808. struct iwl_queue *q = &txq->q;
  809. int last_to_free;
  810. int freed = 0;
  811. /* This function is not meant to release cmd queue*/
  812. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  813. return 0;
  814. lockdep_assert_held(&txq->lock);
  815. /*Since we free until index _not_ inclusive, the one before index is
  816. * the last we will free. This one must be used */
  817. last_to_free = iwl_queue_dec_wrap(index, q->n_bd);
  818. if ((index >= q->n_bd) ||
  819. (iwl_queue_used(q, last_to_free) == 0)) {
  820. IWL_ERR(trans,
  821. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  822. __func__, txq_id, last_to_free, q->n_bd,
  823. q->write_ptr, q->read_ptr);
  824. return 0;
  825. }
  826. if (WARN_ON(!skb_queue_empty(skbs)))
  827. return 0;
  828. for (;
  829. q->read_ptr != index;
  830. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  831. if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
  832. continue;
  833. __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
  834. txq->entries[txq->q.read_ptr].skb = NULL;
  835. iwlagn_txq_inval_byte_cnt_tbl(trans, txq);
  836. iwl_pcie_txq_free_tfd(trans, txq, DMA_TO_DEVICE);
  837. freed++;
  838. }
  839. iwl_queue_progress(trans_pcie, txq);
  840. return freed;
  841. }