iwl-trans-pcie.c 54 KB

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