iwl-trans-pcie.c 65 KB

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