iwl-trans-tx-pcie.c 30 KB

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