iwl-trans-pcie-tx.c 28 KB

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