iwl-trans.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. #include "iwl-shared.h"
  72. static int iwl_trans_rx_alloc(struct iwl_priv *priv)
  73. {
  74. struct iwl_rx_queue *rxq = &priv->rxq;
  75. struct device *dev = priv->bus->dev;
  76. memset(&priv->rxq, 0, sizeof(priv->rxq));
  77. spin_lock_init(&rxq->lock);
  78. INIT_LIST_HEAD(&rxq->rx_free);
  79. INIT_LIST_HEAD(&rxq->rx_used);
  80. if (WARN_ON(rxq->bd || rxq->rb_stts))
  81. return -EINVAL;
  82. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  83. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  84. &rxq->bd_dma, GFP_KERNEL);
  85. if (!rxq->bd)
  86. goto err_bd;
  87. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  88. /*Allocate the driver's pointer to receive buffer status */
  89. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  90. &rxq->rb_stts_dma, GFP_KERNEL);
  91. if (!rxq->rb_stts)
  92. goto err_rb_stts;
  93. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  94. return 0;
  95. err_rb_stts:
  96. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  97. rxq->bd, rxq->bd_dma);
  98. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  99. rxq->bd = NULL;
  100. err_bd:
  101. return -ENOMEM;
  102. }
  103. static void iwl_trans_rxq_free_rx_bufs(struct iwl_priv *priv)
  104. {
  105. struct iwl_rx_queue *rxq = &priv->rxq;
  106. int i;
  107. /* Fill the rx_used queue with _all_ of the Rx buffers */
  108. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  109. /* In the reset function, these buffers may have been allocated
  110. * to an SKB, so we need to unmap and free potential storage */
  111. if (rxq->pool[i].page != NULL) {
  112. dma_unmap_page(priv->bus->dev, rxq->pool[i].page_dma,
  113. PAGE_SIZE << hw_params(priv).rx_page_order,
  114. DMA_FROM_DEVICE);
  115. __iwl_free_pages(priv, rxq->pool[i].page);
  116. rxq->pool[i].page = NULL;
  117. }
  118. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  119. }
  120. }
  121. static void iwl_trans_rx_hw_init(struct iwl_priv *priv,
  122. struct iwl_rx_queue *rxq)
  123. {
  124. u32 rb_size;
  125. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  126. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  127. rb_timeout = RX_RB_TIMEOUT;
  128. if (iwlagn_mod_params.amsdu_size_8K)
  129. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  130. else
  131. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  132. /* Stop Rx DMA */
  133. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  134. /* Reset driver's Rx queue write index */
  135. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  136. /* Tell device where to find RBD circular buffer in DRAM */
  137. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  138. (u32)(rxq->bd_dma >> 8));
  139. /* Tell device where in DRAM to update its Rx status */
  140. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  141. rxq->rb_stts_dma >> 4);
  142. /* Enable Rx DMA
  143. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  144. * the credit mechanism in 5000 HW RX FIFO
  145. * Direct rx interrupts to hosts
  146. * Rx buffer size 4 or 8k
  147. * RB timeout 0x10
  148. * 256 RBDs
  149. */
  150. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  151. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  152. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  153. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  154. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  155. rb_size|
  156. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  157. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  158. /* Set interrupt coalescing timer to default (2048 usecs) */
  159. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  160. }
  161. static int iwl_rx_init(struct iwl_priv *priv)
  162. {
  163. struct iwl_rx_queue *rxq = &priv->rxq;
  164. int i, err;
  165. unsigned long flags;
  166. if (!rxq->bd) {
  167. err = iwl_trans_rx_alloc(priv);
  168. if (err)
  169. return err;
  170. }
  171. spin_lock_irqsave(&rxq->lock, flags);
  172. INIT_LIST_HEAD(&rxq->rx_free);
  173. INIT_LIST_HEAD(&rxq->rx_used);
  174. iwl_trans_rxq_free_rx_bufs(priv);
  175. for (i = 0; i < RX_QUEUE_SIZE; i++)
  176. rxq->queue[i] = NULL;
  177. /* Set us so that we have processed and used all buffers, but have
  178. * not restocked the Rx queue with fresh buffers */
  179. rxq->read = rxq->write = 0;
  180. rxq->write_actual = 0;
  181. rxq->free_count = 0;
  182. spin_unlock_irqrestore(&rxq->lock, flags);
  183. iwlagn_rx_replenish(priv);
  184. iwl_trans_rx_hw_init(priv, rxq);
  185. spin_lock_irqsave(&priv->lock, flags);
  186. rxq->need_update = 1;
  187. iwl_rx_queue_update_write_ptr(priv, rxq);
  188. spin_unlock_irqrestore(&priv->lock, flags);
  189. return 0;
  190. }
  191. static void iwl_trans_rx_free(struct iwl_priv *priv)
  192. {
  193. struct iwl_rx_queue *rxq = &priv->rxq;
  194. unsigned long flags;
  195. /*if rxq->bd is NULL, it means that nothing has been allocated,
  196. * exit now */
  197. if (!rxq->bd) {
  198. IWL_DEBUG_INFO(priv, "Free NULL rx context\n");
  199. return;
  200. }
  201. spin_lock_irqsave(&rxq->lock, flags);
  202. iwl_trans_rxq_free_rx_bufs(priv);
  203. spin_unlock_irqrestore(&rxq->lock, flags);
  204. dma_free_coherent(priv->bus->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  205. rxq->bd, rxq->bd_dma);
  206. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  207. rxq->bd = NULL;
  208. if (rxq->rb_stts)
  209. dma_free_coherent(priv->bus->dev,
  210. sizeof(struct iwl_rb_status),
  211. rxq->rb_stts, rxq->rb_stts_dma);
  212. else
  213. IWL_DEBUG_INFO(priv, "Free rxq->rb_stts which is NULL\n");
  214. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  215. rxq->rb_stts = NULL;
  216. }
  217. static int iwl_trans_rx_stop(struct iwl_priv *priv)
  218. {
  219. /* stop Rx DMA */
  220. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  221. return iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  222. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  223. }
  224. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  225. struct iwl_dma_ptr *ptr, size_t size)
  226. {
  227. if (WARN_ON(ptr->addr))
  228. return -EINVAL;
  229. ptr->addr = dma_alloc_coherent(priv->bus->dev, size,
  230. &ptr->dma, GFP_KERNEL);
  231. if (!ptr->addr)
  232. return -ENOMEM;
  233. ptr->size = size;
  234. return 0;
  235. }
  236. static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
  237. struct iwl_dma_ptr *ptr)
  238. {
  239. if (unlikely(!ptr->addr))
  240. return;
  241. dma_free_coherent(priv->bus->dev, ptr->size, ptr->addr, ptr->dma);
  242. memset(ptr, 0, sizeof(*ptr));
  243. }
  244. static int iwl_trans_txq_alloc(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  245. int slots_num, u32 txq_id)
  246. {
  247. size_t tfd_sz = hw_params(priv).tfd_size * TFD_QUEUE_SIZE_MAX;
  248. int i;
  249. if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
  250. return -EINVAL;
  251. txq->q.n_window = slots_num;
  252. txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
  253. GFP_KERNEL);
  254. txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
  255. GFP_KERNEL);
  256. if (!txq->meta || !txq->cmd)
  257. goto error;
  258. for (i = 0; i < slots_num; i++) {
  259. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  260. GFP_KERNEL);
  261. if (!txq->cmd[i])
  262. goto error;
  263. }
  264. /* Alloc driver data array and TFD circular buffer */
  265. /* Driver private data, only for Tx (not command) queues,
  266. * not shared with device. */
  267. if (txq_id != priv->shrd->cmd_queue) {
  268. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  269. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  270. if (!txq->txb) {
  271. IWL_ERR(priv, "kmalloc for auxiliary BD "
  272. "structures failed\n");
  273. goto error;
  274. }
  275. } else {
  276. txq->txb = NULL;
  277. }
  278. /* Circular buffer of transmit frame descriptors (TFDs),
  279. * shared with device */
  280. txq->tfds = dma_alloc_coherent(priv->bus->dev, tfd_sz, &txq->q.dma_addr,
  281. GFP_KERNEL);
  282. if (!txq->tfds) {
  283. IWL_ERR(priv, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  284. goto error;
  285. }
  286. txq->q.id = txq_id;
  287. return 0;
  288. error:
  289. kfree(txq->txb);
  290. txq->txb = NULL;
  291. /* since txq->cmd has been zeroed,
  292. * all non allocated cmd[i] will be NULL */
  293. if (txq->cmd)
  294. for (i = 0; i < slots_num; i++)
  295. kfree(txq->cmd[i]);
  296. kfree(txq->meta);
  297. kfree(txq->cmd);
  298. txq->meta = NULL;
  299. txq->cmd = NULL;
  300. return -ENOMEM;
  301. }
  302. static int iwl_trans_txq_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  303. int slots_num, u32 txq_id)
  304. {
  305. int ret;
  306. txq->need_update = 0;
  307. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  308. /*
  309. * For the default queues 0-3, set up the swq_id
  310. * already -- all others need to get one later
  311. * (if they need one at all).
  312. */
  313. if (txq_id < 4)
  314. iwl_set_swq_id(txq, txq_id, txq_id);
  315. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  316. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  317. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  318. /* Initialize queue's high/low-water marks, and head/tail indexes */
  319. ret = iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  320. txq_id);
  321. if (ret)
  322. return ret;
  323. /*
  324. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  325. * given Tx queue, and enable the DMA channel used for that queue.
  326. * Circular buffer (TFD queue in DRAM) physical base address */
  327. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  328. txq->q.dma_addr >> 8);
  329. return 0;
  330. }
  331. /**
  332. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  333. */
  334. static void iwl_tx_queue_unmap(struct iwl_priv *priv, int txq_id)
  335. {
  336. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  337. struct iwl_queue *q = &txq->q;
  338. if (!q->n_bd)
  339. return;
  340. while (q->write_ptr != q->read_ptr) {
  341. /* The read_ptr needs to bound by q->n_window */
  342. iwlagn_txq_free_tfd(priv, txq, get_cmd_index(q, q->read_ptr));
  343. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  344. }
  345. }
  346. /**
  347. * iwl_tx_queue_free - Deallocate DMA queue.
  348. * @txq: Transmit queue to deallocate.
  349. *
  350. * Empty queue by removing and destroying all BD's.
  351. * Free all buffers.
  352. * 0-fill, but do not free "txq" descriptor structure.
  353. */
  354. static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  355. {
  356. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  357. struct device *dev = priv->bus->dev;
  358. int i;
  359. if (WARN_ON(!txq))
  360. return;
  361. iwl_tx_queue_unmap(priv, txq_id);
  362. /* De-alloc array of command/tx buffers */
  363. for (i = 0; i < txq->q.n_window; i++)
  364. kfree(txq->cmd[i]);
  365. /* De-alloc circular buffer of TFDs */
  366. if (txq->q.n_bd) {
  367. dma_free_coherent(dev, hw_params(priv).tfd_size *
  368. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  369. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  370. }
  371. /* De-alloc array of per-TFD driver data */
  372. kfree(txq->txb);
  373. txq->txb = NULL;
  374. /* deallocate arrays */
  375. kfree(txq->cmd);
  376. kfree(txq->meta);
  377. txq->cmd = NULL;
  378. txq->meta = NULL;
  379. /* 0-fill queue descriptor structure */
  380. memset(txq, 0, sizeof(*txq));
  381. }
  382. /**
  383. * iwl_trans_tx_free - Free TXQ Context
  384. *
  385. * Destroy all TX DMA queues and structures
  386. */
  387. static void iwl_trans_tx_free(struct iwl_priv *priv)
  388. {
  389. int txq_id;
  390. /* Tx queues */
  391. if (priv->txq) {
  392. for (txq_id = 0;
  393. txq_id < hw_params(priv).max_txq_num; txq_id++)
  394. iwl_tx_queue_free(priv, txq_id);
  395. }
  396. kfree(priv->txq);
  397. priv->txq = NULL;
  398. iwlagn_free_dma_ptr(priv, &priv->kw);
  399. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  400. }
  401. /**
  402. * iwl_trans_tx_alloc - allocate TX context
  403. * Allocate all Tx DMA structures and initialize them
  404. *
  405. * @param priv
  406. * @return error code
  407. */
  408. static int iwl_trans_tx_alloc(struct iwl_priv *priv)
  409. {
  410. int ret;
  411. int txq_id, slots_num;
  412. /*It is not allowed to alloc twice, so warn when this happens.
  413. * We cannot rely on the previous allocation, so free and fail */
  414. if (WARN_ON(priv->txq)) {
  415. ret = -EINVAL;
  416. goto error;
  417. }
  418. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  419. hw_params(priv).scd_bc_tbls_size);
  420. if (ret) {
  421. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  422. goto error;
  423. }
  424. /* Alloc keep-warm buffer */
  425. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  426. if (ret) {
  427. IWL_ERR(priv, "Keep Warm allocation failed\n");
  428. goto error;
  429. }
  430. priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
  431. priv->cfg->base_params->num_of_queues, GFP_KERNEL);
  432. if (!priv->txq) {
  433. IWL_ERR(priv, "Not enough memory for txq\n");
  434. ret = ENOMEM;
  435. goto error;
  436. }
  437. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  438. for (txq_id = 0; txq_id < hw_params(priv).max_txq_num; txq_id++) {
  439. slots_num = (txq_id == priv->shrd->cmd_queue) ?
  440. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  441. ret = iwl_trans_txq_alloc(priv, &priv->txq[txq_id], slots_num,
  442. txq_id);
  443. if (ret) {
  444. IWL_ERR(priv, "Tx %d queue alloc failed\n", txq_id);
  445. goto error;
  446. }
  447. }
  448. return 0;
  449. error:
  450. trans_tx_free(&priv->trans);
  451. return ret;
  452. }
  453. static int iwl_tx_init(struct iwl_priv *priv)
  454. {
  455. int ret;
  456. int txq_id, slots_num;
  457. unsigned long flags;
  458. bool alloc = false;
  459. if (!priv->txq) {
  460. ret = iwl_trans_tx_alloc(priv);
  461. if (ret)
  462. goto error;
  463. alloc = true;
  464. }
  465. spin_lock_irqsave(&priv->lock, flags);
  466. /* Turn off all Tx DMA fifos */
  467. iwl_write_prph(priv, SCD_TXFACT, 0);
  468. /* Tell NIC where to find the "keep warm" buffer */
  469. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  470. spin_unlock_irqrestore(&priv->lock, flags);
  471. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  472. for (txq_id = 0; txq_id < hw_params(priv).max_txq_num; txq_id++) {
  473. slots_num = (txq_id == priv->shrd->cmd_queue) ?
  474. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  475. ret = iwl_trans_txq_init(priv, &priv->txq[txq_id], slots_num,
  476. txq_id);
  477. if (ret) {
  478. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  479. goto error;
  480. }
  481. }
  482. return 0;
  483. error:
  484. /*Upon error, free only if we allocated something */
  485. if (alloc)
  486. trans_tx_free(&priv->trans);
  487. return ret;
  488. }
  489. static void iwl_set_pwr_vmain(struct iwl_priv *priv)
  490. {
  491. /*
  492. * (for documentation purposes)
  493. * to set power to V_AUX, do:
  494. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  495. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  496. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  497. ~APMG_PS_CTRL_MSK_PWR_SRC);
  498. */
  499. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  500. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  501. ~APMG_PS_CTRL_MSK_PWR_SRC);
  502. }
  503. static int iwl_nic_init(struct iwl_priv *priv)
  504. {
  505. unsigned long flags;
  506. /* nic_init */
  507. spin_lock_irqsave(&priv->lock, flags);
  508. iwl_apm_init(priv);
  509. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  510. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  511. spin_unlock_irqrestore(&priv->lock, flags);
  512. iwl_set_pwr_vmain(priv);
  513. priv->cfg->lib->nic_config(priv);
  514. /* Allocate the RX queue, or reset if it is already allocated */
  515. iwl_rx_init(priv);
  516. /* Allocate or reset and init all Tx and Command queues */
  517. if (iwl_tx_init(priv))
  518. return -ENOMEM;
  519. if (priv->cfg->base_params->shadow_reg_enable) {
  520. /* enable shadow regs in HW */
  521. iwl_set_bit(priv, CSR_MAC_SHADOW_REG_CTRL,
  522. 0x800FFFFF);
  523. }
  524. set_bit(STATUS_INIT, &priv->status);
  525. return 0;
  526. }
  527. #define HW_READY_TIMEOUT (50)
  528. /* Note: returns poll_bit return value, which is >= 0 if success */
  529. static int iwl_set_hw_ready(struct iwl_priv *priv)
  530. {
  531. int ret;
  532. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  533. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  534. /* See if we got it */
  535. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  536. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  537. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  538. HW_READY_TIMEOUT);
  539. IWL_DEBUG_INFO(priv, "hardware%s ready\n", ret < 0 ? " not" : "");
  540. return ret;
  541. }
  542. /* Note: returns standard 0/-ERROR code */
  543. static int iwl_trans_prepare_card_hw(struct iwl_priv *priv)
  544. {
  545. int ret;
  546. IWL_DEBUG_INFO(priv, "iwl_trans_prepare_card_hw enter\n");
  547. ret = iwl_set_hw_ready(priv);
  548. if (ret >= 0)
  549. return 0;
  550. /* If HW is not ready, prepare the conditions to check again */
  551. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  552. CSR_HW_IF_CONFIG_REG_PREPARE);
  553. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  554. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  555. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  556. if (ret < 0)
  557. return ret;
  558. /* HW should be ready by now, check again. */
  559. ret = iwl_set_hw_ready(priv);
  560. if (ret >= 0)
  561. return 0;
  562. return ret;
  563. }
  564. static int iwl_trans_start_device(struct iwl_priv *priv)
  565. {
  566. int ret;
  567. priv->ucode_owner = IWL_OWNERSHIP_DRIVER;
  568. if ((priv->cfg->sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  569. iwl_trans_prepare_card_hw(priv)) {
  570. IWL_WARN(priv, "Exit HW not ready\n");
  571. return -EIO;
  572. }
  573. /* If platform's RF_KILL switch is NOT set to KILL */
  574. if (iwl_read32(priv, CSR_GP_CNTRL) &
  575. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  576. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  577. else
  578. set_bit(STATUS_RF_KILL_HW, &priv->status);
  579. if (iwl_is_rfkill(priv)) {
  580. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  581. iwl_enable_interrupts(priv);
  582. return -ERFKILL;
  583. }
  584. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  585. ret = iwl_nic_init(priv);
  586. if (ret) {
  587. IWL_ERR(priv, "Unable to init nic\n");
  588. return ret;
  589. }
  590. /* make sure rfkill handshake bits are cleared */
  591. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  592. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  593. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  594. /* clear (again), then enable host interrupts */
  595. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  596. iwl_enable_interrupts(priv);
  597. /* really make sure rfkill handshake bits are cleared */
  598. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  599. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  600. return 0;
  601. }
  602. /*
  603. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  604. * must be called under priv->lock and mac access
  605. */
  606. static void iwl_trans_txq_set_sched(struct iwl_priv *priv, u32 mask)
  607. {
  608. iwl_write_prph(priv, SCD_TXFACT, mask);
  609. }
  610. #define IWL_AC_UNSET -1
  611. struct queue_to_fifo_ac {
  612. s8 fifo, ac;
  613. };
  614. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  615. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  616. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  617. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  618. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  619. { IWLAGN_CMD_FIFO_NUM, 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. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  625. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  626. };
  627. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  628. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  629. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  630. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  631. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  632. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  633. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  634. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  635. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  636. { IWL_TX_FIFO_BE_IPAN, 2, },
  637. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  638. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  639. };
  640. static void iwl_trans_tx_start(struct iwl_priv *priv)
  641. {
  642. const struct queue_to_fifo_ac *queue_to_fifo;
  643. struct iwl_rxon_context *ctx;
  644. u32 a;
  645. unsigned long flags;
  646. int i, chan;
  647. u32 reg_val;
  648. spin_lock_irqsave(&priv->lock, flags);
  649. priv->scd_base_addr = iwl_read_prph(priv, SCD_SRAM_BASE_ADDR);
  650. a = priv->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  651. /* reset conext data memory */
  652. for (; a < priv->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  653. a += 4)
  654. iwl_write_targ_mem(priv, a, 0);
  655. /* reset tx status memory */
  656. for (; a < priv->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  657. a += 4)
  658. iwl_write_targ_mem(priv, a, 0);
  659. for (; a < priv->scd_base_addr +
  660. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(priv).max_txq_num);
  661. a += 4)
  662. iwl_write_targ_mem(priv, a, 0);
  663. iwl_write_prph(priv, SCD_DRAM_BASE_ADDR,
  664. priv->scd_bc_tbls.dma >> 10);
  665. /* Enable DMA channel */
  666. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  667. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  668. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  669. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  670. /* Update FH chicken bits */
  671. reg_val = iwl_read_direct32(priv, FH_TX_CHICKEN_BITS_REG);
  672. iwl_write_direct32(priv, FH_TX_CHICKEN_BITS_REG,
  673. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  674. iwl_write_prph(priv, SCD_QUEUECHAIN_SEL,
  675. SCD_QUEUECHAIN_SEL_ALL(priv));
  676. iwl_write_prph(priv, SCD_AGGR_SEL, 0);
  677. /* initiate the queues */
  678. for (i = 0; i < hw_params(priv).max_txq_num; i++) {
  679. iwl_write_prph(priv, SCD_QUEUE_RDPTR(i), 0);
  680. iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  681. iwl_write_targ_mem(priv, priv->scd_base_addr +
  682. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  683. iwl_write_targ_mem(priv, priv->scd_base_addr +
  684. SCD_CONTEXT_QUEUE_OFFSET(i) +
  685. sizeof(u32),
  686. ((SCD_WIN_SIZE <<
  687. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  688. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  689. ((SCD_FRAME_LIMIT <<
  690. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  691. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  692. }
  693. iwl_write_prph(priv, SCD_INTERRUPT_MASK,
  694. IWL_MASK(0, hw_params(priv).max_txq_num));
  695. /* Activate all Tx DMA/FIFO channels */
  696. iwl_trans_txq_set_sched(priv, IWL_MASK(0, 7));
  697. /* map queues to FIFOs */
  698. if (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  699. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  700. else
  701. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  702. iwl_trans_set_wr_ptrs(priv, priv->shrd->cmd_queue, 0);
  703. /* make sure all queue are not stopped */
  704. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  705. for (i = 0; i < 4; i++)
  706. atomic_set(&priv->queue_stop_count[i], 0);
  707. for_each_context(priv, ctx)
  708. ctx->last_tx_rejected = false;
  709. /* reset to 0 to enable all the queue first */
  710. priv->txq_ctx_active_msk = 0;
  711. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) !=
  712. IWLAGN_FIRST_AMPDU_QUEUE);
  713. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) !=
  714. IWLAGN_FIRST_AMPDU_QUEUE);
  715. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  716. int fifo = queue_to_fifo[i].fifo;
  717. int ac = queue_to_fifo[i].ac;
  718. iwl_txq_ctx_activate(priv, i);
  719. if (fifo == IWL_TX_FIFO_UNUSED)
  720. continue;
  721. if (ac != IWL_AC_UNSET)
  722. iwl_set_swq_id(&priv->txq[i], ac, i);
  723. iwl_trans_tx_queue_set_status(priv, &priv->txq[i], fifo, 0);
  724. }
  725. spin_unlock_irqrestore(&priv->lock, flags);
  726. /* Enable L1-Active */
  727. iwl_clear_bits_prph(priv, APMG_PCIDEV_STT_REG,
  728. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  729. }
  730. /**
  731. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  732. */
  733. static int iwl_trans_tx_stop(struct iwl_priv *priv)
  734. {
  735. int ch, txq_id;
  736. unsigned long flags;
  737. /* Turn off all Tx DMA fifos */
  738. spin_lock_irqsave(&priv->lock, flags);
  739. iwl_trans_txq_set_sched(priv, 0);
  740. /* Stop each Tx DMA channel, and wait for it to be idle */
  741. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  742. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  743. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  744. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  745. 1000))
  746. IWL_ERR(priv, "Failing on timeout while stopping"
  747. " DMA channel %d [0x%08x]", ch,
  748. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  749. }
  750. spin_unlock_irqrestore(&priv->lock, flags);
  751. if (!priv->txq) {
  752. IWL_WARN(priv, "Stopping tx queues that aren't allocated...");
  753. return 0;
  754. }
  755. /* Unmap DMA from host system and free skb's */
  756. for (txq_id = 0; txq_id < hw_params(priv).max_txq_num; txq_id++)
  757. iwl_tx_queue_unmap(priv, txq_id);
  758. return 0;
  759. }
  760. static void iwl_trans_stop_device(struct iwl_priv *priv)
  761. {
  762. unsigned long flags;
  763. /* stop and reset the on-board processor */
  764. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  765. /* tell the device to stop sending interrupts */
  766. spin_lock_irqsave(&priv->lock, flags);
  767. iwl_disable_interrupts(priv);
  768. spin_unlock_irqrestore(&priv->lock, flags);
  769. trans_sync_irq(&priv->trans);
  770. /* device going down, Stop using ICT table */
  771. iwl_disable_ict(priv);
  772. /*
  773. * If a HW restart happens during firmware loading,
  774. * then the firmware loading might call this function
  775. * and later it might be called again due to the
  776. * restart. So don't process again if the device is
  777. * already dead.
  778. */
  779. if (test_bit(STATUS_DEVICE_ENABLED, &priv->status)) {
  780. iwl_trans_tx_stop(priv);
  781. iwl_trans_rx_stop(priv);
  782. /* Power-down device's busmaster DMA clocks */
  783. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  784. APMG_CLK_VAL_DMA_CLK_RQT);
  785. udelay(5);
  786. }
  787. /* Make sure (redundant) we've released our request to stay awake */
  788. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  789. /* Stop the device, and put it in low power state */
  790. iwl_apm_stop(priv);
  791. }
  792. static struct iwl_tx_cmd *iwl_trans_get_tx_cmd(struct iwl_priv *priv,
  793. int txq_id)
  794. {
  795. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  796. struct iwl_queue *q = &txq->q;
  797. struct iwl_device_cmd *dev_cmd;
  798. if (unlikely(iwl_queue_space(q) < q->high_mark))
  799. return NULL;
  800. /*
  801. * Set up the Tx-command (not MAC!) header.
  802. * Store the chosen Tx queue and TFD index within the sequence field;
  803. * after Tx, uCode's Tx response will return this value so driver can
  804. * locate the frame within the tx queue and do post-tx processing.
  805. */
  806. dev_cmd = txq->cmd[q->write_ptr];
  807. memset(dev_cmd, 0, sizeof(*dev_cmd));
  808. dev_cmd->hdr.cmd = REPLY_TX;
  809. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  810. INDEX_TO_SEQ(q->write_ptr)));
  811. return &dev_cmd->cmd.tx;
  812. }
  813. static int iwl_trans_tx(struct iwl_priv *priv, struct sk_buff *skb,
  814. struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
  815. struct iwl_rxon_context *ctx)
  816. {
  817. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  818. struct iwl_queue *q = &txq->q;
  819. struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
  820. struct iwl_cmd_meta *out_meta;
  821. dma_addr_t phys_addr = 0;
  822. dma_addr_t txcmd_phys;
  823. dma_addr_t scratch_phys;
  824. u16 len, firstlen, secondlen;
  825. u8 wait_write_ptr = 0;
  826. u8 hdr_len = ieee80211_hdrlen(fc);
  827. /* Set up driver data for this TFD */
  828. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  829. txq->txb[q->write_ptr].skb = skb;
  830. txq->txb[q->write_ptr].ctx = ctx;
  831. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  832. out_meta = &txq->meta[q->write_ptr];
  833. /*
  834. * Use the first empty entry in this queue's command buffer array
  835. * to contain the Tx command and MAC header concatenated together
  836. * (payload data will be in another buffer).
  837. * Size of this varies, due to varying MAC header length.
  838. * If end is not dword aligned, we'll have 2 extra bytes at the end
  839. * of the MAC header (device reads on dword boundaries).
  840. * We'll tell device about this padding later.
  841. */
  842. len = sizeof(struct iwl_tx_cmd) +
  843. sizeof(struct iwl_cmd_header) + hdr_len;
  844. firstlen = (len + 3) & ~3;
  845. /* Tell NIC about any 2-byte padding after MAC header */
  846. if (firstlen != len)
  847. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  848. /* Physical address of this Tx command's header (not MAC header!),
  849. * within command buffer array. */
  850. txcmd_phys = dma_map_single(priv->bus->dev,
  851. &dev_cmd->hdr, firstlen,
  852. DMA_BIDIRECTIONAL);
  853. if (unlikely(dma_mapping_error(priv->bus->dev, txcmd_phys)))
  854. return -1;
  855. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  856. dma_unmap_len_set(out_meta, len, firstlen);
  857. if (!ieee80211_has_morefrags(fc)) {
  858. txq->need_update = 1;
  859. } else {
  860. wait_write_ptr = 1;
  861. txq->need_update = 0;
  862. }
  863. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  864. * if any (802.11 null frames have no payload). */
  865. secondlen = skb->len - hdr_len;
  866. if (secondlen > 0) {
  867. phys_addr = dma_map_single(priv->bus->dev, skb->data + hdr_len,
  868. secondlen, DMA_TO_DEVICE);
  869. if (unlikely(dma_mapping_error(priv->bus->dev, phys_addr))) {
  870. dma_unmap_single(priv->bus->dev,
  871. dma_unmap_addr(out_meta, mapping),
  872. dma_unmap_len(out_meta, len),
  873. DMA_BIDIRECTIONAL);
  874. return -1;
  875. }
  876. }
  877. /* Attach buffers to TFD */
  878. iwlagn_txq_attach_buf_to_tfd(priv, txq, txcmd_phys, firstlen, 1);
  879. if (secondlen > 0)
  880. iwlagn_txq_attach_buf_to_tfd(priv, txq, phys_addr,
  881. secondlen, 0);
  882. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  883. offsetof(struct iwl_tx_cmd, scratch);
  884. /* take back ownership of DMA buffer to enable update */
  885. dma_sync_single_for_cpu(priv->bus->dev, txcmd_phys, firstlen,
  886. DMA_BIDIRECTIONAL);
  887. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  888. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  889. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  890. le16_to_cpu(dev_cmd->hdr.sequence));
  891. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  892. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  893. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  894. /* Set up entry for this TFD in Tx byte-count array */
  895. if (ampdu)
  896. iwl_trans_txq_update_byte_cnt_tbl(priv, txq,
  897. le16_to_cpu(tx_cmd->len));
  898. dma_sync_single_for_device(priv->bus->dev, txcmd_phys, firstlen,
  899. DMA_BIDIRECTIONAL);
  900. trace_iwlwifi_dev_tx(priv,
  901. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  902. sizeof(struct iwl_tfd),
  903. &dev_cmd->hdr, firstlen,
  904. skb->data + hdr_len, secondlen);
  905. /* Tell device the write index *just past* this latest filled TFD */
  906. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  907. iwl_txq_update_write_ptr(priv, txq);
  908. /*
  909. * At this point the frame is "transmitted" successfully
  910. * and we will get a TX status notification eventually,
  911. * regardless of the value of ret. "ret" only indicates
  912. * whether or not we should update the write pointer.
  913. */
  914. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  915. if (wait_write_ptr) {
  916. txq->need_update = 1;
  917. iwl_txq_update_write_ptr(priv, txq);
  918. } else {
  919. iwl_stop_queue(priv, txq);
  920. }
  921. }
  922. return 0;
  923. }
  924. static void iwl_trans_kick_nic(struct iwl_priv *priv)
  925. {
  926. /* Remove all resets to allow NIC to operate */
  927. iwl_write32(priv, CSR_RESET, 0);
  928. }
  929. static void iwl_trans_sync_irq(struct iwl_priv *priv)
  930. {
  931. /* wait to make sure we flush pending tasklet*/
  932. synchronize_irq(priv->bus->irq);
  933. tasklet_kill(&priv->irq_tasklet);
  934. }
  935. static void iwl_trans_free(struct iwl_priv *priv)
  936. {
  937. free_irq(priv->bus->irq, priv);
  938. iwl_free_isr_ict(priv);
  939. }
  940. static const struct iwl_trans_ops trans_ops = {
  941. .start_device = iwl_trans_start_device,
  942. .prepare_card_hw = iwl_trans_prepare_card_hw,
  943. .stop_device = iwl_trans_stop_device,
  944. .tx_start = iwl_trans_tx_start,
  945. .rx_free = iwl_trans_rx_free,
  946. .tx_free = iwl_trans_tx_free,
  947. .send_cmd = iwl_send_cmd,
  948. .send_cmd_pdu = iwl_send_cmd_pdu,
  949. .get_tx_cmd = iwl_trans_get_tx_cmd,
  950. .tx = iwl_trans_tx,
  951. .txq_agg_disable = iwl_trans_txq_agg_disable,
  952. .txq_agg_setup = iwl_trans_txq_agg_setup,
  953. .kick_nic = iwl_trans_kick_nic,
  954. .sync_irq = iwl_trans_sync_irq,
  955. .free = iwl_trans_free,
  956. };
  957. int iwl_trans_register(struct iwl_trans *trans, struct iwl_priv *priv)
  958. {
  959. int err;
  960. priv->trans.ops = &trans_ops;
  961. priv->trans.priv = priv;
  962. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  963. iwl_irq_tasklet, (unsigned long)priv);
  964. iwl_alloc_isr_ict(priv);
  965. err = request_irq(priv->bus->irq, iwl_isr_ict, IRQF_SHARED,
  966. DRV_NAME, priv);
  967. if (err) {
  968. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->bus->irq);
  969. iwl_free_isr_ict(priv);
  970. return err;
  971. }
  972. INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
  973. return 0;
  974. }