iwl-trans.c 14 KB

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