iwl-trans-pcie.c 60 KB

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