iwl-trans-pcie.c 60 KB

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