iwl-trans-pcie-tx.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 (hw_params(trans).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->shrd->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", 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, int scd_retry)
  350. {
  351. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  352. int txq_id = txq->q.id;
  353. int active =
  354. test_bit(txq_id, &trans_pcie->txq_ctx_active_msk) ? 1 : 0;
  355. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  356. (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  357. (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
  358. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  359. SCD_QUEUE_STTS_REG_MSK);
  360. txq->sched_retry = scd_retry;
  361. if (active)
  362. IWL_DEBUG_TX_QUEUES(trans, "Activate %s Queue %d on FIFO %d\n",
  363. scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id);
  364. else
  365. IWL_DEBUG_TX_QUEUES(trans, "Deactivate %s Queue %d\n",
  366. scd_retry ? "BA" : "AC/CMD", txq_id);
  367. }
  368. static inline int get_fifo_from_tid(struct iwl_trans_pcie *trans_pcie,
  369. u8 ctx, u16 tid)
  370. {
  371. const u8 *ac_to_fifo = trans_pcie->ac_to_fifo[ctx];
  372. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  373. return ac_to_fifo[tid_to_ac[tid]];
  374. /* no support for TIDs 8-15 yet */
  375. return -EINVAL;
  376. }
  377. static inline bool is_agg_txqid_valid(struct iwl_trans *trans, int txq_id)
  378. {
  379. if (txq_id < IWLAGN_FIRST_AMPDU_QUEUE)
  380. return false;
  381. return txq_id < (IWLAGN_FIRST_AMPDU_QUEUE +
  382. hw_params(trans).num_ampdu_queues);
  383. }
  384. void iwl_trans_pcie_tx_agg_setup(struct iwl_trans *trans,
  385. enum iwl_rxon_context_id ctx, int sta_id,
  386. int tid, int frame_limit, u16 ssn)
  387. {
  388. int tx_fifo, txq_id;
  389. u16 ra_tid;
  390. unsigned long flags;
  391. struct iwl_trans_pcie *trans_pcie =
  392. IWL_TRANS_GET_PCIE_TRANS(trans);
  393. if (WARN_ON(sta_id == IWL_INVALID_STATION))
  394. return;
  395. if (WARN_ON(tid >= IWL_MAX_TID_COUNT))
  396. return;
  397. tx_fifo = get_fifo_from_tid(trans_pcie, ctx, tid);
  398. if (WARN_ON(tx_fifo < 0)) {
  399. IWL_ERR(trans, "txq_agg_setup, bad fifo: %d\n", tx_fifo);
  400. return;
  401. }
  402. txq_id = trans_pcie->agg_txq[sta_id][tid];
  403. if (WARN_ON_ONCE(is_agg_txqid_valid(trans, txq_id) == false)) {
  404. IWL_ERR(trans,
  405. "queue number out of range: %d, must be %d to %d\n",
  406. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  407. IWLAGN_FIRST_AMPDU_QUEUE +
  408. hw_params(trans).num_ampdu_queues - 1);
  409. return;
  410. }
  411. ra_tid = BUILD_RAxTID(sta_id, tid);
  412. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  413. /* Stop this Tx queue before configuring it */
  414. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  415. /* Map receiver-address / traffic-ID to this queue */
  416. iwlagn_tx_queue_set_q2ratid(trans, ra_tid, txq_id);
  417. /* Set this queue as a chain-building queue */
  418. iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, (1<<txq_id));
  419. /* enable aggregations for the queue */
  420. iwl_set_bits_prph(trans, SCD_AGGR_SEL, (1<<txq_id));
  421. /* Place first TFD at index corresponding to start sequence number.
  422. * Assumes that ssn_idx is valid (!= 0xFFF) */
  423. trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
  424. trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
  425. iwl_trans_set_wr_ptrs(trans, txq_id, ssn);
  426. /* Set up Tx window size and frame limit for this queue */
  427. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  428. SCD_CONTEXT_QUEUE_OFFSET(txq_id) +
  429. sizeof(u32),
  430. ((frame_limit <<
  431. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  432. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  433. ((frame_limit <<
  434. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  435. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  436. iwl_set_bits_prph(trans, SCD_INTERRUPT_MASK, (1 << txq_id));
  437. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  438. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[txq_id],
  439. tx_fifo, 1);
  440. trans_pcie->txq[txq_id].sta_id = sta_id;
  441. trans_pcie->txq[txq_id].tid = tid;
  442. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  443. }
  444. /*
  445. * Find first available (lowest unused) Tx Queue, mark it "active".
  446. * Called only when finding queue for aggregation.
  447. * Should never return anything < 7, because they should already
  448. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  449. */
  450. static int iwlagn_txq_ctx_activate_free(struct iwl_trans *trans)
  451. {
  452. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  453. int txq_id;
  454. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  455. if (!test_and_set_bit(txq_id,
  456. &trans_pcie->txq_ctx_active_msk))
  457. return txq_id;
  458. return -1;
  459. }
  460. int iwl_trans_pcie_tx_agg_alloc(struct iwl_trans *trans,
  461. int sta_id, int tid)
  462. {
  463. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  464. int txq_id;
  465. txq_id = iwlagn_txq_ctx_activate_free(trans);
  466. if (txq_id == -1) {
  467. IWL_ERR(trans, "No free aggregation queue available\n");
  468. return -ENXIO;
  469. }
  470. trans_pcie->agg_txq[sta_id][tid] = txq_id;
  471. iwl_set_swq_id(&trans_pcie->txq[txq_id], get_ac_from_tid(tid), txq_id);
  472. return 0;
  473. }
  474. int iwl_trans_pcie_tx_agg_disable(struct iwl_trans *trans, int sta_id, int tid)
  475. {
  476. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  477. u8 txq_id = trans_pcie->agg_txq[sta_id][tid];
  478. if (WARN_ON_ONCE(is_agg_txqid_valid(trans, txq_id) == false)) {
  479. IWL_ERR(trans,
  480. "queue number out of range: %d, must be %d to %d\n",
  481. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  482. IWLAGN_FIRST_AMPDU_QUEUE +
  483. hw_params(trans).num_ampdu_queues - 1);
  484. return -EINVAL;
  485. }
  486. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  487. iwl_clear_bits_prph(trans, SCD_AGGR_SEL, (1 << txq_id));
  488. trans_pcie->agg_txq[sta_id][tid] = 0;
  489. trans_pcie->txq[txq_id].q.read_ptr = 0;
  490. trans_pcie->txq[txq_id].q.write_ptr = 0;
  491. /* supposes that ssn_idx is valid (!= 0xFFF) */
  492. iwl_trans_set_wr_ptrs(trans, txq_id, 0);
  493. iwl_clear_bits_prph(trans, SCD_INTERRUPT_MASK, (1 << txq_id));
  494. iwl_txq_ctx_deactivate(trans_pcie, txq_id);
  495. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[txq_id], 0, 0);
  496. return 0;
  497. }
  498. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  499. /**
  500. * iwl_enqueue_hcmd - enqueue a uCode command
  501. * @priv: device private data point
  502. * @cmd: a point to the ucode command structure
  503. *
  504. * The function returns < 0 values to indicate the operation is
  505. * failed. On success, it turns the index (> 0) of command in the
  506. * command queue.
  507. */
  508. static int iwl_enqueue_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  509. {
  510. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  511. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  512. struct iwl_queue *q = &txq->q;
  513. struct iwl_device_cmd *out_cmd;
  514. struct iwl_cmd_meta *out_meta;
  515. dma_addr_t phys_addr;
  516. u32 idx;
  517. u16 copy_size, cmd_size;
  518. bool is_ct_kill = false;
  519. bool had_nocopy = false;
  520. int i;
  521. u8 *cmd_dest;
  522. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  523. const void *trace_bufs[IWL_MAX_CMD_TFDS + 1] = {};
  524. int trace_lens[IWL_MAX_CMD_TFDS + 1] = {};
  525. int trace_idx;
  526. #endif
  527. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  528. IWL_WARN(trans, "fw recovery, no hcmd send\n");
  529. return -EIO;
  530. }
  531. if ((trans->shrd->ucode_owner == IWL_OWNERSHIP_TM) &&
  532. !(cmd->flags & CMD_ON_DEMAND)) {
  533. IWL_DEBUG_HC(trans, "tm own the uCode, no regular hcmd send\n");
  534. return -EIO;
  535. }
  536. copy_size = sizeof(out_cmd->hdr);
  537. cmd_size = sizeof(out_cmd->hdr);
  538. /* need one for the header if the first is NOCOPY */
  539. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  540. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  541. if (!cmd->len[i])
  542. continue;
  543. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  544. had_nocopy = true;
  545. } else {
  546. /* NOCOPY must not be followed by normal! */
  547. if (WARN_ON(had_nocopy))
  548. return -EINVAL;
  549. copy_size += cmd->len[i];
  550. }
  551. cmd_size += cmd->len[i];
  552. }
  553. /*
  554. * If any of the command structures end up being larger than
  555. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  556. * allocated into separate TFDs, then we will need to
  557. * increase the size of the buffers.
  558. */
  559. if (WARN_ON(copy_size > TFD_MAX_PAYLOAD_SIZE))
  560. return -EINVAL;
  561. if (iwl_is_rfkill(trans->shrd) || iwl_is_ctkill(trans->shrd)) {
  562. IWL_WARN(trans, "Not sending command - %s KILL\n",
  563. iwl_is_rfkill(trans->shrd) ? "RF" : "CT");
  564. return -EIO;
  565. }
  566. spin_lock_bh(&txq->lock);
  567. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  568. spin_unlock_bh(&txq->lock);
  569. IWL_ERR(trans, "No space in command queue\n");
  570. is_ct_kill = iwl_check_for_ct_kill(priv(trans));
  571. if (!is_ct_kill) {
  572. IWL_ERR(trans, "Restarting adapter queue is full\n");
  573. iwl_op_mode_nic_error(trans->op_mode);
  574. }
  575. return -ENOSPC;
  576. }
  577. idx = get_cmd_index(q, q->write_ptr);
  578. out_cmd = txq->cmd[idx];
  579. out_meta = &txq->meta[idx];
  580. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  581. if (cmd->flags & CMD_WANT_SKB)
  582. out_meta->source = cmd;
  583. /* set up the header */
  584. out_cmd->hdr.cmd = cmd->id;
  585. out_cmd->hdr.flags = 0;
  586. out_cmd->hdr.sequence =
  587. cpu_to_le16(QUEUE_TO_SEQ(trans->shrd->cmd_queue) |
  588. INDEX_TO_SEQ(q->write_ptr));
  589. /* and copy the data that needs to be copied */
  590. cmd_dest = out_cmd->payload;
  591. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  592. if (!cmd->len[i])
  593. continue;
  594. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)
  595. break;
  596. memcpy(cmd_dest, cmd->data[i], cmd->len[i]);
  597. cmd_dest += cmd->len[i];
  598. }
  599. IWL_DEBUG_HC(trans, "Sending command %s (#%x), seq: 0x%04X, "
  600. "%d bytes at %d[%d]:%d\n",
  601. get_cmd_string(out_cmd->hdr.cmd),
  602. out_cmd->hdr.cmd,
  603. le16_to_cpu(out_cmd->hdr.sequence), cmd_size,
  604. q->write_ptr, idx, trans->shrd->cmd_queue);
  605. phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size,
  606. DMA_BIDIRECTIONAL);
  607. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  608. idx = -ENOMEM;
  609. goto out;
  610. }
  611. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  612. dma_unmap_len_set(out_meta, len, copy_size);
  613. iwlagn_txq_attach_buf_to_tfd(trans, txq,
  614. phys_addr, copy_size, 1);
  615. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  616. trace_bufs[0] = &out_cmd->hdr;
  617. trace_lens[0] = copy_size;
  618. trace_idx = 1;
  619. #endif
  620. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  621. if (!cmd->len[i])
  622. continue;
  623. if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY))
  624. continue;
  625. phys_addr = dma_map_single(trans->dev,
  626. (void *)cmd->data[i],
  627. cmd->len[i], DMA_BIDIRECTIONAL);
  628. if (dma_mapping_error(trans->dev, phys_addr)) {
  629. iwlagn_unmap_tfd(trans, out_meta,
  630. &txq->tfds[q->write_ptr],
  631. DMA_BIDIRECTIONAL);
  632. idx = -ENOMEM;
  633. goto out;
  634. }
  635. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  636. cmd->len[i], 0);
  637. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  638. trace_bufs[trace_idx] = cmd->data[i];
  639. trace_lens[trace_idx] = cmd->len[i];
  640. trace_idx++;
  641. #endif
  642. }
  643. out_meta->flags = cmd->flags;
  644. txq->need_update = 1;
  645. /* check that tracing gets all possible blocks */
  646. BUILD_BUG_ON(IWL_MAX_CMD_TFDS + 1 != 3);
  647. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  648. trace_iwlwifi_dev_hcmd(priv(trans), cmd->flags,
  649. trace_bufs[0], trace_lens[0],
  650. trace_bufs[1], trace_lens[1],
  651. trace_bufs[2], trace_lens[2]);
  652. #endif
  653. /* Increment and update queue's write index */
  654. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  655. iwl_txq_update_write_ptr(trans, txq);
  656. out:
  657. spin_unlock_bh(&txq->lock);
  658. return idx;
  659. }
  660. /**
  661. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  662. *
  663. * When FW advances 'R' index, all entries between old and new 'R' index
  664. * need to be reclaimed. As result, some free space forms. If there is
  665. * enough free space (> low mark), wake the stack that feeds us.
  666. */
  667. static void iwl_hcmd_queue_reclaim(struct iwl_trans *trans, int txq_id,
  668. int idx)
  669. {
  670. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  671. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  672. struct iwl_queue *q = &txq->q;
  673. int nfreed = 0;
  674. lockdep_assert_held(&txq->lock);
  675. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  676. IWL_ERR(trans, "%s: Read index for DMA queue txq id (%d), "
  677. "index %d is out of range [0-%d] %d %d.\n", __func__,
  678. txq_id, idx, q->n_bd, q->write_ptr, q->read_ptr);
  679. return;
  680. }
  681. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  682. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  683. if (nfreed++ > 0) {
  684. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n", idx,
  685. q->write_ptr, q->read_ptr);
  686. iwl_op_mode_nic_error(trans->op_mode);
  687. }
  688. }
  689. }
  690. /**
  691. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  692. * @rxb: Rx buffer to reclaim
  693. * @handler_status: return value of the handler of the command
  694. * (put in setup_rx_handlers)
  695. *
  696. * If an Rx buffer has an async callback associated with it the callback
  697. * will be executed. The attached skb (if present) will only be freed
  698. * if the callback returns 1
  699. */
  700. void iwl_tx_cmd_complete(struct iwl_trans *trans, struct iwl_rx_mem_buffer *rxb,
  701. int handler_status)
  702. {
  703. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  704. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  705. int txq_id = SEQ_TO_QUEUE(sequence);
  706. int index = SEQ_TO_INDEX(sequence);
  707. int cmd_index;
  708. struct iwl_device_cmd *cmd;
  709. struct iwl_cmd_meta *meta;
  710. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  711. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  712. /* If a Tx command is being handled and it isn't in the actual
  713. * command queue then there a command routing bug has been introduced
  714. * in the queue management code. */
  715. if (WARN(txq_id != trans->shrd->cmd_queue,
  716. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  717. txq_id, trans->shrd->cmd_queue, sequence,
  718. trans_pcie->txq[trans->shrd->cmd_queue].q.read_ptr,
  719. trans_pcie->txq[trans->shrd->cmd_queue].q.write_ptr)) {
  720. iwl_print_hex_error(trans, pkt, 32);
  721. return;
  722. }
  723. spin_lock(&txq->lock);
  724. cmd_index = get_cmd_index(&txq->q, index);
  725. cmd = txq->cmd[cmd_index];
  726. meta = &txq->meta[cmd_index];
  727. txq->time_stamp = jiffies;
  728. iwlagn_unmap_tfd(trans, meta, &txq->tfds[index],
  729. DMA_BIDIRECTIONAL);
  730. /* Input error checking is done when commands are added to queue. */
  731. if (meta->flags & CMD_WANT_SKB) {
  732. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  733. meta->source->handler_status = handler_status;
  734. rxb->page = NULL;
  735. }
  736. iwl_hcmd_queue_reclaim(trans, txq_id, index);
  737. if (!(meta->flags & CMD_ASYNC)) {
  738. if (!test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status)) {
  739. IWL_WARN(trans,
  740. "HCMD_ACTIVE already clear for command %s\n",
  741. get_cmd_string(cmd->hdr.cmd));
  742. }
  743. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  744. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  745. get_cmd_string(cmd->hdr.cmd));
  746. wake_up(&trans->shrd->wait_command_queue);
  747. }
  748. meta->flags = 0;
  749. spin_unlock(&txq->lock);
  750. }
  751. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  752. static int iwl_send_cmd_async(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  753. {
  754. int ret;
  755. /* An asynchronous command can not expect an SKB to be set. */
  756. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  757. return -EINVAL;
  758. if (test_bit(STATUS_EXIT_PENDING, &trans->shrd->status))
  759. return -EBUSY;
  760. ret = iwl_enqueue_hcmd(trans, cmd);
  761. if (ret < 0) {
  762. IWL_DEBUG_QUIET_RFKILL(trans,
  763. "Error sending %s: enqueue_hcmd failed: %d\n",
  764. get_cmd_string(cmd->id), ret);
  765. return ret;
  766. }
  767. return 0;
  768. }
  769. static int iwl_send_cmd_sync(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  770. {
  771. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  772. int cmd_idx;
  773. int ret;
  774. lockdep_assert_held(&trans->shrd->mutex);
  775. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  776. get_cmd_string(cmd->id));
  777. if (test_bit(STATUS_EXIT_PENDING, &trans->shrd->status))
  778. return -EBUSY;
  779. if (test_bit(STATUS_RF_KILL_HW, &trans->shrd->status)) {
  780. IWL_ERR(trans, "Command %s aborted: RF KILL Switch\n",
  781. get_cmd_string(cmd->id));
  782. return -ECANCELED;
  783. }
  784. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  785. IWL_ERR(trans, "Command %s failed: FW Error\n",
  786. get_cmd_string(cmd->id));
  787. return -EIO;
  788. }
  789. set_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  790. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  791. get_cmd_string(cmd->id));
  792. cmd_idx = iwl_enqueue_hcmd(trans, cmd);
  793. if (cmd_idx < 0) {
  794. ret = cmd_idx;
  795. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  796. IWL_DEBUG_QUIET_RFKILL(trans,
  797. "Error sending %s: enqueue_hcmd failed: %d\n",
  798. get_cmd_string(cmd->id), ret);
  799. return ret;
  800. }
  801. ret = wait_event_timeout(trans->shrd->wait_command_queue,
  802. !test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status),
  803. HOST_COMPLETE_TIMEOUT);
  804. if (!ret) {
  805. if (test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status)) {
  806. struct iwl_tx_queue *txq =
  807. &trans_pcie->txq[trans->shrd->cmd_queue];
  808. struct iwl_queue *q = &txq->q;
  809. IWL_DEBUG_QUIET_RFKILL(trans,
  810. "Error sending %s: time out after %dms.\n",
  811. get_cmd_string(cmd->id),
  812. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  813. IWL_DEBUG_QUIET_RFKILL(trans,
  814. "Current CMD queue read_ptr %d write_ptr %d\n",
  815. q->read_ptr, q->write_ptr);
  816. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  817. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command"
  818. "%s\n", get_cmd_string(cmd->id));
  819. ret = -ETIMEDOUT;
  820. goto cancel;
  821. }
  822. }
  823. if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
  824. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  825. get_cmd_string(cmd->id));
  826. ret = -EIO;
  827. goto cancel;
  828. }
  829. return 0;
  830. cancel:
  831. if (cmd->flags & CMD_WANT_SKB) {
  832. /*
  833. * Cancel the CMD_WANT_SKB flag for the cmd in the
  834. * TX cmd queue. Otherwise in case the cmd comes
  835. * in later, it will possibly set an invalid
  836. * address (cmd->meta.source).
  837. */
  838. trans_pcie->txq[trans->shrd->cmd_queue].meta[cmd_idx].flags &=
  839. ~CMD_WANT_SKB;
  840. }
  841. if (cmd->reply_page) {
  842. iwl_free_pages(trans->shrd, cmd->reply_page);
  843. cmd->reply_page = 0;
  844. }
  845. return ret;
  846. }
  847. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  848. {
  849. if (cmd->flags & CMD_ASYNC)
  850. return iwl_send_cmd_async(trans, cmd);
  851. return iwl_send_cmd_sync(trans, cmd);
  852. }
  853. /* Frees buffers until index _not_ inclusive */
  854. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  855. struct sk_buff_head *skbs)
  856. {
  857. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  858. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  859. struct iwl_queue *q = &txq->q;
  860. int last_to_free;
  861. int freed = 0;
  862. /* This function is not meant to release cmd queue*/
  863. if (WARN_ON(txq_id == trans->shrd->cmd_queue))
  864. return 0;
  865. lockdep_assert_held(&txq->lock);
  866. /*Since we free until index _not_ inclusive, the one before index is
  867. * the last we will free. This one must be used */
  868. last_to_free = iwl_queue_dec_wrap(index, q->n_bd);
  869. if ((index >= q->n_bd) ||
  870. (iwl_queue_used(q, last_to_free) == 0)) {
  871. IWL_ERR(trans, "%s: Read index for DMA queue txq id (%d), "
  872. "last_to_free %d is out of range [0-%d] %d %d.\n",
  873. __func__, txq_id, last_to_free, q->n_bd,
  874. q->write_ptr, q->read_ptr);
  875. return 0;
  876. }
  877. if (WARN_ON(!skb_queue_empty(skbs)))
  878. return 0;
  879. for (;
  880. q->read_ptr != index;
  881. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  882. if (WARN_ON_ONCE(txq->skbs[txq->q.read_ptr] == NULL))
  883. continue;
  884. __skb_queue_tail(skbs, txq->skbs[txq->q.read_ptr]);
  885. txq->skbs[txq->q.read_ptr] = NULL;
  886. iwlagn_txq_inval_byte_cnt_tbl(trans, txq);
  887. iwlagn_txq_free_tfd(trans, txq, txq->q.read_ptr, DMA_TO_DEVICE);
  888. freed++;
  889. }
  890. return freed;
  891. }