iwl-trans-pcie.c 55 KB

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