iwl-trans-pcie-tx.c 32 KB

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