iwl-trans-pcie.c 56 KB

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