iwl-tx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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/sched.h>
  31. #include <linux/slab.h>
  32. #include <net/mac80211.h>
  33. #include "iwl-eeprom.h"
  34. #include "iwl-agn.h"
  35. #include "iwl-dev.h"
  36. #include "iwl-core.h"
  37. #include "iwl-sta.h"
  38. #include "iwl-io.h"
  39. #include "iwl-helpers.h"
  40. /**
  41. * iwl_txq_update_write_ptr - Send new write index to hardware
  42. */
  43. void iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  44. {
  45. u32 reg = 0;
  46. int txq_id = txq->q.id;
  47. if (txq->need_update == 0)
  48. return;
  49. if (priv->cfg->base_params->shadow_reg_enable) {
  50. /* shadow register enabled */
  51. iwl_write32(priv, HBUS_TARG_WRPTR,
  52. txq->q.write_ptr | (txq_id << 8));
  53. } else {
  54. /* if we're trying to save power */
  55. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  56. /* wake up nic if it's powered down ...
  57. * uCode will wake up, and interrupt us again, so next
  58. * time we'll skip this part. */
  59. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  60. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  61. IWL_DEBUG_INFO(priv,
  62. "Tx queue %d requesting wakeup,"
  63. " GP1 = 0x%x\n", txq_id, reg);
  64. iwl_set_bit(priv, CSR_GP_CNTRL,
  65. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  66. return;
  67. }
  68. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  69. txq->q.write_ptr | (txq_id << 8));
  70. /*
  71. * else not in power-save mode,
  72. * uCode will never sleep when we're
  73. * trying to tx (during RFKILL, we're not trying to tx).
  74. */
  75. } else
  76. iwl_write32(priv, HBUS_TARG_WRPTR,
  77. txq->q.write_ptr | (txq_id << 8));
  78. }
  79. txq->need_update = 0;
  80. }
  81. static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  82. {
  83. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  84. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  85. if (sizeof(dma_addr_t) > sizeof(u32))
  86. addr |=
  87. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  88. return addr;
  89. }
  90. static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
  91. {
  92. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  93. return le16_to_cpu(tb->hi_n_len) >> 4;
  94. }
  95. static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  96. dma_addr_t addr, u16 len)
  97. {
  98. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  99. u16 hi_n_len = len << 4;
  100. put_unaligned_le32(addr, &tb->lo);
  101. if (sizeof(dma_addr_t) > sizeof(u32))
  102. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  103. tb->hi_n_len = cpu_to_le16(hi_n_len);
  104. tfd->num_tbs = idx + 1;
  105. }
  106. static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
  107. {
  108. return tfd->num_tbs & 0x1f;
  109. }
  110. static void iwlagn_unmap_tfd(struct iwl_priv *priv, struct iwl_cmd_meta *meta,
  111. struct iwl_tfd *tfd, enum dma_data_direction dma_dir)
  112. {
  113. int i;
  114. int num_tbs;
  115. /* Sanity check on number of chunks */
  116. num_tbs = iwl_tfd_get_num_tbs(tfd);
  117. if (num_tbs >= IWL_NUM_OF_TBS) {
  118. IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
  119. /* @todo issue fatal error, it is quite serious situation */
  120. return;
  121. }
  122. /* Unmap tx_cmd */
  123. if (num_tbs)
  124. dma_unmap_single(priv->bus.dev,
  125. dma_unmap_addr(meta, mapping),
  126. dma_unmap_len(meta, len),
  127. DMA_BIDIRECTIONAL);
  128. /* Unmap chunks, if any. */
  129. for (i = 1; i < num_tbs; i++)
  130. dma_unmap_single(priv->bus.dev, iwl_tfd_tb_get_addr(tfd, i),
  131. iwl_tfd_tb_get_len(tfd, i), dma_dir);
  132. }
  133. /**
  134. * iwlagn_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  135. * @priv - driver private data
  136. * @txq - tx queue
  137. *
  138. * Does NOT advance any TFD circular buffer read/write indexes
  139. * Does NOT free the TFD itself (which is within circular buffer)
  140. */
  141. void iwlagn_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  142. {
  143. struct iwl_tfd *tfd_tmp = txq->tfds;
  144. int index = txq->q.read_ptr;
  145. iwlagn_unmap_tfd(priv, &txq->meta[index], &tfd_tmp[index],
  146. DMA_TO_DEVICE);
  147. /* free SKB */
  148. if (txq->txb) {
  149. struct sk_buff *skb;
  150. skb = txq->txb[txq->q.read_ptr].skb;
  151. /* can be called from irqs-disabled context */
  152. if (skb) {
  153. dev_kfree_skb_any(skb);
  154. txq->txb[txq->q.read_ptr].skb = NULL;
  155. }
  156. }
  157. }
  158. int iwlagn_txq_attach_buf_to_tfd(struct iwl_priv *priv,
  159. struct iwl_tx_queue *txq,
  160. dma_addr_t addr, u16 len,
  161. u8 reset)
  162. {
  163. struct iwl_queue *q;
  164. struct iwl_tfd *tfd, *tfd_tmp;
  165. u32 num_tbs;
  166. q = &txq->q;
  167. tfd_tmp = txq->tfds;
  168. tfd = &tfd_tmp[q->write_ptr];
  169. if (reset)
  170. memset(tfd, 0, sizeof(*tfd));
  171. num_tbs = iwl_tfd_get_num_tbs(tfd);
  172. /* Each TFD can point to a maximum 20 Tx buffers */
  173. if (num_tbs >= IWL_NUM_OF_TBS) {
  174. IWL_ERR(priv, "Error can not send more than %d chunks\n",
  175. IWL_NUM_OF_TBS);
  176. return -EINVAL;
  177. }
  178. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  179. return -EINVAL;
  180. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  181. IWL_ERR(priv, "Unaligned address = %llx\n",
  182. (unsigned long long)addr);
  183. iwl_tfd_set_tb(tfd, num_tbs, addr, len);
  184. return 0;
  185. }
  186. /**
  187. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  188. */
  189. void iwl_tx_queue_unmap(struct iwl_priv *priv, int txq_id)
  190. {
  191. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  192. struct iwl_queue *q = &txq->q;
  193. if (q->n_bd == 0)
  194. return;
  195. while (q->write_ptr != q->read_ptr) {
  196. iwlagn_txq_free_tfd(priv, txq);
  197. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  198. }
  199. }
  200. /**
  201. * iwl_tx_queue_free - Deallocate DMA queue.
  202. * @txq: Transmit queue to deallocate.
  203. *
  204. * Empty queue by removing and destroying all BD's.
  205. * Free all buffers.
  206. * 0-fill, but do not free "txq" descriptor structure.
  207. */
  208. void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  209. {
  210. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  211. struct device *dev = priv->bus.dev;
  212. int i;
  213. iwl_tx_queue_unmap(priv, txq_id);
  214. /* De-alloc array of command/tx buffers */
  215. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  216. kfree(txq->cmd[i]);
  217. /* De-alloc circular buffer of TFDs */
  218. if (txq->q.n_bd)
  219. dma_free_coherent(dev, priv->hw_params.tfd_size *
  220. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  221. /* De-alloc array of per-TFD driver data */
  222. kfree(txq->txb);
  223. txq->txb = NULL;
  224. /* deallocate arrays */
  225. kfree(txq->cmd);
  226. kfree(txq->meta);
  227. txq->cmd = NULL;
  228. txq->meta = NULL;
  229. /* 0-fill queue descriptor structure */
  230. memset(txq, 0, sizeof(*txq));
  231. }
  232. /**
  233. * iwl_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
  234. */
  235. void iwl_cmd_queue_unmap(struct iwl_priv *priv)
  236. {
  237. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  238. struct iwl_queue *q = &txq->q;
  239. int i;
  240. if (q->n_bd == 0)
  241. return;
  242. while (q->read_ptr != q->write_ptr) {
  243. i = get_cmd_index(q, q->read_ptr);
  244. if (txq->meta[i].flags & CMD_MAPPED) {
  245. iwlagn_unmap_tfd(priv, &txq->meta[i], &txq->tfds[i],
  246. DMA_BIDIRECTIONAL);
  247. txq->meta[i].flags = 0;
  248. }
  249. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  250. }
  251. }
  252. /**
  253. * iwl_cmd_queue_free - Deallocate DMA queue.
  254. * @txq: Transmit queue to deallocate.
  255. *
  256. * Empty queue by removing and destroying all BD's.
  257. * Free all buffers.
  258. * 0-fill, but do not free "txq" descriptor structure.
  259. */
  260. void iwl_cmd_queue_free(struct iwl_priv *priv)
  261. {
  262. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  263. struct device *dev = priv->bus.dev;
  264. int i;
  265. iwl_cmd_queue_unmap(priv);
  266. /* De-alloc array of command/tx buffers */
  267. for (i = 0; i < TFD_CMD_SLOTS; i++)
  268. kfree(txq->cmd[i]);
  269. /* De-alloc circular buffer of TFDs */
  270. if (txq->q.n_bd)
  271. dma_free_coherent(dev, priv->hw_params.tfd_size * txq->q.n_bd,
  272. txq->tfds, txq->q.dma_addr);
  273. /* deallocate arrays */
  274. kfree(txq->cmd);
  275. kfree(txq->meta);
  276. txq->cmd = NULL;
  277. txq->meta = NULL;
  278. /* 0-fill queue descriptor structure */
  279. memset(txq, 0, sizeof(*txq));
  280. }
  281. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  282. * DMA services
  283. *
  284. * Theory of operation
  285. *
  286. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  287. * of buffer descriptors, each of which points to one or more data buffers for
  288. * the device to read from or fill. Driver and device exchange status of each
  289. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  290. * entries in each circular buffer, to protect against confusing empty and full
  291. * queue states.
  292. *
  293. * The device reads or writes the data in the queues via the device's several
  294. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  295. *
  296. * For Tx queue, there are low mark and high mark limits. If, after queuing
  297. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  298. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  299. * Tx queue resumed.
  300. *
  301. ***************************************************/
  302. int iwl_queue_space(const struct iwl_queue *q)
  303. {
  304. int s = q->read_ptr - q->write_ptr;
  305. if (q->read_ptr > q->write_ptr)
  306. s -= q->n_bd;
  307. if (s <= 0)
  308. s += q->n_window;
  309. /* keep some reserve to not confuse empty and full situations */
  310. s -= 2;
  311. if (s < 0)
  312. s = 0;
  313. return s;
  314. }
  315. /**
  316. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  317. */
  318. int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  319. int count, int slots_num, u32 id)
  320. {
  321. q->n_bd = count;
  322. q->n_window = slots_num;
  323. q->id = id;
  324. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  325. * and iwl_queue_dec_wrap are broken. */
  326. if (WARN_ON(!is_power_of_2(count)))
  327. return -EINVAL;
  328. /* slots_num must be power-of-two size, otherwise
  329. * get_cmd_index is broken. */
  330. if (WARN_ON(!is_power_of_2(slots_num)))
  331. return -EINVAL;
  332. q->low_mark = q->n_window / 4;
  333. if (q->low_mark < 4)
  334. q->low_mark = 4;
  335. q->high_mark = q->n_window / 8;
  336. if (q->high_mark < 2)
  337. q->high_mark = 2;
  338. q->write_ptr = q->read_ptr = 0;
  339. return 0;
  340. }
  341. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  342. /**
  343. * iwl_enqueue_hcmd - enqueue a uCode command
  344. * @priv: device private data point
  345. * @cmd: a point to the ucode command structure
  346. *
  347. * The function returns < 0 values to indicate the operation is
  348. * failed. On success, it turns the index (> 0) of command in the
  349. * command queue.
  350. */
  351. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  352. {
  353. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  354. struct iwl_queue *q = &txq->q;
  355. struct iwl_device_cmd *out_cmd;
  356. struct iwl_cmd_meta *out_meta;
  357. dma_addr_t phys_addr;
  358. unsigned long flags;
  359. u32 idx;
  360. u16 copy_size, cmd_size;
  361. bool is_ct_kill = false;
  362. bool had_nocopy = false;
  363. int i;
  364. u8 *cmd_dest;
  365. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  366. const void *trace_bufs[IWL_MAX_CMD_TFDS + 1] = {};
  367. int trace_lens[IWL_MAX_CMD_TFDS + 1] = {};
  368. int trace_idx;
  369. #endif
  370. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  371. IWL_WARN(priv, "fw recovery, no hcmd send\n");
  372. return -EIO;
  373. }
  374. copy_size = sizeof(out_cmd->hdr);
  375. cmd_size = sizeof(out_cmd->hdr);
  376. /* need one for the header if the first is NOCOPY */
  377. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  378. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  379. if (!cmd->len[i])
  380. continue;
  381. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  382. had_nocopy = true;
  383. } else {
  384. /* NOCOPY must not be followed by normal! */
  385. if (WARN_ON(had_nocopy))
  386. return -EINVAL;
  387. copy_size += cmd->len[i];
  388. }
  389. cmd_size += cmd->len[i];
  390. }
  391. /*
  392. * If any of the command structures end up being larger than
  393. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  394. * allocated into separate TFDs, then we will need to
  395. * increase the size of the buffers.
  396. */
  397. if (WARN_ON(copy_size > TFD_MAX_PAYLOAD_SIZE))
  398. return -EINVAL;
  399. if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
  400. IWL_WARN(priv, "Not sending command - %s KILL\n",
  401. iwl_is_rfkill(priv) ? "RF" : "CT");
  402. return -EIO;
  403. }
  404. spin_lock_irqsave(&priv->hcmd_lock, flags);
  405. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  406. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  407. IWL_ERR(priv, "No space in command queue\n");
  408. is_ct_kill = iwl_check_for_ct_kill(priv);
  409. if (!is_ct_kill) {
  410. IWL_ERR(priv, "Restarting adapter due to queue full\n");
  411. iwlagn_fw_error(priv, false);
  412. }
  413. return -ENOSPC;
  414. }
  415. idx = get_cmd_index(q, q->write_ptr);
  416. out_cmd = txq->cmd[idx];
  417. out_meta = &txq->meta[idx];
  418. if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
  419. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  420. return -ENOSPC;
  421. }
  422. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  423. if (cmd->flags & CMD_WANT_SKB)
  424. out_meta->source = cmd;
  425. if (cmd->flags & CMD_ASYNC)
  426. out_meta->callback = cmd->callback;
  427. /* set up the header */
  428. out_cmd->hdr.cmd = cmd->id;
  429. out_cmd->hdr.flags = 0;
  430. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(priv->cmd_queue) |
  431. INDEX_TO_SEQ(q->write_ptr));
  432. /* and copy the data that needs to be copied */
  433. cmd_dest = &out_cmd->cmd.payload[0];
  434. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  435. if (!cmd->len[i])
  436. continue;
  437. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)
  438. break;
  439. memcpy(cmd_dest, cmd->data[i], cmd->len[i]);
  440. cmd_dest += cmd->len[i];
  441. }
  442. IWL_DEBUG_HC(priv, "Sending command %s (#%x), seq: 0x%04X, "
  443. "%d bytes at %d[%d]:%d\n",
  444. get_cmd_string(out_cmd->hdr.cmd),
  445. out_cmd->hdr.cmd,
  446. le16_to_cpu(out_cmd->hdr.sequence), cmd_size,
  447. q->write_ptr, idx, priv->cmd_queue);
  448. phys_addr = dma_map_single(priv->bus.dev, &out_cmd->hdr, copy_size,
  449. DMA_BIDIRECTIONAL);
  450. if (unlikely(dma_mapping_error(priv->bus.dev, phys_addr))) {
  451. idx = -ENOMEM;
  452. goto out;
  453. }
  454. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  455. dma_unmap_len_set(out_meta, len, copy_size);
  456. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr, copy_size, 1);
  457. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  458. trace_bufs[0] = &out_cmd->hdr;
  459. trace_lens[0] = copy_size;
  460. trace_idx = 1;
  461. #endif
  462. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  463. if (!cmd->len[i])
  464. continue;
  465. if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY))
  466. continue;
  467. phys_addr = dma_map_single(priv->bus.dev, (void *)cmd->data[i],
  468. cmd->len[i], DMA_BIDIRECTIONAL);
  469. if (dma_mapping_error(priv->bus.dev, phys_addr)) {
  470. iwlagn_unmap_tfd(priv, out_meta,
  471. &txq->tfds[q->write_ptr],
  472. DMA_BIDIRECTIONAL);
  473. idx = -ENOMEM;
  474. goto out;
  475. }
  476. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
  477. cmd->len[i], 0);
  478. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  479. trace_bufs[trace_idx] = cmd->data[i];
  480. trace_lens[trace_idx] = cmd->len[i];
  481. trace_idx++;
  482. #endif
  483. }
  484. out_meta->flags = cmd->flags | CMD_MAPPED;
  485. txq->need_update = 1;
  486. /* check that tracing gets all possible blocks */
  487. BUILD_BUG_ON(IWL_MAX_CMD_TFDS + 1 != 3);
  488. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  489. trace_iwlwifi_dev_hcmd(priv, cmd->flags,
  490. trace_bufs[0], trace_lens[0],
  491. trace_bufs[1], trace_lens[1],
  492. trace_bufs[2], trace_lens[2]);
  493. #endif
  494. /* Increment and update queue's write index */
  495. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  496. iwl_txq_update_write_ptr(priv, txq);
  497. out:
  498. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  499. return idx;
  500. }
  501. /**
  502. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  503. *
  504. * When FW advances 'R' index, all entries between old and new 'R' index
  505. * need to be reclaimed. As result, some free space forms. If there is
  506. * enough free space (> low mark), wake the stack that feeds us.
  507. */
  508. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int idx)
  509. {
  510. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  511. struct iwl_queue *q = &txq->q;
  512. int nfreed = 0;
  513. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  514. IWL_ERR(priv, "%s: Read index for DMA queue txq id (%d), "
  515. "index %d is out of range [0-%d] %d %d.\n", __func__,
  516. txq_id, idx, q->n_bd, q->write_ptr, q->read_ptr);
  517. return;
  518. }
  519. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  520. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  521. if (nfreed++ > 0) {
  522. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
  523. q->write_ptr, q->read_ptr);
  524. iwlagn_fw_error(priv, false);
  525. }
  526. }
  527. }
  528. /**
  529. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  530. * @rxb: Rx buffer to reclaim
  531. *
  532. * If an Rx buffer has an async callback associated with it the callback
  533. * will be executed. The attached skb (if present) will only be freed
  534. * if the callback returns 1
  535. */
  536. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  537. {
  538. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  539. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  540. int txq_id = SEQ_TO_QUEUE(sequence);
  541. int index = SEQ_TO_INDEX(sequence);
  542. int cmd_index;
  543. struct iwl_device_cmd *cmd;
  544. struct iwl_cmd_meta *meta;
  545. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  546. unsigned long flags;
  547. /* If a Tx command is being handled and it isn't in the actual
  548. * command queue then there a command routing bug has been introduced
  549. * in the queue management code. */
  550. if (WARN(txq_id != priv->cmd_queue,
  551. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  552. txq_id, priv->cmd_queue, sequence,
  553. priv->txq[priv->cmd_queue].q.read_ptr,
  554. priv->txq[priv->cmd_queue].q.write_ptr)) {
  555. iwl_print_hex_error(priv, pkt, 32);
  556. return;
  557. }
  558. cmd_index = get_cmd_index(&txq->q, index);
  559. cmd = txq->cmd[cmd_index];
  560. meta = &txq->meta[cmd_index];
  561. iwlagn_unmap_tfd(priv, meta, &txq->tfds[index], DMA_BIDIRECTIONAL);
  562. /* Input error checking is done when commands are added to queue. */
  563. if (meta->flags & CMD_WANT_SKB) {
  564. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  565. rxb->page = NULL;
  566. } else if (meta->callback)
  567. meta->callback(priv, cmd, pkt);
  568. spin_lock_irqsave(&priv->hcmd_lock, flags);
  569. iwl_hcmd_queue_reclaim(priv, txq_id, index);
  570. if (!(meta->flags & CMD_ASYNC)) {
  571. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  572. IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s\n",
  573. get_cmd_string(cmd->hdr.cmd));
  574. wake_up_interruptible(&priv->wait_command_queue);
  575. }
  576. /* Mark as unmapped */
  577. meta->flags = 0;
  578. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  579. }