iwl-trans-tx-pcie.c 32 KB

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