trans.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004
  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 - 2015 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2005 - 2015 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/pci.h>
  68. #include <linux/pci-aspm.h>
  69. #include <linux/interrupt.h>
  70. #include <linux/debugfs.h>
  71. #include <linux/sched.h>
  72. #include <linux/bitops.h>
  73. #include <linux/gfp.h>
  74. #include <linux/vmalloc.h>
  75. #include <linux/pm_runtime.h>
  76. #include "iwl-drv.h"
  77. #include "iwl-trans.h"
  78. #include "iwl-csr.h"
  79. #include "iwl-prph.h"
  80. #include "iwl-scd.h"
  81. #include "iwl-agn-hw.h"
  82. #include "iwl-fw-error-dump.h"
  83. #include "internal.h"
  84. #include "iwl-fh.h"
  85. /* extended range in FW SRAM */
  86. #define IWL_FW_MEM_EXTENDED_START 0x40000
  87. #define IWL_FW_MEM_EXTENDED_END 0x57FFF
  88. static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans)
  89. {
  90. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  91. if (!trans_pcie->fw_mon_page)
  92. return;
  93. dma_unmap_page(trans->dev, trans_pcie->fw_mon_phys,
  94. trans_pcie->fw_mon_size, DMA_FROM_DEVICE);
  95. __free_pages(trans_pcie->fw_mon_page,
  96. get_order(trans_pcie->fw_mon_size));
  97. trans_pcie->fw_mon_page = NULL;
  98. trans_pcie->fw_mon_phys = 0;
  99. trans_pcie->fw_mon_size = 0;
  100. }
  101. static void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power)
  102. {
  103. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  104. struct page *page = NULL;
  105. dma_addr_t phys;
  106. u32 size = 0;
  107. u8 power;
  108. if (!max_power) {
  109. /* default max_power is maximum */
  110. max_power = 26;
  111. } else {
  112. max_power += 11;
  113. }
  114. if (WARN(max_power > 26,
  115. "External buffer size for monitor is too big %d, check the FW TLV\n",
  116. max_power))
  117. return;
  118. if (trans_pcie->fw_mon_page) {
  119. dma_sync_single_for_device(trans->dev, trans_pcie->fw_mon_phys,
  120. trans_pcie->fw_mon_size,
  121. DMA_FROM_DEVICE);
  122. return;
  123. }
  124. phys = 0;
  125. for (power = max_power; power >= 11; power--) {
  126. int order;
  127. size = BIT(power);
  128. order = get_order(size);
  129. page = alloc_pages(__GFP_COMP | __GFP_NOWARN | __GFP_ZERO,
  130. order);
  131. if (!page)
  132. continue;
  133. phys = dma_map_page(trans->dev, page, 0, PAGE_SIZE << order,
  134. DMA_FROM_DEVICE);
  135. if (dma_mapping_error(trans->dev, phys)) {
  136. __free_pages(page, order);
  137. page = NULL;
  138. continue;
  139. }
  140. IWL_INFO(trans,
  141. "Allocated 0x%08x bytes (order %d) for firmware monitor.\n",
  142. size, order);
  143. break;
  144. }
  145. if (WARN_ON_ONCE(!page))
  146. return;
  147. if (power != max_power)
  148. IWL_ERR(trans,
  149. "Sorry - debug buffer is only %luK while you requested %luK\n",
  150. (unsigned long)BIT(power - 10),
  151. (unsigned long)BIT(max_power - 10));
  152. trans_pcie->fw_mon_page = page;
  153. trans_pcie->fw_mon_phys = phys;
  154. trans_pcie->fw_mon_size = size;
  155. }
  156. static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
  157. {
  158. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  159. ((reg & 0x0000ffff) | (2 << 28)));
  160. return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
  161. }
  162. static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
  163. {
  164. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
  165. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  166. ((reg & 0x0000ffff) | (3 << 28)));
  167. }
  168. static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
  169. {
  170. if (trans->cfg->apmg_not_supported)
  171. return;
  172. if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
  173. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  174. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  175. ~APMG_PS_CTRL_MSK_PWR_SRC);
  176. else
  177. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  178. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  179. ~APMG_PS_CTRL_MSK_PWR_SRC);
  180. }
  181. /* PCI registers */
  182. #define PCI_CFG_RETRY_TIMEOUT 0x041
  183. static void iwl_pcie_apm_config(struct iwl_trans *trans)
  184. {
  185. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  186. u16 lctl;
  187. u16 cap;
  188. /*
  189. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  190. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  191. * If so (likely), disable L0S, so device moves directly L0->L1;
  192. * costs negligible amount of power savings.
  193. * If not (unlikely), enable L0S, so there is at least some
  194. * power savings, even without L1.
  195. */
  196. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
  197. if (lctl & PCI_EXP_LNKCTL_ASPM_L1)
  198. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  199. else
  200. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  201. trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
  202. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap);
  203. trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN;
  204. dev_info(trans->dev, "L1 %sabled - LTR %sabled\n",
  205. (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis",
  206. trans->ltr_enabled ? "En" : "Dis");
  207. }
  208. /*
  209. * Start up NIC's basic functionality after it has been reset
  210. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  211. * NOTE: This does not load uCode nor start the embedded processor
  212. */
  213. static int iwl_pcie_apm_init(struct iwl_trans *trans)
  214. {
  215. int ret = 0;
  216. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  217. /*
  218. * Use "set_bit" below rather than "write", to preserve any hardware
  219. * bits already set by default after reset.
  220. */
  221. /* Disable L0S exit timer (platform NMI Work/Around) */
  222. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  223. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  224. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  225. /*
  226. * Disable L0s without affecting L1;
  227. * don't wait for ICH L0s (ICH bug W/A)
  228. */
  229. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  230. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  231. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  232. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  233. /*
  234. * Enable HAP INTA (interrupt from management bus) to
  235. * wake device's PCI Express link L1a -> L0s
  236. */
  237. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  238. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  239. iwl_pcie_apm_config(trans);
  240. /* Configure analog phase-lock-loop before activating to D0A */
  241. if (trans->cfg->base_params->pll_cfg)
  242. iwl_set_bit(trans, CSR_ANA_PLL_CFG, CSR50_ANA_PLL_CFG_VAL);
  243. /*
  244. * Set "initialization complete" bit to move adapter from
  245. * D0U* --> D0A* (powered-up active) state.
  246. */
  247. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  248. /*
  249. * Wait for clock stabilization; once stabilized, access to
  250. * device-internal resources is supported, e.g. iwl_write_prph()
  251. * and accesses to uCode SRAM.
  252. */
  253. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  254. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  255. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  256. if (ret < 0) {
  257. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  258. goto out;
  259. }
  260. if (trans->cfg->host_interrupt_operation_mode) {
  261. /*
  262. * This is a bit of an abuse - This is needed for 7260 / 3160
  263. * only check host_interrupt_operation_mode even if this is
  264. * not related to host_interrupt_operation_mode.
  265. *
  266. * Enable the oscillator to count wake up time for L1 exit. This
  267. * consumes slightly more power (100uA) - but allows to be sure
  268. * that we wake up from L1 on time.
  269. *
  270. * This looks weird: read twice the same register, discard the
  271. * value, set a bit, and yet again, read that same register
  272. * just to discard the value. But that's the way the hardware
  273. * seems to like it.
  274. */
  275. iwl_read_prph(trans, OSC_CLK);
  276. iwl_read_prph(trans, OSC_CLK);
  277. iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
  278. iwl_read_prph(trans, OSC_CLK);
  279. iwl_read_prph(trans, OSC_CLK);
  280. }
  281. /*
  282. * Enable DMA clock and wait for it to stabilize.
  283. *
  284. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
  285. * bits do not disable clocks. This preserves any hardware
  286. * bits already set by default in "CLK_CTRL_REG" after reset.
  287. */
  288. if (!trans->cfg->apmg_not_supported) {
  289. iwl_write_prph(trans, APMG_CLK_EN_REG,
  290. APMG_CLK_VAL_DMA_CLK_RQT);
  291. udelay(20);
  292. /* Disable L1-Active */
  293. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  294. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  295. /* Clear the interrupt in APMG if the NIC is in RFKILL */
  296. iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
  297. APMG_RTC_INT_STT_RFKILL);
  298. }
  299. set_bit(STATUS_DEVICE_ENABLED, &trans->status);
  300. out:
  301. return ret;
  302. }
  303. /*
  304. * Enable LP XTAL to avoid HW bug where device may consume much power if
  305. * FW is not loaded after device reset. LP XTAL is disabled by default
  306. * after device HW reset. Do it only if XTAL is fed by internal source.
  307. * Configure device's "persistence" mode to avoid resetting XTAL again when
  308. * SHRD_HW_RST occurs in S3.
  309. */
  310. static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
  311. {
  312. int ret;
  313. u32 apmg_gp1_reg;
  314. u32 apmg_xtal_cfg_reg;
  315. u32 dl_cfg_reg;
  316. /* Force XTAL ON */
  317. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  318. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  319. /* Reset entire device - do controller reset (results in SHRD_HW_RST) */
  320. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  321. usleep_range(1000, 2000);
  322. /*
  323. * Set "initialization complete" bit to move adapter from
  324. * D0U* --> D0A* (powered-up active) state.
  325. */
  326. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  327. /*
  328. * Wait for clock stabilization; once stabilized, access to
  329. * device-internal resources is possible.
  330. */
  331. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  332. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  333. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  334. 25000);
  335. if (WARN_ON(ret < 0)) {
  336. IWL_ERR(trans, "Access time out - failed to enable LP XTAL\n");
  337. /* Release XTAL ON request */
  338. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  339. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  340. return;
  341. }
  342. /*
  343. * Clear "disable persistence" to avoid LP XTAL resetting when
  344. * SHRD_HW_RST is applied in S3.
  345. */
  346. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  347. APMG_PCIDEV_STT_VAL_PERSIST_DIS);
  348. /*
  349. * Force APMG XTAL to be active to prevent its disabling by HW
  350. * caused by APMG idle state.
  351. */
  352. apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
  353. SHR_APMG_XTAL_CFG_REG);
  354. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  355. apmg_xtal_cfg_reg |
  356. SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  357. /*
  358. * Reset entire device again - do controller reset (results in
  359. * SHRD_HW_RST). Turn MAC off before proceeding.
  360. */
  361. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  362. usleep_range(1000, 2000);
  363. /* Enable LP XTAL by indirect access through CSR */
  364. apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
  365. iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
  366. SHR_APMG_GP1_WF_XTAL_LP_EN |
  367. SHR_APMG_GP1_CHICKEN_BIT_SELECT);
  368. /* Clear delay line clock power up */
  369. dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
  370. iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
  371. ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
  372. /*
  373. * Enable persistence mode to avoid LP XTAL resetting when
  374. * SHRD_HW_RST is applied in S3.
  375. */
  376. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  377. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  378. /*
  379. * Clear "initialization complete" bit to move adapter from
  380. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  381. */
  382. iwl_clear_bit(trans, CSR_GP_CNTRL,
  383. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  384. /* Activates XTAL resources monitor */
  385. __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG,
  386. CSR_MONITOR_XTAL_RESOURCES);
  387. /* Release XTAL ON request */
  388. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  389. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  390. udelay(10);
  391. /* Release APMG XTAL */
  392. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  393. apmg_xtal_cfg_reg &
  394. ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  395. }
  396. static int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
  397. {
  398. int ret = 0;
  399. /* stop device's busmaster DMA activity */
  400. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  401. ret = iwl_poll_bit(trans, CSR_RESET,
  402. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  403. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  404. if (ret < 0)
  405. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  406. IWL_DEBUG_INFO(trans, "stop master\n");
  407. return ret;
  408. }
  409. static void iwl_pcie_apm_stop(struct iwl_trans *trans, bool op_mode_leave)
  410. {
  411. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  412. if (op_mode_leave) {
  413. if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  414. iwl_pcie_apm_init(trans);
  415. /* inform ME that we are leaving */
  416. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000)
  417. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  418. APMG_PCIDEV_STT_VAL_WAKE_ME);
  419. else if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
  420. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  421. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  422. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  423. CSR_HW_IF_CONFIG_REG_PREPARE |
  424. CSR_HW_IF_CONFIG_REG_ENABLE_PME);
  425. mdelay(1);
  426. iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  427. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  428. }
  429. mdelay(5);
  430. }
  431. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  432. /* Stop device's DMA activity */
  433. iwl_pcie_apm_stop_master(trans);
  434. if (trans->cfg->lp_xtal_workaround) {
  435. iwl_pcie_apm_lp_xtal_enable(trans);
  436. return;
  437. }
  438. /* Reset the entire device */
  439. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  440. usleep_range(1000, 2000);
  441. /*
  442. * Clear "initialization complete" bit to move adapter from
  443. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  444. */
  445. iwl_clear_bit(trans, CSR_GP_CNTRL,
  446. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  447. }
  448. static int iwl_pcie_nic_init(struct iwl_trans *trans)
  449. {
  450. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  451. /* nic_init */
  452. spin_lock(&trans_pcie->irq_lock);
  453. iwl_pcie_apm_init(trans);
  454. spin_unlock(&trans_pcie->irq_lock);
  455. iwl_pcie_set_pwr(trans, false);
  456. iwl_op_mode_nic_config(trans->op_mode);
  457. /* Allocate the RX queue, or reset if it is already allocated */
  458. iwl_pcie_rx_init(trans);
  459. /* Allocate or reset and init all Tx and Command queues */
  460. if (iwl_pcie_tx_init(trans))
  461. return -ENOMEM;
  462. if (trans->cfg->base_params->shadow_reg_enable) {
  463. /* enable shadow regs in HW */
  464. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  465. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  466. }
  467. return 0;
  468. }
  469. #define HW_READY_TIMEOUT (50)
  470. /* Note: returns poll_bit return value, which is >= 0 if success */
  471. static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
  472. {
  473. int ret;
  474. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  475. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  476. /* See if we got it */
  477. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  478. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  479. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  480. HW_READY_TIMEOUT);
  481. if (ret >= 0)
  482. iwl_set_bit(trans, CSR_MBOX_SET_REG, CSR_MBOX_SET_REG_OS_ALIVE);
  483. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  484. return ret;
  485. }
  486. /* Note: returns standard 0/-ERROR code */
  487. static int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
  488. {
  489. int ret;
  490. int t = 0;
  491. int iter;
  492. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  493. ret = iwl_pcie_set_hw_ready(trans);
  494. /* If the card is ready, exit 0 */
  495. if (ret >= 0)
  496. return 0;
  497. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  498. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  499. usleep_range(1000, 2000);
  500. for (iter = 0; iter < 10; iter++) {
  501. /* If HW is not ready, prepare the conditions to check again */
  502. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  503. CSR_HW_IF_CONFIG_REG_PREPARE);
  504. do {
  505. ret = iwl_pcie_set_hw_ready(trans);
  506. if (ret >= 0)
  507. return 0;
  508. usleep_range(200, 1000);
  509. t += 200;
  510. } while (t < 150000);
  511. msleep(25);
  512. }
  513. IWL_ERR(trans, "Couldn't prepare the card\n");
  514. return ret;
  515. }
  516. /*
  517. * ucode
  518. */
  519. static void iwl_pcie_load_firmware_chunk_fh(struct iwl_trans *trans,
  520. u32 dst_addr, dma_addr_t phy_addr,
  521. u32 byte_cnt)
  522. {
  523. iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  524. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  525. iwl_write32(trans, FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  526. dst_addr);
  527. iwl_write32(trans, FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  528. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  529. iwl_write32(trans, FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  530. (iwl_get_dma_hi_addr(phy_addr)
  531. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  532. iwl_write32(trans, FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  533. BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM) |
  534. BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX) |
  535. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  536. iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  537. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  538. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  539. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  540. }
  541. static void iwl_pcie_load_firmware_chunk_tfh(struct iwl_trans *trans,
  542. u32 dst_addr, dma_addr_t phy_addr,
  543. u32 byte_cnt)
  544. {
  545. /* Stop DMA channel */
  546. iwl_write32(trans, TFH_SRV_DMA_CHNL0_CTRL, 0);
  547. /* Configure SRAM address */
  548. iwl_write32(trans, TFH_SRV_DMA_CHNL0_SRAM_ADDR,
  549. dst_addr);
  550. /* Configure DRAM address - 64 bit */
  551. iwl_write64(trans, TFH_SRV_DMA_CHNL0_DRAM_ADDR, phy_addr);
  552. /* Configure byte count to transfer */
  553. iwl_write32(trans, TFH_SRV_DMA_CHNL0_BC, byte_cnt);
  554. /* Enable the DRAM2SRAM to start */
  555. iwl_write32(trans, TFH_SRV_DMA_CHNL0_CTRL, TFH_SRV_DMA_SNOOP |
  556. TFH_SRV_DMA_TO_DRIVER |
  557. TFH_SRV_DMA_START);
  558. }
  559. static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans,
  560. u32 dst_addr, dma_addr_t phy_addr,
  561. u32 byte_cnt)
  562. {
  563. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  564. unsigned long flags;
  565. int ret;
  566. trans_pcie->ucode_write_complete = false;
  567. if (!iwl_trans_grab_nic_access(trans, &flags))
  568. return -EIO;
  569. if (trans->cfg->use_tfh)
  570. iwl_pcie_load_firmware_chunk_tfh(trans, dst_addr, phy_addr,
  571. byte_cnt);
  572. else
  573. iwl_pcie_load_firmware_chunk_fh(trans, dst_addr, phy_addr,
  574. byte_cnt);
  575. iwl_trans_release_nic_access(trans, &flags);
  576. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  577. trans_pcie->ucode_write_complete, 5 * HZ);
  578. if (!ret) {
  579. IWL_ERR(trans, "Failed to load firmware chunk!\n");
  580. return -ETIMEDOUT;
  581. }
  582. return 0;
  583. }
  584. static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
  585. const struct fw_desc *section)
  586. {
  587. u8 *v_addr;
  588. dma_addr_t p_addr;
  589. u32 offset, chunk_sz = min_t(u32, FH_MEM_TB_MAX_LENGTH, section->len);
  590. int ret = 0;
  591. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  592. section_num);
  593. v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
  594. GFP_KERNEL | __GFP_NOWARN);
  595. if (!v_addr) {
  596. IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
  597. chunk_sz = PAGE_SIZE;
  598. v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
  599. &p_addr, GFP_KERNEL);
  600. if (!v_addr)
  601. return -ENOMEM;
  602. }
  603. for (offset = 0; offset < section->len; offset += chunk_sz) {
  604. u32 copy_size, dst_addr;
  605. bool extended_addr = false;
  606. copy_size = min_t(u32, chunk_sz, section->len - offset);
  607. dst_addr = section->offset + offset;
  608. if (dst_addr >= IWL_FW_MEM_EXTENDED_START &&
  609. dst_addr <= IWL_FW_MEM_EXTENDED_END)
  610. extended_addr = true;
  611. if (extended_addr)
  612. iwl_set_bits_prph(trans, LMPM_CHICK,
  613. LMPM_CHICK_EXTENDED_ADDR_SPACE);
  614. memcpy(v_addr, (u8 *)section->data + offset, copy_size);
  615. ret = iwl_pcie_load_firmware_chunk(trans, dst_addr, p_addr,
  616. copy_size);
  617. if (extended_addr)
  618. iwl_clear_bits_prph(trans, LMPM_CHICK,
  619. LMPM_CHICK_EXTENDED_ADDR_SPACE);
  620. if (ret) {
  621. IWL_ERR(trans,
  622. "Could not load the [%d] uCode section\n",
  623. section_num);
  624. break;
  625. }
  626. }
  627. dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
  628. return ret;
  629. }
  630. /*
  631. * Driver Takes the ownership on secure machine before FW load
  632. * and prevent race with the BT load.
  633. * W/A for ROM bug. (should be remove in the next Si step)
  634. */
  635. static int iwl_pcie_rsa_race_bug_wa(struct iwl_trans *trans)
  636. {
  637. u32 val, loop = 1000;
  638. /*
  639. * Check the RSA semaphore is accessible.
  640. * If the HW isn't locked and the rsa semaphore isn't accessible,
  641. * we are in trouble.
  642. */
  643. val = iwl_read_prph(trans, PREG_AUX_BUS_WPROT_0);
  644. if (val & (BIT(1) | BIT(17))) {
  645. IWL_DEBUG_INFO(trans,
  646. "can't access the RSA semaphore it is write protected\n");
  647. return 0;
  648. }
  649. /* take ownership on the AUX IF */
  650. iwl_write_prph(trans, WFPM_CTRL_REG, WFPM_AUX_CTL_AUX_IF_MAC_OWNER_MSK);
  651. iwl_write_prph(trans, AUX_MISC_MASTER1_EN, AUX_MISC_MASTER1_EN_SBE_MSK);
  652. do {
  653. iwl_write_prph(trans, AUX_MISC_MASTER1_SMPHR_STATUS, 0x1);
  654. val = iwl_read_prph(trans, AUX_MISC_MASTER1_SMPHR_STATUS);
  655. if (val == 0x1) {
  656. iwl_write_prph(trans, RSA_ENABLE, 0);
  657. return 0;
  658. }
  659. udelay(10);
  660. loop--;
  661. } while (loop > 0);
  662. IWL_ERR(trans, "Failed to take ownership on secure machine\n");
  663. return -EIO;
  664. }
  665. static int iwl_pcie_load_cpu_sections_8000(struct iwl_trans *trans,
  666. const struct fw_img *image,
  667. int cpu,
  668. int *first_ucode_section)
  669. {
  670. int shift_param;
  671. int i, ret = 0, sec_num = 0x1;
  672. u32 val, last_read_idx = 0;
  673. if (cpu == 1) {
  674. shift_param = 0;
  675. *first_ucode_section = 0;
  676. } else {
  677. shift_param = 16;
  678. (*first_ucode_section)++;
  679. }
  680. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  681. last_read_idx = i;
  682. /*
  683. * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
  684. * CPU1 to CPU2.
  685. * PAGING_SEPARATOR_SECTION delimiter - separate between
  686. * CPU2 non paged to CPU2 paging sec.
  687. */
  688. if (!image->sec[i].data ||
  689. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
  690. image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
  691. IWL_DEBUG_FW(trans,
  692. "Break since Data not valid or Empty section, sec = %d\n",
  693. i);
  694. break;
  695. }
  696. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  697. if (ret)
  698. return ret;
  699. /* Notify the ucode of the loaded section number and status */
  700. val = iwl_read_direct32(trans, FH_UCODE_LOAD_STATUS);
  701. val = val | (sec_num << shift_param);
  702. iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, val);
  703. sec_num = (sec_num << 1) | 0x1;
  704. }
  705. *first_ucode_section = last_read_idx;
  706. iwl_enable_interrupts(trans);
  707. if (cpu == 1)
  708. iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, 0xFFFF);
  709. else
  710. iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, 0xFFFFFFFF);
  711. return 0;
  712. }
  713. static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
  714. const struct fw_img *image,
  715. int cpu,
  716. int *first_ucode_section)
  717. {
  718. int shift_param;
  719. int i, ret = 0;
  720. u32 last_read_idx = 0;
  721. if (cpu == 1) {
  722. shift_param = 0;
  723. *first_ucode_section = 0;
  724. } else {
  725. shift_param = 16;
  726. (*first_ucode_section)++;
  727. }
  728. for (i = *first_ucode_section; i < IWL_UCODE_SECTION_MAX; i++) {
  729. last_read_idx = i;
  730. /*
  731. * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
  732. * CPU1 to CPU2.
  733. * PAGING_SEPARATOR_SECTION delimiter - separate between
  734. * CPU2 non paged to CPU2 paging sec.
  735. */
  736. if (!image->sec[i].data ||
  737. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
  738. image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
  739. IWL_DEBUG_FW(trans,
  740. "Break since Data not valid or Empty section, sec = %d\n",
  741. i);
  742. break;
  743. }
  744. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  745. if (ret)
  746. return ret;
  747. }
  748. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  749. iwl_set_bits_prph(trans,
  750. CSR_UCODE_LOAD_STATUS_ADDR,
  751. (LMPM_CPU_UCODE_LOADING_COMPLETED |
  752. LMPM_CPU_HDRS_LOADING_COMPLETED |
  753. LMPM_CPU_UCODE_LOADING_STARTED) <<
  754. shift_param);
  755. *first_ucode_section = last_read_idx;
  756. return 0;
  757. }
  758. static void iwl_pcie_apply_destination(struct iwl_trans *trans)
  759. {
  760. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  761. const struct iwl_fw_dbg_dest_tlv *dest = trans->dbg_dest_tlv;
  762. int i;
  763. if (dest->version)
  764. IWL_ERR(trans,
  765. "DBG DEST version is %d - expect issues\n",
  766. dest->version);
  767. IWL_INFO(trans, "Applying debug destination %s\n",
  768. get_fw_dbg_mode_string(dest->monitor_mode));
  769. if (dest->monitor_mode == EXTERNAL_MODE)
  770. iwl_pcie_alloc_fw_monitor(trans, dest->size_power);
  771. else
  772. IWL_WARN(trans, "PCI should have external buffer debug\n");
  773. for (i = 0; i < trans->dbg_dest_reg_num; i++) {
  774. u32 addr = le32_to_cpu(dest->reg_ops[i].addr);
  775. u32 val = le32_to_cpu(dest->reg_ops[i].val);
  776. switch (dest->reg_ops[i].op) {
  777. case CSR_ASSIGN:
  778. iwl_write32(trans, addr, val);
  779. break;
  780. case CSR_SETBIT:
  781. iwl_set_bit(trans, addr, BIT(val));
  782. break;
  783. case CSR_CLEARBIT:
  784. iwl_clear_bit(trans, addr, BIT(val));
  785. break;
  786. case PRPH_ASSIGN:
  787. iwl_write_prph(trans, addr, val);
  788. break;
  789. case PRPH_SETBIT:
  790. iwl_set_bits_prph(trans, addr, BIT(val));
  791. break;
  792. case PRPH_CLEARBIT:
  793. iwl_clear_bits_prph(trans, addr, BIT(val));
  794. break;
  795. case PRPH_BLOCKBIT:
  796. if (iwl_read_prph(trans, addr) & BIT(val)) {
  797. IWL_ERR(trans,
  798. "BIT(%u) in address 0x%x is 1, stopping FW configuration\n",
  799. val, addr);
  800. goto monitor;
  801. }
  802. break;
  803. default:
  804. IWL_ERR(trans, "FW debug - unknown OP %d\n",
  805. dest->reg_ops[i].op);
  806. break;
  807. }
  808. }
  809. monitor:
  810. if (dest->monitor_mode == EXTERNAL_MODE && trans_pcie->fw_mon_size) {
  811. iwl_write_prph(trans, le32_to_cpu(dest->base_reg),
  812. trans_pcie->fw_mon_phys >> dest->base_shift);
  813. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  814. iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
  815. (trans_pcie->fw_mon_phys +
  816. trans_pcie->fw_mon_size - 256) >>
  817. dest->end_shift);
  818. else
  819. iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
  820. (trans_pcie->fw_mon_phys +
  821. trans_pcie->fw_mon_size) >>
  822. dest->end_shift);
  823. }
  824. }
  825. static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
  826. const struct fw_img *image)
  827. {
  828. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  829. int ret = 0;
  830. int first_ucode_section;
  831. IWL_DEBUG_FW(trans, "working with %s CPU\n",
  832. image->is_dual_cpus ? "Dual" : "Single");
  833. /* load to FW the binary non secured sections of CPU1 */
  834. ret = iwl_pcie_load_cpu_sections(trans, image, 1, &first_ucode_section);
  835. if (ret)
  836. return ret;
  837. if (image->is_dual_cpus) {
  838. /* set CPU2 header address */
  839. iwl_write_prph(trans,
  840. LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
  841. LMPM_SECURE_CPU2_HDR_MEM_SPACE);
  842. /* load to FW the binary sections of CPU2 */
  843. ret = iwl_pcie_load_cpu_sections(trans, image, 2,
  844. &first_ucode_section);
  845. if (ret)
  846. return ret;
  847. }
  848. /* supported for 7000 only for the moment */
  849. if (iwlwifi_mod_params.fw_monitor &&
  850. trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  851. iwl_pcie_alloc_fw_monitor(trans, 0);
  852. if (trans_pcie->fw_mon_size) {
  853. iwl_write_prph(trans, MON_BUFF_BASE_ADDR,
  854. trans_pcie->fw_mon_phys >> 4);
  855. iwl_write_prph(trans, MON_BUFF_END_ADDR,
  856. (trans_pcie->fw_mon_phys +
  857. trans_pcie->fw_mon_size) >> 4);
  858. }
  859. } else if (trans->dbg_dest_tlv) {
  860. iwl_pcie_apply_destination(trans);
  861. }
  862. iwl_enable_interrupts(trans);
  863. /* release CPU reset */
  864. iwl_write32(trans, CSR_RESET, 0);
  865. return 0;
  866. }
  867. static int iwl_pcie_load_given_ucode_8000(struct iwl_trans *trans,
  868. const struct fw_img *image)
  869. {
  870. int ret = 0;
  871. int first_ucode_section;
  872. IWL_DEBUG_FW(trans, "working with %s CPU\n",
  873. image->is_dual_cpus ? "Dual" : "Single");
  874. if (trans->dbg_dest_tlv)
  875. iwl_pcie_apply_destination(trans);
  876. /* TODO: remove in the next Si step */
  877. ret = iwl_pcie_rsa_race_bug_wa(trans);
  878. if (ret)
  879. return ret;
  880. /* configure the ucode to be ready to get the secured image */
  881. /* release CPU reset */
  882. iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
  883. /* load to FW the binary Secured sections of CPU1 */
  884. ret = iwl_pcie_load_cpu_sections_8000(trans, image, 1,
  885. &first_ucode_section);
  886. if (ret)
  887. return ret;
  888. /* load to FW the binary sections of CPU2 */
  889. return iwl_pcie_load_cpu_sections_8000(trans, image, 2,
  890. &first_ucode_section);
  891. }
  892. static void _iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power)
  893. {
  894. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  895. bool hw_rfkill, was_hw_rfkill;
  896. lockdep_assert_held(&trans_pcie->mutex);
  897. if (trans_pcie->is_down)
  898. return;
  899. trans_pcie->is_down = true;
  900. was_hw_rfkill = iwl_is_rfkill_set(trans);
  901. /* tell the device to stop sending interrupts */
  902. iwl_disable_interrupts(trans);
  903. /* device going down, Stop using ICT table */
  904. iwl_pcie_disable_ict(trans);
  905. /*
  906. * If a HW restart happens during firmware loading,
  907. * then the firmware loading might call this function
  908. * and later it might be called again due to the
  909. * restart. So don't process again if the device is
  910. * already dead.
  911. */
  912. if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
  913. IWL_DEBUG_INFO(trans,
  914. "DEVICE_ENABLED bit was set and is now cleared\n");
  915. iwl_pcie_tx_stop(trans);
  916. iwl_pcie_rx_stop(trans);
  917. /* Power-down device's busmaster DMA clocks */
  918. if (!trans->cfg->apmg_not_supported) {
  919. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  920. APMG_CLK_VAL_DMA_CLK_RQT);
  921. udelay(5);
  922. }
  923. }
  924. /* Make sure (redundant) we've released our request to stay awake */
  925. iwl_clear_bit(trans, CSR_GP_CNTRL,
  926. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  927. /* Stop the device, and put it in low power state */
  928. iwl_pcie_apm_stop(trans, false);
  929. /* stop and reset the on-board processor */
  930. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  931. usleep_range(1000, 2000);
  932. /*
  933. * Upon stop, the APM issues an interrupt if HW RF kill is set.
  934. * This is a bug in certain verions of the hardware.
  935. * Certain devices also keep sending HW RF kill interrupt all
  936. * the time, unless the interrupt is ACKed even if the interrupt
  937. * should be masked. Re-ACK all the interrupts here.
  938. */
  939. iwl_disable_interrupts(trans);
  940. /* clear all status bits */
  941. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  942. clear_bit(STATUS_INT_ENABLED, &trans->status);
  943. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  944. clear_bit(STATUS_RFKILL, &trans->status);
  945. /*
  946. * Even if we stop the HW, we still want the RF kill
  947. * interrupt
  948. */
  949. iwl_enable_rfkill_int(trans);
  950. /*
  951. * Check again since the RF kill state may have changed while
  952. * all the interrupts were disabled, in this case we couldn't
  953. * receive the RF kill interrupt and update the state in the
  954. * op_mode.
  955. * Don't call the op_mode if the rkfill state hasn't changed.
  956. * This allows the op_mode to call stop_device from the rfkill
  957. * notification without endless recursion. Under very rare
  958. * circumstances, we might have a small recursion if the rfkill
  959. * state changed exactly now while we were called from stop_device.
  960. * This is very unlikely but can happen and is supported.
  961. */
  962. hw_rfkill = iwl_is_rfkill_set(trans);
  963. if (hw_rfkill)
  964. set_bit(STATUS_RFKILL, &trans->status);
  965. else
  966. clear_bit(STATUS_RFKILL, &trans->status);
  967. if (hw_rfkill != was_hw_rfkill)
  968. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  969. /* re-take ownership to prevent other users from stealing the device */
  970. iwl_pcie_prepare_card_hw(trans);
  971. }
  972. static void iwl_pcie_synchronize_irqs(struct iwl_trans *trans)
  973. {
  974. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  975. if (trans_pcie->msix_enabled) {
  976. int i;
  977. for (i = 0; i < trans_pcie->allocated_vector; i++)
  978. synchronize_irq(trans_pcie->msix_entries[i].vector);
  979. } else {
  980. synchronize_irq(trans_pcie->pci_dev->irq);
  981. }
  982. }
  983. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  984. const struct fw_img *fw, bool run_in_rfkill)
  985. {
  986. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  987. bool hw_rfkill;
  988. int ret;
  989. /* This may fail if AMT took ownership of the device */
  990. if (iwl_pcie_prepare_card_hw(trans)) {
  991. IWL_WARN(trans, "Exit HW not ready\n");
  992. ret = -EIO;
  993. goto out;
  994. }
  995. iwl_enable_rfkill_int(trans);
  996. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  997. /*
  998. * We enabled the RF-Kill interrupt and the handler may very
  999. * well be running. Disable the interrupts to make sure no other
  1000. * interrupt can be fired.
  1001. */
  1002. iwl_disable_interrupts(trans);
  1003. /* Make sure it finished running */
  1004. iwl_pcie_synchronize_irqs(trans);
  1005. mutex_lock(&trans_pcie->mutex);
  1006. /* If platform's RF_KILL switch is NOT set to KILL */
  1007. hw_rfkill = iwl_is_rfkill_set(trans);
  1008. if (hw_rfkill)
  1009. set_bit(STATUS_RFKILL, &trans->status);
  1010. else
  1011. clear_bit(STATUS_RFKILL, &trans->status);
  1012. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  1013. if (hw_rfkill && !run_in_rfkill) {
  1014. ret = -ERFKILL;
  1015. goto out;
  1016. }
  1017. /* Someone called stop_device, don't try to start_fw */
  1018. if (trans_pcie->is_down) {
  1019. IWL_WARN(trans,
  1020. "Can't start_fw since the HW hasn't been started\n");
  1021. ret = -EIO;
  1022. goto out;
  1023. }
  1024. /* make sure rfkill handshake bits are cleared */
  1025. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1026. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  1027. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  1028. /* clear (again), then enable host interrupts */
  1029. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  1030. ret = iwl_pcie_nic_init(trans);
  1031. if (ret) {
  1032. IWL_ERR(trans, "Unable to init nic\n");
  1033. goto out;
  1034. }
  1035. /*
  1036. * Now, we load the firmware and don't want to be interrupted, even
  1037. * by the RF-Kill interrupt (hence mask all the interrupt besides the
  1038. * FH_TX interrupt which is needed to load the firmware). If the
  1039. * RF-Kill switch is toggled, we will find out after having loaded
  1040. * the firmware and return the proper value to the caller.
  1041. */
  1042. iwl_enable_fw_load_int(trans);
  1043. /* really make sure rfkill handshake bits are cleared */
  1044. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1045. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1046. /* Load the given image to the HW */
  1047. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1048. ret = iwl_pcie_load_given_ucode_8000(trans, fw);
  1049. else
  1050. ret = iwl_pcie_load_given_ucode(trans, fw);
  1051. /* re-check RF-Kill state since we may have missed the interrupt */
  1052. hw_rfkill = iwl_is_rfkill_set(trans);
  1053. if (hw_rfkill)
  1054. set_bit(STATUS_RFKILL, &trans->status);
  1055. else
  1056. clear_bit(STATUS_RFKILL, &trans->status);
  1057. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  1058. if (hw_rfkill && !run_in_rfkill)
  1059. ret = -ERFKILL;
  1060. out:
  1061. mutex_unlock(&trans_pcie->mutex);
  1062. return ret;
  1063. }
  1064. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  1065. {
  1066. iwl_pcie_reset_ict(trans);
  1067. iwl_pcie_tx_start(trans, scd_addr);
  1068. }
  1069. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power)
  1070. {
  1071. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1072. mutex_lock(&trans_pcie->mutex);
  1073. _iwl_trans_pcie_stop_device(trans, low_power);
  1074. mutex_unlock(&trans_pcie->mutex);
  1075. }
  1076. void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state)
  1077. {
  1078. struct iwl_trans_pcie __maybe_unused *trans_pcie =
  1079. IWL_TRANS_GET_PCIE_TRANS(trans);
  1080. lockdep_assert_held(&trans_pcie->mutex);
  1081. if (iwl_op_mode_hw_rf_kill(trans->op_mode, state))
  1082. _iwl_trans_pcie_stop_device(trans, true);
  1083. }
  1084. static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test,
  1085. bool reset)
  1086. {
  1087. if (!reset) {
  1088. /* Enable persistence mode to avoid reset */
  1089. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  1090. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  1091. }
  1092. iwl_disable_interrupts(trans);
  1093. /*
  1094. * in testing mode, the host stays awake and the
  1095. * hardware won't be reset (not even partially)
  1096. */
  1097. if (test)
  1098. return;
  1099. iwl_pcie_disable_ict(trans);
  1100. iwl_pcie_synchronize_irqs(trans);
  1101. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1102. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1103. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1104. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  1105. iwl_pcie_enable_rx_wake(trans, false);
  1106. if (reset) {
  1107. /*
  1108. * reset TX queues -- some of their registers reset during S3
  1109. * so if we don't reset everything here the D3 image would try
  1110. * to execute some invalid memory upon resume
  1111. */
  1112. iwl_trans_pcie_tx_reset(trans);
  1113. }
  1114. iwl_pcie_set_pwr(trans, true);
  1115. }
  1116. static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
  1117. enum iwl_d3_status *status,
  1118. bool test, bool reset)
  1119. {
  1120. u32 val;
  1121. int ret;
  1122. if (test) {
  1123. iwl_enable_interrupts(trans);
  1124. *status = IWL_D3_STATUS_ALIVE;
  1125. return 0;
  1126. }
  1127. iwl_pcie_enable_rx_wake(trans, true);
  1128. /*
  1129. * Also enables interrupts - none will happen as the device doesn't
  1130. * know we're waking it up, only when the opmode actually tells it
  1131. * after this call.
  1132. */
  1133. iwl_pcie_reset_ict(trans);
  1134. iwl_enable_interrupts(trans);
  1135. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1136. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  1137. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1138. udelay(2);
  1139. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  1140. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  1141. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  1142. 25000);
  1143. if (ret < 0) {
  1144. IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
  1145. return ret;
  1146. }
  1147. iwl_pcie_set_pwr(trans, false);
  1148. if (!reset) {
  1149. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1150. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1151. } else {
  1152. iwl_trans_pcie_tx_reset(trans);
  1153. ret = iwl_pcie_rx_init(trans);
  1154. if (ret) {
  1155. IWL_ERR(trans,
  1156. "Failed to resume the device (RX reset)\n");
  1157. return ret;
  1158. }
  1159. }
  1160. val = iwl_read32(trans, CSR_RESET);
  1161. if (val & CSR_RESET_REG_FLAG_NEVO_RESET)
  1162. *status = IWL_D3_STATUS_RESET;
  1163. else
  1164. *status = IWL_D3_STATUS_ALIVE;
  1165. return 0;
  1166. }
  1167. struct iwl_causes_list {
  1168. u32 cause_num;
  1169. u32 mask_reg;
  1170. u8 addr;
  1171. };
  1172. static struct iwl_causes_list causes_list[] = {
  1173. {MSIX_FH_INT_CAUSES_D2S_CH0_NUM, CSR_MSIX_FH_INT_MASK_AD, 0},
  1174. {MSIX_FH_INT_CAUSES_D2S_CH1_NUM, CSR_MSIX_FH_INT_MASK_AD, 0x1},
  1175. {MSIX_FH_INT_CAUSES_S2D, CSR_MSIX_FH_INT_MASK_AD, 0x3},
  1176. {MSIX_FH_INT_CAUSES_FH_ERR, CSR_MSIX_FH_INT_MASK_AD, 0x5},
  1177. {MSIX_HW_INT_CAUSES_REG_ALIVE, CSR_MSIX_HW_INT_MASK_AD, 0x10},
  1178. {MSIX_HW_INT_CAUSES_REG_WAKEUP, CSR_MSIX_HW_INT_MASK_AD, 0x11},
  1179. {MSIX_HW_INT_CAUSES_REG_CT_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x16},
  1180. {MSIX_HW_INT_CAUSES_REG_RF_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x17},
  1181. {MSIX_HW_INT_CAUSES_REG_PERIODIC, CSR_MSIX_HW_INT_MASK_AD, 0x18},
  1182. {MSIX_HW_INT_CAUSES_REG_SW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x29},
  1183. {MSIX_HW_INT_CAUSES_REG_SCD, CSR_MSIX_HW_INT_MASK_AD, 0x2A},
  1184. {MSIX_HW_INT_CAUSES_REG_FH_TX, CSR_MSIX_HW_INT_MASK_AD, 0x2B},
  1185. {MSIX_HW_INT_CAUSES_REG_HW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x2D},
  1186. {MSIX_HW_INT_CAUSES_REG_HAP, CSR_MSIX_HW_INT_MASK_AD, 0x2E},
  1187. };
  1188. static void iwl_pcie_init_msix(struct iwl_trans_pcie *trans_pcie)
  1189. {
  1190. u32 val, max_rx_vector, i;
  1191. struct iwl_trans *trans = trans_pcie->trans;
  1192. max_rx_vector = trans_pcie->allocated_vector - 1;
  1193. if (!trans_pcie->msix_enabled) {
  1194. if (trans->cfg->mq_rx_supported)
  1195. iwl_write_prph(trans, UREG_CHICK,
  1196. UREG_CHICK_MSI_ENABLE);
  1197. return;
  1198. }
  1199. iwl_write_prph(trans, UREG_CHICK, UREG_CHICK_MSIX_ENABLE);
  1200. /*
  1201. * Each cause from the list above and the RX causes is represented as
  1202. * a byte in the IVAR table. We access the first (N - 1) bytes and map
  1203. * them to the (N - 1) vectors so these vectors will be used as rx
  1204. * vectors. Then access all non rx causes and map them to the
  1205. * default queue (N'th queue).
  1206. */
  1207. for (i = 0; i < max_rx_vector; i++) {
  1208. iwl_write8(trans, CSR_MSIX_RX_IVAR(i), MSIX_FH_INT_CAUSES_Q(i));
  1209. iwl_clear_bit(trans, CSR_MSIX_FH_INT_MASK_AD,
  1210. BIT(MSIX_FH_INT_CAUSES_Q(i)));
  1211. }
  1212. for (i = 0; i < ARRAY_SIZE(causes_list); i++) {
  1213. val = trans_pcie->default_irq_num |
  1214. MSIX_NON_AUTO_CLEAR_CAUSE;
  1215. iwl_write8(trans, CSR_MSIX_IVAR(causes_list[i].addr), val);
  1216. iwl_clear_bit(trans, causes_list[i].mask_reg,
  1217. causes_list[i].cause_num);
  1218. }
  1219. trans_pcie->fh_init_mask =
  1220. ~iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD);
  1221. trans_pcie->fh_mask = trans_pcie->fh_init_mask;
  1222. trans_pcie->hw_init_mask =
  1223. ~iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD);
  1224. trans_pcie->hw_mask = trans_pcie->hw_init_mask;
  1225. }
  1226. static void iwl_pcie_set_interrupt_capa(struct pci_dev *pdev,
  1227. struct iwl_trans *trans)
  1228. {
  1229. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1230. u16 pci_cmd;
  1231. int max_vector;
  1232. int ret, i;
  1233. if (trans->cfg->mq_rx_supported) {
  1234. max_vector = min_t(u32, (num_possible_cpus() + 2),
  1235. IWL_MAX_RX_HW_QUEUES);
  1236. for (i = 0; i < max_vector; i++)
  1237. trans_pcie->msix_entries[i].entry = i;
  1238. ret = pci_enable_msix_range(pdev, trans_pcie->msix_entries,
  1239. MSIX_MIN_INTERRUPT_VECTORS,
  1240. max_vector);
  1241. if (ret > 1) {
  1242. IWL_DEBUG_INFO(trans,
  1243. "Enable MSI-X allocate %d interrupt vector\n",
  1244. ret);
  1245. trans_pcie->allocated_vector = ret;
  1246. trans_pcie->default_irq_num =
  1247. trans_pcie->allocated_vector - 1;
  1248. trans_pcie->trans->num_rx_queues =
  1249. trans_pcie->allocated_vector - 1;
  1250. trans_pcie->msix_enabled = true;
  1251. return;
  1252. }
  1253. IWL_DEBUG_INFO(trans,
  1254. "ret = %d %s move to msi mode\n", ret,
  1255. (ret == 1) ?
  1256. "can't allocate more than 1 interrupt vector" :
  1257. "failed to enable msi-x mode");
  1258. pci_disable_msix(pdev);
  1259. }
  1260. ret = pci_enable_msi(pdev);
  1261. if (ret) {
  1262. dev_err(&pdev->dev, "pci_enable_msi failed - %d\n", ret);
  1263. /* enable rfkill interrupt: hw bug w/a */
  1264. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1265. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1266. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1267. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1268. }
  1269. }
  1270. }
  1271. static int iwl_pcie_init_msix_handler(struct pci_dev *pdev,
  1272. struct iwl_trans_pcie *trans_pcie)
  1273. {
  1274. int i, last_vector;
  1275. last_vector = trans_pcie->trans->num_rx_queues;
  1276. for (i = 0; i < trans_pcie->allocated_vector; i++) {
  1277. int ret;
  1278. ret = request_threaded_irq(trans_pcie->msix_entries[i].vector,
  1279. iwl_pcie_msix_isr,
  1280. (i == last_vector) ?
  1281. iwl_pcie_irq_msix_handler :
  1282. iwl_pcie_irq_rx_msix_handler,
  1283. IRQF_SHARED,
  1284. DRV_NAME,
  1285. &trans_pcie->msix_entries[i]);
  1286. if (ret) {
  1287. int j;
  1288. IWL_ERR(trans_pcie->trans,
  1289. "Error allocating IRQ %d\n", i);
  1290. for (j = 0; j < i; j++)
  1291. free_irq(trans_pcie->msix_entries[j].vector,
  1292. &trans_pcie->msix_entries[j]);
  1293. pci_disable_msix(pdev);
  1294. return ret;
  1295. }
  1296. }
  1297. return 0;
  1298. }
  1299. static int _iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power)
  1300. {
  1301. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1302. bool hw_rfkill;
  1303. int err;
  1304. lockdep_assert_held(&trans_pcie->mutex);
  1305. err = iwl_pcie_prepare_card_hw(trans);
  1306. if (err) {
  1307. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  1308. return err;
  1309. }
  1310. /* Reset the entire device */
  1311. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  1312. usleep_range(1000, 2000);
  1313. iwl_pcie_apm_init(trans);
  1314. iwl_pcie_init_msix(trans_pcie);
  1315. /* From now on, the op_mode will be kept updated about RF kill state */
  1316. iwl_enable_rfkill_int(trans);
  1317. /* Set is_down to false here so that...*/
  1318. trans_pcie->is_down = false;
  1319. hw_rfkill = iwl_is_rfkill_set(trans);
  1320. if (hw_rfkill)
  1321. set_bit(STATUS_RFKILL, &trans->status);
  1322. else
  1323. clear_bit(STATUS_RFKILL, &trans->status);
  1324. /* ... rfkill can call stop_device and set it false if needed */
  1325. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  1326. /* Make sure we sync here, because we'll need full access later */
  1327. if (low_power)
  1328. pm_runtime_resume(trans->dev);
  1329. return 0;
  1330. }
  1331. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power)
  1332. {
  1333. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1334. int ret;
  1335. mutex_lock(&trans_pcie->mutex);
  1336. ret = _iwl_trans_pcie_start_hw(trans, low_power);
  1337. mutex_unlock(&trans_pcie->mutex);
  1338. return ret;
  1339. }
  1340. static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
  1341. {
  1342. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1343. mutex_lock(&trans_pcie->mutex);
  1344. /* disable interrupts - don't enable HW RF kill interrupt */
  1345. iwl_disable_interrupts(trans);
  1346. iwl_pcie_apm_stop(trans, true);
  1347. iwl_disable_interrupts(trans);
  1348. iwl_pcie_disable_ict(trans);
  1349. mutex_unlock(&trans_pcie->mutex);
  1350. iwl_pcie_synchronize_irqs(trans);
  1351. }
  1352. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  1353. {
  1354. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1355. }
  1356. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  1357. {
  1358. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1359. }
  1360. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  1361. {
  1362. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1363. }
  1364. static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
  1365. {
  1366. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
  1367. ((reg & 0x000FFFFF) | (3 << 24)));
  1368. return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
  1369. }
  1370. static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
  1371. u32 val)
  1372. {
  1373. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
  1374. ((addr & 0x000FFFFF) | (3 << 24)));
  1375. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  1376. }
  1377. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  1378. const struct iwl_trans_config *trans_cfg)
  1379. {
  1380. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1381. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  1382. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  1383. trans_pcie->cmd_q_wdg_timeout = trans_cfg->cmd_q_wdg_timeout;
  1384. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  1385. trans_pcie->n_no_reclaim_cmds = 0;
  1386. else
  1387. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  1388. if (trans_pcie->n_no_reclaim_cmds)
  1389. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  1390. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  1391. trans_pcie->rx_buf_size = trans_cfg->rx_buf_size;
  1392. trans_pcie->rx_page_order =
  1393. iwl_trans_get_rb_size_order(trans_pcie->rx_buf_size);
  1394. trans_pcie->wide_cmd_header = trans_cfg->wide_cmd_header;
  1395. trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
  1396. trans_pcie->scd_set_active = trans_cfg->scd_set_active;
  1397. trans_pcie->sw_csum_tx = trans_cfg->sw_csum_tx;
  1398. trans_pcie->page_offs = trans_cfg->cb_data_offs;
  1399. trans_pcie->dev_cmd_offs = trans_cfg->cb_data_offs + sizeof(void *);
  1400. trans->command_groups = trans_cfg->command_groups;
  1401. trans->command_groups_size = trans_cfg->command_groups_size;
  1402. /* Initialize NAPI here - it should be before registering to mac80211
  1403. * in the opmode but after the HW struct is allocated.
  1404. * As this function may be called again in some corner cases don't
  1405. * do anything if NAPI was already initialized.
  1406. */
  1407. if (trans_pcie->napi_dev.reg_state != NETREG_DUMMY)
  1408. init_dummy_netdev(&trans_pcie->napi_dev);
  1409. }
  1410. void iwl_trans_pcie_free(struct iwl_trans *trans)
  1411. {
  1412. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1413. int i;
  1414. iwl_pcie_synchronize_irqs(trans);
  1415. iwl_pcie_tx_free(trans);
  1416. iwl_pcie_rx_free(trans);
  1417. if (trans_pcie->msix_enabled) {
  1418. for (i = 0; i < trans_pcie->allocated_vector; i++)
  1419. free_irq(trans_pcie->msix_entries[i].vector,
  1420. &trans_pcie->msix_entries[i]);
  1421. pci_disable_msix(trans_pcie->pci_dev);
  1422. trans_pcie->msix_enabled = false;
  1423. } else {
  1424. free_irq(trans_pcie->pci_dev->irq, trans);
  1425. iwl_pcie_free_ict(trans);
  1426. pci_disable_msi(trans_pcie->pci_dev);
  1427. }
  1428. iounmap(trans_pcie->hw_base);
  1429. pci_release_regions(trans_pcie->pci_dev);
  1430. pci_disable_device(trans_pcie->pci_dev);
  1431. iwl_pcie_free_fw_monitor(trans);
  1432. for_each_possible_cpu(i) {
  1433. struct iwl_tso_hdr_page *p =
  1434. per_cpu_ptr(trans_pcie->tso_hdr_page, i);
  1435. if (p->page)
  1436. __free_page(p->page);
  1437. }
  1438. free_percpu(trans_pcie->tso_hdr_page);
  1439. mutex_destroy(&trans_pcie->mutex);
  1440. iwl_trans_free(trans);
  1441. }
  1442. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  1443. {
  1444. if (state)
  1445. set_bit(STATUS_TPOWER_PMI, &trans->status);
  1446. else
  1447. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  1448. }
  1449. static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans,
  1450. unsigned long *flags)
  1451. {
  1452. int ret;
  1453. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1454. spin_lock_irqsave(&trans_pcie->reg_lock, *flags);
  1455. if (trans_pcie->cmd_hold_nic_awake)
  1456. goto out;
  1457. /* this bit wakes up the NIC */
  1458. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  1459. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1460. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1461. udelay(2);
  1462. /*
  1463. * These bits say the device is running, and should keep running for
  1464. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  1465. * but they do not indicate that embedded SRAM is restored yet;
  1466. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  1467. * to/from host DRAM when sleeping/waking for power-saving.
  1468. * Each direction takes approximately 1/4 millisecond; with this
  1469. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  1470. * series of register accesses are expected (e.g. reading Event Log),
  1471. * to keep device from sleeping.
  1472. *
  1473. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  1474. * SRAM is okay/restored. We don't check that here because this call
  1475. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  1476. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  1477. *
  1478. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  1479. * and do not save/restore SRAM when power cycling.
  1480. */
  1481. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  1482. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  1483. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  1484. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  1485. if (unlikely(ret < 0)) {
  1486. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  1487. WARN_ONCE(1,
  1488. "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
  1489. iwl_read32(trans, CSR_GP_CNTRL));
  1490. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1491. return false;
  1492. }
  1493. out:
  1494. /*
  1495. * Fool sparse by faking we release the lock - sparse will
  1496. * track nic_access anyway.
  1497. */
  1498. __release(&trans_pcie->reg_lock);
  1499. return true;
  1500. }
  1501. static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans,
  1502. unsigned long *flags)
  1503. {
  1504. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1505. lockdep_assert_held(&trans_pcie->reg_lock);
  1506. /*
  1507. * Fool sparse by faking we acquiring the lock - sparse will
  1508. * track nic_access anyway.
  1509. */
  1510. __acquire(&trans_pcie->reg_lock);
  1511. if (trans_pcie->cmd_hold_nic_awake)
  1512. goto out;
  1513. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  1514. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1515. /*
  1516. * Above we read the CSR_GP_CNTRL register, which will flush
  1517. * any previous writes, but we need the write that clears the
  1518. * MAC_ACCESS_REQ bit to be performed before any other writes
  1519. * scheduled on different CPUs (after we drop reg_lock).
  1520. */
  1521. mmiowb();
  1522. out:
  1523. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1524. }
  1525. static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
  1526. void *buf, int dwords)
  1527. {
  1528. unsigned long flags;
  1529. int offs, ret = 0;
  1530. u32 *vals = buf;
  1531. if (iwl_trans_grab_nic_access(trans, &flags)) {
  1532. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  1533. for (offs = 0; offs < dwords; offs++)
  1534. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  1535. iwl_trans_release_nic_access(trans, &flags);
  1536. } else {
  1537. ret = -EBUSY;
  1538. }
  1539. return ret;
  1540. }
  1541. static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
  1542. const void *buf, int dwords)
  1543. {
  1544. unsigned long flags;
  1545. int offs, ret = 0;
  1546. const u32 *vals = buf;
  1547. if (iwl_trans_grab_nic_access(trans, &flags)) {
  1548. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  1549. for (offs = 0; offs < dwords; offs++)
  1550. iwl_write32(trans, HBUS_TARG_MEM_WDAT,
  1551. vals ? vals[offs] : 0);
  1552. iwl_trans_release_nic_access(trans, &flags);
  1553. } else {
  1554. ret = -EBUSY;
  1555. }
  1556. return ret;
  1557. }
  1558. static void iwl_trans_pcie_freeze_txq_timer(struct iwl_trans *trans,
  1559. unsigned long txqs,
  1560. bool freeze)
  1561. {
  1562. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1563. int queue;
  1564. for_each_set_bit(queue, &txqs, BITS_PER_LONG) {
  1565. struct iwl_txq *txq = &trans_pcie->txq[queue];
  1566. unsigned long now;
  1567. spin_lock_bh(&txq->lock);
  1568. now = jiffies;
  1569. if (txq->frozen == freeze)
  1570. goto next_queue;
  1571. IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n",
  1572. freeze ? "Freezing" : "Waking", queue);
  1573. txq->frozen = freeze;
  1574. if (txq->q.read_ptr == txq->q.write_ptr)
  1575. goto next_queue;
  1576. if (freeze) {
  1577. if (unlikely(time_after(now,
  1578. txq->stuck_timer.expires))) {
  1579. /*
  1580. * The timer should have fired, maybe it is
  1581. * spinning right now on the lock.
  1582. */
  1583. goto next_queue;
  1584. }
  1585. /* remember how long until the timer fires */
  1586. txq->frozen_expiry_remainder =
  1587. txq->stuck_timer.expires - now;
  1588. del_timer(&txq->stuck_timer);
  1589. goto next_queue;
  1590. }
  1591. /*
  1592. * Wake a non-empty queue -> arm timer with the
  1593. * remainder before it froze
  1594. */
  1595. mod_timer(&txq->stuck_timer,
  1596. now + txq->frozen_expiry_remainder);
  1597. next_queue:
  1598. spin_unlock_bh(&txq->lock);
  1599. }
  1600. }
  1601. static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block)
  1602. {
  1603. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1604. int i;
  1605. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  1606. struct iwl_txq *txq = &trans_pcie->txq[i];
  1607. if (i == trans_pcie->cmd_queue)
  1608. continue;
  1609. spin_lock_bh(&txq->lock);
  1610. if (!block && !(WARN_ON_ONCE(!txq->block))) {
  1611. txq->block--;
  1612. if (!txq->block) {
  1613. iwl_write32(trans, HBUS_TARG_WRPTR,
  1614. txq->q.write_ptr | (i << 8));
  1615. }
  1616. } else if (block) {
  1617. txq->block++;
  1618. }
  1619. spin_unlock_bh(&txq->lock);
  1620. }
  1621. }
  1622. #define IWL_FLUSH_WAIT_MS 2000
  1623. void iwl_trans_pcie_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq)
  1624. {
  1625. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1626. u32 scd_sram_addr;
  1627. u8 buf[16];
  1628. int cnt;
  1629. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  1630. txq->q.read_ptr, txq->q.write_ptr);
  1631. scd_sram_addr = trans_pcie->scd_base_addr +
  1632. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  1633. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  1634. iwl_print_hex_error(trans, buf, sizeof(buf));
  1635. for (cnt = 0; cnt < FH_TCSR_CHNL_NUM; cnt++)
  1636. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", cnt,
  1637. iwl_read_direct32(trans, FH_TX_TRB_REG(cnt)));
  1638. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1639. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(cnt));
  1640. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  1641. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  1642. u32 tbl_dw =
  1643. iwl_trans_read_mem32(trans, trans_pcie->scd_base_addr +
  1644. SCD_TRANS_TBL_OFFSET_QUEUE(cnt));
  1645. if (cnt & 0x1)
  1646. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  1647. else
  1648. tbl_dw = tbl_dw & 0x0000FFFF;
  1649. IWL_ERR(trans,
  1650. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  1651. cnt, active ? "" : "in", fifo, tbl_dw,
  1652. iwl_read_prph(trans, SCD_QUEUE_RDPTR(cnt)) &
  1653. (TFD_QUEUE_SIZE_MAX - 1),
  1654. iwl_read_prph(trans, SCD_QUEUE_WRPTR(cnt)));
  1655. }
  1656. }
  1657. static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, u32 txq_bm)
  1658. {
  1659. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1660. struct iwl_txq *txq;
  1661. struct iwl_queue *q;
  1662. int cnt;
  1663. unsigned long now = jiffies;
  1664. int ret = 0;
  1665. /* waiting for all the tx frames complete might take a while */
  1666. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1667. u8 wr_ptr;
  1668. if (cnt == trans_pcie->cmd_queue)
  1669. continue;
  1670. if (!test_bit(cnt, trans_pcie->queue_used))
  1671. continue;
  1672. if (!(BIT(cnt) & txq_bm))
  1673. continue;
  1674. IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", cnt);
  1675. txq = &trans_pcie->txq[cnt];
  1676. q = &txq->q;
  1677. wr_ptr = ACCESS_ONCE(q->write_ptr);
  1678. while (q->read_ptr != ACCESS_ONCE(q->write_ptr) &&
  1679. !time_after(jiffies,
  1680. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
  1681. u8 write_ptr = ACCESS_ONCE(q->write_ptr);
  1682. if (WARN_ONCE(wr_ptr != write_ptr,
  1683. "WR pointer moved while flushing %d -> %d\n",
  1684. wr_ptr, write_ptr))
  1685. return -ETIMEDOUT;
  1686. usleep_range(1000, 2000);
  1687. }
  1688. if (q->read_ptr != q->write_ptr) {
  1689. IWL_ERR(trans,
  1690. "fail to flush all tx fifo queues Q %d\n", cnt);
  1691. ret = -ETIMEDOUT;
  1692. break;
  1693. }
  1694. IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", cnt);
  1695. }
  1696. if (ret)
  1697. iwl_trans_pcie_log_scd_error(trans, txq);
  1698. return ret;
  1699. }
  1700. static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
  1701. u32 mask, u32 value)
  1702. {
  1703. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1704. unsigned long flags;
  1705. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1706. __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value);
  1707. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1708. }
  1709. static void iwl_trans_pcie_ref(struct iwl_trans *trans)
  1710. {
  1711. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1712. if (iwlwifi_mod_params.d0i3_disable)
  1713. return;
  1714. pm_runtime_get(&trans_pcie->pci_dev->dev);
  1715. #ifdef CONFIG_PM
  1716. IWL_DEBUG_RPM(trans, "runtime usage count: %d\n",
  1717. atomic_read(&trans_pcie->pci_dev->dev.power.usage_count));
  1718. #endif /* CONFIG_PM */
  1719. }
  1720. static void iwl_trans_pcie_unref(struct iwl_trans *trans)
  1721. {
  1722. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1723. if (iwlwifi_mod_params.d0i3_disable)
  1724. return;
  1725. pm_runtime_mark_last_busy(&trans_pcie->pci_dev->dev);
  1726. pm_runtime_put_autosuspend(&trans_pcie->pci_dev->dev);
  1727. #ifdef CONFIG_PM
  1728. IWL_DEBUG_RPM(trans, "runtime usage count: %d\n",
  1729. atomic_read(&trans_pcie->pci_dev->dev.power.usage_count));
  1730. #endif /* CONFIG_PM */
  1731. }
  1732. static const char *get_csr_string(int cmd)
  1733. {
  1734. #define IWL_CMD(x) case x: return #x
  1735. switch (cmd) {
  1736. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1737. IWL_CMD(CSR_INT_COALESCING);
  1738. IWL_CMD(CSR_INT);
  1739. IWL_CMD(CSR_INT_MASK);
  1740. IWL_CMD(CSR_FH_INT_STATUS);
  1741. IWL_CMD(CSR_GPIO_IN);
  1742. IWL_CMD(CSR_RESET);
  1743. IWL_CMD(CSR_GP_CNTRL);
  1744. IWL_CMD(CSR_HW_REV);
  1745. IWL_CMD(CSR_EEPROM_REG);
  1746. IWL_CMD(CSR_EEPROM_GP);
  1747. IWL_CMD(CSR_OTP_GP_REG);
  1748. IWL_CMD(CSR_GIO_REG);
  1749. IWL_CMD(CSR_GP_UCODE_REG);
  1750. IWL_CMD(CSR_GP_DRIVER_REG);
  1751. IWL_CMD(CSR_UCODE_DRV_GP1);
  1752. IWL_CMD(CSR_UCODE_DRV_GP2);
  1753. IWL_CMD(CSR_LED_REG);
  1754. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1755. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1756. IWL_CMD(CSR_ANA_PLL_CFG);
  1757. IWL_CMD(CSR_HW_REV_WA_REG);
  1758. IWL_CMD(CSR_MONITOR_STATUS_REG);
  1759. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1760. default:
  1761. return "UNKNOWN";
  1762. }
  1763. #undef IWL_CMD
  1764. }
  1765. void iwl_pcie_dump_csr(struct iwl_trans *trans)
  1766. {
  1767. int i;
  1768. static const u32 csr_tbl[] = {
  1769. CSR_HW_IF_CONFIG_REG,
  1770. CSR_INT_COALESCING,
  1771. CSR_INT,
  1772. CSR_INT_MASK,
  1773. CSR_FH_INT_STATUS,
  1774. CSR_GPIO_IN,
  1775. CSR_RESET,
  1776. CSR_GP_CNTRL,
  1777. CSR_HW_REV,
  1778. CSR_EEPROM_REG,
  1779. CSR_EEPROM_GP,
  1780. CSR_OTP_GP_REG,
  1781. CSR_GIO_REG,
  1782. CSR_GP_UCODE_REG,
  1783. CSR_GP_DRIVER_REG,
  1784. CSR_UCODE_DRV_GP1,
  1785. CSR_UCODE_DRV_GP2,
  1786. CSR_LED_REG,
  1787. CSR_DRAM_INT_TBL_REG,
  1788. CSR_GIO_CHICKEN_BITS,
  1789. CSR_ANA_PLL_CFG,
  1790. CSR_MONITOR_STATUS_REG,
  1791. CSR_HW_REV_WA_REG,
  1792. CSR_DBG_HPET_MEM_REG
  1793. };
  1794. IWL_ERR(trans, "CSR values:\n");
  1795. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1796. "CSR_INT_PERIODIC_REG)\n");
  1797. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1798. IWL_ERR(trans, " %25s: 0X%08x\n",
  1799. get_csr_string(csr_tbl[i]),
  1800. iwl_read32(trans, csr_tbl[i]));
  1801. }
  1802. }
  1803. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1804. /* create and remove of files */
  1805. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1806. if (!debugfs_create_file(#name, mode, parent, trans, \
  1807. &iwl_dbgfs_##name##_ops)) \
  1808. goto err; \
  1809. } while (0)
  1810. /* file operation */
  1811. #define DEBUGFS_READ_FILE_OPS(name) \
  1812. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1813. .read = iwl_dbgfs_##name##_read, \
  1814. .open = simple_open, \
  1815. .llseek = generic_file_llseek, \
  1816. };
  1817. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1818. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1819. .write = iwl_dbgfs_##name##_write, \
  1820. .open = simple_open, \
  1821. .llseek = generic_file_llseek, \
  1822. };
  1823. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1824. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1825. .write = iwl_dbgfs_##name##_write, \
  1826. .read = iwl_dbgfs_##name##_read, \
  1827. .open = simple_open, \
  1828. .llseek = generic_file_llseek, \
  1829. };
  1830. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1831. char __user *user_buf,
  1832. size_t count, loff_t *ppos)
  1833. {
  1834. struct iwl_trans *trans = file->private_data;
  1835. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1836. struct iwl_txq *txq;
  1837. struct iwl_queue *q;
  1838. char *buf;
  1839. int pos = 0;
  1840. int cnt;
  1841. int ret;
  1842. size_t bufsz;
  1843. bufsz = sizeof(char) * 75 * trans->cfg->base_params->num_of_queues;
  1844. if (!trans_pcie->txq)
  1845. return -EAGAIN;
  1846. buf = kzalloc(bufsz, GFP_KERNEL);
  1847. if (!buf)
  1848. return -ENOMEM;
  1849. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1850. txq = &trans_pcie->txq[cnt];
  1851. q = &txq->q;
  1852. pos += scnprintf(buf + pos, bufsz - pos,
  1853. "hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d frozen=%d%s\n",
  1854. cnt, q->read_ptr, q->write_ptr,
  1855. !!test_bit(cnt, trans_pcie->queue_used),
  1856. !!test_bit(cnt, trans_pcie->queue_stopped),
  1857. txq->need_update, txq->frozen,
  1858. (cnt == trans_pcie->cmd_queue ? " HCMD" : ""));
  1859. }
  1860. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1861. kfree(buf);
  1862. return ret;
  1863. }
  1864. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1865. char __user *user_buf,
  1866. size_t count, loff_t *ppos)
  1867. {
  1868. struct iwl_trans *trans = file->private_data;
  1869. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1870. char *buf;
  1871. int pos = 0, i, ret;
  1872. size_t bufsz = sizeof(buf);
  1873. bufsz = sizeof(char) * 121 * trans->num_rx_queues;
  1874. if (!trans_pcie->rxq)
  1875. return -EAGAIN;
  1876. buf = kzalloc(bufsz, GFP_KERNEL);
  1877. if (!buf)
  1878. return -ENOMEM;
  1879. for (i = 0; i < trans->num_rx_queues && pos < bufsz; i++) {
  1880. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  1881. pos += scnprintf(buf + pos, bufsz - pos, "queue#: %2d\n",
  1882. i);
  1883. pos += scnprintf(buf + pos, bufsz - pos, "\tread: %u\n",
  1884. rxq->read);
  1885. pos += scnprintf(buf + pos, bufsz - pos, "\twrite: %u\n",
  1886. rxq->write);
  1887. pos += scnprintf(buf + pos, bufsz - pos, "\twrite_actual: %u\n",
  1888. rxq->write_actual);
  1889. pos += scnprintf(buf + pos, bufsz - pos, "\tneed_update: %2d\n",
  1890. rxq->need_update);
  1891. pos += scnprintf(buf + pos, bufsz - pos, "\tfree_count: %u\n",
  1892. rxq->free_count);
  1893. if (rxq->rb_stts) {
  1894. pos += scnprintf(buf + pos, bufsz - pos,
  1895. "\tclosed_rb_num: %u\n",
  1896. le16_to_cpu(rxq->rb_stts->closed_rb_num) &
  1897. 0x0FFF);
  1898. } else {
  1899. pos += scnprintf(buf + pos, bufsz - pos,
  1900. "\tclosed_rb_num: Not Allocated\n");
  1901. }
  1902. }
  1903. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1904. kfree(buf);
  1905. return ret;
  1906. }
  1907. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1908. char __user *user_buf,
  1909. size_t count, loff_t *ppos)
  1910. {
  1911. struct iwl_trans *trans = file->private_data;
  1912. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1913. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1914. int pos = 0;
  1915. char *buf;
  1916. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1917. ssize_t ret;
  1918. buf = kzalloc(bufsz, GFP_KERNEL);
  1919. if (!buf)
  1920. return -ENOMEM;
  1921. pos += scnprintf(buf + pos, bufsz - pos,
  1922. "Interrupt Statistics Report:\n");
  1923. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1924. isr_stats->hw);
  1925. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1926. isr_stats->sw);
  1927. if (isr_stats->sw || isr_stats->hw) {
  1928. pos += scnprintf(buf + pos, bufsz - pos,
  1929. "\tLast Restarting Code: 0x%X\n",
  1930. isr_stats->err_code);
  1931. }
  1932. #ifdef CONFIG_IWLWIFI_DEBUG
  1933. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1934. isr_stats->sch);
  1935. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1936. isr_stats->alive);
  1937. #endif
  1938. pos += scnprintf(buf + pos, bufsz - pos,
  1939. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1940. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1941. isr_stats->ctkill);
  1942. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1943. isr_stats->wakeup);
  1944. pos += scnprintf(buf + pos, bufsz - pos,
  1945. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1946. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1947. isr_stats->tx);
  1948. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1949. isr_stats->unhandled);
  1950. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1951. kfree(buf);
  1952. return ret;
  1953. }
  1954. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1955. const char __user *user_buf,
  1956. size_t count, loff_t *ppos)
  1957. {
  1958. struct iwl_trans *trans = file->private_data;
  1959. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1960. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1961. char buf[8];
  1962. int buf_size;
  1963. u32 reset_flag;
  1964. memset(buf, 0, sizeof(buf));
  1965. buf_size = min(count, sizeof(buf) - 1);
  1966. if (copy_from_user(buf, user_buf, buf_size))
  1967. return -EFAULT;
  1968. if (sscanf(buf, "%x", &reset_flag) != 1)
  1969. return -EFAULT;
  1970. if (reset_flag == 0)
  1971. memset(isr_stats, 0, sizeof(*isr_stats));
  1972. return count;
  1973. }
  1974. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  1975. const char __user *user_buf,
  1976. size_t count, loff_t *ppos)
  1977. {
  1978. struct iwl_trans *trans = file->private_data;
  1979. char buf[8];
  1980. int buf_size;
  1981. int csr;
  1982. memset(buf, 0, sizeof(buf));
  1983. buf_size = min(count, sizeof(buf) - 1);
  1984. if (copy_from_user(buf, user_buf, buf_size))
  1985. return -EFAULT;
  1986. if (sscanf(buf, "%d", &csr) != 1)
  1987. return -EFAULT;
  1988. iwl_pcie_dump_csr(trans);
  1989. return count;
  1990. }
  1991. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  1992. char __user *user_buf,
  1993. size_t count, loff_t *ppos)
  1994. {
  1995. struct iwl_trans *trans = file->private_data;
  1996. char *buf = NULL;
  1997. ssize_t ret;
  1998. ret = iwl_dump_fh(trans, &buf);
  1999. if (ret < 0)
  2000. return ret;
  2001. if (!buf)
  2002. return -EINVAL;
  2003. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  2004. kfree(buf);
  2005. return ret;
  2006. }
  2007. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  2008. DEBUGFS_READ_FILE_OPS(fh_reg);
  2009. DEBUGFS_READ_FILE_OPS(rx_queue);
  2010. DEBUGFS_READ_FILE_OPS(tx_queue);
  2011. DEBUGFS_WRITE_FILE_OPS(csr);
  2012. /* Create the debugfs files and directories */
  2013. int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans)
  2014. {
  2015. struct dentry *dir = trans->dbgfs_dir;
  2016. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  2017. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  2018. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  2019. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  2020. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  2021. return 0;
  2022. err:
  2023. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  2024. return -ENOMEM;
  2025. }
  2026. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  2027. static u32 iwl_trans_pcie_get_cmdlen(struct iwl_tfd *tfd)
  2028. {
  2029. u32 cmdlen = 0;
  2030. int i;
  2031. for (i = 0; i < IWL_NUM_OF_TBS; i++)
  2032. cmdlen += iwl_pcie_tfd_tb_get_len(tfd, i);
  2033. return cmdlen;
  2034. }
  2035. static u32 iwl_trans_pcie_dump_rbs(struct iwl_trans *trans,
  2036. struct iwl_fw_error_dump_data **data,
  2037. int allocated_rb_nums)
  2038. {
  2039. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2040. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  2041. /* Dump RBs is supported only for pre-9000 devices (1 queue) */
  2042. struct iwl_rxq *rxq = &trans_pcie->rxq[0];
  2043. u32 i, r, j, rb_len = 0;
  2044. spin_lock(&rxq->lock);
  2045. r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
  2046. for (i = rxq->read, j = 0;
  2047. i != r && j < allocated_rb_nums;
  2048. i = (i + 1) & RX_QUEUE_MASK, j++) {
  2049. struct iwl_rx_mem_buffer *rxb = rxq->queue[i];
  2050. struct iwl_fw_error_dump_rb *rb;
  2051. dma_unmap_page(trans->dev, rxb->page_dma, max_len,
  2052. DMA_FROM_DEVICE);
  2053. rb_len += sizeof(**data) + sizeof(*rb) + max_len;
  2054. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RB);
  2055. (*data)->len = cpu_to_le32(sizeof(*rb) + max_len);
  2056. rb = (void *)(*data)->data;
  2057. rb->index = cpu_to_le32(i);
  2058. memcpy(rb->data, page_address(rxb->page), max_len);
  2059. /* remap the page for the free benefit */
  2060. rxb->page_dma = dma_map_page(trans->dev, rxb->page, 0,
  2061. max_len,
  2062. DMA_FROM_DEVICE);
  2063. *data = iwl_fw_error_next_data(*data);
  2064. }
  2065. spin_unlock(&rxq->lock);
  2066. return rb_len;
  2067. }
  2068. #define IWL_CSR_TO_DUMP (0x250)
  2069. static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans,
  2070. struct iwl_fw_error_dump_data **data)
  2071. {
  2072. u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP;
  2073. __le32 *val;
  2074. int i;
  2075. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR);
  2076. (*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP);
  2077. val = (void *)(*data)->data;
  2078. for (i = 0; i < IWL_CSR_TO_DUMP; i += 4)
  2079. *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
  2080. *data = iwl_fw_error_next_data(*data);
  2081. return csr_len;
  2082. }
  2083. static u32 iwl_trans_pcie_fh_regs_dump(struct iwl_trans *trans,
  2084. struct iwl_fw_error_dump_data **data)
  2085. {
  2086. u32 fh_regs_len = FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND;
  2087. unsigned long flags;
  2088. __le32 *val;
  2089. int i;
  2090. if (!iwl_trans_grab_nic_access(trans, &flags))
  2091. return 0;
  2092. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FH_REGS);
  2093. (*data)->len = cpu_to_le32(fh_regs_len);
  2094. val = (void *)(*data)->data;
  2095. for (i = FH_MEM_LOWER_BOUND; i < FH_MEM_UPPER_BOUND; i += sizeof(u32))
  2096. *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
  2097. iwl_trans_release_nic_access(trans, &flags);
  2098. *data = iwl_fw_error_next_data(*data);
  2099. return sizeof(**data) + fh_regs_len;
  2100. }
  2101. static u32
  2102. iwl_trans_pci_dump_marbh_monitor(struct iwl_trans *trans,
  2103. struct iwl_fw_error_dump_fw_mon *fw_mon_data,
  2104. u32 monitor_len)
  2105. {
  2106. u32 buf_size_in_dwords = (monitor_len >> 2);
  2107. u32 *buffer = (u32 *)fw_mon_data->data;
  2108. unsigned long flags;
  2109. u32 i;
  2110. if (!iwl_trans_grab_nic_access(trans, &flags))
  2111. return 0;
  2112. iwl_write_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x1);
  2113. for (i = 0; i < buf_size_in_dwords; i++)
  2114. buffer[i] = iwl_read_prph_no_grab(trans,
  2115. MON_DMARB_RD_DATA_ADDR);
  2116. iwl_write_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x0);
  2117. iwl_trans_release_nic_access(trans, &flags);
  2118. return monitor_len;
  2119. }
  2120. static u32
  2121. iwl_trans_pcie_dump_monitor(struct iwl_trans *trans,
  2122. struct iwl_fw_error_dump_data **data,
  2123. u32 monitor_len)
  2124. {
  2125. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2126. u32 len = 0;
  2127. if ((trans_pcie->fw_mon_page &&
  2128. trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) ||
  2129. trans->dbg_dest_tlv) {
  2130. struct iwl_fw_error_dump_fw_mon *fw_mon_data;
  2131. u32 base, write_ptr, wrap_cnt;
  2132. /* If there was a dest TLV - use the values from there */
  2133. if (trans->dbg_dest_tlv) {
  2134. write_ptr =
  2135. le32_to_cpu(trans->dbg_dest_tlv->write_ptr_reg);
  2136. wrap_cnt = le32_to_cpu(trans->dbg_dest_tlv->wrap_count);
  2137. base = le32_to_cpu(trans->dbg_dest_tlv->base_reg);
  2138. } else {
  2139. base = MON_BUFF_BASE_ADDR;
  2140. write_ptr = MON_BUFF_WRPTR;
  2141. wrap_cnt = MON_BUFF_CYCLE_CNT;
  2142. }
  2143. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR);
  2144. fw_mon_data = (void *)(*data)->data;
  2145. fw_mon_data->fw_mon_wr_ptr =
  2146. cpu_to_le32(iwl_read_prph(trans, write_ptr));
  2147. fw_mon_data->fw_mon_cycle_cnt =
  2148. cpu_to_le32(iwl_read_prph(trans, wrap_cnt));
  2149. fw_mon_data->fw_mon_base_ptr =
  2150. cpu_to_le32(iwl_read_prph(trans, base));
  2151. len += sizeof(**data) + sizeof(*fw_mon_data);
  2152. if (trans_pcie->fw_mon_page) {
  2153. /*
  2154. * The firmware is now asserted, it won't write anything
  2155. * to the buffer. CPU can take ownership to fetch the
  2156. * data. The buffer will be handed back to the device
  2157. * before the firmware will be restarted.
  2158. */
  2159. dma_sync_single_for_cpu(trans->dev,
  2160. trans_pcie->fw_mon_phys,
  2161. trans_pcie->fw_mon_size,
  2162. DMA_FROM_DEVICE);
  2163. memcpy(fw_mon_data->data,
  2164. page_address(trans_pcie->fw_mon_page),
  2165. trans_pcie->fw_mon_size);
  2166. monitor_len = trans_pcie->fw_mon_size;
  2167. } else if (trans->dbg_dest_tlv->monitor_mode == SMEM_MODE) {
  2168. /*
  2169. * Update pointers to reflect actual values after
  2170. * shifting
  2171. */
  2172. base = iwl_read_prph(trans, base) <<
  2173. trans->dbg_dest_tlv->base_shift;
  2174. iwl_trans_read_mem(trans, base, fw_mon_data->data,
  2175. monitor_len / sizeof(u32));
  2176. } else if (trans->dbg_dest_tlv->monitor_mode == MARBH_MODE) {
  2177. monitor_len =
  2178. iwl_trans_pci_dump_marbh_monitor(trans,
  2179. fw_mon_data,
  2180. monitor_len);
  2181. } else {
  2182. /* Didn't match anything - output no monitor data */
  2183. monitor_len = 0;
  2184. }
  2185. len += monitor_len;
  2186. (*data)->len = cpu_to_le32(monitor_len + sizeof(*fw_mon_data));
  2187. }
  2188. return len;
  2189. }
  2190. static struct iwl_trans_dump_data
  2191. *iwl_trans_pcie_dump_data(struct iwl_trans *trans,
  2192. const struct iwl_fw_dbg_trigger_tlv *trigger)
  2193. {
  2194. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2195. struct iwl_fw_error_dump_data *data;
  2196. struct iwl_txq *cmdq = &trans_pcie->txq[trans_pcie->cmd_queue];
  2197. struct iwl_fw_error_dump_txcmd *txcmd;
  2198. struct iwl_trans_dump_data *dump_data;
  2199. u32 len, num_rbs;
  2200. u32 monitor_len;
  2201. int i, ptr;
  2202. bool dump_rbs = test_bit(STATUS_FW_ERROR, &trans->status) &&
  2203. !trans->cfg->mq_rx_supported;
  2204. /* transport dump header */
  2205. len = sizeof(*dump_data);
  2206. /* host commands */
  2207. len += sizeof(*data) +
  2208. cmdq->q.n_window * (sizeof(*txcmd) + TFD_MAX_PAYLOAD_SIZE);
  2209. /* FW monitor */
  2210. if (trans_pcie->fw_mon_page) {
  2211. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
  2212. trans_pcie->fw_mon_size;
  2213. monitor_len = trans_pcie->fw_mon_size;
  2214. } else if (trans->dbg_dest_tlv) {
  2215. u32 base, end;
  2216. base = le32_to_cpu(trans->dbg_dest_tlv->base_reg);
  2217. end = le32_to_cpu(trans->dbg_dest_tlv->end_reg);
  2218. base = iwl_read_prph(trans, base) <<
  2219. trans->dbg_dest_tlv->base_shift;
  2220. end = iwl_read_prph(trans, end) <<
  2221. trans->dbg_dest_tlv->end_shift;
  2222. /* Make "end" point to the actual end */
  2223. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000 ||
  2224. trans->dbg_dest_tlv->monitor_mode == MARBH_MODE)
  2225. end += (1 << trans->dbg_dest_tlv->end_shift);
  2226. monitor_len = end - base;
  2227. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
  2228. monitor_len;
  2229. } else {
  2230. monitor_len = 0;
  2231. }
  2232. if (trigger && (trigger->mode & IWL_FW_DBG_TRIGGER_MONITOR_ONLY)) {
  2233. dump_data = vzalloc(len);
  2234. if (!dump_data)
  2235. return NULL;
  2236. data = (void *)dump_data->data;
  2237. len = iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
  2238. dump_data->len = len;
  2239. return dump_data;
  2240. }
  2241. /* CSR registers */
  2242. len += sizeof(*data) + IWL_CSR_TO_DUMP;
  2243. /* FH registers */
  2244. len += sizeof(*data) + (FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND);
  2245. if (dump_rbs) {
  2246. /* Dump RBs is supported only for pre-9000 devices (1 queue) */
  2247. struct iwl_rxq *rxq = &trans_pcie->rxq[0];
  2248. /* RBs */
  2249. num_rbs = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num))
  2250. & 0x0FFF;
  2251. num_rbs = (num_rbs - rxq->read) & RX_QUEUE_MASK;
  2252. len += num_rbs * (sizeof(*data) +
  2253. sizeof(struct iwl_fw_error_dump_rb) +
  2254. (PAGE_SIZE << trans_pcie->rx_page_order));
  2255. }
  2256. dump_data = vzalloc(len);
  2257. if (!dump_data)
  2258. return NULL;
  2259. len = 0;
  2260. data = (void *)dump_data->data;
  2261. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
  2262. txcmd = (void *)data->data;
  2263. spin_lock_bh(&cmdq->lock);
  2264. ptr = cmdq->q.write_ptr;
  2265. for (i = 0; i < cmdq->q.n_window; i++) {
  2266. u8 idx = get_cmd_index(&cmdq->q, ptr);
  2267. u32 caplen, cmdlen;
  2268. cmdlen = iwl_trans_pcie_get_cmdlen(&cmdq->tfds[ptr]);
  2269. caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
  2270. if (cmdlen) {
  2271. len += sizeof(*txcmd) + caplen;
  2272. txcmd->cmdlen = cpu_to_le32(cmdlen);
  2273. txcmd->caplen = cpu_to_le32(caplen);
  2274. memcpy(txcmd->data, cmdq->entries[idx].cmd, caplen);
  2275. txcmd = (void *)((u8 *)txcmd->data + caplen);
  2276. }
  2277. ptr = iwl_queue_dec_wrap(ptr);
  2278. }
  2279. spin_unlock_bh(&cmdq->lock);
  2280. data->len = cpu_to_le32(len);
  2281. len += sizeof(*data);
  2282. data = iwl_fw_error_next_data(data);
  2283. len += iwl_trans_pcie_dump_csr(trans, &data);
  2284. len += iwl_trans_pcie_fh_regs_dump(trans, &data);
  2285. if (dump_rbs)
  2286. len += iwl_trans_pcie_dump_rbs(trans, &data, num_rbs);
  2287. len += iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
  2288. dump_data->len = len;
  2289. return dump_data;
  2290. }
  2291. #ifdef CONFIG_PM_SLEEP
  2292. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  2293. {
  2294. if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  2295. return iwl_pci_fw_enter_d0i3(trans);
  2296. return 0;
  2297. }
  2298. static void iwl_trans_pcie_resume(struct iwl_trans *trans)
  2299. {
  2300. if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  2301. iwl_pci_fw_exit_d0i3(trans);
  2302. }
  2303. #endif /* CONFIG_PM_SLEEP */
  2304. static const struct iwl_trans_ops trans_ops_pcie = {
  2305. .start_hw = iwl_trans_pcie_start_hw,
  2306. .op_mode_leave = iwl_trans_pcie_op_mode_leave,
  2307. .fw_alive = iwl_trans_pcie_fw_alive,
  2308. .start_fw = iwl_trans_pcie_start_fw,
  2309. .stop_device = iwl_trans_pcie_stop_device,
  2310. .d3_suspend = iwl_trans_pcie_d3_suspend,
  2311. .d3_resume = iwl_trans_pcie_d3_resume,
  2312. #ifdef CONFIG_PM_SLEEP
  2313. .suspend = iwl_trans_pcie_suspend,
  2314. .resume = iwl_trans_pcie_resume,
  2315. #endif /* CONFIG_PM_SLEEP */
  2316. .send_cmd = iwl_trans_pcie_send_hcmd,
  2317. .tx = iwl_trans_pcie_tx,
  2318. .reclaim = iwl_trans_pcie_reclaim,
  2319. .txq_disable = iwl_trans_pcie_txq_disable,
  2320. .txq_enable = iwl_trans_pcie_txq_enable,
  2321. .txq_set_shared_mode = iwl_trans_pcie_txq_set_shared_mode,
  2322. .wait_tx_queue_empty = iwl_trans_pcie_wait_txq_empty,
  2323. .freeze_txq_timer = iwl_trans_pcie_freeze_txq_timer,
  2324. .block_txq_ptrs = iwl_trans_pcie_block_txq_ptrs,
  2325. .write8 = iwl_trans_pcie_write8,
  2326. .write32 = iwl_trans_pcie_write32,
  2327. .read32 = iwl_trans_pcie_read32,
  2328. .read_prph = iwl_trans_pcie_read_prph,
  2329. .write_prph = iwl_trans_pcie_write_prph,
  2330. .read_mem = iwl_trans_pcie_read_mem,
  2331. .write_mem = iwl_trans_pcie_write_mem,
  2332. .configure = iwl_trans_pcie_configure,
  2333. .set_pmi = iwl_trans_pcie_set_pmi,
  2334. .grab_nic_access = iwl_trans_pcie_grab_nic_access,
  2335. .release_nic_access = iwl_trans_pcie_release_nic_access,
  2336. .set_bits_mask = iwl_trans_pcie_set_bits_mask,
  2337. .ref = iwl_trans_pcie_ref,
  2338. .unref = iwl_trans_pcie_unref,
  2339. .dump_data = iwl_trans_pcie_dump_data,
  2340. };
  2341. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  2342. const struct pci_device_id *ent,
  2343. const struct iwl_cfg *cfg)
  2344. {
  2345. struct iwl_trans_pcie *trans_pcie;
  2346. struct iwl_trans *trans;
  2347. int ret, addr_size;
  2348. trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie),
  2349. &pdev->dev, cfg, &trans_ops_pcie, 0);
  2350. if (!trans)
  2351. return ERR_PTR(-ENOMEM);
  2352. trans->max_skb_frags = IWL_PCIE_MAX_FRAGS;
  2353. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2354. trans_pcie->trans = trans;
  2355. spin_lock_init(&trans_pcie->irq_lock);
  2356. spin_lock_init(&trans_pcie->reg_lock);
  2357. mutex_init(&trans_pcie->mutex);
  2358. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  2359. trans_pcie->tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
  2360. if (!trans_pcie->tso_hdr_page) {
  2361. ret = -ENOMEM;
  2362. goto out_no_pci;
  2363. }
  2364. ret = pci_enable_device(pdev);
  2365. if (ret)
  2366. goto out_no_pci;
  2367. if (!cfg->base_params->pcie_l1_allowed) {
  2368. /*
  2369. * W/A - seems to solve weird behavior. We need to remove this
  2370. * if we don't want to stay in L1 all the time. This wastes a
  2371. * lot of power.
  2372. */
  2373. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  2374. PCIE_LINK_STATE_L1 |
  2375. PCIE_LINK_STATE_CLKPM);
  2376. }
  2377. if (cfg->mq_rx_supported)
  2378. addr_size = 64;
  2379. else
  2380. addr_size = 36;
  2381. pci_set_master(pdev);
  2382. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(addr_size));
  2383. if (!ret)
  2384. ret = pci_set_consistent_dma_mask(pdev,
  2385. DMA_BIT_MASK(addr_size));
  2386. if (ret) {
  2387. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2388. if (!ret)
  2389. ret = pci_set_consistent_dma_mask(pdev,
  2390. DMA_BIT_MASK(32));
  2391. /* both attempts failed: */
  2392. if (ret) {
  2393. dev_err(&pdev->dev, "No suitable DMA available\n");
  2394. goto out_pci_disable_device;
  2395. }
  2396. }
  2397. ret = pci_request_regions(pdev, DRV_NAME);
  2398. if (ret) {
  2399. dev_err(&pdev->dev, "pci_request_regions failed\n");
  2400. goto out_pci_disable_device;
  2401. }
  2402. trans_pcie->hw_base = pci_ioremap_bar(pdev, 0);
  2403. if (!trans_pcie->hw_base) {
  2404. dev_err(&pdev->dev, "pci_ioremap_bar failed\n");
  2405. ret = -ENODEV;
  2406. goto out_pci_release_regions;
  2407. }
  2408. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  2409. * PCI Tx retries from interfering with C3 CPU state */
  2410. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  2411. trans->dev = &pdev->dev;
  2412. trans_pcie->pci_dev = pdev;
  2413. iwl_disable_interrupts(trans);
  2414. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  2415. /*
  2416. * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have
  2417. * changed, and now the revision step also includes bit 0-1 (no more
  2418. * "dash" value). To keep hw_rev backwards compatible - we'll store it
  2419. * in the old format.
  2420. */
  2421. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
  2422. unsigned long flags;
  2423. trans->hw_rev = (trans->hw_rev & 0xfff0) |
  2424. (CSR_HW_REV_STEP(trans->hw_rev << 2) << 2);
  2425. ret = iwl_pcie_prepare_card_hw(trans);
  2426. if (ret) {
  2427. IWL_WARN(trans, "Exit HW not ready\n");
  2428. goto out_pci_disable_msi;
  2429. }
  2430. /*
  2431. * in-order to recognize C step driver should read chip version
  2432. * id located at the AUX bus MISC address space.
  2433. */
  2434. iwl_set_bit(trans, CSR_GP_CNTRL,
  2435. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  2436. udelay(2);
  2437. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  2438. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  2439. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  2440. 25000);
  2441. if (ret < 0) {
  2442. IWL_DEBUG_INFO(trans, "Failed to wake up the nic\n");
  2443. goto out_pci_disable_msi;
  2444. }
  2445. if (iwl_trans_grab_nic_access(trans, &flags)) {
  2446. u32 hw_step;
  2447. hw_step = iwl_read_prph_no_grab(trans, WFPM_CTRL_REG);
  2448. hw_step |= ENABLE_WFPM;
  2449. iwl_write_prph_no_grab(trans, WFPM_CTRL_REG, hw_step);
  2450. hw_step = iwl_read_prph_no_grab(trans, AUX_MISC_REG);
  2451. hw_step = (hw_step >> HW_STEP_LOCATION_BITS) & 0xF;
  2452. if (hw_step == 0x3)
  2453. trans->hw_rev = (trans->hw_rev & 0xFFFFFFF3) |
  2454. (SILICON_C_STEP << 2);
  2455. iwl_trans_release_nic_access(trans, &flags);
  2456. }
  2457. }
  2458. trans->hw_rf_id = iwl_read32(trans, CSR_HW_RF_ID);
  2459. iwl_pcie_set_interrupt_capa(pdev, trans);
  2460. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  2461. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  2462. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  2463. /* Initialize the wait queue for commands */
  2464. init_waitqueue_head(&trans_pcie->wait_command_queue);
  2465. init_waitqueue_head(&trans_pcie->d0i3_waitq);
  2466. if (trans_pcie->msix_enabled) {
  2467. if (iwl_pcie_init_msix_handler(pdev, trans_pcie))
  2468. goto out_pci_release_regions;
  2469. } else {
  2470. ret = iwl_pcie_alloc_ict(trans);
  2471. if (ret)
  2472. goto out_pci_disable_msi;
  2473. ret = request_threaded_irq(pdev->irq, iwl_pcie_isr,
  2474. iwl_pcie_irq_handler,
  2475. IRQF_SHARED, DRV_NAME, trans);
  2476. if (ret) {
  2477. IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
  2478. goto out_free_ict;
  2479. }
  2480. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  2481. }
  2482. #ifdef CONFIG_IWLWIFI_PCIE_RTPM
  2483. trans->runtime_pm_mode = IWL_PLAT_PM_MODE_D0I3;
  2484. #else
  2485. trans->runtime_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  2486. #endif /* CONFIG_IWLWIFI_PCIE_RTPM */
  2487. return trans;
  2488. out_free_ict:
  2489. iwl_pcie_free_ict(trans);
  2490. out_pci_disable_msi:
  2491. pci_disable_msi(pdev);
  2492. out_pci_release_regions:
  2493. pci_release_regions(pdev);
  2494. out_pci_disable_device:
  2495. pci_disable_device(pdev);
  2496. out_no_pci:
  2497. free_percpu(trans_pcie->tso_hdr_page);
  2498. iwl_trans_free(trans);
  2499. return ERR_PTR(ret);
  2500. }