iwl-trans.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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 <linux/bitops.h>
  66. #include <linux/gfp.h>
  67. #include "iwl-dev.h"
  68. #include "iwl-trans.h"
  69. #include "iwl-core.h"
  70. #include "iwl-helpers.h"
  71. #include "iwl-trans-int-pcie.h"
  72. /*TODO remove uneeded includes when the transport layer tx_free will be here */
  73. #include "iwl-agn.h"
  74. #include "iwl-shared.h"
  75. static int iwl_trans_rx_alloc(struct iwl_trans *trans)
  76. {
  77. struct iwl_trans_pcie *trans_pcie =
  78. IWL_TRANS_GET_PCIE_TRANS(trans);
  79. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  80. struct device *dev = bus(trans)->dev;
  81. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  82. spin_lock_init(&rxq->lock);
  83. INIT_LIST_HEAD(&rxq->rx_free);
  84. INIT_LIST_HEAD(&rxq->rx_used);
  85. if (WARN_ON(rxq->bd || rxq->rb_stts))
  86. return -EINVAL;
  87. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  88. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  89. &rxq->bd_dma, GFP_KERNEL);
  90. if (!rxq->bd)
  91. goto err_bd;
  92. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  93. /*Allocate the driver's pointer to receive buffer status */
  94. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  95. &rxq->rb_stts_dma, GFP_KERNEL);
  96. if (!rxq->rb_stts)
  97. goto err_rb_stts;
  98. memset(rxq->rb_stts, 0, sizeof(*rxq->rb_stts));
  99. return 0;
  100. err_rb_stts:
  101. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  102. rxq->bd, rxq->bd_dma);
  103. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  104. rxq->bd = NULL;
  105. err_bd:
  106. return -ENOMEM;
  107. }
  108. static void iwl_trans_rxq_free_rx_bufs(struct iwl_trans *trans)
  109. {
  110. struct iwl_trans_pcie *trans_pcie =
  111. IWL_TRANS_GET_PCIE_TRANS(trans);
  112. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  113. int i;
  114. /* Fill the rx_used queue with _all_ of the Rx buffers */
  115. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  116. /* In the reset function, these buffers may have been allocated
  117. * to an SKB, so we need to unmap and free potential storage */
  118. if (rxq->pool[i].page != NULL) {
  119. dma_unmap_page(bus(trans)->dev, rxq->pool[i].page_dma,
  120. PAGE_SIZE << hw_params(trans).rx_page_order,
  121. DMA_FROM_DEVICE);
  122. __free_pages(rxq->pool[i].page,
  123. hw_params(trans).rx_page_order);
  124. rxq->pool[i].page = NULL;
  125. }
  126. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  127. }
  128. }
  129. static void iwl_trans_rx_hw_init(struct iwl_priv *priv,
  130. struct iwl_rx_queue *rxq)
  131. {
  132. u32 rb_size;
  133. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  134. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  135. rb_timeout = RX_RB_TIMEOUT;
  136. if (iwlagn_mod_params.amsdu_size_8K)
  137. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  138. else
  139. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  140. /* Stop Rx DMA */
  141. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  142. /* Reset driver's Rx queue write index */
  143. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  144. /* Tell device where to find RBD circular buffer in DRAM */
  145. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  146. (u32)(rxq->bd_dma >> 8));
  147. /* Tell device where in DRAM to update its Rx status */
  148. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  149. rxq->rb_stts_dma >> 4);
  150. /* Enable Rx DMA
  151. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  152. * the credit mechanism in 5000 HW RX FIFO
  153. * Direct rx interrupts to hosts
  154. * Rx buffer size 4 or 8k
  155. * RB timeout 0x10
  156. * 256 RBDs
  157. */
  158. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  159. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  160. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  161. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  162. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  163. rb_size|
  164. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  165. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  166. /* Set interrupt coalescing timer to default (2048 usecs) */
  167. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  168. }
  169. static int iwl_rx_init(struct iwl_trans *trans)
  170. {
  171. struct iwl_trans_pcie *trans_pcie =
  172. IWL_TRANS_GET_PCIE_TRANS(trans);
  173. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  174. int i, err;
  175. unsigned long flags;
  176. if (!rxq->bd) {
  177. err = iwl_trans_rx_alloc(trans);
  178. if (err)
  179. return err;
  180. }
  181. spin_lock_irqsave(&rxq->lock, flags);
  182. INIT_LIST_HEAD(&rxq->rx_free);
  183. INIT_LIST_HEAD(&rxq->rx_used);
  184. iwl_trans_rxq_free_rx_bufs(trans);
  185. for (i = 0; i < RX_QUEUE_SIZE; i++)
  186. rxq->queue[i] = NULL;
  187. /* Set us so that we have processed and used all buffers, but have
  188. * not restocked the Rx queue with fresh buffers */
  189. rxq->read = rxq->write = 0;
  190. rxq->write_actual = 0;
  191. rxq->free_count = 0;
  192. spin_unlock_irqrestore(&rxq->lock, flags);
  193. iwlagn_rx_replenish(trans);
  194. iwl_trans_rx_hw_init(priv(trans), rxq);
  195. spin_lock_irqsave(&trans->shrd->lock, flags);
  196. rxq->need_update = 1;
  197. iwl_rx_queue_update_write_ptr(trans, rxq);
  198. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  199. return 0;
  200. }
  201. static void iwl_trans_pcie_rx_free(struct iwl_trans *trans)
  202. {
  203. struct iwl_trans_pcie *trans_pcie =
  204. IWL_TRANS_GET_PCIE_TRANS(trans);
  205. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  206. unsigned long flags;
  207. /*if rxq->bd is NULL, it means that nothing has been allocated,
  208. * exit now */
  209. if (!rxq->bd) {
  210. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  211. return;
  212. }
  213. spin_lock_irqsave(&rxq->lock, flags);
  214. iwl_trans_rxq_free_rx_bufs(trans);
  215. spin_unlock_irqrestore(&rxq->lock, flags);
  216. dma_free_coherent(bus(trans)->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  217. rxq->bd, rxq->bd_dma);
  218. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  219. rxq->bd = NULL;
  220. if (rxq->rb_stts)
  221. dma_free_coherent(bus(trans)->dev,
  222. sizeof(struct iwl_rb_status),
  223. rxq->rb_stts, rxq->rb_stts_dma);
  224. else
  225. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  226. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  227. rxq->rb_stts = NULL;
  228. }
  229. static int iwl_trans_rx_stop(struct iwl_trans *trans)
  230. {
  231. /* stop Rx DMA */
  232. iwl_write_direct32(priv(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  233. return iwl_poll_direct_bit(priv(trans), FH_MEM_RSSR_RX_STATUS_REG,
  234. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  235. }
  236. static inline int iwlagn_alloc_dma_ptr(struct iwl_trans *trans,
  237. struct iwl_dma_ptr *ptr, size_t size)
  238. {
  239. if (WARN_ON(ptr->addr))
  240. return -EINVAL;
  241. ptr->addr = dma_alloc_coherent(bus(trans)->dev, size,
  242. &ptr->dma, GFP_KERNEL);
  243. if (!ptr->addr)
  244. return -ENOMEM;
  245. ptr->size = size;
  246. return 0;
  247. }
  248. static inline void iwlagn_free_dma_ptr(struct iwl_trans *trans,
  249. struct iwl_dma_ptr *ptr)
  250. {
  251. if (unlikely(!ptr->addr))
  252. return;
  253. dma_free_coherent(bus(trans)->dev, ptr->size, ptr->addr, ptr->dma);
  254. memset(ptr, 0, sizeof(*ptr));
  255. }
  256. static int iwl_trans_txq_alloc(struct iwl_trans *trans,
  257. struct iwl_tx_queue *txq, int slots_num,
  258. u32 txq_id)
  259. {
  260. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  261. int i;
  262. if (WARN_ON(txq->meta || txq->cmd || txq->txb || txq->tfds))
  263. return -EINVAL;
  264. txq->q.n_window = slots_num;
  265. txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
  266. GFP_KERNEL);
  267. txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
  268. GFP_KERNEL);
  269. if (!txq->meta || !txq->cmd)
  270. goto error;
  271. for (i = 0; i < slots_num; i++) {
  272. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  273. GFP_KERNEL);
  274. if (!txq->cmd[i])
  275. goto error;
  276. }
  277. /* Alloc driver data array and TFD circular buffer */
  278. /* Driver private data, only for Tx (not command) queues,
  279. * not shared with device. */
  280. if (txq_id != trans->shrd->cmd_queue) {
  281. txq->txb = kzalloc(sizeof(txq->txb[0]) *
  282. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  283. if (!txq->txb) {
  284. IWL_ERR(trans, "kmalloc for auxiliary BD "
  285. "structures failed\n");
  286. goto error;
  287. }
  288. } else {
  289. txq->txb = NULL;
  290. }
  291. /* Circular buffer of transmit frame descriptors (TFDs),
  292. * shared with device */
  293. txq->tfds = dma_alloc_coherent(bus(trans)->dev, tfd_sz,
  294. &txq->q.dma_addr, GFP_KERNEL);
  295. if (!txq->tfds) {
  296. IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  297. goto error;
  298. }
  299. txq->q.id = txq_id;
  300. return 0;
  301. error:
  302. kfree(txq->txb);
  303. txq->txb = NULL;
  304. /* since txq->cmd has been zeroed,
  305. * all non allocated cmd[i] will be NULL */
  306. if (txq->cmd)
  307. for (i = 0; i < slots_num; i++)
  308. kfree(txq->cmd[i]);
  309. kfree(txq->meta);
  310. kfree(txq->cmd);
  311. txq->meta = NULL;
  312. txq->cmd = NULL;
  313. return -ENOMEM;
  314. }
  315. static int iwl_trans_txq_init(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  316. int slots_num, u32 txq_id)
  317. {
  318. int ret;
  319. txq->need_update = 0;
  320. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  321. /*
  322. * For the default queues 0-3, set up the swq_id
  323. * already -- all others need to get one later
  324. * (if they need one at all).
  325. */
  326. if (txq_id < 4)
  327. iwl_set_swq_id(txq, txq_id, txq_id);
  328. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  329. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  330. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  331. /* Initialize queue's high/low-water marks, and head/tail indexes */
  332. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  333. txq_id);
  334. if (ret)
  335. return ret;
  336. /*
  337. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  338. * given Tx queue, and enable the DMA channel used for that queue.
  339. * Circular buffer (TFD queue in DRAM) physical base address */
  340. iwl_write_direct32(priv(trans), FH_MEM_CBBC_QUEUE(txq_id),
  341. txq->q.dma_addr >> 8);
  342. return 0;
  343. }
  344. /**
  345. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  346. */
  347. static void iwl_tx_queue_unmap(struct iwl_trans *trans, int txq_id)
  348. {
  349. struct iwl_priv *priv = priv(trans);
  350. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  351. struct iwl_queue *q = &txq->q;
  352. if (!q->n_bd)
  353. return;
  354. while (q->write_ptr != q->read_ptr) {
  355. /* The read_ptr needs to bound by q->n_window */
  356. iwlagn_txq_free_tfd(trans, txq, get_cmd_index(q, q->read_ptr));
  357. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  358. }
  359. }
  360. /**
  361. * iwl_tx_queue_free - Deallocate DMA queue.
  362. * @txq: Transmit queue to deallocate.
  363. *
  364. * Empty queue by removing and destroying all BD's.
  365. * Free all buffers.
  366. * 0-fill, but do not free "txq" descriptor structure.
  367. */
  368. static void iwl_tx_queue_free(struct iwl_trans *trans, int txq_id)
  369. {
  370. struct iwl_priv *priv = priv(trans);
  371. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  372. struct device *dev = bus(trans)->dev;
  373. int i;
  374. if (WARN_ON(!txq))
  375. return;
  376. iwl_tx_queue_unmap(trans, txq_id);
  377. /* De-alloc array of command/tx buffers */
  378. for (i = 0; i < txq->q.n_window; i++)
  379. kfree(txq->cmd[i]);
  380. /* De-alloc circular buffer of TFDs */
  381. if (txq->q.n_bd) {
  382. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  383. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  384. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  385. }
  386. /* De-alloc array of per-TFD driver data */
  387. kfree(txq->txb);
  388. txq->txb = NULL;
  389. /* deallocate arrays */
  390. kfree(txq->cmd);
  391. kfree(txq->meta);
  392. txq->cmd = NULL;
  393. txq->meta = NULL;
  394. /* 0-fill queue descriptor structure */
  395. memset(txq, 0, sizeof(*txq));
  396. }
  397. /**
  398. * iwl_trans_tx_free - Free TXQ Context
  399. *
  400. * Destroy all TX DMA queues and structures
  401. */
  402. static void iwl_trans_pcie_tx_free(struct iwl_trans *trans)
  403. {
  404. int txq_id;
  405. struct iwl_trans_pcie *trans_pcie =
  406. IWL_TRANS_GET_PCIE_TRANS(trans);
  407. struct iwl_priv *priv = priv(trans);
  408. /* Tx queues */
  409. if (priv->txq) {
  410. for (txq_id = 0;
  411. txq_id < hw_params(trans).max_txq_num; txq_id++)
  412. iwl_tx_queue_free(trans, txq_id);
  413. }
  414. kfree(priv->txq);
  415. priv->txq = NULL;
  416. iwlagn_free_dma_ptr(trans, &trans_pcie->kw);
  417. iwlagn_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  418. }
  419. /**
  420. * iwl_trans_tx_alloc - allocate TX context
  421. * Allocate all Tx DMA structures and initialize them
  422. *
  423. * @param priv
  424. * @return error code
  425. */
  426. static int iwl_trans_tx_alloc(struct iwl_trans *trans)
  427. {
  428. int ret;
  429. int txq_id, slots_num;
  430. struct iwl_priv *priv = priv(trans);
  431. struct iwl_trans_pcie *trans_pcie =
  432. IWL_TRANS_GET_PCIE_TRANS(trans);
  433. u16 scd_bc_tbls_size = priv->cfg->base_params->num_of_queues *
  434. sizeof(struct iwlagn_scd_bc_tbl);
  435. /*It is not allowed to alloc twice, so warn when this happens.
  436. * We cannot rely on the previous allocation, so free and fail */
  437. if (WARN_ON(priv->txq)) {
  438. ret = -EINVAL;
  439. goto error;
  440. }
  441. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  442. scd_bc_tbls_size);
  443. if (ret) {
  444. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  445. goto error;
  446. }
  447. /* Alloc keep-warm buffer */
  448. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  449. if (ret) {
  450. IWL_ERR(trans, "Keep Warm allocation failed\n");
  451. goto error;
  452. }
  453. priv->txq = kzalloc(sizeof(struct iwl_tx_queue) *
  454. priv->cfg->base_params->num_of_queues, GFP_KERNEL);
  455. if (!priv->txq) {
  456. IWL_ERR(trans, "Not enough memory for txq\n");
  457. ret = ENOMEM;
  458. goto error;
  459. }
  460. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  461. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  462. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  463. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  464. ret = iwl_trans_txq_alloc(trans, &priv->txq[txq_id], slots_num,
  465. txq_id);
  466. if (ret) {
  467. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  468. goto error;
  469. }
  470. }
  471. return 0;
  472. error:
  473. iwl_trans_tx_free(trans);
  474. return ret;
  475. }
  476. static int iwl_tx_init(struct iwl_trans *trans)
  477. {
  478. int ret;
  479. int txq_id, slots_num;
  480. unsigned long flags;
  481. bool alloc = false;
  482. struct iwl_priv *priv = priv(trans);
  483. struct iwl_trans_pcie *trans_pcie =
  484. IWL_TRANS_GET_PCIE_TRANS(trans);
  485. if (!priv->txq) {
  486. ret = iwl_trans_tx_alloc(trans);
  487. if (ret)
  488. goto error;
  489. alloc = true;
  490. }
  491. spin_lock_irqsave(&trans->shrd->lock, flags);
  492. /* Turn off all Tx DMA fifos */
  493. iwl_write_prph(priv, SCD_TXFACT, 0);
  494. /* Tell NIC where to find the "keep warm" buffer */
  495. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, trans_pcie->kw.dma >> 4);
  496. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  497. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  498. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  499. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  500. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  501. ret = iwl_trans_txq_init(trans, &priv->txq[txq_id], slots_num,
  502. txq_id);
  503. if (ret) {
  504. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  505. goto error;
  506. }
  507. }
  508. return 0;
  509. error:
  510. /*Upon error, free only if we allocated something */
  511. if (alloc)
  512. iwl_trans_tx_free(trans);
  513. return ret;
  514. }
  515. static void iwl_set_pwr_vmain(struct iwl_priv *priv)
  516. {
  517. /*
  518. * (for documentation purposes)
  519. * to set power to V_AUX, do:
  520. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  521. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  522. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  523. ~APMG_PS_CTRL_MSK_PWR_SRC);
  524. */
  525. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  526. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  527. ~APMG_PS_CTRL_MSK_PWR_SRC);
  528. }
  529. static int iwl_nic_init(struct iwl_trans *trans)
  530. {
  531. unsigned long flags;
  532. struct iwl_priv *priv = priv(trans);
  533. /* nic_init */
  534. spin_lock_irqsave(&trans->shrd->lock, flags);
  535. iwl_apm_init(priv);
  536. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  537. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  538. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  539. iwl_set_pwr_vmain(priv);
  540. priv->cfg->lib->nic_config(priv);
  541. /* Allocate the RX queue, or reset if it is already allocated */
  542. iwl_rx_init(trans);
  543. /* Allocate or reset and init all Tx and Command queues */
  544. if (iwl_tx_init(trans))
  545. return -ENOMEM;
  546. if (priv->cfg->base_params->shadow_reg_enable) {
  547. /* enable shadow regs in HW */
  548. iwl_set_bit(priv, CSR_MAC_SHADOW_REG_CTRL,
  549. 0x800FFFFF);
  550. }
  551. set_bit(STATUS_INIT, &trans->shrd->status);
  552. return 0;
  553. }
  554. #define HW_READY_TIMEOUT (50)
  555. /* Note: returns poll_bit return value, which is >= 0 if success */
  556. static int iwl_set_hw_ready(struct iwl_trans *trans)
  557. {
  558. int ret;
  559. iwl_set_bit(priv(trans), CSR_HW_IF_CONFIG_REG,
  560. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  561. /* See if we got it */
  562. ret = iwl_poll_bit(priv(trans), CSR_HW_IF_CONFIG_REG,
  563. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  564. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  565. HW_READY_TIMEOUT);
  566. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  567. return ret;
  568. }
  569. /* Note: returns standard 0/-ERROR code */
  570. static int iwl_trans_pcie_prepare_card_hw(struct iwl_trans *trans)
  571. {
  572. int ret;
  573. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  574. ret = iwl_set_hw_ready(trans);
  575. if (ret >= 0)
  576. return 0;
  577. /* If HW is not ready, prepare the conditions to check again */
  578. iwl_set_bit(priv(trans), CSR_HW_IF_CONFIG_REG,
  579. CSR_HW_IF_CONFIG_REG_PREPARE);
  580. ret = iwl_poll_bit(priv(trans), CSR_HW_IF_CONFIG_REG,
  581. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  582. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  583. if (ret < 0)
  584. return ret;
  585. /* HW should be ready by now, check again. */
  586. ret = iwl_set_hw_ready(trans);
  587. if (ret >= 0)
  588. return 0;
  589. return ret;
  590. }
  591. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  592. {
  593. int ret;
  594. struct iwl_priv *priv = priv(trans);
  595. priv->ucode_owner = IWL_OWNERSHIP_DRIVER;
  596. if ((priv->cfg->sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  597. iwl_trans_pcie_prepare_card_hw(trans)) {
  598. IWL_WARN(trans, "Exit HW not ready\n");
  599. return -EIO;
  600. }
  601. /* If platform's RF_KILL switch is NOT set to KILL */
  602. if (iwl_read32(priv, CSR_GP_CNTRL) &
  603. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  604. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  605. else
  606. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  607. if (iwl_is_rfkill(trans->shrd)) {
  608. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  609. iwl_enable_interrupts(trans);
  610. return -ERFKILL;
  611. }
  612. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  613. ret = iwl_nic_init(trans);
  614. if (ret) {
  615. IWL_ERR(trans, "Unable to init nic\n");
  616. return ret;
  617. }
  618. /* make sure rfkill handshake bits are cleared */
  619. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  620. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  621. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  622. /* clear (again), then enable host interrupts */
  623. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  624. iwl_enable_interrupts(trans);
  625. /* really make sure rfkill handshake bits are cleared */
  626. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  627. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  628. return 0;
  629. }
  630. /*
  631. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  632. * must be called under priv->shrd->lock and mac access
  633. */
  634. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  635. {
  636. iwl_write_prph(priv(trans), SCD_TXFACT, mask);
  637. }
  638. #define IWL_AC_UNSET -1
  639. struct queue_to_fifo_ac {
  640. s8 fifo, ac;
  641. };
  642. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  643. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  644. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  645. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  646. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  647. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  648. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  649. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  650. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  651. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  652. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  653. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  654. };
  655. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  656. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  657. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  658. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  659. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  660. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  661. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  662. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  663. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  664. { IWL_TX_FIFO_BE_IPAN, 2, },
  665. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  666. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  667. };
  668. static void iwl_trans_pcie_tx_start(struct iwl_trans *trans)
  669. {
  670. const struct queue_to_fifo_ac *queue_to_fifo;
  671. struct iwl_rxon_context *ctx;
  672. struct iwl_priv *priv = priv(trans);
  673. struct iwl_trans_pcie *trans_pcie =
  674. IWL_TRANS_GET_PCIE_TRANS(trans);
  675. u32 a;
  676. unsigned long flags;
  677. int i, chan;
  678. u32 reg_val;
  679. spin_lock_irqsave(&trans->shrd->lock, flags);
  680. trans_pcie->scd_base_addr = iwl_read_prph(priv, SCD_SRAM_BASE_ADDR);
  681. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  682. /* reset conext data memory */
  683. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  684. a += 4)
  685. iwl_write_targ_mem(priv, a, 0);
  686. /* reset tx status memory */
  687. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  688. a += 4)
  689. iwl_write_targ_mem(priv, a, 0);
  690. for (; a < trans_pcie->scd_base_addr +
  691. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(priv).max_txq_num);
  692. a += 4)
  693. iwl_write_targ_mem(priv, a, 0);
  694. iwl_write_prph(priv, SCD_DRAM_BASE_ADDR,
  695. trans_pcie->scd_bc_tbls.dma >> 10);
  696. /* Enable DMA channel */
  697. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  698. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  699. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  700. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  701. /* Update FH chicken bits */
  702. reg_val = iwl_read_direct32(priv, FH_TX_CHICKEN_BITS_REG);
  703. iwl_write_direct32(priv, FH_TX_CHICKEN_BITS_REG,
  704. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  705. iwl_write_prph(priv, SCD_QUEUECHAIN_SEL,
  706. SCD_QUEUECHAIN_SEL_ALL(priv));
  707. iwl_write_prph(priv, SCD_AGGR_SEL, 0);
  708. /* initiate the queues */
  709. for (i = 0; i < hw_params(priv).max_txq_num; i++) {
  710. iwl_write_prph(priv, SCD_QUEUE_RDPTR(i), 0);
  711. iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  712. iwl_write_targ_mem(priv, trans_pcie->scd_base_addr +
  713. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  714. iwl_write_targ_mem(priv, trans_pcie->scd_base_addr +
  715. SCD_CONTEXT_QUEUE_OFFSET(i) +
  716. sizeof(u32),
  717. ((SCD_WIN_SIZE <<
  718. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  719. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  720. ((SCD_FRAME_LIMIT <<
  721. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  722. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  723. }
  724. iwl_write_prph(priv, SCD_INTERRUPT_MASK,
  725. IWL_MASK(0, hw_params(trans).max_txq_num));
  726. /* Activate all Tx DMA/FIFO channels */
  727. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  728. /* map queues to FIFOs */
  729. if (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  730. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  731. else
  732. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  733. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  734. /* make sure all queue are not stopped */
  735. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  736. for (i = 0; i < 4; i++)
  737. atomic_set(&priv->queue_stop_count[i], 0);
  738. for_each_context(priv, ctx)
  739. ctx->last_tx_rejected = false;
  740. /* reset to 0 to enable all the queue first */
  741. priv->txq_ctx_active_msk = 0;
  742. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  743. IWLAGN_FIRST_AMPDU_QUEUE);
  744. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  745. IWLAGN_FIRST_AMPDU_QUEUE);
  746. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  747. int fifo = queue_to_fifo[i].fifo;
  748. int ac = queue_to_fifo[i].ac;
  749. iwl_txq_ctx_activate(priv, i);
  750. if (fifo == IWL_TX_FIFO_UNUSED)
  751. continue;
  752. if (ac != IWL_AC_UNSET)
  753. iwl_set_swq_id(&priv->txq[i], ac, i);
  754. iwl_trans_tx_queue_set_status(priv, &priv->txq[i], fifo, 0);
  755. }
  756. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  757. /* Enable L1-Active */
  758. iwl_clear_bits_prph(priv, APMG_PCIDEV_STT_REG,
  759. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  760. }
  761. /**
  762. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  763. */
  764. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  765. {
  766. int ch, txq_id;
  767. unsigned long flags;
  768. struct iwl_priv *priv = priv(trans);
  769. /* Turn off all Tx DMA fifos */
  770. spin_lock_irqsave(&trans->shrd->lock, flags);
  771. iwl_trans_txq_set_sched(trans, 0);
  772. /* Stop each Tx DMA channel, and wait for it to be idle */
  773. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  774. iwl_write_direct32(priv(trans),
  775. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  776. if (iwl_poll_direct_bit(priv(trans), FH_TSSR_TX_STATUS_REG,
  777. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  778. 1000))
  779. IWL_ERR(trans, "Failing on timeout while stopping"
  780. " DMA channel %d [0x%08x]", ch,
  781. iwl_read_direct32(priv(trans),
  782. FH_TSSR_TX_STATUS_REG));
  783. }
  784. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  785. if (!priv->txq) {
  786. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  787. return 0;
  788. }
  789. /* Unmap DMA from host system and free skb's */
  790. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  791. iwl_tx_queue_unmap(trans, txq_id);
  792. return 0;
  793. }
  794. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  795. {
  796. /* stop and reset the on-board processor */
  797. iwl_write32(priv(trans), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  798. /* tell the device to stop sending interrupts */
  799. iwl_trans_disable_sync_irq(trans);
  800. /* device going down, Stop using ICT table */
  801. iwl_disable_ict(trans);
  802. /*
  803. * If a HW restart happens during firmware loading,
  804. * then the firmware loading might call this function
  805. * and later it might be called again due to the
  806. * restart. So don't process again if the device is
  807. * already dead.
  808. */
  809. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  810. iwl_trans_tx_stop(trans);
  811. iwl_trans_rx_stop(trans);
  812. /* Power-down device's busmaster DMA clocks */
  813. iwl_write_prph(priv(trans), APMG_CLK_DIS_REG,
  814. APMG_CLK_VAL_DMA_CLK_RQT);
  815. udelay(5);
  816. }
  817. /* Make sure (redundant) we've released our request to stay awake */
  818. iwl_clear_bit(priv(trans), CSR_GP_CNTRL,
  819. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  820. /* Stop the device, and put it in low power state */
  821. iwl_apm_stop(priv(trans));
  822. }
  823. static struct iwl_tx_cmd *iwl_trans_pcie_get_tx_cmd(struct iwl_trans *trans,
  824. int txq_id)
  825. {
  826. struct iwl_priv *priv = priv(trans);
  827. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  828. struct iwl_queue *q = &txq->q;
  829. struct iwl_device_cmd *dev_cmd;
  830. if (unlikely(iwl_queue_space(q) < q->high_mark))
  831. return NULL;
  832. /*
  833. * Set up the Tx-command (not MAC!) header.
  834. * Store the chosen Tx queue and TFD index within the sequence field;
  835. * after Tx, uCode's Tx response will return this value so driver can
  836. * locate the frame within the tx queue and do post-tx processing.
  837. */
  838. dev_cmd = txq->cmd[q->write_ptr];
  839. memset(dev_cmd, 0, sizeof(*dev_cmd));
  840. dev_cmd->hdr.cmd = REPLY_TX;
  841. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  842. INDEX_TO_SEQ(q->write_ptr)));
  843. return &dev_cmd->cmd.tx;
  844. }
  845. static int iwl_trans_pcie_tx(struct iwl_priv *priv, struct sk_buff *skb,
  846. struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
  847. struct iwl_rxon_context *ctx)
  848. {
  849. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  850. struct iwl_queue *q = &txq->q;
  851. struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
  852. struct iwl_cmd_meta *out_meta;
  853. dma_addr_t phys_addr = 0;
  854. dma_addr_t txcmd_phys;
  855. dma_addr_t scratch_phys;
  856. u16 len, firstlen, secondlen;
  857. u8 wait_write_ptr = 0;
  858. u8 hdr_len = ieee80211_hdrlen(fc);
  859. /* Set up driver data for this TFD */
  860. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  861. txq->txb[q->write_ptr].skb = skb;
  862. txq->txb[q->write_ptr].ctx = ctx;
  863. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  864. out_meta = &txq->meta[q->write_ptr];
  865. /*
  866. * Use the first empty entry in this queue's command buffer array
  867. * to contain the Tx command and MAC header concatenated together
  868. * (payload data will be in another buffer).
  869. * Size of this varies, due to varying MAC header length.
  870. * If end is not dword aligned, we'll have 2 extra bytes at the end
  871. * of the MAC header (device reads on dword boundaries).
  872. * We'll tell device about this padding later.
  873. */
  874. len = sizeof(struct iwl_tx_cmd) +
  875. sizeof(struct iwl_cmd_header) + hdr_len;
  876. firstlen = (len + 3) & ~3;
  877. /* Tell NIC about any 2-byte padding after MAC header */
  878. if (firstlen != len)
  879. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  880. /* Physical address of this Tx command's header (not MAC header!),
  881. * within command buffer array. */
  882. txcmd_phys = dma_map_single(priv->bus->dev,
  883. &dev_cmd->hdr, firstlen,
  884. DMA_BIDIRECTIONAL);
  885. if (unlikely(dma_mapping_error(priv->bus->dev, txcmd_phys)))
  886. return -1;
  887. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  888. dma_unmap_len_set(out_meta, len, firstlen);
  889. if (!ieee80211_has_morefrags(fc)) {
  890. txq->need_update = 1;
  891. } else {
  892. wait_write_ptr = 1;
  893. txq->need_update = 0;
  894. }
  895. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  896. * if any (802.11 null frames have no payload). */
  897. secondlen = skb->len - hdr_len;
  898. if (secondlen > 0) {
  899. phys_addr = dma_map_single(priv->bus->dev, skb->data + hdr_len,
  900. secondlen, DMA_TO_DEVICE);
  901. if (unlikely(dma_mapping_error(priv->bus->dev, phys_addr))) {
  902. dma_unmap_single(priv->bus->dev,
  903. dma_unmap_addr(out_meta, mapping),
  904. dma_unmap_len(out_meta, len),
  905. DMA_BIDIRECTIONAL);
  906. return -1;
  907. }
  908. }
  909. /* Attach buffers to TFD */
  910. iwlagn_txq_attach_buf_to_tfd(trans(priv), txq, txcmd_phys,
  911. firstlen, 1);
  912. if (secondlen > 0)
  913. iwlagn_txq_attach_buf_to_tfd(trans(priv), txq, phys_addr,
  914. secondlen, 0);
  915. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  916. offsetof(struct iwl_tx_cmd, scratch);
  917. /* take back ownership of DMA buffer to enable update */
  918. dma_sync_single_for_cpu(priv->bus->dev, txcmd_phys, firstlen,
  919. DMA_BIDIRECTIONAL);
  920. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  921. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  922. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  923. le16_to_cpu(dev_cmd->hdr.sequence));
  924. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  925. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  926. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  927. /* Set up entry for this TFD in Tx byte-count array */
  928. if (ampdu)
  929. iwl_trans_txq_update_byte_cnt_tbl(trans(priv), txq,
  930. le16_to_cpu(tx_cmd->len));
  931. dma_sync_single_for_device(priv->bus->dev, txcmd_phys, firstlen,
  932. DMA_BIDIRECTIONAL);
  933. trace_iwlwifi_dev_tx(priv,
  934. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  935. sizeof(struct iwl_tfd),
  936. &dev_cmd->hdr, firstlen,
  937. skb->data + hdr_len, secondlen);
  938. /* Tell device the write index *just past* this latest filled TFD */
  939. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  940. iwl_txq_update_write_ptr(priv, txq);
  941. /*
  942. * At this point the frame is "transmitted" successfully
  943. * and we will get a TX status notification eventually,
  944. * regardless of the value of ret. "ret" only indicates
  945. * whether or not we should update the write pointer.
  946. */
  947. if (iwl_queue_space(q) < q->high_mark) {
  948. if (wait_write_ptr) {
  949. txq->need_update = 1;
  950. iwl_txq_update_write_ptr(priv, txq);
  951. } else {
  952. iwl_stop_queue(priv, txq);
  953. }
  954. }
  955. return 0;
  956. }
  957. static void iwl_trans_pcie_kick_nic(struct iwl_trans *trans)
  958. {
  959. /* Remove all resets to allow NIC to operate */
  960. iwl_write32(priv(trans), CSR_RESET, 0);
  961. }
  962. static int iwl_trans_pcie_request_irq(struct iwl_trans *trans)
  963. {
  964. struct iwl_trans_pcie *trans_pcie =
  965. IWL_TRANS_GET_PCIE_TRANS(trans);
  966. int err;
  967. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  968. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  969. iwl_irq_tasklet, (unsigned long)trans);
  970. iwl_alloc_isr_ict(trans);
  971. err = request_irq(bus(trans)->irq, iwl_isr_ict, IRQF_SHARED,
  972. DRV_NAME, trans);
  973. if (err) {
  974. IWL_ERR(trans, "Error allocating IRQ %d\n", bus(trans)->irq);
  975. iwl_free_isr_ict(trans);
  976. return err;
  977. }
  978. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  979. return 0;
  980. }
  981. static void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id,
  982. int ssn, u32 status, struct sk_buff_head *skbs)
  983. {
  984. struct iwl_priv *priv = priv(trans);
  985. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  986. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  987. int tfd_num = ssn & (txq->q.n_bd - 1);
  988. u8 agg_state;
  989. bool cond;
  990. if (txq->sched_retry) {
  991. agg_state =
  992. priv->stations[txq->sta_id].tid[txq->tid].agg.state;
  993. cond = (agg_state != IWL_EMPTYING_HW_QUEUE_DELBA);
  994. } else {
  995. cond = (status != TX_STATUS_FAIL_PASSIVE_NO_RX);
  996. }
  997. if (txq->q.read_ptr != tfd_num) {
  998. IWL_DEBUG_TX_REPLY(trans, "Retry scheduler reclaim "
  999. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  1000. ssn , tfd_num, txq_id, txq->swq_id);
  1001. iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1002. if (iwl_queue_space(&txq->q) > txq->q.low_mark && cond)
  1003. iwl_wake_queue(priv, txq);
  1004. }
  1005. }
  1006. static void iwl_trans_pcie_disable_sync_irq(struct iwl_trans *trans)
  1007. {
  1008. unsigned long flags;
  1009. struct iwl_trans_pcie *trans_pcie =
  1010. IWL_TRANS_GET_PCIE_TRANS(trans);
  1011. spin_lock_irqsave(&trans->shrd->lock, flags);
  1012. iwl_disable_interrupts(trans);
  1013. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1014. /* wait to make sure we flush pending tasklet*/
  1015. synchronize_irq(bus(trans)->irq);
  1016. tasklet_kill(&trans_pcie->irq_tasklet);
  1017. }
  1018. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1019. {
  1020. free_irq(bus(trans)->irq, trans);
  1021. iwl_free_isr_ict(trans);
  1022. trans->shrd->trans = NULL;
  1023. kfree(trans);
  1024. }
  1025. #ifdef CONFIG_PM
  1026. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1027. {
  1028. /*
  1029. * This function is called when system goes into suspend state
  1030. * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
  1031. * first but since iwl_mac_stop() has no knowledge of who the caller is,
  1032. * it will not call apm_ops.stop() to stop the DMA operation.
  1033. * Calling apm_ops.stop here to make sure we stop the DMA.
  1034. *
  1035. * But of course ... if we have configured WoWLAN then we did other
  1036. * things already :-)
  1037. */
  1038. if (!trans->shrd->wowlan)
  1039. iwl_apm_stop(priv(trans));
  1040. return 0;
  1041. }
  1042. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1043. {
  1044. bool hw_rfkill = false;
  1045. iwl_enable_interrupts(trans);
  1046. if (!(iwl_read32(priv(trans), CSR_GP_CNTRL) &
  1047. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1048. hw_rfkill = true;
  1049. if (hw_rfkill)
  1050. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1051. else
  1052. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1053. wiphy_rfkill_set_hw_state(priv(trans)->hw->wiphy, hw_rfkill);
  1054. return 0;
  1055. }
  1056. #else /* CONFIG_PM */
  1057. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1058. { return 0; }
  1059. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1060. { return 0; }
  1061. #endif /* CONFIG_PM */
  1062. const struct iwl_trans_ops trans_ops_pcie;
  1063. static struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd)
  1064. {
  1065. struct iwl_trans *iwl_trans = kzalloc(sizeof(struct iwl_trans) +
  1066. sizeof(struct iwl_trans_pcie),
  1067. GFP_KERNEL);
  1068. if (iwl_trans) {
  1069. struct iwl_trans_pcie *trans_pcie =
  1070. IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
  1071. iwl_trans->ops = &trans_ops_pcie;
  1072. iwl_trans->shrd = shrd;
  1073. trans_pcie->trans = iwl_trans;
  1074. spin_lock_init(&iwl_trans->hcmd_lock);
  1075. }
  1076. return iwl_trans;
  1077. }
  1078. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1079. /* create and remove of files */
  1080. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1081. if (!debugfs_create_file(#name, mode, parent, trans, \
  1082. &iwl_dbgfs_##name##_ops)) \
  1083. return -ENOMEM; \
  1084. } while (0)
  1085. /* file operation */
  1086. #define DEBUGFS_READ_FUNC(name) \
  1087. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1088. char __user *user_buf, \
  1089. size_t count, loff_t *ppos);
  1090. #define DEBUGFS_WRITE_FUNC(name) \
  1091. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1092. const char __user *user_buf, \
  1093. size_t count, loff_t *ppos);
  1094. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1095. {
  1096. file->private_data = inode->i_private;
  1097. return 0;
  1098. }
  1099. #define DEBUGFS_READ_FILE_OPS(name) \
  1100. DEBUGFS_READ_FUNC(name); \
  1101. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1102. .read = iwl_dbgfs_##name##_read, \
  1103. .open = iwl_dbgfs_open_file_generic, \
  1104. .llseek = generic_file_llseek, \
  1105. };
  1106. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1107. DEBUGFS_WRITE_FUNC(name); \
  1108. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1109. .write = iwl_dbgfs_##name##_write, \
  1110. .open = iwl_dbgfs_open_file_generic, \
  1111. .llseek = generic_file_llseek, \
  1112. };
  1113. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1114. DEBUGFS_READ_FUNC(name); \
  1115. DEBUGFS_WRITE_FUNC(name); \
  1116. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1117. .write = iwl_dbgfs_##name##_write, \
  1118. .read = iwl_dbgfs_##name##_read, \
  1119. .open = iwl_dbgfs_open_file_generic, \
  1120. .llseek = generic_file_llseek, \
  1121. };
  1122. static ssize_t iwl_dbgfs_traffic_log_read(struct file *file,
  1123. char __user *user_buf,
  1124. size_t count, loff_t *ppos)
  1125. {
  1126. struct iwl_trans *trans = file->private_data;
  1127. struct iwl_priv *priv = priv(trans);
  1128. int pos = 0, ofs = 0;
  1129. int cnt = 0, entry;
  1130. struct iwl_trans_pcie *trans_pcie =
  1131. IWL_TRANS_GET_PCIE_TRANS(trans);
  1132. struct iwl_tx_queue *txq;
  1133. struct iwl_queue *q;
  1134. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1135. char *buf;
  1136. int bufsz = ((IWL_TRAFFIC_ENTRIES * IWL_TRAFFIC_ENTRY_SIZE * 64) * 2) +
  1137. (priv->cfg->base_params->num_of_queues * 32 * 8) + 400;
  1138. const u8 *ptr;
  1139. ssize_t ret;
  1140. if (!priv->txq) {
  1141. IWL_ERR(trans, "txq not ready\n");
  1142. return -EAGAIN;
  1143. }
  1144. buf = kzalloc(bufsz, GFP_KERNEL);
  1145. if (!buf) {
  1146. IWL_ERR(trans, "Can not allocate buffer\n");
  1147. return -ENOMEM;
  1148. }
  1149. pos += scnprintf(buf + pos, bufsz - pos, "Tx Queue\n");
  1150. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1151. txq = &priv->txq[cnt];
  1152. q = &txq->q;
  1153. pos += scnprintf(buf + pos, bufsz - pos,
  1154. "q[%d]: read_ptr: %u, write_ptr: %u\n",
  1155. cnt, q->read_ptr, q->write_ptr);
  1156. }
  1157. if (priv->tx_traffic &&
  1158. (iwl_get_debug_level(trans->shrd) & IWL_DL_TX)) {
  1159. ptr = priv->tx_traffic;
  1160. pos += scnprintf(buf + pos, bufsz - pos,
  1161. "Tx Traffic idx: %u\n", priv->tx_traffic_idx);
  1162. for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
  1163. for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
  1164. entry++, ofs += 16) {
  1165. pos += scnprintf(buf + pos, bufsz - pos,
  1166. "0x%.4x ", ofs);
  1167. hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
  1168. buf + pos, bufsz - pos, 0);
  1169. pos += strlen(buf + pos);
  1170. if (bufsz - pos > 0)
  1171. buf[pos++] = '\n';
  1172. }
  1173. }
  1174. }
  1175. pos += scnprintf(buf + pos, bufsz - pos, "Rx Queue\n");
  1176. pos += scnprintf(buf + pos, bufsz - pos,
  1177. "read: %u, write: %u\n",
  1178. rxq->read, rxq->write);
  1179. if (priv->rx_traffic &&
  1180. (iwl_get_debug_level(trans->shrd) & IWL_DL_RX)) {
  1181. ptr = priv->rx_traffic;
  1182. pos += scnprintf(buf + pos, bufsz - pos,
  1183. "Rx Traffic idx: %u\n", priv->rx_traffic_idx);
  1184. for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
  1185. for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
  1186. entry++, ofs += 16) {
  1187. pos += scnprintf(buf + pos, bufsz - pos,
  1188. "0x%.4x ", ofs);
  1189. hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
  1190. buf + pos, bufsz - pos, 0);
  1191. pos += strlen(buf + pos);
  1192. if (bufsz - pos > 0)
  1193. buf[pos++] = '\n';
  1194. }
  1195. }
  1196. }
  1197. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1198. kfree(buf);
  1199. return ret;
  1200. }
  1201. static ssize_t iwl_dbgfs_traffic_log_write(struct file *file,
  1202. const char __user *user_buf,
  1203. size_t count, loff_t *ppos)
  1204. {
  1205. struct iwl_trans *trans = file->private_data;
  1206. char buf[8];
  1207. int buf_size;
  1208. int traffic_log;
  1209. memset(buf, 0, sizeof(buf));
  1210. buf_size = min(count, sizeof(buf) - 1);
  1211. if (copy_from_user(buf, user_buf, buf_size))
  1212. return -EFAULT;
  1213. if (sscanf(buf, "%d", &traffic_log) != 1)
  1214. return -EFAULT;
  1215. if (traffic_log == 0)
  1216. iwl_reset_traffic_log(priv(trans));
  1217. return count;
  1218. }
  1219. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1220. char __user *user_buf,
  1221. size_t count, loff_t *ppos) {
  1222. struct iwl_trans *trans = file->private_data;
  1223. struct iwl_priv *priv = priv(trans);
  1224. struct iwl_tx_queue *txq;
  1225. struct iwl_queue *q;
  1226. char *buf;
  1227. int pos = 0;
  1228. int cnt;
  1229. int ret;
  1230. const size_t bufsz = sizeof(char) * 64 *
  1231. priv->cfg->base_params->num_of_queues;
  1232. if (!priv->txq) {
  1233. IWL_ERR(priv, "txq not ready\n");
  1234. return -EAGAIN;
  1235. }
  1236. buf = kzalloc(bufsz, GFP_KERNEL);
  1237. if (!buf)
  1238. return -ENOMEM;
  1239. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1240. txq = &priv->txq[cnt];
  1241. q = &txq->q;
  1242. pos += scnprintf(buf + pos, bufsz - pos,
  1243. "hwq %.2d: read=%u write=%u stop=%d"
  1244. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1245. cnt, q->read_ptr, q->write_ptr,
  1246. !!test_bit(cnt, priv->queue_stopped),
  1247. txq->swq_id, txq->swq_id & 3,
  1248. (txq->swq_id >> 2) & 0x1f);
  1249. if (cnt >= 4)
  1250. continue;
  1251. /* for the ACs, display the stop count too */
  1252. pos += scnprintf(buf + pos, bufsz - pos,
  1253. " stop-count: %d\n",
  1254. atomic_read(&priv->queue_stop_count[cnt]));
  1255. }
  1256. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1257. kfree(buf);
  1258. return ret;
  1259. }
  1260. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1261. char __user *user_buf,
  1262. size_t count, loff_t *ppos) {
  1263. struct iwl_trans *trans = file->private_data;
  1264. struct iwl_trans_pcie *trans_pcie =
  1265. IWL_TRANS_GET_PCIE_TRANS(trans);
  1266. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1267. char buf[256];
  1268. int pos = 0;
  1269. const size_t bufsz = sizeof(buf);
  1270. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1271. rxq->read);
  1272. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1273. rxq->write);
  1274. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1275. rxq->free_count);
  1276. if (rxq->rb_stts) {
  1277. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1278. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1279. } else {
  1280. pos += scnprintf(buf + pos, bufsz - pos,
  1281. "closed_rb_num: Not Allocated\n");
  1282. }
  1283. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1284. }
  1285. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1286. char __user *user_buf,
  1287. size_t count, loff_t *ppos)
  1288. {
  1289. struct iwl_trans *trans = file->private_data;
  1290. char *buf;
  1291. int pos = 0;
  1292. ssize_t ret = -ENOMEM;
  1293. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1294. if (buf) {
  1295. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1296. kfree(buf);
  1297. }
  1298. return ret;
  1299. }
  1300. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1301. const char __user *user_buf,
  1302. size_t count, loff_t *ppos)
  1303. {
  1304. struct iwl_trans *trans = file->private_data;
  1305. u32 event_log_flag;
  1306. char buf[8];
  1307. int buf_size;
  1308. memset(buf, 0, sizeof(buf));
  1309. buf_size = min(count, sizeof(buf) - 1);
  1310. if (copy_from_user(buf, user_buf, buf_size))
  1311. return -EFAULT;
  1312. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1313. return -EFAULT;
  1314. if (event_log_flag == 1)
  1315. iwl_dump_nic_event_log(trans, true, NULL, false);
  1316. return count;
  1317. }
  1318. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1319. char __user *user_buf,
  1320. size_t count, loff_t *ppos) {
  1321. struct iwl_trans *trans = file->private_data;
  1322. struct iwl_trans_pcie *trans_pcie =
  1323. IWL_TRANS_GET_PCIE_TRANS(trans);
  1324. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1325. int pos = 0;
  1326. char *buf;
  1327. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1328. ssize_t ret;
  1329. buf = kzalloc(bufsz, GFP_KERNEL);
  1330. if (!buf) {
  1331. IWL_ERR(trans, "Can not allocate Buffer\n");
  1332. return -ENOMEM;
  1333. }
  1334. pos += scnprintf(buf + pos, bufsz - pos,
  1335. "Interrupt Statistics Report:\n");
  1336. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1337. isr_stats->hw);
  1338. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1339. isr_stats->sw);
  1340. if (isr_stats->sw || isr_stats->hw) {
  1341. pos += scnprintf(buf + pos, bufsz - pos,
  1342. "\tLast Restarting Code: 0x%X\n",
  1343. isr_stats->err_code);
  1344. }
  1345. #ifdef CONFIG_IWLWIFI_DEBUG
  1346. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1347. isr_stats->sch);
  1348. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1349. isr_stats->alive);
  1350. #endif
  1351. pos += scnprintf(buf + pos, bufsz - pos,
  1352. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1353. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1354. isr_stats->ctkill);
  1355. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1356. isr_stats->wakeup);
  1357. pos += scnprintf(buf + pos, bufsz - pos,
  1358. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1359. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1360. isr_stats->tx);
  1361. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1362. isr_stats->unhandled);
  1363. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1364. kfree(buf);
  1365. return ret;
  1366. }
  1367. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1368. const char __user *user_buf,
  1369. size_t count, loff_t *ppos)
  1370. {
  1371. struct iwl_trans *trans = file->private_data;
  1372. struct iwl_trans_pcie *trans_pcie =
  1373. IWL_TRANS_GET_PCIE_TRANS(trans);
  1374. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1375. char buf[8];
  1376. int buf_size;
  1377. u32 reset_flag;
  1378. memset(buf, 0, sizeof(buf));
  1379. buf_size = min(count, sizeof(buf) - 1);
  1380. if (copy_from_user(buf, user_buf, buf_size))
  1381. return -EFAULT;
  1382. if (sscanf(buf, "%x", &reset_flag) != 1)
  1383. return -EFAULT;
  1384. if (reset_flag == 0)
  1385. memset(isr_stats, 0, sizeof(*isr_stats));
  1386. return count;
  1387. }
  1388. static const char *get_csr_string(int cmd)
  1389. {
  1390. switch (cmd) {
  1391. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1392. IWL_CMD(CSR_INT_COALESCING);
  1393. IWL_CMD(CSR_INT);
  1394. IWL_CMD(CSR_INT_MASK);
  1395. IWL_CMD(CSR_FH_INT_STATUS);
  1396. IWL_CMD(CSR_GPIO_IN);
  1397. IWL_CMD(CSR_RESET);
  1398. IWL_CMD(CSR_GP_CNTRL);
  1399. IWL_CMD(CSR_HW_REV);
  1400. IWL_CMD(CSR_EEPROM_REG);
  1401. IWL_CMD(CSR_EEPROM_GP);
  1402. IWL_CMD(CSR_OTP_GP_REG);
  1403. IWL_CMD(CSR_GIO_REG);
  1404. IWL_CMD(CSR_GP_UCODE_REG);
  1405. IWL_CMD(CSR_GP_DRIVER_REG);
  1406. IWL_CMD(CSR_UCODE_DRV_GP1);
  1407. IWL_CMD(CSR_UCODE_DRV_GP2);
  1408. IWL_CMD(CSR_LED_REG);
  1409. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1410. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1411. IWL_CMD(CSR_ANA_PLL_CFG);
  1412. IWL_CMD(CSR_HW_REV_WA_REG);
  1413. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1414. default:
  1415. return "UNKNOWN";
  1416. }
  1417. }
  1418. void iwl_dump_csr(struct iwl_trans *trans)
  1419. {
  1420. int i;
  1421. static const u32 csr_tbl[] = {
  1422. CSR_HW_IF_CONFIG_REG,
  1423. CSR_INT_COALESCING,
  1424. CSR_INT,
  1425. CSR_INT_MASK,
  1426. CSR_FH_INT_STATUS,
  1427. CSR_GPIO_IN,
  1428. CSR_RESET,
  1429. CSR_GP_CNTRL,
  1430. CSR_HW_REV,
  1431. CSR_EEPROM_REG,
  1432. CSR_EEPROM_GP,
  1433. CSR_OTP_GP_REG,
  1434. CSR_GIO_REG,
  1435. CSR_GP_UCODE_REG,
  1436. CSR_GP_DRIVER_REG,
  1437. CSR_UCODE_DRV_GP1,
  1438. CSR_UCODE_DRV_GP2,
  1439. CSR_LED_REG,
  1440. CSR_DRAM_INT_TBL_REG,
  1441. CSR_GIO_CHICKEN_BITS,
  1442. CSR_ANA_PLL_CFG,
  1443. CSR_HW_REV_WA_REG,
  1444. CSR_DBG_HPET_MEM_REG
  1445. };
  1446. IWL_ERR(trans, "CSR values:\n");
  1447. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1448. "CSR_INT_PERIODIC_REG)\n");
  1449. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1450. IWL_ERR(trans, " %25s: 0X%08x\n",
  1451. get_csr_string(csr_tbl[i]),
  1452. iwl_read32(priv(trans), csr_tbl[i]));
  1453. }
  1454. }
  1455. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1456. const char __user *user_buf,
  1457. size_t count, loff_t *ppos)
  1458. {
  1459. struct iwl_trans *trans = file->private_data;
  1460. char buf[8];
  1461. int buf_size;
  1462. int csr;
  1463. memset(buf, 0, sizeof(buf));
  1464. buf_size = min(count, sizeof(buf) - 1);
  1465. if (copy_from_user(buf, user_buf, buf_size))
  1466. return -EFAULT;
  1467. if (sscanf(buf, "%d", &csr) != 1)
  1468. return -EFAULT;
  1469. iwl_dump_csr(trans);
  1470. return count;
  1471. }
  1472. static const char *get_fh_string(int cmd)
  1473. {
  1474. switch (cmd) {
  1475. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1476. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1477. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1478. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1479. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1480. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1481. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1482. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1483. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1484. default:
  1485. return "UNKNOWN";
  1486. }
  1487. }
  1488. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1489. {
  1490. int i;
  1491. #ifdef CONFIG_IWLWIFI_DEBUG
  1492. int pos = 0;
  1493. size_t bufsz = 0;
  1494. #endif
  1495. static const u32 fh_tbl[] = {
  1496. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1497. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1498. FH_RSCSR_CHNL0_WPTR,
  1499. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1500. FH_MEM_RSSR_SHARED_CTRL_REG,
  1501. FH_MEM_RSSR_RX_STATUS_REG,
  1502. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1503. FH_TSSR_TX_STATUS_REG,
  1504. FH_TSSR_TX_ERROR_REG
  1505. };
  1506. #ifdef CONFIG_IWLWIFI_DEBUG
  1507. if (display) {
  1508. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1509. *buf = kmalloc(bufsz, GFP_KERNEL);
  1510. if (!*buf)
  1511. return -ENOMEM;
  1512. pos += scnprintf(*buf + pos, bufsz - pos,
  1513. "FH register values:\n");
  1514. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1515. pos += scnprintf(*buf + pos, bufsz - pos,
  1516. " %34s: 0X%08x\n",
  1517. get_fh_string(fh_tbl[i]),
  1518. iwl_read_direct32(priv(trans), fh_tbl[i]));
  1519. }
  1520. return pos;
  1521. }
  1522. #endif
  1523. IWL_ERR(trans, "FH register values:\n");
  1524. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1525. IWL_ERR(trans, " %34s: 0X%08x\n",
  1526. get_fh_string(fh_tbl[i]),
  1527. iwl_read_direct32(priv(trans), fh_tbl[i]));
  1528. }
  1529. return 0;
  1530. }
  1531. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1532. char __user *user_buf,
  1533. size_t count, loff_t *ppos)
  1534. {
  1535. struct iwl_trans *trans = file->private_data;
  1536. char *buf;
  1537. int pos = 0;
  1538. ssize_t ret = -EFAULT;
  1539. ret = pos = iwl_dump_fh(trans, &buf, true);
  1540. if (buf) {
  1541. ret = simple_read_from_buffer(user_buf,
  1542. count, ppos, buf, pos);
  1543. kfree(buf);
  1544. }
  1545. return ret;
  1546. }
  1547. DEBUGFS_READ_WRITE_FILE_OPS(traffic_log);
  1548. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1549. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1550. DEBUGFS_READ_FILE_OPS(fh_reg);
  1551. DEBUGFS_READ_FILE_OPS(rx_queue);
  1552. DEBUGFS_READ_FILE_OPS(tx_queue);
  1553. DEBUGFS_WRITE_FILE_OPS(csr);
  1554. /*
  1555. * Create the debugfs files and directories
  1556. *
  1557. */
  1558. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1559. struct dentry *dir)
  1560. {
  1561. DEBUGFS_ADD_FILE(traffic_log, dir, S_IWUSR | S_IRUSR);
  1562. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1563. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1564. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1565. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1566. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1567. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1568. return 0;
  1569. }
  1570. #else
  1571. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1572. struct dentry *dir)
  1573. { return 0; }
  1574. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1575. const struct iwl_trans_ops trans_ops_pcie = {
  1576. .alloc = iwl_trans_pcie_alloc,
  1577. .request_irq = iwl_trans_pcie_request_irq,
  1578. .start_device = iwl_trans_pcie_start_device,
  1579. .prepare_card_hw = iwl_trans_pcie_prepare_card_hw,
  1580. .stop_device = iwl_trans_pcie_stop_device,
  1581. .tx_start = iwl_trans_pcie_tx_start,
  1582. .rx_free = iwl_trans_pcie_rx_free,
  1583. .tx_free = iwl_trans_pcie_tx_free,
  1584. .send_cmd = iwl_trans_pcie_send_cmd,
  1585. .send_cmd_pdu = iwl_trans_pcie_send_cmd_pdu,
  1586. .get_tx_cmd = iwl_trans_pcie_get_tx_cmd,
  1587. .tx = iwl_trans_pcie_tx,
  1588. .reclaim = iwl_trans_pcie_reclaim,
  1589. .txq_agg_disable = iwl_trans_pcie_txq_agg_disable,
  1590. .txq_agg_setup = iwl_trans_pcie_txq_agg_setup,
  1591. .kick_nic = iwl_trans_pcie_kick_nic,
  1592. .disable_sync_irq = iwl_trans_pcie_disable_sync_irq,
  1593. .free = iwl_trans_pcie_free,
  1594. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1595. .suspend = iwl_trans_pcie_suspend,
  1596. .resume = iwl_trans_pcie_resume,
  1597. };