iwl-trans-pcie.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  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. if (!q->n_bd)
  349. return;
  350. /* In the command queue, all the TBs are mapped as BIDI
  351. * so unmap them as such.
  352. */
  353. if (txq_id == trans->shrd->cmd_queue)
  354. dma_dir = DMA_BIDIRECTIONAL;
  355. else
  356. dma_dir = DMA_TO_DEVICE;
  357. while (q->write_ptr != q->read_ptr) {
  358. /* The read_ptr needs to bound by q->n_window */
  359. iwlagn_txq_free_tfd(trans, txq, get_cmd_index(q, q->read_ptr),
  360. dma_dir);
  361. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  362. }
  363. }
  364. /**
  365. * iwl_tx_queue_free - Deallocate DMA queue.
  366. * @txq: Transmit queue to deallocate.
  367. *
  368. * Empty queue by removing and destroying all BD's.
  369. * Free all buffers.
  370. * 0-fill, but do not free "txq" descriptor structure.
  371. */
  372. static void iwl_tx_queue_free(struct iwl_trans *trans, int txq_id)
  373. {
  374. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  375. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  376. struct device *dev = bus(trans)->dev;
  377. int i;
  378. if (WARN_ON(!txq))
  379. return;
  380. iwl_tx_queue_unmap(trans, txq_id);
  381. /* De-alloc array of command/tx buffers */
  382. if (txq_id == trans->shrd->cmd_queue)
  383. for (i = 0; i < txq->q.n_window; i++)
  384. kfree(txq->cmd[i]);
  385. /* De-alloc circular buffer of TFDs */
  386. if (txq->q.n_bd) {
  387. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  388. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  389. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  390. }
  391. /* De-alloc array of per-TFD driver data */
  392. kfree(txq->skbs);
  393. txq->skbs = NULL;
  394. /* deallocate arrays */
  395. kfree(txq->cmd);
  396. kfree(txq->meta);
  397. txq->cmd = NULL;
  398. txq->meta = NULL;
  399. /* 0-fill queue descriptor structure */
  400. memset(txq, 0, sizeof(*txq));
  401. }
  402. /**
  403. * iwl_trans_tx_free - Free TXQ Context
  404. *
  405. * Destroy all TX DMA queues and structures
  406. */
  407. static void iwl_trans_pcie_tx_free(struct iwl_trans *trans)
  408. {
  409. int txq_id;
  410. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  411. /* Tx queues */
  412. if (trans_pcie->txq) {
  413. for (txq_id = 0;
  414. txq_id < hw_params(trans).max_txq_num; txq_id++)
  415. iwl_tx_queue_free(trans, txq_id);
  416. }
  417. kfree(trans_pcie->txq);
  418. trans_pcie->txq = NULL;
  419. iwlagn_free_dma_ptr(trans, &trans_pcie->kw);
  420. iwlagn_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  421. }
  422. /**
  423. * iwl_trans_tx_alloc - allocate TX context
  424. * Allocate all Tx DMA structures and initialize them
  425. *
  426. * @param priv
  427. * @return error code
  428. */
  429. static int iwl_trans_tx_alloc(struct iwl_trans *trans)
  430. {
  431. int ret;
  432. int txq_id, slots_num;
  433. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  434. u16 scd_bc_tbls_size = hw_params(trans).max_txq_num *
  435. sizeof(struct iwlagn_scd_bc_tbl);
  436. /*It is not allowed to alloc twice, so warn when this happens.
  437. * We cannot rely on the previous allocation, so free and fail */
  438. if (WARN_ON(trans_pcie->txq)) {
  439. ret = -EINVAL;
  440. goto error;
  441. }
  442. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  443. scd_bc_tbls_size);
  444. if (ret) {
  445. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  446. goto error;
  447. }
  448. /* Alloc keep-warm buffer */
  449. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  450. if (ret) {
  451. IWL_ERR(trans, "Keep Warm allocation failed\n");
  452. goto error;
  453. }
  454. trans_pcie->txq = kcalloc(hw_params(trans).max_txq_num,
  455. sizeof(struct iwl_tx_queue), GFP_KERNEL);
  456. if (!trans_pcie->txq) {
  457. IWL_ERR(trans, "Not enough memory for txq\n");
  458. ret = ENOMEM;
  459. goto error;
  460. }
  461. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  462. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  463. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  464. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  465. ret = iwl_trans_txq_alloc(trans, &trans_pcie->txq[txq_id],
  466. slots_num, txq_id);
  467. if (ret) {
  468. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  469. goto error;
  470. }
  471. }
  472. return 0;
  473. error:
  474. iwl_trans_pcie_tx_free(trans);
  475. return ret;
  476. }
  477. static int iwl_tx_init(struct iwl_trans *trans)
  478. {
  479. int ret;
  480. int txq_id, slots_num;
  481. unsigned long flags;
  482. bool alloc = false;
  483. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  484. if (!trans_pcie->txq) {
  485. ret = iwl_trans_tx_alloc(trans);
  486. if (ret)
  487. goto error;
  488. alloc = true;
  489. }
  490. spin_lock_irqsave(&trans->shrd->lock, flags);
  491. /* Turn off all Tx DMA fifos */
  492. iwl_write_prph(bus(trans), SCD_TXFACT, 0);
  493. /* Tell NIC where to find the "keep warm" buffer */
  494. iwl_write_direct32(bus(trans), FH_KW_MEM_ADDR_REG,
  495. trans_pcie->kw.dma >> 4);
  496. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  497. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  498. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  499. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  500. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  501. ret = iwl_trans_txq_init(trans, &trans_pcie->txq[txq_id],
  502. slots_num, txq_id);
  503. if (ret) {
  504. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  505. goto error;
  506. }
  507. }
  508. return 0;
  509. error:
  510. /*Upon error, free only if we allocated something */
  511. if (alloc)
  512. iwl_trans_pcie_tx_free(trans);
  513. return ret;
  514. }
  515. static void iwl_set_pwr_vmain(struct iwl_trans *trans)
  516. {
  517. /*
  518. * (for documentation purposes)
  519. * to set power to V_AUX, do:
  520. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  521. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  522. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  523. ~APMG_PS_CTRL_MSK_PWR_SRC);
  524. */
  525. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  526. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  527. ~APMG_PS_CTRL_MSK_PWR_SRC);
  528. }
  529. static int iwl_nic_init(struct iwl_trans *trans)
  530. {
  531. unsigned long flags;
  532. /* nic_init */
  533. spin_lock_irqsave(&trans->shrd->lock, flags);
  534. iwl_apm_init(priv(trans));
  535. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  536. iwl_write8(bus(trans), CSR_INT_COALESCING,
  537. IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  538. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  539. iwl_set_pwr_vmain(trans);
  540. iwl_nic_config(priv(trans));
  541. /* Allocate the RX queue, or reset if it is already allocated */
  542. iwl_rx_init(trans);
  543. /* Allocate or reset and init all Tx and Command queues */
  544. if (iwl_tx_init(trans))
  545. return -ENOMEM;
  546. if (hw_params(trans).shadow_reg_enable) {
  547. /* enable shadow regs in HW */
  548. iwl_set_bit(bus(trans), CSR_MAC_SHADOW_REG_CTRL,
  549. 0x800FFFFF);
  550. }
  551. set_bit(STATUS_INIT, &trans->shrd->status);
  552. return 0;
  553. }
  554. #define HW_READY_TIMEOUT (50)
  555. /* Note: returns poll_bit return value, which is >= 0 if success */
  556. static int iwl_set_hw_ready(struct iwl_trans *trans)
  557. {
  558. int ret;
  559. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  560. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  561. /* See if we got it */
  562. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  563. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  564. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  565. HW_READY_TIMEOUT);
  566. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  567. return ret;
  568. }
  569. /* Note: returns standard 0/-ERROR code */
  570. static int iwl_trans_pcie_prepare_card_hw(struct iwl_trans *trans)
  571. {
  572. int ret;
  573. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  574. ret = iwl_set_hw_ready(trans);
  575. if (ret >= 0)
  576. return 0;
  577. /* If HW is not ready, prepare the conditions to check again */
  578. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  579. CSR_HW_IF_CONFIG_REG_PREPARE);
  580. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  581. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  582. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  583. if (ret < 0)
  584. return ret;
  585. /* HW should be ready by now, check again. */
  586. ret = iwl_set_hw_ready(trans);
  587. if (ret >= 0)
  588. return 0;
  589. return ret;
  590. }
  591. #define IWL_AC_UNSET -1
  592. struct queue_to_fifo_ac {
  593. s8 fifo, ac;
  594. };
  595. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  596. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  597. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  598. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  599. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  600. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  601. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  602. { IWL_TX_FIFO_UNUSED, 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. };
  608. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  609. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  610. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  611. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  612. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  613. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  614. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  615. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  616. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  617. { IWL_TX_FIFO_BE_IPAN, 2, },
  618. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  619. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  620. };
  621. static const u8 iwlagn_bss_ac_to_fifo[] = {
  622. IWL_TX_FIFO_VO,
  623. IWL_TX_FIFO_VI,
  624. IWL_TX_FIFO_BE,
  625. IWL_TX_FIFO_BK,
  626. };
  627. static const u8 iwlagn_bss_ac_to_queue[] = {
  628. 0, 1, 2, 3,
  629. };
  630. static const u8 iwlagn_pan_ac_to_fifo[] = {
  631. IWL_TX_FIFO_VO_IPAN,
  632. IWL_TX_FIFO_VI_IPAN,
  633. IWL_TX_FIFO_BE_IPAN,
  634. IWL_TX_FIFO_BK_IPAN,
  635. };
  636. static const u8 iwlagn_pan_ac_to_queue[] = {
  637. 7, 6, 5, 4,
  638. };
  639. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  640. {
  641. int ret;
  642. struct iwl_trans_pcie *trans_pcie =
  643. IWL_TRANS_GET_PCIE_TRANS(trans);
  644. trans->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  645. trans_pcie->ac_to_queue[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_queue;
  646. trans_pcie->ac_to_queue[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_queue;
  647. trans_pcie->ac_to_fifo[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_fifo;
  648. trans_pcie->ac_to_fifo[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_fifo;
  649. trans_pcie->mcast_queue[IWL_RXON_CTX_BSS] = 0;
  650. trans_pcie->mcast_queue[IWL_RXON_CTX_PAN] = IWL_IPAN_MCAST_QUEUE;
  651. if ((hw_params(trans).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  652. iwl_trans_pcie_prepare_card_hw(trans)) {
  653. IWL_WARN(trans, "Exit HW not ready\n");
  654. return -EIO;
  655. }
  656. /* If platform's RF_KILL switch is NOT set to KILL */
  657. if (iwl_read32(bus(trans), CSR_GP_CNTRL) &
  658. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  659. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  660. else
  661. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  662. if (iwl_is_rfkill(trans->shrd)) {
  663. iwl_set_hw_rfkill_state(priv(trans), true);
  664. iwl_enable_interrupts(trans);
  665. return -ERFKILL;
  666. }
  667. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  668. ret = iwl_nic_init(trans);
  669. if (ret) {
  670. IWL_ERR(trans, "Unable to init nic\n");
  671. return ret;
  672. }
  673. /* make sure rfkill handshake bits are cleared */
  674. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  675. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR,
  676. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  677. /* clear (again), then enable host interrupts */
  678. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  679. iwl_enable_interrupts(trans);
  680. /* really 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, CSR_UCODE_SW_BIT_RFKILL);
  683. return 0;
  684. }
  685. /*
  686. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  687. * must be called under priv->shrd->lock and mac access
  688. */
  689. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  690. {
  691. iwl_write_prph(bus(trans), SCD_TXFACT, mask);
  692. }
  693. static void iwl_trans_pcie_tx_start(struct iwl_trans *trans)
  694. {
  695. const struct queue_to_fifo_ac *queue_to_fifo;
  696. struct iwl_trans_pcie *trans_pcie =
  697. IWL_TRANS_GET_PCIE_TRANS(trans);
  698. u32 a;
  699. unsigned long flags;
  700. int i, chan;
  701. u32 reg_val;
  702. spin_lock_irqsave(&trans->shrd->lock, flags);
  703. trans_pcie->scd_base_addr =
  704. iwl_read_prph(bus(trans), SCD_SRAM_BASE_ADDR);
  705. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  706. /* reset conext data memory */
  707. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  708. a += 4)
  709. iwl_write_targ_mem(bus(trans), a, 0);
  710. /* reset tx status memory */
  711. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  712. a += 4)
  713. iwl_write_targ_mem(bus(trans), a, 0);
  714. for (; a < trans_pcie->scd_base_addr +
  715. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(trans).max_txq_num);
  716. a += 4)
  717. iwl_write_targ_mem(bus(trans), a, 0);
  718. iwl_write_prph(bus(trans), SCD_DRAM_BASE_ADDR,
  719. trans_pcie->scd_bc_tbls.dma >> 10);
  720. /* Enable DMA channel */
  721. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  722. iwl_write_direct32(bus(trans), FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  723. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  724. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  725. /* Update FH chicken bits */
  726. reg_val = iwl_read_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG);
  727. iwl_write_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG,
  728. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  729. iwl_write_prph(bus(trans), SCD_QUEUECHAIN_SEL,
  730. SCD_QUEUECHAIN_SEL_ALL(trans));
  731. iwl_write_prph(bus(trans), SCD_AGGR_SEL, 0);
  732. /* initiate the queues */
  733. for (i = 0; i < hw_params(trans).max_txq_num; i++) {
  734. iwl_write_prph(bus(trans), SCD_QUEUE_RDPTR(i), 0);
  735. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR, 0 | (i << 8));
  736. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  737. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  738. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  739. SCD_CONTEXT_QUEUE_OFFSET(i) +
  740. sizeof(u32),
  741. ((SCD_WIN_SIZE <<
  742. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  743. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  744. ((SCD_FRAME_LIMIT <<
  745. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  746. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  747. }
  748. iwl_write_prph(bus(trans), SCD_INTERRUPT_MASK,
  749. IWL_MASK(0, hw_params(trans).max_txq_num));
  750. /* Activate all Tx DMA/FIFO channels */
  751. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  752. /* map queues to FIFOs */
  753. if (trans->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  754. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  755. else
  756. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  757. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  758. /* make sure all queue are not stopped */
  759. memset(&trans_pcie->queue_stopped[0], 0,
  760. sizeof(trans_pcie->queue_stopped));
  761. for (i = 0; i < 4; i++)
  762. atomic_set(&trans_pcie->queue_stop_count[i], 0);
  763. /* reset to 0 to enable all the queue first */
  764. trans_pcie->txq_ctx_active_msk = 0;
  765. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  766. IWLAGN_FIRST_AMPDU_QUEUE);
  767. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  768. IWLAGN_FIRST_AMPDU_QUEUE);
  769. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  770. int fifo = queue_to_fifo[i].fifo;
  771. int ac = queue_to_fifo[i].ac;
  772. iwl_txq_ctx_activate(trans_pcie, i);
  773. if (fifo == IWL_TX_FIFO_UNUSED)
  774. continue;
  775. if (ac != IWL_AC_UNSET)
  776. iwl_set_swq_id(&trans_pcie->txq[i], ac, i);
  777. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[i],
  778. fifo, 0);
  779. }
  780. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  781. /* Enable L1-Active */
  782. iwl_clear_bits_prph(bus(trans), APMG_PCIDEV_STT_REG,
  783. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  784. }
  785. /**
  786. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  787. */
  788. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  789. {
  790. int ch, txq_id;
  791. unsigned long flags;
  792. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  793. /* Turn off all Tx DMA fifos */
  794. spin_lock_irqsave(&trans->shrd->lock, flags);
  795. iwl_trans_txq_set_sched(trans, 0);
  796. /* Stop each Tx DMA channel, and wait for it to be idle */
  797. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  798. iwl_write_direct32(bus(trans),
  799. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  800. if (iwl_poll_direct_bit(bus(trans), FH_TSSR_TX_STATUS_REG,
  801. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  802. 1000))
  803. IWL_ERR(trans, "Failing on timeout while stopping"
  804. " DMA channel %d [0x%08x]", ch,
  805. iwl_read_direct32(bus(trans),
  806. FH_TSSR_TX_STATUS_REG));
  807. }
  808. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  809. if (!trans_pcie->txq) {
  810. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  811. return 0;
  812. }
  813. /* Unmap DMA from host system and free skb's */
  814. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  815. iwl_tx_queue_unmap(trans, txq_id);
  816. return 0;
  817. }
  818. static void iwl_trans_pcie_disable_sync_irq(struct iwl_trans *trans)
  819. {
  820. unsigned long flags;
  821. struct iwl_trans_pcie *trans_pcie =
  822. IWL_TRANS_GET_PCIE_TRANS(trans);
  823. spin_lock_irqsave(&trans->shrd->lock, flags);
  824. iwl_disable_interrupts(trans);
  825. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  826. /* wait to make sure we flush pending tasklet*/
  827. synchronize_irq(bus(trans)->irq);
  828. tasklet_kill(&trans_pcie->irq_tasklet);
  829. }
  830. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  831. {
  832. /* stop and reset the on-board processor */
  833. iwl_write32(bus(trans), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  834. /* tell the device to stop sending interrupts */
  835. iwl_trans_pcie_disable_sync_irq(trans);
  836. /* device going down, Stop using ICT table */
  837. iwl_disable_ict(trans);
  838. /*
  839. * If a HW restart happens during firmware loading,
  840. * then the firmware loading might call this function
  841. * and later it might be called again due to the
  842. * restart. So don't process again if the device is
  843. * already dead.
  844. */
  845. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  846. iwl_trans_tx_stop(trans);
  847. iwl_trans_rx_stop(trans);
  848. /* Power-down device's busmaster DMA clocks */
  849. iwl_write_prph(bus(trans), APMG_CLK_DIS_REG,
  850. APMG_CLK_VAL_DMA_CLK_RQT);
  851. udelay(5);
  852. }
  853. /* Make sure (redundant) we've released our request to stay awake */
  854. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  855. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  856. /* Stop the device, and put it in low power state */
  857. iwl_apm_stop(priv(trans));
  858. }
  859. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  860. struct iwl_device_cmd *dev_cmd, enum iwl_rxon_context_id ctx,
  861. u8 sta_id)
  862. {
  863. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  864. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  865. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  866. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
  867. struct iwl_cmd_meta *out_meta;
  868. struct iwl_tx_queue *txq;
  869. struct iwl_queue *q;
  870. dma_addr_t phys_addr = 0;
  871. dma_addr_t txcmd_phys;
  872. dma_addr_t scratch_phys;
  873. u16 len, firstlen, secondlen;
  874. u16 seq_number = 0;
  875. u8 wait_write_ptr = 0;
  876. u8 txq_id;
  877. u8 tid = 0;
  878. bool is_agg = false;
  879. __le16 fc = hdr->frame_control;
  880. u8 hdr_len = ieee80211_hdrlen(fc);
  881. /*
  882. * Send this frame after DTIM -- there's a special queue
  883. * reserved for this for contexts that support AP mode.
  884. */
  885. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  886. txq_id = trans_pcie->mcast_queue[ctx];
  887. /*
  888. * The microcode will clear the more data
  889. * bit in the last frame it transmits.
  890. */
  891. hdr->frame_control |=
  892. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  893. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  894. txq_id = IWL_AUX_QUEUE;
  895. else
  896. txq_id =
  897. trans_pcie->ac_to_queue[ctx][skb_get_queue_mapping(skb)];
  898. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  899. u8 *qc = NULL;
  900. struct iwl_tid_data *tid_data;
  901. qc = ieee80211_get_qos_ctl(hdr);
  902. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  903. tid_data = &trans->shrd->tid_data[sta_id][tid];
  904. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  905. return -1;
  906. seq_number = tid_data->seq_number;
  907. seq_number &= IEEE80211_SCTL_SEQ;
  908. hdr->seq_ctrl = hdr->seq_ctrl &
  909. cpu_to_le16(IEEE80211_SCTL_FRAG);
  910. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  911. seq_number += 0x10;
  912. /* aggregation is on for this <sta,tid> */
  913. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  914. WARN_ON(tid_data->agg.state != IWL_AGG_ON);
  915. txq_id = tid_data->agg.txq_id;
  916. is_agg = true;
  917. }
  918. }
  919. /* Copy MAC header from skb into command buffer */
  920. memcpy(tx_cmd->hdr, hdr, hdr_len);
  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) && !ieee80211_is_qos_nullfunc(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. } else {
  1159. iwl_disable_interrupts(trans);
  1160. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  1161. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1162. }
  1163. return 0;
  1164. }
  1165. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1166. {
  1167. bool hw_rfkill = false;
  1168. iwl_enable_interrupts(trans);
  1169. if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
  1170. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1171. hw_rfkill = true;
  1172. if (hw_rfkill)
  1173. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1174. else
  1175. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1176. iwl_set_hw_rfkill_state(priv(trans), hw_rfkill);
  1177. return 0;
  1178. }
  1179. #endif /* CONFIG_PM_SLEEP */
  1180. static void iwl_trans_pcie_wake_any_queue(struct iwl_trans *trans,
  1181. enum iwl_rxon_context_id ctx)
  1182. {
  1183. u8 ac, txq_id;
  1184. struct iwl_trans_pcie *trans_pcie =
  1185. IWL_TRANS_GET_PCIE_TRANS(trans);
  1186. for (ac = 0; ac < AC_NUM; ac++) {
  1187. txq_id = trans_pcie->ac_to_queue[ctx][ac];
  1188. IWL_DEBUG_INFO(trans, "Queue Status: Q[%d] %s\n",
  1189. ac,
  1190. (atomic_read(&trans_pcie->queue_stop_count[ac]) > 0)
  1191. ? "stopped" : "awake");
  1192. iwl_wake_queue(trans, &trans_pcie->txq[txq_id]);
  1193. }
  1194. }
  1195. const struct iwl_trans_ops trans_ops_pcie;
  1196. static struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd)
  1197. {
  1198. struct iwl_trans *iwl_trans = kzalloc(sizeof(struct iwl_trans) +
  1199. sizeof(struct iwl_trans_pcie),
  1200. GFP_KERNEL);
  1201. if (iwl_trans) {
  1202. struct iwl_trans_pcie *trans_pcie =
  1203. IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
  1204. iwl_trans->ops = &trans_ops_pcie;
  1205. iwl_trans->shrd = shrd;
  1206. trans_pcie->trans = iwl_trans;
  1207. spin_lock_init(&iwl_trans->hcmd_lock);
  1208. }
  1209. return iwl_trans;
  1210. }
  1211. static void iwl_trans_pcie_stop_queue(struct iwl_trans *trans, int txq_id)
  1212. {
  1213. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1214. iwl_stop_queue(trans, &trans_pcie->txq[txq_id]);
  1215. }
  1216. #define IWL_FLUSH_WAIT_MS 2000
  1217. static int iwl_trans_pcie_wait_tx_queue_empty(struct iwl_trans *trans)
  1218. {
  1219. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1220. struct iwl_tx_queue *txq;
  1221. struct iwl_queue *q;
  1222. int cnt;
  1223. unsigned long now = jiffies;
  1224. int ret = 0;
  1225. /* waiting for all the tx frames complete might take a while */
  1226. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1227. if (cnt == trans->shrd->cmd_queue)
  1228. continue;
  1229. txq = &trans_pcie->txq[cnt];
  1230. q = &txq->q;
  1231. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1232. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1233. msleep(1);
  1234. if (q->read_ptr != q->write_ptr) {
  1235. IWL_ERR(trans, "fail to flush all tx fifo queues\n");
  1236. ret = -ETIMEDOUT;
  1237. break;
  1238. }
  1239. }
  1240. return ret;
  1241. }
  1242. /*
  1243. * On every watchdog tick we check (latest) time stamp. If it does not
  1244. * change during timeout period and queue is not empty we reset firmware.
  1245. */
  1246. static int iwl_trans_pcie_check_stuck_queue(struct iwl_trans *trans, int cnt)
  1247. {
  1248. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1249. struct iwl_tx_queue *txq = &trans_pcie->txq[cnt];
  1250. struct iwl_queue *q = &txq->q;
  1251. unsigned long timeout;
  1252. if (q->read_ptr == q->write_ptr) {
  1253. txq->time_stamp = jiffies;
  1254. return 0;
  1255. }
  1256. timeout = txq->time_stamp +
  1257. msecs_to_jiffies(hw_params(trans).wd_timeout);
  1258. if (time_after(jiffies, timeout)) {
  1259. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", q->id,
  1260. hw_params(trans).wd_timeout);
  1261. IWL_ERR(trans, "Current read_ptr %d write_ptr %d\n",
  1262. q->read_ptr, q->write_ptr);
  1263. return 1;
  1264. }
  1265. return 0;
  1266. }
  1267. static const char *get_fh_string(int cmd)
  1268. {
  1269. switch (cmd) {
  1270. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1271. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1272. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1273. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1274. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1275. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1276. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1277. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1278. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1279. default:
  1280. return "UNKNOWN";
  1281. }
  1282. }
  1283. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1284. {
  1285. int i;
  1286. #ifdef CONFIG_IWLWIFI_DEBUG
  1287. int pos = 0;
  1288. size_t bufsz = 0;
  1289. #endif
  1290. static const u32 fh_tbl[] = {
  1291. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1292. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1293. FH_RSCSR_CHNL0_WPTR,
  1294. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1295. FH_MEM_RSSR_SHARED_CTRL_REG,
  1296. FH_MEM_RSSR_RX_STATUS_REG,
  1297. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1298. FH_TSSR_TX_STATUS_REG,
  1299. FH_TSSR_TX_ERROR_REG
  1300. };
  1301. #ifdef CONFIG_IWLWIFI_DEBUG
  1302. if (display) {
  1303. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1304. *buf = kmalloc(bufsz, GFP_KERNEL);
  1305. if (!*buf)
  1306. return -ENOMEM;
  1307. pos += scnprintf(*buf + pos, bufsz - pos,
  1308. "FH register values:\n");
  1309. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1310. pos += scnprintf(*buf + pos, bufsz - pos,
  1311. " %34s: 0X%08x\n",
  1312. get_fh_string(fh_tbl[i]),
  1313. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1314. }
  1315. return pos;
  1316. }
  1317. #endif
  1318. IWL_ERR(trans, "FH register values:\n");
  1319. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1320. IWL_ERR(trans, " %34s: 0X%08x\n",
  1321. get_fh_string(fh_tbl[i]),
  1322. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1323. }
  1324. return 0;
  1325. }
  1326. static const char *get_csr_string(int cmd)
  1327. {
  1328. switch (cmd) {
  1329. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1330. IWL_CMD(CSR_INT_COALESCING);
  1331. IWL_CMD(CSR_INT);
  1332. IWL_CMD(CSR_INT_MASK);
  1333. IWL_CMD(CSR_FH_INT_STATUS);
  1334. IWL_CMD(CSR_GPIO_IN);
  1335. IWL_CMD(CSR_RESET);
  1336. IWL_CMD(CSR_GP_CNTRL);
  1337. IWL_CMD(CSR_HW_REV);
  1338. IWL_CMD(CSR_EEPROM_REG);
  1339. IWL_CMD(CSR_EEPROM_GP);
  1340. IWL_CMD(CSR_OTP_GP_REG);
  1341. IWL_CMD(CSR_GIO_REG);
  1342. IWL_CMD(CSR_GP_UCODE_REG);
  1343. IWL_CMD(CSR_GP_DRIVER_REG);
  1344. IWL_CMD(CSR_UCODE_DRV_GP1);
  1345. IWL_CMD(CSR_UCODE_DRV_GP2);
  1346. IWL_CMD(CSR_LED_REG);
  1347. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1348. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1349. IWL_CMD(CSR_ANA_PLL_CFG);
  1350. IWL_CMD(CSR_HW_REV_WA_REG);
  1351. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1352. default:
  1353. return "UNKNOWN";
  1354. }
  1355. }
  1356. void iwl_dump_csr(struct iwl_trans *trans)
  1357. {
  1358. int i;
  1359. static const u32 csr_tbl[] = {
  1360. CSR_HW_IF_CONFIG_REG,
  1361. CSR_INT_COALESCING,
  1362. CSR_INT,
  1363. CSR_INT_MASK,
  1364. CSR_FH_INT_STATUS,
  1365. CSR_GPIO_IN,
  1366. CSR_RESET,
  1367. CSR_GP_CNTRL,
  1368. CSR_HW_REV,
  1369. CSR_EEPROM_REG,
  1370. CSR_EEPROM_GP,
  1371. CSR_OTP_GP_REG,
  1372. CSR_GIO_REG,
  1373. CSR_GP_UCODE_REG,
  1374. CSR_GP_DRIVER_REG,
  1375. CSR_UCODE_DRV_GP1,
  1376. CSR_UCODE_DRV_GP2,
  1377. CSR_LED_REG,
  1378. CSR_DRAM_INT_TBL_REG,
  1379. CSR_GIO_CHICKEN_BITS,
  1380. CSR_ANA_PLL_CFG,
  1381. CSR_HW_REV_WA_REG,
  1382. CSR_DBG_HPET_MEM_REG
  1383. };
  1384. IWL_ERR(trans, "CSR values:\n");
  1385. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1386. "CSR_INT_PERIODIC_REG)\n");
  1387. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1388. IWL_ERR(trans, " %25s: 0X%08x\n",
  1389. get_csr_string(csr_tbl[i]),
  1390. iwl_read32(bus(trans), csr_tbl[i]));
  1391. }
  1392. }
  1393. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1394. /* create and remove of files */
  1395. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1396. if (!debugfs_create_file(#name, mode, parent, trans, \
  1397. &iwl_dbgfs_##name##_ops)) \
  1398. return -ENOMEM; \
  1399. } while (0)
  1400. /* file operation */
  1401. #define DEBUGFS_READ_FUNC(name) \
  1402. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1403. char __user *user_buf, \
  1404. size_t count, loff_t *ppos);
  1405. #define DEBUGFS_WRITE_FUNC(name) \
  1406. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1407. const char __user *user_buf, \
  1408. size_t count, loff_t *ppos);
  1409. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1410. {
  1411. file->private_data = inode->i_private;
  1412. return 0;
  1413. }
  1414. #define DEBUGFS_READ_FILE_OPS(name) \
  1415. DEBUGFS_READ_FUNC(name); \
  1416. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1417. .read = iwl_dbgfs_##name##_read, \
  1418. .open = iwl_dbgfs_open_file_generic, \
  1419. .llseek = generic_file_llseek, \
  1420. };
  1421. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1422. DEBUGFS_WRITE_FUNC(name); \
  1423. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1424. .write = iwl_dbgfs_##name##_write, \
  1425. .open = iwl_dbgfs_open_file_generic, \
  1426. .llseek = generic_file_llseek, \
  1427. };
  1428. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1429. DEBUGFS_READ_FUNC(name); \
  1430. DEBUGFS_WRITE_FUNC(name); \
  1431. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1432. .write = iwl_dbgfs_##name##_write, \
  1433. .read = iwl_dbgfs_##name##_read, \
  1434. .open = iwl_dbgfs_open_file_generic, \
  1435. .llseek = generic_file_llseek, \
  1436. };
  1437. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1438. char __user *user_buf,
  1439. size_t count, loff_t *ppos)
  1440. {
  1441. struct iwl_trans *trans = file->private_data;
  1442. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1443. struct iwl_tx_queue *txq;
  1444. struct iwl_queue *q;
  1445. char *buf;
  1446. int pos = 0;
  1447. int cnt;
  1448. int ret;
  1449. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1450. if (!trans_pcie->txq) {
  1451. IWL_ERR(trans, "txq not ready\n");
  1452. return -EAGAIN;
  1453. }
  1454. buf = kzalloc(bufsz, GFP_KERNEL);
  1455. if (!buf)
  1456. return -ENOMEM;
  1457. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1458. txq = &trans_pcie->txq[cnt];
  1459. q = &txq->q;
  1460. pos += scnprintf(buf + pos, bufsz - pos,
  1461. "hwq %.2d: read=%u write=%u stop=%d"
  1462. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1463. cnt, q->read_ptr, q->write_ptr,
  1464. !!test_bit(cnt, trans_pcie->queue_stopped),
  1465. txq->swq_id, txq->swq_id & 3,
  1466. (txq->swq_id >> 2) & 0x1f);
  1467. if (cnt >= 4)
  1468. continue;
  1469. /* for the ACs, display the stop count too */
  1470. pos += scnprintf(buf + pos, bufsz - pos,
  1471. " stop-count: %d\n",
  1472. atomic_read(&trans_pcie->queue_stop_count[cnt]));
  1473. }
  1474. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1475. kfree(buf);
  1476. return ret;
  1477. }
  1478. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1479. char __user *user_buf,
  1480. size_t count, loff_t *ppos) {
  1481. struct iwl_trans *trans = file->private_data;
  1482. struct iwl_trans_pcie *trans_pcie =
  1483. IWL_TRANS_GET_PCIE_TRANS(trans);
  1484. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1485. char buf[256];
  1486. int pos = 0;
  1487. const size_t bufsz = sizeof(buf);
  1488. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1489. rxq->read);
  1490. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1491. rxq->write);
  1492. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1493. rxq->free_count);
  1494. if (rxq->rb_stts) {
  1495. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1496. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1497. } else {
  1498. pos += scnprintf(buf + pos, bufsz - pos,
  1499. "closed_rb_num: Not Allocated\n");
  1500. }
  1501. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1502. }
  1503. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1504. char __user *user_buf,
  1505. size_t count, loff_t *ppos)
  1506. {
  1507. struct iwl_trans *trans = file->private_data;
  1508. char *buf;
  1509. int pos = 0;
  1510. ssize_t ret = -ENOMEM;
  1511. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1512. if (buf) {
  1513. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1514. kfree(buf);
  1515. }
  1516. return ret;
  1517. }
  1518. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1519. const char __user *user_buf,
  1520. size_t count, loff_t *ppos)
  1521. {
  1522. struct iwl_trans *trans = file->private_data;
  1523. u32 event_log_flag;
  1524. char buf[8];
  1525. int buf_size;
  1526. memset(buf, 0, sizeof(buf));
  1527. buf_size = min(count, sizeof(buf) - 1);
  1528. if (copy_from_user(buf, user_buf, buf_size))
  1529. return -EFAULT;
  1530. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1531. return -EFAULT;
  1532. if (event_log_flag == 1)
  1533. iwl_dump_nic_event_log(trans, true, NULL, false);
  1534. return count;
  1535. }
  1536. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1537. char __user *user_buf,
  1538. size_t count, loff_t *ppos) {
  1539. struct iwl_trans *trans = file->private_data;
  1540. struct iwl_trans_pcie *trans_pcie =
  1541. IWL_TRANS_GET_PCIE_TRANS(trans);
  1542. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1543. int pos = 0;
  1544. char *buf;
  1545. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1546. ssize_t ret;
  1547. buf = kzalloc(bufsz, GFP_KERNEL);
  1548. if (!buf) {
  1549. IWL_ERR(trans, "Can not allocate Buffer\n");
  1550. return -ENOMEM;
  1551. }
  1552. pos += scnprintf(buf + pos, bufsz - pos,
  1553. "Interrupt Statistics Report:\n");
  1554. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1555. isr_stats->hw);
  1556. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1557. isr_stats->sw);
  1558. if (isr_stats->sw || isr_stats->hw) {
  1559. pos += scnprintf(buf + pos, bufsz - pos,
  1560. "\tLast Restarting Code: 0x%X\n",
  1561. isr_stats->err_code);
  1562. }
  1563. #ifdef CONFIG_IWLWIFI_DEBUG
  1564. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1565. isr_stats->sch);
  1566. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1567. isr_stats->alive);
  1568. #endif
  1569. pos += scnprintf(buf + pos, bufsz - pos,
  1570. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1571. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1572. isr_stats->ctkill);
  1573. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1574. isr_stats->wakeup);
  1575. pos += scnprintf(buf + pos, bufsz - pos,
  1576. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1577. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1578. isr_stats->tx);
  1579. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1580. isr_stats->unhandled);
  1581. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1582. kfree(buf);
  1583. return ret;
  1584. }
  1585. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1586. const char __user *user_buf,
  1587. size_t count, loff_t *ppos)
  1588. {
  1589. struct iwl_trans *trans = file->private_data;
  1590. struct iwl_trans_pcie *trans_pcie =
  1591. IWL_TRANS_GET_PCIE_TRANS(trans);
  1592. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1593. char buf[8];
  1594. int buf_size;
  1595. u32 reset_flag;
  1596. memset(buf, 0, sizeof(buf));
  1597. buf_size = min(count, sizeof(buf) - 1);
  1598. if (copy_from_user(buf, user_buf, buf_size))
  1599. return -EFAULT;
  1600. if (sscanf(buf, "%x", &reset_flag) != 1)
  1601. return -EFAULT;
  1602. if (reset_flag == 0)
  1603. memset(isr_stats, 0, sizeof(*isr_stats));
  1604. return count;
  1605. }
  1606. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1607. const char __user *user_buf,
  1608. size_t count, loff_t *ppos)
  1609. {
  1610. struct iwl_trans *trans = file->private_data;
  1611. char buf[8];
  1612. int buf_size;
  1613. int csr;
  1614. memset(buf, 0, sizeof(buf));
  1615. buf_size = min(count, sizeof(buf) - 1);
  1616. if (copy_from_user(buf, user_buf, buf_size))
  1617. return -EFAULT;
  1618. if (sscanf(buf, "%d", &csr) != 1)
  1619. return -EFAULT;
  1620. iwl_dump_csr(trans);
  1621. return count;
  1622. }
  1623. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1624. char __user *user_buf,
  1625. size_t count, loff_t *ppos)
  1626. {
  1627. struct iwl_trans *trans = file->private_data;
  1628. char *buf;
  1629. int pos = 0;
  1630. ssize_t ret = -EFAULT;
  1631. ret = pos = iwl_dump_fh(trans, &buf, true);
  1632. if (buf) {
  1633. ret = simple_read_from_buffer(user_buf,
  1634. count, ppos, buf, pos);
  1635. kfree(buf);
  1636. }
  1637. return ret;
  1638. }
  1639. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1640. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1641. DEBUGFS_READ_FILE_OPS(fh_reg);
  1642. DEBUGFS_READ_FILE_OPS(rx_queue);
  1643. DEBUGFS_READ_FILE_OPS(tx_queue);
  1644. DEBUGFS_WRITE_FILE_OPS(csr);
  1645. /*
  1646. * Create the debugfs files and directories
  1647. *
  1648. */
  1649. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1650. struct dentry *dir)
  1651. {
  1652. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1653. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1654. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1655. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1656. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1657. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1658. return 0;
  1659. }
  1660. #else
  1661. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1662. struct dentry *dir)
  1663. { return 0; }
  1664. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1665. const struct iwl_trans_ops trans_ops_pcie = {
  1666. .alloc = iwl_trans_pcie_alloc,
  1667. .request_irq = iwl_trans_pcie_request_irq,
  1668. .start_device = iwl_trans_pcie_start_device,
  1669. .prepare_card_hw = iwl_trans_pcie_prepare_card_hw,
  1670. .stop_device = iwl_trans_pcie_stop_device,
  1671. .tx_start = iwl_trans_pcie_tx_start,
  1672. .wake_any_queue = iwl_trans_pcie_wake_any_queue,
  1673. .send_cmd = iwl_trans_pcie_send_cmd,
  1674. .tx = iwl_trans_pcie_tx,
  1675. .reclaim = iwl_trans_pcie_reclaim,
  1676. .tx_agg_disable = iwl_trans_pcie_tx_agg_disable,
  1677. .tx_agg_alloc = iwl_trans_pcie_tx_agg_alloc,
  1678. .tx_agg_setup = iwl_trans_pcie_tx_agg_setup,
  1679. .kick_nic = iwl_trans_pcie_kick_nic,
  1680. .free = iwl_trans_pcie_free,
  1681. .stop_queue = iwl_trans_pcie_stop_queue,
  1682. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1683. .wait_tx_queue_empty = iwl_trans_pcie_wait_tx_queue_empty,
  1684. .check_stuck_queue = iwl_trans_pcie_check_stuck_queue,
  1685. #ifdef CONFIG_PM_SLEEP
  1686. .suspend = iwl_trans_pcie_suspend,
  1687. .resume = iwl_trans_pcie_resume,
  1688. #endif
  1689. };