trans.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/pci.h>
  66. #include <linux/pci-aspm.h>
  67. #include <linux/interrupt.h>
  68. #include <linux/debugfs.h>
  69. #include <linux/sched.h>
  70. #include <linux/bitops.h>
  71. #include <linux/gfp.h>
  72. #include <linux/vmalloc.h>
  73. #include "iwl-drv.h"
  74. #include "iwl-trans.h"
  75. #include "iwl-csr.h"
  76. #include "iwl-prph.h"
  77. #include "iwl-agn-hw.h"
  78. #include "iwl-fw-error-dump.h"
  79. #include "internal.h"
  80. static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans)
  81. {
  82. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  83. if (!trans_pcie->fw_mon_page)
  84. return;
  85. dma_unmap_page(trans->dev, trans_pcie->fw_mon_phys,
  86. trans_pcie->fw_mon_size, DMA_FROM_DEVICE);
  87. __free_pages(trans_pcie->fw_mon_page,
  88. get_order(trans_pcie->fw_mon_size));
  89. trans_pcie->fw_mon_page = NULL;
  90. trans_pcie->fw_mon_phys = 0;
  91. trans_pcie->fw_mon_size = 0;
  92. }
  93. static void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans)
  94. {
  95. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  96. struct page *page;
  97. dma_addr_t phys;
  98. u32 size;
  99. u8 power;
  100. if (trans_pcie->fw_mon_page) {
  101. dma_sync_single_for_device(trans->dev, trans_pcie->fw_mon_phys,
  102. trans_pcie->fw_mon_size,
  103. DMA_FROM_DEVICE);
  104. return;
  105. }
  106. phys = 0;
  107. for (power = 26; power >= 11; power--) {
  108. int order;
  109. size = BIT(power);
  110. order = get_order(size);
  111. page = alloc_pages(__GFP_COMP | __GFP_NOWARN | __GFP_ZERO,
  112. order);
  113. if (!page)
  114. continue;
  115. phys = dma_map_page(trans->dev, page, 0, PAGE_SIZE << order,
  116. DMA_FROM_DEVICE);
  117. if (dma_mapping_error(trans->dev, phys)) {
  118. __free_pages(page, order);
  119. continue;
  120. }
  121. IWL_INFO(trans,
  122. "Allocated 0x%08x bytes (order %d) for firmware monitor.\n",
  123. size, order);
  124. break;
  125. }
  126. if (WARN_ON_ONCE(!page))
  127. return;
  128. trans_pcie->fw_mon_page = page;
  129. trans_pcie->fw_mon_phys = phys;
  130. trans_pcie->fw_mon_size = size;
  131. }
  132. static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
  133. {
  134. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  135. ((reg & 0x0000ffff) | (2 << 28)));
  136. return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
  137. }
  138. static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
  139. {
  140. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
  141. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  142. ((reg & 0x0000ffff) | (3 << 28)));
  143. }
  144. static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
  145. {
  146. if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
  147. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  148. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  149. ~APMG_PS_CTRL_MSK_PWR_SRC);
  150. else
  151. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  152. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  153. ~APMG_PS_CTRL_MSK_PWR_SRC);
  154. }
  155. /* PCI registers */
  156. #define PCI_CFG_RETRY_TIMEOUT 0x041
  157. static void iwl_pcie_apm_config(struct iwl_trans *trans)
  158. {
  159. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  160. u16 lctl;
  161. u16 cap;
  162. /*
  163. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  164. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  165. * If so (likely), disable L0S, so device moves directly L0->L1;
  166. * costs negligible amount of power savings.
  167. * If not (unlikely), enable L0S, so there is at least some
  168. * power savings, even without L1.
  169. */
  170. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
  171. if (lctl & PCI_EXP_LNKCTL_ASPM_L1)
  172. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  173. else
  174. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  175. trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
  176. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap);
  177. trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN;
  178. dev_info(trans->dev, "L1 %sabled - LTR %sabled\n",
  179. (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis",
  180. trans->ltr_enabled ? "En" : "Dis");
  181. }
  182. /*
  183. * Start up NIC's basic functionality after it has been reset
  184. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  185. * NOTE: This does not load uCode nor start the embedded processor
  186. */
  187. static int iwl_pcie_apm_init(struct iwl_trans *trans)
  188. {
  189. int ret = 0;
  190. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  191. /*
  192. * Use "set_bit" below rather than "write", to preserve any hardware
  193. * bits already set by default after reset.
  194. */
  195. /* Disable L0S exit timer (platform NMI Work/Around) */
  196. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  197. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  198. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  199. /*
  200. * Disable L0s without affecting L1;
  201. * don't wait for ICH L0s (ICH bug W/A)
  202. */
  203. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  204. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  205. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  206. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  207. /*
  208. * Enable HAP INTA (interrupt from management bus) to
  209. * wake device's PCI Express link L1a -> L0s
  210. */
  211. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  212. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  213. iwl_pcie_apm_config(trans);
  214. /* Configure analog phase-lock-loop before activating to D0A */
  215. if (trans->cfg->base_params->pll_cfg_val)
  216. iwl_set_bit(trans, CSR_ANA_PLL_CFG,
  217. trans->cfg->base_params->pll_cfg_val);
  218. /*
  219. * Set "initialization complete" bit to move adapter from
  220. * D0U* --> D0A* (powered-up active) state.
  221. */
  222. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  223. /*
  224. * Wait for clock stabilization; once stabilized, access to
  225. * device-internal resources is supported, e.g. iwl_write_prph()
  226. * and accesses to uCode SRAM.
  227. */
  228. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  229. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  230. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  231. if (ret < 0) {
  232. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  233. goto out;
  234. }
  235. if (trans->cfg->host_interrupt_operation_mode) {
  236. /*
  237. * This is a bit of an abuse - This is needed for 7260 / 3160
  238. * only check host_interrupt_operation_mode even if this is
  239. * not related to host_interrupt_operation_mode.
  240. *
  241. * Enable the oscillator to count wake up time for L1 exit. This
  242. * consumes slightly more power (100uA) - but allows to be sure
  243. * that we wake up from L1 on time.
  244. *
  245. * This looks weird: read twice the same register, discard the
  246. * value, set a bit, and yet again, read that same register
  247. * just to discard the value. But that's the way the hardware
  248. * seems to like it.
  249. */
  250. iwl_read_prph(trans, OSC_CLK);
  251. iwl_read_prph(trans, OSC_CLK);
  252. iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
  253. iwl_read_prph(trans, OSC_CLK);
  254. iwl_read_prph(trans, OSC_CLK);
  255. }
  256. /*
  257. * Enable DMA clock and wait for it to stabilize.
  258. *
  259. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
  260. * bits do not disable clocks. This preserves any hardware
  261. * bits already set by default in "CLK_CTRL_REG" after reset.
  262. */
  263. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000) {
  264. iwl_write_prph(trans, APMG_CLK_EN_REG,
  265. APMG_CLK_VAL_DMA_CLK_RQT);
  266. udelay(20);
  267. /* Disable L1-Active */
  268. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  269. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  270. /* Clear the interrupt in APMG if the NIC is in RFKILL */
  271. iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
  272. APMG_RTC_INT_STT_RFKILL);
  273. }
  274. set_bit(STATUS_DEVICE_ENABLED, &trans->status);
  275. out:
  276. return ret;
  277. }
  278. /*
  279. * Enable LP XTAL to avoid HW bug where device may consume much power if
  280. * FW is not loaded after device reset. LP XTAL is disabled by default
  281. * after device HW reset. Do it only if XTAL is fed by internal source.
  282. * Configure device's "persistence" mode to avoid resetting XTAL again when
  283. * SHRD_HW_RST occurs in S3.
  284. */
  285. static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
  286. {
  287. int ret;
  288. u32 apmg_gp1_reg;
  289. u32 apmg_xtal_cfg_reg;
  290. u32 dl_cfg_reg;
  291. /* Force XTAL ON */
  292. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  293. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  294. /* Reset entire device - do controller reset (results in SHRD_HW_RST) */
  295. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  296. udelay(10);
  297. /*
  298. * Set "initialization complete" bit to move adapter from
  299. * D0U* --> D0A* (powered-up active) state.
  300. */
  301. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  302. /*
  303. * Wait for clock stabilization; once stabilized, access to
  304. * device-internal resources is possible.
  305. */
  306. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  307. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  308. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  309. 25000);
  310. if (WARN_ON(ret < 0)) {
  311. IWL_ERR(trans, "Access time out - failed to enable LP XTAL\n");
  312. /* Release XTAL ON request */
  313. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  314. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  315. return;
  316. }
  317. /*
  318. * Clear "disable persistence" to avoid LP XTAL resetting when
  319. * SHRD_HW_RST is applied in S3.
  320. */
  321. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  322. APMG_PCIDEV_STT_VAL_PERSIST_DIS);
  323. /*
  324. * Force APMG XTAL to be active to prevent its disabling by HW
  325. * caused by APMG idle state.
  326. */
  327. apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
  328. SHR_APMG_XTAL_CFG_REG);
  329. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  330. apmg_xtal_cfg_reg |
  331. SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  332. /*
  333. * Reset entire device again - do controller reset (results in
  334. * SHRD_HW_RST). Turn MAC off before proceeding.
  335. */
  336. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  337. udelay(10);
  338. /* Enable LP XTAL by indirect access through CSR */
  339. apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
  340. iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
  341. SHR_APMG_GP1_WF_XTAL_LP_EN |
  342. SHR_APMG_GP1_CHICKEN_BIT_SELECT);
  343. /* Clear delay line clock power up */
  344. dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
  345. iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
  346. ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
  347. /*
  348. * Enable persistence mode to avoid LP XTAL resetting when
  349. * SHRD_HW_RST is applied in S3.
  350. */
  351. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  352. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  353. /*
  354. * Clear "initialization complete" bit to move adapter from
  355. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  356. */
  357. iwl_clear_bit(trans, CSR_GP_CNTRL,
  358. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  359. /* Activates XTAL resources monitor */
  360. __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG,
  361. CSR_MONITOR_XTAL_RESOURCES);
  362. /* Release XTAL ON request */
  363. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  364. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  365. udelay(10);
  366. /* Release APMG XTAL */
  367. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  368. apmg_xtal_cfg_reg &
  369. ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  370. }
  371. static int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
  372. {
  373. int ret = 0;
  374. /* stop device's busmaster DMA activity */
  375. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  376. ret = iwl_poll_bit(trans, CSR_RESET,
  377. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  378. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  379. if (ret < 0)
  380. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  381. IWL_DEBUG_INFO(trans, "stop master\n");
  382. return ret;
  383. }
  384. static void iwl_pcie_apm_stop(struct iwl_trans *trans)
  385. {
  386. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  387. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  388. /* Stop device's DMA activity */
  389. iwl_pcie_apm_stop_master(trans);
  390. if (trans->cfg->lp_xtal_workaround) {
  391. iwl_pcie_apm_lp_xtal_enable(trans);
  392. return;
  393. }
  394. /* Reset the entire device */
  395. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  396. udelay(10);
  397. /*
  398. * Clear "initialization complete" bit to move adapter from
  399. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  400. */
  401. iwl_clear_bit(trans, CSR_GP_CNTRL,
  402. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  403. }
  404. static int iwl_pcie_nic_init(struct iwl_trans *trans)
  405. {
  406. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  407. /* nic_init */
  408. spin_lock(&trans_pcie->irq_lock);
  409. iwl_pcie_apm_init(trans);
  410. spin_unlock(&trans_pcie->irq_lock);
  411. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  412. iwl_pcie_set_pwr(trans, false);
  413. iwl_op_mode_nic_config(trans->op_mode);
  414. /* Allocate the RX queue, or reset if it is already allocated */
  415. iwl_pcie_rx_init(trans);
  416. /* Allocate or reset and init all Tx and Command queues */
  417. if (iwl_pcie_tx_init(trans))
  418. return -ENOMEM;
  419. if (trans->cfg->base_params->shadow_reg_enable) {
  420. /* enable shadow regs in HW */
  421. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  422. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  423. }
  424. return 0;
  425. }
  426. #define HW_READY_TIMEOUT (50)
  427. /* Note: returns poll_bit return value, which is >= 0 if success */
  428. static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
  429. {
  430. int ret;
  431. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  432. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  433. /* See if we got it */
  434. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  435. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  436. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  437. HW_READY_TIMEOUT);
  438. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  439. return ret;
  440. }
  441. /* Note: returns standard 0/-ERROR code */
  442. static int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
  443. {
  444. int ret;
  445. int t = 0;
  446. int iter;
  447. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  448. ret = iwl_pcie_set_hw_ready(trans);
  449. /* If the card is ready, exit 0 */
  450. if (ret >= 0)
  451. return 0;
  452. for (iter = 0; iter < 10; iter++) {
  453. /* If HW is not ready, prepare the conditions to check again */
  454. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  455. CSR_HW_IF_CONFIG_REG_PREPARE);
  456. do {
  457. ret = iwl_pcie_set_hw_ready(trans);
  458. if (ret >= 0)
  459. return 0;
  460. usleep_range(200, 1000);
  461. t += 200;
  462. } while (t < 150000);
  463. msleep(25);
  464. }
  465. IWL_ERR(trans, "Couldn't prepare the card\n");
  466. return ret;
  467. }
  468. /*
  469. * ucode
  470. */
  471. static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans, u32 dst_addr,
  472. dma_addr_t phy_addr, u32 byte_cnt)
  473. {
  474. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  475. int ret;
  476. trans_pcie->ucode_write_complete = false;
  477. iwl_write_direct32(trans,
  478. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  479. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  480. iwl_write_direct32(trans,
  481. FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  482. dst_addr);
  483. iwl_write_direct32(trans,
  484. FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  485. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  486. iwl_write_direct32(trans,
  487. FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  488. (iwl_get_dma_hi_addr(phy_addr)
  489. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  490. iwl_write_direct32(trans,
  491. FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  492. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM |
  493. 1 << FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX |
  494. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  495. iwl_write_direct32(trans,
  496. FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  497. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  498. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  499. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  500. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  501. trans_pcie->ucode_write_complete, 5 * HZ);
  502. if (!ret) {
  503. IWL_ERR(trans, "Failed to load firmware chunk!\n");
  504. return -ETIMEDOUT;
  505. }
  506. return 0;
  507. }
  508. static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
  509. const struct fw_desc *section)
  510. {
  511. u8 *v_addr;
  512. dma_addr_t p_addr;
  513. u32 offset, chunk_sz = section->len;
  514. int ret = 0;
  515. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  516. section_num);
  517. v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
  518. GFP_KERNEL | __GFP_NOWARN);
  519. if (!v_addr) {
  520. IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
  521. chunk_sz = PAGE_SIZE;
  522. v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
  523. &p_addr, GFP_KERNEL);
  524. if (!v_addr)
  525. return -ENOMEM;
  526. }
  527. for (offset = 0; offset < section->len; offset += chunk_sz) {
  528. u32 copy_size;
  529. copy_size = min_t(u32, chunk_sz, section->len - offset);
  530. memcpy(v_addr, (u8 *)section->data + offset, copy_size);
  531. ret = iwl_pcie_load_firmware_chunk(trans,
  532. section->offset + offset,
  533. p_addr, copy_size);
  534. if (ret) {
  535. IWL_ERR(trans,
  536. "Could not load the [%d] uCode section\n",
  537. section_num);
  538. break;
  539. }
  540. }
  541. dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
  542. return ret;
  543. }
  544. static int iwl_pcie_load_cpu_secured_sections(struct iwl_trans *trans,
  545. const struct fw_img *image,
  546. int cpu,
  547. int *first_ucode_section)
  548. {
  549. int shift_param;
  550. int i, ret = 0;
  551. u32 last_read_idx = 0;
  552. if (cpu == 1) {
  553. shift_param = 0;
  554. *first_ucode_section = 0;
  555. } else {
  556. shift_param = 16;
  557. (*first_ucode_section)++;
  558. }
  559. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  560. last_read_idx = i;
  561. if (!image->sec[i].data ||
  562. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION) {
  563. IWL_DEBUG_FW(trans,
  564. "Break since Data not valid or Empty section, sec = %d\n",
  565. i);
  566. break;
  567. }
  568. if (i == (*first_ucode_section) + 1)
  569. /* set CPU to started */
  570. iwl_set_bits_prph(trans,
  571. CSR_UCODE_LOAD_STATUS_ADDR,
  572. LMPM_CPU_HDRS_LOADING_COMPLETED
  573. << shift_param);
  574. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  575. if (ret)
  576. return ret;
  577. }
  578. /* image loading complete */
  579. iwl_set_bits_prph(trans,
  580. CSR_UCODE_LOAD_STATUS_ADDR,
  581. LMPM_CPU_UCODE_LOADING_COMPLETED << shift_param);
  582. *first_ucode_section = last_read_idx;
  583. return 0;
  584. }
  585. static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
  586. const struct fw_img *image,
  587. int cpu,
  588. int *first_ucode_section)
  589. {
  590. int shift_param;
  591. int i, ret = 0;
  592. u32 last_read_idx = 0;
  593. if (cpu == 1) {
  594. shift_param = 0;
  595. *first_ucode_section = 0;
  596. } else {
  597. shift_param = 16;
  598. (*first_ucode_section)++;
  599. }
  600. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  601. last_read_idx = i;
  602. if (!image->sec[i].data ||
  603. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION) {
  604. IWL_DEBUG_FW(trans,
  605. "Break since Data not valid or Empty section, sec = %d\n",
  606. i);
  607. break;
  608. }
  609. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  610. if (ret)
  611. return ret;
  612. }
  613. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  614. iwl_set_bits_prph(trans,
  615. CSR_UCODE_LOAD_STATUS_ADDR,
  616. (LMPM_CPU_UCODE_LOADING_COMPLETED |
  617. LMPM_CPU_HDRS_LOADING_COMPLETED |
  618. LMPM_CPU_UCODE_LOADING_STARTED) <<
  619. shift_param);
  620. *first_ucode_section = last_read_idx;
  621. return 0;
  622. }
  623. static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
  624. const struct fw_img *image)
  625. {
  626. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  627. int ret = 0;
  628. int first_ucode_section;
  629. IWL_DEBUG_FW(trans,
  630. "working with %s CPU\n",
  631. image->is_dual_cpus ? "Dual" : "Single");
  632. /* configure the ucode to be ready to get the secured image */
  633. if (iwl_has_secure_boot(trans->hw_rev, trans->cfg->device_family)) {
  634. /* set secure boot inspector addresses */
  635. iwl_write_prph(trans,
  636. LMPM_SECURE_INSPECTOR_CODE_ADDR,
  637. LMPM_SECURE_INSPECTOR_CODE_MEM_SPACE);
  638. iwl_write_prph(trans,
  639. LMPM_SECURE_INSPECTOR_DATA_ADDR,
  640. LMPM_SECURE_INSPECTOR_DATA_MEM_SPACE);
  641. /* set CPU1 header address */
  642. iwl_write_prph(trans,
  643. LMPM_SECURE_UCODE_LOAD_CPU1_HDR_ADDR,
  644. LMPM_SECURE_CPU1_HDR_MEM_SPACE);
  645. /* load to FW the binary Secured sections of CPU1 */
  646. ret = iwl_pcie_load_cpu_secured_sections(trans, image, 1,
  647. &first_ucode_section);
  648. if (ret)
  649. return ret;
  650. } else {
  651. /* load to FW the binary Non secured sections of CPU1 */
  652. ret = iwl_pcie_load_cpu_sections(trans, image, 1,
  653. &first_ucode_section);
  654. if (ret)
  655. return ret;
  656. }
  657. if (image->is_dual_cpus) {
  658. /* set CPU2 header address */
  659. iwl_write_prph(trans,
  660. LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
  661. LMPM_SECURE_CPU2_HDR_MEM_SPACE);
  662. /* load to FW the binary sections of CPU2 */
  663. if (iwl_has_secure_boot(trans->hw_rev,
  664. trans->cfg->device_family))
  665. ret = iwl_pcie_load_cpu_secured_sections(
  666. trans, image, 2,
  667. &first_ucode_section);
  668. else
  669. ret = iwl_pcie_load_cpu_sections(trans, image, 2,
  670. &first_ucode_section);
  671. if (ret)
  672. return ret;
  673. }
  674. /* supported for 7000 only for the moment */
  675. if (iwlwifi_mod_params.fw_monitor &&
  676. trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  677. iwl_pcie_alloc_fw_monitor(trans);
  678. if (trans_pcie->fw_mon_size) {
  679. iwl_write_prph(trans, MON_BUFF_BASE_ADDR,
  680. trans_pcie->fw_mon_phys >> 4);
  681. iwl_write_prph(trans, MON_BUFF_END_ADDR,
  682. (trans_pcie->fw_mon_phys +
  683. trans_pcie->fw_mon_size) >> 4);
  684. }
  685. }
  686. /* release CPU reset */
  687. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  688. iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
  689. else
  690. iwl_write32(trans, CSR_RESET, 0);
  691. if (iwl_has_secure_boot(trans->hw_rev, trans->cfg->device_family)) {
  692. /* wait for image verification to complete */
  693. ret = iwl_poll_prph_bit(trans,
  694. LMPM_SECURE_BOOT_CPU1_STATUS_ADDR,
  695. LMPM_SECURE_BOOT_STATUS_SUCCESS,
  696. LMPM_SECURE_BOOT_STATUS_SUCCESS,
  697. LMPM_SECURE_TIME_OUT);
  698. if (ret < 0) {
  699. IWL_ERR(trans, "Time out on secure boot process\n");
  700. return ret;
  701. }
  702. }
  703. return 0;
  704. }
  705. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  706. const struct fw_img *fw, bool run_in_rfkill)
  707. {
  708. int ret;
  709. bool hw_rfkill;
  710. /* This may fail if AMT took ownership of the device */
  711. if (iwl_pcie_prepare_card_hw(trans)) {
  712. IWL_WARN(trans, "Exit HW not ready\n");
  713. return -EIO;
  714. }
  715. iwl_enable_rfkill_int(trans);
  716. /* If platform's RF_KILL switch is NOT set to KILL */
  717. hw_rfkill = iwl_is_rfkill_set(trans);
  718. if (hw_rfkill)
  719. set_bit(STATUS_RFKILL, &trans->status);
  720. else
  721. clear_bit(STATUS_RFKILL, &trans->status);
  722. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  723. if (hw_rfkill && !run_in_rfkill)
  724. return -ERFKILL;
  725. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  726. ret = iwl_pcie_nic_init(trans);
  727. if (ret) {
  728. IWL_ERR(trans, "Unable to init nic\n");
  729. return ret;
  730. }
  731. /* make sure rfkill handshake bits are cleared */
  732. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  733. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  734. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  735. /* clear (again), then enable host interrupts */
  736. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  737. iwl_enable_interrupts(trans);
  738. /* really make sure rfkill handshake bits are cleared */
  739. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  740. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  741. /* Load the given image to the HW */
  742. return iwl_pcie_load_given_ucode(trans, fw);
  743. }
  744. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  745. {
  746. iwl_pcie_reset_ict(trans);
  747. iwl_pcie_tx_start(trans, scd_addr);
  748. }
  749. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans)
  750. {
  751. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  752. bool hw_rfkill, was_hw_rfkill;
  753. was_hw_rfkill = iwl_is_rfkill_set(trans);
  754. /* tell the device to stop sending interrupts */
  755. spin_lock(&trans_pcie->irq_lock);
  756. iwl_disable_interrupts(trans);
  757. spin_unlock(&trans_pcie->irq_lock);
  758. /* device going down, Stop using ICT table */
  759. iwl_pcie_disable_ict(trans);
  760. /*
  761. * If a HW restart happens during firmware loading,
  762. * then the firmware loading might call this function
  763. * and later it might be called again due to the
  764. * restart. So don't process again if the device is
  765. * already dead.
  766. */
  767. if (test_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
  768. iwl_pcie_tx_stop(trans);
  769. iwl_pcie_rx_stop(trans);
  770. /* Power-down device's busmaster DMA clocks */
  771. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  772. APMG_CLK_VAL_DMA_CLK_RQT);
  773. udelay(5);
  774. }
  775. /* Make sure (redundant) we've released our request to stay awake */
  776. iwl_clear_bit(trans, CSR_GP_CNTRL,
  777. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  778. /* Stop the device, and put it in low power state */
  779. iwl_pcie_apm_stop(trans);
  780. /* Upon stop, the APM issues an interrupt if HW RF kill is set.
  781. * Clean again the interrupt here
  782. */
  783. spin_lock(&trans_pcie->irq_lock);
  784. iwl_disable_interrupts(trans);
  785. spin_unlock(&trans_pcie->irq_lock);
  786. /* stop and reset the on-board processor */
  787. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  788. /* clear all status bits */
  789. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  790. clear_bit(STATUS_INT_ENABLED, &trans->status);
  791. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  792. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  793. clear_bit(STATUS_RFKILL, &trans->status);
  794. /*
  795. * Even if we stop the HW, we still want the RF kill
  796. * interrupt
  797. */
  798. iwl_enable_rfkill_int(trans);
  799. /*
  800. * Check again since the RF kill state may have changed while
  801. * all the interrupts were disabled, in this case we couldn't
  802. * receive the RF kill interrupt and update the state in the
  803. * op_mode.
  804. * Don't call the op_mode if the rkfill state hasn't changed.
  805. * This allows the op_mode to call stop_device from the rfkill
  806. * notification without endless recursion. Under very rare
  807. * circumstances, we might have a small recursion if the rfkill
  808. * state changed exactly now while we were called from stop_device.
  809. * This is very unlikely but can happen and is supported.
  810. */
  811. hw_rfkill = iwl_is_rfkill_set(trans);
  812. if (hw_rfkill)
  813. set_bit(STATUS_RFKILL, &trans->status);
  814. else
  815. clear_bit(STATUS_RFKILL, &trans->status);
  816. if (hw_rfkill != was_hw_rfkill)
  817. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  818. }
  819. void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state)
  820. {
  821. if (iwl_op_mode_hw_rf_kill(trans->op_mode, state))
  822. iwl_trans_pcie_stop_device(trans);
  823. }
  824. static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test)
  825. {
  826. iwl_disable_interrupts(trans);
  827. /*
  828. * in testing mode, the host stays awake and the
  829. * hardware won't be reset (not even partially)
  830. */
  831. if (test)
  832. return;
  833. iwl_pcie_disable_ict(trans);
  834. iwl_clear_bit(trans, CSR_GP_CNTRL,
  835. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  836. iwl_clear_bit(trans, CSR_GP_CNTRL,
  837. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  838. /*
  839. * reset TX queues -- some of their registers reset during S3
  840. * so if we don't reset everything here the D3 image would try
  841. * to execute some invalid memory upon resume
  842. */
  843. iwl_trans_pcie_tx_reset(trans);
  844. iwl_pcie_set_pwr(trans, true);
  845. }
  846. static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
  847. enum iwl_d3_status *status,
  848. bool test)
  849. {
  850. u32 val;
  851. int ret;
  852. if (test) {
  853. iwl_enable_interrupts(trans);
  854. *status = IWL_D3_STATUS_ALIVE;
  855. return 0;
  856. }
  857. /*
  858. * Also enables interrupts - none will happen as the device doesn't
  859. * know we're waking it up, only when the opmode actually tells it
  860. * after this call.
  861. */
  862. iwl_pcie_reset_ict(trans);
  863. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  864. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  865. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  866. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  867. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  868. 25000);
  869. if (ret < 0) {
  870. IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
  871. return ret;
  872. }
  873. iwl_pcie_set_pwr(trans, false);
  874. iwl_trans_pcie_tx_reset(trans);
  875. ret = iwl_pcie_rx_init(trans);
  876. if (ret) {
  877. IWL_ERR(trans, "Failed to resume the device (RX reset)\n");
  878. return ret;
  879. }
  880. val = iwl_read32(trans, CSR_RESET);
  881. if (val & CSR_RESET_REG_FLAG_NEVO_RESET)
  882. *status = IWL_D3_STATUS_RESET;
  883. else
  884. *status = IWL_D3_STATUS_ALIVE;
  885. return 0;
  886. }
  887. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans)
  888. {
  889. bool hw_rfkill;
  890. int err;
  891. err = iwl_pcie_prepare_card_hw(trans);
  892. if (err) {
  893. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  894. return err;
  895. }
  896. /* Reset the entire device */
  897. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  898. usleep_range(10, 15);
  899. iwl_pcie_apm_init(trans);
  900. /* From now on, the op_mode will be kept updated about RF kill state */
  901. iwl_enable_rfkill_int(trans);
  902. hw_rfkill = iwl_is_rfkill_set(trans);
  903. if (hw_rfkill)
  904. set_bit(STATUS_RFKILL, &trans->status);
  905. else
  906. clear_bit(STATUS_RFKILL, &trans->status);
  907. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  908. return 0;
  909. }
  910. static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
  911. {
  912. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  913. /* disable interrupts - don't enable HW RF kill interrupt */
  914. spin_lock(&trans_pcie->irq_lock);
  915. iwl_disable_interrupts(trans);
  916. spin_unlock(&trans_pcie->irq_lock);
  917. iwl_pcie_apm_stop(trans);
  918. spin_lock(&trans_pcie->irq_lock);
  919. iwl_disable_interrupts(trans);
  920. spin_unlock(&trans_pcie->irq_lock);
  921. iwl_pcie_disable_ict(trans);
  922. }
  923. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  924. {
  925. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  926. }
  927. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  928. {
  929. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  930. }
  931. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  932. {
  933. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  934. }
  935. static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
  936. {
  937. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
  938. ((reg & 0x000FFFFF) | (3 << 24)));
  939. return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
  940. }
  941. static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
  942. u32 val)
  943. {
  944. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
  945. ((addr & 0x000FFFFF) | (3 << 24)));
  946. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  947. }
  948. static int iwl_pcie_dummy_napi_poll(struct napi_struct *napi, int budget)
  949. {
  950. WARN_ON(1);
  951. return 0;
  952. }
  953. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  954. const struct iwl_trans_config *trans_cfg)
  955. {
  956. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  957. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  958. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  959. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  960. trans_pcie->n_no_reclaim_cmds = 0;
  961. else
  962. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  963. if (trans_pcie->n_no_reclaim_cmds)
  964. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  965. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  966. trans_pcie->rx_buf_size_8k = trans_cfg->rx_buf_size_8k;
  967. if (trans_pcie->rx_buf_size_8k)
  968. trans_pcie->rx_page_order = get_order(8 * 1024);
  969. else
  970. trans_pcie->rx_page_order = get_order(4 * 1024);
  971. trans_pcie->wd_timeout =
  972. msecs_to_jiffies(trans_cfg->queue_watchdog_timeout);
  973. trans_pcie->command_names = trans_cfg->command_names;
  974. trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
  975. trans_pcie->scd_set_active = trans_cfg->scd_set_active;
  976. /* Initialize NAPI here - it should be before registering to mac80211
  977. * in the opmode but after the HW struct is allocated.
  978. * As this function may be called again in some corner cases don't
  979. * do anything if NAPI was already initialized.
  980. */
  981. if (!trans_pcie->napi.poll && trans->op_mode->ops->napi_add) {
  982. init_dummy_netdev(&trans_pcie->napi_dev);
  983. iwl_op_mode_napi_add(trans->op_mode, &trans_pcie->napi,
  984. &trans_pcie->napi_dev,
  985. iwl_pcie_dummy_napi_poll, 64);
  986. }
  987. }
  988. void iwl_trans_pcie_free(struct iwl_trans *trans)
  989. {
  990. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  991. synchronize_irq(trans_pcie->pci_dev->irq);
  992. iwl_pcie_tx_free(trans);
  993. iwl_pcie_rx_free(trans);
  994. free_irq(trans_pcie->pci_dev->irq, trans);
  995. iwl_pcie_free_ict(trans);
  996. pci_disable_msi(trans_pcie->pci_dev);
  997. iounmap(trans_pcie->hw_base);
  998. pci_release_regions(trans_pcie->pci_dev);
  999. pci_disable_device(trans_pcie->pci_dev);
  1000. kmem_cache_destroy(trans->dev_cmd_pool);
  1001. if (trans_pcie->napi.poll)
  1002. netif_napi_del(&trans_pcie->napi);
  1003. iwl_pcie_free_fw_monitor(trans);
  1004. kfree(trans);
  1005. }
  1006. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  1007. {
  1008. if (state)
  1009. set_bit(STATUS_TPOWER_PMI, &trans->status);
  1010. else
  1011. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  1012. }
  1013. static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans, bool silent,
  1014. unsigned long *flags)
  1015. {
  1016. int ret;
  1017. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1018. spin_lock_irqsave(&trans_pcie->reg_lock, *flags);
  1019. if (trans_pcie->cmd_in_flight)
  1020. goto out;
  1021. /* this bit wakes up the NIC */
  1022. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  1023. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1024. /*
  1025. * These bits say the device is running, and should keep running for
  1026. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  1027. * but they do not indicate that embedded SRAM is restored yet;
  1028. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  1029. * to/from host DRAM when sleeping/waking for power-saving.
  1030. * Each direction takes approximately 1/4 millisecond; with this
  1031. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  1032. * series of register accesses are expected (e.g. reading Event Log),
  1033. * to keep device from sleeping.
  1034. *
  1035. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  1036. * SRAM is okay/restored. We don't check that here because this call
  1037. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  1038. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  1039. *
  1040. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  1041. * and do not save/restore SRAM when power cycling.
  1042. */
  1043. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  1044. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  1045. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  1046. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  1047. if (unlikely(ret < 0)) {
  1048. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  1049. if (!silent) {
  1050. u32 val = iwl_read32(trans, CSR_GP_CNTRL);
  1051. WARN_ONCE(1,
  1052. "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
  1053. val);
  1054. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1055. return false;
  1056. }
  1057. }
  1058. out:
  1059. /*
  1060. * Fool sparse by faking we release the lock - sparse will
  1061. * track nic_access anyway.
  1062. */
  1063. __release(&trans_pcie->reg_lock);
  1064. return true;
  1065. }
  1066. static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans,
  1067. unsigned long *flags)
  1068. {
  1069. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1070. lockdep_assert_held(&trans_pcie->reg_lock);
  1071. /*
  1072. * Fool sparse by faking we acquiring the lock - sparse will
  1073. * track nic_access anyway.
  1074. */
  1075. __acquire(&trans_pcie->reg_lock);
  1076. if (trans_pcie->cmd_in_flight)
  1077. goto out;
  1078. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  1079. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1080. /*
  1081. * Above we read the CSR_GP_CNTRL register, which will flush
  1082. * any previous writes, but we need the write that clears the
  1083. * MAC_ACCESS_REQ bit to be performed before any other writes
  1084. * scheduled on different CPUs (after we drop reg_lock).
  1085. */
  1086. mmiowb();
  1087. out:
  1088. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1089. }
  1090. static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
  1091. void *buf, int dwords)
  1092. {
  1093. unsigned long flags;
  1094. int offs, ret = 0;
  1095. u32 *vals = buf;
  1096. if (iwl_trans_grab_nic_access(trans, false, &flags)) {
  1097. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  1098. for (offs = 0; offs < dwords; offs++)
  1099. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  1100. iwl_trans_release_nic_access(trans, &flags);
  1101. } else {
  1102. ret = -EBUSY;
  1103. }
  1104. return ret;
  1105. }
  1106. static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
  1107. const void *buf, int dwords)
  1108. {
  1109. unsigned long flags;
  1110. int offs, ret = 0;
  1111. const u32 *vals = buf;
  1112. if (iwl_trans_grab_nic_access(trans, false, &flags)) {
  1113. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  1114. for (offs = 0; offs < dwords; offs++)
  1115. iwl_write32(trans, HBUS_TARG_MEM_WDAT,
  1116. vals ? vals[offs] : 0);
  1117. iwl_trans_release_nic_access(trans, &flags);
  1118. } else {
  1119. ret = -EBUSY;
  1120. }
  1121. return ret;
  1122. }
  1123. #define IWL_FLUSH_WAIT_MS 2000
  1124. static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, u32 txq_bm)
  1125. {
  1126. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1127. struct iwl_txq *txq;
  1128. struct iwl_queue *q;
  1129. int cnt;
  1130. unsigned long now = jiffies;
  1131. u32 scd_sram_addr;
  1132. u8 buf[16];
  1133. int ret = 0;
  1134. /* waiting for all the tx frames complete might take a while */
  1135. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1136. u8 wr_ptr;
  1137. if (cnt == trans_pcie->cmd_queue)
  1138. continue;
  1139. if (!test_bit(cnt, trans_pcie->queue_used))
  1140. continue;
  1141. if (!(BIT(cnt) & txq_bm))
  1142. continue;
  1143. IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", cnt);
  1144. txq = &trans_pcie->txq[cnt];
  1145. q = &txq->q;
  1146. wr_ptr = ACCESS_ONCE(q->write_ptr);
  1147. while (q->read_ptr != ACCESS_ONCE(q->write_ptr) &&
  1148. !time_after(jiffies,
  1149. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
  1150. u8 write_ptr = ACCESS_ONCE(q->write_ptr);
  1151. if (WARN_ONCE(wr_ptr != write_ptr,
  1152. "WR pointer moved while flushing %d -> %d\n",
  1153. wr_ptr, write_ptr))
  1154. return -ETIMEDOUT;
  1155. msleep(1);
  1156. }
  1157. if (q->read_ptr != q->write_ptr) {
  1158. IWL_ERR(trans,
  1159. "fail to flush all tx fifo queues Q %d\n", cnt);
  1160. ret = -ETIMEDOUT;
  1161. break;
  1162. }
  1163. IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", cnt);
  1164. }
  1165. if (!ret)
  1166. return 0;
  1167. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  1168. txq->q.read_ptr, txq->q.write_ptr);
  1169. scd_sram_addr = trans_pcie->scd_base_addr +
  1170. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  1171. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  1172. iwl_print_hex_error(trans, buf, sizeof(buf));
  1173. for (cnt = 0; cnt < FH_TCSR_CHNL_NUM; cnt++)
  1174. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", cnt,
  1175. iwl_read_direct32(trans, FH_TX_TRB_REG(cnt)));
  1176. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1177. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(cnt));
  1178. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  1179. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  1180. u32 tbl_dw =
  1181. iwl_trans_read_mem32(trans, trans_pcie->scd_base_addr +
  1182. SCD_TRANS_TBL_OFFSET_QUEUE(cnt));
  1183. if (cnt & 0x1)
  1184. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  1185. else
  1186. tbl_dw = tbl_dw & 0x0000FFFF;
  1187. IWL_ERR(trans,
  1188. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  1189. cnt, active ? "" : "in", fifo, tbl_dw,
  1190. iwl_read_prph(trans, SCD_QUEUE_RDPTR(cnt)) &
  1191. (TFD_QUEUE_SIZE_MAX - 1),
  1192. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  1193. }
  1194. return ret;
  1195. }
  1196. static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
  1197. u32 mask, u32 value)
  1198. {
  1199. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1200. unsigned long flags;
  1201. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1202. __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value);
  1203. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1204. }
  1205. static const char *get_csr_string(int cmd)
  1206. {
  1207. #define IWL_CMD(x) case x: return #x
  1208. switch (cmd) {
  1209. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1210. IWL_CMD(CSR_INT_COALESCING);
  1211. IWL_CMD(CSR_INT);
  1212. IWL_CMD(CSR_INT_MASK);
  1213. IWL_CMD(CSR_FH_INT_STATUS);
  1214. IWL_CMD(CSR_GPIO_IN);
  1215. IWL_CMD(CSR_RESET);
  1216. IWL_CMD(CSR_GP_CNTRL);
  1217. IWL_CMD(CSR_HW_REV);
  1218. IWL_CMD(CSR_EEPROM_REG);
  1219. IWL_CMD(CSR_EEPROM_GP);
  1220. IWL_CMD(CSR_OTP_GP_REG);
  1221. IWL_CMD(CSR_GIO_REG);
  1222. IWL_CMD(CSR_GP_UCODE_REG);
  1223. IWL_CMD(CSR_GP_DRIVER_REG);
  1224. IWL_CMD(CSR_UCODE_DRV_GP1);
  1225. IWL_CMD(CSR_UCODE_DRV_GP2);
  1226. IWL_CMD(CSR_LED_REG);
  1227. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1228. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1229. IWL_CMD(CSR_ANA_PLL_CFG);
  1230. IWL_CMD(CSR_HW_REV_WA_REG);
  1231. IWL_CMD(CSR_MONITOR_STATUS_REG);
  1232. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1233. default:
  1234. return "UNKNOWN";
  1235. }
  1236. #undef IWL_CMD
  1237. }
  1238. void iwl_pcie_dump_csr(struct iwl_trans *trans)
  1239. {
  1240. int i;
  1241. static const u32 csr_tbl[] = {
  1242. CSR_HW_IF_CONFIG_REG,
  1243. CSR_INT_COALESCING,
  1244. CSR_INT,
  1245. CSR_INT_MASK,
  1246. CSR_FH_INT_STATUS,
  1247. CSR_GPIO_IN,
  1248. CSR_RESET,
  1249. CSR_GP_CNTRL,
  1250. CSR_HW_REV,
  1251. CSR_EEPROM_REG,
  1252. CSR_EEPROM_GP,
  1253. CSR_OTP_GP_REG,
  1254. CSR_GIO_REG,
  1255. CSR_GP_UCODE_REG,
  1256. CSR_GP_DRIVER_REG,
  1257. CSR_UCODE_DRV_GP1,
  1258. CSR_UCODE_DRV_GP2,
  1259. CSR_LED_REG,
  1260. CSR_DRAM_INT_TBL_REG,
  1261. CSR_GIO_CHICKEN_BITS,
  1262. CSR_ANA_PLL_CFG,
  1263. CSR_MONITOR_STATUS_REG,
  1264. CSR_HW_REV_WA_REG,
  1265. CSR_DBG_HPET_MEM_REG
  1266. };
  1267. IWL_ERR(trans, "CSR values:\n");
  1268. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1269. "CSR_INT_PERIODIC_REG)\n");
  1270. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1271. IWL_ERR(trans, " %25s: 0X%08x\n",
  1272. get_csr_string(csr_tbl[i]),
  1273. iwl_read32(trans, csr_tbl[i]));
  1274. }
  1275. }
  1276. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1277. /* create and remove of files */
  1278. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1279. if (!debugfs_create_file(#name, mode, parent, trans, \
  1280. &iwl_dbgfs_##name##_ops)) \
  1281. goto err; \
  1282. } while (0)
  1283. /* file operation */
  1284. #define DEBUGFS_READ_FILE_OPS(name) \
  1285. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1286. .read = iwl_dbgfs_##name##_read, \
  1287. .open = simple_open, \
  1288. .llseek = generic_file_llseek, \
  1289. };
  1290. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1291. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1292. .write = iwl_dbgfs_##name##_write, \
  1293. .open = simple_open, \
  1294. .llseek = generic_file_llseek, \
  1295. };
  1296. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1297. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1298. .write = iwl_dbgfs_##name##_write, \
  1299. .read = iwl_dbgfs_##name##_read, \
  1300. .open = simple_open, \
  1301. .llseek = generic_file_llseek, \
  1302. };
  1303. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1304. char __user *user_buf,
  1305. size_t count, loff_t *ppos)
  1306. {
  1307. struct iwl_trans *trans = file->private_data;
  1308. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1309. struct iwl_txq *txq;
  1310. struct iwl_queue *q;
  1311. char *buf;
  1312. int pos = 0;
  1313. int cnt;
  1314. int ret;
  1315. size_t bufsz;
  1316. bufsz = sizeof(char) * 64 * trans->cfg->base_params->num_of_queues;
  1317. if (!trans_pcie->txq)
  1318. return -EAGAIN;
  1319. buf = kzalloc(bufsz, GFP_KERNEL);
  1320. if (!buf)
  1321. return -ENOMEM;
  1322. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1323. txq = &trans_pcie->txq[cnt];
  1324. q = &txq->q;
  1325. pos += scnprintf(buf + pos, bufsz - pos,
  1326. "hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d%s\n",
  1327. cnt, q->read_ptr, q->write_ptr,
  1328. !!test_bit(cnt, trans_pcie->queue_used),
  1329. !!test_bit(cnt, trans_pcie->queue_stopped),
  1330. txq->need_update,
  1331. (cnt == trans_pcie->cmd_queue ? " HCMD" : ""));
  1332. }
  1333. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1334. kfree(buf);
  1335. return ret;
  1336. }
  1337. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1338. char __user *user_buf,
  1339. size_t count, loff_t *ppos)
  1340. {
  1341. struct iwl_trans *trans = file->private_data;
  1342. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1343. struct iwl_rxq *rxq = &trans_pcie->rxq;
  1344. char buf[256];
  1345. int pos = 0;
  1346. const size_t bufsz = sizeof(buf);
  1347. pos += scnprintf(buf + pos, bufsz - pos, "read: %u\n",
  1348. rxq->read);
  1349. pos += scnprintf(buf + pos, bufsz - pos, "write: %u\n",
  1350. rxq->write);
  1351. pos += scnprintf(buf + pos, bufsz - pos, "write_actual: %u\n",
  1352. rxq->write_actual);
  1353. pos += scnprintf(buf + pos, bufsz - pos, "need_update: %d\n",
  1354. rxq->need_update);
  1355. pos += scnprintf(buf + pos, bufsz - pos, "free_count: %u\n",
  1356. rxq->free_count);
  1357. if (rxq->rb_stts) {
  1358. pos += scnprintf(buf + pos, bufsz - pos, "closed_rb_num: %u\n",
  1359. le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF);
  1360. } else {
  1361. pos += scnprintf(buf + pos, bufsz - pos,
  1362. "closed_rb_num: Not Allocated\n");
  1363. }
  1364. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1365. }
  1366. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1367. char __user *user_buf,
  1368. size_t count, loff_t *ppos)
  1369. {
  1370. struct iwl_trans *trans = file->private_data;
  1371. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1372. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1373. int pos = 0;
  1374. char *buf;
  1375. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1376. ssize_t ret;
  1377. buf = kzalloc(bufsz, GFP_KERNEL);
  1378. if (!buf)
  1379. return -ENOMEM;
  1380. pos += scnprintf(buf + pos, bufsz - pos,
  1381. "Interrupt Statistics Report:\n");
  1382. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1383. isr_stats->hw);
  1384. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1385. isr_stats->sw);
  1386. if (isr_stats->sw || isr_stats->hw) {
  1387. pos += scnprintf(buf + pos, bufsz - pos,
  1388. "\tLast Restarting Code: 0x%X\n",
  1389. isr_stats->err_code);
  1390. }
  1391. #ifdef CONFIG_IWLWIFI_DEBUG
  1392. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1393. isr_stats->sch);
  1394. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1395. isr_stats->alive);
  1396. #endif
  1397. pos += scnprintf(buf + pos, bufsz - pos,
  1398. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1399. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1400. isr_stats->ctkill);
  1401. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1402. isr_stats->wakeup);
  1403. pos += scnprintf(buf + pos, bufsz - pos,
  1404. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1405. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1406. isr_stats->tx);
  1407. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1408. isr_stats->unhandled);
  1409. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1410. kfree(buf);
  1411. return ret;
  1412. }
  1413. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1414. const char __user *user_buf,
  1415. size_t count, loff_t *ppos)
  1416. {
  1417. struct iwl_trans *trans = file->private_data;
  1418. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1419. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1420. char buf[8];
  1421. int buf_size;
  1422. u32 reset_flag;
  1423. memset(buf, 0, sizeof(buf));
  1424. buf_size = min(count, sizeof(buf) - 1);
  1425. if (copy_from_user(buf, user_buf, buf_size))
  1426. return -EFAULT;
  1427. if (sscanf(buf, "%x", &reset_flag) != 1)
  1428. return -EFAULT;
  1429. if (reset_flag == 0)
  1430. memset(isr_stats, 0, sizeof(*isr_stats));
  1431. return count;
  1432. }
  1433. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1434. const char __user *user_buf,
  1435. size_t count, loff_t *ppos)
  1436. {
  1437. struct iwl_trans *trans = file->private_data;
  1438. char buf[8];
  1439. int buf_size;
  1440. int csr;
  1441. memset(buf, 0, sizeof(buf));
  1442. buf_size = min(count, sizeof(buf) - 1);
  1443. if (copy_from_user(buf, user_buf, buf_size))
  1444. return -EFAULT;
  1445. if (sscanf(buf, "%d", &csr) != 1)
  1446. return -EFAULT;
  1447. iwl_pcie_dump_csr(trans);
  1448. return count;
  1449. }
  1450. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1451. char __user *user_buf,
  1452. size_t count, loff_t *ppos)
  1453. {
  1454. struct iwl_trans *trans = file->private_data;
  1455. char *buf = NULL;
  1456. ssize_t ret;
  1457. ret = iwl_dump_fh(trans, &buf);
  1458. if (ret < 0)
  1459. return ret;
  1460. if (!buf)
  1461. return -EINVAL;
  1462. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1463. kfree(buf);
  1464. return ret;
  1465. }
  1466. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  1467. DEBUGFS_READ_FILE_OPS(fh_reg);
  1468. DEBUGFS_READ_FILE_OPS(rx_queue);
  1469. DEBUGFS_READ_FILE_OPS(tx_queue);
  1470. DEBUGFS_WRITE_FILE_OPS(csr);
  1471. /*
  1472. * Create the debugfs files and directories
  1473. *
  1474. */
  1475. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1476. struct dentry *dir)
  1477. {
  1478. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  1479. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  1480. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  1481. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  1482. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  1483. return 0;
  1484. err:
  1485. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  1486. return -ENOMEM;
  1487. }
  1488. #else
  1489. static int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans,
  1490. struct dentry *dir)
  1491. {
  1492. return 0;
  1493. }
  1494. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  1495. static u32 iwl_trans_pcie_get_cmdlen(struct iwl_tfd *tfd)
  1496. {
  1497. u32 cmdlen = 0;
  1498. int i;
  1499. for (i = 0; i < IWL_NUM_OF_TBS; i++)
  1500. cmdlen += iwl_pcie_tfd_tb_get_len(tfd, i);
  1501. return cmdlen;
  1502. }
  1503. static const struct {
  1504. u32 start, end;
  1505. } iwl_prph_dump_addr[] = {
  1506. { .start = 0x00a00000, .end = 0x00a00000 },
  1507. { .start = 0x00a0000c, .end = 0x00a00024 },
  1508. { .start = 0x00a0002c, .end = 0x00a0003c },
  1509. { .start = 0x00a00410, .end = 0x00a00418 },
  1510. { .start = 0x00a00420, .end = 0x00a00420 },
  1511. { .start = 0x00a00428, .end = 0x00a00428 },
  1512. { .start = 0x00a00430, .end = 0x00a0043c },
  1513. { .start = 0x00a00444, .end = 0x00a00444 },
  1514. { .start = 0x00a004c0, .end = 0x00a004cc },
  1515. { .start = 0x00a004d8, .end = 0x00a004d8 },
  1516. { .start = 0x00a004e0, .end = 0x00a004f0 },
  1517. { .start = 0x00a00840, .end = 0x00a00840 },
  1518. { .start = 0x00a00850, .end = 0x00a00858 },
  1519. { .start = 0x00a01004, .end = 0x00a01008 },
  1520. { .start = 0x00a01010, .end = 0x00a01010 },
  1521. { .start = 0x00a01018, .end = 0x00a01018 },
  1522. { .start = 0x00a01024, .end = 0x00a01024 },
  1523. { .start = 0x00a0102c, .end = 0x00a01034 },
  1524. { .start = 0x00a0103c, .end = 0x00a01040 },
  1525. { .start = 0x00a01048, .end = 0x00a01094 },
  1526. { .start = 0x00a01c00, .end = 0x00a01c20 },
  1527. { .start = 0x00a01c58, .end = 0x00a01c58 },
  1528. { .start = 0x00a01c7c, .end = 0x00a01c7c },
  1529. { .start = 0x00a01c28, .end = 0x00a01c54 },
  1530. { .start = 0x00a01c5c, .end = 0x00a01c5c },
  1531. { .start = 0x00a01c84, .end = 0x00a01c84 },
  1532. { .start = 0x00a01ce0, .end = 0x00a01d0c },
  1533. { .start = 0x00a01d18, .end = 0x00a01d20 },
  1534. { .start = 0x00a01d2c, .end = 0x00a01d30 },
  1535. { .start = 0x00a01d40, .end = 0x00a01d5c },
  1536. { .start = 0x00a01d80, .end = 0x00a01d80 },
  1537. { .start = 0x00a01d98, .end = 0x00a01d98 },
  1538. { .start = 0x00a01dc0, .end = 0x00a01dfc },
  1539. { .start = 0x00a01e00, .end = 0x00a01e2c },
  1540. { .start = 0x00a01e40, .end = 0x00a01e60 },
  1541. { .start = 0x00a01e84, .end = 0x00a01e90 },
  1542. { .start = 0x00a01e9c, .end = 0x00a01ec4 },
  1543. { .start = 0x00a01ed0, .end = 0x00a01ed0 },
  1544. { .start = 0x00a01f00, .end = 0x00a01f14 },
  1545. { .start = 0x00a01f44, .end = 0x00a01f58 },
  1546. { .start = 0x00a01f80, .end = 0x00a01fa8 },
  1547. { .start = 0x00a01fb0, .end = 0x00a01fbc },
  1548. { .start = 0x00a01ff8, .end = 0x00a01ffc },
  1549. { .start = 0x00a02000, .end = 0x00a02048 },
  1550. { .start = 0x00a02068, .end = 0x00a020f0 },
  1551. { .start = 0x00a02100, .end = 0x00a02118 },
  1552. { .start = 0x00a02140, .end = 0x00a0214c },
  1553. { .start = 0x00a02168, .end = 0x00a0218c },
  1554. { .start = 0x00a021c0, .end = 0x00a021c0 },
  1555. { .start = 0x00a02400, .end = 0x00a02410 },
  1556. { .start = 0x00a02418, .end = 0x00a02420 },
  1557. { .start = 0x00a02428, .end = 0x00a0242c },
  1558. { .start = 0x00a02434, .end = 0x00a02434 },
  1559. { .start = 0x00a02440, .end = 0x00a02460 },
  1560. { .start = 0x00a02468, .end = 0x00a024b0 },
  1561. { .start = 0x00a024c8, .end = 0x00a024cc },
  1562. { .start = 0x00a02500, .end = 0x00a02504 },
  1563. { .start = 0x00a0250c, .end = 0x00a02510 },
  1564. { .start = 0x00a02540, .end = 0x00a02554 },
  1565. { .start = 0x00a02580, .end = 0x00a025f4 },
  1566. { .start = 0x00a02600, .end = 0x00a0260c },
  1567. { .start = 0x00a02648, .end = 0x00a02650 },
  1568. { .start = 0x00a02680, .end = 0x00a02680 },
  1569. { .start = 0x00a026c0, .end = 0x00a026d0 },
  1570. { .start = 0x00a02700, .end = 0x00a0270c },
  1571. { .start = 0x00a02804, .end = 0x00a02804 },
  1572. { .start = 0x00a02818, .end = 0x00a0281c },
  1573. { .start = 0x00a02c00, .end = 0x00a02db4 },
  1574. { .start = 0x00a02df4, .end = 0x00a02fb0 },
  1575. { .start = 0x00a03000, .end = 0x00a03014 },
  1576. { .start = 0x00a0301c, .end = 0x00a0302c },
  1577. { .start = 0x00a03034, .end = 0x00a03038 },
  1578. { .start = 0x00a03040, .end = 0x00a03048 },
  1579. { .start = 0x00a03060, .end = 0x00a03068 },
  1580. { .start = 0x00a03070, .end = 0x00a03074 },
  1581. { .start = 0x00a0307c, .end = 0x00a0307c },
  1582. { .start = 0x00a03080, .end = 0x00a03084 },
  1583. { .start = 0x00a0308c, .end = 0x00a03090 },
  1584. { .start = 0x00a03098, .end = 0x00a03098 },
  1585. { .start = 0x00a030a0, .end = 0x00a030a0 },
  1586. { .start = 0x00a030a8, .end = 0x00a030b4 },
  1587. { .start = 0x00a030bc, .end = 0x00a030bc },
  1588. { .start = 0x00a030c0, .end = 0x00a0312c },
  1589. { .start = 0x00a03c00, .end = 0x00a03c5c },
  1590. { .start = 0x00a04400, .end = 0x00a04454 },
  1591. { .start = 0x00a04460, .end = 0x00a04474 },
  1592. { .start = 0x00a044c0, .end = 0x00a044ec },
  1593. { .start = 0x00a04500, .end = 0x00a04504 },
  1594. { .start = 0x00a04510, .end = 0x00a04538 },
  1595. { .start = 0x00a04540, .end = 0x00a04548 },
  1596. { .start = 0x00a04560, .end = 0x00a0457c },
  1597. { .start = 0x00a04590, .end = 0x00a04598 },
  1598. { .start = 0x00a045c0, .end = 0x00a045f4 },
  1599. };
  1600. static u32 iwl_trans_pcie_dump_prph(struct iwl_trans *trans,
  1601. struct iwl_fw_error_dump_data **data)
  1602. {
  1603. struct iwl_fw_error_dump_prph *prph;
  1604. unsigned long flags;
  1605. u32 prph_len = 0, i;
  1606. if (!iwl_trans_grab_nic_access(trans, false, &flags))
  1607. return 0;
  1608. for (i = 0; i < ARRAY_SIZE(iwl_prph_dump_addr); i++) {
  1609. /* The range includes both boundaries */
  1610. int num_bytes_in_chunk = iwl_prph_dump_addr[i].end -
  1611. iwl_prph_dump_addr[i].start + 4;
  1612. int reg;
  1613. __le32 *val;
  1614. prph_len += sizeof(*data) + sizeof(*prph) +
  1615. num_bytes_in_chunk;
  1616. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PRPH);
  1617. (*data)->len = cpu_to_le32(sizeof(*prph) +
  1618. num_bytes_in_chunk);
  1619. prph = (void *)(*data)->data;
  1620. prph->prph_start = cpu_to_le32(iwl_prph_dump_addr[i].start);
  1621. val = (void *)prph->data;
  1622. for (reg = iwl_prph_dump_addr[i].start;
  1623. reg <= iwl_prph_dump_addr[i].end;
  1624. reg += 4)
  1625. *val++ = cpu_to_le32(iwl_trans_pcie_read_prph(trans,
  1626. reg));
  1627. *data = iwl_fw_error_next_data(*data);
  1628. }
  1629. iwl_trans_release_nic_access(trans, &flags);
  1630. return prph_len;
  1631. }
  1632. #define IWL_CSR_TO_DUMP (0x250)
  1633. static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans,
  1634. struct iwl_fw_error_dump_data **data)
  1635. {
  1636. u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP;
  1637. __le32 *val;
  1638. int i;
  1639. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR);
  1640. (*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP);
  1641. val = (void *)(*data)->data;
  1642. for (i = 0; i < IWL_CSR_TO_DUMP; i += 4)
  1643. *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
  1644. *data = iwl_fw_error_next_data(*data);
  1645. return csr_len;
  1646. }
  1647. static
  1648. struct iwl_trans_dump_data *iwl_trans_pcie_dump_data(struct iwl_trans *trans)
  1649. {
  1650. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1651. struct iwl_fw_error_dump_data *data;
  1652. struct iwl_txq *cmdq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1653. struct iwl_fw_error_dump_txcmd *txcmd;
  1654. struct iwl_trans_dump_data *dump_data;
  1655. u32 len;
  1656. int i, ptr;
  1657. /* transport dump header */
  1658. len = sizeof(*dump_data);
  1659. /* host commands */
  1660. len += sizeof(*data) +
  1661. cmdq->q.n_window * (sizeof(*txcmd) + TFD_MAX_PAYLOAD_SIZE);
  1662. /* CSR registers */
  1663. len += sizeof(*data) + IWL_CSR_TO_DUMP;
  1664. /* PRPH registers */
  1665. for (i = 0; i < ARRAY_SIZE(iwl_prph_dump_addr); i++) {
  1666. /* The range includes both boundaries */
  1667. int num_bytes_in_chunk = iwl_prph_dump_addr[i].end -
  1668. iwl_prph_dump_addr[i].start + 4;
  1669. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_prph) +
  1670. num_bytes_in_chunk;
  1671. }
  1672. /* FW monitor */
  1673. if (trans_pcie->fw_mon_page)
  1674. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
  1675. trans_pcie->fw_mon_size;
  1676. dump_data = vzalloc(len);
  1677. if (!dump_data)
  1678. return NULL;
  1679. len = 0;
  1680. data = (void *)dump_data->data;
  1681. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
  1682. txcmd = (void *)data->data;
  1683. spin_lock_bh(&cmdq->lock);
  1684. ptr = cmdq->q.write_ptr;
  1685. for (i = 0; i < cmdq->q.n_window; i++) {
  1686. u8 idx = get_cmd_index(&cmdq->q, ptr);
  1687. u32 caplen, cmdlen;
  1688. cmdlen = iwl_trans_pcie_get_cmdlen(&cmdq->tfds[ptr]);
  1689. caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
  1690. if (cmdlen) {
  1691. len += sizeof(*txcmd) + caplen;
  1692. txcmd->cmdlen = cpu_to_le32(cmdlen);
  1693. txcmd->caplen = cpu_to_le32(caplen);
  1694. memcpy(txcmd->data, cmdq->entries[idx].cmd, caplen);
  1695. txcmd = (void *)((u8 *)txcmd->data + caplen);
  1696. }
  1697. ptr = iwl_queue_dec_wrap(ptr);
  1698. }
  1699. spin_unlock_bh(&cmdq->lock);
  1700. data->len = cpu_to_le32(len);
  1701. len += sizeof(*data);
  1702. data = iwl_fw_error_next_data(data);
  1703. len += iwl_trans_pcie_dump_prph(trans, &data);
  1704. len += iwl_trans_pcie_dump_csr(trans, &data);
  1705. /* data is already pointing to the next section */
  1706. if (trans_pcie->fw_mon_page) {
  1707. struct iwl_fw_error_dump_fw_mon *fw_mon_data;
  1708. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR);
  1709. data->len = cpu_to_le32(trans_pcie->fw_mon_size +
  1710. sizeof(*fw_mon_data));
  1711. fw_mon_data = (void *)data->data;
  1712. fw_mon_data->fw_mon_wr_ptr =
  1713. cpu_to_le32(iwl_read_prph(trans, MON_BUFF_WRPTR));
  1714. fw_mon_data->fw_mon_cycle_cnt =
  1715. cpu_to_le32(iwl_read_prph(trans, MON_BUFF_CYCLE_CNT));
  1716. fw_mon_data->fw_mon_base_ptr =
  1717. cpu_to_le32(iwl_read_prph(trans, MON_BUFF_BASE_ADDR));
  1718. /*
  1719. * The firmware is now asserted, it won't write anything to
  1720. * the buffer. CPU can take ownership to fetch the data.
  1721. * The buffer will be handed back to the device before the
  1722. * firmware will be restarted.
  1723. */
  1724. dma_sync_single_for_cpu(trans->dev, trans_pcie->fw_mon_phys,
  1725. trans_pcie->fw_mon_size,
  1726. DMA_FROM_DEVICE);
  1727. memcpy(fw_mon_data->data, page_address(trans_pcie->fw_mon_page),
  1728. trans_pcie->fw_mon_size);
  1729. len += sizeof(*data) + sizeof(*fw_mon_data) +
  1730. trans_pcie->fw_mon_size;
  1731. }
  1732. dump_data->len = len;
  1733. return dump_data;
  1734. }
  1735. static const struct iwl_trans_ops trans_ops_pcie = {
  1736. .start_hw = iwl_trans_pcie_start_hw,
  1737. .op_mode_leave = iwl_trans_pcie_op_mode_leave,
  1738. .fw_alive = iwl_trans_pcie_fw_alive,
  1739. .start_fw = iwl_trans_pcie_start_fw,
  1740. .stop_device = iwl_trans_pcie_stop_device,
  1741. .d3_suspend = iwl_trans_pcie_d3_suspend,
  1742. .d3_resume = iwl_trans_pcie_d3_resume,
  1743. .send_cmd = iwl_trans_pcie_send_hcmd,
  1744. .tx = iwl_trans_pcie_tx,
  1745. .reclaim = iwl_trans_pcie_reclaim,
  1746. .txq_disable = iwl_trans_pcie_txq_disable,
  1747. .txq_enable = iwl_trans_pcie_txq_enable,
  1748. .dbgfs_register = iwl_trans_pcie_dbgfs_register,
  1749. .wait_tx_queue_empty = iwl_trans_pcie_wait_txq_empty,
  1750. .write8 = iwl_trans_pcie_write8,
  1751. .write32 = iwl_trans_pcie_write32,
  1752. .read32 = iwl_trans_pcie_read32,
  1753. .read_prph = iwl_trans_pcie_read_prph,
  1754. .write_prph = iwl_trans_pcie_write_prph,
  1755. .read_mem = iwl_trans_pcie_read_mem,
  1756. .write_mem = iwl_trans_pcie_write_mem,
  1757. .configure = iwl_trans_pcie_configure,
  1758. .set_pmi = iwl_trans_pcie_set_pmi,
  1759. .grab_nic_access = iwl_trans_pcie_grab_nic_access,
  1760. .release_nic_access = iwl_trans_pcie_release_nic_access,
  1761. .set_bits_mask = iwl_trans_pcie_set_bits_mask,
  1762. .dump_data = iwl_trans_pcie_dump_data,
  1763. };
  1764. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  1765. const struct pci_device_id *ent,
  1766. const struct iwl_cfg *cfg)
  1767. {
  1768. struct iwl_trans_pcie *trans_pcie;
  1769. struct iwl_trans *trans;
  1770. u16 pci_cmd;
  1771. int err;
  1772. trans = kzalloc(sizeof(struct iwl_trans) +
  1773. sizeof(struct iwl_trans_pcie), GFP_KERNEL);
  1774. if (!trans) {
  1775. err = -ENOMEM;
  1776. goto out;
  1777. }
  1778. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1779. trans->ops = &trans_ops_pcie;
  1780. trans->cfg = cfg;
  1781. trans_lockdep_init(trans);
  1782. trans_pcie->trans = trans;
  1783. spin_lock_init(&trans_pcie->irq_lock);
  1784. spin_lock_init(&trans_pcie->reg_lock);
  1785. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  1786. err = pci_enable_device(pdev);
  1787. if (err)
  1788. goto out_no_pci;
  1789. if (!cfg->base_params->pcie_l1_allowed) {
  1790. /*
  1791. * W/A - seems to solve weird behavior. We need to remove this
  1792. * if we don't want to stay in L1 all the time. This wastes a
  1793. * lot of power.
  1794. */
  1795. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  1796. PCIE_LINK_STATE_L1 |
  1797. PCIE_LINK_STATE_CLKPM);
  1798. }
  1799. pci_set_master(pdev);
  1800. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  1801. if (!err)
  1802. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  1803. if (err) {
  1804. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1805. if (!err)
  1806. err = pci_set_consistent_dma_mask(pdev,
  1807. DMA_BIT_MASK(32));
  1808. /* both attempts failed: */
  1809. if (err) {
  1810. dev_err(&pdev->dev, "No suitable DMA available\n");
  1811. goto out_pci_disable_device;
  1812. }
  1813. }
  1814. err = pci_request_regions(pdev, DRV_NAME);
  1815. if (err) {
  1816. dev_err(&pdev->dev, "pci_request_regions failed\n");
  1817. goto out_pci_disable_device;
  1818. }
  1819. trans_pcie->hw_base = pci_ioremap_bar(pdev, 0);
  1820. if (!trans_pcie->hw_base) {
  1821. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  1822. err = -ENODEV;
  1823. goto out_pci_release_regions;
  1824. }
  1825. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  1826. * PCI Tx retries from interfering with C3 CPU state */
  1827. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1828. trans->dev = &pdev->dev;
  1829. trans_pcie->pci_dev = pdev;
  1830. iwl_disable_interrupts(trans);
  1831. err = pci_enable_msi(pdev);
  1832. if (err) {
  1833. dev_err(&pdev->dev, "pci_enable_msi failed(0X%x)\n", err);
  1834. /* enable rfkill interrupt: hw bug w/a */
  1835. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1836. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1837. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1838. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1839. }
  1840. }
  1841. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  1842. /*
  1843. * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have
  1844. * changed, and now the revision step also includes bit 0-1 (no more
  1845. * "dash" value). To keep hw_rev backwards compatible - we'll store it
  1846. * in the old format.
  1847. */
  1848. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1849. trans->hw_rev = (trans->hw_rev & 0xfff0) |
  1850. (CSR_HW_REV_STEP(trans->hw_rev << 2) << 2);
  1851. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  1852. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  1853. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  1854. /* Initialize the wait queue for commands */
  1855. init_waitqueue_head(&trans_pcie->wait_command_queue);
  1856. snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
  1857. "iwl_cmd_pool:%s", dev_name(trans->dev));
  1858. trans->dev_cmd_headroom = 0;
  1859. trans->dev_cmd_pool =
  1860. kmem_cache_create(trans->dev_cmd_pool_name,
  1861. sizeof(struct iwl_device_cmd)
  1862. + trans->dev_cmd_headroom,
  1863. sizeof(void *),
  1864. SLAB_HWCACHE_ALIGN,
  1865. NULL);
  1866. if (!trans->dev_cmd_pool) {
  1867. err = -ENOMEM;
  1868. goto out_pci_disable_msi;
  1869. }
  1870. if (iwl_pcie_alloc_ict(trans))
  1871. goto out_free_cmd_pool;
  1872. err = request_threaded_irq(pdev->irq, iwl_pcie_isr,
  1873. iwl_pcie_irq_handler,
  1874. IRQF_SHARED, DRV_NAME, trans);
  1875. if (err) {
  1876. IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
  1877. goto out_free_ict;
  1878. }
  1879. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  1880. return trans;
  1881. out_free_ict:
  1882. iwl_pcie_free_ict(trans);
  1883. out_free_cmd_pool:
  1884. kmem_cache_destroy(trans->dev_cmd_pool);
  1885. out_pci_disable_msi:
  1886. pci_disable_msi(pdev);
  1887. out_pci_release_regions:
  1888. pci_release_regions(pdev);
  1889. out_pci_disable_device:
  1890. pci_disable_device(pdev);
  1891. out_no_pci:
  1892. kfree(trans);
  1893. out:
  1894. return ERR_PTR(err);
  1895. }