iwl-trans-tx-pcie.c 30 KB

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