iwl-tx.c 22 KB

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