iwl-trans.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. static int iwl_trans_rx_alloc(struct iwl_priv *priv)
  70. {
  71. struct iwl_rx_queue *rxq = &priv->rxq;
  72. struct device *dev = priv->bus.dev;
  73. memset(&priv->rxq, 0, sizeof(priv->rxq));
  74. spin_lock_init(&rxq->lock);
  75. INIT_LIST_HEAD(&rxq->rx_free);
  76. INIT_LIST_HEAD(&rxq->rx_used);
  77. if (WARN_ON(rxq->bd || rxq->rb_stts))
  78. return -EINVAL;
  79. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  80. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  81. &rxq->bd_dma, GFP_KERNEL);
  82. if (!rxq->bd)
  83. goto err_bd;
  84. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  85. /*Allocate the driver's pointer to receive buffer status */
  86. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  87. &rxq->rb_stts_dma, GFP_KERNEL);
  88. if (!rxq->rb_stts)
  89. goto err_rb_stts;
  90. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  91. return 0;
  92. err_rb_stts:
  93. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  94. rxq->bd, rxq->bd_dma);
  95. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  96. rxq->bd = NULL;
  97. err_bd:
  98. return -ENOMEM;
  99. }
  100. static void iwl_trans_rxq_free_rx_bufs(struct iwl_priv *priv)
  101. {
  102. struct iwl_rx_queue *rxq = &priv->rxq;
  103. int i;
  104. /* Fill the rx_used queue with _all_ of the Rx buffers */
  105. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  106. /* In the reset function, these buffers may have been allocated
  107. * to an SKB, so we need to unmap and free potential storage */
  108. if (rxq->pool[i].page != NULL) {
  109. dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
  110. PAGE_SIZE << priv->hw_params.rx_page_order,
  111. DMA_FROM_DEVICE);
  112. __iwl_free_pages(priv, rxq->pool[i].page);
  113. rxq->pool[i].page = NULL;
  114. }
  115. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  116. }
  117. }
  118. static int iwl_trans_rx_init(struct iwl_priv *priv)
  119. {
  120. struct iwl_rx_queue *rxq = &priv->rxq;
  121. int i, err;
  122. unsigned long flags;
  123. if (!rxq->bd) {
  124. err = iwl_trans_rx_alloc(priv);
  125. if (err)
  126. return err;
  127. }
  128. spin_lock_irqsave(&rxq->lock, flags);
  129. INIT_LIST_HEAD(&rxq->rx_free);
  130. INIT_LIST_HEAD(&rxq->rx_used);
  131. iwl_trans_rxq_free_rx_bufs(priv);
  132. for (i = 0; i < RX_QUEUE_SIZE; i++)
  133. rxq->queue[i] = NULL;
  134. /* Set us so that we have processed and used all buffers, but have
  135. * not restocked the Rx queue with fresh buffers */
  136. rxq->read = rxq->write = 0;
  137. rxq->write_actual = 0;
  138. rxq->free_count = 0;
  139. spin_unlock_irqrestore(&rxq->lock, flags);
  140. return 0;
  141. }
  142. static void iwl_trans_rx_free(struct iwl_priv *priv)
  143. {
  144. struct iwl_rx_queue *rxq = &priv->rxq;
  145. unsigned long flags;
  146. /*if rxq->bd is NULL, it means that nothing has been allocated,
  147. * exit now */
  148. if (!rxq->bd) {
  149. IWL_DEBUG_INFO(priv, "Free NULL rx context\n");
  150. return;
  151. }
  152. spin_lock_irqsave(&rxq->lock, flags);
  153. iwl_trans_rxq_free_rx_bufs(priv);
  154. spin_unlock_irqrestore(&rxq->lock, flags);
  155. dma_free_coherent(priv->bus.dev, sizeof(__le32) * RX_QUEUE_SIZE,
  156. rxq->bd, rxq->bd_dma);
  157. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  158. rxq->bd = NULL;
  159. if (rxq->rb_stts)
  160. dma_free_coherent(priv->bus.dev,
  161. sizeof(struct iwl_rb_status),
  162. rxq->rb_stts, rxq->rb_stts_dma);
  163. else
  164. IWL_DEBUG_INFO(priv, "Free rxq->rb_stts which is NULL\n");
  165. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  166. rxq->rb_stts = NULL;
  167. }
  168. static int iwl_trans_rx_stop(struct iwl_priv *priv)
  169. {
  170. /* stop Rx DMA */
  171. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  172. return iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  173. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  174. }
  175. /* TODO:remove this code duplication */
  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. priv->trans.ops->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. priv->trans.ops->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 < priv->hw_params.dma_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 const struct iwl_trans_ops trans_ops = {
  471. .rx_init = iwl_trans_rx_init,
  472. .rx_stop = iwl_trans_rx_stop,
  473. .rx_free = iwl_trans_rx_free,
  474. .tx_init = iwl_trans_tx_init,
  475. .tx_stop = iwl_trans_tx_stop,
  476. .tx_free = iwl_trans_tx_free,
  477. };
  478. void iwl_trans_register(struct iwl_trans *trans)
  479. {
  480. trans->ops = &trans_ops;
  481. }