iwl-trans.c 40 KB

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