iwl-trans.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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_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->trans);
  449. return ret;
  450. }
  451. static int iwl_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, 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->trans);
  485. return ret;
  486. }
  487. static void iwl_set_pwr_vmain(struct iwl_priv *priv)
  488. {
  489. /*
  490. * (for documentation purposes)
  491. * to set power to V_AUX, do:
  492. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  493. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  494. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  495. ~APMG_PS_CTRL_MSK_PWR_SRC);
  496. */
  497. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  498. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  499. ~APMG_PS_CTRL_MSK_PWR_SRC);
  500. }
  501. static int iwl_nic_init(struct iwl_priv *priv)
  502. {
  503. unsigned long flags;
  504. /* nic_init */
  505. spin_lock_irqsave(&priv->lock, flags);
  506. iwl_apm_init(priv);
  507. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  508. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  509. spin_unlock_irqrestore(&priv->lock, flags);
  510. iwl_set_pwr_vmain(priv);
  511. priv->cfg->lib->nic_config(priv);
  512. /* Allocate the RX queue, or reset if it is already allocated */
  513. iwl_rx_init(priv);
  514. /* Allocate or reset and init all Tx and Command queues */
  515. if (iwl_tx_init(priv))
  516. return -ENOMEM;
  517. if (priv->cfg->base_params->shadow_reg_enable) {
  518. /* enable shadow regs in HW */
  519. iwl_set_bit(priv, CSR_MAC_SHADOW_REG_CTRL,
  520. 0x800FFFFF);
  521. }
  522. set_bit(STATUS_INIT, &priv->status);
  523. return 0;
  524. }
  525. #define HW_READY_TIMEOUT (50)
  526. /* Note: returns poll_bit return value, which is >= 0 if success */
  527. static int iwl_set_hw_ready(struct iwl_priv *priv)
  528. {
  529. int ret;
  530. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  531. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  532. /* See if we got it */
  533. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  534. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  535. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  536. HW_READY_TIMEOUT);
  537. IWL_DEBUG_INFO(priv, "hardware%s ready\n", ret < 0 ? " not" : "");
  538. return ret;
  539. }
  540. /* Note: returns standard 0/-ERROR code */
  541. static int iwl_trans_prepare_card_hw(struct iwl_priv *priv)
  542. {
  543. int ret;
  544. IWL_DEBUG_INFO(priv, "iwl_trans_prepare_card_hw enter\n");
  545. ret = iwl_set_hw_ready(priv);
  546. if (ret >= 0)
  547. return 0;
  548. /* If HW is not ready, prepare the conditions to check again */
  549. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  550. CSR_HW_IF_CONFIG_REG_PREPARE);
  551. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  552. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  553. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  554. if (ret < 0)
  555. return ret;
  556. /* HW should be ready by now, check again. */
  557. ret = iwl_set_hw_ready(priv);
  558. if (ret >= 0)
  559. return 0;
  560. return ret;
  561. }
  562. static int iwl_trans_start_device(struct iwl_priv *priv)
  563. {
  564. int ret;
  565. priv->ucode_owner = IWL_OWNERSHIP_DRIVER;
  566. if ((priv->cfg->sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  567. iwl_trans_prepare_card_hw(priv)) {
  568. IWL_WARN(priv, "Exit HW not ready\n");
  569. return -EIO;
  570. }
  571. /* If platform's RF_KILL switch is NOT set to KILL */
  572. if (iwl_read32(priv, CSR_GP_CNTRL) &
  573. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  574. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  575. else
  576. set_bit(STATUS_RF_KILL_HW, &priv->status);
  577. if (iwl_is_rfkill(priv)) {
  578. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  579. iwl_enable_interrupts(priv);
  580. return -ERFKILL;
  581. }
  582. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  583. ret = iwl_nic_init(priv);
  584. if (ret) {
  585. IWL_ERR(priv, "Unable to init nic\n");
  586. return ret;
  587. }
  588. /* make sure rfkill handshake bits are cleared */
  589. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  590. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  591. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  592. /* clear (again), then enable host interrupts */
  593. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  594. iwl_enable_interrupts(priv);
  595. /* really make sure rfkill handshake bits are cleared */
  596. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  597. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  598. return 0;
  599. }
  600. /*
  601. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  602. * must be called under priv->lock and mac access
  603. */
  604. static void iwl_trans_txq_set_sched(struct iwl_priv *priv, u32 mask)
  605. {
  606. iwl_write_prph(priv, SCD_TXFACT, mask);
  607. }
  608. #define IWL_AC_UNSET -1
  609. struct queue_to_fifo_ac {
  610. s8 fifo, ac;
  611. };
  612. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  613. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  614. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  615. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  616. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  617. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  618. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  619. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  620. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  621. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  622. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  623. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  624. };
  625. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  626. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  627. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  628. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  629. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  630. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  631. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  632. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  633. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  634. { IWL_TX_FIFO_BE_IPAN, 2, },
  635. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  636. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  637. };
  638. static void iwl_trans_tx_start(struct iwl_priv *priv)
  639. {
  640. const struct queue_to_fifo_ac *queue_to_fifo;
  641. struct iwl_rxon_context *ctx;
  642. u32 a;
  643. unsigned long flags;
  644. int i, chan;
  645. u32 reg_val;
  646. spin_lock_irqsave(&priv->lock, flags);
  647. priv->scd_base_addr = iwl_read_prph(priv, SCD_SRAM_BASE_ADDR);
  648. a = priv->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  649. /* reset conext data memory */
  650. for (; a < priv->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  651. a += 4)
  652. iwl_write_targ_mem(priv, a, 0);
  653. /* reset tx status memory */
  654. for (; a < priv->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  655. a += 4)
  656. iwl_write_targ_mem(priv, a, 0);
  657. for (; a < priv->scd_base_addr +
  658. SCD_TRANS_TBL_OFFSET_QUEUE(priv->hw_params.max_txq_num); a += 4)
  659. iwl_write_targ_mem(priv, a, 0);
  660. iwl_write_prph(priv, SCD_DRAM_BASE_ADDR,
  661. priv->scd_bc_tbls.dma >> 10);
  662. /* Enable DMA channel */
  663. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  664. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  665. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  666. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  667. /* Update FH chicken bits */
  668. reg_val = iwl_read_direct32(priv, FH_TX_CHICKEN_BITS_REG);
  669. iwl_write_direct32(priv, FH_TX_CHICKEN_BITS_REG,
  670. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  671. iwl_write_prph(priv, SCD_QUEUECHAIN_SEL,
  672. SCD_QUEUECHAIN_SEL_ALL(priv));
  673. iwl_write_prph(priv, SCD_AGGR_SEL, 0);
  674. /* initiate the queues */
  675. for (i = 0; i < priv->hw_params.max_txq_num; i++) {
  676. iwl_write_prph(priv, SCD_QUEUE_RDPTR(i), 0);
  677. iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  678. iwl_write_targ_mem(priv, priv->scd_base_addr +
  679. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  680. iwl_write_targ_mem(priv, priv->scd_base_addr +
  681. SCD_CONTEXT_QUEUE_OFFSET(i) +
  682. sizeof(u32),
  683. ((SCD_WIN_SIZE <<
  684. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  685. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  686. ((SCD_FRAME_LIMIT <<
  687. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  688. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  689. }
  690. iwl_write_prph(priv, SCD_INTERRUPT_MASK,
  691. IWL_MASK(0, priv->hw_params.max_txq_num));
  692. /* Activate all Tx DMA/FIFO channels */
  693. iwl_trans_txq_set_sched(priv, IWL_MASK(0, 7));
  694. /* map queues to FIFOs */
  695. if (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  696. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  697. else
  698. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  699. iwl_trans_set_wr_ptrs(priv, priv->cmd_queue, 0);
  700. /* make sure all queue are not stopped */
  701. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  702. for (i = 0; i < 4; i++)
  703. atomic_set(&priv->queue_stop_count[i], 0);
  704. for_each_context(priv, ctx)
  705. ctx->last_tx_rejected = false;
  706. /* reset to 0 to enable all the queue first */
  707. priv->txq_ctx_active_msk = 0;
  708. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) !=
  709. IWLAGN_FIRST_AMPDU_QUEUE);
  710. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) !=
  711. IWLAGN_FIRST_AMPDU_QUEUE);
  712. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  713. int fifo = queue_to_fifo[i].fifo;
  714. int ac = queue_to_fifo[i].ac;
  715. iwl_txq_ctx_activate(priv, i);
  716. if (fifo == IWL_TX_FIFO_UNUSED)
  717. continue;
  718. if (ac != IWL_AC_UNSET)
  719. iwl_set_swq_id(&priv->txq[i], ac, i);
  720. iwl_trans_tx_queue_set_status(priv, &priv->txq[i], fifo, 0);
  721. }
  722. spin_unlock_irqrestore(&priv->lock, flags);
  723. /* Enable L1-Active */
  724. iwl_clear_bits_prph(priv, APMG_PCIDEV_STT_REG,
  725. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  726. }
  727. /**
  728. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  729. */
  730. static int iwl_trans_tx_stop(struct iwl_priv *priv)
  731. {
  732. int ch, txq_id;
  733. unsigned long flags;
  734. /* Turn off all Tx DMA fifos */
  735. spin_lock_irqsave(&priv->lock, flags);
  736. iwl_trans_txq_set_sched(priv, 0);
  737. /* Stop each Tx DMA channel, and wait for it to be idle */
  738. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  739. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  740. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  741. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  742. 1000))
  743. IWL_ERR(priv, "Failing on timeout while stopping"
  744. " DMA channel %d [0x%08x]", ch,
  745. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  746. }
  747. spin_unlock_irqrestore(&priv->lock, flags);
  748. if (!priv->txq) {
  749. IWL_WARN(priv, "Stopping tx queues that aren't allocated...");
  750. return 0;
  751. }
  752. /* Unmap DMA from host system and free skb's */
  753. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  754. iwl_tx_queue_unmap(priv, txq_id);
  755. return 0;
  756. }
  757. static void iwl_trans_stop_device(struct iwl_priv *priv)
  758. {
  759. unsigned long flags;
  760. /* stop and reset the on-board processor */
  761. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  762. /* tell the device to stop sending interrupts */
  763. spin_lock_irqsave(&priv->lock, flags);
  764. iwl_disable_interrupts(priv);
  765. spin_unlock_irqrestore(&priv->lock, flags);
  766. trans_sync_irq(&priv->trans);
  767. /* device going down, Stop using ICT table */
  768. iwl_disable_ict(priv);
  769. /*
  770. * If a HW restart happens during firmware loading,
  771. * then the firmware loading might call this function
  772. * and later it might be called again due to the
  773. * restart. So don't process again if the device is
  774. * already dead.
  775. */
  776. if (test_bit(STATUS_DEVICE_ENABLED, &priv->status)) {
  777. iwl_trans_tx_stop(priv);
  778. iwl_trans_rx_stop(priv);
  779. /* Power-down device's busmaster DMA clocks */
  780. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  781. APMG_CLK_VAL_DMA_CLK_RQT);
  782. udelay(5);
  783. }
  784. /* Make sure (redundant) we've released our request to stay awake */
  785. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  786. /* Stop the device, and put it in low power state */
  787. iwl_apm_stop(priv);
  788. }
  789. static struct iwl_tx_cmd *iwl_trans_get_tx_cmd(struct iwl_priv *priv,
  790. int txq_id)
  791. {
  792. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  793. struct iwl_queue *q = &txq->q;
  794. struct iwl_device_cmd *dev_cmd;
  795. if (unlikely(iwl_queue_space(q) < q->high_mark))
  796. return NULL;
  797. /*
  798. * Set up the Tx-command (not MAC!) header.
  799. * Store the chosen Tx queue and TFD index within the sequence field;
  800. * after Tx, uCode's Tx response will return this value so driver can
  801. * locate the frame within the tx queue and do post-tx processing.
  802. */
  803. dev_cmd = txq->cmd[q->write_ptr];
  804. memset(dev_cmd, 0, sizeof(*dev_cmd));
  805. dev_cmd->hdr.cmd = REPLY_TX;
  806. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  807. INDEX_TO_SEQ(q->write_ptr)));
  808. return &dev_cmd->cmd.tx;
  809. }
  810. static int iwl_trans_tx(struct iwl_priv *priv, struct sk_buff *skb,
  811. struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
  812. struct iwl_rxon_context *ctx)
  813. {
  814. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  815. struct iwl_queue *q = &txq->q;
  816. struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
  817. struct iwl_cmd_meta *out_meta;
  818. dma_addr_t phys_addr = 0;
  819. dma_addr_t txcmd_phys;
  820. dma_addr_t scratch_phys;
  821. u16 len, firstlen, secondlen;
  822. u8 wait_write_ptr = 0;
  823. u8 hdr_len = ieee80211_hdrlen(fc);
  824. /* Set up driver data for this TFD */
  825. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  826. txq->txb[q->write_ptr].skb = skb;
  827. txq->txb[q->write_ptr].ctx = ctx;
  828. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  829. out_meta = &txq->meta[q->write_ptr];
  830. /*
  831. * Use the first empty entry in this queue's command buffer array
  832. * to contain the Tx command and MAC header concatenated together
  833. * (payload data will be in another buffer).
  834. * Size of this varies, due to varying MAC header length.
  835. * If end is not dword aligned, we'll have 2 extra bytes at the end
  836. * of the MAC header (device reads on dword boundaries).
  837. * We'll tell device about this padding later.
  838. */
  839. len = sizeof(struct iwl_tx_cmd) +
  840. sizeof(struct iwl_cmd_header) + hdr_len;
  841. firstlen = (len + 3) & ~3;
  842. /* Tell NIC about any 2-byte padding after MAC header */
  843. if (firstlen != len)
  844. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  845. /* Physical address of this Tx command's header (not MAC header!),
  846. * within command buffer array. */
  847. txcmd_phys = dma_map_single(priv->bus->dev,
  848. &dev_cmd->hdr, firstlen,
  849. DMA_BIDIRECTIONAL);
  850. if (unlikely(dma_mapping_error(priv->bus->dev, txcmd_phys)))
  851. return -1;
  852. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  853. dma_unmap_len_set(out_meta, len, firstlen);
  854. if (!ieee80211_has_morefrags(fc)) {
  855. txq->need_update = 1;
  856. } else {
  857. wait_write_ptr = 1;
  858. txq->need_update = 0;
  859. }
  860. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  861. * if any (802.11 null frames have no payload). */
  862. secondlen = skb->len - hdr_len;
  863. if (secondlen > 0) {
  864. phys_addr = dma_map_single(priv->bus->dev, skb->data + hdr_len,
  865. secondlen, DMA_TO_DEVICE);
  866. if (unlikely(dma_mapping_error(priv->bus->dev, phys_addr))) {
  867. dma_unmap_single(priv->bus->dev,
  868. dma_unmap_addr(out_meta, mapping),
  869. dma_unmap_len(out_meta, len),
  870. DMA_BIDIRECTIONAL);
  871. return -1;
  872. }
  873. }
  874. /* Attach buffers to TFD */
  875. iwlagn_txq_attach_buf_to_tfd(priv, txq, txcmd_phys, firstlen, 1);
  876. if (secondlen > 0)
  877. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
  878. secondlen, 0);
  879. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  880. offsetof(struct iwl_tx_cmd, scratch);
  881. /* take back ownership of DMA buffer to enable update */
  882. dma_sync_single_for_cpu(priv->bus->dev, txcmd_phys, firstlen,
  883. DMA_BIDIRECTIONAL);
  884. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  885. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  886. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  887. le16_to_cpu(dev_cmd->hdr.sequence));
  888. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  889. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  890. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  891. /* Set up entry for this TFD in Tx byte-count array */
  892. if (ampdu)
  893. iwl_trans_txq_update_byte_cnt_tbl(priv, txq,
  894. le16_to_cpu(tx_cmd->len));
  895. dma_sync_single_for_device(priv->bus->dev, txcmd_phys, firstlen,
  896. DMA_BIDIRECTIONAL);
  897. trace_iwlwifi_dev_tx(priv,
  898. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  899. sizeof(struct iwl_tfd),
  900. &dev_cmd->hdr, firstlen,
  901. skb->data + hdr_len, secondlen);
  902. /* Tell device the write index *just past* this latest filled TFD */
  903. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  904. iwl_txq_update_write_ptr(priv, txq);
  905. /*
  906. * At this point the frame is "transmitted" successfully
  907. * and we will get a TX status notification eventually,
  908. * regardless of the value of ret. "ret" only indicates
  909. * whether or not we should update the write pointer.
  910. */
  911. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  912. if (wait_write_ptr) {
  913. txq->need_update = 1;
  914. iwl_txq_update_write_ptr(priv, txq);
  915. } else {
  916. iwl_stop_queue(priv, txq);
  917. }
  918. }
  919. return 0;
  920. }
  921. static void iwl_trans_kick_nic(struct iwl_priv *priv)
  922. {
  923. /* Remove all resets to allow NIC to operate */
  924. iwl_write32(priv, CSR_RESET, 0);
  925. }
  926. static void iwl_trans_sync_irq(struct iwl_priv *priv)
  927. {
  928. /* wait to make sure we flush pending tasklet*/
  929. synchronize_irq(priv->bus->irq);
  930. tasklet_kill(&priv->irq_tasklet);
  931. }
  932. static void iwl_trans_free(struct iwl_priv *priv)
  933. {
  934. free_irq(priv->bus->irq, priv);
  935. iwl_free_isr_ict(priv);
  936. }
  937. static const struct iwl_trans_ops trans_ops = {
  938. .start_device = iwl_trans_start_device,
  939. .prepare_card_hw = iwl_trans_prepare_card_hw,
  940. .stop_device = iwl_trans_stop_device,
  941. .tx_start = iwl_trans_tx_start,
  942. .rx_free = iwl_trans_rx_free,
  943. .tx_free = iwl_trans_tx_free,
  944. .send_cmd = iwl_send_cmd,
  945. .send_cmd_pdu = iwl_send_cmd_pdu,
  946. .get_tx_cmd = iwl_trans_get_tx_cmd,
  947. .tx = iwl_trans_tx,
  948. .txq_agg_disable = iwl_trans_txq_agg_disable,
  949. .txq_agg_setup = iwl_trans_txq_agg_setup,
  950. .kick_nic = iwl_trans_kick_nic,
  951. .sync_irq = iwl_trans_sync_irq,
  952. .free = iwl_trans_free,
  953. };
  954. int iwl_trans_register(struct iwl_trans *trans, struct iwl_priv *priv)
  955. {
  956. int err;
  957. priv->trans.ops = &trans_ops;
  958. priv->trans.priv = priv;
  959. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  960. iwl_irq_tasklet, (unsigned long)priv);
  961. iwl_alloc_isr_ict(priv);
  962. err = request_irq(priv->bus->irq, iwl_isr_ict, IRQF_SHARED,
  963. DRV_NAME, priv);
  964. if (err) {
  965. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->bus->irq);
  966. iwl_free_isr_ict(priv);
  967. return err;
  968. }
  969. INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
  970. return 0;
  971. }