iwl-tx.c 20 KB

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