iwl-trans-pcie.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  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. /* PCI registers */
  539. #define PCI_CFG_RETRY_TIMEOUT 0x041
  540. #define PCI_CFG_LINK_CTRL_VAL_L0S_EN 0x01
  541. #define PCI_CFG_LINK_CTRL_VAL_L1_EN 0x02
  542. static u16 iwl_pciexp_link_ctrl(struct iwl_trans *trans)
  543. {
  544. int pos;
  545. u16 pci_lnk_ctl;
  546. struct iwl_trans_pcie *trans_pcie =
  547. IWL_TRANS_GET_PCIE_TRANS(trans);
  548. struct pci_dev *pci_dev = trans_pcie->pci_dev;
  549. pos = pci_pcie_cap(pci_dev);
  550. pci_read_config_word(pci_dev, pos + PCI_EXP_LNKCTL, &pci_lnk_ctl);
  551. return pci_lnk_ctl;
  552. }
  553. static void iwl_apm_config(struct iwl_trans *trans)
  554. {
  555. /*
  556. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  557. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  558. * If so (likely), disable L0S, so device moves directly L0->L1;
  559. * costs negligible amount of power savings.
  560. * If not (unlikely), enable L0S, so there is at least some
  561. * power savings, even without L1.
  562. */
  563. u16 lctl = iwl_pciexp_link_ctrl(trans);
  564. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  565. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  566. /* L1-ASPM enabled; disable(!) L0S */
  567. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  568. dev_printk(KERN_INFO, trans->dev,
  569. "L1 Enabled; Disabling L0S\n");
  570. } else {
  571. /* L1-ASPM disabled; enable(!) L0S */
  572. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  573. dev_printk(KERN_INFO, trans->dev,
  574. "L1 Disabled; Enabling L0S\n");
  575. }
  576. trans->pm_support = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  577. }
  578. /*
  579. * Start up NIC's basic functionality after it has been reset
  580. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  581. * NOTE: This does not load uCode nor start the embedded processor
  582. */
  583. static int iwl_apm_init(struct iwl_trans *trans)
  584. {
  585. int ret = 0;
  586. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  587. /*
  588. * Use "set_bit" below rather than "write", to preserve any hardware
  589. * bits already set by default after reset.
  590. */
  591. /* Disable L0S exit timer (platform NMI Work/Around) */
  592. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  593. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  594. /*
  595. * Disable L0s without affecting L1;
  596. * don't wait for ICH L0s (ICH bug W/A)
  597. */
  598. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  599. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  600. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  601. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  602. /*
  603. * Enable HAP INTA (interrupt from management bus) to
  604. * wake device's PCI Express link L1a -> L0s
  605. */
  606. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  607. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  608. iwl_apm_config(trans);
  609. /* Configure analog phase-lock-loop before activating to D0A */
  610. if (cfg(trans)->base_params->pll_cfg_val)
  611. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  612. cfg(trans)->base_params->pll_cfg_val);
  613. /*
  614. * Set "initialization complete" bit to move adapter from
  615. * D0U* --> D0A* (powered-up active) state.
  616. */
  617. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  618. /*
  619. * Wait for clock stabilization; once stabilized, access to
  620. * device-internal resources is supported, e.g. iwl_write_prph()
  621. * and accesses to uCode SRAM.
  622. */
  623. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  624. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  625. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  626. if (ret < 0) {
  627. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  628. goto out;
  629. }
  630. /*
  631. * Enable DMA clock and wait for it to stabilize.
  632. *
  633. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  634. * do not disable clocks. This preserves any hardware bits already
  635. * set by default in "CLK_CTRL_REG" after reset.
  636. */
  637. iwl_write_prph(trans, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  638. udelay(20);
  639. /* Disable L1-Active */
  640. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  641. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  642. set_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status);
  643. out:
  644. return ret;
  645. }
  646. static int iwl_apm_stop_master(struct iwl_trans *trans)
  647. {
  648. int ret = 0;
  649. /* stop device's busmaster DMA activity */
  650. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  651. ret = iwl_poll_bit(trans, CSR_RESET,
  652. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  653. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  654. if (ret)
  655. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  656. IWL_DEBUG_INFO(trans, "stop master\n");
  657. return ret;
  658. }
  659. static void iwl_apm_stop(struct iwl_trans *trans)
  660. {
  661. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  662. clear_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status);
  663. /* Stop device's DMA activity */
  664. iwl_apm_stop_master(trans);
  665. /* Reset the entire device */
  666. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  667. udelay(10);
  668. /*
  669. * Clear "initialization complete" bit to move adapter from
  670. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  671. */
  672. iwl_clear_bit(trans, CSR_GP_CNTRL,
  673. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  674. }
  675. static int iwl_nic_init(struct iwl_trans *trans)
  676. {
  677. unsigned long flags;
  678. /* nic_init */
  679. spin_lock_irqsave(&trans->shrd->lock, flags);
  680. iwl_apm_init(trans);
  681. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  682. iwl_write8(trans, CSR_INT_COALESCING,
  683. IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  684. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  685. iwl_set_pwr_vmain(trans);
  686. iwl_nic_config(priv(trans));
  687. #ifndef CONFIG_IWLWIFI_IDI
  688. /* Allocate the RX queue, or reset if it is already allocated */
  689. iwl_rx_init(trans);
  690. #endif
  691. /* Allocate or reset and init all Tx and Command queues */
  692. if (iwl_tx_init(trans))
  693. return -ENOMEM;
  694. if (hw_params(trans).shadow_reg_enable) {
  695. /* enable shadow regs in HW */
  696. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL,
  697. 0x800FFFFF);
  698. }
  699. set_bit(STATUS_INIT, &trans->shrd->status);
  700. return 0;
  701. }
  702. #define HW_READY_TIMEOUT (50)
  703. /* Note: returns poll_bit return value, which is >= 0 if success */
  704. static int iwl_set_hw_ready(struct iwl_trans *trans)
  705. {
  706. int ret;
  707. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  708. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  709. /* See if we got it */
  710. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  711. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  712. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  713. HW_READY_TIMEOUT);
  714. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  715. return ret;
  716. }
  717. /* Note: returns standard 0/-ERROR code */
  718. static int iwl_prepare_card_hw(struct iwl_trans *trans)
  719. {
  720. int ret;
  721. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  722. ret = iwl_set_hw_ready(trans);
  723. /* If the card is ready, exit 0 */
  724. if (ret >= 0)
  725. return 0;
  726. /* If HW is not ready, prepare the conditions to check again */
  727. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  728. CSR_HW_IF_CONFIG_REG_PREPARE);
  729. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  730. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  731. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  732. if (ret < 0)
  733. return ret;
  734. /* HW should be ready by now, check again. */
  735. ret = iwl_set_hw_ready(trans);
  736. if (ret >= 0)
  737. return 0;
  738. return ret;
  739. }
  740. #define IWL_AC_UNSET -1
  741. struct queue_to_fifo_ac {
  742. s8 fifo, ac;
  743. };
  744. static const struct queue_to_fifo_ac iwlagn_default_queue_to_tx_fifo[] = {
  745. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  746. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  747. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  748. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  749. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  750. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  751. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  752. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  753. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  754. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  755. { IWL_TX_FIFO_UNUSED, IWL_AC_UNSET, },
  756. };
  757. static const struct queue_to_fifo_ac iwlagn_ipan_queue_to_tx_fifo[] = {
  758. { IWL_TX_FIFO_VO, IEEE80211_AC_VO, },
  759. { IWL_TX_FIFO_VI, IEEE80211_AC_VI, },
  760. { IWL_TX_FIFO_BE, IEEE80211_AC_BE, },
  761. { IWL_TX_FIFO_BK, IEEE80211_AC_BK, },
  762. { IWL_TX_FIFO_BK_IPAN, IEEE80211_AC_BK, },
  763. { IWL_TX_FIFO_BE_IPAN, IEEE80211_AC_BE, },
  764. { IWL_TX_FIFO_VI_IPAN, IEEE80211_AC_VI, },
  765. { IWL_TX_FIFO_VO_IPAN, IEEE80211_AC_VO, },
  766. { IWL_TX_FIFO_BE_IPAN, 2, },
  767. { IWLAGN_CMD_FIFO_NUM, IWL_AC_UNSET, },
  768. { IWL_TX_FIFO_AUX, IWL_AC_UNSET, },
  769. };
  770. static const u8 iwlagn_bss_ac_to_fifo[] = {
  771. IWL_TX_FIFO_VO,
  772. IWL_TX_FIFO_VI,
  773. IWL_TX_FIFO_BE,
  774. IWL_TX_FIFO_BK,
  775. };
  776. static const u8 iwlagn_bss_ac_to_queue[] = {
  777. 0, 1, 2, 3,
  778. };
  779. static const u8 iwlagn_pan_ac_to_fifo[] = {
  780. IWL_TX_FIFO_VO_IPAN,
  781. IWL_TX_FIFO_VI_IPAN,
  782. IWL_TX_FIFO_BE_IPAN,
  783. IWL_TX_FIFO_BK_IPAN,
  784. };
  785. static const u8 iwlagn_pan_ac_to_queue[] = {
  786. 7, 6, 5, 4,
  787. };
  788. /*
  789. * ucode
  790. */
  791. static int iwl_load_section(struct iwl_trans *trans, const char *name,
  792. struct fw_desc *image, u32 dst_addr)
  793. {
  794. dma_addr_t phy_addr = image->p_addr;
  795. u32 byte_cnt = image->len;
  796. int ret;
  797. trans->ucode_write_complete = 0;
  798. iwl_write_direct32(trans,
  799. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  800. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  801. iwl_write_direct32(trans,
  802. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL), dst_addr);
  803. iwl_write_direct32(trans,
  804. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  805. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  806. iwl_write_direct32(trans,
  807. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  808. (iwl_get_dma_hi_addr(phy_addr)
  809. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  810. iwl_write_direct32(trans,
  811. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  812. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  813. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  814. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  815. iwl_write_direct32(trans,
  816. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  817. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  818. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  819. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  820. IWL_DEBUG_FW(trans, "%s uCode section being loaded...\n", name);
  821. ret = wait_event_timeout(trans->shrd->wait_command_queue,
  822. trans->ucode_write_complete, 5 * HZ);
  823. if (!ret) {
  824. IWL_ERR(trans, "Could not load the %s uCode section\n",
  825. name);
  826. return -ETIMEDOUT;
  827. }
  828. return 0;
  829. }
  830. static int iwl_load_given_ucode(struct iwl_trans *trans, struct fw_img *image)
  831. {
  832. int ret = 0;
  833. ret = iwl_load_section(trans, "INST", &image->code,
  834. IWLAGN_RTC_INST_LOWER_BOUND);
  835. if (ret)
  836. return ret;
  837. ret = iwl_load_section(trans, "DATA", &image->data,
  838. IWLAGN_RTC_DATA_LOWER_BOUND);
  839. if (ret)
  840. return ret;
  841. /* Remove all resets to allow NIC to operate */
  842. iwl_write32(trans, CSR_RESET, 0);
  843. return 0;
  844. }
  845. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans, struct fw_img *fw)
  846. {
  847. int ret;
  848. struct iwl_trans_pcie *trans_pcie =
  849. IWL_TRANS_GET_PCIE_TRANS(trans);
  850. trans->shrd->ucode_owner = IWL_OWNERSHIP_DRIVER;
  851. trans_pcie->ac_to_queue[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_queue;
  852. trans_pcie->ac_to_queue[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_queue;
  853. trans_pcie->ac_to_fifo[IWL_RXON_CTX_BSS] = iwlagn_bss_ac_to_fifo;
  854. trans_pcie->ac_to_fifo[IWL_RXON_CTX_PAN] = iwlagn_pan_ac_to_fifo;
  855. trans_pcie->mcast_queue[IWL_RXON_CTX_BSS] = 0;
  856. trans_pcie->mcast_queue[IWL_RXON_CTX_PAN] = IWL_IPAN_MCAST_QUEUE;
  857. if ((hw_params(trans).sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  858. iwl_prepare_card_hw(trans)) {
  859. IWL_WARN(trans, "Exit HW not ready\n");
  860. return -EIO;
  861. }
  862. /* If platform's RF_KILL switch is NOT set to KILL */
  863. if (iwl_read32(trans, CSR_GP_CNTRL) &
  864. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  865. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  866. else
  867. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  868. if (iwl_is_rfkill(trans->shrd)) {
  869. iwl_set_hw_rfkill_state(priv(trans), true);
  870. iwl_enable_interrupts(trans);
  871. return -ERFKILL;
  872. }
  873. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  874. ret = iwl_nic_init(trans);
  875. if (ret) {
  876. IWL_ERR(trans, "Unable to init nic\n");
  877. return ret;
  878. }
  879. /* make sure rfkill handshake bits are cleared */
  880. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  881. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  882. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  883. /* clear (again), then enable host interrupts */
  884. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  885. iwl_enable_interrupts(trans);
  886. /* really make sure rfkill handshake bits are cleared */
  887. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  888. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  889. /* Load the given image to the HW */
  890. iwl_load_given_ucode(trans, fw);
  891. return 0;
  892. }
  893. /*
  894. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  895. * must be called under priv->shrd->lock and mac access
  896. */
  897. static void iwl_trans_txq_set_sched(struct iwl_trans *trans, u32 mask)
  898. {
  899. iwl_write_prph(trans, SCD_TXFACT, mask);
  900. }
  901. static void iwl_tx_start(struct iwl_trans *trans)
  902. {
  903. const struct queue_to_fifo_ac *queue_to_fifo;
  904. struct iwl_trans_pcie *trans_pcie =
  905. IWL_TRANS_GET_PCIE_TRANS(trans);
  906. u32 a;
  907. unsigned long flags;
  908. int i, chan;
  909. u32 reg_val;
  910. spin_lock_irqsave(&trans->shrd->lock, flags);
  911. trans_pcie->scd_base_addr =
  912. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  913. a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND;
  914. /* reset conext data memory */
  915. for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND;
  916. a += 4)
  917. iwl_write_targ_mem(trans, a, 0);
  918. /* reset tx status memory */
  919. for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND;
  920. a += 4)
  921. iwl_write_targ_mem(trans, a, 0);
  922. for (; a < trans_pcie->scd_base_addr +
  923. SCD_TRANS_TBL_OFFSET_QUEUE(hw_params(trans).max_txq_num);
  924. a += 4)
  925. iwl_write_targ_mem(trans, a, 0);
  926. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  927. trans_pcie->scd_bc_tbls.dma >> 10);
  928. /* Enable DMA channel */
  929. for (chan = 0; chan < FH_TCSR_CHNL_NUM ; chan++)
  930. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  931. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  932. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  933. /* Update FH chicken bits */
  934. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  935. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  936. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  937. iwl_write_prph(trans, SCD_QUEUECHAIN_SEL,
  938. SCD_QUEUECHAIN_SEL_ALL(trans));
  939. iwl_write_prph(trans, SCD_AGGR_SEL, 0);
  940. /* initiate the queues */
  941. for (i = 0; i < hw_params(trans).max_txq_num; i++) {
  942. iwl_write_prph(trans, SCD_QUEUE_RDPTR(i), 0);
  943. iwl_write_direct32(trans, HBUS_TARG_WRPTR, 0 | (i << 8));
  944. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  945. SCD_CONTEXT_QUEUE_OFFSET(i), 0);
  946. iwl_write_targ_mem(trans, trans_pcie->scd_base_addr +
  947. SCD_CONTEXT_QUEUE_OFFSET(i) +
  948. sizeof(u32),
  949. ((SCD_WIN_SIZE <<
  950. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  951. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  952. ((SCD_FRAME_LIMIT <<
  953. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  954. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  955. }
  956. iwl_write_prph(trans, SCD_INTERRUPT_MASK,
  957. IWL_MASK(0, hw_params(trans).max_txq_num));
  958. /* Activate all Tx DMA/FIFO channels */
  959. iwl_trans_txq_set_sched(trans, IWL_MASK(0, 7));
  960. /* map queues to FIFOs */
  961. if (trans->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS))
  962. queue_to_fifo = iwlagn_ipan_queue_to_tx_fifo;
  963. else
  964. queue_to_fifo = iwlagn_default_queue_to_tx_fifo;
  965. iwl_trans_set_wr_ptrs(trans, trans->shrd->cmd_queue, 0);
  966. /* make sure all queue are not stopped */
  967. memset(&trans_pcie->queue_stopped[0], 0,
  968. sizeof(trans_pcie->queue_stopped));
  969. for (i = 0; i < 4; i++)
  970. atomic_set(&trans_pcie->queue_stop_count[i], 0);
  971. /* reset to 0 to enable all the queue first */
  972. trans_pcie->txq_ctx_active_msk = 0;
  973. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo) <
  974. IWLAGN_FIRST_AMPDU_QUEUE);
  975. BUILD_BUG_ON(ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo) <
  976. IWLAGN_FIRST_AMPDU_QUEUE);
  977. for (i = 0; i < IWLAGN_FIRST_AMPDU_QUEUE; i++) {
  978. int fifo = queue_to_fifo[i].fifo;
  979. int ac = queue_to_fifo[i].ac;
  980. iwl_txq_ctx_activate(trans_pcie, i);
  981. if (fifo == IWL_TX_FIFO_UNUSED)
  982. continue;
  983. if (ac != IWL_AC_UNSET)
  984. iwl_set_swq_id(&trans_pcie->txq[i], ac, i);
  985. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[i],
  986. fifo, 0);
  987. }
  988. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  989. /* Enable L1-Active */
  990. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  991. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  992. }
  993. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans)
  994. {
  995. iwl_reset_ict(trans);
  996. iwl_tx_start(trans);
  997. }
  998. /**
  999. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  1000. */
  1001. static int iwl_trans_tx_stop(struct iwl_trans *trans)
  1002. {
  1003. int ch, txq_id;
  1004. unsigned long flags;
  1005. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1006. /* Turn off all Tx DMA fifos */
  1007. spin_lock_irqsave(&trans->shrd->lock, flags);
  1008. iwl_trans_txq_set_sched(trans, 0);
  1009. /* Stop each Tx DMA channel, and wait for it to be idle */
  1010. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  1011. iwl_write_direct32(trans,
  1012. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  1013. if (iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
  1014. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  1015. 1000))
  1016. IWL_ERR(trans, "Failing on timeout while stopping"
  1017. " DMA channel %d [0x%08x]", ch,
  1018. iwl_read_direct32(trans,
  1019. FH_TSSR_TX_STATUS_REG));
  1020. }
  1021. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1022. if (!trans_pcie->txq) {
  1023. IWL_WARN(trans, "Stopping tx queues that aren't allocated...");
  1024. return 0;
  1025. }
  1026. /* Unmap DMA from host system and free skb's */
  1027. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  1028. iwl_tx_queue_unmap(trans, txq_id);
  1029. return 0;
  1030. }
  1031. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  1032. {
  1033. unsigned long flags;
  1034. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1035. /* tell the device to stop sending interrupts */
  1036. spin_lock_irqsave(&trans->shrd->lock, flags);
  1037. iwl_disable_interrupts(trans);
  1038. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1039. /* device going down, Stop using ICT table */
  1040. iwl_disable_ict(trans);
  1041. /*
  1042. * If a HW restart happens during firmware loading,
  1043. * then the firmware loading might call this function
  1044. * and later it might be called again due to the
  1045. * restart. So don't process again if the device is
  1046. * already dead.
  1047. */
  1048. if (test_bit(STATUS_DEVICE_ENABLED, &trans->shrd->status)) {
  1049. iwl_trans_tx_stop(trans);
  1050. #ifndef CONFIG_IWLWIFI_IDI
  1051. iwl_trans_rx_stop(trans);
  1052. #endif
  1053. /* Power-down device's busmaster DMA clocks */
  1054. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  1055. APMG_CLK_VAL_DMA_CLK_RQT);
  1056. udelay(5);
  1057. }
  1058. /* Make sure (redundant) we've released our request to stay awake */
  1059. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1060. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1061. /* Stop the device, and put it in low power state */
  1062. iwl_apm_stop(trans);
  1063. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  1064. * Clean again the interrupt here
  1065. */
  1066. spin_lock_irqsave(&trans->shrd->lock, flags);
  1067. iwl_disable_interrupts(trans);
  1068. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  1069. /* wait to make sure we flush pending tasklet*/
  1070. synchronize_irq(trans->irq);
  1071. tasklet_kill(&trans_pcie->irq_tasklet);
  1072. /* stop and reset the on-board processor */
  1073. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  1074. }
  1075. static int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1076. struct iwl_device_cmd *dev_cmd, enum iwl_rxon_context_id ctx,
  1077. u8 sta_id, u8 tid)
  1078. {
  1079. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1080. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1081. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1082. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
  1083. struct iwl_cmd_meta *out_meta;
  1084. struct iwl_tx_queue *txq;
  1085. struct iwl_queue *q;
  1086. dma_addr_t phys_addr = 0;
  1087. dma_addr_t txcmd_phys;
  1088. dma_addr_t scratch_phys;
  1089. u16 len, firstlen, secondlen;
  1090. u8 wait_write_ptr = 0;
  1091. u8 txq_id;
  1092. bool is_agg = false;
  1093. __le16 fc = hdr->frame_control;
  1094. u8 hdr_len = ieee80211_hdrlen(fc);
  1095. u16 __maybe_unused wifi_seq;
  1096. /*
  1097. * Send this frame after DTIM -- there's a special queue
  1098. * reserved for this for contexts that support AP mode.
  1099. */
  1100. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1101. txq_id = trans_pcie->mcast_queue[ctx];
  1102. /*
  1103. * The microcode will clear the more data
  1104. * bit in the last frame it transmits.
  1105. */
  1106. hdr->frame_control |=
  1107. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1108. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  1109. txq_id = IWL_AUX_QUEUE;
  1110. else
  1111. txq_id =
  1112. trans_pcie->ac_to_queue[ctx][skb_get_queue_mapping(skb)];
  1113. /* aggregation is on for this <sta,tid> */
  1114. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1115. WARN_ON(tid >= IWL_MAX_TID_COUNT);
  1116. txq_id = trans_pcie->agg_txq[sta_id][tid];
  1117. is_agg = true;
  1118. }
  1119. txq = &trans_pcie->txq[txq_id];
  1120. q = &txq->q;
  1121. /* In AGG mode, the index in the ring must correspond to the WiFi
  1122. * sequence number. This is a HW requirements to help the SCD to parse
  1123. * the BA.
  1124. * Check here that the packets are in the right place on the ring.
  1125. */
  1126. #ifdef CONFIG_IWLWIFI_DEBUG
  1127. wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1128. WARN_ONCE(is_agg && ((wifi_seq & 0xff) != q->write_ptr),
  1129. "Q: %d WiFi Seq %d tfdNum %d",
  1130. txq_id, wifi_seq, q->write_ptr);
  1131. #endif
  1132. /* Set up driver data for this TFD */
  1133. txq->skbs[q->write_ptr] = skb;
  1134. txq->cmd[q->write_ptr] = dev_cmd;
  1135. dev_cmd->hdr.cmd = REPLY_TX;
  1136. dev_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1137. INDEX_TO_SEQ(q->write_ptr)));
  1138. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1139. out_meta = &txq->meta[q->write_ptr];
  1140. /*
  1141. * Use the first empty entry in this queue's command buffer array
  1142. * to contain the Tx command and MAC header concatenated together
  1143. * (payload data will be in another buffer).
  1144. * Size of this varies, due to varying MAC header length.
  1145. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1146. * of the MAC header (device reads on dword boundaries).
  1147. * We'll tell device about this padding later.
  1148. */
  1149. len = sizeof(struct iwl_tx_cmd) +
  1150. sizeof(struct iwl_cmd_header) + hdr_len;
  1151. firstlen = (len + 3) & ~3;
  1152. /* Tell NIC about any 2-byte padding after MAC header */
  1153. if (firstlen != len)
  1154. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1155. /* Physical address of this Tx command's header (not MAC header!),
  1156. * within command buffer array. */
  1157. txcmd_phys = dma_map_single(trans->dev,
  1158. &dev_cmd->hdr, firstlen,
  1159. DMA_BIDIRECTIONAL);
  1160. if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
  1161. return -1;
  1162. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  1163. dma_unmap_len_set(out_meta, len, firstlen);
  1164. if (!ieee80211_has_morefrags(fc)) {
  1165. txq->need_update = 1;
  1166. } else {
  1167. wait_write_ptr = 1;
  1168. txq->need_update = 0;
  1169. }
  1170. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1171. * if any (802.11 null frames have no payload). */
  1172. secondlen = skb->len - hdr_len;
  1173. if (secondlen > 0) {
  1174. phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
  1175. secondlen, DMA_TO_DEVICE);
  1176. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  1177. dma_unmap_single(trans->dev,
  1178. dma_unmap_addr(out_meta, mapping),
  1179. dma_unmap_len(out_meta, len),
  1180. DMA_BIDIRECTIONAL);
  1181. return -1;
  1182. }
  1183. }
  1184. /* Attach buffers to TFD */
  1185. iwlagn_txq_attach_buf_to_tfd(trans, txq, txcmd_phys, firstlen, 1);
  1186. if (secondlen > 0)
  1187. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  1188. secondlen, 0);
  1189. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  1190. offsetof(struct iwl_tx_cmd, scratch);
  1191. /* take back ownership of DMA buffer to enable update */
  1192. dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
  1193. DMA_BIDIRECTIONAL);
  1194. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1195. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1196. IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n",
  1197. le16_to_cpu(dev_cmd->hdr.sequence));
  1198. IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  1199. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  1200. iwl_print_hex_dump(trans, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  1201. /* Set up entry for this TFD in Tx byte-count array */
  1202. iwl_trans_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1203. dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
  1204. DMA_BIDIRECTIONAL);
  1205. trace_iwlwifi_dev_tx(priv(trans),
  1206. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  1207. sizeof(struct iwl_tfd),
  1208. &dev_cmd->hdr, firstlen,
  1209. skb->data + hdr_len, secondlen);
  1210. /* Tell device the write index *just past* this latest filled TFD */
  1211. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1212. iwl_txq_update_write_ptr(trans, txq);
  1213. /*
  1214. * At this point the frame is "transmitted" successfully
  1215. * and we will get a TX status notification eventually,
  1216. * regardless of the value of ret. "ret" only indicates
  1217. * whether or not we should update the write pointer.
  1218. */
  1219. if (iwl_queue_space(q) < q->high_mark) {
  1220. if (wait_write_ptr) {
  1221. txq->need_update = 1;
  1222. iwl_txq_update_write_ptr(trans, txq);
  1223. } else {
  1224. iwl_stop_queue(trans, txq, "Queue is full");
  1225. }
  1226. }
  1227. return 0;
  1228. }
  1229. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  1230. {
  1231. struct iwl_trans_pcie *trans_pcie =
  1232. IWL_TRANS_GET_PCIE_TRANS(trans);
  1233. int err;
  1234. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1235. if (!trans_pcie->irq_requested) {
  1236. tasklet_init(&trans_pcie->irq_tasklet, (void (*)(unsigned long))
  1237. iwl_irq_tasklet, (unsigned long)trans);
  1238. iwl_alloc_isr_ict(trans);
  1239. err = request_irq(trans->irq, iwl_isr_ict, IRQF_SHARED,
  1240. DRV_NAME, trans);
  1241. if (err) {
  1242. IWL_ERR(trans, "Error allocating IRQ %d\n",
  1243. trans->irq);
  1244. goto error;
  1245. }
  1246. INIT_WORK(&trans_pcie->rx_replenish, iwl_bg_rx_replenish);
  1247. trans_pcie->irq_requested = true;
  1248. }
  1249. err = iwl_prepare_card_hw(trans);
  1250. if (err) {
  1251. IWL_ERR(trans, "Error while preparing HW: %d", err);
  1252. goto error;
  1253. }
  1254. iwl_apm_init(trans);
  1255. /* If platform's RF_KILL switch is NOT set to KILL */
  1256. if (iwl_read32(trans,
  1257. CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  1258. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1259. else
  1260. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1261. iwl_set_hw_rfkill_state(priv(trans),
  1262. test_bit(STATUS_RF_KILL_HW,
  1263. &trans->shrd->status));
  1264. return err;
  1265. error:
  1266. iwl_free_isr_ict(trans);
  1267. tasklet_kill(&trans_pcie->irq_tasklet);
  1268. return err;
  1269. }
  1270. static void iwl_trans_pcie_stop_hw(struct iwl_trans *trans)
  1271. {
  1272. iwl_apm_stop(trans);
  1273. /* Even if we stop the HW, we still want the RF kill interrupt */
  1274. IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
  1275. iwl_write32(trans, CSR_INT_MASK, CSR_INT_BIT_RF_KILL);
  1276. }
  1277. static int iwl_trans_pcie_reclaim(struct iwl_trans *trans, int sta_id, int tid,
  1278. int txq_id, int ssn, u32 status,
  1279. struct sk_buff_head *skbs)
  1280. {
  1281. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1282. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  1283. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  1284. int tfd_num = ssn & (txq->q.n_bd - 1);
  1285. int freed = 0;
  1286. txq->time_stamp = jiffies;
  1287. if (unlikely(txq_id >= IWLAGN_FIRST_AMPDU_QUEUE &&
  1288. txq_id != trans_pcie->agg_txq[sta_id][tid])) {
  1289. /*
  1290. * FIXME: this is a uCode bug which need to be addressed,
  1291. * log the information and return for now.
  1292. * Since it is can possibly happen very often and in order
  1293. * not to fill the syslog, don't use IWL_ERR or IWL_WARN
  1294. */
  1295. IWL_DEBUG_TX_QUEUES(trans, "Bad queue mapping txq_id %d, "
  1296. "agg_txq[sta_id[tid] %d", txq_id,
  1297. trans_pcie->agg_txq[sta_id][tid]);
  1298. return 1;
  1299. }
  1300. if (txq->q.read_ptr != tfd_num) {
  1301. IWL_DEBUG_TX_REPLY(trans, "[Q %d | AC %d] %d -> %d (%d)\n",
  1302. txq_id, iwl_get_queue_ac(txq), txq->q.read_ptr,
  1303. tfd_num, ssn);
  1304. freed = iwl_tx_queue_reclaim(trans, txq_id, tfd_num, skbs);
  1305. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  1306. (!txq->sched_retry ||
  1307. status != TX_STATUS_FAIL_PASSIVE_NO_RX))
  1308. iwl_wake_queue(trans, txq, "Packets reclaimed");
  1309. }
  1310. return 0;
  1311. }
  1312. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  1313. {
  1314. iowrite8(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1315. }
  1316. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  1317. {
  1318. iowrite32(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1319. }
  1320. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  1321. {
  1322. u32 val = ioread32(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1323. return val;
  1324. }
  1325. static void iwl_trans_pcie_free(struct iwl_trans *trans)
  1326. {
  1327. struct iwl_trans_pcie *trans_pcie =
  1328. IWL_TRANS_GET_PCIE_TRANS(trans);
  1329. iwl_calib_free_results(trans);
  1330. iwl_trans_pcie_tx_free(trans);
  1331. #ifndef CONFIG_IWLWIFI_IDI
  1332. iwl_trans_pcie_rx_free(trans);
  1333. #endif
  1334. if (trans_pcie->irq_requested == true) {
  1335. free_irq(trans->irq, trans);
  1336. iwl_free_isr_ict(trans);
  1337. }
  1338. pci_disable_msi(trans_pcie->pci_dev);
  1339. pci_iounmap(trans_pcie->pci_dev, trans_pcie->hw_base);
  1340. pci_release_regions(trans_pcie->pci_dev);
  1341. pci_disable_device(trans_pcie->pci_dev);
  1342. trans->shrd->trans = NULL;
  1343. kfree(trans);
  1344. }
  1345. #ifdef CONFIG_PM_SLEEP
  1346. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  1347. {
  1348. /*
  1349. * This function is called when system goes into suspend state
  1350. * mac80211 will call iwlagn_mac_stop() from the mac80211 suspend
  1351. * function first but since iwlagn_mac_stop() has no knowledge of
  1352. * who the caller is,
  1353. * it will not call apm_ops.stop() to stop the DMA operation.
  1354. * Calling apm_ops.stop here to make sure we stop the DMA.
  1355. *
  1356. * But of course ... if we have configured WoWLAN then we did other
  1357. * things already :-)
  1358. */
  1359. if (!trans->shrd->wowlan) {
  1360. iwl_apm_stop(trans);
  1361. } else {
  1362. iwl_disable_interrupts(trans);
  1363. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1364. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1365. }
  1366. return 0;
  1367. }
  1368. static int iwl_trans_pcie_resume(struct iwl_trans *trans)
  1369. {
  1370. bool hw_rfkill = false;
  1371. iwl_enable_interrupts(trans);
  1372. if (!(iwl_read32(trans, CSR_GP_CNTRL) &
  1373. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1374. hw_rfkill = true;
  1375. if (hw_rfkill)
  1376. set_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1377. else
  1378. clear_bit(STATUS_RF_KILL_HW, &trans->shrd->status);
  1379. iwl_set_hw_rfkill_state(priv(trans), hw_rfkill);
  1380. return 0;
  1381. }
  1382. #endif /* CONFIG_PM_SLEEP */
  1383. static void iwl_trans_pcie_wake_any_queue(struct iwl_trans *trans,
  1384. enum iwl_rxon_context_id ctx,
  1385. const char *msg)
  1386. {
  1387. u8 ac, txq_id;
  1388. struct iwl_trans_pcie *trans_pcie =
  1389. IWL_TRANS_GET_PCIE_TRANS(trans);
  1390. for (ac = 0; ac < AC_NUM; ac++) {
  1391. txq_id = trans_pcie->ac_to_queue[ctx][ac];
  1392. IWL_DEBUG_TX_QUEUES(trans, "Queue Status: Q[%d] %s\n",
  1393. ac,
  1394. (atomic_read(&trans_pcie->queue_stop_count[ac]) > 0)
  1395. ? "stopped" : "awake");
  1396. iwl_wake_queue(trans, &trans_pcie->txq[txq_id], msg);
  1397. }
  1398. }
  1399. static void iwl_trans_pcie_stop_queue(struct iwl_trans *trans, int txq_id,
  1400. const char *msg)
  1401. {
  1402. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1403. iwl_stop_queue(trans, &trans_pcie->txq[txq_id], msg);
  1404. }
  1405. #define IWL_FLUSH_WAIT_MS 2000
  1406. static int iwl_trans_pcie_wait_tx_queue_empty(struct iwl_trans *trans)
  1407. {
  1408. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1409. struct iwl_tx_queue *txq;
  1410. struct iwl_queue *q;
  1411. int cnt;
  1412. unsigned long now = jiffies;
  1413. int ret = 0;
  1414. /* waiting for all the tx frames complete might take a while */
  1415. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1416. if (cnt == trans->shrd->cmd_queue)
  1417. continue;
  1418. txq = &trans_pcie->txq[cnt];
  1419. q = &txq->q;
  1420. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1421. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1422. msleep(1);
  1423. if (q->read_ptr != q->write_ptr) {
  1424. IWL_ERR(trans, "fail to flush all tx fifo queues\n");
  1425. ret = -ETIMEDOUT;
  1426. break;
  1427. }
  1428. }
  1429. return ret;
  1430. }
  1431. /*
  1432. * On every watchdog tick we check (latest) time stamp. If it does not
  1433. * change during timeout period and queue is not empty we reset firmware.
  1434. */
  1435. static int iwl_trans_pcie_check_stuck_queue(struct iwl_trans *trans, int cnt)
  1436. {
  1437. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1438. struct iwl_tx_queue *txq = &trans_pcie->txq[cnt];
  1439. struct iwl_queue *q = &txq->q;
  1440. unsigned long timeout;
  1441. if (q->read_ptr == q->write_ptr) {
  1442. txq->time_stamp = jiffies;
  1443. return 0;
  1444. }
  1445. timeout = txq->time_stamp +
  1446. msecs_to_jiffies(hw_params(trans).wd_timeout);
  1447. if (time_after(jiffies, timeout)) {
  1448. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", q->id,
  1449. hw_params(trans).wd_timeout);
  1450. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  1451. q->read_ptr, q->write_ptr);
  1452. IWL_ERR(trans, "Current HW read_ptr %d write_ptr %d\n",
  1453. iwl_read_prph(trans, SCD_QUEUE_RDPTR(cnt))
  1454. & (TFD_QUEUE_SIZE_MAX - 1),
  1455. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  1456. return 1;
  1457. }
  1458. return 0;
  1459. }
  1460. static const char *get_fh_string(int cmd)
  1461. {
  1462. switch (cmd) {
  1463. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1464. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1465. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1466. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1467. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1468. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1469. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1470. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1471. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1472. default:
  1473. return "UNKNOWN";
  1474. }
  1475. }
  1476. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display)
  1477. {
  1478. int i;
  1479. #ifdef CONFIG_IWLWIFI_DEBUG
  1480. int pos = 0;
  1481. size_t bufsz = 0;
  1482. #endif
  1483. static const u32 fh_tbl[] = {
  1484. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1485. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1486. FH_RSCSR_CHNL0_WPTR,
  1487. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1488. FH_MEM_RSSR_SHARED_CTRL_REG,
  1489. FH_MEM_RSSR_RX_STATUS_REG,
  1490. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1491. FH_TSSR_TX_STATUS_REG,
  1492. FH_TSSR_TX_ERROR_REG
  1493. };
  1494. #ifdef CONFIG_IWLWIFI_DEBUG
  1495. if (display) {
  1496. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1497. *buf = kmalloc(bufsz, GFP_KERNEL);
  1498. if (!*buf)
  1499. return -ENOMEM;
  1500. pos += scnprintf(*buf + pos, bufsz - pos,
  1501. "FH register values:\n");
  1502. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1503. pos += scnprintf(*buf + pos, bufsz - pos,
  1504. " %34s: 0X%08x\n",
  1505. get_fh_string(fh_tbl[i]),
  1506. iwl_read_direct32(trans, fh_tbl[i]));
  1507. }
  1508. return pos;
  1509. }
  1510. #endif
  1511. IWL_ERR(trans, "FH register values:\n");
  1512. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1513. IWL_ERR(trans, " %34s: 0X%08x\n",
  1514. get_fh_string(fh_tbl[i]),
  1515. iwl_read_direct32(trans, fh_tbl[i]));
  1516. }
  1517. return 0;
  1518. }
  1519. static const char *get_csr_string(int cmd)
  1520. {
  1521. switch (cmd) {
  1522. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1523. IWL_CMD(CSR_INT_COALESCING);
  1524. IWL_CMD(CSR_INT);
  1525. IWL_CMD(CSR_INT_MASK);
  1526. IWL_CMD(CSR_FH_INT_STATUS);
  1527. IWL_CMD(CSR_GPIO_IN);
  1528. IWL_CMD(CSR_RESET);
  1529. IWL_CMD(CSR_GP_CNTRL);
  1530. IWL_CMD(CSR_HW_REV);
  1531. IWL_CMD(CSR_EEPROM_REG);
  1532. IWL_CMD(CSR_EEPROM_GP);
  1533. IWL_CMD(CSR_OTP_GP_REG);
  1534. IWL_CMD(CSR_GIO_REG);
  1535. IWL_CMD(CSR_GP_UCODE_REG);
  1536. IWL_CMD(CSR_GP_DRIVER_REG);
  1537. IWL_CMD(CSR_UCODE_DRV_GP1);
  1538. IWL_CMD(CSR_UCODE_DRV_GP2);
  1539. IWL_CMD(CSR_LED_REG);
  1540. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1541. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1542. IWL_CMD(CSR_ANA_PLL_CFG);
  1543. IWL_CMD(CSR_HW_REV_WA_REG);
  1544. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1545. default:
  1546. return "UNKNOWN";
  1547. }
  1548. }
  1549. void iwl_dump_csr(struct iwl_trans *trans)
  1550. {
  1551. int i;
  1552. static const u32 csr_tbl[] = {
  1553. CSR_HW_IF_CONFIG_REG,
  1554. CSR_INT_COALESCING,
  1555. CSR_INT,
  1556. CSR_INT_MASK,
  1557. CSR_FH_INT_STATUS,
  1558. CSR_GPIO_IN,
  1559. CSR_RESET,
  1560. CSR_GP_CNTRL,
  1561. CSR_HW_REV,
  1562. CSR_EEPROM_REG,
  1563. CSR_EEPROM_GP,
  1564. CSR_OTP_GP_REG,
  1565. CSR_GIO_REG,
  1566. CSR_GP_UCODE_REG,
  1567. CSR_GP_DRIVER_REG,
  1568. CSR_UCODE_DRV_GP1,
  1569. CSR_UCODE_DRV_GP2,
  1570. CSR_LED_REG,
  1571. CSR_DRAM_INT_TBL_REG,
  1572. CSR_GIO_CHICKEN_BITS,
  1573. CSR_ANA_PLL_CFG,
  1574. CSR_HW_REV_WA_REG,
  1575. CSR_DBG_HPET_MEM_REG
  1576. };
  1577. IWL_ERR(trans, "CSR values:\n");
  1578. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1579. "CSR_INT_PERIODIC_REG)\n");
  1580. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1581. IWL_ERR(trans, " %25s: 0X%08x\n",
  1582. get_csr_string(csr_tbl[i]),
  1583. iwl_read32(trans, csr_tbl[i]));
  1584. }
  1585. }
  1586. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1587. /* create and remove of files */
  1588. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1589. if (!debugfs_create_file(#name, mode, parent, trans, \
  1590. &iwl_dbgfs_##name##_ops)) \
  1591. return -ENOMEM; \
  1592. } while (0)
  1593. /* file operation */
  1594. #define DEBUGFS_READ_FUNC(name) \
  1595. static ssize_t iwl_dbgfs_##name##_read(struct file *file, \
  1596. char __user *user_buf, \
  1597. size_t count, loff_t *ppos);
  1598. #define DEBUGFS_WRITE_FUNC(name) \
  1599. static ssize_t iwl_dbgfs_##name##_write(struct file *file, \
  1600. const char __user *user_buf, \
  1601. size_t count, loff_t *ppos);
  1602. static int iwl_dbgfs_open_file_generic(struct inode *inode, struct file *file)
  1603. {
  1604. file->private_data = inode->i_private;
  1605. return 0;
  1606. }
  1607. #define DEBUGFS_READ_FILE_OPS(name) \
  1608. DEBUGFS_READ_FUNC(name); \
  1609. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1610. .read = iwl_dbgfs_##name##_read, \
  1611. .open = iwl_dbgfs_open_file_generic, \
  1612. .llseek = generic_file_llseek, \
  1613. };
  1614. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1615. DEBUGFS_WRITE_FUNC(name); \
  1616. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1617. .write = iwl_dbgfs_##name##_write, \
  1618. .open = iwl_dbgfs_open_file_generic, \
  1619. .llseek = generic_file_llseek, \
  1620. };
  1621. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1622. DEBUGFS_READ_FUNC(name); \
  1623. DEBUGFS_WRITE_FUNC(name); \
  1624. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1625. .write = iwl_dbgfs_##name##_write, \
  1626. .read = iwl_dbgfs_##name##_read, \
  1627. .open = iwl_dbgfs_open_file_generic, \
  1628. .llseek = generic_file_llseek, \
  1629. };
  1630. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1631. char __user *user_buf,
  1632. size_t count, loff_t *ppos)
  1633. {
  1634. struct iwl_trans *trans = file->private_data;
  1635. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1636. struct iwl_tx_queue *txq;
  1637. struct iwl_queue *q;
  1638. char *buf;
  1639. int pos = 0;
  1640. int cnt;
  1641. int ret;
  1642. const size_t bufsz = sizeof(char) * 64 * hw_params(trans).max_txq_num;
  1643. if (!trans_pcie->txq) {
  1644. IWL_ERR(trans, "txq not ready\n");
  1645. return -EAGAIN;
  1646. }
  1647. buf = kzalloc(bufsz, GFP_KERNEL);
  1648. if (!buf)
  1649. return -ENOMEM;
  1650. for (cnt = 0; cnt < hw_params(trans).max_txq_num; cnt++) {
  1651. txq = &trans_pcie->txq[cnt];
  1652. q = &txq->q;
  1653. pos += scnprintf(buf + pos, bufsz - pos,
  1654. "hwq %.2d: read=%u write=%u stop=%d"
  1655. " swq_id=%#.2x (ac %d/hwq %d)\n",
  1656. cnt, q->read_ptr, q->write_ptr,
  1657. !!test_bit(cnt, trans_pcie->queue_stopped),
  1658. txq->swq_id, txq->swq_id & 3,
  1659. (txq->swq_id >> 2) & 0x1f);
  1660. if (cnt >= 4)
  1661. continue;
  1662. /* for the ACs, display the stop count too */
  1663. pos += scnprintf(buf + pos, bufsz - pos,
  1664. " stop-count: %d\n",
  1665. atomic_read(&trans_pcie->queue_stop_count[cnt]));
  1666. }
  1667. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1668. kfree(buf);
  1669. return ret;
  1670. }
  1671. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1672. char __user *user_buf,
  1673. size_t count, loff_t *ppos) {
  1674. struct iwl_trans *trans = file->private_data;
  1675. struct iwl_trans_pcie *trans_pcie =
  1676. IWL_TRANS_GET_PCIE_TRANS(trans);
  1677. struct iwl_rx_queue *rxq = &trans_pcie->rxq;
  1678. char buf[256];
  1679. int pos = 0;
  1680. const size_t bufsz = sizeof(buf);
  1681. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1682. rxq->read);
  1683. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1684. rxq->write);
  1685. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1686. rxq->free_count);
  1687. if (rxq->rb_stts) {
  1688. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1689. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1690. } else {
  1691. pos += scnprintf(buf + pos, bufsz - pos,
  1692. "closed_rb_num: Not Allocated\n");
  1693. }
  1694. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1695. }
  1696. static ssize_t iwl_dbgfs_log_event_read(struct file *file,
  1697. char __user *user_buf,
  1698. size_t count, loff_t *ppos)
  1699. {
  1700. struct iwl_trans *trans = file->private_data;
  1701. char *buf;
  1702. int pos = 0;
  1703. ssize_t ret = -ENOMEM;
  1704. ret = pos = iwl_dump_nic_event_log(trans, true, &buf, true);
  1705. if (buf) {
  1706. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1707. kfree(buf);
  1708. }
  1709. return ret;
  1710. }
  1711. static ssize_t iwl_dbgfs_log_event_write(struct file *file,
  1712. const char __user *user_buf,
  1713. size_t count, loff_t *ppos)
  1714. {
  1715. struct iwl_trans *trans = file->private_data;
  1716. u32 event_log_flag;
  1717. char buf[8];
  1718. int buf_size;
  1719. memset(buf, 0, sizeof(buf));
  1720. buf_size = min(count, sizeof(buf) - 1);
  1721. if (copy_from_user(buf, user_buf, buf_size))
  1722. return -EFAULT;
  1723. if (sscanf(buf, "%d", &event_log_flag) != 1)
  1724. return -EFAULT;
  1725. if (event_log_flag == 1)
  1726. iwl_dump_nic_event_log(trans, true, NULL, false);
  1727. return count;
  1728. }
  1729. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1730. char __user *user_buf,
  1731. size_t count, loff_t *ppos) {
  1732. struct iwl_trans *trans = file->private_data;
  1733. struct iwl_trans_pcie *trans_pcie =
  1734. IWL_TRANS_GET_PCIE_TRANS(trans);
  1735. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1736. int pos = 0;
  1737. char *buf;
  1738. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1739. ssize_t ret;
  1740. buf = kzalloc(bufsz, GFP_KERNEL);
  1741. if (!buf) {
  1742. IWL_ERR(trans, "Can not allocate Buffer\n");
  1743. return -ENOMEM;
  1744. }
  1745. pos += scnprintf(buf + pos, bufsz - pos,
  1746. "Interrupt Statistics Report:\n");
  1747. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1748. isr_stats->hw);
  1749. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1750. isr_stats->sw);
  1751. if (isr_stats->sw || isr_stats->hw) {
  1752. pos += scnprintf(buf + pos, bufsz - pos,
  1753. "\tLast Restarting Code: 0x%X\n",
  1754. isr_stats->err_code);
  1755. }
  1756. #ifdef CONFIG_IWLWIFI_DEBUG
  1757. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1758. isr_stats->sch);
  1759. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1760. isr_stats->alive);
  1761. #endif
  1762. pos += scnprintf(buf + pos, bufsz - pos,
  1763. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1764. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1765. isr_stats->ctkill);
  1766. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1767. isr_stats->wakeup);
  1768. pos += scnprintf(buf + pos, bufsz - pos,
  1769. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1770. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1771. isr_stats->tx);
  1772. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1773. isr_stats->unhandled);
  1774. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1775. kfree(buf);
  1776. return ret;
  1777. }
  1778. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1779. const char __user *user_buf,
  1780. size_t count, loff_t *ppos)
  1781. {
  1782. struct iwl_trans *trans = file->private_data;
  1783. struct iwl_trans_pcie *trans_pcie =
  1784. IWL_TRANS_GET_PCIE_TRANS(trans);
  1785. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1786. char buf[8];
  1787. int buf_size;
  1788. u32 reset_flag;
  1789. memset(buf, 0, sizeof(buf));
  1790. buf_size = min(count, sizeof(buf) - 1);
  1791. if (copy_from_user(buf, user_buf, buf_size))
  1792. return -EFAULT;
  1793. if (sscanf(buf, "%x", &reset_flag) != 1)
  1794. return -EFAULT;
  1795. if (reset_flag == 0)
  1796. memset(isr_stats, 0, sizeof(*isr_stats));
  1797. return count;
  1798. }
  1799. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1800. const char __user *user_buf,
  1801. size_t count, loff_t *ppos)
  1802. {
  1803. struct iwl_trans *trans = file->private_data;
  1804. char buf[8];
  1805. int buf_size;
  1806. int csr;
  1807. memset(buf, 0, sizeof(buf));
  1808. buf_size = min(count, sizeof(buf) - 1);
  1809. if (copy_from_user(buf, user_buf, buf_size))
  1810. return -EFAULT;
  1811. if (sscanf(buf, "%d", &csr) != 1)
  1812. return -EFAULT;
  1813. iwl_dump_csr(trans);
  1814. return count;
  1815. }
  1816. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1817. char __user *user_buf,
  1818. size_t count, loff_t *ppos)
  1819. {
  1820. struct iwl_trans *trans = file->private_data;
  1821. char *buf;
  1822. int pos = 0;
  1823. ssize_t ret = -EFAULT;
  1824. ret = pos = iwl_dump_fh(trans, &buf, true);
  1825. if (buf) {
  1826. ret = simple_read_from_buffer(user_buf,
  1827. count, ppos, buf, pos);
  1828. kfree(buf);
  1829. }
  1830. return ret;
  1831. }
  1832. DEBUGFS_READ_WRITE_FILE_OPS(log_event);
  1833. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1834. DEBUGFS_READ_FILE_OPS(fh_reg);
  1835. DEBUGFS_READ_FILE_OPS(rx_queue);
  1836. DEBUGFS_READ_FILE_OPS(tx_queue);
  1837. DEBUGFS_WRITE_FILE_OPS(csr);
  1838. /*
  1839. * Create the debugfs files and directories
  1840. *
  1841. */
  1842. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1843. struct dentry *dir)
  1844. {
  1845. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1846. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1847. DEBUGFS_ADD_FILE(log_event, dir, S_IWUSR | S_IRUSR);
  1848. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1849. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1850. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1851. return 0;
  1852. }
  1853. #else
  1854. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1855. struct dentry *dir)
  1856. { return 0; }
  1857. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1858. const struct iwl_trans_ops trans_ops_pcie = {
  1859. .start_hw = iwl_trans_pcie_start_hw,
  1860. .stop_hw = iwl_trans_pcie_stop_hw,
  1861. .fw_alive = iwl_trans_pcie_fw_alive,
  1862. .start_fw = iwl_trans_pcie_start_fw,
  1863. .stop_device = iwl_trans_pcie_stop_device,
  1864. .wake_any_queue = iwl_trans_pcie_wake_any_queue,
  1865. .send_cmd = iwl_trans_pcie_send_cmd,
  1866. .tx = iwl_trans_pcie_tx,
  1867. .reclaim = iwl_trans_pcie_reclaim,
  1868. .tx_agg_disable = iwl_trans_pcie_tx_agg_disable,
  1869. .tx_agg_alloc = iwl_trans_pcie_tx_agg_alloc,
  1870. .tx_agg_setup = iwl_trans_pcie_tx_agg_setup,
  1871. .free = iwl_trans_pcie_free,
  1872. .stop_queue = iwl_trans_pcie_stop_queue,
  1873. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1874. .wait_tx_queue_empty = iwl_trans_pcie_wait_tx_queue_empty,
  1875. .check_stuck_queue = iwl_trans_pcie_check_stuck_queue,
  1876. #ifdef CONFIG_PM_SLEEP
  1877. .suspend = iwl_trans_pcie_suspend,
  1878. .resume = iwl_trans_pcie_resume,
  1879. #endif
  1880. .write8 = iwl_trans_pcie_write8,
  1881. .write32 = iwl_trans_pcie_write32,
  1882. .read32 = iwl_trans_pcie_read32,
  1883. };
  1884. struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd,
  1885. struct pci_dev *pdev,
  1886. const struct pci_device_id *ent)
  1887. {
  1888. struct iwl_trans_pcie *trans_pcie;
  1889. struct iwl_trans *trans;
  1890. u16 pci_cmd;
  1891. int err;
  1892. trans = kzalloc(sizeof(struct iwl_trans) +
  1893. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1894. if (WARN_ON(!trans))
  1895. return NULL;
  1896. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1897. trans->ops = &trans_ops_pcie;
  1898. trans->shrd = shrd;
  1899. trans_pcie->trans = trans;
  1900. spin_lock_init(&trans->hcmd_lock);
  1901. /* W/A - seems to solve weird behavior. We need to remove this if we
  1902. * don't want to stay in L1 all the time. This wastes a lot of power */
  1903. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  1904. PCIE_LINK_STATE_CLKPM);
  1905. if (pci_enable_device(pdev)) {
  1906. err = -ENODEV;
  1907. goto out_no_pci;
  1908. }
  1909. pci_set_master(pdev);
  1910. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1911. if (!err)
  1912. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1913. if (err) {
  1914. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1915. if (!err)
  1916. err = pci_set_consistent_dma_mask(pdev,
  1917. DMA_BIT_MASK(32));
  1918. /* both attempts failed: */
  1919. if (err) {
  1920. dev_printk(KERN_ERR, &pdev->dev,
  1921. "No suitable DMA available.\n");
  1922. goto out_pci_disable_device;
  1923. }
  1924. }
  1925. err = pci_request_regions(pdev, DRV_NAME);
  1926. if (err) {
  1927. dev_printk(KERN_ERR, &pdev->dev, "pci_request_regions failed");
  1928. goto out_pci_disable_device;
  1929. }
  1930. trans_pcie->hw_base = pci_iomap(pdev, 0, 0);
  1931. if (!trans_pcie->hw_base) {
  1932. dev_printk(KERN_ERR, &pdev->dev, "pci_iomap failed");
  1933. err = -ENODEV;
  1934. goto out_pci_release_regions;
  1935. }
  1936. dev_printk(KERN_INFO, &pdev->dev,
  1937. "pci_resource_len = 0x%08llx\n",
  1938. (unsigned long long) pci_resource_len(pdev, 0));
  1939. dev_printk(KERN_INFO, &pdev->dev,
  1940. "pci_resource_base = %p\n", trans_pcie->hw_base);
  1941. dev_printk(KERN_INFO, &pdev->dev,
  1942. "HW Revision ID = 0x%X\n", pdev->revision);
  1943. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1944. * PCI Tx retries from interfering with C3 CPU state */
  1945. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1946. err = pci_enable_msi(pdev);
  1947. if (err)
  1948. dev_printk(KERN_ERR, &pdev->dev,
  1949. "pci_enable_msi failed(0X%x)", err);
  1950. trans->dev = &pdev->dev;
  1951. trans->irq = pdev->irq;
  1952. trans_pcie->pci_dev = pdev;
  1953. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  1954. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  1955. /* TODO: Move this away, not needed if not MSI */
  1956. /* enable rfkill interrupt: hw bug w/a */
  1957. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1958. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1959. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1960. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1961. }
  1962. return trans;
  1963. out_pci_release_regions:
  1964. pci_release_regions(pdev);
  1965. out_pci_disable_device:
  1966. pci_disable_device(pdev);
  1967. out_no_pci:
  1968. kfree(trans);
  1969. return NULL;
  1970. }