iwl-trans-pcie.c 60 KB

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