trans.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/pci.h>
  64. #include <linux/pci-aspm.h>
  65. #include <linux/interrupt.h>
  66. #include <linux/debugfs.h>
  67. #include <linux/sched.h>
  68. #include <linux/bitops.h>
  69. #include <linux/gfp.h>
  70. #include "iwl-drv.h"
  71. #include "iwl-trans.h"
  72. #include "iwl-csr.h"
  73. #include "iwl-prph.h"
  74. #include "iwl-agn-hw.h"
  75. #include "internal.h"
  76. /* FIXME: need to abstract out TX command (once we know what it looks like) */
  77. #include "dvm/commands.h"
  78. #define SCD_QUEUECHAIN_SEL_ALL(trans, trans_pcie) \
  79. (((1<<trans->cfg->base_params->num_of_queues) - 1) &\
  80. (~(1<<(trans_pcie)->cmd_queue)))
  81. static int iwl_trans_rx_alloc(struct iwl_trans *trans)
  82. {
  83. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  84. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  85. struct device *dev = trans->dev;
  86. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  87. spin_lock_init(&rxq->lock);
  88. if (WARN_ON(rxq->bd || rxq->rb_stts))
  89. return -EINVAL;
  90. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  91. rxq->bd = dma_zalloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  92. &rxq->bd_dma, GFP_KERNEL);
  93. if (!rxq->bd)
  94. goto err_bd;
  95. /*Allocate the driver's pointer to receive buffer status */
  96. rxq->rb_stts = dma_zalloc_coherent(dev, sizeof(*rxq->rb_stts),
  97. &rxq->rb_stts_dma, GFP_KERNEL);
  98. if (!rxq->rb_stts)
  99. goto err_rb_stts;
  100. return 0;
  101. err_rb_stts:
  102. dma_free_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  103. rxq->bd, rxq->bd_dma);
  104. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  105. rxq->bd = NULL;
  106. err_bd:
  107. return -ENOMEM;
  108. }
  109. static void iwl_trans_rxq_free_rx_bufs(struct iwl_trans *trans)
  110. {
  111. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  112. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  113. int i;
  114. /* Fill the rx_used queue with _all_ of the Rx buffers */
  115. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  116. /* In the reset function, these buffers may have been allocated
  117. * to an SKB, so we need to unmap and free potential storage */
  118. if (rxq->pool[i].page != NULL) {
  119. dma_unmap_page(trans->dev, rxq->pool[i].page_dma,
  120. PAGE_SIZE << trans_pcie->rx_page_order,
  121. DMA_FROM_DEVICE);
  122. __free_pages(rxq->pool[i].page,
  123. trans_pcie->rx_page_order);
  124. rxq->pool[i].page = NULL;
  125. }
  126. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  127. }
  128. }
  129. static void iwl_trans_rx_hw_init(struct iwl_trans *trans,
  130. struct iwl_rx_queue *rxq)
  131. {
  132. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  133. u32 rb_size;
  134. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  135. u32 rb_timeout = RX_RB_TIMEOUT; /* FIXME: RX_RB_TIMEOUT for all devices? */
  136. if (trans_pcie->rx_buf_size_8k)
  137. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  138. else
  139. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  140. /* Stop Rx DMA */
  141. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  142. /* Reset driver's Rx queue write index */
  143. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  144. /* Tell device where to find RBD circular buffer in DRAM */
  145. iwl_write_direct32(trans, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  146. (u32)(rxq->bd_dma >> 8));
  147. /* Tell device where in DRAM to update its Rx status */
  148. iwl_write_direct32(trans, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  149. rxq->rb_stts_dma >> 4);
  150. /* Enable Rx DMA
  151. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  152. * the credit mechanism in 5000 HW RX FIFO
  153. * Direct rx interrupts to hosts
  154. * Rx buffer size 4 or 8k
  155. * RB timeout 0x10
  156. * 256 RBDs
  157. */
  158. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  159. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  160. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  161. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  162. rb_size|
  163. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  164. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  165. /* Set interrupt coalescing timer to default (2048 usecs) */
  166. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  167. }
  168. static int iwl_rx_init(struct iwl_trans *trans)
  169. {
  170. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  171. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  172. int i, err;
  173. unsigned long flags;
  174. if (!rxq->bd) {
  175. err = iwl_trans_rx_alloc(trans);
  176. if (err)
  177. return err;
  178. }
  179. spin_lock_irqsave(&rxq->lock, flags);
  180. INIT_LIST_HEAD(&rxq->rx_free);
  181. INIT_LIST_HEAD(&rxq->rx_used);
  182. iwl_trans_rxq_free_rx_bufs(trans);
  183. for (i = 0; i < RX_QUEUE_SIZE; i++)
  184. rxq->queue[i] = NULL;
  185. /* Set us so that we have processed and used all buffers, but have
  186. * not restocked the Rx queue with fresh buffers */
  187. rxq->read = rxq->write = 0;
  188. rxq->write_actual = 0;
  189. rxq->free_count = 0;
  190. spin_unlock_irqrestore(&rxq->lock, flags);
  191. iwlagn_rx_replenish(trans);
  192. iwl_trans_rx_hw_init(trans, rxq);
  193. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  194. rxq->need_update = 1;
  195. iwl_rx_queue_update_write_ptr(trans, rxq);
  196. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  197. return 0;
  198. }
  199. static void iwl_trans_pcie_rx_free(struct iwl_trans *trans)
  200. {
  201. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  202. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  203. unsigned long flags;
  204. /*if rxq->bd is NULL, it means that nothing has been allocated,
  205. * exit now */
  206. if (!rxq->bd) {
  207. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  208. return;
  209. }
  210. spin_lock_irqsave(&rxq->lock, flags);
  211. iwl_trans_rxq_free_rx_bufs(trans);
  212. spin_unlock_irqrestore(&rxq->lock, flags);
  213. dma_free_coherent(trans->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  214. rxq->bd, rxq->bd_dma);
  215. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  216. rxq->bd = NULL;
  217. if (rxq->rb_stts)
  218. dma_free_coherent(trans->dev,
  219. sizeof(struct iwl_rb_status),
  220. rxq->rb_stts, rxq->rb_stts_dma);
  221. else
  222. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  223. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  224. rxq->rb_stts = NULL;
  225. }
  226. static int iwl_trans_rx_stop(struct iwl_trans *trans)
  227. {
  228. /* stop Rx DMA */
  229. iwl_write_direct32(trans, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  230. return iwl_poll_direct_bit(trans, FH_MEM_RSSR_RX_STATUS_REG,
  231. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  232. }
  233. static int iwlagn_alloc_dma_ptr(struct iwl_trans *trans,
  234. struct iwl_dma_ptr *ptr, size_t size)
  235. {
  236. if (WARN_ON(ptr->addr))
  237. return -EINVAL;
  238. ptr->addr = dma_alloc_coherent(trans->dev, size,
  239. &ptr->dma, GFP_KERNEL);
  240. if (!ptr->addr)
  241. return -ENOMEM;
  242. ptr->size = size;
  243. return 0;
  244. }
  245. static void iwlagn_free_dma_ptr(struct iwl_trans *trans,
  246. struct iwl_dma_ptr *ptr)
  247. {
  248. if (unlikely(!ptr->addr))
  249. return;
  250. dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
  251. memset(ptr, 0, sizeof(*ptr));
  252. }
  253. static void iwl_trans_pcie_queue_stuck_timer(unsigned long data)
  254. {
  255. struct iwl_tx_queue *txq = (void *)data;
  256. struct iwl_queue *q = &txq->q;
  257. struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
  258. struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
  259. u32 scd_sram_addr = trans_pcie->scd_base_addr +
  260. SCD_TX_STTS_MEM_LOWER_BOUND + (16 * txq->q.id);
  261. u8 buf[16];
  262. int i;
  263. spin_lock(&txq->lock);
  264. /* check if triggered erroneously */
  265. if (txq->q.read_ptr == txq->q.write_ptr) {
  266. spin_unlock(&txq->lock);
  267. return;
  268. }
  269. spin_unlock(&txq->lock);
  270. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
  271. jiffies_to_msecs(trans_pcie->wd_timeout));
  272. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  273. txq->q.read_ptr, txq->q.write_ptr);
  274. iwl_read_targ_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  275. iwl_print_hex_error(trans, buf, sizeof(buf));
  276. for (i = 0; i < FH_TCSR_CHNL_NUM; i++)
  277. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i,
  278. iwl_read_direct32(trans, FH_TX_TRB_REG(i)));
  279. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  280. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i));
  281. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  282. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  283. u32 tbl_dw =
  284. iwl_read_targ_mem(trans,
  285. trans_pcie->scd_base_addr +
  286. SCD_TRANS_TBL_OFFSET_QUEUE(i));
  287. if (i & 0x1)
  288. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  289. else
  290. tbl_dw = tbl_dw & 0x0000FFFF;
  291. IWL_ERR(trans,
  292. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  293. i, active ? "" : "in", fifo, tbl_dw,
  294. iwl_read_prph(trans,
  295. SCD_QUEUE_RDPTR(i)) & (txq->q.n_bd - 1),
  296. iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
  297. }
  298. for (i = q->read_ptr; i != q->write_ptr;
  299. i = iwl_queue_inc_wrap(i, q->n_bd)) {
  300. struct iwl_tx_cmd *tx_cmd =
  301. (struct iwl_tx_cmd *)txq->entries[i].cmd->payload;
  302. IWL_ERR(trans, "scratch %d = 0x%08x\n", i,
  303. get_unaligned_le32(&tx_cmd->scratch));
  304. }
  305. iwl_op_mode_nic_error(trans->op_mode);
  306. }
  307. static int iwl_trans_txq_alloc(struct iwl_trans *trans,
  308. struct iwl_tx_queue *txq, int slots_num,
  309. u32 txq_id)
  310. {
  311. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  312. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  313. int i;
  314. if (WARN_ON(txq->entries || txq->tfds))
  315. return -EINVAL;
  316. setup_timer(&txq->stuck_timer, iwl_trans_pcie_queue_stuck_timer,
  317. (unsigned long)txq);
  318. txq->trans_pcie = trans_pcie;
  319. txq->q.n_window = slots_num;
  320. txq->entries = kcalloc(slots_num,
  321. sizeof(struct iwl_pcie_tx_queue_entry),
  322. GFP_KERNEL);
  323. if (!txq->entries)
  324. goto error;
  325. if (txq_id == trans_pcie->cmd_queue)
  326. for (i = 0; i < slots_num; i++) {
  327. txq->entries[i].cmd =
  328. kmalloc(sizeof(struct iwl_device_cmd),
  329. GFP_KERNEL);
  330. if (!txq->entries[i].cmd)
  331. goto error;
  332. }
  333. /* Circular buffer of transmit frame descriptors (TFDs),
  334. * shared with device */
  335. txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
  336. &txq->q.dma_addr, GFP_KERNEL);
  337. if (!txq->tfds) {
  338. IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  339. goto error;
  340. }
  341. txq->q.id = txq_id;
  342. return 0;
  343. error:
  344. if (txq->entries && txq_id == trans_pcie->cmd_queue)
  345. for (i = 0; i < slots_num; i++)
  346. kfree(txq->entries[i].cmd);
  347. kfree(txq->entries);
  348. txq->entries = NULL;
  349. return -ENOMEM;
  350. }
  351. static int iwl_trans_txq_init(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  352. int slots_num, u32 txq_id)
  353. {
  354. int ret;
  355. txq->need_update = 0;
  356. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  357. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  358. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  359. /* Initialize queue's high/low-water marks, and head/tail indexes */
  360. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  361. txq_id);
  362. if (ret)
  363. return ret;
  364. spin_lock_init(&txq->lock);
  365. /*
  366. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  367. * given Tx queue, and enable the DMA channel used for that queue.
  368. * Circular buffer (TFD queue in DRAM) physical base address */
  369. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  370. txq->q.dma_addr >> 8);
  371. return 0;
  372. }
  373. /**
  374. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  375. */
  376. static void iwl_tx_queue_unmap(struct iwl_trans *trans, int txq_id)
  377. {
  378. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  379. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  380. struct iwl_queue *q = &txq->q;
  381. enum dma_data_direction dma_dir;
  382. if (!q->n_bd)
  383. return;
  384. /* In the command queue, all the TBs are mapped as BIDI
  385. * so unmap them as such.
  386. */
  387. if (txq_id == trans_pcie->cmd_queue)
  388. dma_dir = DMA_BIDIRECTIONAL;
  389. else
  390. dma_dir = DMA_TO_DEVICE;
  391. spin_lock_bh(&txq->lock);
  392. while (q->write_ptr != q->read_ptr) {
  393. iwl_txq_free_tfd(trans, txq, dma_dir);
  394. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  395. }
  396. spin_unlock_bh(&txq->lock);
  397. }
  398. /**
  399. * iwl_tx_queue_free - Deallocate DMA queue.
  400. * @txq: Transmit queue to deallocate.
  401. *
  402. * Empty queue by removing and destroying all BD's.
  403. * Free all buffers.
  404. * 0-fill, but do not free "txq" descriptor structure.
  405. */
  406. static void iwl_tx_queue_free(struct iwl_trans *trans, int txq_id)
  407. {
  408. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  409. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  410. struct device *dev = trans->dev;
  411. int i;
  412. if (WARN_ON(!txq))
  413. return;
  414. iwl_tx_queue_unmap(trans, txq_id);
  415. /* De-alloc array of command/tx buffers */
  416. if (txq_id == trans_pcie->cmd_queue)
  417. for (i = 0; i < txq->q.n_window; i++) {
  418. kfree(txq->entries[i].cmd);
  419. kfree(txq->entries[i].copy_cmd);
  420. }
  421. /* De-alloc circular buffer of TFDs */
  422. if (txq->q.n_bd) {
  423. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  424. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  425. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  426. }
  427. kfree(txq->entries);
  428. txq->entries = NULL;
  429. del_timer_sync(&txq->stuck_timer);
  430. /* 0-fill queue descriptor structure */
  431. memset(txq, 0, sizeof(*txq));
  432. }
  433. /**
  434. * iwl_trans_tx_free - Free TXQ Context
  435. *
  436. * Destroy all TX DMA queues and structures
  437. */
  438. static void iwl_trans_pcie_tx_free(struct iwl_trans *trans)
  439. {
  440. int txq_id;
  441. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  442. /* Tx queues */
  443. if (trans_pcie->txq) {
  444. for (txq_id = 0;
  445. txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
  446. iwl_tx_queue_free(trans, txq_id);
  447. }
  448. kfree(trans_pcie->txq);
  449. trans_pcie->txq = NULL;
  450. iwlagn_free_dma_ptr(trans, &trans_pcie->kw);
  451. iwlagn_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  452. }
  453. /**
  454. * iwl_trans_tx_alloc - allocate TX context
  455. * Allocate all Tx DMA structures and initialize them
  456. *
  457. * @param priv
  458. * @return error code
  459. */
  460. static int iwl_trans_tx_alloc(struct iwl_trans *trans)
  461. {
  462. int ret;
  463. int txq_id, slots_num;
  464. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  465. u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
  466. sizeof(struct iwlagn_scd_bc_tbl);
  467. /*It is not allowed to alloc twice, so warn when this happens.
  468. * We cannot rely on the previous allocation, so free and fail */
  469. if (WARN_ON(trans_pcie->txq)) {
  470. ret = -EINVAL;
  471. goto error;
  472. }
  473. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  474. scd_bc_tbls_size);
  475. if (ret) {
  476. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  477. goto error;
  478. }
  479. /* Alloc keep-warm buffer */
  480. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  481. if (ret) {
  482. IWL_ERR(trans, "Keep Warm allocation failed\n");
  483. goto error;
  484. }
  485. trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
  486. sizeof(struct iwl_tx_queue), GFP_KERNEL);
  487. if (!trans_pcie->txq) {
  488. IWL_ERR(trans, "Not enough memory for txq\n");
  489. ret = ENOMEM;
  490. goto error;
  491. }
  492. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  493. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  494. txq_id++) {
  495. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  496. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  497. ret = iwl_trans_txq_alloc(trans, &trans_pcie->txq[txq_id],
  498. slots_num, txq_id);
  499. if (ret) {
  500. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  501. goto error;
  502. }
  503. }
  504. return 0;
  505. error:
  506. iwl_trans_pcie_tx_free(trans);
  507. return ret;
  508. }
  509. static int iwl_tx_init(struct iwl_trans *trans)
  510. {
  511. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  512. int ret;
  513. int txq_id, slots_num;
  514. unsigned long flags;
  515. bool alloc = false;
  516. if (!trans_pcie->txq) {
  517. ret = iwl_trans_tx_alloc(trans);
  518. if (ret)
  519. goto error;
  520. alloc = true;
  521. }
  522. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  523. /* Turn off all Tx DMA fifos */
  524. iwl_write_prph(trans, SCD_TXFACT, 0);
  525. /* Tell NIC where to find the "keep warm" buffer */
  526. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  527. trans_pcie->kw.dma >> 4);
  528. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  529. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  530. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  531. txq_id++) {
  532. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  533. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  534. ret = iwl_trans_txq_init(trans, &trans_pcie->txq[txq_id],
  535. slots_num, txq_id);
  536. if (ret) {
  537. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  538. goto error;
  539. }
  540. }
  541. return 0;
  542. error:
  543. /*Upon error, free only if we allocated something */
  544. if (alloc)
  545. iwl_trans_pcie_tx_free(trans);
  546. return ret;
  547. }
  548. static void iwl_set_pwr_vmain(struct iwl_trans *trans)
  549. {
  550. /*
  551. * (for documentation purposes)
  552. * to set power to V_AUX, do:
  553. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  554. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  555. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  556. ~APMG_PS_CTRL_MSK_PWR_SRC);
  557. */
  558. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  559. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  560. ~APMG_PS_CTRL_MSK_PWR_SRC);
  561. }
  562. /* PCI registers */
  563. #define PCI_CFG_RETRY_TIMEOUT 0x041
  564. #define PCI_CFG_LINK_CTRL_VAL_L0S_EN 0x01
  565. #define PCI_CFG_LINK_CTRL_VAL_L1_EN 0x02
  566. static u16 iwl_pciexp_link_ctrl(struct iwl_trans *trans)
  567. {
  568. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  569. int pos;
  570. u16 pci_lnk_ctl;
  571. struct pci_dev *pci_dev = trans_pcie->pci_dev;
  572. pos = pci_pcie_cap(pci_dev);
  573. pci_read_config_word(pci_dev, pos + PCI_EXP_LNKCTL, &pci_lnk_ctl);
  574. return pci_lnk_ctl;
  575. }
  576. static void iwl_apm_config(struct iwl_trans *trans)
  577. {
  578. /*
  579. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  580. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  581. * If so (likely), disable L0S, so device moves directly L0->L1;
  582. * costs negligible amount of power savings.
  583. * If not (unlikely), enable L0S, so there is at least some
  584. * power savings, even without L1.
  585. */
  586. u16 lctl = iwl_pciexp_link_ctrl(trans);
  587. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  588. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  589. /* L1-ASPM enabled; disable(!) L0S */
  590. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  591. dev_printk(KERN_INFO, trans->dev,
  592. "L1 Enabled; Disabling L0S\n");
  593. } else {
  594. /* L1-ASPM disabled; enable(!) L0S */
  595. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  596. dev_printk(KERN_INFO, trans->dev,
  597. "L1 Disabled; Enabling L0S\n");
  598. }
  599. trans->pm_support = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  600. }
  601. /*
  602. * Start up NIC's basic functionality after it has been reset
  603. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  604. * NOTE: This does not load uCode nor start the embedded processor
  605. */
  606. static int iwl_apm_init(struct iwl_trans *trans)
  607. {
  608. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  609. int ret = 0;
  610. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  611. /*
  612. * Use "set_bit" below rather than "write", to preserve any hardware
  613. * bits already set by default after reset.
  614. */
  615. /* Disable L0S exit timer (platform NMI Work/Around) */
  616. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  617. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  618. /*
  619. * Disable L0s without affecting L1;
  620. * don't wait for ICH L0s (ICH bug W/A)
  621. */
  622. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  623. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  624. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  625. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  626. /*
  627. * Enable HAP INTA (interrupt from management bus) to
  628. * wake device's PCI Express link L1a -> L0s
  629. */
  630. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  631. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  632. iwl_apm_config(trans);
  633. /* Configure analog phase-lock-loop before activating to D0A */
  634. if (trans->cfg->base_params->pll_cfg_val)
  635. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  636. trans->cfg->base_params->pll_cfg_val);
  637. /*
  638. * Set "initialization complete" bit to move adapter from
  639. * D0U* --> D0A* (powered-up active) state.
  640. */
  641. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  642. /*
  643. * Wait for clock stabilization; once stabilized, access to
  644. * device-internal resources is supported, e.g. iwl_write_prph()
  645. * and accesses to uCode SRAM.
  646. */
  647. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  648. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  649. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  650. if (ret < 0) {
  651. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  652. goto out;
  653. }
  654. /*
  655. * Enable DMA clock and wait for it to stabilize.
  656. *
  657. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  658. * do not disable clocks. This preserves any hardware bits already
  659. * set by default in "CLK_CTRL_REG" after reset.
  660. */
  661. iwl_write_prph(trans, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  662. udelay(20);
  663. /* Disable L1-Active */
  664. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  665. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  666. set_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status);
  667. out:
  668. return ret;
  669. }
  670. static int iwl_apm_stop_master(struct iwl_trans *trans)
  671. {
  672. int ret = 0;
  673. /* stop device's busmaster DMA activity */
  674. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  675. ret = iwl_poll_bit(trans, CSR_RESET,
  676. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  677. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  678. if (ret)
  679. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  680. IWL_DEBUG_INFO(trans, "stop master\n");
  681. return ret;
  682. }
  683. static void iwl_apm_stop(struct iwl_trans *trans)
  684. {
  685. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  686. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  687. clear_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status);
  688. /* Stop device's DMA activity */
  689. iwl_apm_stop_master(trans);
  690. /* Reset the entire device */
  691. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  692. udelay(10);
  693. /*
  694. * Clear "initialization complete" bit to move adapter from
  695. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  696. */
  697. iwl_clear_bit(trans, CSR_GP_CNTRL,
  698. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  699. }
  700. static int iwl_nic_init(struct iwl_trans *trans)
  701. {
  702. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  703. unsigned long flags;
  704. /* nic_init */
  705. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  706. iwl_apm_init(trans);
  707. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  708. iwl_write8(trans, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  709. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  710. iwl_set_pwr_vmain(trans);
  711. iwl_op_mode_nic_config(trans->op_mode);
  712. /* Allocate the RX queue, or reset if it is already allocated */
  713. iwl_rx_init(trans);
  714. /* Allocate or reset and init all Tx and Command queues */
  715. if (iwl_tx_init(trans))
  716. return -ENOMEM;
  717. if (trans->cfg->base_params->shadow_reg_enable) {
  718. /* enable shadow regs in HW */
  719. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  720. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  721. }
  722. return 0;
  723. }
  724. #define HW_READY_TIMEOUT (50)
  725. /* Note: returns poll_bit return value, which is >= 0 if success */
  726. static int iwl_set_hw_ready(struct iwl_trans *trans)
  727. {
  728. int ret;
  729. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  730. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  731. /* See if we got it */
  732. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  733. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  734. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  735. HW_READY_TIMEOUT);
  736. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  737. return ret;
  738. }
  739. /* Note: returns standard 0/-ERROR code */
  740. static int iwl_prepare_card_hw(struct iwl_trans *trans)
  741. {
  742. int ret;
  743. int t = 0;
  744. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  745. ret = iwl_set_hw_ready(trans);
  746. /* If the card is ready, exit 0 */
  747. if (ret >= 0)
  748. return 0;
  749. /* If HW is not ready, prepare the conditions to check again */
  750. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  751. CSR_HW_IF_CONFIG_REG_PREPARE);
  752. do {
  753. ret = iwl_set_hw_ready(trans);
  754. if (ret >= 0)
  755. return 0;
  756. usleep_range(200, 1000);
  757. t += 200;
  758. } while (t < 150000);
  759. return ret;
  760. }
  761. /*
  762. * ucode
  763. */
  764. static int iwl_load_section(struct iwl_trans *trans, u8 section_num,
  765. const struct fw_desc *section)
  766. {
  767. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  768. dma_addr_t phy_addr = section->p_addr;
  769. u32 byte_cnt = section->len;
  770. u32 dst_addr = section->offset;
  771. int ret;
  772. trans_pcie->ucode_write_complete = false;
  773. iwl_write_direct32(trans,
  774. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  775. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  776. iwl_write_direct32(trans,
  777. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  778. dst_addr);
  779. iwl_write_direct32(trans,
  780. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  781. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  782. iwl_write_direct32(trans,
  783. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  784. (iwl_get_dma_hi_addr(phy_addr)
  785. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  786. iwl_write_direct32(trans,
  787. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  788. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  789. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  790. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  791. iwl_write_direct32(trans,
  792. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  793. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  794. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  795. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  796. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  797. section_num);
  798. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  799. trans_pcie->ucode_write_complete, 5 * HZ);
  800. if (!ret) {
  801. IWL_ERR(trans, "Could not load the [%d] uCode section\n",
  802. section_num);
  803. return -ETIMEDOUT;
  804. }
  805. return 0;
  806. }
  807. static int iwl_load_given_ucode(struct iwl_trans *trans,
  808. const struct fw_img *image)
  809. {
  810. int ret = 0;
  811. int i;
  812. for (i = 0; i < IWL_UCODE_SECTION_MAX; i++) {
  813. if (!image->sec[i].p_addr)
  814. break;
  815. ret = iwl_load_section(trans, i, &image->sec[i]);
  816. if (ret)
  817. return ret;
  818. }
  819. /* Remove all resets to allow NIC to operate */
  820. iwl_write32(trans, CSR_RESET, 0);
  821. return 0;
  822. }
  823. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  824. const struct fw_img *fw)
  825. {
  826. int ret;
  827. bool hw_rfkill;
  828. /* This may fail if AMT took ownership of the device */
  829. if (iwl_prepare_card_hw(trans)) {
  830. IWL_WARN(trans, "Exit HW not ready\n");
  831. return -EIO;
  832. }
  833. iwl_enable_rfkill_int(trans);
  834. /* If platform's RF_KILL switch is NOT set to KILL */
  835. hw_rfkill = iwl_is_rfkill_set(trans);
  836. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  837. if (hw_rfkill)
  838. return -ERFKILL;
  839. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  840. ret = iwl_nic_init(trans);
  841. if (ret) {
  842. IWL_ERR(trans, "Unable to init nic\n");
  843. return ret;
  844. }
  845. /* make sure rfkill handshake bits are cleared */
  846. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  847. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  848. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  849. /* clear (again), then enable host interrupts */
  850. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  851. iwl_enable_interrupts(trans);
  852. /* really make sure rfkill handshake bits are cleared */
  853. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  854. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  855. /* Load the given image to the HW */
  856. return iwl_load_given_ucode(trans, fw);
  857. }
  858. /*
  859. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  860. */
  861. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  862. {
  863. struct iwl_trans_pcie __maybe_unused *trans_pcie =
  864. IWL_TRANS_GET_PCIE_TRANS(trans);
  865. iwl_write_prph(trans, SCD_TXFACT, mask);
  866. }
  867. static void iwl_tx_start(struct iwl_trans *trans)
  868. {
  869. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  870. u32 a;
  871. int chan;
  872. u32 reg_val;
  873. /* make sure all queue are not stopped/used */
  874. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  875. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  876. trans_pcie->scd_base_addr =
  877. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  878. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  879. /* reset conext data memory */
  880. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  881. a += 4)
  882. iwl_write_targ_mem(trans, a, 0);
  883. /* reset tx status memory */
  884. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  885. a += 4)
  886. iwl_write_targ_mem(trans, a, 0);
  887. for (; a < trans_pcie->scd_base_addr +
  888. SCD_TRANS_TBL_OFFSET_QUEUE(
  889. trans->cfg->base_params->num_of_queues);
  890. a += 4)
  891. iwl_write_targ_mem(trans, a, 0);
  892. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  893. trans_pcie->scd_bc_tbls.dma >> 10);
  894. /* The chain extension of the SCD doesn't work well. This feature is
  895. * enabled by default by the HW, so we need to disable it manually.
  896. */
  897. iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
  898. iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
  899. trans_pcie->cmd_fifo);
  900. /* Activate all Tx DMA/FIFO channels */
  901. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  902. /* Enable DMA channel */
  903. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  904. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  905. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  906. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  907. /* Update FH chicken bits */
  908. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  909. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  910. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  911. /* Enable L1-Active */
  912. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  913. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  914. }
  915. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans)
  916. {
  917. iwl_reset_ict(trans);
  918. iwl_tx_start(trans);
  919. }
  920. /**
  921. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  922. */
  923. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  924. {
  925. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  926. int ch, txq_id, ret;
  927. unsigned long flags;
  928. /* Turn off all Tx DMA fifos */
  929. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  930. iwl_trans_txq_set_sched(trans, 0);
  931. /* Stop each Tx DMA channel, and wait for it to be idle */
  932. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  933. iwl_write_direct32(trans,
  934. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  935. ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
  936. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
  937. if (ret < 0)
  938. IWL_ERR(trans,
  939. "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
  940. ch,
  941. iwl_read_direct32(trans,
  942. FH_TSSR_TX_STATUS_REG));
  943. }
  944. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  945. if (!trans_pcie->txq) {
  946. IWL_WARN(trans,
  947. "Stopping tx queues that aren't allocated...\n");
  948. return 0;
  949. }
  950. /* Unmap DMA from host system and free skb's */
  951. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  952. txq_id++)
  953. iwl_tx_queue_unmap(trans, txq_id);
  954. return 0;
  955. }
  956. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  957. {
  958. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  959. unsigned long flags;
  960. /* tell the device to stop sending interrupts */
  961. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  962. iwl_disable_interrupts(trans);
  963. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  964. /* device going down, Stop using ICT table */
  965. iwl_disable_ict(trans);
  966. /*
  967. * If a HW restart happens during firmware loading,
  968. * then the firmware loading might call this function
  969. * and later it might be called again due to the
  970. * restart. So don't process again if the device is
  971. * already dead.
  972. */
  973. if (test_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status)) {
  974. iwl_trans_tx_stop(trans);
  975. iwl_trans_rx_stop(trans);
  976. /* Power-down device's busmaster DMA clocks */
  977. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  978. APMG_CLK_VAL_DMA_CLK_RQT);
  979. udelay(5);
  980. }
  981. /* Make sure (redundant) we've released our request to stay awake */
  982. iwl_clear_bit(trans, CSR_GP_CNTRL,
  983. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  984. /* Stop the device, and put it in low power state */
  985. iwl_apm_stop(trans);
  986. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  987. * Clean again the interrupt here
  988. */
  989. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  990. iwl_disable_interrupts(trans);
  991. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  992. iwl_enable_rfkill_int(trans);
  993. /* wait to make sure we flush pending tasklet*/
  994. synchronize_irq(trans_pcie->irq);
  995. tasklet_kill(&trans_pcie->irq_tasklet);
  996. cancel_work_sync(&trans_pcie->rx_replenish);
  997. /* stop and reset the on-board processor */
  998. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  999. /* clear all status bits */
  1000. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1001. clear_bit(STATUS_INT_ENABLED, &trans_pcie->status);
  1002. clear_bit(STATUS_DEVICE_ENABLED, &trans_pcie->status);
  1003. clear_bit(STATUS_TPOWER_PMI, &trans_pcie->status);
  1004. }
  1005. static void iwl_trans_pcie_wowlan_suspend(struct iwl_trans *trans)
  1006. {
  1007. /* let the ucode operate on its own */
  1008. iwl_write32(trans, CSR_UCODE_DRV_GP1_SET,
  1009. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  1010. iwl_disable_interrupts(trans);
  1011. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1012. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1013. }
  1014. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1015. struct iwl_device_cmd *dev_cmd, int txq_id)
  1016. {
  1017. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1018. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1019. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
  1020. struct iwl_cmd_meta *out_meta;
  1021. struct iwl_tx_queue *txq;
  1022. struct iwl_queue *q;
  1023. dma_addr_t phys_addr = 0;
  1024. dma_addr_t txcmd_phys;
  1025. dma_addr_t scratch_phys;
  1026. u16 len, firstlen, secondlen;
  1027. u8 wait_write_ptr = 0;
  1028. __le16 fc = hdr->frame_control;
  1029. u8 hdr_len = ieee80211_hdrlen(fc);
  1030. u16 __maybe_unused wifi_seq;
  1031. txq = &trans_pcie->txq[txq_id];
  1032. q = &txq->q;
  1033. if (unlikely(!test_bit(txq_id, trans_pcie->queue_used))) {
  1034. WARN_ON_ONCE(1);
  1035. return -EINVAL;
  1036. }
  1037. spin_lock(&txq->lock);
  1038. /* In AGG mode, the index in the ring must correspond to the WiFi
  1039. * sequence number. This is a HW requirements to help the SCD to parse
  1040. * the BA.
  1041. * Check here that the packets are in the right place on the ring.
  1042. */
  1043. #ifdef CONFIG_IWLWIFI_DEBUG
  1044. wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1045. WARN_ONCE((iwl_read_prph(trans, SCD_AGGR_SEL) & BIT(txq_id)) &&
  1046. ((wifi_seq & 0xff) != q->write_ptr),
  1047. "Q: %d WiFi Seq %d tfdNum %d",
  1048. txq_id, wifi_seq, q->write_ptr);
  1049. #endif
  1050. /* Set up driver data for this TFD */
  1051. txq->entries[q->write_ptr].skb = skb;
  1052. txq->entries[q->write_ptr].cmd = dev_cmd;
  1053. dev_cmd->hdr.cmd = REPLY_TX;
  1054. dev_cmd->hdr.sequence =
  1055. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1056. INDEX_TO_SEQ(q->write_ptr)));
  1057. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1058. out_meta = &txq->entries[q->write_ptr].meta;
  1059. /*
  1060. * Use the first empty entry in this queue's command buffer array
  1061. * to contain the Tx command and MAC header concatenated together
  1062. * (payload data will be in another buffer).
  1063. * Size of this varies, due to varying MAC header length.
  1064. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1065. * of the MAC header (device reads on dword boundaries).
  1066. * We'll tell device about this padding later.
  1067. */
  1068. len = sizeof(struct iwl_tx_cmd) +
  1069. sizeof(struct iwl_cmd_header) + hdr_len;
  1070. firstlen = (len + 3) & ~3;
  1071. /* Tell NIC about any 2-byte padding after MAC header */
  1072. if (firstlen != len)
  1073. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1074. /* Physical address of this Tx command's header (not MAC header!),
  1075. * within command buffer array. */
  1076. txcmd_phys = dma_map_single(trans->dev,
  1077. &dev_cmd->hdr, firstlen,
  1078. DMA_BIDIRECTIONAL);
  1079. if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
  1080. goto out_err;
  1081. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  1082. dma_unmap_len_set(out_meta, len, firstlen);
  1083. if (!ieee80211_has_morefrags(fc)) {
  1084. txq->need_update = 1;
  1085. } else {
  1086. wait_write_ptr = 1;
  1087. txq->need_update = 0;
  1088. }
  1089. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1090. * if any (802.11 null frames have no payload). */
  1091. secondlen = skb->len - hdr_len;
  1092. if (secondlen > 0) {
  1093. phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
  1094. secondlen, DMA_TO_DEVICE);
  1095. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  1096. dma_unmap_single(trans->dev,
  1097. dma_unmap_addr(out_meta, mapping),
  1098. dma_unmap_len(out_meta, len),
  1099. DMA_BIDIRECTIONAL);
  1100. goto out_err;
  1101. }
  1102. }
  1103. /* Attach buffers to TFD */
  1104. iwlagn_txq_attach_buf_to_tfd(trans, txq, txcmd_phys, firstlen, 1);
  1105. if (secondlen > 0)
  1106. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  1107. secondlen, 0);
  1108. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  1109. offsetof(struct iwl_tx_cmd, scratch);
  1110. /* take back ownership of DMA buffer to enable update */
  1111. dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
  1112. DMA_BIDIRECTIONAL);
  1113. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1114. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1115. IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
  1116. le16_to_cpu(dev_cmd->hdr.sequence));
  1117. IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  1118. /* Set up entry for this TFD in Tx byte-count array */
  1119. iwl_trans_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1120. dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
  1121. DMA_BIDIRECTIONAL);
  1122. trace_iwlwifi_dev_tx(trans->dev,
  1123. &txq->tfds[txq->q.write_ptr],
  1124. sizeof(struct iwl_tfd),
  1125. &dev_cmd->hdr, firstlen,
  1126. skb->data + hdr_len, secondlen);
  1127. /* start timer if queue currently empty */
  1128. if (txq->need_update && q->read_ptr == q->write_ptr &&
  1129. trans_pcie->wd_timeout)
  1130. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  1131. /* Tell device the write index *just past* this latest filled TFD */
  1132. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1133. iwl_txq_update_write_ptr(trans, txq);
  1134. /*
  1135. * At this point the frame is "transmitted" successfully
  1136. * and we will get a TX status notification eventually,
  1137. * regardless of the value of ret. "ret" only indicates
  1138. * whether or not we should update the write pointer.
  1139. */
  1140. if (iwl_queue_space(q) < q->high_mark) {
  1141. if (wait_write_ptr) {
  1142. txq->need_update = 1;
  1143. iwl_txq_update_write_ptr(trans, txq);
  1144. } else {
  1145. iwl_stop_queue(trans, txq);
  1146. }
  1147. }
  1148. spin_unlock(&txq->lock);
  1149. return 0;
  1150. out_err:
  1151. spin_unlock(&txq->lock);
  1152. return -1;
  1153. }
  1154. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  1155. {
  1156. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1157. int err;
  1158. bool hw_rfkill;
  1159. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1160. if (!trans_pcie->irq_requested) {
  1161. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1162. iwl_irq_tasklet, (unsigned long)trans);
  1163. iwl_alloc_isr_ict(trans);
  1164. err = request_irq(trans_pcie->irq, iwl_isr_ict, IRQF_SHARED,
  1165. DRV_NAME, trans);
  1166. if (err) {
  1167. IWL_ERR(trans, "Error allocating IRQ %d\n",
  1168. trans_pcie->irq);
  1169. goto error;
  1170. }
  1171. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  1172. trans_pcie->irq_requested = true;
  1173. }
  1174. err = iwl_prepare_card_hw(trans);
  1175. if (err) {
  1176. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  1177. goto err_free_irq;
  1178. }
  1179. iwl_apm_init(trans);
  1180. /* From now on, the op_mode will be kept updated about RF kill state */
  1181. iwl_enable_rfkill_int(trans);
  1182. hw_rfkill = iwl_is_rfkill_set(trans);
  1183. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  1184. return err;
  1185. err_free_irq:
  1186. free_irq(trans_pcie->irq, trans);
  1187. error:
  1188. iwl_free_isr_ict(trans);
  1189. tasklet_kill(&trans_pcie->irq_tasklet);
  1190. return err;
  1191. }
  1192. static void iwl_trans_pcie_stop_hw(struct iwl_trans *trans,
  1193. bool op_mode_leaving)
  1194. {
  1195. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1196. bool hw_rfkill;
  1197. unsigned long flags;
  1198. iwl_apm_stop(trans);
  1199. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  1200. iwl_disable_interrupts(trans);
  1201. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  1202. if (!op_mode_leaving) {
  1203. /*
  1204. * Even if we stop the HW, we still want the RF kill
  1205. * interrupt
  1206. */
  1207. iwl_enable_rfkill_int(trans);
  1208. /*
  1209. * Check again since the RF kill state may have changed while
  1210. * all the interrupts were disabled, in this case we couldn't
  1211. * receive the RF kill interrupt and update the state in the
  1212. * op_mode.
  1213. */
  1214. hw_rfkill = iwl_is_rfkill_set(trans);
  1215. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  1216. }
  1217. }
  1218. static void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
  1219. struct sk_buff_head *skbs)
  1220. {
  1221. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1222. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  1223. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  1224. int tfd_num = ssn & (txq->q.n_bd - 1);
  1225. int freed = 0;
  1226. spin_lock(&txq->lock);
  1227. if (txq->q.read_ptr != tfd_num) {
  1228. IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
  1229. txq_id, txq->q.read_ptr, tfd_num, ssn);
  1230. freed = iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1231. if (iwl_queue_space(&txq->q) > txq->q.low_mark)
  1232. iwl_wake_queue(trans, txq);
  1233. }
  1234. spin_unlock(&txq->lock);
  1235. }
  1236. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  1237. {
  1238. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1239. }
  1240. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  1241. {
  1242. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1243. }
  1244. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  1245. {
  1246. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1247. }
  1248. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  1249. const struct iwl_trans_config *trans_cfg)
  1250. {
  1251. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1252. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  1253. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  1254. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  1255. trans_pcie->n_no_reclaim_cmds = 0;
  1256. else
  1257. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  1258. if (trans_pcie->n_no_reclaim_cmds)
  1259. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  1260. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  1261. trans_pcie->rx_buf_size_8k = trans_cfg->rx_buf_size_8k;
  1262. if (trans_pcie->rx_buf_size_8k)
  1263. trans_pcie->rx_page_order = get_order(8 * 1024);
  1264. else
  1265. trans_pcie->rx_page_order = get_order(4 * 1024);
  1266. trans_pcie->wd_timeout =
  1267. msecs_to_jiffies(trans_cfg->queue_watchdog_timeout);
  1268. trans_pcie->command_names = trans_cfg->command_names;
  1269. }
  1270. void iwl_trans_pcie_free(struct iwl_trans *trans)
  1271. {
  1272. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1273. iwl_trans_pcie_tx_free(trans);
  1274. iwl_trans_pcie_rx_free(trans);
  1275. if (trans_pcie->irq_requested == true) {
  1276. free_irq(trans_pcie->irq, trans);
  1277. iwl_free_isr_ict(trans);
  1278. }
  1279. pci_disable_msi(trans_pcie->pci_dev);
  1280. iounmap(trans_pcie->hw_base);
  1281. pci_release_regions(trans_pcie->pci_dev);
  1282. pci_disable_device(trans_pcie->pci_dev);
  1283. kmem_cache_destroy(trans->dev_cmd_pool);
  1284. kfree(trans);
  1285. }
  1286. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  1287. {
  1288. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1289. if (state)
  1290. set_bit(STATUS_TPOWER_PMI, &trans_pcie->status);
  1291. else
  1292. clear_bit(STATUS_TPOWER_PMI, &trans_pcie->status);
  1293. }
  1294. #ifdef CONFIG_PM_SLEEP
  1295. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1296. {
  1297. return 0;
  1298. }
  1299. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1300. {
  1301. bool hw_rfkill;
  1302. iwl_enable_rfkill_int(trans);
  1303. hw_rfkill = iwl_is_rfkill_set(trans);
  1304. iwl_op_mode_hw_rf_kill(trans->op_mode, hw_rfkill);
  1305. if (!hw_rfkill)
  1306. iwl_enable_interrupts(trans);
  1307. return 0;
  1308. }
  1309. #endif /* CONFIG_PM_SLEEP */
  1310. #define IWL_FLUSH_WAIT_MS 2000
  1311. static int iwl_trans_pcie_wait_tx_queue_empty(struct iwl_trans *trans)
  1312. {
  1313. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1314. struct iwl_tx_queue *txq;
  1315. struct iwl_queue *q;
  1316. int cnt;
  1317. unsigned long now = jiffies;
  1318. int ret = 0;
  1319. /* waiting for all the tx frames complete might take a while */
  1320. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1321. if (cnt == trans_pcie->cmd_queue)
  1322. continue;
  1323. txq = &trans_pcie->txq[cnt];
  1324. q = &txq->q;
  1325. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1326. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1327. msleep(1);
  1328. if (q->read_ptr != q->write_ptr) {
  1329. IWL_ERR(trans, "fail to flush all tx fifo queues\n");
  1330. ret = -ETIMEDOUT;
  1331. break;
  1332. }
  1333. }
  1334. return ret;
  1335. }
  1336. static const char *get_fh_string(int cmd)
  1337. {
  1338. #define IWL_CMD(x) case x: return #x
  1339. switch (cmd) {
  1340. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1341. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1342. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1343. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1344. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1345. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1346. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1347. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1348. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1349. default:
  1350. return "UNKNOWN";
  1351. }
  1352. #undef IWL_CMD
  1353. }
  1354. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1355. {
  1356. int i;
  1357. #ifdef CONFIG_IWLWIFI_DEBUG
  1358. int pos = 0;
  1359. size_t bufsz = 0;
  1360. #endif
  1361. static const u32 fh_tbl[] = {
  1362. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1363. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1364. FH_RSCSR_CHNL0_WPTR,
  1365. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1366. FH_MEM_RSSR_SHARED_CTRL_REG,
  1367. FH_MEM_RSSR_RX_STATUS_REG,
  1368. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1369. FH_TSSR_TX_STATUS_REG,
  1370. FH_TSSR_TX_ERROR_REG
  1371. };
  1372. #ifdef CONFIG_IWLWIFI_DEBUG
  1373. if (display) {
  1374. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1375. *buf = kmalloc(bufsz, GFP_KERNEL);
  1376. if (!*buf)
  1377. return -ENOMEM;
  1378. pos += scnprintf(*buf + pos, bufsz - pos,
  1379. "FH register values:\n");
  1380. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1381. pos += scnprintf(*buf + pos, bufsz - pos,
  1382. " %34s: 0X%08x\n",
  1383. get_fh_string(fh_tbl[i]),
  1384. iwl_read_direct32(trans, fh_tbl[i]));
  1385. }
  1386. return pos;
  1387. }
  1388. #endif
  1389. IWL_ERR(trans, "FH register values:\n");
  1390. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1391. IWL_ERR(trans, " %34s: 0X%08x\n",
  1392. get_fh_string(fh_tbl[i]),
  1393. iwl_read_direct32(trans, fh_tbl[i]));
  1394. }
  1395. return 0;
  1396. }
  1397. static const char *get_csr_string(int cmd)
  1398. {
  1399. #define IWL_CMD(x) case x: return #x
  1400. switch (cmd) {
  1401. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1402. IWL_CMD(CSR_INT_COALESCING);
  1403. IWL_CMD(CSR_INT);
  1404. IWL_CMD(CSR_INT_MASK);
  1405. IWL_CMD(CSR_FH_INT_STATUS);
  1406. IWL_CMD(CSR_GPIO_IN);
  1407. IWL_CMD(CSR_RESET);
  1408. IWL_CMD(CSR_GP_CNTRL);
  1409. IWL_CMD(CSR_HW_REV);
  1410. IWL_CMD(CSR_EEPROM_REG);
  1411. IWL_CMD(CSR_EEPROM_GP);
  1412. IWL_CMD(CSR_OTP_GP_REG);
  1413. IWL_CMD(CSR_GIO_REG);
  1414. IWL_CMD(CSR_GP_UCODE_REG);
  1415. IWL_CMD(CSR_GP_DRIVER_REG);
  1416. IWL_CMD(CSR_UCODE_DRV_GP1);
  1417. IWL_CMD(CSR_UCODE_DRV_GP2);
  1418. IWL_CMD(CSR_LED_REG);
  1419. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1420. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1421. IWL_CMD(CSR_ANA_PLL_CFG);
  1422. IWL_CMD(CSR_HW_REV_WA_REG);
  1423. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1424. default:
  1425. return "UNKNOWN";
  1426. }
  1427. #undef IWL_CMD
  1428. }
  1429. void iwl_dump_csr(struct iwl_trans *trans)
  1430. {
  1431. int i;
  1432. static const u32 csr_tbl[] = {
  1433. CSR_HW_IF_CONFIG_REG,
  1434. CSR_INT_COALESCING,
  1435. CSR_INT,
  1436. CSR_INT_MASK,
  1437. CSR_FH_INT_STATUS,
  1438. CSR_GPIO_IN,
  1439. CSR_RESET,
  1440. CSR_GP_CNTRL,
  1441. CSR_HW_REV,
  1442. CSR_EEPROM_REG,
  1443. CSR_EEPROM_GP,
  1444. CSR_OTP_GP_REG,
  1445. CSR_GIO_REG,
  1446. CSR_GP_UCODE_REG,
  1447. CSR_GP_DRIVER_REG,
  1448. CSR_UCODE_DRV_GP1,
  1449. CSR_UCODE_DRV_GP2,
  1450. CSR_LED_REG,
  1451. CSR_DRAM_INT_TBL_REG,
  1452. CSR_GIO_CHICKEN_BITS,
  1453. CSR_ANA_PLL_CFG,
  1454. CSR_HW_REV_WA_REG,
  1455. CSR_DBG_HPET_MEM_REG
  1456. };
  1457. IWL_ERR(trans, "CSR values:\n");
  1458. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1459. "CSR_INT_PERIODIC_REG)\n");
  1460. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1461. IWL_ERR(trans, " %25s: 0X%08x\n",
  1462. get_csr_string(csr_tbl[i]),
  1463. iwl_read32(trans, csr_tbl[i]));
  1464. }
  1465. }
  1466. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1467. /* create and remove of files */
  1468. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1469. if (!debugfs_create_file(#name, mode, parent, trans, \
  1470. &iwl_dbgfs_##name##_ops)) \
  1471. goto err; \
  1472. } while (0)
  1473. /* file operation */
  1474. #define DEBUGFS_READ_FUNC(name) \
  1475. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1476. char __user *user_buf, \
  1477. size_t count, loff_t *ppos);
  1478. #define DEBUGFS_WRITE_FUNC(name) \
  1479. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1480. const char __user *user_buf, \
  1481. size_t count, loff_t *ppos);
  1482. #define DEBUGFS_READ_FILE_OPS(name) \
  1483. DEBUGFS_READ_FUNC(name); \
  1484. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1485. .read = iwl_dbgfs_##name##_read, \
  1486. .open = simple_open, \
  1487. .llseek = generic_file_llseek, \
  1488. };
  1489. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1490. DEBUGFS_WRITE_FUNC(name); \
  1491. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1492. .write = iwl_dbgfs_##name##_write, \
  1493. .open = simple_open, \
  1494. .llseek = generic_file_llseek, \
  1495. };
  1496. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1497. DEBUGFS_READ_FUNC(name); \
  1498. DEBUGFS_WRITE_FUNC(name); \
  1499. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1500. .write = iwl_dbgfs_##name##_write, \
  1501. .read = iwl_dbgfs_##name##_read, \
  1502. .open = simple_open, \
  1503. .llseek = generic_file_llseek, \
  1504. };
  1505. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1506. char __user *user_buf,
  1507. size_t count, loff_t *ppos)
  1508. {
  1509. struct iwl_trans *trans = file->private_data;
  1510. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1511. struct iwl_tx_queue *txq;
  1512. struct iwl_queue *q;
  1513. char *buf;
  1514. int pos = 0;
  1515. int cnt;
  1516. int ret;
  1517. size_t bufsz;
  1518. bufsz = sizeof(char) * 64 * trans->cfg->base_params->num_of_queues;
  1519. if (!trans_pcie->txq)
  1520. return -EAGAIN;
  1521. buf = kzalloc(bufsz, GFP_KERNEL);
  1522. if (!buf)
  1523. return -ENOMEM;
  1524. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1525. txq = &trans_pcie->txq[cnt];
  1526. q = &txq->q;
  1527. pos += scnprintf(buf + pos, bufsz - pos,
  1528. "hwq %.2d: read=%u write=%u use=%d stop=%d\n",
  1529. cnt, q->read_ptr, q->write_ptr,
  1530. !!test_bit(cnt, trans_pcie->queue_used),
  1531. !!test_bit(cnt, trans_pcie->queue_stopped));
  1532. }
  1533. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1534. kfree(buf);
  1535. return ret;
  1536. }
  1537. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1538. char __user *user_buf,
  1539. size_t count, loff_t *ppos)
  1540. {
  1541. struct iwl_trans *trans = file->private_data;
  1542. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1543. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1544. char buf[256];
  1545. int pos = 0;
  1546. const size_t bufsz = sizeof(buf);
  1547. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1548. rxq->read);
  1549. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1550. rxq->write);
  1551. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1552. rxq->free_count);
  1553. if (rxq->rb_stts) {
  1554. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1555. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1556. } else {
  1557. pos += scnprintf(buf + pos, bufsz - pos,
  1558. "closed_rb_num: Not Allocated\n");
  1559. }
  1560. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1561. }
  1562. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1563. char __user *user_buf,
  1564. size_t count, loff_t *ppos)
  1565. {
  1566. struct iwl_trans *trans = file->private_data;
  1567. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1568. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1569. int pos = 0;
  1570. char *buf;
  1571. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1572. ssize_t ret;
  1573. buf = kzalloc(bufsz, GFP_KERNEL);
  1574. if (!buf)
  1575. return -ENOMEM;
  1576. pos += scnprintf(buf + pos, bufsz - pos,
  1577. "Interrupt Statistics Report:\n");
  1578. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1579. isr_stats->hw);
  1580. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1581. isr_stats->sw);
  1582. if (isr_stats->sw || isr_stats->hw) {
  1583. pos += scnprintf(buf + pos, bufsz - pos,
  1584. "\tLast Restarting Code: 0x%X\n",
  1585. isr_stats->err_code);
  1586. }
  1587. #ifdef CONFIG_IWLWIFI_DEBUG
  1588. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1589. isr_stats->sch);
  1590. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1591. isr_stats->alive);
  1592. #endif
  1593. pos += scnprintf(buf + pos, bufsz - pos,
  1594. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1595. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1596. isr_stats->ctkill);
  1597. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1598. isr_stats->wakeup);
  1599. pos += scnprintf(buf + pos, bufsz - pos,
  1600. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1601. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1602. isr_stats->tx);
  1603. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1604. isr_stats->unhandled);
  1605. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1606. kfree(buf);
  1607. return ret;
  1608. }
  1609. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1610. const char __user *user_buf,
  1611. size_t count, loff_t *ppos)
  1612. {
  1613. struct iwl_trans *trans = file->private_data;
  1614. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1615. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1616. char buf[8];
  1617. int buf_size;
  1618. u32 reset_flag;
  1619. memset(buf, 0, sizeof(buf));
  1620. buf_size = min(count, sizeof(buf) - 1);
  1621. if (copy_from_user(buf, user_buf, buf_size))
  1622. return -EFAULT;
  1623. if (sscanf(buf, "%x", &reset_flag) != 1)
  1624. return -EFAULT;
  1625. if (reset_flag == 0)
  1626. memset(isr_stats, 0, sizeof(*isr_stats));
  1627. return count;
  1628. }
  1629. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1630. const char __user *user_buf,
  1631. size_t count, loff_t *ppos)
  1632. {
  1633. struct iwl_trans *trans = file->private_data;
  1634. char buf[8];
  1635. int buf_size;
  1636. int csr;
  1637. memset(buf, 0, sizeof(buf));
  1638. buf_size = min(count, sizeof(buf) - 1);
  1639. if (copy_from_user(buf, user_buf, buf_size))
  1640. return -EFAULT;
  1641. if (sscanf(buf, "%d", &csr) != 1)
  1642. return -EFAULT;
  1643. iwl_dump_csr(trans);
  1644. return count;
  1645. }
  1646. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1647. char __user *user_buf,
  1648. size_t count, loff_t *ppos)
  1649. {
  1650. struct iwl_trans *trans = file->private_data;
  1651. char *buf;
  1652. int pos = 0;
  1653. ssize_t ret = -EFAULT;
  1654. ret = pos = iwl_dump_fh(trans, &buf, true);
  1655. if (buf) {
  1656. ret = simple_read_from_buffer(user_buf,
  1657. count, ppos, buf, pos);
  1658. kfree(buf);
  1659. }
  1660. return ret;
  1661. }
  1662. static ssize_t iwl_dbgfs_fw_restart_write(struct file *file,
  1663. const char __user *user_buf,
  1664. size_t count, loff_t *ppos)
  1665. {
  1666. struct iwl_trans *trans = file->private_data;
  1667. if (!trans->op_mode)
  1668. return -EAGAIN;
  1669. local_bh_disable();
  1670. iwl_op_mode_nic_error(trans->op_mode);
  1671. local_bh_enable();
  1672. return count;
  1673. }
  1674. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1675. DEBUGFS_READ_FILE_OPS(fh_reg);
  1676. DEBUGFS_READ_FILE_OPS(rx_queue);
  1677. DEBUGFS_READ_FILE_OPS(tx_queue);
  1678. DEBUGFS_WRITE_FILE_OPS(csr);
  1679. DEBUGFS_WRITE_FILE_OPS(fw_restart);
  1680. /*
  1681. * Create the debugfs files and directories
  1682. *
  1683. */
  1684. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1685. struct dentry *dir)
  1686. {
  1687. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1688. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1689. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1690. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1691. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1692. DEBUGFS_ADD_FILE(fw_restart, dir, S_IWUSR);
  1693. return 0;
  1694. err:
  1695. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  1696. return -ENOMEM;
  1697. }
  1698. #else
  1699. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1700. struct dentry *dir)
  1701. {
  1702. return 0;
  1703. }
  1704. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1705. static const struct iwl_trans_ops trans_ops_pcie = {
  1706. .start_hw = iwl_trans_pcie_start_hw,
  1707. .stop_hw = iwl_trans_pcie_stop_hw,
  1708. .fw_alive = iwl_trans_pcie_fw_alive,
  1709. .start_fw = iwl_trans_pcie_start_fw,
  1710. .stop_device = iwl_trans_pcie_stop_device,
  1711. .wowlan_suspend = iwl_trans_pcie_wowlan_suspend,
  1712. .send_cmd = iwl_trans_pcie_send_cmd,
  1713. .tx = iwl_trans_pcie_tx,
  1714. .reclaim = iwl_trans_pcie_reclaim,
  1715. .txq_disable = iwl_trans_pcie_txq_disable,
  1716. .txq_enable = iwl_trans_pcie_txq_enable,
  1717. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1718. .wait_tx_queue_empty = iwl_trans_pcie_wait_tx_queue_empty,
  1719. #ifdef CONFIG_PM_SLEEP
  1720. .suspend = iwl_trans_pcie_suspend,
  1721. .resume = iwl_trans_pcie_resume,
  1722. #endif
  1723. .write8 = iwl_trans_pcie_write8,
  1724. .write32 = iwl_trans_pcie_write32,
  1725. .read32 = iwl_trans_pcie_read32,
  1726. .configure = iwl_trans_pcie_configure,
  1727. .set_pmi = iwl_trans_pcie_set_pmi,
  1728. };
  1729. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  1730. const struct pci_device_id *ent,
  1731. const struct iwl_cfg *cfg)
  1732. {
  1733. struct iwl_trans_pcie *trans_pcie;
  1734. struct iwl_trans *trans;
  1735. u16 pci_cmd;
  1736. int err;
  1737. trans = kzalloc(sizeof(struct iwl_trans) +
  1738. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1739. if (WARN_ON(!trans))
  1740. return NULL;
  1741. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1742. trans->ops = &trans_ops_pcie;
  1743. trans->cfg = cfg;
  1744. trans_pcie->trans = trans;
  1745. spin_lock_init(&trans_pcie->irq_lock);
  1746. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  1747. /* W/A - seems to solve weird behavior. We need to remove this if we
  1748. * don't want to stay in L1 all the time. This wastes a lot of power */
  1749. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  1750. PCIE_LINK_STATE_CLKPM);
  1751. if (pci_enable_device(pdev)) {
  1752. err = -ENODEV;
  1753. goto out_no_pci;
  1754. }
  1755. pci_set_master(pdev);
  1756. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1757. if (!err)
  1758. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1759. if (err) {
  1760. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1761. if (!err)
  1762. err = pci_set_consistent_dma_mask(pdev,
  1763. DMA_BIT_MASK(32));
  1764. /* both attempts failed: */
  1765. if (err) {
  1766. dev_printk(KERN_ERR, &pdev->dev,
  1767. "No suitable DMA available.\n");
  1768. goto out_pci_disable_device;
  1769. }
  1770. }
  1771. err = pci_request_regions(pdev, DRV_NAME);
  1772. if (err) {
  1773. dev_printk(KERN_ERR, &pdev->dev,
  1774. "pci_request_regions failed\n");
  1775. goto out_pci_disable_device;
  1776. }
  1777. trans_pcie->hw_base = pci_ioremap_bar(pdev, 0);
  1778. if (!trans_pcie->hw_base) {
  1779. dev_printk(KERN_ERR, &pdev->dev, "pci_ioremap_bar failed\n");
  1780. err = -ENODEV;
  1781. goto out_pci_release_regions;
  1782. }
  1783. dev_printk(KERN_INFO, &pdev->dev,
  1784. "pci_resource_len = 0x%08llx\n",
  1785. (unsigned long long) pci_resource_len(pdev, 0));
  1786. dev_printk(KERN_INFO, &pdev->dev,
  1787. "pci_resource_base = %p\n", trans_pcie->hw_base);
  1788. dev_printk(KERN_INFO, &pdev->dev,
  1789. "HW Revision ID = 0x%X\n", pdev->revision);
  1790. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1791. * PCI Tx retries from interfering with C3 CPU state */
  1792. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1793. err = pci_enable_msi(pdev);
  1794. if (err)
  1795. dev_printk(KERN_ERR, &pdev->dev,
  1796. "pci_enable_msi failed(0X%x)\n", err);
  1797. trans->dev = &pdev->dev;
  1798. trans_pcie->irq = pdev->irq;
  1799. trans_pcie->pci_dev = pdev;
  1800. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  1801. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  1802. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  1803. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  1804. /* TODO: Move this away, not needed if not MSI */
  1805. /* enable rfkill interrupt: hw bug w/a */
  1806. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1807. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1808. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1809. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1810. }
  1811. /* Initialize the wait queue for commands */
  1812. init_waitqueue_head(&trans->wait_command_queue);
  1813. spin_lock_init(&trans->reg_lock);
  1814. snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
  1815. "iwl_cmd_pool:%s", dev_name(trans->dev));
  1816. trans->dev_cmd_headroom = 0;
  1817. trans->dev_cmd_pool =
  1818. kmem_cache_create(trans->dev_cmd_pool_name,
  1819. sizeof(struct iwl_device_cmd)
  1820. + trans->dev_cmd_headroom,
  1821. sizeof(void *),
  1822. SLAB_HWCACHE_ALIGN,
  1823. NULL);
  1824. if (!trans->dev_cmd_pool)
  1825. goto out_pci_disable_msi;
  1826. return trans;
  1827. out_pci_disable_msi:
  1828. pci_disable_msi(pdev);
  1829. out_pci_release_regions:
  1830. pci_release_regions(pdev);
  1831. out_pci_disable_device:
  1832. pci_disable_device(pdev);
  1833. out_no_pci:
  1834. kfree(trans);
  1835. return NULL;
  1836. }