iwl-trans.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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_trans *trans,
  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(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  142. /* Reset driver's Rx queue write index */
  143. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  144. /* Tell device where to find RBD circular buffer in DRAM */
  145. iwl_write_direct32(bus(trans), 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(bus(trans), 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(bus(trans), 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(bus(trans), 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(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(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  233. return iwl_poll_direct_bit(bus(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(bus(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 = hw_params(trans).max_txq_num *
  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. hw_params(trans).max_txq_num, 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(bus(trans), SCD_TXFACT, 0);
  494. /* Tell NIC where to find the "keep warm" buffer */
  495. iwl_write_direct32(bus(trans), FH_KW_MEM_ADDR_REG,
  496. trans_pcie->kw.dma >> 4);
  497. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  498. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  499. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  500. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  501. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  502. ret = iwl_trans_txq_init(trans, &priv->txq[txq_id], slots_num,
  503. txq_id);
  504. if (ret) {
  505. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  506. goto error;
  507. }
  508. }
  509. return 0;
  510. error:
  511. /*Upon error, free only if we allocated something */
  512. if (alloc)
  513. iwl_trans_tx_free(trans);
  514. return ret;
  515. }
  516. static void iwl_set_pwr_vmain(struct iwl_priv *priv)
  517. {
  518. struct iwl_trans *trans = trans(priv);
  519. /*
  520. * (for documentation purposes)
  521. * to set power to V_AUX, do:
  522. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  523. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  524. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  525. ~APMG_PS_CTRL_MSK_PWR_SRC);
  526. */
  527. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  528. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  529. ~APMG_PS_CTRL_MSK_PWR_SRC);
  530. }
  531. static int iwl_nic_init(struct iwl_trans *trans)
  532. {
  533. unsigned long flags;
  534. struct iwl_priv *priv = priv(trans);
  535. /* nic_init */
  536. spin_lock_irqsave(&trans->shrd->lock, flags);
  537. iwl_apm_init(priv);
  538. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  539. iwl_write8(bus(trans), CSR_INT_COALESCING,
  540. IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  541. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  542. iwl_set_pwr_vmain(priv);
  543. priv->cfg->lib->nic_config(priv);
  544. /* Allocate the RX queue, or reset if it is already allocated */
  545. iwl_rx_init(trans);
  546. /* Allocate or reset and init all Tx and Command queues */
  547. if (iwl_tx_init(trans))
  548. return -ENOMEM;
  549. if (hw_params(trans).shadow_reg_enable) {
  550. /* enable shadow regs in HW */
  551. iwl_set_bit(bus(trans), CSR_MAC_SHADOW_REG_CTRL,
  552. 0x800FFFFF);
  553. }
  554. set_bit(STATUS_INIT, &trans->shrd->status);
  555. return 0;
  556. }
  557. #define HW_READY_TIMEOUT (50)
  558. /* Note: returns poll_bit return value, which is >= 0 if success */
  559. static int iwl_set_hw_ready(struct iwl_trans *trans)
  560. {
  561. int ret;
  562. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  563. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  564. /* See if we got it */
  565. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  566. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  567. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  568. HW_READY_TIMEOUT);
  569. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  570. return ret;
  571. }
  572. /* Note: returns standard 0/-ERROR code */
  573. static int iwl_trans_pcie_prepare_card_hw(struct iwl_trans *trans)
  574. {
  575. int ret;
  576. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  577. ret = iwl_set_hw_ready(trans);
  578. if (ret >= 0)
  579. return 0;
  580. /* If HW is not ready, prepare the conditions to check again */
  581. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  582. CSR_HW_IF_CONFIG_REG_PREPARE);
  583. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  584. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  585. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  586. if (ret < 0)
  587. return ret;
  588. /* HW should be ready by now, check again. */
  589. ret = iwl_set_hw_ready(trans);
  590. if (ret >= 0)
  591. return 0;
  592. return ret;
  593. }
  594. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  595. {
  596. int ret;
  597. struct iwl_priv *priv = priv(trans);
  598. priv->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  599. if ((hw_params(priv).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  600. iwl_trans_pcie_prepare_card_hw(trans)) {
  601. IWL_WARN(trans, "Exit HW not ready\n");
  602. return -EIO;
  603. }
  604. /* If platform's RF_KILL switch is NOT set to KILL */
  605. if (iwl_read32(bus(trans), CSR_GP_CNTRL) &
  606. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  607. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  608. else
  609. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  610. if (iwl_is_rfkill(trans->shrd)) {
  611. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  612. iwl_enable_interrupts(trans);
  613. return -ERFKILL;
  614. }
  615. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  616. ret = iwl_nic_init(trans);
  617. if (ret) {
  618. IWL_ERR(trans, "Unable to init nic\n");
  619. return ret;
  620. }
  621. /* make sure rfkill handshake bits are cleared */
  622. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  623. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR,
  624. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  625. /* clear (again), then enable host interrupts */
  626. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  627. iwl_enable_interrupts(trans);
  628. /* really make sure rfkill handshake bits are cleared */
  629. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  630. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  631. return 0;
  632. }
  633. /*
  634. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  635. * must be called under priv->shrd->lock and mac access
  636. */
  637. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  638. {
  639. iwl_write_prph(bus(trans), SCD_TXFACT, mask);
  640. }
  641. #define IWL_AC_UNSET -1
  642. struct queue_to_fifo_ac {
  643. s8 fifo, ac;
  644. };
  645. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  646. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  647. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  648. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  649. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  650. { IWLAGN_CMD_FIFO_NUM, 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. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  655. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  656. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  657. };
  658. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  659. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  660. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  661. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  662. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  663. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  664. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  665. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  666. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  667. { IWL_TX_FIFO_BE_IPAN, 2, },
  668. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  669. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  670. };
  671. static void iwl_trans_pcie_tx_start(struct iwl_trans *trans)
  672. {
  673. const struct queue_to_fifo_ac *queue_to_fifo;
  674. struct iwl_rxon_context *ctx;
  675. struct iwl_priv *priv = priv(trans);
  676. struct iwl_trans_pcie *trans_pcie =
  677. IWL_TRANS_GET_PCIE_TRANS(trans);
  678. u32 a;
  679. unsigned long flags;
  680. int i, chan;
  681. u32 reg_val;
  682. spin_lock_irqsave(&trans->shrd->lock, flags);
  683. trans_pcie->scd_base_addr =
  684. iwl_read_prph(bus(trans), SCD_SRAM_BASE_ADDR);
  685. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  686. /* reset conext data memory */
  687. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  688. a += 4)
  689. iwl_write_targ_mem(bus(trans), a, 0);
  690. /* reset tx status memory */
  691. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  692. a += 4)
  693. iwl_write_targ_mem(bus(trans), a, 0);
  694. for (; a < trans_pcie->scd_base_addr +
  695. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(priv).max_txq_num);
  696. a += 4)
  697. iwl_write_targ_mem(bus(trans), a, 0);
  698. iwl_write_prph(bus(trans), SCD_DRAM_BASE_ADDR,
  699. trans_pcie->scd_bc_tbls.dma >> 10);
  700. /* Enable DMA channel */
  701. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  702. iwl_write_direct32(bus(trans), FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  703. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  704. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  705. /* Update FH chicken bits */
  706. reg_val = iwl_read_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG);
  707. iwl_write_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG,
  708. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  709. iwl_write_prph(bus(trans), SCD_QUEUECHAIN_SEL,
  710. SCD_QUEUECHAIN_SEL_ALL(priv));
  711. iwl_write_prph(bus(trans), SCD_AGGR_SEL, 0);
  712. /* initiate the queues */
  713. for (i = 0; i < hw_params(priv).max_txq_num; i++) {
  714. iwl_write_prph(bus(trans), SCD_QUEUE_RDPTR(i), 0);
  715. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR, 0 | (i << 8));
  716. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  717. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  718. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  719. SCD_CONTEXT_QUEUE_OFFSET(i) +
  720. sizeof(u32),
  721. ((SCD_WIN_SIZE <<
  722. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  723. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  724. ((SCD_FRAME_LIMIT <<
  725. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  726. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  727. }
  728. iwl_write_prph(bus(trans), SCD_INTERRUPT_MASK,
  729. IWL_MASK(0, hw_params(trans).max_txq_num));
  730. /* Activate all Tx DMA/FIFO channels */
  731. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  732. /* map queues to FIFOs */
  733. if (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  734. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  735. else
  736. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  737. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  738. /* make sure all queue are not stopped */
  739. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  740. for (i = 0; i < 4; i++)
  741. atomic_set(&priv->queue_stop_count[i], 0);
  742. for_each_context(priv, ctx)
  743. ctx->last_tx_rejected = false;
  744. /* reset to 0 to enable all the queue first */
  745. priv->txq_ctx_active_msk = 0;
  746. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  747. IWLAGN_FIRST_AMPDU_QUEUE);
  748. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  749. IWLAGN_FIRST_AMPDU_QUEUE);
  750. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  751. int fifo = queue_to_fifo[i].fifo;
  752. int ac = queue_to_fifo[i].ac;
  753. iwl_txq_ctx_activate(priv, i);
  754. if (fifo == IWL_TX_FIFO_UNUSED)
  755. continue;
  756. if (ac != IWL_AC_UNSET)
  757. iwl_set_swq_id(&priv->txq[i], ac, i);
  758. iwl_trans_tx_queue_set_status(priv, &priv->txq[i], fifo, 0);
  759. }
  760. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  761. /* Enable L1-Active */
  762. iwl_clear_bits_prph(bus(trans), APMG_PCIDEV_STT_REG,
  763. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  764. }
  765. /**
  766. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  767. */
  768. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  769. {
  770. int ch, txq_id;
  771. unsigned long flags;
  772. struct iwl_priv *priv = priv(trans);
  773. /* Turn off all Tx DMA fifos */
  774. spin_lock_irqsave(&trans->shrd->lock, flags);
  775. iwl_trans_txq_set_sched(trans, 0);
  776. /* Stop each Tx DMA channel, and wait for it to be idle */
  777. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  778. iwl_write_direct32(bus(trans),
  779. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  780. if (iwl_poll_direct_bit(bus(trans), FH_TSSR_TX_STATUS_REG,
  781. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  782. 1000))
  783. IWL_ERR(trans, "Failing on timeout while stopping"
  784. " DMA channel %d [0x%08x]", ch,
  785. iwl_read_direct32(bus(trans),
  786. FH_TSSR_TX_STATUS_REG));
  787. }
  788. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  789. if (!priv->txq) {
  790. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  791. return 0;
  792. }
  793. /* Unmap DMA from host system and free skb's */
  794. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  795. iwl_tx_queue_unmap(trans, txq_id);
  796. return 0;
  797. }
  798. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  799. {
  800. /* stop and reset the on-board processor */
  801. iwl_write32(bus(trans), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  802. /* tell the device to stop sending interrupts */
  803. iwl_trans_disable_sync_irq(trans);
  804. /* device going down, Stop using ICT table */
  805. iwl_disable_ict(trans);
  806. /*
  807. * If a HW restart happens during firmware loading,
  808. * then the firmware loading might call this function
  809. * and later it might be called again due to the
  810. * restart. So don't process again if the device is
  811. * already dead.
  812. */
  813. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  814. iwl_trans_tx_stop(trans);
  815. iwl_trans_rx_stop(trans);
  816. /* Power-down device's busmaster DMA clocks */
  817. iwl_write_prph(bus(trans), APMG_CLK_DIS_REG,
  818. APMG_CLK_VAL_DMA_CLK_RQT);
  819. udelay(5);
  820. }
  821. /* Make sure (redundant) we've released our request to stay awake */
  822. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  823. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  824. /* Stop the device, and put it in low power state */
  825. iwl_apm_stop(priv(trans));
  826. }
  827. static struct iwl_tx_cmd *iwl_trans_pcie_get_tx_cmd(struct iwl_trans *trans,
  828. int txq_id)
  829. {
  830. struct iwl_priv *priv = priv(trans);
  831. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  832. struct iwl_queue *q = &txq->q;
  833. struct iwl_device_cmd *dev_cmd;
  834. if (unlikely(iwl_queue_space(q) < q->high_mark))
  835. return NULL;
  836. /*
  837. * Set up the Tx-command (not MAC!) header.
  838. * Store the chosen Tx queue and TFD index within the sequence field;
  839. * after Tx, uCode's Tx response will return this value so driver can
  840. * locate the frame within the tx queue and do post-tx processing.
  841. */
  842. dev_cmd = txq->cmd[q->write_ptr];
  843. memset(dev_cmd, 0, sizeof(*dev_cmd));
  844. dev_cmd->hdr.cmd = REPLY_TX;
  845. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  846. INDEX_TO_SEQ(q->write_ptr)));
  847. return &dev_cmd->cmd.tx;
  848. }
  849. static int iwl_trans_pcie_tx(struct iwl_priv *priv, struct sk_buff *skb,
  850. struct iwl_tx_cmd *tx_cmd, int txq_id, __le16 fc, bool ampdu,
  851. struct iwl_rxon_context *ctx)
  852. {
  853. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  854. struct iwl_queue *q = &txq->q;
  855. struct iwl_device_cmd *dev_cmd = txq->cmd[q->write_ptr];
  856. struct iwl_cmd_meta *out_meta;
  857. dma_addr_t phys_addr = 0;
  858. dma_addr_t txcmd_phys;
  859. dma_addr_t scratch_phys;
  860. u16 len, firstlen, secondlen;
  861. u8 wait_write_ptr = 0;
  862. u8 hdr_len = ieee80211_hdrlen(fc);
  863. /* Set up driver data for this TFD */
  864. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  865. txq->txb[q->write_ptr].skb = skb;
  866. txq->txb[q->write_ptr].ctx = ctx;
  867. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  868. out_meta = &txq->meta[q->write_ptr];
  869. /*
  870. * Use the first empty entry in this queue's command buffer array
  871. * to contain the Tx command and MAC header concatenated together
  872. * (payload data will be in another buffer).
  873. * Size of this varies, due to varying MAC header length.
  874. * If end is not dword aligned, we'll have 2 extra bytes at the end
  875. * of the MAC header (device reads on dword boundaries).
  876. * We'll tell device about this padding later.
  877. */
  878. len = sizeof(struct iwl_tx_cmd) +
  879. sizeof(struct iwl_cmd_header) + hdr_len;
  880. firstlen = (len + 3) & ~3;
  881. /* Tell NIC about any 2-byte padding after MAC header */
  882. if (firstlen != len)
  883. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  884. /* Physical address of this Tx command's header (not MAC header!),
  885. * within command buffer array. */
  886. txcmd_phys = dma_map_single(priv->bus->dev,
  887. &dev_cmd->hdr, firstlen,
  888. DMA_BIDIRECTIONAL);
  889. if (unlikely(dma_mapping_error(priv->bus->dev, txcmd_phys)))
  890. return -1;
  891. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  892. dma_unmap_len_set(out_meta, len, firstlen);
  893. if (!ieee80211_has_morefrags(fc)) {
  894. txq->need_update = 1;
  895. } else {
  896. wait_write_ptr = 1;
  897. txq->need_update = 0;
  898. }
  899. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  900. * if any (802.11 null frames have no payload). */
  901. secondlen = skb->len - hdr_len;
  902. if (secondlen > 0) {
  903. phys_addr = dma_map_single(priv->bus->dev, skb->data + hdr_len,
  904. secondlen, DMA_TO_DEVICE);
  905. if (unlikely(dma_mapping_error(priv->bus->dev, phys_addr))) {
  906. dma_unmap_single(priv->bus->dev,
  907. dma_unmap_addr(out_meta, mapping),
  908. dma_unmap_len(out_meta, len),
  909. DMA_BIDIRECTIONAL);
  910. return -1;
  911. }
  912. }
  913. /* Attach buffers to TFD */
  914. iwlagn_txq_attach_buf_to_tfd(trans(priv), txq, txcmd_phys,
  915. firstlen, 1);
  916. if (secondlen > 0)
  917. iwlagn_txq_attach_buf_to_tfd(trans(priv), txq, phys_addr,
  918. secondlen, 0);
  919. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  920. offsetof(struct iwl_tx_cmd, scratch);
  921. /* take back ownership of DMA buffer to enable update */
  922. dma_sync_single_for_cpu(priv->bus->dev, txcmd_phys, firstlen,
  923. DMA_BIDIRECTIONAL);
  924. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  925. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  926. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  927. le16_to_cpu(dev_cmd->hdr.sequence));
  928. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  929. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  930. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  931. /* Set up entry for this TFD in Tx byte-count array */
  932. if (ampdu)
  933. iwl_trans_txq_update_byte_cnt_tbl(trans(priv), txq,
  934. le16_to_cpu(tx_cmd->len));
  935. dma_sync_single_for_device(priv->bus->dev, txcmd_phys, firstlen,
  936. DMA_BIDIRECTIONAL);
  937. trace_iwlwifi_dev_tx(priv,
  938. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  939. sizeof(struct iwl_tfd),
  940. &dev_cmd->hdr, firstlen,
  941. skb->data + hdr_len, secondlen);
  942. /* Tell device the write index *just past* this latest filled TFD */
  943. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  944. iwl_txq_update_write_ptr(trans(priv), txq);
  945. /*
  946. * At this point the frame is "transmitted" successfully
  947. * and we will get a TX status notification eventually,
  948. * regardless of the value of ret. "ret" only indicates
  949. * whether or not we should update the write pointer.
  950. */
  951. if (iwl_queue_space(q) < q->high_mark) {
  952. if (wait_write_ptr) {
  953. txq->need_update = 1;
  954. iwl_txq_update_write_ptr(trans(priv), txq);
  955. } else {
  956. iwl_stop_queue(priv, txq);
  957. }
  958. }
  959. return 0;
  960. }
  961. static void iwl_trans_pcie_kick_nic(struct iwl_trans *trans)
  962. {
  963. /* Remove all resets to allow NIC to operate */
  964. iwl_write32(bus(trans), CSR_RESET, 0);
  965. }
  966. static int iwl_trans_pcie_request_irq(struct iwl_trans *trans)
  967. {
  968. struct iwl_trans_pcie *trans_pcie =
  969. IWL_TRANS_GET_PCIE_TRANS(trans);
  970. int err;
  971. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  972. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  973. iwl_irq_tasklet, (unsigned long)trans);
  974. iwl_alloc_isr_ict(trans);
  975. err = request_irq(bus(trans)->irq, iwl_isr_ict, IRQF_SHARED,
  976. DRV_NAME, trans);
  977. if (err) {
  978. IWL_ERR(trans, "Error allocating IRQ %d\n", bus(trans)->irq);
  979. iwl_free_isr_ict(trans);
  980. return err;
  981. }
  982. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  983. return 0;
  984. }
  985. static void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id,
  986. int ssn, u32 status, struct sk_buff_head *skbs)
  987. {
  988. struct iwl_priv *priv = priv(trans);
  989. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  990. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  991. int tfd_num = ssn & (txq->q.n_bd - 1);
  992. u8 agg_state;
  993. bool cond;
  994. if (txq->sched_retry) {
  995. agg_state =
  996. priv->shrd->tid_data[txq->sta_id][txq->tid].agg.state;
  997. cond = (agg_state != IWL_EMPTYING_HW_QUEUE_DELBA);
  998. } else {
  999. cond = (status != TX_STATUS_FAIL_PASSIVE_NO_RX);
  1000. }
  1001. if (txq->q.read_ptr != tfd_num) {
  1002. IWL_DEBUG_TX_REPLY(trans, "Retry scheduler reclaim "
  1003. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  1004. ssn , tfd_num, txq_id, txq->swq_id);
  1005. iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1006. if (iwl_queue_space(&txq->q) > txq->q.low_mark && cond)
  1007. iwl_wake_queue(priv, txq);
  1008. }
  1009. }
  1010. static void iwl_trans_pcie_disable_sync_irq(struct iwl_trans *trans)
  1011. {
  1012. unsigned long flags;
  1013. struct iwl_trans_pcie *trans_pcie =
  1014. IWL_TRANS_GET_PCIE_TRANS(trans);
  1015. spin_lock_irqsave(&trans->shrd->lock, flags);
  1016. iwl_disable_interrupts(trans);
  1017. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1018. /* wait to make sure we flush pending tasklet*/
  1019. synchronize_irq(bus(trans)->irq);
  1020. tasklet_kill(&trans_pcie->irq_tasklet);
  1021. }
  1022. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1023. {
  1024. free_irq(bus(trans)->irq, trans);
  1025. iwl_free_isr_ict(trans);
  1026. trans->shrd->trans = NULL;
  1027. kfree(trans);
  1028. }
  1029. #ifdef CONFIG_PM
  1030. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1031. {
  1032. /*
  1033. * This function is called when system goes into suspend state
  1034. * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
  1035. * first but since iwl_mac_stop() has no knowledge of who the caller is,
  1036. * it will not call apm_ops.stop() to stop the DMA operation.
  1037. * Calling apm_ops.stop here to make sure we stop the DMA.
  1038. *
  1039. * But of course ... if we have configured WoWLAN then we did other
  1040. * things already :-)
  1041. */
  1042. if (!trans->shrd->wowlan)
  1043. iwl_apm_stop(priv(trans));
  1044. return 0;
  1045. }
  1046. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1047. {
  1048. bool hw_rfkill = false;
  1049. iwl_enable_interrupts(trans);
  1050. if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
  1051. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1052. hw_rfkill = true;
  1053. if (hw_rfkill)
  1054. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1055. else
  1056. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1057. wiphy_rfkill_set_hw_state(priv(trans)->hw->wiphy, hw_rfkill);
  1058. return 0;
  1059. }
  1060. #else /* CONFIG_PM */
  1061. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1062. { return 0; }
  1063. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1064. { return 0; }
  1065. #endif /* CONFIG_PM */
  1066. const struct iwl_trans_ops trans_ops_pcie;
  1067. static struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd)
  1068. {
  1069. struct iwl_trans *iwl_trans = kzalloc(sizeof(struct iwl_trans) +
  1070. sizeof(struct iwl_trans_pcie),
  1071. GFP_KERNEL);
  1072. if (iwl_trans) {
  1073. struct iwl_trans_pcie *trans_pcie =
  1074. IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
  1075. iwl_trans->ops = &trans_ops_pcie;
  1076. iwl_trans->shrd = shrd;
  1077. trans_pcie->trans = iwl_trans;
  1078. spin_lock_init(&iwl_trans->hcmd_lock);
  1079. }
  1080. return iwl_trans;
  1081. }
  1082. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1083. /* create and remove of files */
  1084. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1085. if (!debugfs_create_file(#name, mode, parent, trans, \
  1086. &iwl_dbgfs_##name##_ops)) \
  1087. return -ENOMEM; \
  1088. } while (0)
  1089. /* file operation */
  1090. #define DEBUGFS_READ_FUNC(name) \
  1091. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1092. char __user *user_buf, \
  1093. size_t count, loff_t *ppos);
  1094. #define DEBUGFS_WRITE_FUNC(name) \
  1095. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1096. const char __user *user_buf, \
  1097. size_t count, loff_t *ppos);
  1098. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1099. {
  1100. file->private_data = inode->i_private;
  1101. return 0;
  1102. }
  1103. #define DEBUGFS_READ_FILE_OPS(name) \
  1104. DEBUGFS_READ_FUNC(name); \
  1105. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1106. .read = iwl_dbgfs_##name##_read, \
  1107. .open = iwl_dbgfs_open_file_generic, \
  1108. .llseek = generic_file_llseek, \
  1109. };
  1110. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1111. DEBUGFS_WRITE_FUNC(name); \
  1112. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1113. .write = iwl_dbgfs_##name##_write, \
  1114. .open = iwl_dbgfs_open_file_generic, \
  1115. .llseek = generic_file_llseek, \
  1116. };
  1117. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1118. DEBUGFS_READ_FUNC(name); \
  1119. DEBUGFS_WRITE_FUNC(name); \
  1120. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1121. .write = iwl_dbgfs_##name##_write, \
  1122. .read = iwl_dbgfs_##name##_read, \
  1123. .open = iwl_dbgfs_open_file_generic, \
  1124. .llseek = generic_file_llseek, \
  1125. };
  1126. static ssize_t iwl_dbgfs_traffic_log_read(struct file *file,
  1127. char __user *user_buf,
  1128. size_t count, loff_t *ppos)
  1129. {
  1130. struct iwl_trans *trans = file->private_data;
  1131. struct iwl_priv *priv = priv(trans);
  1132. int pos = 0, ofs = 0;
  1133. int cnt = 0, entry;
  1134. struct iwl_trans_pcie *trans_pcie =
  1135. IWL_TRANS_GET_PCIE_TRANS(trans);
  1136. struct iwl_tx_queue *txq;
  1137. struct iwl_queue *q;
  1138. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1139. char *buf;
  1140. int bufsz = ((IWL_TRAFFIC_ENTRIES * IWL_TRAFFIC_ENTRY_SIZE * 64) * 2) +
  1141. (hw_params(trans).max_txq_num * 32 * 8) + 400;
  1142. const u8 *ptr;
  1143. ssize_t ret;
  1144. if (!priv->txq) {
  1145. IWL_ERR(trans, "txq not ready\n");
  1146. return -EAGAIN;
  1147. }
  1148. buf = kzalloc(bufsz, GFP_KERNEL);
  1149. if (!buf) {
  1150. IWL_ERR(trans, "Can not allocate buffer\n");
  1151. return -ENOMEM;
  1152. }
  1153. pos += scnprintf(buf + pos, bufsz - pos, "Tx Queue\n");
  1154. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1155. txq = &priv->txq[cnt];
  1156. q = &txq->q;
  1157. pos += scnprintf(buf + pos, bufsz - pos,
  1158. "q[%d]: read_ptr: %u, write_ptr: %u\n",
  1159. cnt, q->read_ptr, q->write_ptr);
  1160. }
  1161. if (priv->tx_traffic &&
  1162. (iwl_get_debug_level(trans->shrd) & IWL_DL_TX)) {
  1163. ptr = priv->tx_traffic;
  1164. pos += scnprintf(buf + pos, bufsz - pos,
  1165. "Tx Traffic idx: %u\n", priv->tx_traffic_idx);
  1166. for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
  1167. for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
  1168. entry++, ofs += 16) {
  1169. pos += scnprintf(buf + pos, bufsz - pos,
  1170. "0x%.4x ", ofs);
  1171. hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
  1172. buf + pos, bufsz - pos, 0);
  1173. pos += strlen(buf + pos);
  1174. if (bufsz - pos > 0)
  1175. buf[pos++] = '\n';
  1176. }
  1177. }
  1178. }
  1179. pos += scnprintf(buf + pos, bufsz - pos, "Rx Queue\n");
  1180. pos += scnprintf(buf + pos, bufsz - pos,
  1181. "read: %u, write: %u\n",
  1182. rxq->read, rxq->write);
  1183. if (priv->rx_traffic &&
  1184. (iwl_get_debug_level(trans->shrd) & IWL_DL_RX)) {
  1185. ptr = priv->rx_traffic;
  1186. pos += scnprintf(buf + pos, bufsz - pos,
  1187. "Rx Traffic idx: %u\n", priv->rx_traffic_idx);
  1188. for (cnt = 0, ofs = 0; cnt < IWL_TRAFFIC_ENTRIES; cnt++) {
  1189. for (entry = 0; entry < IWL_TRAFFIC_ENTRY_SIZE / 16;
  1190. entry++, ofs += 16) {
  1191. pos += scnprintf(buf + pos, bufsz - pos,
  1192. "0x%.4x ", ofs);
  1193. hex_dump_to_buffer(ptr + ofs, 16, 16, 2,
  1194. buf + pos, bufsz - pos, 0);
  1195. pos += strlen(buf + pos);
  1196. if (bufsz - pos > 0)
  1197. buf[pos++] = '\n';
  1198. }
  1199. }
  1200. }
  1201. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1202. kfree(buf);
  1203. return ret;
  1204. }
  1205. static ssize_t iwl_dbgfs_traffic_log_write(struct file *file,
  1206. const char __user *user_buf,
  1207. size_t count, loff_t *ppos)
  1208. {
  1209. struct iwl_trans *trans = file->private_data;
  1210. char buf[8];
  1211. int buf_size;
  1212. int traffic_log;
  1213. memset(buf, 0, sizeof(buf));
  1214. buf_size = min(count, sizeof(buf) - 1);
  1215. if (copy_from_user(buf, user_buf, buf_size))
  1216. return -EFAULT;
  1217. if (sscanf(buf, "%d", &traffic_log) != 1)
  1218. return -EFAULT;
  1219. if (traffic_log == 0)
  1220. iwl_reset_traffic_log(priv(trans));
  1221. return count;
  1222. }
  1223. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1224. char __user *user_buf,
  1225. size_t count, loff_t *ppos) {
  1226. struct iwl_trans *trans = file->private_data;
  1227. struct iwl_priv *priv = priv(trans);
  1228. struct iwl_tx_queue *txq;
  1229. struct iwl_queue *q;
  1230. char *buf;
  1231. int pos = 0;
  1232. int cnt;
  1233. int ret;
  1234. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1235. if (!priv->txq) {
  1236. IWL_ERR(priv, "txq not ready\n");
  1237. return -EAGAIN;
  1238. }
  1239. buf = kzalloc(bufsz, GFP_KERNEL);
  1240. if (!buf)
  1241. return -ENOMEM;
  1242. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1243. txq = &priv->txq[cnt];
  1244. q = &txq->q;
  1245. pos += scnprintf(buf + pos, bufsz - pos,
  1246. "hwq %.2d: read=%u write=%u stop=%d"
  1247. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1248. cnt, q->read_ptr, q->write_ptr,
  1249. !!test_bit(cnt, priv->queue_stopped),
  1250. txq->swq_id, txq->swq_id & 3,
  1251. (txq->swq_id >> 2) & 0x1f);
  1252. if (cnt >= 4)
  1253. continue;
  1254. /* for the ACs, display the stop count too */
  1255. pos += scnprintf(buf + pos, bufsz - pos,
  1256. " stop-count: %d\n",
  1257. atomic_read(&priv->queue_stop_count[cnt]));
  1258. }
  1259. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1260. kfree(buf);
  1261. return ret;
  1262. }
  1263. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1264. char __user *user_buf,
  1265. size_t count, loff_t *ppos) {
  1266. struct iwl_trans *trans = file->private_data;
  1267. struct iwl_trans_pcie *trans_pcie =
  1268. IWL_TRANS_GET_PCIE_TRANS(trans);
  1269. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1270. char buf[256];
  1271. int pos = 0;
  1272. const size_t bufsz = sizeof(buf);
  1273. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1274. rxq->read);
  1275. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1276. rxq->write);
  1277. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1278. rxq->free_count);
  1279. if (rxq->rb_stts) {
  1280. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1281. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1282. } else {
  1283. pos += scnprintf(buf + pos, bufsz - pos,
  1284. "closed_rb_num: Not Allocated\n");
  1285. }
  1286. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1287. }
  1288. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1289. char __user *user_buf,
  1290. size_t count, loff_t *ppos)
  1291. {
  1292. struct iwl_trans *trans = file->private_data;
  1293. char *buf;
  1294. int pos = 0;
  1295. ssize_t ret = -ENOMEM;
  1296. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1297. if (buf) {
  1298. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1299. kfree(buf);
  1300. }
  1301. return ret;
  1302. }
  1303. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1304. const char __user *user_buf,
  1305. size_t count, loff_t *ppos)
  1306. {
  1307. struct iwl_trans *trans = file->private_data;
  1308. u32 event_log_flag;
  1309. char buf[8];
  1310. int buf_size;
  1311. memset(buf, 0, sizeof(buf));
  1312. buf_size = min(count, sizeof(buf) - 1);
  1313. if (copy_from_user(buf, user_buf, buf_size))
  1314. return -EFAULT;
  1315. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1316. return -EFAULT;
  1317. if (event_log_flag == 1)
  1318. iwl_dump_nic_event_log(trans, true, NULL, false);
  1319. return count;
  1320. }
  1321. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1322. char __user *user_buf,
  1323. size_t count, loff_t *ppos) {
  1324. struct iwl_trans *trans = file->private_data;
  1325. struct iwl_trans_pcie *trans_pcie =
  1326. IWL_TRANS_GET_PCIE_TRANS(trans);
  1327. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1328. int pos = 0;
  1329. char *buf;
  1330. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1331. ssize_t ret;
  1332. buf = kzalloc(bufsz, GFP_KERNEL);
  1333. if (!buf) {
  1334. IWL_ERR(trans, "Can not allocate Buffer\n");
  1335. return -ENOMEM;
  1336. }
  1337. pos += scnprintf(buf + pos, bufsz - pos,
  1338. "Interrupt Statistics Report:\n");
  1339. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1340. isr_stats->hw);
  1341. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1342. isr_stats->sw);
  1343. if (isr_stats->sw || isr_stats->hw) {
  1344. pos += scnprintf(buf + pos, bufsz - pos,
  1345. "\tLast Restarting Code: 0x%X\n",
  1346. isr_stats->err_code);
  1347. }
  1348. #ifdef CONFIG_IWLWIFI_DEBUG
  1349. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1350. isr_stats->sch);
  1351. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1352. isr_stats->alive);
  1353. #endif
  1354. pos += scnprintf(buf + pos, bufsz - pos,
  1355. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1356. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1357. isr_stats->ctkill);
  1358. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1359. isr_stats->wakeup);
  1360. pos += scnprintf(buf + pos, bufsz - pos,
  1361. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1362. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1363. isr_stats->tx);
  1364. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1365. isr_stats->unhandled);
  1366. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1367. kfree(buf);
  1368. return ret;
  1369. }
  1370. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1371. const char __user *user_buf,
  1372. size_t count, loff_t *ppos)
  1373. {
  1374. struct iwl_trans *trans = file->private_data;
  1375. struct iwl_trans_pcie *trans_pcie =
  1376. IWL_TRANS_GET_PCIE_TRANS(trans);
  1377. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1378. char buf[8];
  1379. int buf_size;
  1380. u32 reset_flag;
  1381. memset(buf, 0, sizeof(buf));
  1382. buf_size = min(count, sizeof(buf) - 1);
  1383. if (copy_from_user(buf, user_buf, buf_size))
  1384. return -EFAULT;
  1385. if (sscanf(buf, "%x", &reset_flag) != 1)
  1386. return -EFAULT;
  1387. if (reset_flag == 0)
  1388. memset(isr_stats, 0, sizeof(*isr_stats));
  1389. return count;
  1390. }
  1391. static const char *get_csr_string(int cmd)
  1392. {
  1393. switch (cmd) {
  1394. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1395. IWL_CMD(CSR_INT_COALESCING);
  1396. IWL_CMD(CSR_INT);
  1397. IWL_CMD(CSR_INT_MASK);
  1398. IWL_CMD(CSR_FH_INT_STATUS);
  1399. IWL_CMD(CSR_GPIO_IN);
  1400. IWL_CMD(CSR_RESET);
  1401. IWL_CMD(CSR_GP_CNTRL);
  1402. IWL_CMD(CSR_HW_REV);
  1403. IWL_CMD(CSR_EEPROM_REG);
  1404. IWL_CMD(CSR_EEPROM_GP);
  1405. IWL_CMD(CSR_OTP_GP_REG);
  1406. IWL_CMD(CSR_GIO_REG);
  1407. IWL_CMD(CSR_GP_UCODE_REG);
  1408. IWL_CMD(CSR_GP_DRIVER_REG);
  1409. IWL_CMD(CSR_UCODE_DRV_GP1);
  1410. IWL_CMD(CSR_UCODE_DRV_GP2);
  1411. IWL_CMD(CSR_LED_REG);
  1412. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1413. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1414. IWL_CMD(CSR_ANA_PLL_CFG);
  1415. IWL_CMD(CSR_HW_REV_WA_REG);
  1416. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1417. default:
  1418. return "UNKNOWN";
  1419. }
  1420. }
  1421. void iwl_dump_csr(struct iwl_trans *trans)
  1422. {
  1423. int i;
  1424. static const u32 csr_tbl[] = {
  1425. CSR_HW_IF_CONFIG_REG,
  1426. CSR_INT_COALESCING,
  1427. CSR_INT,
  1428. CSR_INT_MASK,
  1429. CSR_FH_INT_STATUS,
  1430. CSR_GPIO_IN,
  1431. CSR_RESET,
  1432. CSR_GP_CNTRL,
  1433. CSR_HW_REV,
  1434. CSR_EEPROM_REG,
  1435. CSR_EEPROM_GP,
  1436. CSR_OTP_GP_REG,
  1437. CSR_GIO_REG,
  1438. CSR_GP_UCODE_REG,
  1439. CSR_GP_DRIVER_REG,
  1440. CSR_UCODE_DRV_GP1,
  1441. CSR_UCODE_DRV_GP2,
  1442. CSR_LED_REG,
  1443. CSR_DRAM_INT_TBL_REG,
  1444. CSR_GIO_CHICKEN_BITS,
  1445. CSR_ANA_PLL_CFG,
  1446. CSR_HW_REV_WA_REG,
  1447. CSR_DBG_HPET_MEM_REG
  1448. };
  1449. IWL_ERR(trans, "CSR values:\n");
  1450. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1451. "CSR_INT_PERIODIC_REG)\n");
  1452. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1453. IWL_ERR(trans, " %25s: 0X%08x\n",
  1454. get_csr_string(csr_tbl[i]),
  1455. iwl_read32(bus(trans), csr_tbl[i]));
  1456. }
  1457. }
  1458. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1459. const char __user *user_buf,
  1460. size_t count, loff_t *ppos)
  1461. {
  1462. struct iwl_trans *trans = file->private_data;
  1463. char buf[8];
  1464. int buf_size;
  1465. int csr;
  1466. memset(buf, 0, sizeof(buf));
  1467. buf_size = min(count, sizeof(buf) - 1);
  1468. if (copy_from_user(buf, user_buf, buf_size))
  1469. return -EFAULT;
  1470. if (sscanf(buf, "%d", &csr) != 1)
  1471. return -EFAULT;
  1472. iwl_dump_csr(trans);
  1473. return count;
  1474. }
  1475. static const char *get_fh_string(int cmd)
  1476. {
  1477. switch (cmd) {
  1478. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1479. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1480. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1481. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1482. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1483. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1484. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1485. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1486. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1487. default:
  1488. return "UNKNOWN";
  1489. }
  1490. }
  1491. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1492. {
  1493. int i;
  1494. #ifdef CONFIG_IWLWIFI_DEBUG
  1495. int pos = 0;
  1496. size_t bufsz = 0;
  1497. #endif
  1498. static const u32 fh_tbl[] = {
  1499. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1500. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1501. FH_RSCSR_CHNL0_WPTR,
  1502. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1503. FH_MEM_RSSR_SHARED_CTRL_REG,
  1504. FH_MEM_RSSR_RX_STATUS_REG,
  1505. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1506. FH_TSSR_TX_STATUS_REG,
  1507. FH_TSSR_TX_ERROR_REG
  1508. };
  1509. #ifdef CONFIG_IWLWIFI_DEBUG
  1510. if (display) {
  1511. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1512. *buf = kmalloc(bufsz, GFP_KERNEL);
  1513. if (!*buf)
  1514. return -ENOMEM;
  1515. pos += scnprintf(*buf + pos, bufsz - pos,
  1516. "FH register values:\n");
  1517. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1518. pos += scnprintf(*buf + pos, bufsz - pos,
  1519. " %34s: 0X%08x\n",
  1520. get_fh_string(fh_tbl[i]),
  1521. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1522. }
  1523. return pos;
  1524. }
  1525. #endif
  1526. IWL_ERR(trans, "FH register values:\n");
  1527. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1528. IWL_ERR(trans, " %34s: 0X%08x\n",
  1529. get_fh_string(fh_tbl[i]),
  1530. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1531. }
  1532. return 0;
  1533. }
  1534. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1535. char __user *user_buf,
  1536. size_t count, loff_t *ppos)
  1537. {
  1538. struct iwl_trans *trans = file->private_data;
  1539. char *buf;
  1540. int pos = 0;
  1541. ssize_t ret = -EFAULT;
  1542. ret = pos = iwl_dump_fh(trans, &buf, true);
  1543. if (buf) {
  1544. ret = simple_read_from_buffer(user_buf,
  1545. count, ppos, buf, pos);
  1546. kfree(buf);
  1547. }
  1548. return ret;
  1549. }
  1550. DEBUGFS_READ_WRITE_FILE_OPS(traffic_log);
  1551. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1552. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1553. DEBUGFS_READ_FILE_OPS(fh_reg);
  1554. DEBUGFS_READ_FILE_OPS(rx_queue);
  1555. DEBUGFS_READ_FILE_OPS(tx_queue);
  1556. DEBUGFS_WRITE_FILE_OPS(csr);
  1557. /*
  1558. * Create the debugfs files and directories
  1559. *
  1560. */
  1561. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1562. struct dentry *dir)
  1563. {
  1564. DEBUGFS_ADD_FILE(traffic_log, dir, S_IWUSR | S_IRUSR);
  1565. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1566. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1567. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1568. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1569. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1570. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1571. return 0;
  1572. }
  1573. #else
  1574. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1575. struct dentry *dir)
  1576. { return 0; }
  1577. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1578. const struct iwl_trans_ops trans_ops_pcie = {
  1579. .alloc = iwl_trans_pcie_alloc,
  1580. .request_irq = iwl_trans_pcie_request_irq,
  1581. .start_device = iwl_trans_pcie_start_device,
  1582. .prepare_card_hw = iwl_trans_pcie_prepare_card_hw,
  1583. .stop_device = iwl_trans_pcie_stop_device,
  1584. .tx_start = iwl_trans_pcie_tx_start,
  1585. .rx_free = iwl_trans_pcie_rx_free,
  1586. .tx_free = iwl_trans_pcie_tx_free,
  1587. .send_cmd = iwl_trans_pcie_send_cmd,
  1588. .send_cmd_pdu = iwl_trans_pcie_send_cmd_pdu,
  1589. .get_tx_cmd = iwl_trans_pcie_get_tx_cmd,
  1590. .tx = iwl_trans_pcie_tx,
  1591. .reclaim = iwl_trans_pcie_reclaim,
  1592. .txq_agg_disable = iwl_trans_pcie_txq_agg_disable,
  1593. .txq_agg_setup = iwl_trans_pcie_txq_agg_setup,
  1594. .kick_nic = iwl_trans_pcie_kick_nic,
  1595. .disable_sync_irq = iwl_trans_pcie_disable_sync_irq,
  1596. .free = iwl_trans_pcie_free,
  1597. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1598. .suspend = iwl_trans_pcie_suspend,
  1599. .resume = iwl_trans_pcie_resume,
  1600. };