iwl-trans.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  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 - 2011 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 - 2011 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/interrupt.h>
  64. #include <linux/debugfs.h>
  65. #include <linux/bitops.h>
  66. #include <linux/gfp.h>
  67. #include "iwl-trans.h"
  68. #include "iwl-trans-int-pcie.h"
  69. #include "iwl-csr.h"
  70. #include "iwl-prph.h"
  71. #include "iwl-shared.h"
  72. #include "iwl-eeprom.h"
  73. #include "iwl-agn-hw.h"
  74. static int iwl_trans_rx_alloc(struct iwl_trans *trans)
  75. {
  76. struct iwl_trans_pcie *trans_pcie =
  77. IWL_TRANS_GET_PCIE_TRANS(trans);
  78. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  79. struct device *dev = bus(trans)->dev;
  80. memset(&trans_pcie->rxq, 0, sizeof(trans_pcie->rxq));
  81. spin_lock_init(&rxq->lock);
  82. INIT_LIST_HEAD(&rxq->rx_free);
  83. INIT_LIST_HEAD(&rxq->rx_used);
  84. if (WARN_ON(rxq->bd || rxq->rb_stts))
  85. return -EINVAL;
  86. /* Allocate the circular buffer of Read Buffer Descriptors (RBDs) */
  87. rxq->bd = dma_alloc_coherent(dev, sizeof(__le32) * RX_QUEUE_SIZE,
  88. &rxq->bd_dma, GFP_KERNEL);
  89. if (!rxq->bd)
  90. goto err_bd;
  91. memset(rxq->bd, 0, sizeof(__le32) * RX_QUEUE_SIZE);
  92. /*Allocate the driver's pointer to receive buffer status */
  93. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(*rxq->rb_stts),
  94. &rxq->rb_stts_dma, GFP_KERNEL);
  95. if (!rxq->rb_stts)
  96. goto err_rb_stts;
  97. memset(rxq->rb_stts, 0, sizeof(*rxq->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(bus(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 = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  134. rb_timeout = RX_RB_TIMEOUT;
  135. if (iwlagn_mod_params.amsdu_size_8K)
  136. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  137. else
  138. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  139. /* Stop Rx DMA */
  140. iwl_write_direct32(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  141. /* Reset driver's Rx queue write index */
  142. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  143. /* Tell device where to find RBD circular buffer in DRAM */
  144. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  145. (u32)(rxq->bd_dma >> 8));
  146. /* Tell device where in DRAM to update its Rx status */
  147. iwl_write_direct32(bus(trans), FH_RSCSR_CHNL0_STTS_WPTR_REG,
  148. rxq->rb_stts_dma >> 4);
  149. /* Enable Rx DMA
  150. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  151. * the credit mechanism in 5000 HW RX FIFO
  152. * Direct rx interrupts to hosts
  153. * Rx buffer size 4 or 8k
  154. * RB timeout 0x10
  155. * 256 RBDs
  156. */
  157. iwl_write_direct32(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG,
  158. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  159. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  160. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  161. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  162. rb_size|
  163. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  164. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  165. /* Set interrupt coalescing timer to default (2048 usecs) */
  166. iwl_write8(bus(trans), CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  167. }
  168. static int iwl_rx_init(struct iwl_trans *trans)
  169. {
  170. struct iwl_trans_pcie *trans_pcie =
  171. IWL_TRANS_GET_PCIE_TRANS(trans);
  172. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  173. int i, err;
  174. unsigned long flags;
  175. if (!rxq->bd) {
  176. err = iwl_trans_rx_alloc(trans);
  177. if (err)
  178. return err;
  179. }
  180. spin_lock_irqsave(&rxq->lock, flags);
  181. INIT_LIST_HEAD(&rxq->rx_free);
  182. INIT_LIST_HEAD(&rxq->rx_used);
  183. iwl_trans_rxq_free_rx_bufs(trans);
  184. for (i = 0; i < RX_QUEUE_SIZE; i++)
  185. rxq->queue[i] = NULL;
  186. /* Set us so that we have processed and used all buffers, but have
  187. * not restocked the Rx queue with fresh buffers */
  188. rxq->read = rxq->write = 0;
  189. rxq->write_actual = 0;
  190. rxq->free_count = 0;
  191. spin_unlock_irqrestore(&rxq->lock, flags);
  192. iwlagn_rx_replenish(trans);
  193. iwl_trans_rx_hw_init(trans, rxq);
  194. spin_lock_irqsave(&trans->shrd->lock, flags);
  195. rxq->need_update = 1;
  196. iwl_rx_queue_update_write_ptr(trans, rxq);
  197. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  198. return 0;
  199. }
  200. static void iwl_trans_pcie_rx_free(struct iwl_trans *trans)
  201. {
  202. struct iwl_trans_pcie *trans_pcie =
  203. IWL_TRANS_GET_PCIE_TRANS(trans);
  204. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  205. unsigned long flags;
  206. /*if rxq->bd is NULL, it means that nothing has been allocated,
  207. * exit now */
  208. if (!rxq->bd) {
  209. IWL_DEBUG_INFO(trans, "Free NULL rx context\n");
  210. return;
  211. }
  212. spin_lock_irqsave(&rxq->lock, flags);
  213. iwl_trans_rxq_free_rx_bufs(trans);
  214. spin_unlock_irqrestore(&rxq->lock, flags);
  215. dma_free_coherent(bus(trans)->dev, sizeof(__le32) * RX_QUEUE_SIZE,
  216. rxq->bd, rxq->bd_dma);
  217. memset(&rxq->bd_dma, 0, sizeof(rxq->bd_dma));
  218. rxq->bd = NULL;
  219. if (rxq->rb_stts)
  220. dma_free_coherent(bus(trans)->dev,
  221. sizeof(struct iwl_rb_status),
  222. rxq->rb_stts, rxq->rb_stts_dma);
  223. else
  224. IWL_DEBUG_INFO(trans, "Free rxq->rb_stts which is NULL\n");
  225. memset(&rxq->rb_stts_dma, 0, sizeof(rxq->rb_stts_dma));
  226. rxq->rb_stts = NULL;
  227. }
  228. static int iwl_trans_rx_stop(struct iwl_trans *trans)
  229. {
  230. /* stop Rx DMA */
  231. iwl_write_direct32(bus(trans), FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  232. return iwl_poll_direct_bit(bus(trans), FH_MEM_RSSR_RX_STATUS_REG,
  233. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  234. }
  235. static inline int iwlagn_alloc_dma_ptr(struct iwl_trans *trans,
  236. struct iwl_dma_ptr *ptr, size_t size)
  237. {
  238. if (WARN_ON(ptr->addr))
  239. return -EINVAL;
  240. ptr->addr = dma_alloc_coherent(bus(trans)->dev, size,
  241. &ptr->dma, GFP_KERNEL);
  242. if (!ptr->addr)
  243. return -ENOMEM;
  244. ptr->size = size;
  245. return 0;
  246. }
  247. static inline void iwlagn_free_dma_ptr(struct iwl_trans *trans,
  248. struct iwl_dma_ptr *ptr)
  249. {
  250. if (unlikely(!ptr->addr))
  251. return;
  252. dma_free_coherent(bus(trans)->dev, ptr->size, ptr->addr, ptr->dma);
  253. memset(ptr, 0, sizeof(*ptr));
  254. }
  255. static int iwl_trans_txq_alloc(struct iwl_trans *trans,
  256. struct iwl_tx_queue *txq, int slots_num,
  257. u32 txq_id)
  258. {
  259. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  260. int i;
  261. if (WARN_ON(txq->meta || txq->cmd || txq->skbs || txq->tfds))
  262. return -EINVAL;
  263. txq->q.n_window = slots_num;
  264. txq->meta = kzalloc(sizeof(txq->meta[0]) * slots_num,
  265. GFP_KERNEL);
  266. txq->cmd = kzalloc(sizeof(txq->cmd[0]) * slots_num,
  267. GFP_KERNEL);
  268. if (!txq->meta || !txq->cmd)
  269. goto error;
  270. if (txq_id == trans->shrd->cmd_queue)
  271. for (i = 0; i < slots_num; i++) {
  272. txq->cmd[i] = kmalloc(sizeof(struct iwl_device_cmd),
  273. GFP_KERNEL);
  274. if (!txq->cmd[i])
  275. goto error;
  276. }
  277. /* Alloc driver data array and TFD circular buffer */
  278. /* Driver private data, only for Tx (not command) queues,
  279. * not shared with device. */
  280. if (txq_id != trans->shrd->cmd_queue) {
  281. txq->skbs = kzalloc(sizeof(txq->skbs[0]) *
  282. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  283. if (!txq->skbs) {
  284. IWL_ERR(trans, "kmalloc for auxiliary BD "
  285. "structures failed\n");
  286. goto error;
  287. }
  288. } else {
  289. txq->skbs = NULL;
  290. }
  291. /* Circular buffer of transmit frame descriptors (TFDs),
  292. * shared with device */
  293. txq->tfds = dma_alloc_coherent(bus(trans)->dev, tfd_sz,
  294. &txq->q.dma_addr, GFP_KERNEL);
  295. if (!txq->tfds) {
  296. IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  297. goto error;
  298. }
  299. txq->q.id = txq_id;
  300. return 0;
  301. error:
  302. kfree(txq->skbs);
  303. txq->skbs = NULL;
  304. /* since txq->cmd has been zeroed,
  305. * all non allocated cmd[i] will be NULL */
  306. if (txq->cmd && txq_id == trans->shrd->cmd_queue)
  307. for (i = 0; i < slots_num; i++)
  308. kfree(txq->cmd[i]);
  309. kfree(txq->meta);
  310. kfree(txq->cmd);
  311. txq->meta = NULL;
  312. txq->cmd = NULL;
  313. return -ENOMEM;
  314. }
  315. static int iwl_trans_txq_init(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  316. int slots_num, u32 txq_id)
  317. {
  318. int ret;
  319. txq->need_update = 0;
  320. memset(txq->meta, 0, sizeof(txq->meta[0]) * slots_num);
  321. /*
  322. * For the default queues 0-3, set up the swq_id
  323. * already -- all others need to get one later
  324. * (if they need one at all).
  325. */
  326. if (txq_id < 4)
  327. iwl_set_swq_id(txq, txq_id, txq_id);
  328. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  329. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  330. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  331. /* Initialize queue's high/low-water marks, and head/tail indexes */
  332. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  333. txq_id);
  334. if (ret)
  335. return ret;
  336. /*
  337. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  338. * given Tx queue, and enable the DMA channel used for that queue.
  339. * Circular buffer (TFD queue in DRAM) physical base address */
  340. iwl_write_direct32(bus(trans), FH_MEM_CBBC_QUEUE(txq_id),
  341. txq->q.dma_addr >> 8);
  342. return 0;
  343. }
  344. /**
  345. * iwl_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  346. */
  347. static void iwl_tx_queue_unmap(struct iwl_trans *trans, int txq_id)
  348. {
  349. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  350. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  351. struct iwl_queue *q = &txq->q;
  352. if (!q->n_bd)
  353. return;
  354. while (q->write_ptr != q->read_ptr) {
  355. /* The read_ptr needs to bound by q->n_window */
  356. iwlagn_txq_free_tfd(trans, txq, get_cmd_index(q, q->read_ptr));
  357. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  358. }
  359. }
  360. /**
  361. * iwl_tx_queue_free - Deallocate DMA queue.
  362. * @txq: Transmit queue to deallocate.
  363. *
  364. * Empty queue by removing and destroying all BD's.
  365. * Free all buffers.
  366. * 0-fill, but do not free "txq" descriptor structure.
  367. */
  368. static void iwl_tx_queue_free(struct iwl_trans *trans, int txq_id)
  369. {
  370. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  371. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  372. struct device *dev = bus(trans)->dev;
  373. int i;
  374. if (WARN_ON(!txq))
  375. return;
  376. iwl_tx_queue_unmap(trans, txq_id);
  377. /* De-alloc array of command/tx buffers */
  378. if (txq_id == trans->shrd->cmd_queue)
  379. for (i = 0; i < txq->q.n_window; i++)
  380. kfree(txq->cmd[i]);
  381. /* De-alloc circular buffer of TFDs */
  382. if (txq->q.n_bd) {
  383. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  384. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  385. memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr));
  386. }
  387. /* De-alloc array of per-TFD driver data */
  388. kfree(txq->skbs);
  389. txq->skbs = NULL;
  390. /* deallocate arrays */
  391. kfree(txq->cmd);
  392. kfree(txq->meta);
  393. txq->cmd = NULL;
  394. txq->meta = NULL;
  395. /* 0-fill queue descriptor structure */
  396. memset(txq, 0, sizeof(*txq));
  397. }
  398. /**
  399. * iwl_trans_tx_free - Free TXQ Context
  400. *
  401. * Destroy all TX DMA queues and structures
  402. */
  403. static void iwl_trans_pcie_tx_free(struct iwl_trans *trans)
  404. {
  405. int txq_id;
  406. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  407. /* Tx queues */
  408. if (trans_pcie->txq) {
  409. for (txq_id = 0;
  410. txq_id < hw_params(trans).max_txq_num; txq_id++)
  411. iwl_tx_queue_free(trans, txq_id);
  412. }
  413. kfree(trans_pcie->txq);
  414. trans_pcie->txq = NULL;
  415. iwlagn_free_dma_ptr(trans, &trans_pcie->kw);
  416. iwlagn_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  417. }
  418. /**
  419. * iwl_trans_tx_alloc - allocate TX context
  420. * Allocate all Tx DMA structures and initialize them
  421. *
  422. * @param priv
  423. * @return error code
  424. */
  425. static int iwl_trans_tx_alloc(struct iwl_trans *trans)
  426. {
  427. int ret;
  428. int txq_id, slots_num;
  429. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  430. u16 scd_bc_tbls_size = hw_params(trans).max_txq_num *
  431. sizeof(struct iwlagn_scd_bc_tbl);
  432. /*It is not allowed to alloc twice, so warn when this happens.
  433. * We cannot rely on the previous allocation, so free and fail */
  434. if (WARN_ON(trans_pcie->txq)) {
  435. ret = -EINVAL;
  436. goto error;
  437. }
  438. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  439. scd_bc_tbls_size);
  440. if (ret) {
  441. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  442. goto error;
  443. }
  444. /* Alloc keep-warm buffer */
  445. ret = iwlagn_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  446. if (ret) {
  447. IWL_ERR(trans, "Keep Warm allocation failed\n");
  448. goto error;
  449. }
  450. trans_pcie->txq = kzalloc(sizeof(struct iwl_tx_queue) *
  451. hw_params(trans).max_txq_num, GFP_KERNEL);
  452. if (!trans_pcie->txq) {
  453. IWL_ERR(trans, "Not enough memory for txq\n");
  454. ret = ENOMEM;
  455. goto error;
  456. }
  457. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  458. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  459. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  460. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  461. ret = iwl_trans_txq_alloc(trans, &trans_pcie->txq[txq_id],
  462. slots_num, txq_id);
  463. if (ret) {
  464. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  465. goto error;
  466. }
  467. }
  468. return 0;
  469. error:
  470. iwl_trans_pcie_tx_free(trans);
  471. return ret;
  472. }
  473. static int iwl_tx_init(struct iwl_trans *trans)
  474. {
  475. int ret;
  476. int txq_id, slots_num;
  477. unsigned long flags;
  478. bool alloc = false;
  479. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  480. if (!trans_pcie->txq) {
  481. ret = iwl_trans_tx_alloc(trans);
  482. if (ret)
  483. goto error;
  484. alloc = true;
  485. }
  486. spin_lock_irqsave(&trans->shrd->lock, flags);
  487. /* Turn off all Tx DMA fifos */
  488. iwl_write_prph(bus(trans), SCD_TXFACT, 0);
  489. /* Tell NIC where to find the "keep warm" buffer */
  490. iwl_write_direct32(bus(trans), FH_KW_MEM_ADDR_REG,
  491. trans_pcie->kw.dma >> 4);
  492. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  493. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  494. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++) {
  495. slots_num = (txq_id == trans->shrd->cmd_queue) ?
  496. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  497. ret = iwl_trans_txq_init(trans, &trans_pcie->txq[txq_id],
  498. slots_num, txq_id);
  499. if (ret) {
  500. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  501. goto error;
  502. }
  503. }
  504. return 0;
  505. error:
  506. /*Upon error, free only if we allocated something */
  507. if (alloc)
  508. iwl_trans_pcie_tx_free(trans);
  509. return ret;
  510. }
  511. static void iwl_set_pwr_vmain(struct iwl_trans *trans)
  512. {
  513. /*
  514. * (for documentation purposes)
  515. * to set power to V_AUX, do:
  516. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  517. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  518. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  519. ~APMG_PS_CTRL_MSK_PWR_SRC);
  520. */
  521. iwl_set_bits_mask_prph(bus(trans), APMG_PS_CTRL_REG,
  522. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  523. ~APMG_PS_CTRL_MSK_PWR_SRC);
  524. }
  525. static int iwl_nic_init(struct iwl_trans *trans)
  526. {
  527. unsigned long flags;
  528. /* nic_init */
  529. spin_lock_irqsave(&trans->shrd->lock, flags);
  530. iwl_apm_init(priv(trans));
  531. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  532. iwl_write8(bus(trans), CSR_INT_COALESCING,
  533. IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  534. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  535. iwl_set_pwr_vmain(trans);
  536. iwl_nic_config(priv(trans));
  537. /* Allocate the RX queue, or reset if it is already allocated */
  538. iwl_rx_init(trans);
  539. /* Allocate or reset and init all Tx and Command queues */
  540. if (iwl_tx_init(trans))
  541. return -ENOMEM;
  542. if (hw_params(trans).shadow_reg_enable) {
  543. /* enable shadow regs in HW */
  544. iwl_set_bit(bus(trans), CSR_MAC_SHADOW_REG_CTRL,
  545. 0x800FFFFF);
  546. }
  547. set_bit(STATUS_INIT, &trans->shrd->status);
  548. return 0;
  549. }
  550. #define HW_READY_TIMEOUT (50)
  551. /* Note: returns poll_bit return value, which is >= 0 if success */
  552. static int iwl_set_hw_ready(struct iwl_trans *trans)
  553. {
  554. int ret;
  555. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  556. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  557. /* See if we got it */
  558. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  559. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  560. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  561. HW_READY_TIMEOUT);
  562. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  563. return ret;
  564. }
  565. /* Note: returns standard 0/-ERROR code */
  566. static int iwl_trans_pcie_prepare_card_hw(struct iwl_trans *trans)
  567. {
  568. int ret;
  569. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  570. ret = iwl_set_hw_ready(trans);
  571. if (ret >= 0)
  572. return 0;
  573. /* If HW is not ready, prepare the conditions to check again */
  574. iwl_set_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  575. CSR_HW_IF_CONFIG_REG_PREPARE);
  576. ret = iwl_poll_bit(bus(trans), CSR_HW_IF_CONFIG_REG,
  577. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  578. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  579. if (ret < 0)
  580. return ret;
  581. /* HW should be ready by now, check again. */
  582. ret = iwl_set_hw_ready(trans);
  583. if (ret >= 0)
  584. return 0;
  585. return ret;
  586. }
  587. #define IWL_AC_UNSET -1
  588. struct queue_to_fifo_ac {
  589. s8 fifo, ac;
  590. };
  591. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  592. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  593. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  594. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  595. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  596. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  597. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  598. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  599. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  600. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  601. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  602. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  603. };
  604. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  605. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  606. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  607. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  608. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  609. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  610. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  611. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  612. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  613. { IWL_TX_FIFO_BE_IPAN, 2, },
  614. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  615. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  616. };
  617. static const u8 iwlagn_bss_ac_to_fifo[] = {
  618. IWL_TX_FIFO_VO,
  619. IWL_TX_FIFO_VI,
  620. IWL_TX_FIFO_BE,
  621. IWL_TX_FIFO_BK,
  622. };
  623. static const u8 iwlagn_bss_ac_to_queue[] = {
  624. 0, 1, 2, 3,
  625. };
  626. static const u8 iwlagn_pan_ac_to_fifo[] = {
  627. IWL_TX_FIFO_VO_IPAN,
  628. IWL_TX_FIFO_VI_IPAN,
  629. IWL_TX_FIFO_BE_IPAN,
  630. IWL_TX_FIFO_BK_IPAN,
  631. };
  632. static const u8 iwlagn_pan_ac_to_queue[] = {
  633. 7, 6, 5, 4,
  634. };
  635. static int iwl_trans_pcie_start_device(struct iwl_trans *trans)
  636. {
  637. int ret;
  638. struct iwl_trans_pcie *trans_pcie =
  639. IWL_TRANS_GET_PCIE_TRANS(trans);
  640. trans->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  641. trans_pcie->ac_to_queue[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_queue;
  642. trans_pcie->ac_to_queue[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_queue;
  643. trans_pcie->ac_to_fifo[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_fifo;
  644. trans_pcie->ac_to_fifo[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_fifo;
  645. trans_pcie->mcast_queue[IWL_RXON_CTX_BSS] = 0;
  646. trans_pcie->mcast_queue[IWL_RXON_CTX_PAN] = IWL_IPAN_MCAST_QUEUE;
  647. if ((hw_params(trans).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  648. iwl_trans_pcie_prepare_card_hw(trans)) {
  649. IWL_WARN(trans, "Exit HW not ready\n");
  650. return -EIO;
  651. }
  652. /* If platform's RF_KILL switch is NOT set to KILL */
  653. if (iwl_read32(bus(trans), CSR_GP_CNTRL) &
  654. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  655. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  656. else
  657. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  658. if (iwl_is_rfkill(trans->shrd)) {
  659. iwl_set_hw_rfkill_state(priv(trans), true);
  660. iwl_enable_interrupts(trans);
  661. return -ERFKILL;
  662. }
  663. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  664. ret = iwl_nic_init(trans);
  665. if (ret) {
  666. IWL_ERR(trans, "Unable to init nic\n");
  667. return ret;
  668. }
  669. /* make sure rfkill handshake bits are cleared */
  670. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  671. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR,
  672. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  673. /* clear (again), then enable host interrupts */
  674. iwl_write32(bus(trans), CSR_INT, 0xFFFFFFFF);
  675. iwl_enable_interrupts(trans);
  676. /* really make sure rfkill handshake bits are cleared */
  677. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  678. iwl_write32(bus(trans), CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  679. return 0;
  680. }
  681. /*
  682. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  683. * must be called under priv->shrd->lock and mac access
  684. */
  685. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  686. {
  687. iwl_write_prph(bus(trans), SCD_TXFACT, mask);
  688. }
  689. static void iwl_trans_pcie_tx_start(struct iwl_trans *trans)
  690. {
  691. const struct queue_to_fifo_ac *queue_to_fifo;
  692. struct iwl_trans_pcie *trans_pcie =
  693. IWL_TRANS_GET_PCIE_TRANS(trans);
  694. u32 a;
  695. unsigned long flags;
  696. int i, chan;
  697. u32 reg_val;
  698. spin_lock_irqsave(&trans->shrd->lock, flags);
  699. trans_pcie->scd_base_addr =
  700. iwl_read_prph(bus(trans), SCD_SRAM_BASE_ADDR);
  701. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  702. /* reset conext data memory */
  703. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  704. a += 4)
  705. iwl_write_targ_mem(bus(trans), a, 0);
  706. /* reset tx status memory */
  707. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  708. a += 4)
  709. iwl_write_targ_mem(bus(trans), a, 0);
  710. for (; a < trans_pcie->scd_base_addr +
  711. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(trans).max_txq_num);
  712. a += 4)
  713. iwl_write_targ_mem(bus(trans), a, 0);
  714. iwl_write_prph(bus(trans), SCD_DRAM_BASE_ADDR,
  715. trans_pcie->scd_bc_tbls.dma >> 10);
  716. /* Enable DMA channel */
  717. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  718. iwl_write_direct32(bus(trans), FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  719. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  720. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  721. /* Update FH chicken bits */
  722. reg_val = iwl_read_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG);
  723. iwl_write_direct32(bus(trans), FH_TX_CHICKEN_BITS_REG,
  724. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  725. iwl_write_prph(bus(trans), SCD_QUEUECHAIN_SEL,
  726. SCD_QUEUECHAIN_SEL_ALL(trans));
  727. iwl_write_prph(bus(trans), SCD_AGGR_SEL, 0);
  728. /* initiate the queues */
  729. for (i = 0; i < hw_params(trans).max_txq_num; i++) {
  730. iwl_write_prph(bus(trans), SCD_QUEUE_RDPTR(i), 0);
  731. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR, 0 | (i << 8));
  732. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  733. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  734. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  735. SCD_CONTEXT_QUEUE_OFFSET(i) +
  736. sizeof(u32),
  737. ((SCD_WIN_SIZE <<
  738. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  739. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  740. ((SCD_FRAME_LIMIT <<
  741. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  742. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  743. }
  744. iwl_write_prph(bus(trans), SCD_INTERRUPT_MASK,
  745. IWL_MASK(0, hw_params(trans).max_txq_num));
  746. /* Activate all Tx DMA/FIFO channels */
  747. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  748. /* map queues to FIFOs */
  749. if (trans->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  750. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  751. else
  752. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  753. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  754. /* make sure all queue are not stopped */
  755. memset(&trans_pcie->queue_stopped[0], 0,
  756. sizeof(trans_pcie->queue_stopped));
  757. for (i = 0; i < 4; i++)
  758. atomic_set(&trans_pcie->queue_stop_count[i], 0);
  759. /* reset to 0 to enable all the queue first */
  760. trans_pcie->txq_ctx_active_msk = 0;
  761. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  762. IWLAGN_FIRST_AMPDU_QUEUE);
  763. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  764. IWLAGN_FIRST_AMPDU_QUEUE);
  765. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  766. int fifo = queue_to_fifo[i].fifo;
  767. int ac = queue_to_fifo[i].ac;
  768. iwl_txq_ctx_activate(trans_pcie, i);
  769. if (fifo == IWL_TX_FIFO_UNUSED)
  770. continue;
  771. if (ac != IWL_AC_UNSET)
  772. iwl_set_swq_id(&trans_pcie->txq[i], ac, i);
  773. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[i],
  774. fifo, 0);
  775. }
  776. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  777. /* Enable L1-Active */
  778. iwl_clear_bits_prph(bus(trans), APMG_PCIDEV_STT_REG,
  779. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  780. }
  781. /**
  782. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  783. */
  784. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  785. {
  786. int ch, txq_id;
  787. unsigned long flags;
  788. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  789. /* Turn off all Tx DMA fifos */
  790. spin_lock_irqsave(&trans->shrd->lock, flags);
  791. iwl_trans_txq_set_sched(trans, 0);
  792. /* Stop each Tx DMA channel, and wait for it to be idle */
  793. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  794. iwl_write_direct32(bus(trans),
  795. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  796. if (iwl_poll_direct_bit(bus(trans), FH_TSSR_TX_STATUS_REG,
  797. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  798. 1000))
  799. IWL_ERR(trans, "Failing on timeout while stopping"
  800. " DMA channel %d [0x%08x]", ch,
  801. iwl_read_direct32(bus(trans),
  802. FH_TSSR_TX_STATUS_REG));
  803. }
  804. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  805. if (!trans_pcie->txq) {
  806. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  807. return 0;
  808. }
  809. /* Unmap DMA from host system and free skb's */
  810. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  811. iwl_tx_queue_unmap(trans, txq_id);
  812. return 0;
  813. }
  814. static void iwl_trans_pcie_disable_sync_irq(struct iwl_trans *trans)
  815. {
  816. unsigned long flags;
  817. struct iwl_trans_pcie *trans_pcie =
  818. IWL_TRANS_GET_PCIE_TRANS(trans);
  819. spin_lock_irqsave(&trans->shrd->lock, flags);
  820. iwl_disable_interrupts(trans);
  821. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  822. /* wait to make sure we flush pending tasklet*/
  823. synchronize_irq(bus(trans)->irq);
  824. tasklet_kill(&trans_pcie->irq_tasklet);
  825. }
  826. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  827. {
  828. /* stop and reset the on-board processor */
  829. iwl_write32(bus(trans), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  830. /* tell the device to stop sending interrupts */
  831. iwl_trans_pcie_disable_sync_irq(trans);
  832. /* device going down, Stop using ICT table */
  833. iwl_disable_ict(trans);
  834. /*
  835. * If a HW restart happens during firmware loading,
  836. * then the firmware loading might call this function
  837. * and later it might be called again due to the
  838. * restart. So don't process again if the device is
  839. * already dead.
  840. */
  841. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  842. iwl_trans_tx_stop(trans);
  843. iwl_trans_rx_stop(trans);
  844. /* Power-down device's busmaster DMA clocks */
  845. iwl_write_prph(bus(trans), APMG_CLK_DIS_REG,
  846. APMG_CLK_VAL_DMA_CLK_RQT);
  847. udelay(5);
  848. }
  849. /* Make sure (redundant) we've released our request to stay awake */
  850. iwl_clear_bit(bus(trans), CSR_GP_CNTRL,
  851. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  852. /* Stop the device, and put it in low power state */
  853. iwl_apm_stop(priv(trans));
  854. }
  855. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  856. struct iwl_device_cmd *dev_cmd, u8 ctx, u8 sta_id)
  857. {
  858. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  859. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  860. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  861. struct iwl_tx_cmd *tx_cmd = &dev_cmd->cmd.tx;
  862. struct iwl_cmd_meta *out_meta;
  863. struct iwl_tx_queue *txq;
  864. struct iwl_queue *q;
  865. dma_addr_t phys_addr = 0;
  866. dma_addr_t txcmd_phys;
  867. dma_addr_t scratch_phys;
  868. u16 len, firstlen, secondlen;
  869. u16 seq_number = 0;
  870. u8 wait_write_ptr = 0;
  871. u8 txq_id;
  872. u8 tid = 0;
  873. bool is_agg = false;
  874. __le16 fc = hdr->frame_control;
  875. u8 hdr_len = ieee80211_hdrlen(fc);
  876. /*
  877. * Send this frame after DTIM -- there's a special queue
  878. * reserved for this for contexts that support AP mode.
  879. */
  880. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  881. txq_id = trans_pcie->mcast_queue[ctx];
  882. /*
  883. * The microcode will clear the more data
  884. * bit in the last frame it transmits.
  885. */
  886. hdr->frame_control |=
  887. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  888. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  889. txq_id = IWL_AUX_QUEUE;
  890. else
  891. txq_id =
  892. trans_pcie->ac_to_queue[ctx][skb_get_queue_mapping(skb)];
  893. if (ieee80211_is_data_qos(fc)) {
  894. u8 *qc = NULL;
  895. struct iwl_tid_data *tid_data;
  896. qc = ieee80211_get_qos_ctl(hdr);
  897. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  898. tid_data = &trans->shrd->tid_data[sta_id][tid];
  899. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  900. return -1;
  901. seq_number = tid_data->seq_number;
  902. seq_number &= IEEE80211_SCTL_SEQ;
  903. hdr->seq_ctrl = hdr->seq_ctrl &
  904. cpu_to_le16(IEEE80211_SCTL_FRAG);
  905. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  906. seq_number += 0x10;
  907. /* aggregation is on for this <sta,tid> */
  908. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  909. tid_data->agg.state == IWL_AGG_ON) {
  910. txq_id = tid_data->agg.txq_id;
  911. is_agg = true;
  912. }
  913. }
  914. txq = &trans_pcie->txq[txq_id];
  915. q = &txq->q;
  916. /* Set up driver data for this TFD */
  917. txq->skbs[q->write_ptr] = skb;
  918. txq->cmd[q->write_ptr] = dev_cmd;
  919. dev_cmd->hdr.cmd = REPLY_TX;
  920. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  921. INDEX_TO_SEQ(q->write_ptr)));
  922. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  923. out_meta = &txq->meta[q->write_ptr];
  924. /*
  925. * Use the first empty entry in this queue's command buffer array
  926. * to contain the Tx command and MAC header concatenated together
  927. * (payload data will be in another buffer).
  928. * Size of this varies, due to varying MAC header length.
  929. * If end is not dword aligned, we'll have 2 extra bytes at the end
  930. * of the MAC header (device reads on dword boundaries).
  931. * We'll tell device about this padding later.
  932. */
  933. len = sizeof(struct iwl_tx_cmd) +
  934. sizeof(struct iwl_cmd_header) + hdr_len;
  935. firstlen = (len + 3) & ~3;
  936. /* Tell NIC about any 2-byte padding after MAC header */
  937. if (firstlen != len)
  938. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  939. /* Physical address of this Tx command's header (not MAC header!),
  940. * within command buffer array. */
  941. txcmd_phys = dma_map_single(bus(trans)->dev,
  942. &dev_cmd->hdr, firstlen,
  943. DMA_BIDIRECTIONAL);
  944. if (unlikely(dma_mapping_error(bus(trans)->dev, txcmd_phys)))
  945. return -1;
  946. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  947. dma_unmap_len_set(out_meta, len, firstlen);
  948. if (!ieee80211_has_morefrags(fc)) {
  949. txq->need_update = 1;
  950. } else {
  951. wait_write_ptr = 1;
  952. txq->need_update = 0;
  953. }
  954. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  955. * if any (802.11 null frames have no payload). */
  956. secondlen = skb->len - hdr_len;
  957. if (secondlen > 0) {
  958. phys_addr = dma_map_single(bus(trans)->dev, skb->data + hdr_len,
  959. secondlen, DMA_TO_DEVICE);
  960. if (unlikely(dma_mapping_error(bus(trans)->dev, phys_addr))) {
  961. dma_unmap_single(bus(trans)->dev,
  962. dma_unmap_addr(out_meta, mapping),
  963. dma_unmap_len(out_meta, len),
  964. DMA_BIDIRECTIONAL);
  965. return -1;
  966. }
  967. }
  968. /* Attach buffers to TFD */
  969. iwlagn_txq_attach_buf_to_tfd(trans, txq, txcmd_phys, firstlen, 1);
  970. if (secondlen > 0)
  971. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  972. secondlen, 0);
  973. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  974. offsetof(struct iwl_tx_cmd, scratch);
  975. /* take back ownership of DMA buffer to enable update */
  976. dma_sync_single_for_cpu(bus(trans)->dev, txcmd_phys, firstlen,
  977. DMA_BIDIRECTIONAL);
  978. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  979. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  980. IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
  981. le16_to_cpu(dev_cmd->hdr.sequence));
  982. IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  983. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  984. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  985. /* Set up entry for this TFD in Tx byte-count array */
  986. if (is_agg)
  987. iwl_trans_txq_update_byte_cnt_tbl(trans, txq,
  988. le16_to_cpu(tx_cmd->len));
  989. dma_sync_single_for_device(bus(trans)->dev, txcmd_phys, firstlen,
  990. DMA_BIDIRECTIONAL);
  991. trace_iwlwifi_dev_tx(priv(trans),
  992. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  993. sizeof(struct iwl_tfd),
  994. &dev_cmd->hdr, firstlen,
  995. skb->data + hdr_len, secondlen);
  996. /* Tell device the write index *just past* this latest filled TFD */
  997. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  998. iwl_txq_update_write_ptr(trans, txq);
  999. if (ieee80211_is_data_qos(fc)) {
  1000. trans->shrd->tid_data[sta_id][tid].tfds_in_queue++;
  1001. if (!ieee80211_has_morefrags(fc))
  1002. trans->shrd->tid_data[sta_id][tid].seq_number =
  1003. seq_number;
  1004. }
  1005. /*
  1006. * At this point the frame is "transmitted" successfully
  1007. * and we will get a TX status notification eventually,
  1008. * regardless of the value of ret. "ret" only indicates
  1009. * whether or not we should update the write pointer.
  1010. */
  1011. if (iwl_queue_space(q) < q->high_mark) {
  1012. if (wait_write_ptr) {
  1013. txq->need_update = 1;
  1014. iwl_txq_update_write_ptr(trans, txq);
  1015. } else {
  1016. iwl_stop_queue(trans, txq);
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. static void iwl_trans_pcie_kick_nic(struct iwl_trans *trans)
  1022. {
  1023. /* Remove all resets to allow NIC to operate */
  1024. iwl_write32(bus(trans), CSR_RESET, 0);
  1025. }
  1026. static int iwl_trans_pcie_request_irq(struct iwl_trans *trans)
  1027. {
  1028. struct iwl_trans_pcie *trans_pcie =
  1029. IWL_TRANS_GET_PCIE_TRANS(trans);
  1030. int err;
  1031. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1032. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1033. iwl_irq_tasklet, (unsigned long)trans);
  1034. iwl_alloc_isr_ict(trans);
  1035. err = request_irq(bus(trans)->irq, iwl_isr_ict, IRQF_SHARED,
  1036. DRV_NAME, trans);
  1037. if (err) {
  1038. IWL_ERR(trans, "Error allocating IRQ %d\n", bus(trans)->irq);
  1039. iwl_free_isr_ict(trans);
  1040. return err;
  1041. }
  1042. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  1043. return 0;
  1044. }
  1045. static int iwlagn_txq_check_empty(struct iwl_trans *trans,
  1046. int sta_id, u8 tid, int txq_id)
  1047. {
  1048. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1049. struct iwl_queue *q = &trans_pcie->txq[txq_id].q;
  1050. struct iwl_tid_data *tid_data = &trans->shrd->tid_data[sta_id][tid];
  1051. lockdep_assert_held(&trans->shrd->sta_lock);
  1052. switch (trans->shrd->tid_data[sta_id][tid].agg.state) {
  1053. case IWL_EMPTYING_HW_QUEUE_DELBA:
  1054. /* We are reclaiming the last packet of the */
  1055. /* aggregated HW queue */
  1056. if ((txq_id == tid_data->agg.txq_id) &&
  1057. (q->read_ptr == q->write_ptr)) {
  1058. IWL_DEBUG_HT(trans,
  1059. "HW queue empty: continue DELBA flow\n");
  1060. iwl_trans_pcie_txq_agg_disable(trans, txq_id);
  1061. tid_data->agg.state = IWL_AGG_OFF;
  1062. iwl_stop_tx_ba_trans_ready(priv(trans),
  1063. NUM_IWL_RXON_CTX,
  1064. sta_id, tid);
  1065. iwl_wake_queue(trans, &trans_pcie->txq[txq_id]);
  1066. }
  1067. break;
  1068. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  1069. /* We are reclaiming the last packet of the queue */
  1070. if (tid_data->tfds_in_queue == 0) {
  1071. IWL_DEBUG_HT(trans,
  1072. "HW queue empty: continue ADDBA flow\n");
  1073. tid_data->agg.state = IWL_AGG_ON;
  1074. iwl_start_tx_ba_trans_ready(priv(trans),
  1075. NUM_IWL_RXON_CTX,
  1076. sta_id, tid);
  1077. }
  1078. break;
  1079. }
  1080. return 0;
  1081. }
  1082. static void iwl_free_tfds_in_queue(struct iwl_trans *trans,
  1083. int sta_id, int tid, int freed)
  1084. {
  1085. lockdep_assert_held(&trans->shrd->sta_lock);
  1086. if (trans->shrd->tid_data[sta_id][tid].tfds_in_queue >= freed)
  1087. trans->shrd->tid_data[sta_id][tid].tfds_in_queue -= freed;
  1088. else {
  1089. IWL_DEBUG_TX(trans, "free more than tfds_in_queue (%u:%d)\n",
  1090. trans->shrd->tid_data[sta_id][tid].tfds_in_queue,
  1091. freed);
  1092. trans->shrd->tid_data[sta_id][tid].tfds_in_queue = 0;
  1093. }
  1094. }
  1095. static void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int sta_id, int tid,
  1096. int txq_id, int ssn, u32 status,
  1097. struct sk_buff_head *skbs)
  1098. {
  1099. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1100. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  1101. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  1102. int tfd_num = ssn & (txq->q.n_bd - 1);
  1103. int freed = 0;
  1104. u8 agg_state;
  1105. bool cond;
  1106. txq->time_stamp = jiffies;
  1107. if (txq->sched_retry) {
  1108. agg_state =
  1109. trans->shrd->tid_data[txq->sta_id][txq->tid].agg.state;
  1110. cond = (agg_state != IWL_EMPTYING_HW_QUEUE_DELBA);
  1111. } else {
  1112. cond = (status != TX_STATUS_FAIL_PASSIVE_NO_RX);
  1113. }
  1114. if (txq->q.read_ptr != tfd_num) {
  1115. IWL_DEBUG_TX_REPLY(trans, "Retry scheduler reclaim "
  1116. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  1117. ssn , tfd_num, txq_id, txq->swq_id);
  1118. freed = iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1119. if (iwl_queue_space(&txq->q) > txq->q.low_mark && cond)
  1120. iwl_wake_queue(trans, txq);
  1121. }
  1122. iwl_free_tfds_in_queue(trans, sta_id, tid, freed);
  1123. iwlagn_txq_check_empty(trans, sta_id, tid, txq_id);
  1124. }
  1125. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1126. {
  1127. iwl_trans_pcie_tx_free(trans);
  1128. iwl_trans_pcie_rx_free(trans);
  1129. free_irq(bus(trans)->irq, trans);
  1130. iwl_free_isr_ict(trans);
  1131. trans->shrd->trans = NULL;
  1132. kfree(trans);
  1133. }
  1134. #ifdef CONFIG_PM
  1135. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1136. {
  1137. /*
  1138. * This function is called when system goes into suspend state
  1139. * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
  1140. * first but since iwl_mac_stop() has no knowledge of who the caller is,
  1141. * it will not call apm_ops.stop() to stop the DMA operation.
  1142. * Calling apm_ops.stop here to make sure we stop the DMA.
  1143. *
  1144. * But of course ... if we have configured WoWLAN then we did other
  1145. * things already :-)
  1146. */
  1147. if (!trans->shrd->wowlan)
  1148. iwl_apm_stop(priv(trans));
  1149. return 0;
  1150. }
  1151. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1152. {
  1153. bool hw_rfkill = false;
  1154. iwl_enable_interrupts(trans);
  1155. if (!(iwl_read32(bus(trans), CSR_GP_CNTRL) &
  1156. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1157. hw_rfkill = true;
  1158. if (hw_rfkill)
  1159. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1160. else
  1161. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1162. iwl_set_hw_rfkill_state(priv(trans), hw_rfkill);
  1163. return 0;
  1164. }
  1165. #else /* CONFIG_PM */
  1166. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1167. { return 0; }
  1168. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1169. { return 0; }
  1170. #endif /* CONFIG_PM */
  1171. static void iwl_trans_pcie_wake_any_queue(struct iwl_trans *trans,
  1172. u8 ctx)
  1173. {
  1174. u8 ac, txq_id;
  1175. struct iwl_trans_pcie *trans_pcie =
  1176. IWL_TRANS_GET_PCIE_TRANS(trans);
  1177. for (ac = 0; ac < AC_NUM; ac++) {
  1178. txq_id = trans_pcie->ac_to_queue[ctx][ac];
  1179. IWL_DEBUG_INFO(trans, "Queue Status: Q[%d] %s\n",
  1180. ac,
  1181. (atomic_read(&trans_pcie->queue_stop_count[ac]) > 0)
  1182. ? "stopped" : "awake");
  1183. iwl_wake_queue(trans, &trans_pcie->txq[txq_id]);
  1184. }
  1185. }
  1186. const struct iwl_trans_ops trans_ops_pcie;
  1187. static struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd)
  1188. {
  1189. struct iwl_trans *iwl_trans = kzalloc(sizeof(struct iwl_trans) +
  1190. sizeof(struct iwl_trans_pcie),
  1191. GFP_KERNEL);
  1192. if (iwl_trans) {
  1193. struct iwl_trans_pcie *trans_pcie =
  1194. IWL_TRANS_GET_PCIE_TRANS(iwl_trans);
  1195. iwl_trans->ops = &trans_ops_pcie;
  1196. iwl_trans->shrd = shrd;
  1197. trans_pcie->trans = iwl_trans;
  1198. spin_lock_init(&iwl_trans->hcmd_lock);
  1199. }
  1200. return iwl_trans;
  1201. }
  1202. static void iwl_trans_pcie_stop_queue(struct iwl_trans *trans, int txq_id)
  1203. {
  1204. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1205. iwl_stop_queue(trans, &trans_pcie->txq[txq_id]);
  1206. }
  1207. #define IWL_FLUSH_WAIT_MS 2000
  1208. static int iwl_trans_pcie_wait_tx_queue_empty(struct iwl_trans *trans)
  1209. {
  1210. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1211. struct iwl_tx_queue *txq;
  1212. struct iwl_queue *q;
  1213. int cnt;
  1214. unsigned long now = jiffies;
  1215. int ret = 0;
  1216. /* waiting for all the tx frames complete might take a while */
  1217. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1218. if (cnt == trans->shrd->cmd_queue)
  1219. continue;
  1220. txq = &trans_pcie->txq[cnt];
  1221. q = &txq->q;
  1222. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1223. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1224. msleep(1);
  1225. if (q->read_ptr != q->write_ptr) {
  1226. IWL_ERR(trans, "fail to flush all tx fifo queues\n");
  1227. ret = -ETIMEDOUT;
  1228. break;
  1229. }
  1230. }
  1231. return ret;
  1232. }
  1233. /*
  1234. * On every watchdog tick we check (latest) time stamp. If it does not
  1235. * change during timeout period and queue is not empty we reset firmware.
  1236. */
  1237. static int iwl_trans_pcie_check_stuck_queue(struct iwl_trans *trans, int cnt)
  1238. {
  1239. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1240. struct iwl_tx_queue *txq = &trans_pcie->txq[cnt];
  1241. struct iwl_queue *q = &txq->q;
  1242. unsigned long timeout;
  1243. if (q->read_ptr == q->write_ptr) {
  1244. txq->time_stamp = jiffies;
  1245. return 0;
  1246. }
  1247. timeout = txq->time_stamp +
  1248. msecs_to_jiffies(hw_params(trans).wd_timeout);
  1249. if (time_after(jiffies, timeout)) {
  1250. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", q->id,
  1251. hw_params(trans).wd_timeout);
  1252. IWL_ERR(trans, "Current read_ptr %d write_ptr %d\n",
  1253. q->read_ptr, q->write_ptr);
  1254. return 1;
  1255. }
  1256. return 0;
  1257. }
  1258. static const char *get_fh_string(int cmd)
  1259. {
  1260. switch (cmd) {
  1261. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1262. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1263. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1264. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1265. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1266. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1267. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1268. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1269. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1270. default:
  1271. return "UNKNOWN";
  1272. }
  1273. }
  1274. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1275. {
  1276. int i;
  1277. #ifdef CONFIG_IWLWIFI_DEBUG
  1278. int pos = 0;
  1279. size_t bufsz = 0;
  1280. #endif
  1281. static const u32 fh_tbl[] = {
  1282. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1283. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1284. FH_RSCSR_CHNL0_WPTR,
  1285. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1286. FH_MEM_RSSR_SHARED_CTRL_REG,
  1287. FH_MEM_RSSR_RX_STATUS_REG,
  1288. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1289. FH_TSSR_TX_STATUS_REG,
  1290. FH_TSSR_TX_ERROR_REG
  1291. };
  1292. #ifdef CONFIG_IWLWIFI_DEBUG
  1293. if (display) {
  1294. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1295. *buf = kmalloc(bufsz, GFP_KERNEL);
  1296. if (!*buf)
  1297. return -ENOMEM;
  1298. pos += scnprintf(*buf + pos, bufsz - pos,
  1299. "FH register values:\n");
  1300. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1301. pos += scnprintf(*buf + pos, bufsz - pos,
  1302. " %34s: 0X%08x\n",
  1303. get_fh_string(fh_tbl[i]),
  1304. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1305. }
  1306. return pos;
  1307. }
  1308. #endif
  1309. IWL_ERR(trans, "FH register values:\n");
  1310. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1311. IWL_ERR(trans, " %34s: 0X%08x\n",
  1312. get_fh_string(fh_tbl[i]),
  1313. iwl_read_direct32(bus(trans), fh_tbl[i]));
  1314. }
  1315. return 0;
  1316. }
  1317. static const char *get_csr_string(int cmd)
  1318. {
  1319. switch (cmd) {
  1320. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1321. IWL_CMD(CSR_INT_COALESCING);
  1322. IWL_CMD(CSR_INT);
  1323. IWL_CMD(CSR_INT_MASK);
  1324. IWL_CMD(CSR_FH_INT_STATUS);
  1325. IWL_CMD(CSR_GPIO_IN);
  1326. IWL_CMD(CSR_RESET);
  1327. IWL_CMD(CSR_GP_CNTRL);
  1328. IWL_CMD(CSR_HW_REV);
  1329. IWL_CMD(CSR_EEPROM_REG);
  1330. IWL_CMD(CSR_EEPROM_GP);
  1331. IWL_CMD(CSR_OTP_GP_REG);
  1332. IWL_CMD(CSR_GIO_REG);
  1333. IWL_CMD(CSR_GP_UCODE_REG);
  1334. IWL_CMD(CSR_GP_DRIVER_REG);
  1335. IWL_CMD(CSR_UCODE_DRV_GP1);
  1336. IWL_CMD(CSR_UCODE_DRV_GP2);
  1337. IWL_CMD(CSR_LED_REG);
  1338. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1339. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1340. IWL_CMD(CSR_ANA_PLL_CFG);
  1341. IWL_CMD(CSR_HW_REV_WA_REG);
  1342. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1343. default:
  1344. return "UNKNOWN";
  1345. }
  1346. }
  1347. void iwl_dump_csr(struct iwl_trans *trans)
  1348. {
  1349. int i;
  1350. static const u32 csr_tbl[] = {
  1351. CSR_HW_IF_CONFIG_REG,
  1352. CSR_INT_COALESCING,
  1353. CSR_INT,
  1354. CSR_INT_MASK,
  1355. CSR_FH_INT_STATUS,
  1356. CSR_GPIO_IN,
  1357. CSR_RESET,
  1358. CSR_GP_CNTRL,
  1359. CSR_HW_REV,
  1360. CSR_EEPROM_REG,
  1361. CSR_EEPROM_GP,
  1362. CSR_OTP_GP_REG,
  1363. CSR_GIO_REG,
  1364. CSR_GP_UCODE_REG,
  1365. CSR_GP_DRIVER_REG,
  1366. CSR_UCODE_DRV_GP1,
  1367. CSR_UCODE_DRV_GP2,
  1368. CSR_LED_REG,
  1369. CSR_DRAM_INT_TBL_REG,
  1370. CSR_GIO_CHICKEN_BITS,
  1371. CSR_ANA_PLL_CFG,
  1372. CSR_HW_REV_WA_REG,
  1373. CSR_DBG_HPET_MEM_REG
  1374. };
  1375. IWL_ERR(trans, "CSR values:\n");
  1376. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1377. "CSR_INT_PERIODIC_REG)\n");
  1378. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1379. IWL_ERR(trans, " %25s: 0X%08x\n",
  1380. get_csr_string(csr_tbl[i]),
  1381. iwl_read32(bus(trans), csr_tbl[i]));
  1382. }
  1383. }
  1384. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1385. /* create and remove of files */
  1386. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1387. if (!debugfs_create_file(#name, mode, parent, trans, \
  1388. &iwl_dbgfs_##name##_ops)) \
  1389. return -ENOMEM; \
  1390. } while (0)
  1391. /* file operation */
  1392. #define DEBUGFS_READ_FUNC(name) \
  1393. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1394. char __user *user_buf, \
  1395. size_t count, loff_t *ppos);
  1396. #define DEBUGFS_WRITE_FUNC(name) \
  1397. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1398. const char __user *user_buf, \
  1399. size_t count, loff_t *ppos);
  1400. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1401. {
  1402. file->private_data = inode->i_private;
  1403. return 0;
  1404. }
  1405. #define DEBUGFS_READ_FILE_OPS(name) \
  1406. DEBUGFS_READ_FUNC(name); \
  1407. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1408. .read = iwl_dbgfs_##name##_read, \
  1409. .open = iwl_dbgfs_open_file_generic, \
  1410. .llseek = generic_file_llseek, \
  1411. };
  1412. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1413. DEBUGFS_WRITE_FUNC(name); \
  1414. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1415. .write = iwl_dbgfs_##name##_write, \
  1416. .open = iwl_dbgfs_open_file_generic, \
  1417. .llseek = generic_file_llseek, \
  1418. };
  1419. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1420. DEBUGFS_READ_FUNC(name); \
  1421. DEBUGFS_WRITE_FUNC(name); \
  1422. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1423. .write = iwl_dbgfs_##name##_write, \
  1424. .read = iwl_dbgfs_##name##_read, \
  1425. .open = iwl_dbgfs_open_file_generic, \
  1426. .llseek = generic_file_llseek, \
  1427. };
  1428. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1429. char __user *user_buf,
  1430. size_t count, loff_t *ppos)
  1431. {
  1432. struct iwl_trans *trans = file->private_data;
  1433. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1434. struct iwl_tx_queue *txq;
  1435. struct iwl_queue *q;
  1436. char *buf;
  1437. int pos = 0;
  1438. int cnt;
  1439. int ret;
  1440. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1441. if (!trans_pcie->txq) {
  1442. IWL_ERR(trans, "txq not ready\n");
  1443. return -EAGAIN;
  1444. }
  1445. buf = kzalloc(bufsz, GFP_KERNEL);
  1446. if (!buf)
  1447. return -ENOMEM;
  1448. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1449. txq = &trans_pcie->txq[cnt];
  1450. q = &txq->q;
  1451. pos += scnprintf(buf + pos, bufsz - pos,
  1452. "hwq %.2d: read=%u write=%u stop=%d"
  1453. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1454. cnt, q->read_ptr, q->write_ptr,
  1455. !!test_bit(cnt, trans_pcie->queue_stopped),
  1456. txq->swq_id, txq->swq_id & 3,
  1457. (txq->swq_id >> 2) & 0x1f);
  1458. if (cnt >= 4)
  1459. continue;
  1460. /* for the ACs, display the stop count too */
  1461. pos += scnprintf(buf + pos, bufsz - pos,
  1462. " stop-count: %d\n",
  1463. atomic_read(&trans_pcie->queue_stop_count[cnt]));
  1464. }
  1465. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1466. kfree(buf);
  1467. return ret;
  1468. }
  1469. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1470. char __user *user_buf,
  1471. size_t count, loff_t *ppos) {
  1472. struct iwl_trans *trans = file->private_data;
  1473. struct iwl_trans_pcie *trans_pcie =
  1474. IWL_TRANS_GET_PCIE_TRANS(trans);
  1475. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1476. char buf[256];
  1477. int pos = 0;
  1478. const size_t bufsz = sizeof(buf);
  1479. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1480. rxq->read);
  1481. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1482. rxq->write);
  1483. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1484. rxq->free_count);
  1485. if (rxq->rb_stts) {
  1486. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1487. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1488. } else {
  1489. pos += scnprintf(buf + pos, bufsz - pos,
  1490. "closed_rb_num: Not Allocated\n");
  1491. }
  1492. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1493. }
  1494. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1495. char __user *user_buf,
  1496. size_t count, loff_t *ppos)
  1497. {
  1498. struct iwl_trans *trans = file->private_data;
  1499. char *buf;
  1500. int pos = 0;
  1501. ssize_t ret = -ENOMEM;
  1502. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1503. if (buf) {
  1504. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1505. kfree(buf);
  1506. }
  1507. return ret;
  1508. }
  1509. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1510. const char __user *user_buf,
  1511. size_t count, loff_t *ppos)
  1512. {
  1513. struct iwl_trans *trans = file->private_data;
  1514. u32 event_log_flag;
  1515. char buf[8];
  1516. int buf_size;
  1517. memset(buf, 0, sizeof(buf));
  1518. buf_size = min(count, sizeof(buf) - 1);
  1519. if (copy_from_user(buf, user_buf, buf_size))
  1520. return -EFAULT;
  1521. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1522. return -EFAULT;
  1523. if (event_log_flag == 1)
  1524. iwl_dump_nic_event_log(trans, true, NULL, false);
  1525. return count;
  1526. }
  1527. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1528. char __user *user_buf,
  1529. size_t count, loff_t *ppos) {
  1530. struct iwl_trans *trans = file->private_data;
  1531. struct iwl_trans_pcie *trans_pcie =
  1532. IWL_TRANS_GET_PCIE_TRANS(trans);
  1533. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1534. int pos = 0;
  1535. char *buf;
  1536. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1537. ssize_t ret;
  1538. buf = kzalloc(bufsz, GFP_KERNEL);
  1539. if (!buf) {
  1540. IWL_ERR(trans, "Can not allocate Buffer\n");
  1541. return -ENOMEM;
  1542. }
  1543. pos += scnprintf(buf + pos, bufsz - pos,
  1544. "Interrupt Statistics Report:\n");
  1545. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1546. isr_stats->hw);
  1547. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1548. isr_stats->sw);
  1549. if (isr_stats->sw || isr_stats->hw) {
  1550. pos += scnprintf(buf + pos, bufsz - pos,
  1551. "\tLast Restarting Code: 0x%X\n",
  1552. isr_stats->err_code);
  1553. }
  1554. #ifdef CONFIG_IWLWIFI_DEBUG
  1555. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1556. isr_stats->sch);
  1557. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1558. isr_stats->alive);
  1559. #endif
  1560. pos += scnprintf(buf + pos, bufsz - pos,
  1561. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1562. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1563. isr_stats->ctkill);
  1564. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1565. isr_stats->wakeup);
  1566. pos += scnprintf(buf + pos, bufsz - pos,
  1567. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1568. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1569. isr_stats->tx);
  1570. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1571. isr_stats->unhandled);
  1572. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1573. kfree(buf);
  1574. return ret;
  1575. }
  1576. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1577. const char __user *user_buf,
  1578. size_t count, loff_t *ppos)
  1579. {
  1580. struct iwl_trans *trans = file->private_data;
  1581. struct iwl_trans_pcie *trans_pcie =
  1582. IWL_TRANS_GET_PCIE_TRANS(trans);
  1583. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1584. char buf[8];
  1585. int buf_size;
  1586. u32 reset_flag;
  1587. memset(buf, 0, sizeof(buf));
  1588. buf_size = min(count, sizeof(buf) - 1);
  1589. if (copy_from_user(buf, user_buf, buf_size))
  1590. return -EFAULT;
  1591. if (sscanf(buf, "%x", &reset_flag) != 1)
  1592. return -EFAULT;
  1593. if (reset_flag == 0)
  1594. memset(isr_stats, 0, sizeof(*isr_stats));
  1595. return count;
  1596. }
  1597. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1598. const char __user *user_buf,
  1599. size_t count, loff_t *ppos)
  1600. {
  1601. struct iwl_trans *trans = file->private_data;
  1602. char buf[8];
  1603. int buf_size;
  1604. int csr;
  1605. memset(buf, 0, sizeof(buf));
  1606. buf_size = min(count, sizeof(buf) - 1);
  1607. if (copy_from_user(buf, user_buf, buf_size))
  1608. return -EFAULT;
  1609. if (sscanf(buf, "%d", &csr) != 1)
  1610. return -EFAULT;
  1611. iwl_dump_csr(trans);
  1612. return count;
  1613. }
  1614. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1615. char __user *user_buf,
  1616. size_t count, loff_t *ppos)
  1617. {
  1618. struct iwl_trans *trans = file->private_data;
  1619. char *buf;
  1620. int pos = 0;
  1621. ssize_t ret = -EFAULT;
  1622. ret = pos = iwl_dump_fh(trans, &buf, true);
  1623. if (buf) {
  1624. ret = simple_read_from_buffer(user_buf,
  1625. count, ppos, buf, pos);
  1626. kfree(buf);
  1627. }
  1628. return ret;
  1629. }
  1630. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1631. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1632. DEBUGFS_READ_FILE_OPS(fh_reg);
  1633. DEBUGFS_READ_FILE_OPS(rx_queue);
  1634. DEBUGFS_READ_FILE_OPS(tx_queue);
  1635. DEBUGFS_WRITE_FILE_OPS(csr);
  1636. /*
  1637. * Create the debugfs files and directories
  1638. *
  1639. */
  1640. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1641. struct dentry *dir)
  1642. {
  1643. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1644. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1645. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1646. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1647. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1648. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1649. return 0;
  1650. }
  1651. #else
  1652. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1653. struct dentry *dir)
  1654. { return 0; }
  1655. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1656. const struct iwl_trans_ops trans_ops_pcie = {
  1657. .alloc = iwl_trans_pcie_alloc,
  1658. .request_irq = iwl_trans_pcie_request_irq,
  1659. .start_device = iwl_trans_pcie_start_device,
  1660. .prepare_card_hw = iwl_trans_pcie_prepare_card_hw,
  1661. .stop_device = iwl_trans_pcie_stop_device,
  1662. .tx_start = iwl_trans_pcie_tx_start,
  1663. .wake_any_queue = iwl_trans_pcie_wake_any_queue,
  1664. .send_cmd = iwl_trans_pcie_send_cmd,
  1665. .send_cmd_pdu = iwl_trans_pcie_send_cmd_pdu,
  1666. .tx = iwl_trans_pcie_tx,
  1667. .reclaim = iwl_trans_pcie_reclaim,
  1668. .tx_agg_disable = iwl_trans_pcie_tx_agg_disable,
  1669. .tx_agg_alloc = iwl_trans_pcie_tx_agg_alloc,
  1670. .tx_agg_setup = iwl_trans_pcie_tx_agg_setup,
  1671. .kick_nic = iwl_trans_pcie_kick_nic,
  1672. .free = iwl_trans_pcie_free,
  1673. .stop_queue = iwl_trans_pcie_stop_queue,
  1674. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1675. .wait_tx_queue_empty = iwl_trans_pcie_wait_tx_queue_empty,
  1676. .check_stuck_queue = iwl_trans_pcie_check_stuck_queue,
  1677. .suspend = iwl_trans_pcie_suspend,
  1678. .resume = iwl_trans_pcie_resume,
  1679. };