iwl-trans.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include "iwl-dev.h"
  64. #include "iwl-trans.h"
  65. #include "iwl-core.h"
  66. #include "iwl-helpers.h"
  67. /*TODO remove uneeded includes when the transport layer tx_free will be here */
  68. #include "iwl-agn.h"
  69. #include "iwl-core.h"
  70. static int iwl_trans_rx_alloc(struct iwl_priv *priv)
  71. {
  72. struct iwl_rx_queue *rxq = &priv->rxq;
  73. struct device *dev = priv->bus.dev;
  74. memset(&priv->rxq, 0, sizeof(priv->rxq));
  75. spin_lock_init(&rxq->lock);
  76. INIT_LIST_HEAD(&rxq->rx_free);
  77. INIT_LIST_HEAD(&rxq->rx_used);
  78. if (WARN_ON(rxq->bd || rxq->rb_stts))
  79. return -EINVAL;
  80. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  81. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  82. &rxq->bd_dma, GFP_KERNEL);
  83. if (!rxq->bd)
  84. goto err_bd;
  85. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  86. /*Allocate the driver's pointer to receive buffer status */
  87. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  88. &rxq->rb_stts_dma, GFP_KERNEL);
  89. if (!rxq->rb_stts)
  90. goto err_rb_stts;
  91. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  92. return 0;
  93. err_rb_stts:
  94. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  95. rxq->bd, rxq->bd_dma);
  96. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  97. rxq->bd = NULL;
  98. err_bd:
  99. return -ENOMEM;
  100. }
  101. static void iwl_trans_rxq_free_rx_bufs(struct iwl_priv *priv)
  102. {
  103. struct iwl_rx_queue *rxq = &priv->rxq;
  104. int i;
  105. /* Fill the rx_used queue with _all_ of the Rx buffers */
  106. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  107. /* In the reset function, these buffers may have been allocated
  108. * to an SKB, so we need to unmap and free potential storage */
  109. if (rxq->pool[i].page != NULL) {
  110. dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
  111. PAGE_SIZE << priv->hw_params.rx_page_order,
  112. DMA_FROM_DEVICE);
  113. __iwl_free_pages(priv, rxq->pool[i].page);
  114. rxq->pool[i].page = NULL;
  115. }
  116. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  117. }
  118. }
  119. static int iwl_trans_rx_init(struct iwl_priv *priv)
  120. {
  121. struct iwl_rx_queue *rxq = &priv->rxq;
  122. int i, err;
  123. unsigned long flags;
  124. if (!rxq->bd) {
  125. err = iwl_trans_rx_alloc(priv);
  126. if (err)
  127. return err;
  128. }
  129. spin_lock_irqsave(&rxq->lock, flags);
  130. INIT_LIST_HEAD(&rxq->rx_free);
  131. INIT_LIST_HEAD(&rxq->rx_used);
  132. iwl_trans_rxq_free_rx_bufs(priv);
  133. for (i = 0; i < RX_QUEUE_SIZE; i++)
  134. rxq->queue[i] = NULL;
  135. /* Set us so that we have processed and used all buffers, but have
  136. * not restocked the Rx queue with fresh buffers */
  137. rxq->read = rxq->write = 0;
  138. rxq->write_actual = 0;
  139. rxq->free_count = 0;
  140. spin_unlock_irqrestore(&rxq->lock, flags);
  141. return 0;
  142. }
  143. static void iwl_trans_rx_free(struct iwl_priv *priv)
  144. {
  145. struct iwl_rx_queue *rxq = &priv->rxq;
  146. unsigned long flags;
  147. /*if rxq->bd is NULL, it means that nothing has been allocated,
  148. * exit now */
  149. if (!rxq->bd) {
  150. IWL_DEBUG_INFO(priv, "Free NULL rx context\n");
  151. return;
  152. }
  153. spin_lock_irqsave(&rxq->lock, flags);
  154. iwl_trans_rxq_free_rx_bufs(priv);
  155. spin_unlock_irqrestore(&rxq->lock, flags);
  156. dma_free_coherent(priv->bus.dev, sizeof(__le32) * RX_QUEUE_SIZE,
  157. rxq->bd, rxq->bd_dma);
  158. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  159. rxq->bd = NULL;
  160. if (rxq->rb_stts)
  161. dma_free_coherent(priv->bus.dev,
  162. sizeof(struct iwl_rb_status),
  163. rxq->rb_stts, rxq->rb_stts_dma);
  164. else
  165. IWL_DEBUG_INFO(priv, "Free rxq->rb_stts which is NULL\n");
  166. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  167. rxq->rb_stts = NULL;
  168. }
  169. static int iwl_trans_rx_stop(struct iwl_priv *priv)
  170. {
  171. /* stop Rx DMA */
  172. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  173. return iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  174. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  175. }
  176. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  177. struct iwl_dma_ptr *ptr, size_t size)
  178. {
  179. if (WARN_ON(ptr->addr))
  180. return -EINVAL;
  181. ptr->addr = dma_alloc_coherent(priv->bus.dev, size,
  182. &ptr->dma, GFP_KERNEL);
  183. if (!ptr->addr)
  184. return -ENOMEM;
  185. ptr->size = size;
  186. return 0;
  187. }
  188. static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
  189. struct iwl_dma_ptr *ptr)
  190. {
  191. if (unlikely(!ptr->addr))
  192. return;
  193. dma_free_coherent(priv->bus.dev, ptr->size, ptr->addr, ptr->dma);
  194. memset(ptr, 0, sizeof(*ptr));
  195. }
  196. static int iwl_trans_txq_alloc(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  197. int slots_num, u32 txq_id)
  198. {
  199. size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  200. int i;
  201. if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
  202. return -EINVAL;
  203. txq->q.n_window = slots_num;
  204. txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
  205. GFP_KERNEL);
  206. txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
  207. GFP_KERNEL);
  208. if (!txq->meta || !txq->cmd)
  209. goto error;
  210. for (i = 0; i < slots_num; i++) {
  211. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  212. GFP_KERNEL);
  213. if (!txq->cmd[i])
  214. goto error;
  215. }
  216. /* Alloc driver data array and TFD circular buffer */
  217. /* Driver private data, only for Tx (not command) queues,
  218. * not shared with device. */
  219. if (txq_id != priv->cmd_queue) {
  220. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  221. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  222. if (!txq->txb) {
  223. IWL_ERR(priv, "kmalloc for auxiliary BD "
  224. "structures failed\n");
  225. goto error;
  226. }
  227. } else {
  228. txq->txb = NULL;
  229. }
  230. /* Circular buffer of transmit frame descriptors (TFDs),
  231. * shared with device */
  232. txq->tfds = dma_alloc_coherent(priv->bus.dev, tfd_sz, &txq->q.dma_addr,
  233. GFP_KERNEL);
  234. if (!txq->tfds) {
  235. IWL_ERR(priv, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  236. goto error;
  237. }
  238. txq->q.id = txq_id;
  239. return 0;
  240. error:
  241. kfree(txq->txb);
  242. txq->txb = NULL;
  243. /* since txq->cmd has been zeroed,
  244. * all non allocated cmd[i] will be NULL */
  245. if (txq->cmd)
  246. for (i = 0; i < slots_num; i++)
  247. kfree(txq->cmd[i]);
  248. kfree(txq->meta);
  249. kfree(txq->cmd);
  250. txq->meta = NULL;
  251. txq->cmd = NULL;
  252. return -ENOMEM;
  253. }
  254. static int iwl_trans_txq_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  255. int slots_num, u32 txq_id)
  256. {
  257. int ret;
  258. txq->need_update = 0;
  259. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  260. /*
  261. * For the default queues 0-3, set up the swq_id
  262. * already -- all others need to get one later
  263. * (if they need one at all).
  264. */
  265. if (txq_id < 4)
  266. iwl_set_swq_id(txq, txq_id, txq_id);
  267. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  268. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  269. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  270. /* Initialize queue's high/low-water marks, and head/tail indexes */
  271. ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  272. txq_id);
  273. if (ret)
  274. return ret;
  275. /*
  276. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  277. * given Tx queue, and enable the DMA channel used for that queue.
  278. * Circular buffer (TFD queue in DRAM) physical base address */
  279. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  280. txq->q.dma_addr >> 8);
  281. return 0;
  282. }
  283. /**
  284. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  285. */
  286. static void iwl_tx_queue_unmap(struct iwl_priv *priv, int txq_id)
  287. {
  288. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  289. struct iwl_queue *q = &txq->q;
  290. if (!q->n_bd)
  291. return;
  292. while (q->write_ptr != q->read_ptr) {
  293. /* The read_ptr needs to bound by q->n_window */
  294. iwlagn_txq_free_tfd(priv, txq, get_cmd_index(q, q->read_ptr));
  295. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  296. }
  297. }
  298. /**
  299. * iwl_tx_queue_free - Deallocate DMA queue.
  300. * @txq: Transmit queue to deallocate.
  301. *
  302. * Empty queue by removing and destroying all BD's.
  303. * Free all buffers.
  304. * 0-fill, but do not free "txq" descriptor structure.
  305. */
  306. static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  307. {
  308. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  309. struct device *dev = priv->bus.dev;
  310. int i;
  311. if (WARN_ON(!txq))
  312. return;
  313. iwl_tx_queue_unmap(priv, txq_id);
  314. /* De-alloc array of command/tx buffers */
  315. for (i = 0; i < txq->q.n_window; i++)
  316. kfree(txq->cmd[i]);
  317. /* De-alloc circular buffer of TFDs */
  318. if (txq->q.n_bd) {
  319. dma_free_coherent(dev, priv->hw_params.tfd_size *
  320. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  321. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  322. }
  323. /* De-alloc array of per-TFD driver data */
  324. kfree(txq->txb);
  325. txq->txb = NULL;
  326. /* deallocate arrays */
  327. kfree(txq->cmd);
  328. kfree(txq->meta);
  329. txq->cmd = NULL;
  330. txq->meta = NULL;
  331. /* 0-fill queue descriptor structure */
  332. memset(txq, 0, sizeof(*txq));
  333. }
  334. /**
  335. * iwl_trans_tx_free - Free TXQ Context
  336. *
  337. * Destroy all TX DMA queues and structures
  338. */
  339. static void iwl_trans_tx_free(struct iwl_priv *priv)
  340. {
  341. int txq_id;
  342. /* Tx queues */
  343. if (priv->txq) {
  344. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  345. iwl_tx_queue_free(priv, txq_id);
  346. }
  347. kfree(priv->txq);
  348. priv->txq = NULL;
  349. iwlagn_free_dma_ptr(priv, &priv->kw);
  350. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  351. }
  352. /**
  353. * iwl_trans_tx_alloc - allocate TX context
  354. * Allocate all Tx DMA structures and initialize them
  355. *
  356. * @param priv
  357. * @return error code
  358. */
  359. static int iwl_trans_tx_alloc(struct iwl_priv *priv)
  360. {
  361. int ret;
  362. int txq_id, slots_num;
  363. /*It is not allowed to alloc twice, so warn when this happens.
  364. * We cannot rely on the previous allocation, so free and fail */
  365. if (WARN_ON(priv->txq)) {
  366. ret = -EINVAL;
  367. goto error;
  368. }
  369. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  370. priv->hw_params.scd_bc_tbls_size);
  371. if (ret) {
  372. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  373. goto error;
  374. }
  375. /* Alloc keep-warm buffer */
  376. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  377. if (ret) {
  378. IWL_ERR(priv, "Keep Warm allocation failed\n");
  379. goto error;
  380. }
  381. priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
  382. priv->cfg->base_params->num_of_queues, GFP_KERNEL);
  383. if (!priv->txq) {
  384. IWL_ERR(priv, "Not enough memory for txq\n");
  385. ret = ENOMEM;
  386. goto error;
  387. }
  388. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  389. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  390. slots_num = (txq_id == priv->cmd_queue) ?
  391. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  392. ret = iwl_trans_txq_alloc(priv, &priv->txq[txq_id], slots_num,
  393. txq_id);
  394. if (ret) {
  395. IWL_ERR(priv, "Tx %d queue alloc failed\n", txq_id);
  396. goto error;
  397. }
  398. }
  399. return 0;
  400. error:
  401. trans_tx_free(priv);
  402. return ret;
  403. }
  404. static int iwl_trans_tx_init(struct iwl_priv *priv)
  405. {
  406. int ret;
  407. int txq_id, slots_num;
  408. unsigned long flags;
  409. bool alloc = false;
  410. if (!priv->txq) {
  411. ret = iwl_trans_tx_alloc(priv);
  412. if (ret)
  413. goto error;
  414. alloc = true;
  415. }
  416. spin_lock_irqsave(&priv->lock, flags);
  417. /* Turn off all Tx DMA fifos */
  418. iwl_write_prph(priv, IWLAGN_SCD_TXFACT, 0);
  419. /* Tell NIC where to find the "keep warm" buffer */
  420. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  421. spin_unlock_irqrestore(&priv->lock, flags);
  422. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  423. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  424. slots_num = (txq_id == priv->cmd_queue) ?
  425. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  426. ret = iwl_trans_txq_init(priv, &priv->txq[txq_id], slots_num,
  427. txq_id);
  428. if (ret) {
  429. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  430. goto error;
  431. }
  432. }
  433. return 0;
  434. error:
  435. /*Upon error, free only if we allocated something */
  436. if (alloc)
  437. trans_tx_free(priv);
  438. return ret;
  439. }
  440. /**
  441. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  442. */
  443. static int iwl_trans_tx_stop(struct iwl_priv *priv)
  444. {
  445. int ch, txq_id;
  446. unsigned long flags;
  447. /* Turn off all Tx DMA fifos */
  448. spin_lock_irqsave(&priv->lock, flags);
  449. iwlagn_txq_set_sched(priv, 0);
  450. /* Stop each Tx DMA channel, and wait for it to be idle */
  451. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  452. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  453. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  454. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  455. 1000))
  456. IWL_ERR(priv, "Failing on timeout while stopping"
  457. " DMA channel %d [0x%08x]", ch,
  458. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  459. }
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. if (!priv->txq) {
  462. IWL_WARN(priv, "Stopping tx queues that aren't allocated...");
  463. return 0;
  464. }
  465. /* Unmap DMA from host system and free skb's */
  466. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  467. iwl_tx_queue_unmap(priv, txq_id);
  468. return 0;
  469. }
  470. static struct iwl_tx_cmd *iwl_trans_get_tx_cmd(struct iwl_priv *priv,
  471. int txq_id)
  472. {
  473. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  474. struct iwl_queue *q = &txq->q;
  475. struct iwl_device_cmd *dev_cmd;
  476. if (unlikely(iwl_queue_space(q) < q->high_mark))
  477. return NULL;
  478. /*
  479. * Set up the Tx-command (not MAC!) header.
  480. * Store the chosen Tx queue and TFD index within the sequence field;
  481. * after Tx, uCode's Tx response will return this value so driver can
  482. * locate the frame within the tx queue and do post-tx processing.
  483. */
  484. dev_cmd = txq->cmd[q->write_ptr];
  485. memset(dev_cmd, 0, sizeof(*dev_cmd));
  486. dev_cmd->hdr.cmd = REPLY_TX;
  487. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  488. INDEX_TO_SEQ(q->write_ptr)));
  489. return &dev_cmd->cmd.tx;
  490. }
  491. static int iwl_trans_tx(struct iwl_priv *priv, struct sk_buff *skb,
  492. struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
  493. struct iwl_rxon_context *ctx)
  494. {
  495. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  496. struct iwl_queue *q = &txq->q;
  497. struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
  498. struct iwl_cmd_meta *out_meta;
  499. dma_addr_t phys_addr = 0;
  500. dma_addr_t txcmd_phys;
  501. dma_addr_t scratch_phys;
  502. u16 len, firstlen, secondlen;
  503. u8 wait_write_ptr = 0;
  504. u8 hdr_len = ieee80211_hdrlen(fc);
  505. /* Set up driver data for this TFD */
  506. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  507. txq->txb[q->write_ptr].skb = skb;
  508. txq->txb[q->write_ptr].ctx = ctx;
  509. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  510. out_meta = &txq->meta[q->write_ptr];
  511. /*
  512. * Use the first empty entry in this queue's command buffer array
  513. * to contain the Tx command and MAC header concatenated together
  514. * (payload data will be in another buffer).
  515. * Size of this varies, due to varying MAC header length.
  516. * If end is not dword aligned, we'll have 2 extra bytes at the end
  517. * of the MAC header (device reads on dword boundaries).
  518. * We'll tell device about this padding later.
  519. */
  520. len = sizeof(struct iwl_tx_cmd) +
  521. sizeof(struct iwl_cmd_header) + hdr_len;
  522. firstlen = (len + 3) & ~3;
  523. /* Tell NIC about any 2-byte padding after MAC header */
  524. if (firstlen != len)
  525. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  526. /* Physical address of this Tx command's header (not MAC header!),
  527. * within command buffer array. */
  528. txcmd_phys = dma_map_single(priv->bus.dev,
  529. &dev_cmd->hdr, firstlen,
  530. DMA_BIDIRECTIONAL);
  531. if (unlikely(dma_mapping_error(priv->bus.dev, txcmd_phys)))
  532. return -1;
  533. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  534. dma_unmap_len_set(out_meta, len, firstlen);
  535. if (!ieee80211_has_morefrags(fc)) {
  536. txq->need_update = 1;
  537. } else {
  538. wait_write_ptr = 1;
  539. txq->need_update = 0;
  540. }
  541. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  542. * if any (802.11 null frames have no payload). */
  543. secondlen = skb->len - hdr_len;
  544. if (secondlen > 0) {
  545. phys_addr = dma_map_single(priv->bus.dev, skb->data + hdr_len,
  546. secondlen, DMA_TO_DEVICE);
  547. if (unlikely(dma_mapping_error(priv->bus.dev, phys_addr))) {
  548. dma_unmap_single(priv->bus.dev,
  549. dma_unmap_addr(out_meta, mapping),
  550. dma_unmap_len(out_meta, len),
  551. DMA_BIDIRECTIONAL);
  552. return -1;
  553. }
  554. }
  555. /* Attach buffers to TFD */
  556. iwlagn_txq_attach_buf_to_tfd(priv, txq, txcmd_phys, firstlen, 1);
  557. if (secondlen > 0)
  558. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
  559. secondlen, 0);
  560. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  561. offsetof(struct iwl_tx_cmd, scratch);
  562. /* take back ownership of DMA buffer to enable update */
  563. dma_sync_single_for_cpu(priv->bus.dev, txcmd_phys, firstlen,
  564. DMA_BIDIRECTIONAL);
  565. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  566. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  567. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  568. le16_to_cpu(dev_cmd->hdr.sequence));
  569. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  570. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  571. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  572. /* Set up entry for this TFD in Tx byte-count array */
  573. if (ampdu)
  574. iwlagn_txq_update_byte_cnt_tbl(priv, txq,
  575. le16_to_cpu(tx_cmd->len));
  576. dma_sync_single_for_device(priv->bus.dev, txcmd_phys, firstlen,
  577. DMA_BIDIRECTIONAL);
  578. trace_iwlwifi_dev_tx(priv,
  579. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  580. sizeof(struct iwl_tfd),
  581. &dev_cmd->hdr, firstlen,
  582. skb->data + hdr_len, secondlen);
  583. /* Tell device the write index *just past* this latest filled TFD */
  584. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  585. iwl_txq_update_write_ptr(priv, txq);
  586. /*
  587. * At this point the frame is "transmitted" successfully
  588. * and we will get a TX status notification eventually,
  589. * regardless of the value of ret. "ret" only indicates
  590. * whether or not we should update the write pointer.
  591. */
  592. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  593. if (wait_write_ptr) {
  594. txq->need_update = 1;
  595. iwl_txq_update_write_ptr(priv, txq);
  596. } else {
  597. iwl_stop_queue(priv, txq);
  598. }
  599. }
  600. return 0;
  601. }
  602. static void iwl_trans_free(struct iwl_priv *priv)
  603. {
  604. free_irq(priv->bus.irq, priv);
  605. iwl_free_isr_ict(priv);
  606. }
  607. static const struct iwl_trans_ops trans_ops = {
  608. .rx_init = iwl_trans_rx_init,
  609. .rx_stop = iwl_trans_rx_stop,
  610. .rx_free = iwl_trans_rx_free,
  611. .tx_init = iwl_trans_tx_init,
  612. .tx_stop = iwl_trans_tx_stop,
  613. .tx_free = iwl_trans_tx_free,
  614. .send_cmd = iwl_send_cmd,
  615. .send_cmd_pdu = iwl_send_cmd_pdu,
  616. .get_tx_cmd = iwl_trans_get_tx_cmd,
  617. .tx = iwl_trans_tx,
  618. .free = iwl_trans_free,
  619. };
  620. int iwl_trans_register(struct iwl_priv *priv)
  621. {
  622. int err;
  623. priv->trans.ops = &trans_ops;
  624. iwl_alloc_isr_ict(priv);
  625. err = request_irq(priv->bus.irq, iwl_isr_ict, IRQF_SHARED,
  626. DRV_NAME, priv);
  627. if (err) {
  628. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->bus.irq);
  629. iwl_free_isr_ict(priv);
  630. return err;
  631. }
  632. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  633. iwl_irq_tasklet, (unsigned long)priv);
  634. return 0;
  635. }