iwl-trans.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. #include "iwl-trans-int-pcie.h"
  68. /*TODO remove uneeded includes when the transport layer tx_free will be here */
  69. #include "iwl-agn.h"
  70. #include "iwl-core.h"
  71. static int iwl_trans_rx_alloc(struct iwl_priv *priv)
  72. {
  73. struct iwl_rx_queue *rxq = &priv->rxq;
  74. struct device *dev = priv->bus.dev;
  75. memset(&priv->rxq, 0, sizeof(priv->rxq));
  76. spin_lock_init(&rxq->lock);
  77. INIT_LIST_HEAD(&rxq->rx_free);
  78. INIT_LIST_HEAD(&rxq->rx_used);
  79. if (WARN_ON(rxq->bd || rxq->rb_stts))
  80. return -EINVAL;
  81. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  82. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  83. &rxq->bd_dma, GFP_KERNEL);
  84. if (!rxq->bd)
  85. goto err_bd;
  86. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  87. /*Allocate the driver's pointer to receive buffer status */
  88. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  89. &rxq->rb_stts_dma, GFP_KERNEL);
  90. if (!rxq->rb_stts)
  91. goto err_rb_stts;
  92. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  93. return 0;
  94. err_rb_stts:
  95. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  96. rxq->bd, rxq->bd_dma);
  97. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  98. rxq->bd = NULL;
  99. err_bd:
  100. return -ENOMEM;
  101. }
  102. static void iwl_trans_rxq_free_rx_bufs(struct iwl_priv *priv)
  103. {
  104. struct iwl_rx_queue *rxq = &priv->rxq;
  105. int i;
  106. /* Fill the rx_used queue with _all_ of the Rx buffers */
  107. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  108. /* In the reset function, these buffers may have been allocated
  109. * to an SKB, so we need to unmap and free potential storage */
  110. if (rxq->pool[i].page != NULL) {
  111. dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
  112. PAGE_SIZE << priv->hw_params.rx_page_order,
  113. DMA_FROM_DEVICE);
  114. __iwl_free_pages(priv, rxq->pool[i].page);
  115. rxq->pool[i].page = NULL;
  116. }
  117. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  118. }
  119. }
  120. static void iwl_trans_rx_hw_init(struct iwl_priv *priv,
  121. struct iwl_rx_queue *rxq)
  122. {
  123. u32 rb_size;
  124. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  125. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  126. rb_timeout = RX_RB_TIMEOUT;
  127. if (iwlagn_mod_params.amsdu_size_8K)
  128. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  129. else
  130. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  131. /* Stop Rx DMA */
  132. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  133. /* Reset driver's Rx queue write index */
  134. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  135. /* Tell device where to find RBD circular buffer in DRAM */
  136. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  137. (u32)(rxq->bd_dma >> 8));
  138. /* Tell device where in DRAM to update its Rx status */
  139. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  140. rxq->rb_stts_dma >> 4);
  141. /* Enable Rx DMA
  142. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  143. * the credit mechanism in 5000 HW RX FIFO
  144. * Direct rx interrupts to hosts
  145. * Rx buffer size 4 or 8k
  146. * RB timeout 0x10
  147. * 256 RBDs
  148. */
  149. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  150. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  151. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  152. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  153. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  154. rb_size|
  155. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  156. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  157. /* Set interrupt coalescing timer to default (2048 usecs) */
  158. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  159. }
  160. static int iwl_trans_rx_init(struct iwl_priv *priv)
  161. {
  162. struct iwl_rx_queue *rxq = &priv->rxq;
  163. int i, err;
  164. unsigned long flags;
  165. if (!rxq->bd) {
  166. err = iwl_trans_rx_alloc(priv);
  167. if (err)
  168. return err;
  169. }
  170. spin_lock_irqsave(&rxq->lock, flags);
  171. INIT_LIST_HEAD(&rxq->rx_free);
  172. INIT_LIST_HEAD(&rxq->rx_used);
  173. iwl_trans_rxq_free_rx_bufs(priv);
  174. for (i = 0; i < RX_QUEUE_SIZE; i++)
  175. rxq->queue[i] = NULL;
  176. /* Set us so that we have processed and used all buffers, but have
  177. * not restocked the Rx queue with fresh buffers */
  178. rxq->read = rxq->write = 0;
  179. rxq->write_actual = 0;
  180. rxq->free_count = 0;
  181. spin_unlock_irqrestore(&rxq->lock, flags);
  182. iwlagn_rx_replenish(priv);
  183. iwl_trans_rx_hw_init(priv, rxq);
  184. spin_lock_irqsave(&priv->lock, flags);
  185. rxq->need_update = 1;
  186. iwl_rx_queue_update_write_ptr(priv, rxq);
  187. spin_unlock_irqrestore(&priv->lock, flags);
  188. return 0;
  189. }
  190. static void iwl_trans_rx_free(struct iwl_priv *priv)
  191. {
  192. struct iwl_rx_queue *rxq = &priv->rxq;
  193. unsigned long flags;
  194. /*if rxq->bd is NULL, it means that nothing has been allocated,
  195. * exit now */
  196. if (!rxq->bd) {
  197. IWL_DEBUG_INFO(priv, "Free NULL rx context\n");
  198. return;
  199. }
  200. spin_lock_irqsave(&rxq->lock, flags);
  201. iwl_trans_rxq_free_rx_bufs(priv);
  202. spin_unlock_irqrestore(&rxq->lock, flags);
  203. dma_free_coherent(priv->bus.dev, sizeof(__le32) * RX_QUEUE_SIZE,
  204. rxq->bd, rxq->bd_dma);
  205. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  206. rxq->bd = NULL;
  207. if (rxq->rb_stts)
  208. dma_free_coherent(priv->bus.dev,
  209. sizeof(struct iwl_rb_status),
  210. rxq->rb_stts, rxq->rb_stts_dma);
  211. else
  212. IWL_DEBUG_INFO(priv, "Free rxq->rb_stts which is NULL\n");
  213. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  214. rxq->rb_stts = NULL;
  215. }
  216. static int iwl_trans_rx_stop(struct iwl_priv *priv)
  217. {
  218. /* stop Rx DMA */
  219. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  220. return iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  221. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  222. }
  223. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  224. struct iwl_dma_ptr *ptr, size_t size)
  225. {
  226. if (WARN_ON(ptr->addr))
  227. return -EINVAL;
  228. ptr->addr = dma_alloc_coherent(priv->bus.dev, size,
  229. &ptr->dma, GFP_KERNEL);
  230. if (!ptr->addr)
  231. return -ENOMEM;
  232. ptr->size = size;
  233. return 0;
  234. }
  235. static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
  236. struct iwl_dma_ptr *ptr)
  237. {
  238. if (unlikely(!ptr->addr))
  239. return;
  240. dma_free_coherent(priv->bus.dev, ptr->size, ptr->addr, ptr->dma);
  241. memset(ptr, 0, sizeof(*ptr));
  242. }
  243. static int iwl_trans_txq_alloc(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  244. int slots_num, u32 txq_id)
  245. {
  246. size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  247. int i;
  248. if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
  249. return -EINVAL;
  250. txq->q.n_window = slots_num;
  251. txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
  252. GFP_KERNEL);
  253. txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
  254. GFP_KERNEL);
  255. if (!txq->meta || !txq->cmd)
  256. goto error;
  257. for (i = 0; i < slots_num; i++) {
  258. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  259. GFP_KERNEL);
  260. if (!txq->cmd[i])
  261. goto error;
  262. }
  263. /* Alloc driver data array and TFD circular buffer */
  264. /* Driver private data, only for Tx (not command) queues,
  265. * not shared with device. */
  266. if (txq_id != priv->cmd_queue) {
  267. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  268. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  269. if (!txq->txb) {
  270. IWL_ERR(priv, "kmalloc for auxiliary BD "
  271. "structures failed\n");
  272. goto error;
  273. }
  274. } else {
  275. txq->txb = NULL;
  276. }
  277. /* Circular buffer of transmit frame descriptors (TFDs),
  278. * shared with device */
  279. txq->tfds = dma_alloc_coherent(priv->bus.dev, tfd_sz, &txq->q.dma_addr,
  280. GFP_KERNEL);
  281. if (!txq->tfds) {
  282. IWL_ERR(priv, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  283. goto error;
  284. }
  285. txq->q.id = txq_id;
  286. return 0;
  287. error:
  288. kfree(txq->txb);
  289. txq->txb = NULL;
  290. /* since txq->cmd has been zeroed,
  291. * all non allocated cmd[i] will be NULL */
  292. if (txq->cmd)
  293. for (i = 0; i < slots_num; i++)
  294. kfree(txq->cmd[i]);
  295. kfree(txq->meta);
  296. kfree(txq->cmd);
  297. txq->meta = NULL;
  298. txq->cmd = NULL;
  299. return -ENOMEM;
  300. }
  301. static int iwl_trans_txq_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  302. int slots_num, u32 txq_id)
  303. {
  304. int ret;
  305. txq->need_update = 0;
  306. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  307. /*
  308. * For the default queues 0-3, set up the swq_id
  309. * already -- all others need to get one later
  310. * (if they need one at all).
  311. */
  312. if (txq_id < 4)
  313. iwl_set_swq_id(txq, txq_id, txq_id);
  314. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  315. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  316. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  317. /* Initialize queue's high/low-water marks, and head/tail indexes */
  318. ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  319. txq_id);
  320. if (ret)
  321. return ret;
  322. /*
  323. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  324. * given Tx queue, and enable the DMA channel used for that queue.
  325. * Circular buffer (TFD queue in DRAM) physical base address */
  326. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  327. txq->q.dma_addr >> 8);
  328. return 0;
  329. }
  330. /**
  331. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  332. */
  333. static void iwl_tx_queue_unmap(struct iwl_priv *priv, int txq_id)
  334. {
  335. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  336. struct iwl_queue *q = &txq->q;
  337. if (!q->n_bd)
  338. return;
  339. while (q->write_ptr != q->read_ptr) {
  340. /* The read_ptr needs to bound by q->n_window */
  341. iwlagn_txq_free_tfd(priv, txq, get_cmd_index(q, q->read_ptr));
  342. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  343. }
  344. }
  345. /**
  346. * iwl_tx_queue_free - Deallocate DMA queue.
  347. * @txq: Transmit queue to deallocate.
  348. *
  349. * Empty queue by removing and destroying all BD's.
  350. * Free all buffers.
  351. * 0-fill, but do not free "txq" descriptor structure.
  352. */
  353. static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  354. {
  355. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  356. struct device *dev = priv->bus.dev;
  357. int i;
  358. if (WARN_ON(!txq))
  359. return;
  360. iwl_tx_queue_unmap(priv, txq_id);
  361. /* De-alloc array of command/tx buffers */
  362. for (i = 0; i < txq->q.n_window; i++)
  363. kfree(txq->cmd[i]);
  364. /* De-alloc circular buffer of TFDs */
  365. if (txq->q.n_bd) {
  366. dma_free_coherent(dev, priv->hw_params.tfd_size *
  367. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  368. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  369. }
  370. /* De-alloc array of per-TFD driver data */
  371. kfree(txq->txb);
  372. txq->txb = NULL;
  373. /* deallocate arrays */
  374. kfree(txq->cmd);
  375. kfree(txq->meta);
  376. txq->cmd = NULL;
  377. txq->meta = NULL;
  378. /* 0-fill queue descriptor structure */
  379. memset(txq, 0, sizeof(*txq));
  380. }
  381. /**
  382. * iwl_trans_tx_free - Free TXQ Context
  383. *
  384. * Destroy all TX DMA queues and structures
  385. */
  386. static void iwl_trans_tx_free(struct iwl_priv *priv)
  387. {
  388. int txq_id;
  389. /* Tx queues */
  390. if (priv->txq) {
  391. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  392. iwl_tx_queue_free(priv, txq_id);
  393. }
  394. kfree(priv->txq);
  395. priv->txq = NULL;
  396. iwlagn_free_dma_ptr(priv, &priv->kw);
  397. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  398. }
  399. /**
  400. * iwl_trans_tx_alloc - allocate TX context
  401. * Allocate all Tx DMA structures and initialize them
  402. *
  403. * @param priv
  404. * @return error code
  405. */
  406. static int iwl_trans_tx_alloc(struct iwl_priv *priv)
  407. {
  408. int ret;
  409. int txq_id, slots_num;
  410. /*It is not allowed to alloc twice, so warn when this happens.
  411. * We cannot rely on the previous allocation, so free and fail */
  412. if (WARN_ON(priv->txq)) {
  413. ret = -EINVAL;
  414. goto error;
  415. }
  416. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  417. priv->hw_params.scd_bc_tbls_size);
  418. if (ret) {
  419. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  420. goto error;
  421. }
  422. /* Alloc keep-warm buffer */
  423. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  424. if (ret) {
  425. IWL_ERR(priv, "Keep Warm allocation failed\n");
  426. goto error;
  427. }
  428. priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
  429. priv->cfg->base_params->num_of_queues, GFP_KERNEL);
  430. if (!priv->txq) {
  431. IWL_ERR(priv, "Not enough memory for txq\n");
  432. ret = ENOMEM;
  433. goto error;
  434. }
  435. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  436. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  437. slots_num = (txq_id == priv->cmd_queue) ?
  438. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  439. ret = iwl_trans_txq_alloc(priv, &priv->txq[txq_id], slots_num,
  440. txq_id);
  441. if (ret) {
  442. IWL_ERR(priv, "Tx %d queue alloc failed\n", txq_id);
  443. goto error;
  444. }
  445. }
  446. return 0;
  447. error:
  448. trans_tx_free(priv);
  449. return ret;
  450. }
  451. static int iwl_trans_tx_init(struct iwl_priv *priv)
  452. {
  453. int ret;
  454. int txq_id, slots_num;
  455. unsigned long flags;
  456. bool alloc = false;
  457. if (!priv->txq) {
  458. ret = iwl_trans_tx_alloc(priv);
  459. if (ret)
  460. goto error;
  461. alloc = true;
  462. }
  463. spin_lock_irqsave(&priv->lock, flags);
  464. /* Turn off all Tx DMA fifos */
  465. iwl_write_prph(priv, IWLAGN_SCD_TXFACT, 0);
  466. /* Tell NIC where to find the "keep warm" buffer */
  467. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  468. spin_unlock_irqrestore(&priv->lock, flags);
  469. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  470. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  471. slots_num = (txq_id == priv->cmd_queue) ?
  472. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  473. ret = iwl_trans_txq_init(priv, &priv->txq[txq_id], slots_num,
  474. txq_id);
  475. if (ret) {
  476. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  477. goto error;
  478. }
  479. }
  480. return 0;
  481. error:
  482. /*Upon error, free only if we allocated something */
  483. if (alloc)
  484. trans_tx_free(priv);
  485. return ret;
  486. }
  487. /**
  488. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  489. */
  490. static int iwl_trans_tx_stop(struct iwl_priv *priv)
  491. {
  492. int ch, txq_id;
  493. unsigned long flags;
  494. /* Turn off all Tx DMA fifos */
  495. spin_lock_irqsave(&priv->lock, flags);
  496. iwlagn_txq_set_sched(priv, 0);
  497. /* Stop each Tx DMA channel, and wait for it to be idle */
  498. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  499. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  500. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  501. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  502. 1000))
  503. IWL_ERR(priv, "Failing on timeout while stopping"
  504. " DMA channel %d [0x%08x]", ch,
  505. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  506. }
  507. spin_unlock_irqrestore(&priv->lock, flags);
  508. if (!priv->txq) {
  509. IWL_WARN(priv, "Stopping tx queues that aren't allocated...");
  510. return 0;
  511. }
  512. /* Unmap DMA from host system and free skb's */
  513. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  514. iwl_tx_queue_unmap(priv, txq_id);
  515. return 0;
  516. }
  517. static struct iwl_tx_cmd *iwl_trans_get_tx_cmd(struct iwl_priv *priv,
  518. int txq_id)
  519. {
  520. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  521. struct iwl_queue *q = &txq->q;
  522. struct iwl_device_cmd *dev_cmd;
  523. if (unlikely(iwl_queue_space(q) < q->high_mark))
  524. return NULL;
  525. /*
  526. * Set up the Tx-command (not MAC!) header.
  527. * Store the chosen Tx queue and TFD index within the sequence field;
  528. * after Tx, uCode's Tx response will return this value so driver can
  529. * locate the frame within the tx queue and do post-tx processing.
  530. */
  531. dev_cmd = txq->cmd[q->write_ptr];
  532. memset(dev_cmd, 0, sizeof(*dev_cmd));
  533. dev_cmd->hdr.cmd = REPLY_TX;
  534. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  535. INDEX_TO_SEQ(q->write_ptr)));
  536. return &dev_cmd->cmd.tx;
  537. }
  538. static int iwl_trans_tx(struct iwl_priv *priv, struct sk_buff *skb,
  539. struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
  540. struct iwl_rxon_context *ctx)
  541. {
  542. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  543. struct iwl_queue *q = &txq->q;
  544. struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
  545. struct iwl_cmd_meta *out_meta;
  546. dma_addr_t phys_addr = 0;
  547. dma_addr_t txcmd_phys;
  548. dma_addr_t scratch_phys;
  549. u16 len, firstlen, secondlen;
  550. u8 wait_write_ptr = 0;
  551. u8 hdr_len = ieee80211_hdrlen(fc);
  552. /* Set up driver data for this TFD */
  553. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  554. txq->txb[q->write_ptr].skb = skb;
  555. txq->txb[q->write_ptr].ctx = ctx;
  556. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  557. out_meta = &txq->meta[q->write_ptr];
  558. /*
  559. * Use the first empty entry in this queue's command buffer array
  560. * to contain the Tx command and MAC header concatenated together
  561. * (payload data will be in another buffer).
  562. * Size of this varies, due to varying MAC header length.
  563. * If end is not dword aligned, we'll have 2 extra bytes at the end
  564. * of the MAC header (device reads on dword boundaries).
  565. * We'll tell device about this padding later.
  566. */
  567. len = sizeof(struct iwl_tx_cmd) +
  568. sizeof(struct iwl_cmd_header) + hdr_len;
  569. firstlen = (len + 3) & ~3;
  570. /* Tell NIC about any 2-byte padding after MAC header */
  571. if (firstlen != len)
  572. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  573. /* Physical address of this Tx command's header (not MAC header!),
  574. * within command buffer array. */
  575. txcmd_phys = dma_map_single(priv->bus.dev,
  576. &dev_cmd->hdr, firstlen,
  577. DMA_BIDIRECTIONAL);
  578. if (unlikely(dma_mapping_error(priv->bus.dev, txcmd_phys)))
  579. return -1;
  580. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  581. dma_unmap_len_set(out_meta, len, firstlen);
  582. if (!ieee80211_has_morefrags(fc)) {
  583. txq->need_update = 1;
  584. } else {
  585. wait_write_ptr = 1;
  586. txq->need_update = 0;
  587. }
  588. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  589. * if any (802.11 null frames have no payload). */
  590. secondlen = skb->len - hdr_len;
  591. if (secondlen > 0) {
  592. phys_addr = dma_map_single(priv->bus.dev, skb->data + hdr_len,
  593. secondlen, DMA_TO_DEVICE);
  594. if (unlikely(dma_mapping_error(priv->bus.dev, phys_addr))) {
  595. dma_unmap_single(priv->bus.dev,
  596. dma_unmap_addr(out_meta, mapping),
  597. dma_unmap_len(out_meta, len),
  598. DMA_BIDIRECTIONAL);
  599. return -1;
  600. }
  601. }
  602. /* Attach buffers to TFD */
  603. iwlagn_txq_attach_buf_to_tfd(priv, txq, txcmd_phys, firstlen, 1);
  604. if (secondlen > 0)
  605. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
  606. secondlen, 0);
  607. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  608. offsetof(struct iwl_tx_cmd, scratch);
  609. /* take back ownership of DMA buffer to enable update */
  610. dma_sync_single_for_cpu(priv->bus.dev, txcmd_phys, firstlen,
  611. DMA_BIDIRECTIONAL);
  612. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  613. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  614. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  615. le16_to_cpu(dev_cmd->hdr.sequence));
  616. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  617. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  618. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  619. /* Set up entry for this TFD in Tx byte-count array */
  620. if (ampdu)
  621. iwlagn_txq_update_byte_cnt_tbl(priv, txq,
  622. le16_to_cpu(tx_cmd->len));
  623. dma_sync_single_for_device(priv->bus.dev, txcmd_phys, firstlen,
  624. DMA_BIDIRECTIONAL);
  625. trace_iwlwifi_dev_tx(priv,
  626. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  627. sizeof(struct iwl_tfd),
  628. &dev_cmd->hdr, firstlen,
  629. skb->data + hdr_len, secondlen);
  630. /* Tell device the write index *just past* this latest filled TFD */
  631. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  632. iwl_txq_update_write_ptr(priv, txq);
  633. /*
  634. * At this point the frame is "transmitted" successfully
  635. * and we will get a TX status notification eventually,
  636. * regardless of the value of ret. "ret" only indicates
  637. * whether or not we should update the write pointer.
  638. */
  639. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  640. if (wait_write_ptr) {
  641. txq->need_update = 1;
  642. iwl_txq_update_write_ptr(priv, txq);
  643. } else {
  644. iwl_stop_queue(priv, txq);
  645. }
  646. }
  647. return 0;
  648. }
  649. static void iwl_trans_sync_irq(struct iwl_priv *priv)
  650. {
  651. /* wait to make sure we flush pending tasklet*/
  652. synchronize_irq(priv->bus.irq);
  653. tasklet_kill(&priv->irq_tasklet);
  654. }
  655. static void iwl_trans_free(struct iwl_priv *priv)
  656. {
  657. free_irq(priv->bus.irq, priv);
  658. iwl_free_isr_ict(priv);
  659. }
  660. static const struct iwl_trans_ops trans_ops = {
  661. .rx_init = iwl_trans_rx_init,
  662. .rx_stop = iwl_trans_rx_stop,
  663. .rx_free = iwl_trans_rx_free,
  664. .tx_init = iwl_trans_tx_init,
  665. .tx_stop = iwl_trans_tx_stop,
  666. .tx_free = iwl_trans_tx_free,
  667. .send_cmd = iwl_send_cmd,
  668. .send_cmd_pdu = iwl_send_cmd_pdu,
  669. .get_tx_cmd = iwl_trans_get_tx_cmd,
  670. .tx = iwl_trans_tx,
  671. .sync_irq = iwl_trans_sync_irq,
  672. .free = iwl_trans_free,
  673. };
  674. int iwl_trans_register(struct iwl_priv *priv)
  675. {
  676. int err;
  677. priv->trans.ops = &trans_ops;
  678. iwl_alloc_isr_ict(priv);
  679. err = request_irq(priv->bus.irq, iwl_isr_ict, IRQF_SHARED,
  680. DRV_NAME, priv);
  681. if (err) {
  682. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->bus.irq);
  683. iwl_free_isr_ict(priv);
  684. return err;
  685. }
  686. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  687. iwl_irq_tasklet, (unsigned long)priv);
  688. INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
  689. return 0;
  690. }