iwl-trans-pcie.c 65 KB

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