iwl-tx.c 19 KB

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