iwl-trans-pcie.c 65 KB

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