trans.c 61 KB

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