iwl-tx.c 18 KB

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