iwl-trans-pcie-tx.c 26 KB

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