iwl-trans-pcie-tx.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. }
  207. /**
  208. * iwlagn_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  209. * @trans - transport private data
  210. * @txq - tx queue
  211. * @index - the index of the TFD to be freed
  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. int index, enum dma_data_direction dma_dir)
  219. {
  220. struct iwl_tfd *tfd_tmp = txq->tfds;
  221. lockdep_assert_held(&txq->lock);
  222. iwlagn_unmap_tfd(trans, &txq->meta[index], &tfd_tmp[index], dma_dir);
  223. /* free SKB */
  224. if (txq->skbs) {
  225. struct sk_buff *skb;
  226. skb = txq->skbs[index];
  227. /* Can be called from irqs-disabled context
  228. * If skb is not NULL, it means that the whole queue is being
  229. * freed and that the queue is not empty - free the skb
  230. */
  231. if (skb) {
  232. iwl_op_mode_free_skb(trans->op_mode, skb);
  233. txq->skbs[index] = NULL;
  234. }
  235. }
  236. }
  237. int iwlagn_txq_attach_buf_to_tfd(struct iwl_trans *trans,
  238. struct iwl_tx_queue *txq,
  239. dma_addr_t addr, u16 len,
  240. u8 reset)
  241. {
  242. struct iwl_queue *q;
  243. struct iwl_tfd *tfd, *tfd_tmp;
  244. u32 num_tbs;
  245. q = &txq->q;
  246. tfd_tmp = txq->tfds;
  247. tfd = &tfd_tmp[q->write_ptr];
  248. if (reset)
  249. memset(tfd, 0, sizeof(*tfd));
  250. num_tbs = iwl_tfd_get_num_tbs(tfd);
  251. /* Each TFD can point to a maximum 20 Tx buffers */
  252. if (num_tbs >= IWL_NUM_OF_TBS) {
  253. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  254. IWL_NUM_OF_TBS);
  255. return -EINVAL;
  256. }
  257. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  258. return -EINVAL;
  259. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  260. IWL_ERR(trans, "Unaligned address = %llx\n",
  261. (unsigned long long)addr);
  262. iwl_tfd_set_tb(tfd, num_tbs, addr, len);
  263. return 0;
  264. }
  265. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  266. * DMA services
  267. *
  268. * Theory of operation
  269. *
  270. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  271. * of buffer descriptors, each of which points to one or more data buffers for
  272. * the device to read from or fill. Driver and device exchange status of each
  273. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  274. * entries in each circular buffer, to protect against confusing empty and full
  275. * queue states.
  276. *
  277. * The device reads or writes the data in the queues via the device's several
  278. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  279. *
  280. * For Tx queue, there are low mark and high mark limits. If, after queuing
  281. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  282. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  283. * Tx queue resumed.
  284. *
  285. ***************************************************/
  286. int iwl_queue_space(const struct iwl_queue *q)
  287. {
  288. int s = q->read_ptr - q->write_ptr;
  289. if (q->read_ptr > q->write_ptr)
  290. s -= q->n_bd;
  291. if (s <= 0)
  292. s += q->n_window;
  293. /* keep some reserve to not confuse empty and full situations */
  294. s -= 2;
  295. if (s < 0)
  296. s = 0;
  297. return s;
  298. }
  299. /**
  300. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  301. */
  302. int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
  303. {
  304. q->n_bd = count;
  305. q->n_window = slots_num;
  306. q->id = id;
  307. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  308. * and iwl_queue_dec_wrap are broken. */
  309. if (WARN_ON(!is_power_of_2(count)))
  310. return -EINVAL;
  311. /* slots_num must be power-of-two size, otherwise
  312. * get_cmd_index is broken. */
  313. if (WARN_ON(!is_power_of_2(slots_num)))
  314. return -EINVAL;
  315. q->low_mark = q->n_window / 4;
  316. if (q->low_mark < 4)
  317. q->low_mark = 4;
  318. q->high_mark = q->n_window / 8;
  319. if (q->high_mark < 2)
  320. q->high_mark = 2;
  321. q->write_ptr = q->read_ptr = 0;
  322. return 0;
  323. }
  324. static void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  325. struct iwl_tx_queue *txq)
  326. {
  327. struct iwl_trans_pcie *trans_pcie =
  328. IWL_TRANS_GET_PCIE_TRANS(trans);
  329. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  330. int txq_id = txq->q.id;
  331. int read_ptr = txq->q.read_ptr;
  332. u8 sta_id = 0;
  333. __le16 bc_ent;
  334. struct iwl_tx_cmd *tx_cmd =
  335. (struct iwl_tx_cmd *) txq->cmd[txq->q.read_ptr]->payload;
  336. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  337. if (txq_id != trans->shrd->cmd_queue)
  338. sta_id = tx_cmd->sta_id;
  339. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  340. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  341. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  342. scd_bc_tbl[txq_id].
  343. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  344. }
  345. static int iwlagn_tx_queue_set_q2ratid(struct iwl_trans *trans, u16 ra_tid,
  346. u16 txq_id)
  347. {
  348. u32 tbl_dw_addr;
  349. u32 tbl_dw;
  350. u16 scd_q2ratid;
  351. struct iwl_trans_pcie *trans_pcie =
  352. IWL_TRANS_GET_PCIE_TRANS(trans);
  353. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  354. tbl_dw_addr = trans_pcie->scd_base_addr +
  355. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  356. tbl_dw = iwl_read_targ_mem(trans, tbl_dw_addr);
  357. if (txq_id & 0x1)
  358. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  359. else
  360. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  361. iwl_write_targ_mem(trans, tbl_dw_addr, tbl_dw);
  362. return 0;
  363. }
  364. static void iwlagn_tx_queue_stop_scheduler(struct iwl_trans *trans, u16 txq_id)
  365. {
  366. /* Simply stop the queue, but don't change any configuration;
  367. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  368. iwl_write_prph(trans,
  369. SCD_QUEUE_STATUS_BITS(txq_id),
  370. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  371. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  372. }
  373. void iwl_trans_set_wr_ptrs(struct iwl_trans *trans,
  374. int txq_id, u32 index)
  375. {
  376. IWL_DEBUG_TX_QUEUES(trans, "Q %d WrPtr: %d", txq_id, index & 0xff);
  377. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  378. (index & 0xff) | (txq_id << 8));
  379. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), index);
  380. }
  381. void iwl_trans_tx_queue_set_status(struct iwl_trans *trans,
  382. struct iwl_tx_queue *txq,
  383. int tx_fifo_id, int scd_retry)
  384. {
  385. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  386. int txq_id = txq->q.id;
  387. int active =
  388. test_bit(txq_id, &trans_pcie->txq_ctx_active_msk) ? 1 : 0;
  389. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  390. (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  391. (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
  392. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  393. SCD_QUEUE_STTS_REG_MSK);
  394. txq->sched_retry = scd_retry;
  395. if (active)
  396. IWL_DEBUG_TX_QUEUES(trans, "Activate %s Queue %d on FIFO %d\n",
  397. scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id);
  398. else
  399. IWL_DEBUG_TX_QUEUES(trans, "Deactivate %s Queue %d\n",
  400. scd_retry ? "BA" : "AC/CMD", txq_id);
  401. }
  402. static inline int get_ac_from_tid(u16 tid)
  403. {
  404. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  405. return tid_to_ac[tid];
  406. /* no support for TIDs 8-15 yet */
  407. return -EINVAL;
  408. }
  409. static inline int get_fifo_from_tid(struct iwl_trans_pcie *trans_pcie,
  410. u8 ctx, u16 tid)
  411. {
  412. const u8 *ac_to_fifo = trans_pcie->ac_to_fifo[ctx];
  413. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  414. return ac_to_fifo[tid_to_ac[tid]];
  415. /* no support for TIDs 8-15 yet */
  416. return -EINVAL;
  417. }
  418. static inline bool is_agg_txqid_valid(struct iwl_trans *trans, int txq_id)
  419. {
  420. if (txq_id < IWLAGN_FIRST_AMPDU_QUEUE)
  421. return false;
  422. return txq_id < (IWLAGN_FIRST_AMPDU_QUEUE +
  423. hw_params(trans).num_ampdu_queues);
  424. }
  425. void iwl_trans_pcie_tx_agg_setup(struct iwl_trans *trans,
  426. enum iwl_rxon_context_id ctx, int sta_id,
  427. int tid, int frame_limit, u16 ssn)
  428. {
  429. int tx_fifo, txq_id;
  430. u16 ra_tid;
  431. unsigned long flags;
  432. struct iwl_trans_pcie *trans_pcie =
  433. IWL_TRANS_GET_PCIE_TRANS(trans);
  434. if (WARN_ON(sta_id == IWL_INVALID_STATION))
  435. return;
  436. if (WARN_ON(tid >= IWL_MAX_TID_COUNT))
  437. return;
  438. tx_fifo = get_fifo_from_tid(trans_pcie, ctx, tid);
  439. if (WARN_ON(tx_fifo < 0)) {
  440. IWL_ERR(trans, "txq_agg_setup, bad fifo: %d\n", tx_fifo);
  441. return;
  442. }
  443. txq_id = trans_pcie->agg_txq[sta_id][tid];
  444. if (WARN_ON_ONCE(is_agg_txqid_valid(trans, txq_id) == false)) {
  445. IWL_ERR(trans,
  446. "queue number out of range: %d, must be %d to %d\n",
  447. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  448. IWLAGN_FIRST_AMPDU_QUEUE +
  449. hw_params(trans).num_ampdu_queues - 1);
  450. return;
  451. }
  452. ra_tid = BUILD_RAxTID(sta_id, tid);
  453. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  454. /* Stop this Tx queue before configuring it */
  455. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  456. /* Map receiver-address / traffic-ID to this queue */
  457. iwlagn_tx_queue_set_q2ratid(trans, ra_tid, txq_id);
  458. /* Set this queue as a chain-building queue */
  459. iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, (1<<txq_id));
  460. /* enable aggregations for the queue */
  461. iwl_set_bits_prph(trans, SCD_AGGR_SEL, (1<<txq_id));
  462. /* Place first TFD at index corresponding to start sequence number.
  463. * Assumes that ssn_idx is valid (!= 0xFFF) */
  464. trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
  465. trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
  466. iwl_trans_set_wr_ptrs(trans, txq_id, ssn);
  467. /* Set up Tx window size and frame limit for this queue */
  468. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  469. SCD_CONTEXT_QUEUE_OFFSET(txq_id) +
  470. sizeof(u32),
  471. ((frame_limit <<
  472. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  473. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  474. ((frame_limit <<
  475. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  476. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  477. iwl_set_bits_prph(trans, SCD_INTERRUPT_MASK, (1 << txq_id));
  478. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  479. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[txq_id],
  480. tx_fifo, 1);
  481. trans_pcie->txq[txq_id].sta_id = sta_id;
  482. trans_pcie->txq[txq_id].tid = tid;
  483. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  484. }
  485. /*
  486. * Find first available (lowest unused) Tx Queue, mark it "active".
  487. * Called only when finding queue for aggregation.
  488. * Should never return anything < 7, because they should already
  489. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  490. */
  491. static int iwlagn_txq_ctx_activate_free(struct iwl_trans *trans)
  492. {
  493. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  494. int txq_id;
  495. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  496. if (!test_and_set_bit(txq_id,
  497. &trans_pcie->txq_ctx_active_msk))
  498. return txq_id;
  499. return -1;
  500. }
  501. int iwl_trans_pcie_tx_agg_alloc(struct iwl_trans *trans,
  502. int sta_id, int tid)
  503. {
  504. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  505. int txq_id;
  506. txq_id = iwlagn_txq_ctx_activate_free(trans);
  507. if (txq_id == -1) {
  508. IWL_ERR(trans, "No free aggregation queue available\n");
  509. return -ENXIO;
  510. }
  511. trans_pcie->agg_txq[sta_id][tid] = txq_id;
  512. iwl_set_swq_id(&trans_pcie->txq[txq_id], get_ac_from_tid(tid), txq_id);
  513. return 0;
  514. }
  515. int iwl_trans_pcie_tx_agg_disable(struct iwl_trans *trans, int sta_id, int tid)
  516. {
  517. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  518. u8 txq_id = trans_pcie->agg_txq[sta_id][tid];
  519. if (WARN_ON_ONCE(is_agg_txqid_valid(trans, txq_id) == false)) {
  520. IWL_ERR(trans,
  521. "queue number out of range: %d, must be %d to %d\n",
  522. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  523. IWLAGN_FIRST_AMPDU_QUEUE +
  524. hw_params(trans).num_ampdu_queues - 1);
  525. return -EINVAL;
  526. }
  527. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  528. iwl_clear_bits_prph(trans, SCD_AGGR_SEL, (1 << txq_id));
  529. trans_pcie->agg_txq[sta_id][tid] = 0;
  530. trans_pcie->txq[txq_id].q.read_ptr = 0;
  531. trans_pcie->txq[txq_id].q.write_ptr = 0;
  532. /* supposes that ssn_idx is valid (!= 0xFFF) */
  533. iwl_trans_set_wr_ptrs(trans, txq_id, 0);
  534. iwl_clear_bits_prph(trans, SCD_INTERRUPT_MASK, (1 << txq_id));
  535. iwl_txq_ctx_deactivate(trans_pcie, txq_id);
  536. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[txq_id], 0, 0);
  537. return 0;
  538. }
  539. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  540. /**
  541. * iwl_enqueue_hcmd - enqueue a uCode command
  542. * @priv: device private data point
  543. * @cmd: a point to the ucode command structure
  544. *
  545. * The function returns < 0 values to indicate the operation is
  546. * failed. On success, it turns the index (> 0) of command in the
  547. * command queue.
  548. */
  549. static int iwl_enqueue_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  550. {
  551. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  552. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  553. struct iwl_queue *q = &txq->q;
  554. struct iwl_device_cmd *out_cmd;
  555. struct iwl_cmd_meta *out_meta;
  556. dma_addr_t phys_addr;
  557. u32 idx;
  558. u16 copy_size, cmd_size;
  559. bool is_ct_kill = false;
  560. bool had_nocopy = false;
  561. int i;
  562. u8 *cmd_dest;
  563. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  564. const void *trace_bufs[IWL_MAX_CMD_TFDS + 1] = {};
  565. int trace_lens[IWL_MAX_CMD_TFDS + 1] = {};
  566. int trace_idx;
  567. #endif
  568. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  569. IWL_WARN(trans, "fw recovery, no hcmd send\n");
  570. return -EIO;
  571. }
  572. if ((trans->shrd->ucode_owner == IWL_OWNERSHIP_TM) &&
  573. !(cmd->flags & CMD_ON_DEMAND)) {
  574. IWL_DEBUG_HC(trans, "tm own the uCode, no regular hcmd send\n");
  575. return -EIO;
  576. }
  577. copy_size = sizeof(out_cmd->hdr);
  578. cmd_size = sizeof(out_cmd->hdr);
  579. /* need one for the header if the first is NOCOPY */
  580. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  581. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  582. if (!cmd->len[i])
  583. continue;
  584. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  585. had_nocopy = true;
  586. } else {
  587. /* NOCOPY must not be followed by normal! */
  588. if (WARN_ON(had_nocopy))
  589. return -EINVAL;
  590. copy_size += cmd->len[i];
  591. }
  592. cmd_size += cmd->len[i];
  593. }
  594. /*
  595. * If any of the command structures end up being larger than
  596. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  597. * allocated into separate TFDs, then we will need to
  598. * increase the size of the buffers.
  599. */
  600. if (WARN_ON(copy_size > TFD_MAX_PAYLOAD_SIZE))
  601. return -EINVAL;
  602. spin_lock_bh(&txq->lock);
  603. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  604. spin_unlock_bh(&txq->lock);
  605. IWL_ERR(trans, "No space in command queue\n");
  606. is_ct_kill = iwl_check_for_ct_kill(priv(trans));
  607. if (!is_ct_kill) {
  608. IWL_ERR(trans, "Restarting adapter queue is full\n");
  609. iwl_op_mode_nic_error(trans->op_mode);
  610. }
  611. return -ENOSPC;
  612. }
  613. idx = get_cmd_index(q, q->write_ptr);
  614. out_cmd = txq->cmd[idx];
  615. out_meta = &txq->meta[idx];
  616. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  617. if (cmd->flags & CMD_WANT_SKB)
  618. out_meta->source = cmd;
  619. /* set up the header */
  620. out_cmd->hdr.cmd = cmd->id;
  621. out_cmd->hdr.flags = 0;
  622. out_cmd->hdr.sequence =
  623. cpu_to_le16(QUEUE_TO_SEQ(trans->shrd->cmd_queue) |
  624. INDEX_TO_SEQ(q->write_ptr));
  625. /* and copy the data that needs to be copied */
  626. cmd_dest = out_cmd->payload;
  627. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  628. if (!cmd->len[i])
  629. continue;
  630. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)
  631. break;
  632. memcpy(cmd_dest, cmd->data[i], cmd->len[i]);
  633. cmd_dest += cmd->len[i];
  634. }
  635. IWL_DEBUG_HC(trans, "Sending command %s (#%x), seq: 0x%04X, "
  636. "%d bytes at %d[%d]:%d\n",
  637. get_cmd_string(out_cmd->hdr.cmd),
  638. out_cmd->hdr.cmd,
  639. le16_to_cpu(out_cmd->hdr.sequence), cmd_size,
  640. q->write_ptr, idx, trans->shrd->cmd_queue);
  641. phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size,
  642. DMA_BIDIRECTIONAL);
  643. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  644. idx = -ENOMEM;
  645. goto out;
  646. }
  647. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  648. dma_unmap_len_set(out_meta, len, copy_size);
  649. iwlagn_txq_attach_buf_to_tfd(trans, txq,
  650. phys_addr, copy_size, 1);
  651. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  652. trace_bufs[0] = &out_cmd->hdr;
  653. trace_lens[0] = copy_size;
  654. trace_idx = 1;
  655. #endif
  656. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  657. if (!cmd->len[i])
  658. continue;
  659. if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY))
  660. continue;
  661. phys_addr = dma_map_single(trans->dev,
  662. (void *)cmd->data[i],
  663. cmd->len[i], DMA_BIDIRECTIONAL);
  664. if (dma_mapping_error(trans->dev, phys_addr)) {
  665. iwlagn_unmap_tfd(trans, out_meta,
  666. &txq->tfds[q->write_ptr],
  667. DMA_BIDIRECTIONAL);
  668. idx = -ENOMEM;
  669. goto out;
  670. }
  671. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  672. cmd->len[i], 0);
  673. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  674. trace_bufs[trace_idx] = cmd->data[i];
  675. trace_lens[trace_idx] = cmd->len[i];
  676. trace_idx++;
  677. #endif
  678. }
  679. out_meta->flags = cmd->flags;
  680. txq->need_update = 1;
  681. /* check that tracing gets all possible blocks */
  682. BUILD_BUG_ON(IWL_MAX_CMD_TFDS + 1 != 3);
  683. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  684. trace_iwlwifi_dev_hcmd(trans->dev, cmd->flags,
  685. trace_bufs[0], trace_lens[0],
  686. trace_bufs[1], trace_lens[1],
  687. trace_bufs[2], trace_lens[2]);
  688. #endif
  689. /* Increment and update queue's write index */
  690. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  691. iwl_txq_update_write_ptr(trans, txq);
  692. out:
  693. spin_unlock_bh(&txq->lock);
  694. return idx;
  695. }
  696. /**
  697. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  698. *
  699. * When FW advances 'R' index, all entries between old and new 'R' index
  700. * need to be reclaimed. As result, some free space forms. If there is
  701. * enough free space (> low mark), wake the stack that feeds us.
  702. */
  703. static void iwl_hcmd_queue_reclaim(struct iwl_trans *trans, int txq_id,
  704. int idx)
  705. {
  706. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  707. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  708. struct iwl_queue *q = &txq->q;
  709. int nfreed = 0;
  710. lockdep_assert_held(&txq->lock);
  711. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  712. IWL_ERR(trans, "%s: Read index for DMA queue txq id (%d), "
  713. "index %d is out of range [0-%d] %d %d.\n", __func__,
  714. txq_id, idx, q->n_bd, q->write_ptr, q->read_ptr);
  715. return;
  716. }
  717. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  718. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  719. if (nfreed++ > 0) {
  720. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n", idx,
  721. q->write_ptr, q->read_ptr);
  722. iwl_op_mode_nic_error(trans->op_mode);
  723. }
  724. }
  725. }
  726. /**
  727. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  728. * @rxb: Rx buffer to reclaim
  729. * @handler_status: return value of the handler of the command
  730. * (put in setup_rx_handlers)
  731. *
  732. * If an Rx buffer has an async callback associated with it the callback
  733. * will be executed. The attached skb (if present) will only be freed
  734. * if the callback returns 1
  735. */
  736. void iwl_tx_cmd_complete(struct iwl_trans *trans, struct iwl_rx_cmd_buffer *rxb,
  737. int handler_status)
  738. {
  739. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  740. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  741. int txq_id = SEQ_TO_QUEUE(sequence);
  742. int index = SEQ_TO_INDEX(sequence);
  743. int cmd_index;
  744. struct iwl_device_cmd *cmd;
  745. struct iwl_cmd_meta *meta;
  746. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  747. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  748. /* If a Tx command is being handled and it isn't in the actual
  749. * command queue then there a command routing bug has been introduced
  750. * in the queue management code. */
  751. if (WARN(txq_id != trans->shrd->cmd_queue,
  752. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  753. txq_id, trans->shrd->cmd_queue, sequence,
  754. trans_pcie->txq[trans->shrd->cmd_queue].q.read_ptr,
  755. trans_pcie->txq[trans->shrd->cmd_queue].q.write_ptr)) {
  756. iwl_print_hex_error(trans, pkt, 32);
  757. return;
  758. }
  759. spin_lock(&txq->lock);
  760. cmd_index = get_cmd_index(&txq->q, index);
  761. cmd = txq->cmd[cmd_index];
  762. meta = &txq->meta[cmd_index];
  763. txq->time_stamp = jiffies;
  764. iwlagn_unmap_tfd(trans, meta, &txq->tfds[index],
  765. DMA_BIDIRECTIONAL);
  766. /* Input error checking is done when commands are added to queue. */
  767. if (meta->flags & CMD_WANT_SKB) {
  768. struct page *p = rxb_steal_page(rxb);
  769. meta->source->resp_pkt = pkt;
  770. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  771. meta->source->_rx_page_order = hw_params(trans).rx_page_order;
  772. meta->source->handler_status = handler_status;
  773. }
  774. iwl_hcmd_queue_reclaim(trans, txq_id, index);
  775. if (!(meta->flags & CMD_ASYNC)) {
  776. if (!test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status)) {
  777. IWL_WARN(trans,
  778. "HCMD_ACTIVE already clear for command %s\n",
  779. get_cmd_string(cmd->hdr.cmd));
  780. }
  781. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  782. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  783. get_cmd_string(cmd->hdr.cmd));
  784. wake_up(&trans->shrd->wait_command_queue);
  785. }
  786. meta->flags = 0;
  787. spin_unlock(&txq->lock);
  788. }
  789. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  790. static int iwl_send_cmd_async(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  791. {
  792. int ret;
  793. /* An asynchronous command can not expect an SKB to be set. */
  794. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  795. return -EINVAL;
  796. ret = iwl_enqueue_hcmd(trans, cmd);
  797. if (ret < 0) {
  798. IWL_ERR(trans,
  799. "Error sending %s: enqueue_hcmd failed: %d\n",
  800. get_cmd_string(cmd->id), ret);
  801. return ret;
  802. }
  803. return 0;
  804. }
  805. static int iwl_send_cmd_sync(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  806. {
  807. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  808. int cmd_idx;
  809. int ret;
  810. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  811. get_cmd_string(cmd->id));
  812. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  813. IWL_ERR(trans, "Command %s failed: FW Error\n",
  814. get_cmd_string(cmd->id));
  815. return -EIO;
  816. }
  817. if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
  818. &trans->shrd->status))) {
  819. IWL_ERR(trans, "Command %s: a command is already active!\n",
  820. get_cmd_string(cmd->id));
  821. return -EIO;
  822. }
  823. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  824. get_cmd_string(cmd->id));
  825. cmd_idx = iwl_enqueue_hcmd(trans, cmd);
  826. if (cmd_idx < 0) {
  827. ret = cmd_idx;
  828. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  829. IWL_ERR(trans,
  830. "Error sending %s: enqueue_hcmd failed: %d\n",
  831. get_cmd_string(cmd->id), ret);
  832. return ret;
  833. }
  834. ret = wait_event_timeout(trans->shrd->wait_command_queue,
  835. !test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status),
  836. HOST_COMPLETE_TIMEOUT);
  837. if (!ret) {
  838. if (test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status)) {
  839. struct iwl_tx_queue *txq =
  840. &trans_pcie->txq[trans->shrd->cmd_queue];
  841. struct iwl_queue *q = &txq->q;
  842. IWL_ERR(trans,
  843. "Error sending %s: time out after %dms.\n",
  844. get_cmd_string(cmd->id),
  845. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  846. IWL_ERR(trans,
  847. "Current CMD queue read_ptr %d write_ptr %d\n",
  848. q->read_ptr, q->write_ptr);
  849. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  850. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command"
  851. "%s\n", get_cmd_string(cmd->id));
  852. ret = -ETIMEDOUT;
  853. goto cancel;
  854. }
  855. }
  856. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  857. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  858. get_cmd_string(cmd->id));
  859. ret = -EIO;
  860. goto cancel;
  861. }
  862. return 0;
  863. cancel:
  864. if (cmd->flags & CMD_WANT_SKB) {
  865. /*
  866. * Cancel the CMD_WANT_SKB flag for the cmd in the
  867. * TX cmd queue. Otherwise in case the cmd comes
  868. * in later, it will possibly set an invalid
  869. * address (cmd->meta.source).
  870. */
  871. trans_pcie->txq[trans->shrd->cmd_queue].meta[cmd_idx].flags &=
  872. ~CMD_WANT_SKB;
  873. }
  874. if (cmd->resp_pkt) {
  875. iwl_free_resp(cmd);
  876. cmd->resp_pkt = NULL;
  877. }
  878. return ret;
  879. }
  880. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  881. {
  882. if (cmd->flags & CMD_ASYNC)
  883. return iwl_send_cmd_async(trans, cmd);
  884. return iwl_send_cmd_sync(trans, cmd);
  885. }
  886. /* Frees buffers until index _not_ inclusive */
  887. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  888. struct sk_buff_head *skbs)
  889. {
  890. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  891. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  892. struct iwl_queue *q = &txq->q;
  893. int last_to_free;
  894. int freed = 0;
  895. /* This function is not meant to release cmd queue*/
  896. if (WARN_ON(txq_id == trans->shrd->cmd_queue))
  897. return 0;
  898. lockdep_assert_held(&txq->lock);
  899. /*Since we free until index _not_ inclusive, the one before index is
  900. * the last we will free. This one must be used */
  901. last_to_free = iwl_queue_dec_wrap(index, q->n_bd);
  902. if ((index >= q->n_bd) ||
  903. (iwl_queue_used(q, last_to_free) == 0)) {
  904. IWL_ERR(trans, "%s: Read index for DMA queue txq id (%d), "
  905. "last_to_free %d is out of range [0-%d] %d %d.\n",
  906. __func__, txq_id, last_to_free, q->n_bd,
  907. q->write_ptr, q->read_ptr);
  908. return 0;
  909. }
  910. if (WARN_ON(!skb_queue_empty(skbs)))
  911. return 0;
  912. for (;
  913. q->read_ptr != index;
  914. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  915. if (WARN_ON_ONCE(txq->skbs[txq->q.read_ptr] == NULL))
  916. continue;
  917. __skb_queue_tail(skbs, txq->skbs[txq->q.read_ptr]);
  918. txq->skbs[txq->q.read_ptr] = NULL;
  919. iwlagn_txq_inval_byte_cnt_tbl(trans, txq);
  920. iwlagn_txq_free_tfd(trans, txq, txq->q.read_ptr, DMA_TO_DEVICE);
  921. freed++;
  922. }
  923. return freed;
  924. }