iwl-trans-pcie-tx.c 32 KB

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