iwl-trans-pcie.c 63 KB

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