iwl-trans.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. /* TODO:remove this code duplication */
  169. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  170. struct iwl_dma_ptr *ptr, size_t size)
  171. {
  172. if (WARN_ON(ptr->addr))
  173. return -EINVAL;
  174. ptr->addr = dma_alloc_coherent(priv->bus.dev, size,
  175. &ptr->dma, GFP_KERNEL);
  176. if (!ptr->addr)
  177. return -ENOMEM;
  178. ptr->size = size;
  179. return 0;
  180. }
  181. static int iwl_trans_txq_alloc(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  182. int slots_num, u32 txq_id)
  183. {
  184. size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  185. int i;
  186. if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
  187. return -EINVAL;
  188. txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
  189. GFP_KERNEL);
  190. txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
  191. GFP_KERNEL);
  192. if (!txq->meta || !txq->cmd)
  193. goto error;
  194. for (i = 0; i < slots_num; i++) {
  195. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  196. GFP_KERNEL);
  197. if (!txq->cmd[i])
  198. goto error;
  199. }
  200. /* Alloc driver data array and TFD circular buffer */
  201. /* Driver private data, only for Tx (not command) queues,
  202. * not shared with device. */
  203. if (txq_id != priv->cmd_queue) {
  204. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  205. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  206. if (!txq->txb) {
  207. IWL_ERR(priv, "kmalloc for auxiliary BD "
  208. "structures failed\n");
  209. goto error;
  210. }
  211. } else {
  212. txq->txb = NULL;
  213. }
  214. /* Circular buffer of transmit frame descriptors (TFDs),
  215. * shared with device */
  216. txq->tfds = dma_alloc_coherent(priv->bus.dev, tfd_sz, &txq->q.dma_addr,
  217. GFP_KERNEL);
  218. if (!txq->tfds) {
  219. IWL_ERR(priv, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  220. goto error;
  221. }
  222. txq->q.id = txq_id;
  223. return 0;
  224. error:
  225. kfree(txq->txb);
  226. txq->txb = NULL;
  227. /* since txq->cmd has been zeroed,
  228. * all non allocated cmd[i] will be NULL */
  229. if (txq->cmd)
  230. for (i = 0; i < slots_num; i++)
  231. kfree(txq->cmd[i]);
  232. kfree(txq->meta);
  233. kfree(txq->cmd);
  234. txq->meta = NULL;
  235. txq->cmd = NULL;
  236. return -ENOMEM;
  237. }
  238. static int iwl_trans_txq_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  239. int slots_num, u32 txq_id)
  240. {
  241. int ret;
  242. txq->need_update = 0;
  243. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  244. /*
  245. * For the default queues 0-3, set up the swq_id
  246. * already -- all others need to get one later
  247. * (if they need one at all).
  248. */
  249. if (txq_id < 4)
  250. iwl_set_swq_id(txq, txq_id, txq_id);
  251. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  252. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  253. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  254. /* Initialize queue's high/low-water marks, and head/tail indexes */
  255. ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  256. txq_id);
  257. if (ret)
  258. return ret;
  259. /*
  260. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  261. * given Tx queue, and enable the DMA channel used for that queue.
  262. * Circular buffer (TFD queue in DRAM) physical base address */
  263. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  264. txq->q.dma_addr >> 8);
  265. return 0;
  266. }
  267. /**
  268. * iwl_trans_tx_alloc - allocate TX context
  269. * Allocate all Tx DMA structures and initialize them
  270. *
  271. * @param priv
  272. * @return error code
  273. */
  274. static int iwl_trans_tx_alloc(struct iwl_priv *priv)
  275. {
  276. int ret;
  277. int txq_id, slots_num;
  278. /*It is not allowed to alloc twice, so warn when this happens.
  279. * We cannot rely on the previous allocation, so free and fail */
  280. if (WARN_ON(priv->txq)) {
  281. ret = -EINVAL;
  282. goto error;
  283. }
  284. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  285. priv->hw_params.scd_bc_tbls_size);
  286. if (ret) {
  287. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  288. goto error;
  289. }
  290. /* Alloc keep-warm buffer */
  291. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  292. if (ret) {
  293. IWL_ERR(priv, "Keep Warm allocation failed\n");
  294. goto error;
  295. }
  296. priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
  297. priv->cfg->base_params->num_of_queues, GFP_KERNEL);
  298. if (!priv->txq) {
  299. IWL_ERR(priv, "Not enough memory for txq\n");
  300. ret = ENOMEM;
  301. goto error;
  302. }
  303. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  304. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  305. slots_num = (txq_id == priv->cmd_queue) ?
  306. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  307. ret = iwl_trans_txq_alloc(priv, &priv->txq[txq_id], slots_num,
  308. txq_id);
  309. if (ret) {
  310. IWL_ERR(priv, "Tx %d queue alloc failed\n", txq_id);
  311. goto error;
  312. }
  313. }
  314. return 0;
  315. error:
  316. iwlagn_hw_txq_ctx_free(priv);
  317. return ret;
  318. }
  319. static int iwl_trans_tx_init(struct iwl_priv *priv)
  320. {
  321. int ret;
  322. int txq_id, slots_num;
  323. unsigned long flags;
  324. bool alloc = false;
  325. if (!priv->txq) {
  326. ret = iwl_trans_tx_alloc(priv);
  327. if (ret)
  328. goto error;
  329. alloc = true;
  330. }
  331. spin_lock_irqsave(&priv->lock, flags);
  332. /* Turn off all Tx DMA fifos */
  333. iwl_write_prph(priv, IWLAGN_SCD_TXFACT, 0);
  334. /* Tell NIC where to find the "keep warm" buffer */
  335. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  336. spin_unlock_irqrestore(&priv->lock, flags);
  337. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  338. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  339. slots_num = (txq_id == priv->cmd_queue) ?
  340. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  341. ret = iwl_trans_txq_init(priv, &priv->txq[txq_id], slots_num,
  342. txq_id);
  343. if (ret) {
  344. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  345. goto error;
  346. }
  347. }
  348. return 0;
  349. error:
  350. /*Upon error, free only if we allocated something */
  351. if (alloc)
  352. iwlagn_hw_txq_ctx_free(priv);
  353. return ret;
  354. }
  355. static const struct iwl_trans_ops trans_ops = {
  356. .rx_init = iwl_trans_rx_init,
  357. .rx_free = iwl_trans_rx_free,
  358. .tx_init = iwl_trans_tx_init,
  359. };
  360. void iwl_trans_register(struct iwl_trans *trans)
  361. {
  362. trans->ops = &trans_ops;
  363. }