iwl-trans-pcie-tx.c 31 KB

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