iwl-tx.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 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-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-sta.h"
  37. #include "iwl-io.h"
  38. #include "iwl-helpers.h"
  39. /**
  40. * iwl_txq_update_write_ptr - Send new write index to hardware
  41. */
  42. void iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  43. {
  44. u32 reg = 0;
  45. int txq_id = txq->q.id;
  46. if (txq->need_update == 0)
  47. return;
  48. if (priv->cfg->base_params->shadow_reg_enable) {
  49. /* shadow register enabled */
  50. iwl_write32(priv, HBUS_TARG_WRPTR,
  51. txq->q.write_ptr | (txq_id << 8));
  52. } else {
  53. /* if we're trying to save power */
  54. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  55. /* wake up nic if it's powered down ...
  56. * uCode will wake up, and interrupt us again, so next
  57. * time we'll skip this part. */
  58. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  59. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  60. IWL_DEBUG_INFO(priv,
  61. "Tx queue %d requesting wakeup,"
  62. " GP1 = 0x%x\n", txq_id, reg);
  63. iwl_set_bit(priv, CSR_GP_CNTRL,
  64. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  65. return;
  66. }
  67. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  68. txq->q.write_ptr | (txq_id << 8));
  69. /*
  70. * else not in power-save mode,
  71. * uCode will never sleep when we're
  72. * trying to tx (during RFKILL, we're not trying to tx).
  73. */
  74. } else
  75. iwl_write32(priv, HBUS_TARG_WRPTR,
  76. txq->q.write_ptr | (txq_id << 8));
  77. }
  78. txq->need_update = 0;
  79. }
  80. EXPORT_SYMBOL(iwl_txq_update_write_ptr);
  81. /**
  82. * iwl_tx_queue_free - Deallocate DMA queue.
  83. * @txq: Transmit queue to deallocate.
  84. *
  85. * Empty queue by removing and destroying all BD's.
  86. * Free all buffers.
  87. * 0-fill, but do not free "txq" descriptor structure.
  88. */
  89. void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  90. {
  91. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  92. struct iwl_queue *q = &txq->q;
  93. struct device *dev = &priv->pci_dev->dev;
  94. int i;
  95. if (q->n_bd == 0)
  96. return;
  97. /* first, empty all BD's */
  98. for (; q->write_ptr != q->read_ptr;
  99. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  100. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  101. /* De-alloc array of command/tx buffers */
  102. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  103. kfree(txq->cmd[i]);
  104. /* De-alloc circular buffer of TFDs */
  105. if (txq->q.n_bd)
  106. dma_free_coherent(dev, priv->hw_params.tfd_size *
  107. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  108. /* De-alloc array of per-TFD driver data */
  109. kfree(txq->txb);
  110. txq->txb = NULL;
  111. /* deallocate arrays */
  112. kfree(txq->cmd);
  113. kfree(txq->meta);
  114. txq->cmd = NULL;
  115. txq->meta = NULL;
  116. /* 0-fill queue descriptor structure */
  117. memset(txq, 0, sizeof(*txq));
  118. }
  119. EXPORT_SYMBOL(iwl_tx_queue_free);
  120. /**
  121. * iwl_cmd_queue_free - Deallocate DMA queue.
  122. * @txq: Transmit queue to deallocate.
  123. *
  124. * Empty queue by removing and destroying all BD's.
  125. * Free all buffers.
  126. * 0-fill, but do not free "txq" descriptor structure.
  127. */
  128. void iwl_cmd_queue_free(struct iwl_priv *priv)
  129. {
  130. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  131. struct iwl_queue *q = &txq->q;
  132. struct device *dev = &priv->pci_dev->dev;
  133. int i;
  134. bool huge = false;
  135. if (q->n_bd == 0)
  136. return;
  137. for (; q->read_ptr != q->write_ptr;
  138. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  139. /* we have no way to tell if it is a huge cmd ATM */
  140. i = get_cmd_index(q, q->read_ptr, 0);
  141. if (txq->meta[i].flags & CMD_SIZE_HUGE) {
  142. huge = true;
  143. continue;
  144. }
  145. pci_unmap_single(priv->pci_dev,
  146. dma_unmap_addr(&txq->meta[i], mapping),
  147. dma_unmap_len(&txq->meta[i], len),
  148. PCI_DMA_BIDIRECTIONAL);
  149. }
  150. if (huge) {
  151. i = q->n_window;
  152. pci_unmap_single(priv->pci_dev,
  153. dma_unmap_addr(&txq->meta[i], mapping),
  154. dma_unmap_len(&txq->meta[i], len),
  155. PCI_DMA_BIDIRECTIONAL);
  156. }
  157. /* De-alloc array of command/tx buffers */
  158. for (i = 0; i <= TFD_CMD_SLOTS; i++)
  159. kfree(txq->cmd[i]);
  160. /* De-alloc circular buffer of TFDs */
  161. if (txq->q.n_bd)
  162. dma_free_coherent(dev, priv->hw_params.tfd_size * txq->q.n_bd,
  163. txq->tfds, txq->q.dma_addr);
  164. /* deallocate arrays */
  165. kfree(txq->cmd);
  166. kfree(txq->meta);
  167. txq->cmd = NULL;
  168. txq->meta = NULL;
  169. /* 0-fill queue descriptor structure */
  170. memset(txq, 0, sizeof(*txq));
  171. }
  172. EXPORT_SYMBOL(iwl_cmd_queue_free);
  173. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  174. * DMA services
  175. *
  176. * Theory of operation
  177. *
  178. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  179. * of buffer descriptors, each of which points to one or more data buffers for
  180. * the device to read from or fill. Driver and device exchange status of each
  181. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  182. * entries in each circular buffer, to protect against confusing empty and full
  183. * queue states.
  184. *
  185. * The device reads or writes the data in the queues via the device's several
  186. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  187. *
  188. * For Tx queue, there are low mark and high mark limits. If, after queuing
  189. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  190. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  191. * Tx queue resumed.
  192. *
  193. * See more detailed info in iwl-4965-hw.h.
  194. ***************************************************/
  195. int iwl_queue_space(const struct iwl_queue *q)
  196. {
  197. int s = q->read_ptr - q->write_ptr;
  198. if (q->read_ptr > q->write_ptr)
  199. s -= q->n_bd;
  200. if (s <= 0)
  201. s += q->n_window;
  202. /* keep some reserve to not confuse empty and full situations */
  203. s -= 2;
  204. if (s < 0)
  205. s = 0;
  206. return s;
  207. }
  208. EXPORT_SYMBOL(iwl_queue_space);
  209. /**
  210. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  211. */
  212. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  213. int count, int slots_num, u32 id)
  214. {
  215. q->n_bd = count;
  216. q->n_window = slots_num;
  217. q->id = id;
  218. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  219. * and iwl_queue_dec_wrap are broken. */
  220. BUG_ON(!is_power_of_2(count));
  221. /* slots_num must be power-of-two size, otherwise
  222. * get_cmd_index is broken. */
  223. BUG_ON(!is_power_of_2(slots_num));
  224. q->low_mark = q->n_window / 4;
  225. if (q->low_mark < 4)
  226. q->low_mark = 4;
  227. q->high_mark = q->n_window / 8;
  228. if (q->high_mark < 2)
  229. q->high_mark = 2;
  230. q->write_ptr = q->read_ptr = 0;
  231. return 0;
  232. }
  233. /**
  234. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  235. */
  236. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  237. struct iwl_tx_queue *txq, u32 id)
  238. {
  239. struct device *dev = &priv->pci_dev->dev;
  240. size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  241. /* Driver private data, only for Tx (not command) queues,
  242. * not shared with device. */
  243. if (id != priv->cmd_queue) {
  244. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  245. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  246. if (!txq->txb) {
  247. IWL_ERR(priv, "kmalloc for auxiliary BD "
  248. "structures failed\n");
  249. goto error;
  250. }
  251. } else {
  252. txq->txb = NULL;
  253. }
  254. /* Circular buffer of transmit frame descriptors (TFDs),
  255. * shared with device */
  256. txq->tfds = dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr,
  257. GFP_KERNEL);
  258. if (!txq->tfds) {
  259. IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n", tfd_sz);
  260. goto error;
  261. }
  262. txq->q.id = id;
  263. return 0;
  264. error:
  265. kfree(txq->txb);
  266. txq->txb = NULL;
  267. return -ENOMEM;
  268. }
  269. /**
  270. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  271. */
  272. int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  273. int slots_num, u32 txq_id)
  274. {
  275. int i, len;
  276. int ret;
  277. int actual_slots = slots_num;
  278. /*
  279. * Alloc buffer array for commands (Tx or other types of commands).
  280. * For the command queue (#4/#9), allocate command space + one big
  281. * command for scan, since scan command is very huge; the system will
  282. * not have two scans at the same time, so only one is needed.
  283. * For normal Tx queues (all other queues), no super-size command
  284. * space is needed.
  285. */
  286. if (txq_id == priv->cmd_queue)
  287. actual_slots++;
  288. txq->meta = kzalloc(sizeof(struct iwl_cmd_meta) * actual_slots,
  289. GFP_KERNEL);
  290. txq->cmd = kzalloc(sizeof(struct iwl_device_cmd *) * actual_slots,
  291. GFP_KERNEL);
  292. if (!txq->meta || !txq->cmd)
  293. goto out_free_arrays;
  294. len = sizeof(struct iwl_device_cmd);
  295. for (i = 0; i < actual_slots; i++) {
  296. /* only happens for cmd queue */
  297. if (i == slots_num)
  298. len = IWL_MAX_CMD_SIZE;
  299. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  300. if (!txq->cmd[i])
  301. goto err;
  302. }
  303. /* Alloc driver data array and TFD circular buffer */
  304. ret = iwl_tx_queue_alloc(priv, txq, txq_id);
  305. if (ret)
  306. goto err;
  307. txq->need_update = 0;
  308. /*
  309. * For the default queues 0-3, set up the swq_id
  310. * already -- all others need to get one later
  311. * (if they need one at all).
  312. */
  313. if (txq_id < 4)
  314. iwl_set_swq_id(txq, txq_id, txq_id);
  315. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  316. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  317. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  318. /* Initialize queue's high/low-water marks, and head/tail indexes */
  319. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  320. /* Tell device where to find queue */
  321. priv->cfg->ops->lib->txq_init(priv, txq);
  322. return 0;
  323. err:
  324. for (i = 0; i < actual_slots; i++)
  325. kfree(txq->cmd[i]);
  326. out_free_arrays:
  327. kfree(txq->meta);
  328. kfree(txq->cmd);
  329. return -ENOMEM;
  330. }
  331. EXPORT_SYMBOL(iwl_tx_queue_init);
  332. void iwl_tx_queue_reset(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  333. int slots_num, u32 txq_id)
  334. {
  335. int actual_slots = slots_num;
  336. if (txq_id == priv->cmd_queue)
  337. actual_slots++;
  338. memset(txq->meta, 0, sizeof(struct iwl_cmd_meta) * actual_slots);
  339. txq->need_update = 0;
  340. /* Initialize queue's high/low-water marks, and head/tail indexes */
  341. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  342. /* Tell device where to find queue */
  343. priv->cfg->ops->lib->txq_init(priv, txq);
  344. }
  345. EXPORT_SYMBOL(iwl_tx_queue_reset);
  346. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  347. /**
  348. * iwl_enqueue_hcmd - enqueue a uCode command
  349. * @priv: device private data point
  350. * @cmd: a point to the ucode command structure
  351. *
  352. * The function returns < 0 values to indicate the operation is
  353. * failed. On success, it turns the index (> 0) of command in the
  354. * command queue.
  355. */
  356. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  357. {
  358. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  359. struct iwl_queue *q = &txq->q;
  360. struct iwl_device_cmd *out_cmd;
  361. struct iwl_cmd_meta *out_meta;
  362. dma_addr_t phys_addr;
  363. unsigned long flags;
  364. int len;
  365. u32 idx;
  366. u16 fix_size;
  367. bool is_ct_kill = false;
  368. cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  369. fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  370. /* If any of the command structures end up being larger than
  371. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  372. * we will need to increase the size of the TFD entries
  373. * Also, check to see if command buffer should not exceed the size
  374. * of device_cmd and max_cmd_size. */
  375. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  376. !(cmd->flags & CMD_SIZE_HUGE));
  377. BUG_ON(fix_size > IWL_MAX_CMD_SIZE);
  378. if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
  379. IWL_WARN(priv, "Not sending command - %s KILL\n",
  380. iwl_is_rfkill(priv) ? "RF" : "CT");
  381. return -EIO;
  382. }
  383. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  384. IWL_ERR(priv, "No space in command queue\n");
  385. if (priv->cfg->ops->lib->tt_ops.ct_kill_check) {
  386. is_ct_kill =
  387. priv->cfg->ops->lib->tt_ops.ct_kill_check(priv);
  388. }
  389. if (!is_ct_kill) {
  390. IWL_ERR(priv, "Restarting adapter due to queue full\n");
  391. queue_work(priv->workqueue, &priv->restart);
  392. }
  393. return -ENOSPC;
  394. }
  395. spin_lock_irqsave(&priv->hcmd_lock, flags);
  396. /* If this is a huge cmd, mark the huge flag also on the meta.flags
  397. * of the _original_ cmd. This is used for DMA mapping clean up.
  398. */
  399. if (cmd->flags & CMD_SIZE_HUGE) {
  400. idx = get_cmd_index(q, q->write_ptr, 0);
  401. txq->meta[idx].flags = CMD_SIZE_HUGE;
  402. }
  403. idx = get_cmd_index(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
  404. out_cmd = txq->cmd[idx];
  405. out_meta = &txq->meta[idx];
  406. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  407. out_meta->flags = cmd->flags;
  408. if (cmd->flags & CMD_WANT_SKB)
  409. out_meta->source = cmd;
  410. if (cmd->flags & CMD_ASYNC)
  411. out_meta->callback = cmd->callback;
  412. out_cmd->hdr.cmd = cmd->id;
  413. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  414. /* At this point, the out_cmd now has all of the incoming cmd
  415. * information */
  416. out_cmd->hdr.flags = 0;
  417. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(priv->cmd_queue) |
  418. INDEX_TO_SEQ(q->write_ptr));
  419. if (cmd->flags & CMD_SIZE_HUGE)
  420. out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
  421. len = sizeof(struct iwl_device_cmd);
  422. if (idx == TFD_CMD_SLOTS)
  423. len = IWL_MAX_CMD_SIZE;
  424. #ifdef CONFIG_IWLWIFI_DEBUG
  425. switch (out_cmd->hdr.cmd) {
  426. case REPLY_TX_LINK_QUALITY_CMD:
  427. case SENSITIVITY_CMD:
  428. IWL_DEBUG_HC_DUMP(priv, "Sending command %s (#%x), seq: 0x%04X, "
  429. "%d bytes at %d[%d]:%d\n",
  430. get_cmd_string(out_cmd->hdr.cmd),
  431. out_cmd->hdr.cmd,
  432. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  433. q->write_ptr, idx, priv->cmd_queue);
  434. break;
  435. default:
  436. IWL_DEBUG_HC(priv, "Sending command %s (#%x), seq: 0x%04X, "
  437. "%d bytes at %d[%d]:%d\n",
  438. get_cmd_string(out_cmd->hdr.cmd),
  439. out_cmd->hdr.cmd,
  440. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  441. q->write_ptr, idx, priv->cmd_queue);
  442. }
  443. #endif
  444. txq->need_update = 1;
  445. if (priv->cfg->ops->lib->txq_update_byte_cnt_tbl)
  446. /* Set up entry in queue's byte count circular buffer */
  447. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  448. phys_addr = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  449. fix_size, PCI_DMA_BIDIRECTIONAL);
  450. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  451. dma_unmap_len_set(out_meta, len, fix_size);
  452. trace_iwlwifi_dev_hcmd(priv, &out_cmd->hdr, fix_size, cmd->flags);
  453. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  454. phys_addr, fix_size, 1,
  455. U32_PAD(cmd->len));
  456. /* Increment and update queue's write index */
  457. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  458. iwl_txq_update_write_ptr(priv, txq);
  459. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  460. return idx;
  461. }
  462. /**
  463. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  464. *
  465. * When FW advances 'R' index, all entries between old and new 'R' index
  466. * need to be reclaimed. As result, some free space forms. If there is
  467. * enough free space (> low mark), wake the stack that feeds us.
  468. */
  469. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id,
  470. int idx, int cmd_idx)
  471. {
  472. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  473. struct iwl_queue *q = &txq->q;
  474. int nfreed = 0;
  475. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  476. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  477. "is out of range [0-%d] %d %d.\n", txq_id,
  478. idx, q->n_bd, q->write_ptr, q->read_ptr);
  479. return;
  480. }
  481. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  482. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  483. if (nfreed++ > 0) {
  484. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
  485. q->write_ptr, q->read_ptr);
  486. queue_work(priv->workqueue, &priv->restart);
  487. }
  488. }
  489. }
  490. /**
  491. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  492. * @rxb: Rx buffer to reclaim
  493. *
  494. * If an Rx buffer has an async callback associated with it the callback
  495. * will be executed. The attached skb (if present) will only be freed
  496. * if the callback returns 1
  497. */
  498. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  499. {
  500. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  501. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  502. int txq_id = SEQ_TO_QUEUE(sequence);
  503. int index = SEQ_TO_INDEX(sequence);
  504. int cmd_index;
  505. bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  506. struct iwl_device_cmd *cmd;
  507. struct iwl_cmd_meta *meta;
  508. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  509. /* If a Tx command is being handled and it isn't in the actual
  510. * command queue then there a command routing bug has been introduced
  511. * in the queue management code. */
  512. if (WARN(txq_id != priv->cmd_queue,
  513. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  514. txq_id, priv->cmd_queue, sequence,
  515. priv->txq[priv->cmd_queue].q.read_ptr,
  516. priv->txq[priv->cmd_queue].q.write_ptr)) {
  517. iwl_print_hex_error(priv, pkt, 32);
  518. return;
  519. }
  520. /* If this is a huge cmd, clear the huge flag on the meta.flags
  521. * of the _original_ cmd. So that iwl_cmd_queue_free won't unmap
  522. * the DMA buffer for the scan (huge) command.
  523. */
  524. if (huge) {
  525. cmd_index = get_cmd_index(&txq->q, index, 0);
  526. txq->meta[cmd_index].flags = 0;
  527. }
  528. cmd_index = get_cmd_index(&txq->q, index, huge);
  529. cmd = txq->cmd[cmd_index];
  530. meta = &txq->meta[cmd_index];
  531. pci_unmap_single(priv->pci_dev,
  532. dma_unmap_addr(meta, mapping),
  533. dma_unmap_len(meta, len),
  534. PCI_DMA_BIDIRECTIONAL);
  535. /* Input error checking is done when commands are added to queue. */
  536. if (meta->flags & CMD_WANT_SKB) {
  537. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  538. rxb->page = NULL;
  539. } else if (meta->callback)
  540. meta->callback(priv, cmd, pkt);
  541. iwl_hcmd_queue_reclaim(priv, txq_id, index, cmd_index);
  542. if (!(meta->flags & CMD_ASYNC)) {
  543. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  544. IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s\n",
  545. get_cmd_string(cmd->hdr.cmd));
  546. wake_up_interruptible(&priv->wait_command_queue);
  547. }
  548. meta->flags = 0;
  549. }
  550. EXPORT_SYMBOL(iwl_tx_cmd_complete);