iwl-trans-tx-pcie.c 28 KB

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