tx.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include "iwl-debug.h"
  33. #include "iwl-csr.h"
  34. #include "iwl-prph.h"
  35. #include "iwl-io.h"
  36. #include "iwl-op-mode.h"
  37. #include "internal.h"
  38. /* FIXME: need to abstract out TX command (once we know what it looks like) */
  39. #include "dvm/commands.h"
  40. #define IWL_TX_CRC_SIZE 4
  41. #define IWL_TX_DELIMITER_SIZE 4
  42. /**
  43. * iwl_trans_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  44. */
  45. void iwl_trans_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  46. struct iwl_tx_queue *txq,
  47. u16 byte_cnt)
  48. {
  49. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  50. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  51. int write_ptr = txq->q.write_ptr;
  52. int txq_id = txq->q.id;
  53. u8 sec_ctl = 0;
  54. u8 sta_id = 0;
  55. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  56. __le16 bc_ent;
  57. struct iwl_tx_cmd *tx_cmd =
  58. (void *) txq->entries[txq->q.write_ptr].cmd->payload;
  59. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  60. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  61. sta_id = tx_cmd->sta_id;
  62. sec_ctl = tx_cmd->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 (trans->cfg->base_params->shadow_reg_enable) {
  90. /* shadow register enabled */
  91. iwl_write32(trans, HBUS_TARG_WRPTR,
  92. txq->q.write_ptr | (txq_id << 8));
  93. } else {
  94. struct iwl_trans_pcie *trans_pcie =
  95. IWL_TRANS_GET_PCIE_TRANS(trans);
  96. /* if we're trying to save power */
  97. if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->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(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(trans, CSR_GP_CNTRL,
  107. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  108. return;
  109. }
  110. iwl_write_direct32(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(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 iwl_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(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(trans->dev, iwl_tfd_tb_get_addr(tfd, i),
  173. iwl_tfd_tb_get_len(tfd, i), dma_dir);
  174. tfd->num_tbs = 0;
  175. }
  176. /**
  177. * iwl_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  178. * @trans - transport private data
  179. * @txq - tx queue
  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 iwl_txq_free_tfd(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  186. enum dma_data_direction dma_dir)
  187. {
  188. struct iwl_tfd *tfd_tmp = txq->tfds;
  189. /* rd_ptr is bounded by n_bd and idx is bounded by n_window */
  190. int rd_ptr = txq->q.read_ptr;
  191. int idx = get_cmd_index(&txq->q, rd_ptr);
  192. lockdep_assert_held(&txq->lock);
  193. /* We have only q->n_window txq->entries, but we use q->n_bd tfds */
  194. iwl_unmap_tfd(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr],
  195. dma_dir);
  196. /* free SKB */
  197. if (txq->entries) {
  198. struct sk_buff *skb;
  199. skb = txq->entries[idx].skb;
  200. /* Can be called from irqs-disabled context
  201. * If skb is not NULL, it means that the whole queue is being
  202. * freed and that the queue is not empty - free the skb
  203. */
  204. if (skb) {
  205. iwl_op_mode_free_skb(trans->op_mode, skb);
  206. txq->entries[idx].skb = NULL;
  207. }
  208. }
  209. }
  210. int iwlagn_txq_attach_buf_to_tfd(struct iwl_trans *trans,
  211. struct iwl_tx_queue *txq,
  212. dma_addr_t addr, u16 len,
  213. u8 reset)
  214. {
  215. struct iwl_queue *q;
  216. struct iwl_tfd *tfd, *tfd_tmp;
  217. u32 num_tbs;
  218. q = &txq->q;
  219. tfd_tmp = txq->tfds;
  220. tfd = &tfd_tmp[q->write_ptr];
  221. if (reset)
  222. memset(tfd, 0, sizeof(*tfd));
  223. num_tbs = iwl_tfd_get_num_tbs(tfd);
  224. /* Each TFD can point to a maximum 20 Tx buffers */
  225. if (num_tbs >= IWL_NUM_OF_TBS) {
  226. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  227. IWL_NUM_OF_TBS);
  228. return -EINVAL;
  229. }
  230. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  231. return -EINVAL;
  232. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  233. IWL_ERR(trans, "Unaligned address = %llx\n",
  234. (unsigned long long)addr);
  235. iwl_tfd_set_tb(tfd, num_tbs, addr, len);
  236. return 0;
  237. }
  238. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  239. * DMA services
  240. *
  241. * Theory of operation
  242. *
  243. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  244. * of buffer descriptors, each of which points to one or more data buffers for
  245. * the device to read from or fill. Driver and device exchange status of each
  246. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  247. * entries in each circular buffer, to protect against confusing empty and full
  248. * queue states.
  249. *
  250. * The device reads or writes the data in the queues via the device's several
  251. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  252. *
  253. * For Tx queue, there are low mark and high mark limits. If, after queuing
  254. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  255. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  256. * Tx queue resumed.
  257. *
  258. ***************************************************/
  259. int iwl_queue_space(const struct iwl_queue *q)
  260. {
  261. int s = q->read_ptr - q->write_ptr;
  262. if (q->read_ptr > q->write_ptr)
  263. s -= q->n_bd;
  264. if (s <= 0)
  265. s += q->n_window;
  266. /* keep some reserve to not confuse empty and full situations */
  267. s -= 2;
  268. if (s < 0)
  269. s = 0;
  270. return s;
  271. }
  272. /**
  273. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  274. */
  275. int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
  276. {
  277. q->n_bd = count;
  278. q->n_window = slots_num;
  279. q->id = id;
  280. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  281. * and iwl_queue_dec_wrap are broken. */
  282. if (WARN_ON(!is_power_of_2(count)))
  283. return -EINVAL;
  284. /* slots_num must be power-of-two size, otherwise
  285. * get_cmd_index is broken. */
  286. if (WARN_ON(!is_power_of_2(slots_num)))
  287. return -EINVAL;
  288. q->low_mark = q->n_window / 4;
  289. if (q->low_mark < 4)
  290. q->low_mark = 4;
  291. q->high_mark = q->n_window / 8;
  292. if (q->high_mark < 2)
  293. q->high_mark = 2;
  294. q->write_ptr = q->read_ptr = 0;
  295. return 0;
  296. }
  297. static void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  298. struct iwl_tx_queue *txq)
  299. {
  300. struct iwl_trans_pcie *trans_pcie =
  301. IWL_TRANS_GET_PCIE_TRANS(trans);
  302. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  303. int txq_id = txq->q.id;
  304. int read_ptr = txq->q.read_ptr;
  305. u8 sta_id = 0;
  306. __le16 bc_ent;
  307. struct iwl_tx_cmd *tx_cmd =
  308. (void *)txq->entries[txq->q.read_ptr].cmd->payload;
  309. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  310. if (txq_id != trans_pcie->cmd_queue)
  311. sta_id = tx_cmd->sta_id;
  312. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  313. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  314. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  315. scd_bc_tbl[txq_id].
  316. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  317. }
  318. static int iwl_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  319. u16 txq_id)
  320. {
  321. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  322. u32 tbl_dw_addr;
  323. u32 tbl_dw;
  324. u16 scd_q2ratid;
  325. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  326. tbl_dw_addr = trans_pcie->scd_base_addr +
  327. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  328. tbl_dw = iwl_read_targ_mem(trans, tbl_dw_addr);
  329. if (txq_id & 0x1)
  330. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  331. else
  332. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  333. iwl_write_targ_mem(trans, tbl_dw_addr, tbl_dw);
  334. return 0;
  335. }
  336. static inline void iwl_txq_set_inactive(struct iwl_trans *trans, u16 txq_id)
  337. {
  338. /* Simply stop the queue, but don't change any configuration;
  339. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  340. iwl_write_prph(trans,
  341. SCD_QUEUE_STATUS_BITS(txq_id),
  342. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  343. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  344. }
  345. void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
  346. int sta_id, int tid, int frame_limit, u16 ssn)
  347. {
  348. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  349. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  350. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  351. /* Stop this Tx queue before configuring it */
  352. iwl_txq_set_inactive(trans, txq_id);
  353. /* Set this queue as a chain-building queue unless it is CMD queue */
  354. if (txq_id != trans_pcie->cmd_queue)
  355. iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
  356. /* If this queue is mapped to a certain station: it is an AGG queue */
  357. if (sta_id != IWL_INVALID_STATION) {
  358. u16 ra_tid = BUILD_RAxTID(sta_id, tid);
  359. /* Map receiver-address / traffic-ID to this queue */
  360. iwl_txq_set_ratid_map(trans, ra_tid, txq_id);
  361. /* enable aggregations for the queue */
  362. iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  363. } else {
  364. /*
  365. * disable aggregations for the queue, this will also make the
  366. * ra_tid mapping configuration irrelevant since it is now a
  367. * non-AGG queue.
  368. */
  369. iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  370. }
  371. /* Place first TFD at index corresponding to start sequence number.
  372. * Assumes that ssn_idx is valid (!= 0xFFF) */
  373. trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
  374. trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
  375. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  376. (ssn & 0xff) | (txq_id << 8));
  377. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  378. /* Set up Tx window size and frame limit for this queue */
  379. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  380. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  381. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  382. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  383. ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  384. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  385. ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  386. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  387. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  388. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  389. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  390. (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  391. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  392. SCD_QUEUE_STTS_REG_MSK);
  393. IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
  394. txq_id, fifo, ssn & 0xff);
  395. }
  396. void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
  397. {
  398. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  399. u32 stts_addr = trans_pcie->scd_base_addr +
  400. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  401. static const u32 zero_val[4] = {};
  402. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  403. WARN_ONCE(1, "queue %d not used", txq_id);
  404. return;
  405. }
  406. iwl_txq_set_inactive(trans, txq_id);
  407. _iwl_write_targ_mem_dwords(trans, stts_addr,
  408. zero_val, ARRAY_SIZE(zero_val));
  409. iwl_tx_queue_unmap(trans, txq_id);
  410. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  411. }
  412. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  413. /**
  414. * iwl_enqueue_hcmd - enqueue a uCode command
  415. * @priv: device private data point
  416. * @cmd: a point to the ucode command structure
  417. *
  418. * The function returns < 0 values to indicate the operation is
  419. * failed. On success, it turns the index (> 0) of command in the
  420. * command queue.
  421. */
  422. static int iwl_enqueue_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  423. {
  424. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  425. struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  426. struct iwl_queue *q = &txq->q;
  427. struct iwl_device_cmd *out_cmd;
  428. struct iwl_cmd_meta *out_meta;
  429. void *dup_buf = NULL;
  430. dma_addr_t phys_addr;
  431. int idx;
  432. u16 copy_size, cmd_size;
  433. bool had_nocopy = false;
  434. int i;
  435. u32 cmd_pos;
  436. copy_size = sizeof(out_cmd->hdr);
  437. cmd_size = sizeof(out_cmd->hdr);
  438. /* need one for the header if the first is NOCOPY */
  439. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  440. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  441. if (!cmd->len[i])
  442. continue;
  443. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  444. had_nocopy = true;
  445. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  446. idx = -EINVAL;
  447. goto free_dup_buf;
  448. }
  449. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  450. /*
  451. * This is also a chunk that isn't copied
  452. * to the static buffer so set had_nocopy.
  453. */
  454. had_nocopy = true;
  455. /* only allowed once */
  456. if (WARN_ON(dup_buf)) {
  457. idx = -EINVAL;
  458. goto free_dup_buf;
  459. }
  460. dup_buf = kmemdup(cmd->data[i], cmd->len[i],
  461. GFP_ATOMIC);
  462. if (!dup_buf)
  463. return -ENOMEM;
  464. } else {
  465. /* NOCOPY must not be followed by normal! */
  466. if (WARN_ON(had_nocopy)) {
  467. idx = -EINVAL;
  468. goto free_dup_buf;
  469. }
  470. copy_size += cmd->len[i];
  471. }
  472. cmd_size += cmd->len[i];
  473. }
  474. /*
  475. * If any of the command structures end up being larger than
  476. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  477. * allocated into separate TFDs, then we will need to
  478. * increase the size of the buffers.
  479. */
  480. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  481. "Command %s (%#x) is too large (%d bytes)\n",
  482. trans_pcie_get_cmd_string(trans_pcie, cmd->id),
  483. cmd->id, copy_size)) {
  484. idx = -EINVAL;
  485. goto free_dup_buf;
  486. }
  487. spin_lock_bh(&txq->lock);
  488. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  489. spin_unlock_bh(&txq->lock);
  490. IWL_ERR(trans, "No space in command queue\n");
  491. iwl_op_mode_cmd_queue_full(trans->op_mode);
  492. idx = -ENOSPC;
  493. goto free_dup_buf;
  494. }
  495. idx = get_cmd_index(q, q->write_ptr);
  496. out_cmd = txq->entries[idx].cmd;
  497. out_meta = &txq->entries[idx].meta;
  498. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  499. if (cmd->flags & CMD_WANT_SKB)
  500. out_meta->source = cmd;
  501. /* set up the header */
  502. out_cmd->hdr.cmd = cmd->id;
  503. out_cmd->hdr.flags = 0;
  504. out_cmd->hdr.sequence =
  505. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  506. INDEX_TO_SEQ(q->write_ptr));
  507. /* and copy the data that needs to be copied */
  508. cmd_pos = offsetof(struct iwl_device_cmd, payload);
  509. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  510. if (!cmd->len[i])
  511. continue;
  512. if (cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  513. IWL_HCMD_DFL_DUP))
  514. break;
  515. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], cmd->len[i]);
  516. cmd_pos += cmd->len[i];
  517. }
  518. WARN_ON_ONCE(txq->entries[idx].copy_cmd);
  519. /*
  520. * since out_cmd will be the source address of the FH, it will write
  521. * the retry count there. So when the user needs to receivce the HCMD
  522. * that corresponds to the response in the response handler, it needs
  523. * to set CMD_WANT_HCMD.
  524. */
  525. if (cmd->flags & CMD_WANT_HCMD) {
  526. txq->entries[idx].copy_cmd =
  527. kmemdup(out_cmd, cmd_pos, GFP_ATOMIC);
  528. if (unlikely(!txq->entries[idx].copy_cmd)) {
  529. idx = -ENOMEM;
  530. goto out;
  531. }
  532. }
  533. IWL_DEBUG_HC(trans,
  534. "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  535. trans_pcie_get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
  536. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  537. cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
  538. phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size,
  539. DMA_BIDIRECTIONAL);
  540. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  541. idx = -ENOMEM;
  542. goto out;
  543. }
  544. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  545. dma_unmap_len_set(out_meta, len, copy_size);
  546. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr, copy_size, 1);
  547. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  548. const void *data = cmd->data[i];
  549. if (!cmd->len[i])
  550. continue;
  551. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  552. IWL_HCMD_DFL_DUP)))
  553. continue;
  554. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  555. data = dup_buf;
  556. phys_addr = dma_map_single(trans->dev, (void *)data,
  557. cmd->len[i], DMA_BIDIRECTIONAL);
  558. if (dma_mapping_error(trans->dev, phys_addr)) {
  559. iwl_unmap_tfd(trans, out_meta,
  560. &txq->tfds[q->write_ptr],
  561. DMA_BIDIRECTIONAL);
  562. idx = -ENOMEM;
  563. goto out;
  564. }
  565. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  566. cmd->len[i], 0);
  567. }
  568. out_meta->flags = cmd->flags;
  569. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  570. kfree(txq->entries[idx].free_buf);
  571. txq->entries[idx].free_buf = dup_buf;
  572. txq->need_update = 1;
  573. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size,
  574. &out_cmd->hdr, copy_size);
  575. /* start timer if queue currently empty */
  576. if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
  577. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  578. /* Increment and update queue's write index */
  579. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  580. iwl_txq_update_write_ptr(trans, txq);
  581. out:
  582. spin_unlock_bh(&txq->lock);
  583. free_dup_buf:
  584. if (idx < 0)
  585. kfree(dup_buf);
  586. return idx;
  587. }
  588. static inline void iwl_queue_progress(struct iwl_trans_pcie *trans_pcie,
  589. struct iwl_tx_queue *txq)
  590. {
  591. if (!trans_pcie->wd_timeout)
  592. return;
  593. /*
  594. * if empty delete timer, otherwise move timer forward
  595. * since we're making progress on this queue
  596. */
  597. if (txq->q.read_ptr == txq->q.write_ptr)
  598. del_timer(&txq->stuck_timer);
  599. else
  600. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  601. }
  602. /**
  603. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  604. *
  605. * When FW advances 'R' index, all entries between old and new 'R' index
  606. * need to be reclaimed. As result, some free space forms. If there is
  607. * enough free space (> low mark), wake the stack that feeds us.
  608. */
  609. static void iwl_hcmd_queue_reclaim(struct iwl_trans *trans, int txq_id,
  610. int idx)
  611. {
  612. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  613. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  614. struct iwl_queue *q = &txq->q;
  615. int nfreed = 0;
  616. lockdep_assert_held(&txq->lock);
  617. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  618. IWL_ERR(trans,
  619. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  620. __func__, txq_id, idx, q->n_bd,
  621. q->write_ptr, q->read_ptr);
  622. return;
  623. }
  624. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  625. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  626. if (nfreed++ > 0) {
  627. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  628. idx, q->write_ptr, q->read_ptr);
  629. iwl_op_mode_nic_error(trans->op_mode);
  630. }
  631. }
  632. iwl_queue_progress(trans_pcie, txq);
  633. }
  634. /**
  635. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  636. * @rxb: Rx buffer to reclaim
  637. * @handler_status: return value of the handler of the command
  638. * (put in setup_rx_handlers)
  639. *
  640. * If an Rx buffer has an async callback associated with it the callback
  641. * will be executed. The attached skb (if present) will only be freed
  642. * if the callback returns 1
  643. */
  644. void iwl_tx_cmd_complete(struct iwl_trans *trans, struct iwl_rx_cmd_buffer *rxb,
  645. int handler_status)
  646. {
  647. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  648. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  649. int txq_id = SEQ_TO_QUEUE(sequence);
  650. int index = SEQ_TO_INDEX(sequence);
  651. int cmd_index;
  652. struct iwl_device_cmd *cmd;
  653. struct iwl_cmd_meta *meta;
  654. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  655. struct iwl_tx_queue *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  656. /* If a Tx command is being handled and it isn't in the actual
  657. * command queue then there a command routing bug has been introduced
  658. * in the queue management code. */
  659. if (WARN(txq_id != trans_pcie->cmd_queue,
  660. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  661. txq_id, trans_pcie->cmd_queue, sequence,
  662. trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
  663. trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
  664. iwl_print_hex_error(trans, pkt, 32);
  665. return;
  666. }
  667. spin_lock(&txq->lock);
  668. cmd_index = get_cmd_index(&txq->q, index);
  669. cmd = txq->entries[cmd_index].cmd;
  670. meta = &txq->entries[cmd_index].meta;
  671. iwl_unmap_tfd(trans, meta, &txq->tfds[index], DMA_BIDIRECTIONAL);
  672. /* Input error checking is done when commands are added to queue. */
  673. if (meta->flags & CMD_WANT_SKB) {
  674. struct page *p = rxb_steal_page(rxb);
  675. meta->source->resp_pkt = pkt;
  676. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  677. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  678. meta->source->handler_status = handler_status;
  679. }
  680. iwl_hcmd_queue_reclaim(trans, txq_id, index);
  681. if (!(meta->flags & CMD_ASYNC)) {
  682. if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  683. IWL_WARN(trans,
  684. "HCMD_ACTIVE already clear for command %s\n",
  685. trans_pcie_get_cmd_string(trans_pcie,
  686. cmd->hdr.cmd));
  687. }
  688. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  689. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  690. trans_pcie_get_cmd_string(trans_pcie,
  691. cmd->hdr.cmd));
  692. wake_up(&trans_pcie->wait_command_queue);
  693. }
  694. meta->flags = 0;
  695. spin_unlock(&txq->lock);
  696. }
  697. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  698. static int iwl_send_cmd_async(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  699. {
  700. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  701. int ret;
  702. /* An asynchronous command can not expect an SKB to be set. */
  703. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  704. return -EINVAL;
  705. ret = iwl_enqueue_hcmd(trans, cmd);
  706. if (ret < 0) {
  707. IWL_ERR(trans,
  708. "Error sending %s: enqueue_hcmd failed: %d\n",
  709. trans_pcie_get_cmd_string(trans_pcie, cmd->id), ret);
  710. return ret;
  711. }
  712. return 0;
  713. }
  714. static int iwl_send_cmd_sync(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  715. {
  716. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  717. int cmd_idx;
  718. int ret;
  719. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  720. trans_pcie_get_cmd_string(trans_pcie, cmd->id));
  721. if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
  722. &trans_pcie->status))) {
  723. IWL_ERR(trans, "Command %s: a command is already active!\n",
  724. trans_pcie_get_cmd_string(trans_pcie, cmd->id));
  725. return -EIO;
  726. }
  727. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  728. trans_pcie_get_cmd_string(trans_pcie, cmd->id));
  729. cmd_idx = iwl_enqueue_hcmd(trans, cmd);
  730. if (cmd_idx < 0) {
  731. ret = cmd_idx;
  732. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  733. IWL_ERR(trans,
  734. "Error sending %s: enqueue_hcmd failed: %d\n",
  735. trans_pcie_get_cmd_string(trans_pcie, cmd->id), ret);
  736. return ret;
  737. }
  738. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  739. !test_bit(STATUS_HCMD_ACTIVE,
  740. &trans_pcie->status),
  741. HOST_COMPLETE_TIMEOUT);
  742. if (!ret) {
  743. if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  744. struct iwl_tx_queue *txq =
  745. &trans_pcie->txq[trans_pcie->cmd_queue];
  746. struct iwl_queue *q = &txq->q;
  747. IWL_ERR(trans,
  748. "Error sending %s: time out after %dms.\n",
  749. trans_pcie_get_cmd_string(trans_pcie, cmd->id),
  750. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  751. IWL_ERR(trans,
  752. "Current CMD queue read_ptr %d write_ptr %d\n",
  753. q->read_ptr, q->write_ptr);
  754. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  755. IWL_DEBUG_INFO(trans,
  756. "Clearing HCMD_ACTIVE for command %s\n",
  757. trans_pcie_get_cmd_string(trans_pcie,
  758. cmd->id));
  759. ret = -ETIMEDOUT;
  760. goto cancel;
  761. }
  762. }
  763. if (test_bit(STATUS_RFKILL, &trans_pcie->status)) {
  764. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  765. ret = -ERFKILL;
  766. goto cancel;
  767. }
  768. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  769. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  770. trans_pcie_get_cmd_string(trans_pcie, cmd->id));
  771. ret = -EIO;
  772. goto cancel;
  773. }
  774. return 0;
  775. cancel:
  776. if (cmd->flags & CMD_WANT_SKB) {
  777. /*
  778. * Cancel the CMD_WANT_SKB flag for the cmd in the
  779. * TX cmd queue. Otherwise in case the cmd comes
  780. * in later, it will possibly set an invalid
  781. * address (cmd->meta.source).
  782. */
  783. trans_pcie->txq[trans_pcie->cmd_queue].
  784. entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  785. }
  786. if (cmd->resp_pkt) {
  787. iwl_free_resp(cmd);
  788. cmd->resp_pkt = NULL;
  789. }
  790. return ret;
  791. }
  792. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  793. {
  794. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  795. if (test_bit(STATUS_RFKILL, &trans_pcie->status))
  796. return -ERFKILL;
  797. if (cmd->flags & CMD_ASYNC)
  798. return iwl_send_cmd_async(trans, cmd);
  799. /* We still can fail on RFKILL that can be asserted while we wait */
  800. return iwl_send_cmd_sync(trans, cmd);
  801. }
  802. /* Frees buffers until index _not_ inclusive */
  803. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  804. struct sk_buff_head *skbs)
  805. {
  806. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  807. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  808. struct iwl_queue *q = &txq->q;
  809. int last_to_free;
  810. int freed = 0;
  811. /* This function is not meant to release cmd queue*/
  812. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  813. return 0;
  814. lockdep_assert_held(&txq->lock);
  815. /*Since we free until index _not_ inclusive, the one before index is
  816. * the last we will free. This one must be used */
  817. last_to_free = iwl_queue_dec_wrap(index, q->n_bd);
  818. if ((index >= q->n_bd) ||
  819. (iwl_queue_used(q, last_to_free) == 0)) {
  820. IWL_ERR(trans,
  821. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  822. __func__, txq_id, last_to_free, q->n_bd,
  823. q->write_ptr, q->read_ptr);
  824. return 0;
  825. }
  826. if (WARN_ON(!skb_queue_empty(skbs)))
  827. return 0;
  828. for (;
  829. q->read_ptr != index;
  830. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  831. if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
  832. continue;
  833. __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
  834. txq->entries[txq->q.read_ptr].skb = NULL;
  835. iwlagn_txq_inval_byte_cnt_tbl(trans, txq);
  836. iwl_txq_free_tfd(trans, txq, DMA_TO_DEVICE);
  837. freed++;
  838. }
  839. iwl_queue_progress(trans_pcie, txq);
  840. return freed;
  841. }