iwl-trans-pcie.c 55 KB

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