trans.c 94 KB

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