iwl-tx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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-agn.h"
  35. #include "iwl-dev.h"
  36. #include "iwl-core.h"
  37. #include "iwl-sta.h"
  38. #include "iwl-io.h"
  39. #include "iwl-helpers.h"
  40. /**
  41. * iwl_txq_update_write_ptr - Send new write index to hardware
  42. */
  43. void iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  44. {
  45. u32 reg = 0;
  46. int txq_id = txq->q.id;
  47. if (txq->need_update == 0)
  48. return;
  49. if (priv->cfg->base_params->shadow_reg_enable) {
  50. /* shadow register enabled */
  51. iwl_write32(priv, HBUS_TARG_WRPTR,
  52. txq->q.write_ptr | (txq_id << 8));
  53. } else {
  54. /* if we're trying to save power */
  55. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  56. /* wake up nic if it's powered down ...
  57. * uCode will wake up, and interrupt us again, so next
  58. * time we'll skip this part. */
  59. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  60. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  61. IWL_DEBUG_INFO(priv,
  62. "Tx queue %d requesting wakeup,"
  63. " GP1 = 0x%x\n", txq_id, reg);
  64. iwl_set_bit(priv, CSR_GP_CNTRL,
  65. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  66. return;
  67. }
  68. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  69. txq->q.write_ptr | (txq_id << 8));
  70. /*
  71. * else not in power-save mode,
  72. * uCode will never sleep when we're
  73. * trying to tx (during RFKILL, we're not trying to tx).
  74. */
  75. } else
  76. iwl_write32(priv, HBUS_TARG_WRPTR,
  77. txq->q.write_ptr | (txq_id << 8));
  78. }
  79. txq->need_update = 0;
  80. }
  81. static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  82. {
  83. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  84. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  85. if (sizeof(dma_addr_t) > sizeof(u32))
  86. addr |=
  87. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  88. return addr;
  89. }
  90. static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
  91. {
  92. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  93. return le16_to_cpu(tb->hi_n_len) >> 4;
  94. }
  95. static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  96. dma_addr_t addr, u16 len)
  97. {
  98. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  99. u16 hi_n_len = len << 4;
  100. put_unaligned_le32(addr, &tb->lo);
  101. if (sizeof(dma_addr_t) > sizeof(u32))
  102. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  103. tb->hi_n_len = cpu_to_le16(hi_n_len);
  104. tfd->num_tbs = idx + 1;
  105. }
  106. static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
  107. {
  108. return tfd->num_tbs & 0x1f;
  109. }
  110. static void iwlagn_unmap_tfd(struct iwl_priv *priv, struct iwl_cmd_meta *meta,
  111. struct iwl_tfd *tfd, int dma_dir)
  112. {
  113. struct pci_dev *dev = priv->pci_dev;
  114. int i;
  115. int num_tbs;
  116. /* Sanity check on number of chunks */
  117. num_tbs = iwl_tfd_get_num_tbs(tfd);
  118. if (num_tbs >= IWL_NUM_OF_TBS) {
  119. IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
  120. /* @todo issue fatal error, it is quite serious situation */
  121. return;
  122. }
  123. /* Unmap tx_cmd */
  124. if (num_tbs)
  125. pci_unmap_single(dev,
  126. dma_unmap_addr(meta, mapping),
  127. dma_unmap_len(meta, len),
  128. PCI_DMA_BIDIRECTIONAL);
  129. /* Unmap chunks, if any. */
  130. for (i = 1; i < num_tbs; i++)
  131. pci_unmap_single(dev, iwl_tfd_tb_get_addr(tfd, i),
  132. iwl_tfd_tb_get_len(tfd, i), dma_dir);
  133. }
  134. /**
  135. * iwlagn_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  136. * @priv - driver private data
  137. * @txq - tx queue
  138. *
  139. * Does NOT advance any TFD circular buffer read/write indexes
  140. * Does NOT free the TFD itself (which is within circular buffer)
  141. */
  142. void iwlagn_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  143. {
  144. struct iwl_tfd *tfd_tmp = txq->tfds;
  145. int index = txq->q.read_ptr;
  146. iwlagn_unmap_tfd(priv, &txq->meta[index], &tfd_tmp[index],
  147. PCI_DMA_TODEVICE);
  148. /* free SKB */
  149. if (txq->txb) {
  150. struct sk_buff *skb;
  151. skb = txq->txb[txq->q.read_ptr].skb;
  152. /* can be called from irqs-disabled context */
  153. if (skb) {
  154. dev_kfree_skb_any(skb);
  155. txq->txb[txq->q.read_ptr].skb = NULL;
  156. }
  157. }
  158. }
  159. int iwlagn_txq_attach_buf_to_tfd(struct iwl_priv *priv,
  160. struct iwl_tx_queue *txq,
  161. dma_addr_t addr, u16 len,
  162. u8 reset)
  163. {
  164. struct iwl_queue *q;
  165. struct iwl_tfd *tfd, *tfd_tmp;
  166. u32 num_tbs;
  167. q = &txq->q;
  168. tfd_tmp = txq->tfds;
  169. tfd = &tfd_tmp[q->write_ptr];
  170. if (reset)
  171. memset(tfd, 0, sizeof(*tfd));
  172. num_tbs = iwl_tfd_get_num_tbs(tfd);
  173. /* Each TFD can point to a maximum 20 Tx buffers */
  174. if (num_tbs >= IWL_NUM_OF_TBS) {
  175. IWL_ERR(priv, "Error can not send more than %d chunks\n",
  176. IWL_NUM_OF_TBS);
  177. return -EINVAL;
  178. }
  179. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  180. return -EINVAL;
  181. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  182. IWL_ERR(priv, "Unaligned address = %llx\n",
  183. (unsigned long long)addr);
  184. iwl_tfd_set_tb(tfd, num_tbs, addr, len);
  185. return 0;
  186. }
  187. /*
  188. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  189. * given Tx queue, and enable the DMA channel used for that queue.
  190. *
  191. * supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  192. * channels supported in hardware.
  193. */
  194. static int iwlagn_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  195. {
  196. int txq_id = txq->q.id;
  197. /* Circular buffer (TFD queue in DRAM) physical base address */
  198. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  199. txq->q.dma_addr >> 8);
  200. return 0;
  201. }
  202. /**
  203. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  204. */
  205. void iwl_tx_queue_unmap(struct iwl_priv *priv, int txq_id)
  206. {
  207. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  208. struct iwl_queue *q = &txq->q;
  209. if (q->n_bd == 0)
  210. return;
  211. while (q->write_ptr != q->read_ptr) {
  212. iwlagn_txq_free_tfd(priv, txq);
  213. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  214. }
  215. }
  216. /**
  217. * iwl_tx_queue_free - Deallocate DMA queue.
  218. * @txq: Transmit queue to deallocate.
  219. *
  220. * Empty queue by removing and destroying all BD's.
  221. * Free all buffers.
  222. * 0-fill, but do not free "txq" descriptor structure.
  223. */
  224. void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  225. {
  226. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  227. struct device *dev = &priv->pci_dev->dev;
  228. int i;
  229. iwl_tx_queue_unmap(priv, txq_id);
  230. /* De-alloc array of command/tx buffers */
  231. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  232. kfree(txq->cmd[i]);
  233. /* De-alloc circular buffer of TFDs */
  234. if (txq->q.n_bd)
  235. dma_free_coherent(dev, priv->hw_params.tfd_size *
  236. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  237. /* De-alloc array of per-TFD driver data */
  238. kfree(txq->txb);
  239. txq->txb = NULL;
  240. /* deallocate arrays */
  241. kfree(txq->cmd);
  242. kfree(txq->meta);
  243. txq->cmd = NULL;
  244. txq->meta = NULL;
  245. /* 0-fill queue descriptor structure */
  246. memset(txq, 0, sizeof(*txq));
  247. }
  248. /**
  249. * iwl_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
  250. */
  251. void iwl_cmd_queue_unmap(struct iwl_priv *priv)
  252. {
  253. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  254. struct iwl_queue *q = &txq->q;
  255. int i;
  256. if (q->n_bd == 0)
  257. return;
  258. while (q->read_ptr != q->write_ptr) {
  259. i = get_cmd_index(q, q->read_ptr);
  260. if (txq->meta[i].flags & CMD_MAPPED) {
  261. iwlagn_unmap_tfd(priv, &txq->meta[i], &txq->tfds[i],
  262. PCI_DMA_BIDIRECTIONAL);
  263. txq->meta[i].flags = 0;
  264. }
  265. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  266. }
  267. }
  268. /**
  269. * iwl_cmd_queue_free - Deallocate DMA queue.
  270. * @txq: Transmit queue to deallocate.
  271. *
  272. * Empty queue by removing and destroying all BD's.
  273. * Free all buffers.
  274. * 0-fill, but do not free "txq" descriptor structure.
  275. */
  276. void iwl_cmd_queue_free(struct iwl_priv *priv)
  277. {
  278. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  279. struct device *dev = &priv->pci_dev->dev;
  280. int i;
  281. iwl_cmd_queue_unmap(priv);
  282. /* De-alloc array of command/tx buffers */
  283. for (i = 0; i < TFD_CMD_SLOTS; i++)
  284. kfree(txq->cmd[i]);
  285. /* De-alloc circular buffer of TFDs */
  286. if (txq->q.n_bd)
  287. dma_free_coherent(dev, priv->hw_params.tfd_size * txq->q.n_bd,
  288. txq->tfds, txq->q.dma_addr);
  289. /* deallocate arrays */
  290. kfree(txq->cmd);
  291. kfree(txq->meta);
  292. txq->cmd = NULL;
  293. txq->meta = NULL;
  294. /* 0-fill queue descriptor structure */
  295. memset(txq, 0, sizeof(*txq));
  296. }
  297. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  298. * DMA services
  299. *
  300. * Theory of operation
  301. *
  302. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  303. * of buffer descriptors, each of which points to one or more data buffers for
  304. * the device to read from or fill. Driver and device exchange status of each
  305. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  306. * entries in each circular buffer, to protect against confusing empty and full
  307. * queue states.
  308. *
  309. * The device reads or writes the data in the queues via the device's several
  310. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  311. *
  312. * For Tx queue, there are low mark and high mark limits. If, after queuing
  313. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  314. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  315. * Tx queue resumed.
  316. *
  317. ***************************************************/
  318. int iwl_queue_space(const struct iwl_queue *q)
  319. {
  320. int s = q->read_ptr - q->write_ptr;
  321. if (q->read_ptr > q->write_ptr)
  322. s -= q->n_bd;
  323. if (s <= 0)
  324. s += q->n_window;
  325. /* keep some reserve to not confuse empty and full situations */
  326. s -= 2;
  327. if (s < 0)
  328. s = 0;
  329. return s;
  330. }
  331. /**
  332. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  333. */
  334. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  335. int count, int slots_num, u32 id)
  336. {
  337. q->n_bd = count;
  338. q->n_window = slots_num;
  339. q->id = id;
  340. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  341. * and iwl_queue_dec_wrap are broken. */
  342. if (WARN_ON(!is_power_of_2(count)))
  343. return -EINVAL;
  344. /* slots_num must be power-of-two size, otherwise
  345. * get_cmd_index is broken. */
  346. if (WARN_ON(!is_power_of_2(slots_num)))
  347. return -EINVAL;
  348. q->low_mark = q->n_window / 4;
  349. if (q->low_mark < 4)
  350. q->low_mark = 4;
  351. q->high_mark = q->n_window / 8;
  352. if (q->high_mark < 2)
  353. q->high_mark = 2;
  354. q->write_ptr = q->read_ptr = 0;
  355. return 0;
  356. }
  357. /**
  358. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  359. */
  360. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  361. struct iwl_tx_queue *txq, u32 id)
  362. {
  363. struct device *dev = &priv->pci_dev->dev;
  364. size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  365. /* Driver private data, only for Tx (not command) queues,
  366. * not shared with device. */
  367. if (id != priv->cmd_queue) {
  368. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  369. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  370. if (!txq->txb) {
  371. IWL_ERR(priv, "kmalloc for auxiliary BD "
  372. "structures failed\n");
  373. goto error;
  374. }
  375. } else {
  376. txq->txb = NULL;
  377. }
  378. /* Circular buffer of transmit frame descriptors (TFDs),
  379. * shared with device */
  380. txq->tfds = dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr,
  381. GFP_KERNEL);
  382. if (!txq->tfds) {
  383. IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n", tfd_sz);
  384. goto error;
  385. }
  386. txq->q.id = id;
  387. return 0;
  388. error:
  389. kfree(txq->txb);
  390. txq->txb = NULL;
  391. return -ENOMEM;
  392. }
  393. /**
  394. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  395. */
  396. int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  397. int slots_num, u32 txq_id)
  398. {
  399. int i, len;
  400. int ret;
  401. txq->meta = kzalloc(sizeof(struct iwl_cmd_meta) * slots_num,
  402. GFP_KERNEL);
  403. txq->cmd = kzalloc(sizeof(struct iwl_device_cmd *) * slots_num,
  404. GFP_KERNEL);
  405. if (!txq->meta || !txq->cmd)
  406. goto out_free_arrays;
  407. len = sizeof(struct iwl_device_cmd);
  408. for (i = 0; i < slots_num; i++) {
  409. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  410. if (!txq->cmd[i])
  411. goto err;
  412. }
  413. /* Alloc driver data array and TFD circular buffer */
  414. ret = iwl_tx_queue_alloc(priv, txq, txq_id);
  415. if (ret)
  416. goto err;
  417. txq->need_update = 0;
  418. /*
  419. * For the default queues 0-3, set up the swq_id
  420. * already -- all others need to get one later
  421. * (if they need one at all).
  422. */
  423. if (txq_id < 4)
  424. iwl_set_swq_id(txq, txq_id, txq_id);
  425. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  426. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  427. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  428. /* Initialize queue's high/low-water marks, and head/tail indexes */
  429. ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  430. if (ret)
  431. return ret;
  432. /* Tell device where to find queue */
  433. iwlagn_tx_queue_init(priv, txq);
  434. return 0;
  435. err:
  436. for (i = 0; i < slots_num; i++)
  437. kfree(txq->cmd[i]);
  438. out_free_arrays:
  439. kfree(txq->meta);
  440. kfree(txq->cmd);
  441. return -ENOMEM;
  442. }
  443. void iwl_tx_queue_reset(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  444. int slots_num, u32 txq_id)
  445. {
  446. memset(txq->meta, 0, sizeof(struct iwl_cmd_meta) * slots_num);
  447. txq->need_update = 0;
  448. /* Initialize queue's high/low-water marks, and head/tail indexes */
  449. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  450. /* Tell device where to find queue */
  451. iwlagn_tx_queue_init(priv, txq);
  452. }
  453. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  454. /**
  455. * iwl_enqueue_hcmd - enqueue a uCode command
  456. * @priv: device private data point
  457. * @cmd: a point to the ucode command structure
  458. *
  459. * The function returns < 0 values to indicate the operation is
  460. * failed. On success, it turns the index (> 0) of command in the
  461. * command queue.
  462. */
  463. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  464. {
  465. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  466. struct iwl_queue *q = &txq->q;
  467. struct iwl_device_cmd *out_cmd;
  468. struct iwl_cmd_meta *out_meta;
  469. dma_addr_t phys_addr;
  470. unsigned long flags;
  471. u32 idx;
  472. u16 copy_size, cmd_size;
  473. bool is_ct_kill = false;
  474. bool had_nocopy = false;
  475. int i;
  476. u8 *cmd_dest;
  477. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  478. const void *trace_bufs[IWL_MAX_CMD_TFDS + 1] = {};
  479. int trace_lens[IWL_MAX_CMD_TFDS + 1] = {};
  480. int trace_idx;
  481. #endif
  482. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  483. IWL_WARN(priv, "fw recovery, no hcmd send\n");
  484. return -EIO;
  485. }
  486. copy_size = sizeof(out_cmd->hdr);
  487. cmd_size = sizeof(out_cmd->hdr);
  488. /* need one for the header if the first is NOCOPY */
  489. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  490. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  491. if (!cmd->len[i])
  492. continue;
  493. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  494. had_nocopy = true;
  495. } else {
  496. /* NOCOPY must not be followed by normal! */
  497. if (WARN_ON(had_nocopy))
  498. return -EINVAL;
  499. copy_size += cmd->len[i];
  500. }
  501. cmd_size += cmd->len[i];
  502. }
  503. /*
  504. * If any of the command structures end up being larger than
  505. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  506. * allocated into separate TFDs, then we will need to
  507. * increase the size of the buffers.
  508. */
  509. if (WARN_ON(copy_size > TFD_MAX_PAYLOAD_SIZE))
  510. return -EINVAL;
  511. if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
  512. IWL_WARN(priv, "Not sending command - %s KILL\n",
  513. iwl_is_rfkill(priv) ? "RF" : "CT");
  514. return -EIO;
  515. }
  516. spin_lock_irqsave(&priv->hcmd_lock, flags);
  517. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  518. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  519. IWL_ERR(priv, "No space in command queue\n");
  520. is_ct_kill = iwl_check_for_ct_kill(priv);
  521. if (!is_ct_kill) {
  522. IWL_ERR(priv, "Restarting adapter due to queue full\n");
  523. iwlagn_fw_error(priv, false);
  524. }
  525. return -ENOSPC;
  526. }
  527. idx = get_cmd_index(q, q->write_ptr);
  528. out_cmd = txq->cmd[idx];
  529. out_meta = &txq->meta[idx];
  530. if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
  531. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  532. return -ENOSPC;
  533. }
  534. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  535. if (cmd->flags & CMD_WANT_SKB)
  536. out_meta->source = cmd;
  537. if (cmd->flags & CMD_ASYNC)
  538. out_meta->callback = cmd->callback;
  539. /* set up the header */
  540. out_cmd->hdr.cmd = cmd->id;
  541. out_cmd->hdr.flags = 0;
  542. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(priv->cmd_queue) |
  543. INDEX_TO_SEQ(q->write_ptr));
  544. /* and copy the data that needs to be copied */
  545. cmd_dest = &out_cmd->cmd.payload[0];
  546. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  547. if (!cmd->len[i])
  548. continue;
  549. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)
  550. break;
  551. memcpy(cmd_dest, cmd->data[i], cmd->len[i]);
  552. cmd_dest += cmd->len[i];
  553. }
  554. IWL_DEBUG_HC(priv, "Sending command %s (#%x), seq: 0x%04X, "
  555. "%d bytes at %d[%d]:%d\n",
  556. get_cmd_string(out_cmd->hdr.cmd),
  557. out_cmd->hdr.cmd,
  558. le16_to_cpu(out_cmd->hdr.sequence), cmd_size,
  559. q->write_ptr, idx, priv->cmd_queue);
  560. phys_addr = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  561. copy_size, PCI_DMA_BIDIRECTIONAL);
  562. if (unlikely(pci_dma_mapping_error(priv->pci_dev, phys_addr))) {
  563. idx = -ENOMEM;
  564. goto out;
  565. }
  566. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  567. dma_unmap_len_set(out_meta, len, copy_size);
  568. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr, copy_size, 1);
  569. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  570. trace_bufs[0] = &out_cmd->hdr;
  571. trace_lens[0] = copy_size;
  572. trace_idx = 1;
  573. #endif
  574. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  575. if (!cmd->len[i])
  576. continue;
  577. if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY))
  578. continue;
  579. phys_addr = pci_map_single(priv->pci_dev, (void *)cmd->data[i],
  580. cmd->len[i], PCI_DMA_BIDIRECTIONAL);
  581. if (pci_dma_mapping_error(priv->pci_dev, phys_addr)) {
  582. iwlagn_unmap_tfd(priv, out_meta,
  583. &txq->tfds[q->write_ptr],
  584. PCI_DMA_BIDIRECTIONAL);
  585. idx = -ENOMEM;
  586. goto out;
  587. }
  588. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
  589. cmd->len[i], 0);
  590. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  591. trace_bufs[trace_idx] = cmd->data[i];
  592. trace_lens[trace_idx] = cmd->len[i];
  593. trace_idx++;
  594. #endif
  595. }
  596. out_meta->flags = cmd->flags | CMD_MAPPED;
  597. txq->need_update = 1;
  598. /* check that tracing gets all possible blocks */
  599. BUILD_BUG_ON(IWL_MAX_CMD_TFDS + 1 != 3);
  600. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  601. trace_iwlwifi_dev_hcmd(priv, cmd->flags,
  602. trace_bufs[0], trace_lens[0],
  603. trace_bufs[1], trace_lens[1],
  604. trace_bufs[2], trace_lens[2]);
  605. #endif
  606. /* Increment and update queue's write index */
  607. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  608. iwl_txq_update_write_ptr(priv, txq);
  609. out:
  610. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  611. return idx;
  612. }
  613. /**
  614. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  615. *
  616. * When FW advances 'R' index, all entries between old and new 'R' index
  617. * need to be reclaimed. As result, some free space forms. If there is
  618. * enough free space (> low mark), wake the stack that feeds us.
  619. */
  620. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int idx)
  621. {
  622. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  623. struct iwl_queue *q = &txq->q;
  624. int nfreed = 0;
  625. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  626. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  627. "is out of range [0-%d] %d %d.\n", txq_id,
  628. idx, q->n_bd, q->write_ptr, q->read_ptr);
  629. return;
  630. }
  631. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  632. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  633. if (nfreed++ > 0) {
  634. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
  635. q->write_ptr, q->read_ptr);
  636. iwlagn_fw_error(priv, false);
  637. }
  638. }
  639. }
  640. /**
  641. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  642. * @rxb: Rx buffer to reclaim
  643. *
  644. * If an Rx buffer has an async callback associated with it the callback
  645. * will be executed. The attached skb (if present) will only be freed
  646. * if the callback returns 1
  647. */
  648. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  649. {
  650. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  651. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  652. int txq_id = SEQ_TO_QUEUE(sequence);
  653. int index = SEQ_TO_INDEX(sequence);
  654. int cmd_index;
  655. struct iwl_device_cmd *cmd;
  656. struct iwl_cmd_meta *meta;
  657. struct iwl_tx_queue *txq = &priv->txq[priv->cmd_queue];
  658. unsigned long flags;
  659. /* If a Tx command is being handled and it isn't in the actual
  660. * command queue then there a command routing bug has been introduced
  661. * in the queue management code. */
  662. if (WARN(txq_id != priv->cmd_queue,
  663. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  664. txq_id, priv->cmd_queue, sequence,
  665. priv->txq[priv->cmd_queue].q.read_ptr,
  666. priv->txq[priv->cmd_queue].q.write_ptr)) {
  667. iwl_print_hex_error(priv, pkt, 32);
  668. return;
  669. }
  670. cmd_index = get_cmd_index(&txq->q, index);
  671. cmd = txq->cmd[cmd_index];
  672. meta = &txq->meta[cmd_index];
  673. txq->time_stamp = jiffies;
  674. iwlagn_unmap_tfd(priv, meta, &txq->tfds[index], PCI_DMA_BIDIRECTIONAL);
  675. /* Input error checking is done when commands are added to queue. */
  676. if (meta->flags & CMD_WANT_SKB) {
  677. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  678. rxb->page = NULL;
  679. } else if (meta->callback)
  680. meta->callback(priv, cmd, pkt);
  681. spin_lock_irqsave(&priv->hcmd_lock, flags);
  682. iwl_hcmd_queue_reclaim(priv, txq_id, index);
  683. if (!(meta->flags & CMD_ASYNC)) {
  684. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  685. IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s\n",
  686. get_cmd_string(cmd->hdr.cmd));
  687. wake_up(&priv->wait_command_queue);
  688. }
  689. /* Mark as unmapped */
  690. meta->flags = 0;
  691. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  692. }