iwl-trans-pcie-tx.c 31 KB

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