iwl-tx.c 19 KB

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