iwl-trans-pcie.c 64 KB

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