csio_hw.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/pci.h>
  35. #include <linux/pci_regs.h>
  36. #include <linux/firmware.h>
  37. #include <linux/stddef.h>
  38. #include <linux/delay.h>
  39. #include <linux/string.h>
  40. #include <linux/compiler.h>
  41. #include <linux/jiffies.h>
  42. #include <linux/kernel.h>
  43. #include <linux/log2.h>
  44. #include "csio_hw.h"
  45. #include "csio_lnode.h"
  46. #include "csio_rnode.h"
  47. int csio_force_master;
  48. int csio_dbg_level = 0xFEFF;
  49. unsigned int csio_port_mask = 0xf;
  50. /* Default FW event queue entries. */
  51. static uint32_t csio_evtq_sz = CSIO_EVTQ_SIZE;
  52. /* Default MSI param level */
  53. int csio_msi = 2;
  54. /* FCoE function instances */
  55. static int dev_num;
  56. /* FCoE Adapter types & its description */
  57. static const struct csio_adap_desc csio_t4_fcoe_adapters[] = {
  58. {"T440-Dbg 10G", "Chelsio T440-Dbg 10G [FCoE]"},
  59. {"T420-CR 10G", "Chelsio T420-CR 10G [FCoE]"},
  60. {"T422-CR 10G/1G", "Chelsio T422-CR 10G/1G [FCoE]"},
  61. {"T440-CR 10G", "Chelsio T440-CR 10G [FCoE]"},
  62. {"T420-BCH 10G", "Chelsio T420-BCH 10G [FCoE]"},
  63. {"T440-BCH 10G", "Chelsio T440-BCH 10G [FCoE]"},
  64. {"T440-CH 10G", "Chelsio T440-CH 10G [FCoE]"},
  65. {"T420-SO 10G", "Chelsio T420-SO 10G [FCoE]"},
  66. {"T420-CX4 10G", "Chelsio T420-CX4 10G [FCoE]"},
  67. {"T420-BT 10G", "Chelsio T420-BT 10G [FCoE]"},
  68. {"T404-BT 1G", "Chelsio T404-BT 1G [FCoE]"},
  69. {"B420-SR 10G", "Chelsio B420-SR 10G [FCoE]"},
  70. {"B404-BT 1G", "Chelsio B404-BT 1G [FCoE]"},
  71. {"T480-CR 10G", "Chelsio T480-CR 10G [FCoE]"},
  72. {"T440-LP-CR 10G", "Chelsio T440-LP-CR 10G [FCoE]"},
  73. {"AMSTERDAM 10G", "Chelsio AMSTERDAM 10G [FCoE]"},
  74. {"HUAWEI T480 10G", "Chelsio HUAWEI T480 10G [FCoE]"},
  75. {"HUAWEI T440 10G", "Chelsio HUAWEI T440 10G [FCoE]"},
  76. {"HUAWEI STG 10G", "Chelsio HUAWEI STG 10G [FCoE]"},
  77. {"ACROMAG XAUI 10G", "Chelsio ACROMAG XAUI 10G [FCoE]"},
  78. {"ACROMAG SFP+ 10G", "Chelsio ACROMAG SFP+ 10G [FCoE]"},
  79. {"QUANTA SFP+ 10G", "Chelsio QUANTA SFP+ 10G [FCoE]"},
  80. {"HUAWEI 10Gbase-T", "Chelsio HUAWEI 10Gbase-T [FCoE]"},
  81. {"HUAWEI T4TOE 10G", "Chelsio HUAWEI T4TOE 10G [FCoE]"}
  82. };
  83. static const struct csio_adap_desc csio_t5_fcoe_adapters[] = {
  84. {"T580-Dbg 10G", "Chelsio T580-Dbg 10G [FCoE]"},
  85. {"T520-CR 10G", "Chelsio T520-CR 10G [FCoE]"},
  86. {"T522-CR 10G/1G", "Chelsio T452-CR 10G/1G [FCoE]"},
  87. {"T540-CR 10G", "Chelsio T540-CR 10G [FCoE]"},
  88. {"T520-BCH 10G", "Chelsio T520-BCH 10G [FCoE]"},
  89. {"T540-BCH 10G", "Chelsio T540-BCH 10G [FCoE]"},
  90. {"T540-CH 10G", "Chelsio T540-CH 10G [FCoE]"},
  91. {"T520-SO 10G", "Chelsio T520-SO 10G [FCoE]"},
  92. {"T520-CX4 10G", "Chelsio T520-CX4 10G [FCoE]"},
  93. {"T520-BT 10G", "Chelsio T520-BT 10G [FCoE]"},
  94. {"T504-BT 1G", "Chelsio T504-BT 1G [FCoE]"},
  95. {"B520-SR 10G", "Chelsio B520-SR 10G [FCoE]"},
  96. {"B504-BT 1G", "Chelsio B504-BT 1G [FCoE]"},
  97. {"T580-CR 10G", "Chelsio T580-CR 10G [FCoE]"},
  98. {"T540-LP-CR 10G", "Chelsio T540-LP-CR 10G [FCoE]"},
  99. {"AMSTERDAM 10G", "Chelsio AMSTERDAM 10G [FCoE]"},
  100. {"T580-LP-CR 40G", "Chelsio T580-LP-CR 40G [FCoE]"},
  101. {"T520-LL-CR 10G", "Chelsio T520-LL-CR 10G [FCoE]"},
  102. {"T560-CR 40G", "Chelsio T560-CR 40G [FCoE]"},
  103. {"T580-CR 40G", "Chelsio T580-CR 40G [FCoE]"}
  104. };
  105. static void csio_mgmtm_cleanup(struct csio_mgmtm *);
  106. static void csio_hw_mbm_cleanup(struct csio_hw *);
  107. /* State machine forward declarations */
  108. static void csio_hws_uninit(struct csio_hw *, enum csio_hw_ev);
  109. static void csio_hws_configuring(struct csio_hw *, enum csio_hw_ev);
  110. static void csio_hws_initializing(struct csio_hw *, enum csio_hw_ev);
  111. static void csio_hws_ready(struct csio_hw *, enum csio_hw_ev);
  112. static void csio_hws_quiescing(struct csio_hw *, enum csio_hw_ev);
  113. static void csio_hws_quiesced(struct csio_hw *, enum csio_hw_ev);
  114. static void csio_hws_resetting(struct csio_hw *, enum csio_hw_ev);
  115. static void csio_hws_removing(struct csio_hw *, enum csio_hw_ev);
  116. static void csio_hws_pcierr(struct csio_hw *, enum csio_hw_ev);
  117. static void csio_hw_initialize(struct csio_hw *hw);
  118. static void csio_evtq_stop(struct csio_hw *hw);
  119. static void csio_evtq_start(struct csio_hw *hw);
  120. int csio_is_hw_ready(struct csio_hw *hw)
  121. {
  122. return csio_match_state(hw, csio_hws_ready);
  123. }
  124. int csio_is_hw_removing(struct csio_hw *hw)
  125. {
  126. return csio_match_state(hw, csio_hws_removing);
  127. }
  128. /*
  129. * csio_hw_wait_op_done_val - wait until an operation is completed
  130. * @hw: the HW module
  131. * @reg: the register to check for completion
  132. * @mask: a single-bit field within @reg that indicates completion
  133. * @polarity: the value of the field when the operation is completed
  134. * @attempts: number of check iterations
  135. * @delay: delay in usecs between iterations
  136. * @valp: where to store the value of the register at completion time
  137. *
  138. * Wait until an operation is completed by checking a bit in a register
  139. * up to @attempts times. If @valp is not NULL the value of the register
  140. * at the time it indicated completion is stored there. Returns 0 if the
  141. * operation completes and -EAGAIN otherwise.
  142. */
  143. int
  144. csio_hw_wait_op_done_val(struct csio_hw *hw, int reg, uint32_t mask,
  145. int polarity, int attempts, int delay, uint32_t *valp)
  146. {
  147. uint32_t val;
  148. while (1) {
  149. val = csio_rd_reg32(hw, reg);
  150. if (!!(val & mask) == polarity) {
  151. if (valp)
  152. *valp = val;
  153. return 0;
  154. }
  155. if (--attempts == 0)
  156. return -EAGAIN;
  157. if (delay)
  158. udelay(delay);
  159. }
  160. }
  161. /*
  162. * csio_hw_tp_wr_bits_indirect - set/clear bits in an indirect TP register
  163. * @hw: the adapter
  164. * @addr: the indirect TP register address
  165. * @mask: specifies the field within the register to modify
  166. * @val: new value for the field
  167. *
  168. * Sets a field of an indirect TP register to the given value.
  169. */
  170. void
  171. csio_hw_tp_wr_bits_indirect(struct csio_hw *hw, unsigned int addr,
  172. unsigned int mask, unsigned int val)
  173. {
  174. csio_wr_reg32(hw, addr, TP_PIO_ADDR);
  175. val |= csio_rd_reg32(hw, TP_PIO_DATA) & ~mask;
  176. csio_wr_reg32(hw, val, TP_PIO_DATA);
  177. }
  178. void
  179. csio_set_reg_field(struct csio_hw *hw, uint32_t reg, uint32_t mask,
  180. uint32_t value)
  181. {
  182. uint32_t val = csio_rd_reg32(hw, reg) & ~mask;
  183. csio_wr_reg32(hw, val | value, reg);
  184. /* Flush */
  185. csio_rd_reg32(hw, reg);
  186. }
  187. static int
  188. csio_memory_write(struct csio_hw *hw, int mtype, u32 addr, u32 len, u32 *buf)
  189. {
  190. return hw->chip_ops->chip_memory_rw(hw, MEMWIN_CSIOSTOR, mtype,
  191. addr, len, buf, 0);
  192. }
  193. /*
  194. * EEPROM reads take a few tens of us while writes can take a bit over 5 ms.
  195. */
  196. #define EEPROM_MAX_RD_POLL 40
  197. #define EEPROM_MAX_WR_POLL 6
  198. #define EEPROM_STAT_ADDR 0x7bfc
  199. #define VPD_BASE 0x400
  200. #define VPD_BASE_OLD 0
  201. #define VPD_LEN 1024
  202. #define VPD_INFO_FLD_HDR_SIZE 3
  203. /*
  204. * csio_hw_seeprom_read - read a serial EEPROM location
  205. * @hw: hw to read
  206. * @addr: EEPROM virtual address
  207. * @data: where to store the read data
  208. *
  209. * Read a 32-bit word from a location in serial EEPROM using the card's PCI
  210. * VPD capability. Note that this function must be called with a virtual
  211. * address.
  212. */
  213. static int
  214. csio_hw_seeprom_read(struct csio_hw *hw, uint32_t addr, uint32_t *data)
  215. {
  216. uint16_t val = 0;
  217. int attempts = EEPROM_MAX_RD_POLL;
  218. uint32_t base = hw->params.pci.vpd_cap_addr;
  219. if (addr >= EEPROMVSIZE || (addr & 3))
  220. return -EINVAL;
  221. pci_write_config_word(hw->pdev, base + PCI_VPD_ADDR, (uint16_t)addr);
  222. do {
  223. udelay(10);
  224. pci_read_config_word(hw->pdev, base + PCI_VPD_ADDR, &val);
  225. } while (!(val & PCI_VPD_ADDR_F) && --attempts);
  226. if (!(val & PCI_VPD_ADDR_F)) {
  227. csio_err(hw, "reading EEPROM address 0x%x failed\n", addr);
  228. return -EINVAL;
  229. }
  230. pci_read_config_dword(hw->pdev, base + PCI_VPD_DATA, data);
  231. *data = le32_to_cpu(*data);
  232. return 0;
  233. }
  234. /*
  235. * Partial EEPROM Vital Product Data structure. Includes only the ID and
  236. * VPD-R sections.
  237. */
  238. struct t4_vpd_hdr {
  239. u8 id_tag;
  240. u8 id_len[2];
  241. u8 id_data[ID_LEN];
  242. u8 vpdr_tag;
  243. u8 vpdr_len[2];
  244. };
  245. /*
  246. * csio_hw_get_vpd_keyword_val - Locates an information field keyword in
  247. * the VPD
  248. * @v: Pointer to buffered vpd data structure
  249. * @kw: The keyword to search for
  250. *
  251. * Returns the value of the information field keyword or
  252. * -EINVAL otherwise.
  253. */
  254. static int
  255. csio_hw_get_vpd_keyword_val(const struct t4_vpd_hdr *v, const char *kw)
  256. {
  257. int32_t i;
  258. int32_t offset , len;
  259. const uint8_t *buf = &v->id_tag;
  260. const uint8_t *vpdr_len = &v->vpdr_tag;
  261. offset = sizeof(struct t4_vpd_hdr);
  262. len = (uint16_t)vpdr_len[1] + ((uint16_t)vpdr_len[2] << 8);
  263. if (len + sizeof(struct t4_vpd_hdr) > VPD_LEN)
  264. return -EINVAL;
  265. for (i = offset; (i + VPD_INFO_FLD_HDR_SIZE) <= (offset + len);) {
  266. if (memcmp(buf + i , kw, 2) == 0) {
  267. i += VPD_INFO_FLD_HDR_SIZE;
  268. return i;
  269. }
  270. i += VPD_INFO_FLD_HDR_SIZE + buf[i+2];
  271. }
  272. return -EINVAL;
  273. }
  274. static int
  275. csio_pci_capability(struct pci_dev *pdev, int cap, int *pos)
  276. {
  277. *pos = pci_find_capability(pdev, cap);
  278. if (*pos)
  279. return 0;
  280. return -1;
  281. }
  282. /*
  283. * csio_hw_get_vpd_params - read VPD parameters from VPD EEPROM
  284. * @hw: HW module
  285. * @p: where to store the parameters
  286. *
  287. * Reads card parameters stored in VPD EEPROM.
  288. */
  289. static int
  290. csio_hw_get_vpd_params(struct csio_hw *hw, struct csio_vpd *p)
  291. {
  292. int i, ret, ec, sn, addr;
  293. uint8_t *vpd, csum;
  294. const struct t4_vpd_hdr *v;
  295. /* To get around compilation warning from strstrip */
  296. char *s;
  297. if (csio_is_valid_vpd(hw))
  298. return 0;
  299. ret = csio_pci_capability(hw->pdev, PCI_CAP_ID_VPD,
  300. &hw->params.pci.vpd_cap_addr);
  301. if (ret)
  302. return -EINVAL;
  303. vpd = kzalloc(VPD_LEN, GFP_ATOMIC);
  304. if (vpd == NULL)
  305. return -ENOMEM;
  306. /*
  307. * Card information normally starts at VPD_BASE but early cards had
  308. * it at 0.
  309. */
  310. ret = csio_hw_seeprom_read(hw, VPD_BASE, (uint32_t *)(vpd));
  311. addr = *vpd == 0x82 ? VPD_BASE : VPD_BASE_OLD;
  312. for (i = 0; i < VPD_LEN; i += 4) {
  313. ret = csio_hw_seeprom_read(hw, addr + i, (uint32_t *)(vpd + i));
  314. if (ret) {
  315. kfree(vpd);
  316. return ret;
  317. }
  318. }
  319. /* Reset the VPD flag! */
  320. hw->flags &= (~CSIO_HWF_VPD_VALID);
  321. v = (const struct t4_vpd_hdr *)vpd;
  322. #define FIND_VPD_KW(var, name) do { \
  323. var = csio_hw_get_vpd_keyword_val(v, name); \
  324. if (var < 0) { \
  325. csio_err(hw, "missing VPD keyword " name "\n"); \
  326. kfree(vpd); \
  327. return -EINVAL; \
  328. } \
  329. } while (0)
  330. FIND_VPD_KW(i, "RV");
  331. for (csum = 0; i >= 0; i--)
  332. csum += vpd[i];
  333. if (csum) {
  334. csio_err(hw, "corrupted VPD EEPROM, actual csum %u\n", csum);
  335. kfree(vpd);
  336. return -EINVAL;
  337. }
  338. FIND_VPD_KW(ec, "EC");
  339. FIND_VPD_KW(sn, "SN");
  340. #undef FIND_VPD_KW
  341. memcpy(p->id, v->id_data, ID_LEN);
  342. s = strstrip(p->id);
  343. memcpy(p->ec, vpd + ec, EC_LEN);
  344. s = strstrip(p->ec);
  345. i = vpd[sn - VPD_INFO_FLD_HDR_SIZE + 2];
  346. memcpy(p->sn, vpd + sn, min(i, SERNUM_LEN));
  347. s = strstrip(p->sn);
  348. csio_valid_vpd_copied(hw);
  349. kfree(vpd);
  350. return 0;
  351. }
  352. /*
  353. * csio_hw_sf1_read - read data from the serial flash
  354. * @hw: the HW module
  355. * @byte_cnt: number of bytes to read
  356. * @cont: whether another operation will be chained
  357. * @lock: whether to lock SF for PL access only
  358. * @valp: where to store the read data
  359. *
  360. * Reads up to 4 bytes of data from the serial flash. The location of
  361. * the read needs to be specified prior to calling this by issuing the
  362. * appropriate commands to the serial flash.
  363. */
  364. static int
  365. csio_hw_sf1_read(struct csio_hw *hw, uint32_t byte_cnt, int32_t cont,
  366. int32_t lock, uint32_t *valp)
  367. {
  368. int ret;
  369. if (!byte_cnt || byte_cnt > 4)
  370. return -EINVAL;
  371. if (csio_rd_reg32(hw, SF_OP) & SF_BUSY)
  372. return -EBUSY;
  373. cont = cont ? SF_CONT : 0;
  374. lock = lock ? SF_LOCK : 0;
  375. csio_wr_reg32(hw, lock | cont | BYTECNT(byte_cnt - 1), SF_OP);
  376. ret = csio_hw_wait_op_done_val(hw, SF_OP, SF_BUSY, 0, SF_ATTEMPTS,
  377. 10, NULL);
  378. if (!ret)
  379. *valp = csio_rd_reg32(hw, SF_DATA);
  380. return ret;
  381. }
  382. /*
  383. * csio_hw_sf1_write - write data to the serial flash
  384. * @hw: the HW module
  385. * @byte_cnt: number of bytes to write
  386. * @cont: whether another operation will be chained
  387. * @lock: whether to lock SF for PL access only
  388. * @val: value to write
  389. *
  390. * Writes up to 4 bytes of data to the serial flash. The location of
  391. * the write needs to be specified prior to calling this by issuing the
  392. * appropriate commands to the serial flash.
  393. */
  394. static int
  395. csio_hw_sf1_write(struct csio_hw *hw, uint32_t byte_cnt, uint32_t cont,
  396. int32_t lock, uint32_t val)
  397. {
  398. if (!byte_cnt || byte_cnt > 4)
  399. return -EINVAL;
  400. if (csio_rd_reg32(hw, SF_OP) & SF_BUSY)
  401. return -EBUSY;
  402. cont = cont ? SF_CONT : 0;
  403. lock = lock ? SF_LOCK : 0;
  404. csio_wr_reg32(hw, val, SF_DATA);
  405. csio_wr_reg32(hw, cont | BYTECNT(byte_cnt - 1) | OP_WR | lock, SF_OP);
  406. return csio_hw_wait_op_done_val(hw, SF_OP, SF_BUSY, 0, SF_ATTEMPTS,
  407. 10, NULL);
  408. }
  409. /*
  410. * csio_hw_flash_wait_op - wait for a flash operation to complete
  411. * @hw: the HW module
  412. * @attempts: max number of polls of the status register
  413. * @delay: delay between polls in ms
  414. *
  415. * Wait for a flash operation to complete by polling the status register.
  416. */
  417. static int
  418. csio_hw_flash_wait_op(struct csio_hw *hw, int32_t attempts, int32_t delay)
  419. {
  420. int ret;
  421. uint32_t status;
  422. while (1) {
  423. ret = csio_hw_sf1_write(hw, 1, 1, 1, SF_RD_STATUS);
  424. if (ret != 0)
  425. return ret;
  426. ret = csio_hw_sf1_read(hw, 1, 0, 1, &status);
  427. if (ret != 0)
  428. return ret;
  429. if (!(status & 1))
  430. return 0;
  431. if (--attempts == 0)
  432. return -EAGAIN;
  433. if (delay)
  434. msleep(delay);
  435. }
  436. }
  437. /*
  438. * csio_hw_read_flash - read words from serial flash
  439. * @hw: the HW module
  440. * @addr: the start address for the read
  441. * @nwords: how many 32-bit words to read
  442. * @data: where to store the read data
  443. * @byte_oriented: whether to store data as bytes or as words
  444. *
  445. * Read the specified number of 32-bit words from the serial flash.
  446. * If @byte_oriented is set the read data is stored as a byte array
  447. * (i.e., big-endian), otherwise as 32-bit words in the platform's
  448. * natural endianess.
  449. */
  450. static int
  451. csio_hw_read_flash(struct csio_hw *hw, uint32_t addr, uint32_t nwords,
  452. uint32_t *data, int32_t byte_oriented)
  453. {
  454. int ret;
  455. if (addr + nwords * sizeof(uint32_t) > hw->params.sf_size || (addr & 3))
  456. return -EINVAL;
  457. addr = swab32(addr) | SF_RD_DATA_FAST;
  458. ret = csio_hw_sf1_write(hw, 4, 1, 0, addr);
  459. if (ret != 0)
  460. return ret;
  461. ret = csio_hw_sf1_read(hw, 1, 1, 0, data);
  462. if (ret != 0)
  463. return ret;
  464. for ( ; nwords; nwords--, data++) {
  465. ret = csio_hw_sf1_read(hw, 4, nwords > 1, nwords == 1, data);
  466. if (nwords == 1)
  467. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  468. if (ret)
  469. return ret;
  470. if (byte_oriented)
  471. *data = htonl(*data);
  472. }
  473. return 0;
  474. }
  475. /*
  476. * csio_hw_write_flash - write up to a page of data to the serial flash
  477. * @hw: the hw
  478. * @addr: the start address to write
  479. * @n: length of data to write in bytes
  480. * @data: the data to write
  481. *
  482. * Writes up to a page of data (256 bytes) to the serial flash starting
  483. * at the given address. All the data must be written to the same page.
  484. */
  485. static int
  486. csio_hw_write_flash(struct csio_hw *hw, uint32_t addr,
  487. uint32_t n, const uint8_t *data)
  488. {
  489. int ret = -EINVAL;
  490. uint32_t buf[64];
  491. uint32_t i, c, left, val, offset = addr & 0xff;
  492. if (addr >= hw->params.sf_size || offset + n > SF_PAGE_SIZE)
  493. return -EINVAL;
  494. val = swab32(addr) | SF_PROG_PAGE;
  495. ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
  496. if (ret != 0)
  497. goto unlock;
  498. ret = csio_hw_sf1_write(hw, 4, 1, 1, val);
  499. if (ret != 0)
  500. goto unlock;
  501. for (left = n; left; left -= c) {
  502. c = min(left, 4U);
  503. for (val = 0, i = 0; i < c; ++i)
  504. val = (val << 8) + *data++;
  505. ret = csio_hw_sf1_write(hw, c, c != left, 1, val);
  506. if (ret)
  507. goto unlock;
  508. }
  509. ret = csio_hw_flash_wait_op(hw, 8, 1);
  510. if (ret)
  511. goto unlock;
  512. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  513. /* Read the page to verify the write succeeded */
  514. ret = csio_hw_read_flash(hw, addr & ~0xff, ARRAY_SIZE(buf), buf, 1);
  515. if (ret)
  516. return ret;
  517. if (memcmp(data - n, (uint8_t *)buf + offset, n)) {
  518. csio_err(hw,
  519. "failed to correctly write the flash page at %#x\n",
  520. addr);
  521. return -EINVAL;
  522. }
  523. return 0;
  524. unlock:
  525. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  526. return ret;
  527. }
  528. /*
  529. * csio_hw_flash_erase_sectors - erase a range of flash sectors
  530. * @hw: the HW module
  531. * @start: the first sector to erase
  532. * @end: the last sector to erase
  533. *
  534. * Erases the sectors in the given inclusive range.
  535. */
  536. static int
  537. csio_hw_flash_erase_sectors(struct csio_hw *hw, int32_t start, int32_t end)
  538. {
  539. int ret = 0;
  540. while (start <= end) {
  541. ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
  542. if (ret != 0)
  543. goto out;
  544. ret = csio_hw_sf1_write(hw, 4, 0, 1,
  545. SF_ERASE_SECTOR | (start << 8));
  546. if (ret != 0)
  547. goto out;
  548. ret = csio_hw_flash_wait_op(hw, 14, 500);
  549. if (ret != 0)
  550. goto out;
  551. start++;
  552. }
  553. out:
  554. if (ret)
  555. csio_err(hw, "erase of flash sector %d failed, error %d\n",
  556. start, ret);
  557. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  558. return 0;
  559. }
  560. static void
  561. csio_hw_print_fw_version(struct csio_hw *hw, char *str)
  562. {
  563. csio_info(hw, "%s: %u.%u.%u.%u\n", str,
  564. FW_HDR_FW_VER_MAJOR_GET(hw->fwrev),
  565. FW_HDR_FW_VER_MINOR_GET(hw->fwrev),
  566. FW_HDR_FW_VER_MICRO_GET(hw->fwrev),
  567. FW_HDR_FW_VER_BUILD_GET(hw->fwrev));
  568. }
  569. /*
  570. * csio_hw_get_fw_version - read the firmware version
  571. * @hw: HW module
  572. * @vers: where to place the version
  573. *
  574. * Reads the FW version from flash.
  575. */
  576. static int
  577. csio_hw_get_fw_version(struct csio_hw *hw, uint32_t *vers)
  578. {
  579. return csio_hw_read_flash(hw, FW_IMG_START +
  580. offsetof(struct fw_hdr, fw_ver), 1,
  581. vers, 0);
  582. }
  583. /*
  584. * csio_hw_get_tp_version - read the TP microcode version
  585. * @hw: HW module
  586. * @vers: where to place the version
  587. *
  588. * Reads the TP microcode version from flash.
  589. */
  590. static int
  591. csio_hw_get_tp_version(struct csio_hw *hw, u32 *vers)
  592. {
  593. return csio_hw_read_flash(hw, FLASH_FW_START +
  594. offsetof(struct fw_hdr, tp_microcode_ver), 1,
  595. vers, 0);
  596. }
  597. /*
  598. * csio_hw_check_fw_version - check if the FW is compatible with
  599. * this driver
  600. * @hw: HW module
  601. *
  602. * Checks if an adapter's FW is compatible with the driver. Returns 0
  603. * if there's exact match, a negative error if the version could not be
  604. * read or there's a major/minor version mismatch/minor.
  605. */
  606. static int
  607. csio_hw_check_fw_version(struct csio_hw *hw)
  608. {
  609. int ret, major, minor, micro;
  610. ret = csio_hw_get_fw_version(hw, &hw->fwrev);
  611. if (!ret)
  612. ret = csio_hw_get_tp_version(hw, &hw->tp_vers);
  613. if (ret)
  614. return ret;
  615. major = FW_HDR_FW_VER_MAJOR_GET(hw->fwrev);
  616. minor = FW_HDR_FW_VER_MINOR_GET(hw->fwrev);
  617. micro = FW_HDR_FW_VER_MICRO_GET(hw->fwrev);
  618. if (major != FW_VERSION_MAJOR(hw)) { /* major mismatch - fail */
  619. csio_err(hw, "card FW has major version %u, driver wants %u\n",
  620. major, FW_VERSION_MAJOR(hw));
  621. return -EINVAL;
  622. }
  623. if (minor == FW_VERSION_MINOR(hw) && micro == FW_VERSION_MICRO(hw))
  624. return 0; /* perfect match */
  625. /* Minor/micro version mismatch */
  626. return -EINVAL;
  627. }
  628. /*
  629. * csio_hw_fw_dload - download firmware.
  630. * @hw: HW module
  631. * @fw_data: firmware image to write.
  632. * @size: image size
  633. *
  634. * Write the supplied firmware image to the card's serial flash.
  635. */
  636. static int
  637. csio_hw_fw_dload(struct csio_hw *hw, uint8_t *fw_data, uint32_t size)
  638. {
  639. uint32_t csum;
  640. int32_t addr;
  641. int ret;
  642. uint32_t i;
  643. uint8_t first_page[SF_PAGE_SIZE];
  644. const __be32 *p = (const __be32 *)fw_data;
  645. struct fw_hdr *hdr = (struct fw_hdr *)fw_data;
  646. uint32_t sf_sec_size;
  647. if ((!hw->params.sf_size) || (!hw->params.sf_nsec)) {
  648. csio_err(hw, "Serial Flash data invalid\n");
  649. return -EINVAL;
  650. }
  651. if (!size) {
  652. csio_err(hw, "FW image has no data\n");
  653. return -EINVAL;
  654. }
  655. if (size & 511) {
  656. csio_err(hw, "FW image size not multiple of 512 bytes\n");
  657. return -EINVAL;
  658. }
  659. if (ntohs(hdr->len512) * 512 != size) {
  660. csio_err(hw, "FW image size differs from size in FW header\n");
  661. return -EINVAL;
  662. }
  663. if (size > FW_MAX_SIZE) {
  664. csio_err(hw, "FW image too large, max is %u bytes\n",
  665. FW_MAX_SIZE);
  666. return -EINVAL;
  667. }
  668. for (csum = 0, i = 0; i < size / sizeof(csum); i++)
  669. csum += ntohl(p[i]);
  670. if (csum != 0xffffffff) {
  671. csio_err(hw, "corrupted firmware image, checksum %#x\n", csum);
  672. return -EINVAL;
  673. }
  674. sf_sec_size = hw->params.sf_size / hw->params.sf_nsec;
  675. i = DIV_ROUND_UP(size, sf_sec_size); /* # of sectors spanned */
  676. csio_dbg(hw, "Erasing sectors... start:%d end:%d\n",
  677. FW_START_SEC, FW_START_SEC + i - 1);
  678. ret = csio_hw_flash_erase_sectors(hw, FW_START_SEC,
  679. FW_START_SEC + i - 1);
  680. if (ret) {
  681. csio_err(hw, "Flash Erase failed\n");
  682. goto out;
  683. }
  684. /*
  685. * We write the correct version at the end so the driver can see a bad
  686. * version if the FW write fails. Start by writing a copy of the
  687. * first page with a bad version.
  688. */
  689. memcpy(first_page, fw_data, SF_PAGE_SIZE);
  690. ((struct fw_hdr *)first_page)->fw_ver = htonl(0xffffffff);
  691. ret = csio_hw_write_flash(hw, FW_IMG_START, SF_PAGE_SIZE, first_page);
  692. if (ret)
  693. goto out;
  694. csio_dbg(hw, "Writing Flash .. start:%d end:%d\n",
  695. FW_IMG_START, FW_IMG_START + size);
  696. addr = FW_IMG_START;
  697. for (size -= SF_PAGE_SIZE; size; size -= SF_PAGE_SIZE) {
  698. addr += SF_PAGE_SIZE;
  699. fw_data += SF_PAGE_SIZE;
  700. ret = csio_hw_write_flash(hw, addr, SF_PAGE_SIZE, fw_data);
  701. if (ret)
  702. goto out;
  703. }
  704. ret = csio_hw_write_flash(hw,
  705. FW_IMG_START +
  706. offsetof(struct fw_hdr, fw_ver),
  707. sizeof(hdr->fw_ver),
  708. (const uint8_t *)&hdr->fw_ver);
  709. out:
  710. if (ret)
  711. csio_err(hw, "firmware download failed, error %d\n", ret);
  712. return ret;
  713. }
  714. static int
  715. csio_hw_get_flash_params(struct csio_hw *hw)
  716. {
  717. int ret;
  718. uint32_t info = 0;
  719. ret = csio_hw_sf1_write(hw, 1, 1, 0, SF_RD_ID);
  720. if (!ret)
  721. ret = csio_hw_sf1_read(hw, 3, 0, 1, &info);
  722. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  723. if (ret != 0)
  724. return ret;
  725. if ((info & 0xff) != 0x20) /* not a Numonix flash */
  726. return -EINVAL;
  727. info >>= 16; /* log2 of size */
  728. if (info >= 0x14 && info < 0x18)
  729. hw->params.sf_nsec = 1 << (info - 16);
  730. else if (info == 0x18)
  731. hw->params.sf_nsec = 64;
  732. else
  733. return -EINVAL;
  734. hw->params.sf_size = 1 << info;
  735. return 0;
  736. }
  737. static void
  738. csio_set_pcie_completion_timeout(struct csio_hw *hw, u8 range)
  739. {
  740. uint16_t val;
  741. int pcie_cap;
  742. if (!csio_pci_capability(hw->pdev, PCI_CAP_ID_EXP, &pcie_cap)) {
  743. pci_read_config_word(hw->pdev,
  744. pcie_cap + PCI_EXP_DEVCTL2, &val);
  745. val &= 0xfff0;
  746. val |= range ;
  747. pci_write_config_word(hw->pdev,
  748. pcie_cap + PCI_EXP_DEVCTL2, val);
  749. }
  750. }
  751. /*****************************************************************************/
  752. /* HW State machine assists */
  753. /*****************************************************************************/
  754. static int
  755. csio_hw_dev_ready(struct csio_hw *hw)
  756. {
  757. uint32_t reg;
  758. int cnt = 6;
  759. while (((reg = csio_rd_reg32(hw, PL_WHOAMI)) == 0xFFFFFFFF) &&
  760. (--cnt != 0))
  761. mdelay(100);
  762. if ((cnt == 0) && (((int32_t)(SOURCEPF_GET(reg)) < 0) ||
  763. (SOURCEPF_GET(reg) >= CSIO_MAX_PFN))) {
  764. csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
  765. return -EIO;
  766. }
  767. hw->pfn = SOURCEPF_GET(reg);
  768. return 0;
  769. }
  770. /*
  771. * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
  772. * @hw: HW module
  773. * @state: Device state
  774. *
  775. * FW_HELLO_CMD has to be polled for completion.
  776. */
  777. static int
  778. csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
  779. {
  780. struct csio_mb *mbp;
  781. int rv = 0;
  782. enum csio_dev_master master;
  783. enum fw_retval retval;
  784. uint8_t mpfn;
  785. char state_str[16];
  786. int retries = FW_CMD_HELLO_RETRIES;
  787. memset(state_str, 0, sizeof(state_str));
  788. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  789. if (!mbp) {
  790. rv = -ENOMEM;
  791. CSIO_INC_STATS(hw, n_err_nomem);
  792. goto out;
  793. }
  794. master = csio_force_master ? CSIO_MASTER_MUST : CSIO_MASTER_MAY;
  795. retry:
  796. csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
  797. hw->pfn, master, NULL);
  798. rv = csio_mb_issue(hw, mbp);
  799. if (rv) {
  800. csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
  801. goto out_free_mb;
  802. }
  803. csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
  804. if (retval != FW_SUCCESS) {
  805. csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
  806. rv = -EINVAL;
  807. goto out_free_mb;
  808. }
  809. /* Firmware has designated us to be master */
  810. if (hw->pfn == mpfn) {
  811. hw->flags |= CSIO_HWF_MASTER;
  812. } else if (*state == CSIO_DEV_STATE_UNINIT) {
  813. /*
  814. * If we're not the Master PF then we need to wait around for
  815. * the Master PF Driver to finish setting up the adapter.
  816. *
  817. * Note that we also do this wait if we're a non-Master-capable
  818. * PF and there is no current Master PF; a Master PF may show up
  819. * momentarily and we wouldn't want to fail pointlessly. (This
  820. * can happen when an OS loads lots of different drivers rapidly
  821. * at the same time). In this case, the Master PF returned by
  822. * the firmware will be PCIE_FW_MASTER_MASK so the test below
  823. * will work ...
  824. */
  825. int waiting = FW_CMD_HELLO_TIMEOUT;
  826. /*
  827. * Wait for the firmware to either indicate an error or
  828. * initialized state. If we see either of these we bail out
  829. * and report the issue to the caller. If we exhaust the
  830. * "hello timeout" and we haven't exhausted our retries, try
  831. * again. Otherwise bail with a timeout error.
  832. */
  833. for (;;) {
  834. uint32_t pcie_fw;
  835. spin_unlock_irq(&hw->lock);
  836. msleep(50);
  837. spin_lock_irq(&hw->lock);
  838. waiting -= 50;
  839. /*
  840. * If neither Error nor Initialialized are indicated
  841. * by the firmware keep waiting till we exaust our
  842. * timeout ... and then retry if we haven't exhausted
  843. * our retries ...
  844. */
  845. pcie_fw = csio_rd_reg32(hw, PCIE_FW);
  846. if (!(pcie_fw & (PCIE_FW_ERR|PCIE_FW_INIT))) {
  847. if (waiting <= 0) {
  848. if (retries-- > 0)
  849. goto retry;
  850. rv = -ETIMEDOUT;
  851. break;
  852. }
  853. continue;
  854. }
  855. /*
  856. * We either have an Error or Initialized condition
  857. * report errors preferentially.
  858. */
  859. if (state) {
  860. if (pcie_fw & PCIE_FW_ERR) {
  861. *state = CSIO_DEV_STATE_ERR;
  862. rv = -ETIMEDOUT;
  863. } else if (pcie_fw & PCIE_FW_INIT)
  864. *state = CSIO_DEV_STATE_INIT;
  865. }
  866. /*
  867. * If we arrived before a Master PF was selected and
  868. * there's not a valid Master PF, grab its identity
  869. * for our caller.
  870. */
  871. if (mpfn == PCIE_FW_MASTER_MASK &&
  872. (pcie_fw & PCIE_FW_MASTER_VLD))
  873. mpfn = PCIE_FW_MASTER_GET(pcie_fw);
  874. break;
  875. }
  876. hw->flags &= ~CSIO_HWF_MASTER;
  877. }
  878. switch (*state) {
  879. case CSIO_DEV_STATE_UNINIT:
  880. strcpy(state_str, "Initializing");
  881. break;
  882. case CSIO_DEV_STATE_INIT:
  883. strcpy(state_str, "Initialized");
  884. break;
  885. case CSIO_DEV_STATE_ERR:
  886. strcpy(state_str, "Error");
  887. break;
  888. default:
  889. strcpy(state_str, "Unknown");
  890. break;
  891. }
  892. if (hw->pfn == mpfn)
  893. csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
  894. hw->pfn, state_str);
  895. else
  896. csio_info(hw,
  897. "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
  898. hw->pfn, mpfn, state_str);
  899. out_free_mb:
  900. mempool_free(mbp, hw->mb_mempool);
  901. out:
  902. return rv;
  903. }
  904. /*
  905. * csio_do_bye - Perform the BYE FW Mailbox command and process response.
  906. * @hw: HW module
  907. *
  908. */
  909. static int
  910. csio_do_bye(struct csio_hw *hw)
  911. {
  912. struct csio_mb *mbp;
  913. enum fw_retval retval;
  914. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  915. if (!mbp) {
  916. CSIO_INC_STATS(hw, n_err_nomem);
  917. return -ENOMEM;
  918. }
  919. csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  920. if (csio_mb_issue(hw, mbp)) {
  921. csio_err(hw, "Issue of BYE command failed\n");
  922. mempool_free(mbp, hw->mb_mempool);
  923. return -EINVAL;
  924. }
  925. retval = csio_mb_fw_retval(mbp);
  926. if (retval != FW_SUCCESS) {
  927. mempool_free(mbp, hw->mb_mempool);
  928. return -EINVAL;
  929. }
  930. mempool_free(mbp, hw->mb_mempool);
  931. return 0;
  932. }
  933. /*
  934. * csio_do_reset- Perform the device reset.
  935. * @hw: HW module
  936. * @fw_rst: FW reset
  937. *
  938. * If fw_rst is set, issues FW reset mbox cmd otherwise
  939. * does PIO reset.
  940. * Performs reset of the function.
  941. */
  942. static int
  943. csio_do_reset(struct csio_hw *hw, bool fw_rst)
  944. {
  945. struct csio_mb *mbp;
  946. enum fw_retval retval;
  947. if (!fw_rst) {
  948. /* PIO reset */
  949. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  950. mdelay(2000);
  951. return 0;
  952. }
  953. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  954. if (!mbp) {
  955. CSIO_INC_STATS(hw, n_err_nomem);
  956. return -ENOMEM;
  957. }
  958. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  959. PIORSTMODE | PIORST, 0, NULL);
  960. if (csio_mb_issue(hw, mbp)) {
  961. csio_err(hw, "Issue of RESET command failed.n");
  962. mempool_free(mbp, hw->mb_mempool);
  963. return -EINVAL;
  964. }
  965. retval = csio_mb_fw_retval(mbp);
  966. if (retval != FW_SUCCESS) {
  967. csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
  968. mempool_free(mbp, hw->mb_mempool);
  969. return -EINVAL;
  970. }
  971. mempool_free(mbp, hw->mb_mempool);
  972. return 0;
  973. }
  974. static int
  975. csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
  976. {
  977. struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
  978. uint16_t caps;
  979. caps = ntohs(rsp->fcoecaps);
  980. if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
  981. csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
  982. return -EINVAL;
  983. }
  984. if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
  985. csio_err(hw, "No FCoE Control Offload capability\n");
  986. return -EINVAL;
  987. }
  988. return 0;
  989. }
  990. /*
  991. * csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
  992. * @hw: the HW module
  993. * @mbox: mailbox to use for the FW RESET command (if desired)
  994. * @force: force uP into RESET even if FW RESET command fails
  995. *
  996. * Issues a RESET command to firmware (if desired) with a HALT indication
  997. * and then puts the microprocessor into RESET state. The RESET command
  998. * will only be issued if a legitimate mailbox is provided (mbox <=
  999. * PCIE_FW_MASTER_MASK).
  1000. *
  1001. * This is generally used in order for the host to safely manipulate the
  1002. * adapter without fear of conflicting with whatever the firmware might
  1003. * be doing. The only way out of this state is to RESTART the firmware
  1004. * ...
  1005. */
  1006. static int
  1007. csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
  1008. {
  1009. enum fw_retval retval = 0;
  1010. /*
  1011. * If a legitimate mailbox is provided, issue a RESET command
  1012. * with a HALT indication.
  1013. */
  1014. if (mbox <= PCIE_FW_MASTER_MASK) {
  1015. struct csio_mb *mbp;
  1016. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1017. if (!mbp) {
  1018. CSIO_INC_STATS(hw, n_err_nomem);
  1019. return -ENOMEM;
  1020. }
  1021. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1022. PIORSTMODE | PIORST, FW_RESET_CMD_HALT(1),
  1023. NULL);
  1024. if (csio_mb_issue(hw, mbp)) {
  1025. csio_err(hw, "Issue of RESET command failed!\n");
  1026. mempool_free(mbp, hw->mb_mempool);
  1027. return -EINVAL;
  1028. }
  1029. retval = csio_mb_fw_retval(mbp);
  1030. mempool_free(mbp, hw->mb_mempool);
  1031. }
  1032. /*
  1033. * Normally we won't complete the operation if the firmware RESET
  1034. * command fails but if our caller insists we'll go ahead and put the
  1035. * uP into RESET. This can be useful if the firmware is hung or even
  1036. * missing ... We'll have to take the risk of putting the uP into
  1037. * RESET without the cooperation of firmware in that case.
  1038. *
  1039. * We also force the firmware's HALT flag to be on in case we bypassed
  1040. * the firmware RESET command above or we're dealing with old firmware
  1041. * which doesn't have the HALT capability. This will serve as a flag
  1042. * for the incoming firmware to know that it's coming out of a HALT
  1043. * rather than a RESET ... if it's new enough to understand that ...
  1044. */
  1045. if (retval == 0 || force) {
  1046. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, UPCRST);
  1047. csio_set_reg_field(hw, PCIE_FW, PCIE_FW_HALT, PCIE_FW_HALT);
  1048. }
  1049. /*
  1050. * And we always return the result of the firmware RESET command
  1051. * even when we force the uP into RESET ...
  1052. */
  1053. return retval ? -EINVAL : 0;
  1054. }
  1055. /*
  1056. * csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
  1057. * @hw: the HW module
  1058. * @reset: if we want to do a RESET to restart things
  1059. *
  1060. * Restart firmware previously halted by csio_hw_fw_halt(). On successful
  1061. * return the previous PF Master remains as the new PF Master and there
  1062. * is no need to issue a new HELLO command, etc.
  1063. *
  1064. * We do this in two ways:
  1065. *
  1066. * 1. If we're dealing with newer firmware we'll simply want to take
  1067. * the chip's microprocessor out of RESET. This will cause the
  1068. * firmware to start up from its start vector. And then we'll loop
  1069. * until the firmware indicates it's started again (PCIE_FW.HALT
  1070. * reset to 0) or we timeout.
  1071. *
  1072. * 2. If we're dealing with older firmware then we'll need to RESET
  1073. * the chip since older firmware won't recognize the PCIE_FW.HALT
  1074. * flag and automatically RESET itself on startup.
  1075. */
  1076. static int
  1077. csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
  1078. {
  1079. if (reset) {
  1080. /*
  1081. * Since we're directing the RESET instead of the firmware
  1082. * doing it automatically, we need to clear the PCIE_FW.HALT
  1083. * bit.
  1084. */
  1085. csio_set_reg_field(hw, PCIE_FW, PCIE_FW_HALT, 0);
  1086. /*
  1087. * If we've been given a valid mailbox, first try to get the
  1088. * firmware to do the RESET. If that works, great and we can
  1089. * return success. Otherwise, if we haven't been given a
  1090. * valid mailbox or the RESET command failed, fall back to
  1091. * hitting the chip with a hammer.
  1092. */
  1093. if (mbox <= PCIE_FW_MASTER_MASK) {
  1094. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, 0);
  1095. msleep(100);
  1096. if (csio_do_reset(hw, true) == 0)
  1097. return 0;
  1098. }
  1099. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  1100. msleep(2000);
  1101. } else {
  1102. int ms;
  1103. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, 0);
  1104. for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
  1105. if (!(csio_rd_reg32(hw, PCIE_FW) & PCIE_FW_HALT))
  1106. return 0;
  1107. msleep(100);
  1108. ms += 100;
  1109. }
  1110. return -ETIMEDOUT;
  1111. }
  1112. return 0;
  1113. }
  1114. /*
  1115. * csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
  1116. * @hw: the HW module
  1117. * @mbox: mailbox to use for the FW RESET command (if desired)
  1118. * @fw_data: the firmware image to write
  1119. * @size: image size
  1120. * @force: force upgrade even if firmware doesn't cooperate
  1121. *
  1122. * Perform all of the steps necessary for upgrading an adapter's
  1123. * firmware image. Normally this requires the cooperation of the
  1124. * existing firmware in order to halt all existing activities
  1125. * but if an invalid mailbox token is passed in we skip that step
  1126. * (though we'll still put the adapter microprocessor into RESET in
  1127. * that case).
  1128. *
  1129. * On successful return the new firmware will have been loaded and
  1130. * the adapter will have been fully RESET losing all previous setup
  1131. * state. On unsuccessful return the adapter may be completely hosed ...
  1132. * positive errno indicates that the adapter is ~probably~ intact, a
  1133. * negative errno indicates that things are looking bad ...
  1134. */
  1135. static int
  1136. csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
  1137. const u8 *fw_data, uint32_t size, int32_t force)
  1138. {
  1139. const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
  1140. int reset, ret;
  1141. ret = csio_hw_fw_halt(hw, mbox, force);
  1142. if (ret != 0 && !force)
  1143. return ret;
  1144. ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
  1145. if (ret != 0)
  1146. return ret;
  1147. /*
  1148. * Older versions of the firmware don't understand the new
  1149. * PCIE_FW.HALT flag and so won't know to perform a RESET when they
  1150. * restart. So for newly loaded older firmware we'll have to do the
  1151. * RESET for it so it starts up on a clean slate. We can tell if
  1152. * the newly loaded firmware will handle this right by checking
  1153. * its header flags to see if it advertises the capability.
  1154. */
  1155. reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
  1156. return csio_hw_fw_restart(hw, mbox, reset);
  1157. }
  1158. /*
  1159. * csio_hw_fw_config_file - setup an adapter via a Configuration File
  1160. * @hw: the HW module
  1161. * @mbox: mailbox to use for the FW command
  1162. * @mtype: the memory type where the Configuration File is located
  1163. * @maddr: the memory address where the Configuration File is located
  1164. * @finiver: return value for CF [fini] version
  1165. * @finicsum: return value for CF [fini] checksum
  1166. * @cfcsum: return value for CF computed checksum
  1167. *
  1168. * Issue a command to get the firmware to process the Configuration
  1169. * File located at the specified mtype/maddress. If the Configuration
  1170. * File is processed successfully and return value pointers are
  1171. * provided, the Configuration File "[fini] section version and
  1172. * checksum values will be returned along with the computed checksum.
  1173. * It's up to the caller to decide how it wants to respond to the
  1174. * checksums not matching but it recommended that a prominant warning
  1175. * be emitted in order to help people rapidly identify changed or
  1176. * corrupted Configuration Files.
  1177. *
  1178. * Also note that it's possible to modify things like "niccaps",
  1179. * "toecaps",etc. between processing the Configuration File and telling
  1180. * the firmware to use the new configuration. Callers which want to
  1181. * do this will need to "hand-roll" their own CAPS_CONFIGS commands for
  1182. * Configuration Files if they want to do this.
  1183. */
  1184. static int
  1185. csio_hw_fw_config_file(struct csio_hw *hw,
  1186. unsigned int mtype, unsigned int maddr,
  1187. uint32_t *finiver, uint32_t *finicsum, uint32_t *cfcsum)
  1188. {
  1189. struct csio_mb *mbp;
  1190. struct fw_caps_config_cmd *caps_cmd;
  1191. int rv = -EINVAL;
  1192. enum fw_retval ret;
  1193. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1194. if (!mbp) {
  1195. CSIO_INC_STATS(hw, n_err_nomem);
  1196. return -ENOMEM;
  1197. }
  1198. /*
  1199. * Tell the firmware to process the indicated Configuration File.
  1200. * If there are no errors and the caller has provided return value
  1201. * pointers for the [fini] section version, checksum and computed
  1202. * checksum, pass those back to the caller.
  1203. */
  1204. caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
  1205. CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
  1206. caps_cmd->op_to_write =
  1207. htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  1208. FW_CMD_REQUEST |
  1209. FW_CMD_READ);
  1210. caps_cmd->cfvalid_to_len16 =
  1211. htonl(FW_CAPS_CONFIG_CMD_CFVALID |
  1212. FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
  1213. FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
  1214. FW_LEN16(*caps_cmd));
  1215. if (csio_mb_issue(hw, mbp)) {
  1216. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
  1217. goto out;
  1218. }
  1219. ret = csio_mb_fw_retval(mbp);
  1220. if (ret != FW_SUCCESS) {
  1221. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1222. goto out;
  1223. }
  1224. if (finiver)
  1225. *finiver = ntohl(caps_cmd->finiver);
  1226. if (finicsum)
  1227. *finicsum = ntohl(caps_cmd->finicsum);
  1228. if (cfcsum)
  1229. *cfcsum = ntohl(caps_cmd->cfcsum);
  1230. /* Validate device capabilities */
  1231. if (csio_hw_validate_caps(hw, mbp)) {
  1232. rv = -ENOENT;
  1233. goto out;
  1234. }
  1235. /*
  1236. * And now tell the firmware to use the configuration we just loaded.
  1237. */
  1238. caps_cmd->op_to_write =
  1239. htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  1240. FW_CMD_REQUEST |
  1241. FW_CMD_WRITE);
  1242. caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
  1243. if (csio_mb_issue(hw, mbp)) {
  1244. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
  1245. goto out;
  1246. }
  1247. ret = csio_mb_fw_retval(mbp);
  1248. if (ret != FW_SUCCESS) {
  1249. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1250. goto out;
  1251. }
  1252. rv = 0;
  1253. out:
  1254. mempool_free(mbp, hw->mb_mempool);
  1255. return rv;
  1256. }
  1257. /*
  1258. * csio_get_device_params - Get device parameters.
  1259. * @hw: HW module
  1260. *
  1261. */
  1262. static int
  1263. csio_get_device_params(struct csio_hw *hw)
  1264. {
  1265. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1266. struct csio_mb *mbp;
  1267. enum fw_retval retval;
  1268. u32 param[6];
  1269. int i, j = 0;
  1270. /* Initialize portids to -1 */
  1271. for (i = 0; i < CSIO_MAX_PPORTS; i++)
  1272. hw->pport[i].portid = -1;
  1273. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1274. if (!mbp) {
  1275. CSIO_INC_STATS(hw, n_err_nomem);
  1276. return -ENOMEM;
  1277. }
  1278. /* Get port vec information. */
  1279. param[0] = FW_PARAM_DEV(PORTVEC);
  1280. /* Get Core clock. */
  1281. param[1] = FW_PARAM_DEV(CCLK);
  1282. /* Get EQ id start and end. */
  1283. param[2] = FW_PARAM_PFVF(EQ_START);
  1284. param[3] = FW_PARAM_PFVF(EQ_END);
  1285. /* Get IQ id start and end. */
  1286. param[4] = FW_PARAM_PFVF(IQFLINT_START);
  1287. param[5] = FW_PARAM_PFVF(IQFLINT_END);
  1288. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1289. ARRAY_SIZE(param), param, NULL, false, NULL);
  1290. if (csio_mb_issue(hw, mbp)) {
  1291. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1292. mempool_free(mbp, hw->mb_mempool);
  1293. return -EINVAL;
  1294. }
  1295. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1296. ARRAY_SIZE(param), param);
  1297. if (retval != FW_SUCCESS) {
  1298. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1299. retval);
  1300. mempool_free(mbp, hw->mb_mempool);
  1301. return -EINVAL;
  1302. }
  1303. /* cache the information. */
  1304. hw->port_vec = param[0];
  1305. hw->vpd.cclk = param[1];
  1306. wrm->fw_eq_start = param[2];
  1307. wrm->fw_iq_start = param[4];
  1308. /* Using FW configured max iqs & eqs */
  1309. if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
  1310. !csio_is_hw_master(hw)) {
  1311. hw->cfg_niq = param[5] - param[4] + 1;
  1312. hw->cfg_neq = param[3] - param[2] + 1;
  1313. csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
  1314. hw->cfg_niq, hw->cfg_neq);
  1315. }
  1316. hw->port_vec &= csio_port_mask;
  1317. hw->num_pports = hweight32(hw->port_vec);
  1318. csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
  1319. hw->port_vec, hw->num_pports);
  1320. for (i = 0; i < hw->num_pports; i++) {
  1321. while ((hw->port_vec & (1 << j)) == 0)
  1322. j++;
  1323. hw->pport[i].portid = j++;
  1324. csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
  1325. }
  1326. mempool_free(mbp, hw->mb_mempool);
  1327. return 0;
  1328. }
  1329. /*
  1330. * csio_config_device_caps - Get and set device capabilities.
  1331. * @hw: HW module
  1332. *
  1333. */
  1334. static int
  1335. csio_config_device_caps(struct csio_hw *hw)
  1336. {
  1337. struct csio_mb *mbp;
  1338. enum fw_retval retval;
  1339. int rv = -EINVAL;
  1340. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1341. if (!mbp) {
  1342. CSIO_INC_STATS(hw, n_err_nomem);
  1343. return -ENOMEM;
  1344. }
  1345. /* Get device capabilities */
  1346. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
  1347. if (csio_mb_issue(hw, mbp)) {
  1348. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
  1349. goto out;
  1350. }
  1351. retval = csio_mb_fw_retval(mbp);
  1352. if (retval != FW_SUCCESS) {
  1353. csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
  1354. goto out;
  1355. }
  1356. /* Validate device capabilities */
  1357. if (csio_hw_validate_caps(hw, mbp))
  1358. goto out;
  1359. /* Don't config device capabilities if already configured */
  1360. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  1361. rv = 0;
  1362. goto out;
  1363. }
  1364. /* Write back desired device capabilities */
  1365. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
  1366. false, true, NULL);
  1367. if (csio_mb_issue(hw, mbp)) {
  1368. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
  1369. goto out;
  1370. }
  1371. retval = csio_mb_fw_retval(mbp);
  1372. if (retval != FW_SUCCESS) {
  1373. csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
  1374. goto out;
  1375. }
  1376. rv = 0;
  1377. out:
  1378. mempool_free(mbp, hw->mb_mempool);
  1379. return rv;
  1380. }
  1381. static int
  1382. csio_config_global_rss(struct csio_hw *hw)
  1383. {
  1384. struct csio_mb *mbp;
  1385. enum fw_retval retval;
  1386. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1387. if (!mbp) {
  1388. CSIO_INC_STATS(hw, n_err_nomem);
  1389. return -ENOMEM;
  1390. }
  1391. csio_rss_glb_config(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1392. FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
  1393. FW_RSS_GLB_CONFIG_CMD_TNLMAPEN |
  1394. FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ |
  1395. FW_RSS_GLB_CONFIG_CMD_TNLALLLKP,
  1396. NULL);
  1397. if (csio_mb_issue(hw, mbp)) {
  1398. csio_err(hw, "Issue of FW_RSS_GLB_CONFIG_CMD failed!\n");
  1399. mempool_free(mbp, hw->mb_mempool);
  1400. return -EINVAL;
  1401. }
  1402. retval = csio_mb_fw_retval(mbp);
  1403. if (retval != FW_SUCCESS) {
  1404. csio_err(hw, "FW_RSS_GLB_CONFIG_CMD returned 0x%x!\n", retval);
  1405. mempool_free(mbp, hw->mb_mempool);
  1406. return -EINVAL;
  1407. }
  1408. mempool_free(mbp, hw->mb_mempool);
  1409. return 0;
  1410. }
  1411. /*
  1412. * csio_config_pfvf - Configure Physical/Virtual functions settings.
  1413. * @hw: HW module
  1414. *
  1415. */
  1416. static int
  1417. csio_config_pfvf(struct csio_hw *hw)
  1418. {
  1419. struct csio_mb *mbp;
  1420. enum fw_retval retval;
  1421. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1422. if (!mbp) {
  1423. CSIO_INC_STATS(hw, n_err_nomem);
  1424. return -ENOMEM;
  1425. }
  1426. /*
  1427. * For now, allow all PFs to access to all ports using a pmask
  1428. * value of 0xF (M_FW_PFVF_CMD_PMASK). Once we have VFs, we will
  1429. * need to provide access based on some rule.
  1430. */
  1431. csio_mb_pfvf(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0, CSIO_NEQ,
  1432. CSIO_NETH_CTRL, CSIO_NIQ_FLINT, 0, 0, CSIO_NVI, CSIO_CMASK,
  1433. CSIO_PMASK, CSIO_NEXACTF, CSIO_R_CAPS, CSIO_WX_CAPS, NULL);
  1434. if (csio_mb_issue(hw, mbp)) {
  1435. csio_err(hw, "Issue of FW_PFVF_CMD failed!\n");
  1436. mempool_free(mbp, hw->mb_mempool);
  1437. return -EINVAL;
  1438. }
  1439. retval = csio_mb_fw_retval(mbp);
  1440. if (retval != FW_SUCCESS) {
  1441. csio_err(hw, "FW_PFVF_CMD returned 0x%x!\n", retval);
  1442. mempool_free(mbp, hw->mb_mempool);
  1443. return -EINVAL;
  1444. }
  1445. mempool_free(mbp, hw->mb_mempool);
  1446. return 0;
  1447. }
  1448. /*
  1449. * csio_enable_ports - Bring up all available ports.
  1450. * @hw: HW module.
  1451. *
  1452. */
  1453. static int
  1454. csio_enable_ports(struct csio_hw *hw)
  1455. {
  1456. struct csio_mb *mbp;
  1457. enum fw_retval retval;
  1458. uint8_t portid;
  1459. int i;
  1460. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1461. if (!mbp) {
  1462. CSIO_INC_STATS(hw, n_err_nomem);
  1463. return -ENOMEM;
  1464. }
  1465. for (i = 0; i < hw->num_pports; i++) {
  1466. portid = hw->pport[i].portid;
  1467. /* Read PORT information */
  1468. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
  1469. false, 0, 0, NULL);
  1470. if (csio_mb_issue(hw, mbp)) {
  1471. csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
  1472. portid);
  1473. mempool_free(mbp, hw->mb_mempool);
  1474. return -EINVAL;
  1475. }
  1476. csio_mb_process_read_port_rsp(hw, mbp, &retval,
  1477. &hw->pport[i].pcap);
  1478. if (retval != FW_SUCCESS) {
  1479. csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
  1480. portid, retval);
  1481. mempool_free(mbp, hw->mb_mempool);
  1482. return -EINVAL;
  1483. }
  1484. /* Write back PORT information */
  1485. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid, true,
  1486. (PAUSE_RX | PAUSE_TX), hw->pport[i].pcap, NULL);
  1487. if (csio_mb_issue(hw, mbp)) {
  1488. csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
  1489. portid);
  1490. mempool_free(mbp, hw->mb_mempool);
  1491. return -EINVAL;
  1492. }
  1493. retval = csio_mb_fw_retval(mbp);
  1494. if (retval != FW_SUCCESS) {
  1495. csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
  1496. portid, retval);
  1497. mempool_free(mbp, hw->mb_mempool);
  1498. return -EINVAL;
  1499. }
  1500. } /* For all ports */
  1501. mempool_free(mbp, hw->mb_mempool);
  1502. return 0;
  1503. }
  1504. /*
  1505. * csio_get_fcoe_resinfo - Read fcoe fw resource info.
  1506. * @hw: HW module
  1507. * Issued with lock held.
  1508. */
  1509. static int
  1510. csio_get_fcoe_resinfo(struct csio_hw *hw)
  1511. {
  1512. struct csio_fcoe_res_info *res_info = &hw->fres_info;
  1513. struct fw_fcoe_res_info_cmd *rsp;
  1514. struct csio_mb *mbp;
  1515. enum fw_retval retval;
  1516. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1517. if (!mbp) {
  1518. CSIO_INC_STATS(hw, n_err_nomem);
  1519. return -ENOMEM;
  1520. }
  1521. /* Get FCoE FW resource information */
  1522. csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1523. if (csio_mb_issue(hw, mbp)) {
  1524. csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
  1525. mempool_free(mbp, hw->mb_mempool);
  1526. return -EINVAL;
  1527. }
  1528. rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
  1529. retval = FW_CMD_RETVAL_GET(ntohl(rsp->retval_len16));
  1530. if (retval != FW_SUCCESS) {
  1531. csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
  1532. retval);
  1533. mempool_free(mbp, hw->mb_mempool);
  1534. return -EINVAL;
  1535. }
  1536. res_info->e_d_tov = ntohs(rsp->e_d_tov);
  1537. res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
  1538. res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
  1539. res_info->r_r_tov = ntohs(rsp->r_r_tov);
  1540. res_info->max_xchgs = ntohl(rsp->max_xchgs);
  1541. res_info->max_ssns = ntohl(rsp->max_ssns);
  1542. res_info->used_xchgs = ntohl(rsp->used_xchgs);
  1543. res_info->used_ssns = ntohl(rsp->used_ssns);
  1544. res_info->max_fcfs = ntohl(rsp->max_fcfs);
  1545. res_info->max_vnps = ntohl(rsp->max_vnps);
  1546. res_info->used_fcfs = ntohl(rsp->used_fcfs);
  1547. res_info->used_vnps = ntohl(rsp->used_vnps);
  1548. csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
  1549. res_info->max_xchgs);
  1550. mempool_free(mbp, hw->mb_mempool);
  1551. return 0;
  1552. }
  1553. static int
  1554. csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
  1555. {
  1556. struct csio_mb *mbp;
  1557. enum fw_retval retval;
  1558. u32 _param[1];
  1559. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1560. if (!mbp) {
  1561. CSIO_INC_STATS(hw, n_err_nomem);
  1562. return -ENOMEM;
  1563. }
  1564. /*
  1565. * Find out whether we're dealing with a version of
  1566. * the firmware which has configuration file support.
  1567. */
  1568. _param[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  1569. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CF));
  1570. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1571. ARRAY_SIZE(_param), _param, NULL, false, NULL);
  1572. if (csio_mb_issue(hw, mbp)) {
  1573. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1574. mempool_free(mbp, hw->mb_mempool);
  1575. return -EINVAL;
  1576. }
  1577. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1578. ARRAY_SIZE(_param), _param);
  1579. if (retval != FW_SUCCESS) {
  1580. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1581. retval);
  1582. mempool_free(mbp, hw->mb_mempool);
  1583. return -EINVAL;
  1584. }
  1585. mempool_free(mbp, hw->mb_mempool);
  1586. *param = _param[0];
  1587. return 0;
  1588. }
  1589. static int
  1590. csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
  1591. {
  1592. int ret = 0;
  1593. const struct firmware *cf;
  1594. struct pci_dev *pci_dev = hw->pdev;
  1595. struct device *dev = &pci_dev->dev;
  1596. unsigned int mtype = 0, maddr = 0;
  1597. uint32_t *cfg_data;
  1598. int value_to_add = 0;
  1599. if (request_firmware(&cf, CSIO_CF_FNAME(hw), dev) < 0) {
  1600. csio_err(hw, "could not find config file %s, err: %d\n",
  1601. CSIO_CF_FNAME(hw), ret);
  1602. return -ENOENT;
  1603. }
  1604. if (cf->size%4 != 0)
  1605. value_to_add = 4 - (cf->size % 4);
  1606. cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
  1607. if (cfg_data == NULL) {
  1608. ret = -ENOMEM;
  1609. goto leave;
  1610. }
  1611. memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
  1612. if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
  1613. ret = -EINVAL;
  1614. goto leave;
  1615. }
  1616. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1617. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1618. ret = csio_memory_write(hw, mtype, maddr,
  1619. cf->size + value_to_add, cfg_data);
  1620. if ((ret == 0) && (value_to_add != 0)) {
  1621. union {
  1622. u32 word;
  1623. char buf[4];
  1624. } last;
  1625. size_t size = cf->size & ~0x3;
  1626. int i;
  1627. last.word = cfg_data[size >> 2];
  1628. for (i = value_to_add; i < 4; i++)
  1629. last.buf[i] = 0;
  1630. ret = csio_memory_write(hw, mtype, maddr + size, 4, &last.word);
  1631. }
  1632. if (ret == 0) {
  1633. csio_info(hw, "config file upgraded to %s\n",
  1634. CSIO_CF_FNAME(hw));
  1635. snprintf(path, 64, "%s%s", "/lib/firmware/", CSIO_CF_FNAME(hw));
  1636. }
  1637. leave:
  1638. kfree(cfg_data);
  1639. release_firmware(cf);
  1640. return ret;
  1641. }
  1642. /*
  1643. * HW initialization: contact FW, obtain config, perform basic init.
  1644. *
  1645. * If the firmware we're dealing with has Configuration File support, then
  1646. * we use that to perform all configuration -- either using the configuration
  1647. * file stored in flash on the adapter or using a filesystem-local file
  1648. * if available.
  1649. *
  1650. * If we don't have configuration file support in the firmware, then we'll
  1651. * have to set things up the old fashioned way with hard-coded register
  1652. * writes and firmware commands ...
  1653. */
  1654. /*
  1655. * Attempt to initialize the HW via a Firmware Configuration File.
  1656. */
  1657. static int
  1658. csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
  1659. {
  1660. unsigned int mtype, maddr;
  1661. int rv;
  1662. uint32_t finiver = 0, finicsum = 0, cfcsum = 0;
  1663. int using_flash;
  1664. char path[64];
  1665. /*
  1666. * Reset device if necessary
  1667. */
  1668. if (reset) {
  1669. rv = csio_do_reset(hw, true);
  1670. if (rv != 0)
  1671. goto bye;
  1672. }
  1673. /*
  1674. * If we have a configuration file in host ,
  1675. * then use that. Otherwise, use the configuration file stored
  1676. * in the HW flash ...
  1677. */
  1678. spin_unlock_irq(&hw->lock);
  1679. rv = csio_hw_flash_config(hw, fw_cfg_param, path);
  1680. spin_lock_irq(&hw->lock);
  1681. if (rv != 0) {
  1682. if (rv == -ENOENT) {
  1683. /*
  1684. * config file was not found. Use default
  1685. * config file from flash.
  1686. */
  1687. mtype = FW_MEMTYPE_CF_FLASH;
  1688. maddr = hw->chip_ops->chip_flash_cfg_addr(hw);
  1689. using_flash = 1;
  1690. } else {
  1691. /*
  1692. * we revert back to the hardwired config if
  1693. * flashing failed.
  1694. */
  1695. goto bye;
  1696. }
  1697. } else {
  1698. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1699. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1700. using_flash = 0;
  1701. }
  1702. hw->cfg_store = (uint8_t)mtype;
  1703. /*
  1704. * Issue a Capability Configuration command to the firmware to get it
  1705. * to parse the Configuration File.
  1706. */
  1707. rv = csio_hw_fw_config_file(hw, mtype, maddr, &finiver,
  1708. &finicsum, &cfcsum);
  1709. if (rv != 0)
  1710. goto bye;
  1711. hw->cfg_finiver = finiver;
  1712. hw->cfg_finicsum = finicsum;
  1713. hw->cfg_cfcsum = cfcsum;
  1714. hw->cfg_csum_status = true;
  1715. if (finicsum != cfcsum) {
  1716. csio_warn(hw,
  1717. "Config File checksum mismatch: csum=%#x, computed=%#x\n",
  1718. finicsum, cfcsum);
  1719. hw->cfg_csum_status = false;
  1720. }
  1721. /*
  1722. * Note that we're operating with parameters
  1723. * not supplied by the driver, rather than from hard-wired
  1724. * initialization constants buried in the driver.
  1725. */
  1726. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1727. /* device parameters */
  1728. rv = csio_get_device_params(hw);
  1729. if (rv != 0)
  1730. goto bye;
  1731. /* Configure SGE */
  1732. csio_wr_sge_init(hw);
  1733. /*
  1734. * And finally tell the firmware to initialize itself using the
  1735. * parameters from the Configuration File.
  1736. */
  1737. /* Post event to notify completion of configuration */
  1738. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1739. csio_info(hw,
  1740. "Firmware Configuration File %s, version %#x, computed checksum %#x\n",
  1741. (using_flash ? "in device FLASH" : path), finiver, cfcsum);
  1742. return 0;
  1743. /*
  1744. * Something bad happened. Return the error ...
  1745. */
  1746. bye:
  1747. hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
  1748. csio_dbg(hw, "Configuration file error %d\n", rv);
  1749. return rv;
  1750. }
  1751. /*
  1752. * Attempt to initialize the adapter via hard-coded, driver supplied
  1753. * parameters ...
  1754. */
  1755. static int
  1756. csio_hw_no_fwconfig(struct csio_hw *hw, int reset)
  1757. {
  1758. int rv;
  1759. /*
  1760. * Reset device if necessary
  1761. */
  1762. if (reset) {
  1763. rv = csio_do_reset(hw, true);
  1764. if (rv != 0)
  1765. goto out;
  1766. }
  1767. /* Get and set device capabilities */
  1768. rv = csio_config_device_caps(hw);
  1769. if (rv != 0)
  1770. goto out;
  1771. /* Config Global RSS command */
  1772. rv = csio_config_global_rss(hw);
  1773. if (rv != 0)
  1774. goto out;
  1775. /* Configure PF/VF capabilities of device */
  1776. rv = csio_config_pfvf(hw);
  1777. if (rv != 0)
  1778. goto out;
  1779. /* device parameters */
  1780. rv = csio_get_device_params(hw);
  1781. if (rv != 0)
  1782. goto out;
  1783. /* Configure SGE */
  1784. csio_wr_sge_init(hw);
  1785. /* Post event to notify completion of configuration */
  1786. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1787. out:
  1788. return rv;
  1789. }
  1790. /*
  1791. * Returns -EINVAL if attempts to flash the firmware failed
  1792. * else returns 0,
  1793. * if flashing was not attempted because the card had the
  1794. * latest firmware ECANCELED is returned
  1795. */
  1796. static int
  1797. csio_hw_flash_fw(struct csio_hw *hw)
  1798. {
  1799. int ret = -ECANCELED;
  1800. const struct firmware *fw;
  1801. const struct fw_hdr *hdr;
  1802. u32 fw_ver;
  1803. struct pci_dev *pci_dev = hw->pdev;
  1804. struct device *dev = &pci_dev->dev ;
  1805. if (request_firmware(&fw, CSIO_FW_FNAME(hw), dev) < 0) {
  1806. csio_err(hw, "could not find firmware image %s, err: %d\n",
  1807. CSIO_FW_FNAME(hw), ret);
  1808. return -EINVAL;
  1809. }
  1810. hdr = (const struct fw_hdr *)fw->data;
  1811. fw_ver = ntohl(hdr->fw_ver);
  1812. if (FW_HDR_FW_VER_MAJOR_GET(fw_ver) != FW_VERSION_MAJOR(hw))
  1813. return -EINVAL; /* wrong major version, won't do */
  1814. /*
  1815. * If the flash FW is unusable or we found something newer, load it.
  1816. */
  1817. if (FW_HDR_FW_VER_MAJOR_GET(hw->fwrev) != FW_VERSION_MAJOR(hw) ||
  1818. fw_ver > hw->fwrev) {
  1819. ret = csio_hw_fw_upgrade(hw, hw->pfn, fw->data, fw->size,
  1820. /*force=*/false);
  1821. if (!ret)
  1822. csio_info(hw,
  1823. "firmware upgraded to version %pI4 from %s\n",
  1824. &hdr->fw_ver, CSIO_FW_FNAME(hw));
  1825. else
  1826. csio_err(hw, "firmware upgrade failed! err=%d\n", ret);
  1827. } else
  1828. ret = -EINVAL;
  1829. release_firmware(fw);
  1830. return ret;
  1831. }
  1832. /*
  1833. * csio_hw_configure - Configure HW
  1834. * @hw - HW module
  1835. *
  1836. */
  1837. static void
  1838. csio_hw_configure(struct csio_hw *hw)
  1839. {
  1840. int reset = 1;
  1841. int rv;
  1842. u32 param[1];
  1843. rv = csio_hw_dev_ready(hw);
  1844. if (rv != 0) {
  1845. CSIO_INC_STATS(hw, n_err_fatal);
  1846. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  1847. goto out;
  1848. }
  1849. /* HW version */
  1850. hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV);
  1851. /* Needed for FW download */
  1852. rv = csio_hw_get_flash_params(hw);
  1853. if (rv != 0) {
  1854. csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
  1855. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  1856. goto out;
  1857. }
  1858. /* Set pci completion timeout value to 4 seconds. */
  1859. csio_set_pcie_completion_timeout(hw, 0xd);
  1860. hw->chip_ops->chip_set_mem_win(hw, MEMWIN_CSIOSTOR);
  1861. rv = csio_hw_get_fw_version(hw, &hw->fwrev);
  1862. if (rv != 0)
  1863. goto out;
  1864. csio_hw_print_fw_version(hw, "Firmware revision");
  1865. rv = csio_do_hello(hw, &hw->fw_state);
  1866. if (rv != 0) {
  1867. CSIO_INC_STATS(hw, n_err_fatal);
  1868. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  1869. goto out;
  1870. }
  1871. /* Read vpd */
  1872. rv = csio_hw_get_vpd_params(hw, &hw->vpd);
  1873. if (rv != 0)
  1874. goto out;
  1875. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  1876. rv = csio_hw_check_fw_version(hw);
  1877. if (rv == -EINVAL) {
  1878. /* Do firmware update */
  1879. spin_unlock_irq(&hw->lock);
  1880. rv = csio_hw_flash_fw(hw);
  1881. spin_lock_irq(&hw->lock);
  1882. if (rv == 0) {
  1883. reset = 0;
  1884. /*
  1885. * Note that the chip was reset as part of the
  1886. * firmware upgrade so we don't reset it again
  1887. * below and grab the new firmware version.
  1888. */
  1889. rv = csio_hw_check_fw_version(hw);
  1890. }
  1891. }
  1892. /*
  1893. * If the firmware doesn't support Configuration
  1894. * Files, use the old Driver-based, hard-wired
  1895. * initialization. Otherwise, try using the
  1896. * Configuration File support and fall back to the
  1897. * Driver-based initialization if there's no
  1898. * Configuration File found.
  1899. */
  1900. if (csio_hw_check_fwconfig(hw, param) == 0) {
  1901. rv = csio_hw_use_fwconfig(hw, reset, param);
  1902. if (rv == -ENOENT)
  1903. goto out;
  1904. if (rv != 0) {
  1905. csio_info(hw,
  1906. "No Configuration File present "
  1907. "on adapter. Using hard-wired "
  1908. "configuration parameters.\n");
  1909. rv = csio_hw_no_fwconfig(hw, reset);
  1910. }
  1911. } else {
  1912. rv = csio_hw_no_fwconfig(hw, reset);
  1913. }
  1914. if (rv != 0)
  1915. goto out;
  1916. } else {
  1917. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  1918. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1919. /* device parameters */
  1920. rv = csio_get_device_params(hw);
  1921. if (rv != 0)
  1922. goto out;
  1923. /* Get device capabilities */
  1924. rv = csio_config_device_caps(hw);
  1925. if (rv != 0)
  1926. goto out;
  1927. /* Configure SGE */
  1928. csio_wr_sge_init(hw);
  1929. /* Post event to notify completion of configuration */
  1930. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1931. goto out;
  1932. }
  1933. } /* if not master */
  1934. out:
  1935. return;
  1936. }
  1937. /*
  1938. * csio_hw_initialize - Initialize HW
  1939. * @hw - HW module
  1940. *
  1941. */
  1942. static void
  1943. csio_hw_initialize(struct csio_hw *hw)
  1944. {
  1945. struct csio_mb *mbp;
  1946. enum fw_retval retval;
  1947. int rv;
  1948. int i;
  1949. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  1950. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1951. if (!mbp)
  1952. goto out;
  1953. csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1954. if (csio_mb_issue(hw, mbp)) {
  1955. csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
  1956. goto free_and_out;
  1957. }
  1958. retval = csio_mb_fw_retval(mbp);
  1959. if (retval != FW_SUCCESS) {
  1960. csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
  1961. retval);
  1962. goto free_and_out;
  1963. }
  1964. mempool_free(mbp, hw->mb_mempool);
  1965. }
  1966. rv = csio_get_fcoe_resinfo(hw);
  1967. if (rv != 0) {
  1968. csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
  1969. goto out;
  1970. }
  1971. spin_unlock_irq(&hw->lock);
  1972. rv = csio_config_queues(hw);
  1973. spin_lock_irq(&hw->lock);
  1974. if (rv != 0) {
  1975. csio_err(hw, "Config of queues failed!: %d\n", rv);
  1976. goto out;
  1977. }
  1978. for (i = 0; i < hw->num_pports; i++)
  1979. hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
  1980. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  1981. rv = csio_enable_ports(hw);
  1982. if (rv != 0) {
  1983. csio_err(hw, "Failed to enable ports: %d\n", rv);
  1984. goto out;
  1985. }
  1986. }
  1987. csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
  1988. return;
  1989. free_and_out:
  1990. mempool_free(mbp, hw->mb_mempool);
  1991. out:
  1992. return;
  1993. }
  1994. #define PF_INTR_MASK (PFSW | PFCIM)
  1995. /*
  1996. * csio_hw_intr_enable - Enable HW interrupts
  1997. * @hw: Pointer to HW module.
  1998. *
  1999. * Enable interrupts in HW registers.
  2000. */
  2001. static void
  2002. csio_hw_intr_enable(struct csio_hw *hw)
  2003. {
  2004. uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
  2005. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  2006. uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE);
  2007. /*
  2008. * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
  2009. * by FW, so do nothing for INTX.
  2010. */
  2011. if (hw->intr_mode == CSIO_IM_MSIX)
  2012. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  2013. AIVEC(AIVEC_MASK), vec);
  2014. else if (hw->intr_mode == CSIO_IM_MSI)
  2015. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  2016. AIVEC(AIVEC_MASK), 0);
  2017. csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE));
  2018. /* Turn on MB interrupts - this will internally flush PIO as well */
  2019. csio_mb_intr_enable(hw);
  2020. /* These are common registers - only a master can modify them */
  2021. if (csio_is_hw_master(hw)) {
  2022. /*
  2023. * Disable the Serial FLASH interrupt, if enabled!
  2024. */
  2025. pl &= (~SF);
  2026. csio_wr_reg32(hw, pl, PL_INT_ENABLE);
  2027. csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE |
  2028. EGRESS_SIZE_ERR | ERR_INVALID_CIDX_INC |
  2029. ERR_CPL_OPCODE_0 | ERR_DROPPED_DB |
  2030. ERR_DATA_CPL_ON_HIGH_QID1 |
  2031. ERR_DATA_CPL_ON_HIGH_QID0 | ERR_BAD_DB_PIDX3 |
  2032. ERR_BAD_DB_PIDX2 | ERR_BAD_DB_PIDX1 |
  2033. ERR_BAD_DB_PIDX0 | ERR_ING_CTXT_PRIO |
  2034. ERR_EGR_CTXT_PRIO | INGRESS_SIZE_ERR,
  2035. SGE_INT_ENABLE3);
  2036. csio_set_reg_field(hw, PL_INT_MAP0, 0, 1 << pf);
  2037. }
  2038. hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
  2039. }
  2040. /*
  2041. * csio_hw_intr_disable - Disable HW interrupts
  2042. * @hw: Pointer to HW module.
  2043. *
  2044. * Turn off Mailbox and PCI_PF_CFG interrupts.
  2045. */
  2046. void
  2047. csio_hw_intr_disable(struct csio_hw *hw)
  2048. {
  2049. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  2050. if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
  2051. return;
  2052. hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
  2053. csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE));
  2054. if (csio_is_hw_master(hw))
  2055. csio_set_reg_field(hw, PL_INT_MAP0, 1 << pf, 0);
  2056. /* Turn off MB interrupts */
  2057. csio_mb_intr_disable(hw);
  2058. }
  2059. void
  2060. csio_hw_fatal_err(struct csio_hw *hw)
  2061. {
  2062. csio_set_reg_field(hw, SGE_CONTROL, GLOBALENABLE, 0);
  2063. csio_hw_intr_disable(hw);
  2064. /* Do not reset HW, we may need FW state for debugging */
  2065. csio_fatal(hw, "HW Fatal error encountered!\n");
  2066. }
  2067. /*****************************************************************************/
  2068. /* START: HW SM */
  2069. /*****************************************************************************/
  2070. /*
  2071. * csio_hws_uninit - Uninit state
  2072. * @hw - HW module
  2073. * @evt - Event
  2074. *
  2075. */
  2076. static void
  2077. csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
  2078. {
  2079. hw->prev_evt = hw->cur_evt;
  2080. hw->cur_evt = evt;
  2081. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2082. switch (evt) {
  2083. case CSIO_HWE_CFG:
  2084. csio_set_state(&hw->sm, csio_hws_configuring);
  2085. csio_hw_configure(hw);
  2086. break;
  2087. default:
  2088. CSIO_INC_STATS(hw, n_evt_unexp);
  2089. break;
  2090. }
  2091. }
  2092. /*
  2093. * csio_hws_configuring - Configuring state
  2094. * @hw - HW module
  2095. * @evt - Event
  2096. *
  2097. */
  2098. static void
  2099. csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
  2100. {
  2101. hw->prev_evt = hw->cur_evt;
  2102. hw->cur_evt = evt;
  2103. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2104. switch (evt) {
  2105. case CSIO_HWE_INIT:
  2106. csio_set_state(&hw->sm, csio_hws_initializing);
  2107. csio_hw_initialize(hw);
  2108. break;
  2109. case CSIO_HWE_INIT_DONE:
  2110. csio_set_state(&hw->sm, csio_hws_ready);
  2111. /* Fan out event to all lnode SMs */
  2112. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2113. break;
  2114. case CSIO_HWE_FATAL:
  2115. csio_set_state(&hw->sm, csio_hws_uninit);
  2116. break;
  2117. case CSIO_HWE_PCI_REMOVE:
  2118. csio_do_bye(hw);
  2119. break;
  2120. default:
  2121. CSIO_INC_STATS(hw, n_evt_unexp);
  2122. break;
  2123. }
  2124. }
  2125. /*
  2126. * csio_hws_initializing - Initialiazing state
  2127. * @hw - HW module
  2128. * @evt - Event
  2129. *
  2130. */
  2131. static void
  2132. csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
  2133. {
  2134. hw->prev_evt = hw->cur_evt;
  2135. hw->cur_evt = evt;
  2136. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2137. switch (evt) {
  2138. case CSIO_HWE_INIT_DONE:
  2139. csio_set_state(&hw->sm, csio_hws_ready);
  2140. /* Fan out event to all lnode SMs */
  2141. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2142. /* Enable interrupts */
  2143. csio_hw_intr_enable(hw);
  2144. break;
  2145. case CSIO_HWE_FATAL:
  2146. csio_set_state(&hw->sm, csio_hws_uninit);
  2147. break;
  2148. case CSIO_HWE_PCI_REMOVE:
  2149. csio_do_bye(hw);
  2150. break;
  2151. default:
  2152. CSIO_INC_STATS(hw, n_evt_unexp);
  2153. break;
  2154. }
  2155. }
  2156. /*
  2157. * csio_hws_ready - Ready state
  2158. * @hw - HW module
  2159. * @evt - Event
  2160. *
  2161. */
  2162. static void
  2163. csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
  2164. {
  2165. /* Remember the event */
  2166. hw->evtflag = evt;
  2167. hw->prev_evt = hw->cur_evt;
  2168. hw->cur_evt = evt;
  2169. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2170. switch (evt) {
  2171. case CSIO_HWE_HBA_RESET:
  2172. case CSIO_HWE_FW_DLOAD:
  2173. case CSIO_HWE_SUSPEND:
  2174. case CSIO_HWE_PCI_REMOVE:
  2175. case CSIO_HWE_PCIERR_DETECTED:
  2176. csio_set_state(&hw->sm, csio_hws_quiescing);
  2177. /* cleanup all outstanding cmds */
  2178. if (evt == CSIO_HWE_HBA_RESET ||
  2179. evt == CSIO_HWE_PCIERR_DETECTED)
  2180. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
  2181. else
  2182. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
  2183. csio_hw_intr_disable(hw);
  2184. csio_hw_mbm_cleanup(hw);
  2185. csio_evtq_stop(hw);
  2186. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
  2187. csio_evtq_flush(hw);
  2188. csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
  2189. csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
  2190. break;
  2191. case CSIO_HWE_FATAL:
  2192. csio_set_state(&hw->sm, csio_hws_uninit);
  2193. break;
  2194. default:
  2195. CSIO_INC_STATS(hw, n_evt_unexp);
  2196. break;
  2197. }
  2198. }
  2199. /*
  2200. * csio_hws_quiescing - Quiescing state
  2201. * @hw - HW module
  2202. * @evt - Event
  2203. *
  2204. */
  2205. static void
  2206. csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
  2207. {
  2208. hw->prev_evt = hw->cur_evt;
  2209. hw->cur_evt = evt;
  2210. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2211. switch (evt) {
  2212. case CSIO_HWE_QUIESCED:
  2213. switch (hw->evtflag) {
  2214. case CSIO_HWE_FW_DLOAD:
  2215. csio_set_state(&hw->sm, csio_hws_resetting);
  2216. /* Download firmware */
  2217. /* Fall through */
  2218. case CSIO_HWE_HBA_RESET:
  2219. csio_set_state(&hw->sm, csio_hws_resetting);
  2220. /* Start reset of the HBA */
  2221. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
  2222. csio_wr_destroy_queues(hw, false);
  2223. csio_do_reset(hw, false);
  2224. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
  2225. break;
  2226. case CSIO_HWE_PCI_REMOVE:
  2227. csio_set_state(&hw->sm, csio_hws_removing);
  2228. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
  2229. csio_wr_destroy_queues(hw, true);
  2230. /* Now send the bye command */
  2231. csio_do_bye(hw);
  2232. break;
  2233. case CSIO_HWE_SUSPEND:
  2234. csio_set_state(&hw->sm, csio_hws_quiesced);
  2235. break;
  2236. case CSIO_HWE_PCIERR_DETECTED:
  2237. csio_set_state(&hw->sm, csio_hws_pcierr);
  2238. csio_wr_destroy_queues(hw, false);
  2239. break;
  2240. default:
  2241. CSIO_INC_STATS(hw, n_evt_unexp);
  2242. break;
  2243. }
  2244. break;
  2245. default:
  2246. CSIO_INC_STATS(hw, n_evt_unexp);
  2247. break;
  2248. }
  2249. }
  2250. /*
  2251. * csio_hws_quiesced - Quiesced state
  2252. * @hw - HW module
  2253. * @evt - Event
  2254. *
  2255. */
  2256. static void
  2257. csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
  2258. {
  2259. hw->prev_evt = hw->cur_evt;
  2260. hw->cur_evt = evt;
  2261. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2262. switch (evt) {
  2263. case CSIO_HWE_RESUME:
  2264. csio_set_state(&hw->sm, csio_hws_configuring);
  2265. csio_hw_configure(hw);
  2266. break;
  2267. default:
  2268. CSIO_INC_STATS(hw, n_evt_unexp);
  2269. break;
  2270. }
  2271. }
  2272. /*
  2273. * csio_hws_resetting - HW Resetting state
  2274. * @hw - HW module
  2275. * @evt - Event
  2276. *
  2277. */
  2278. static void
  2279. csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
  2280. {
  2281. hw->prev_evt = hw->cur_evt;
  2282. hw->cur_evt = evt;
  2283. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2284. switch (evt) {
  2285. case CSIO_HWE_HBA_RESET_DONE:
  2286. csio_evtq_start(hw);
  2287. csio_set_state(&hw->sm, csio_hws_configuring);
  2288. csio_hw_configure(hw);
  2289. break;
  2290. default:
  2291. CSIO_INC_STATS(hw, n_evt_unexp);
  2292. break;
  2293. }
  2294. }
  2295. /*
  2296. * csio_hws_removing - PCI Hotplug removing state
  2297. * @hw - HW module
  2298. * @evt - Event
  2299. *
  2300. */
  2301. static void
  2302. csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
  2303. {
  2304. hw->prev_evt = hw->cur_evt;
  2305. hw->cur_evt = evt;
  2306. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2307. switch (evt) {
  2308. case CSIO_HWE_HBA_RESET:
  2309. if (!csio_is_hw_master(hw))
  2310. break;
  2311. /*
  2312. * The BYE should have alerady been issued, so we cant
  2313. * use the mailbox interface. Hence we use the PL_RST
  2314. * register directly.
  2315. */
  2316. csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
  2317. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  2318. mdelay(2000);
  2319. break;
  2320. /* Should never receive any new events */
  2321. default:
  2322. CSIO_INC_STATS(hw, n_evt_unexp);
  2323. break;
  2324. }
  2325. }
  2326. /*
  2327. * csio_hws_pcierr - PCI Error state
  2328. * @hw - HW module
  2329. * @evt - Event
  2330. *
  2331. */
  2332. static void
  2333. csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
  2334. {
  2335. hw->prev_evt = hw->cur_evt;
  2336. hw->cur_evt = evt;
  2337. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2338. switch (evt) {
  2339. case CSIO_HWE_PCIERR_SLOT_RESET:
  2340. csio_evtq_start(hw);
  2341. csio_set_state(&hw->sm, csio_hws_configuring);
  2342. csio_hw_configure(hw);
  2343. break;
  2344. default:
  2345. CSIO_INC_STATS(hw, n_evt_unexp);
  2346. break;
  2347. }
  2348. }
  2349. /*****************************************************************************/
  2350. /* END: HW SM */
  2351. /*****************************************************************************/
  2352. /*
  2353. * csio_handle_intr_status - table driven interrupt handler
  2354. * @hw: HW instance
  2355. * @reg: the interrupt status register to process
  2356. * @acts: table of interrupt actions
  2357. *
  2358. * A table driven interrupt handler that applies a set of masks to an
  2359. * interrupt status word and performs the corresponding actions if the
  2360. * interrupts described by the mask have occured. The actions include
  2361. * optionally emitting a warning or alert message. The table is terminated
  2362. * by an entry specifying mask 0. Returns the number of fatal interrupt
  2363. * conditions.
  2364. */
  2365. int
  2366. csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
  2367. const struct intr_info *acts)
  2368. {
  2369. int fatal = 0;
  2370. unsigned int mask = 0;
  2371. unsigned int status = csio_rd_reg32(hw, reg);
  2372. for ( ; acts->mask; ++acts) {
  2373. if (!(status & acts->mask))
  2374. continue;
  2375. if (acts->fatal) {
  2376. fatal++;
  2377. csio_fatal(hw, "Fatal %s (0x%x)\n",
  2378. acts->msg, status & acts->mask);
  2379. } else if (acts->msg)
  2380. csio_info(hw, "%s (0x%x)\n",
  2381. acts->msg, status & acts->mask);
  2382. mask |= acts->mask;
  2383. }
  2384. status &= mask;
  2385. if (status) /* clear processed interrupts */
  2386. csio_wr_reg32(hw, status, reg);
  2387. return fatal;
  2388. }
  2389. /*
  2390. * TP interrupt handler.
  2391. */
  2392. static void csio_tp_intr_handler(struct csio_hw *hw)
  2393. {
  2394. static struct intr_info tp_intr_info[] = {
  2395. { 0x3fffffff, "TP parity error", -1, 1 },
  2396. { FLMTXFLSTEMPTY, "TP out of Tx pages", -1, 1 },
  2397. { 0, NULL, 0, 0 }
  2398. };
  2399. if (csio_handle_intr_status(hw, TP_INT_CAUSE, tp_intr_info))
  2400. csio_hw_fatal_err(hw);
  2401. }
  2402. /*
  2403. * SGE interrupt handler.
  2404. */
  2405. static void csio_sge_intr_handler(struct csio_hw *hw)
  2406. {
  2407. uint64_t v;
  2408. static struct intr_info sge_intr_info[] = {
  2409. { ERR_CPL_EXCEED_IQE_SIZE,
  2410. "SGE received CPL exceeding IQE size", -1, 1 },
  2411. { ERR_INVALID_CIDX_INC,
  2412. "SGE GTS CIDX increment too large", -1, 0 },
  2413. { ERR_CPL_OPCODE_0, "SGE received 0-length CPL", -1, 0 },
  2414. { ERR_DROPPED_DB, "SGE doorbell dropped", -1, 0 },
  2415. { ERR_DATA_CPL_ON_HIGH_QID1 | ERR_DATA_CPL_ON_HIGH_QID0,
  2416. "SGE IQID > 1023 received CPL for FL", -1, 0 },
  2417. { ERR_BAD_DB_PIDX3, "SGE DBP 3 pidx increment too large", -1,
  2418. 0 },
  2419. { ERR_BAD_DB_PIDX2, "SGE DBP 2 pidx increment too large", -1,
  2420. 0 },
  2421. { ERR_BAD_DB_PIDX1, "SGE DBP 1 pidx increment too large", -1,
  2422. 0 },
  2423. { ERR_BAD_DB_PIDX0, "SGE DBP 0 pidx increment too large", -1,
  2424. 0 },
  2425. { ERR_ING_CTXT_PRIO,
  2426. "SGE too many priority ingress contexts", -1, 0 },
  2427. { ERR_EGR_CTXT_PRIO,
  2428. "SGE too many priority egress contexts", -1, 0 },
  2429. { INGRESS_SIZE_ERR, "SGE illegal ingress QID", -1, 0 },
  2430. { EGRESS_SIZE_ERR, "SGE illegal egress QID", -1, 0 },
  2431. { 0, NULL, 0, 0 }
  2432. };
  2433. v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1) |
  2434. ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2) << 32);
  2435. if (v) {
  2436. csio_fatal(hw, "SGE parity error (%#llx)\n",
  2437. (unsigned long long)v);
  2438. csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
  2439. SGE_INT_CAUSE1);
  2440. csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2);
  2441. }
  2442. v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info);
  2443. if (csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info) ||
  2444. v != 0)
  2445. csio_hw_fatal_err(hw);
  2446. }
  2447. #define CIM_OBQ_INTR (OBQULP0PARERR | OBQULP1PARERR | OBQULP2PARERR |\
  2448. OBQULP3PARERR | OBQSGEPARERR | OBQNCSIPARERR)
  2449. #define CIM_IBQ_INTR (IBQTP0PARERR | IBQTP1PARERR | IBQULPPARERR |\
  2450. IBQSGEHIPARERR | IBQSGELOPARERR | IBQNCSIPARERR)
  2451. /*
  2452. * CIM interrupt handler.
  2453. */
  2454. static void csio_cim_intr_handler(struct csio_hw *hw)
  2455. {
  2456. static struct intr_info cim_intr_info[] = {
  2457. { PREFDROPINT, "CIM control register prefetch drop", -1, 1 },
  2458. { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
  2459. { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
  2460. { MBUPPARERR, "CIM mailbox uP parity error", -1, 1 },
  2461. { MBHOSTPARERR, "CIM mailbox host parity error", -1, 1 },
  2462. { TIEQINPARERRINT, "CIM TIEQ outgoing parity error", -1, 1 },
  2463. { TIEQOUTPARERRINT, "CIM TIEQ incoming parity error", -1, 1 },
  2464. { 0, NULL, 0, 0 }
  2465. };
  2466. static struct intr_info cim_upintr_info[] = {
  2467. { RSVDSPACEINT, "CIM reserved space access", -1, 1 },
  2468. { ILLTRANSINT, "CIM illegal transaction", -1, 1 },
  2469. { ILLWRINT, "CIM illegal write", -1, 1 },
  2470. { ILLRDINT, "CIM illegal read", -1, 1 },
  2471. { ILLRDBEINT, "CIM illegal read BE", -1, 1 },
  2472. { ILLWRBEINT, "CIM illegal write BE", -1, 1 },
  2473. { SGLRDBOOTINT, "CIM single read from boot space", -1, 1 },
  2474. { SGLWRBOOTINT, "CIM single write to boot space", -1, 1 },
  2475. { BLKWRBOOTINT, "CIM block write to boot space", -1, 1 },
  2476. { SGLRDFLASHINT, "CIM single read from flash space", -1, 1 },
  2477. { SGLWRFLASHINT, "CIM single write to flash space", -1, 1 },
  2478. { BLKWRFLASHINT, "CIM block write to flash space", -1, 1 },
  2479. { SGLRDEEPROMINT, "CIM single EEPROM read", -1, 1 },
  2480. { SGLWREEPROMINT, "CIM single EEPROM write", -1, 1 },
  2481. { BLKRDEEPROMINT, "CIM block EEPROM read", -1, 1 },
  2482. { BLKWREEPROMINT, "CIM block EEPROM write", -1, 1 },
  2483. { SGLRDCTLINT , "CIM single read from CTL space", -1, 1 },
  2484. { SGLWRCTLINT , "CIM single write to CTL space", -1, 1 },
  2485. { BLKRDCTLINT , "CIM block read from CTL space", -1, 1 },
  2486. { BLKWRCTLINT , "CIM block write to CTL space", -1, 1 },
  2487. { SGLRDPLINT , "CIM single read from PL space", -1, 1 },
  2488. { SGLWRPLINT , "CIM single write to PL space", -1, 1 },
  2489. { BLKRDPLINT , "CIM block read from PL space", -1, 1 },
  2490. { BLKWRPLINT , "CIM block write to PL space", -1, 1 },
  2491. { REQOVRLOOKUPINT , "CIM request FIFO overwrite", -1, 1 },
  2492. { RSPOVRLOOKUPINT , "CIM response FIFO overwrite", -1, 1 },
  2493. { TIMEOUTINT , "CIM PIF timeout", -1, 1 },
  2494. { TIMEOUTMAINT , "CIM PIF MA timeout", -1, 1 },
  2495. { 0, NULL, 0, 0 }
  2496. };
  2497. int fat;
  2498. fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE,
  2499. cim_intr_info) +
  2500. csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE,
  2501. cim_upintr_info);
  2502. if (fat)
  2503. csio_hw_fatal_err(hw);
  2504. }
  2505. /*
  2506. * ULP RX interrupt handler.
  2507. */
  2508. static void csio_ulprx_intr_handler(struct csio_hw *hw)
  2509. {
  2510. static struct intr_info ulprx_intr_info[] = {
  2511. { 0x1800000, "ULPRX context error", -1, 1 },
  2512. { 0x7fffff, "ULPRX parity error", -1, 1 },
  2513. { 0, NULL, 0, 0 }
  2514. };
  2515. if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE, ulprx_intr_info))
  2516. csio_hw_fatal_err(hw);
  2517. }
  2518. /*
  2519. * ULP TX interrupt handler.
  2520. */
  2521. static void csio_ulptx_intr_handler(struct csio_hw *hw)
  2522. {
  2523. static struct intr_info ulptx_intr_info[] = {
  2524. { PBL_BOUND_ERR_CH3, "ULPTX channel 3 PBL out of bounds", -1,
  2525. 0 },
  2526. { PBL_BOUND_ERR_CH2, "ULPTX channel 2 PBL out of bounds", -1,
  2527. 0 },
  2528. { PBL_BOUND_ERR_CH1, "ULPTX channel 1 PBL out of bounds", -1,
  2529. 0 },
  2530. { PBL_BOUND_ERR_CH0, "ULPTX channel 0 PBL out of bounds", -1,
  2531. 0 },
  2532. { 0xfffffff, "ULPTX parity error", -1, 1 },
  2533. { 0, NULL, 0, 0 }
  2534. };
  2535. if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE, ulptx_intr_info))
  2536. csio_hw_fatal_err(hw);
  2537. }
  2538. /*
  2539. * PM TX interrupt handler.
  2540. */
  2541. static void csio_pmtx_intr_handler(struct csio_hw *hw)
  2542. {
  2543. static struct intr_info pmtx_intr_info[] = {
  2544. { PCMD_LEN_OVFL0, "PMTX channel 0 pcmd too large", -1, 1 },
  2545. { PCMD_LEN_OVFL1, "PMTX channel 1 pcmd too large", -1, 1 },
  2546. { PCMD_LEN_OVFL2, "PMTX channel 2 pcmd too large", -1, 1 },
  2547. { ZERO_C_CMD_ERROR, "PMTX 0-length pcmd", -1, 1 },
  2548. { 0xffffff0, "PMTX framing error", -1, 1 },
  2549. { OESPI_PAR_ERROR, "PMTX oespi parity error", -1, 1 },
  2550. { DB_OPTIONS_PAR_ERROR, "PMTX db_options parity error", -1,
  2551. 1 },
  2552. { ICSPI_PAR_ERROR, "PMTX icspi parity error", -1, 1 },
  2553. { C_PCMD_PAR_ERROR, "PMTX c_pcmd parity error", -1, 1},
  2554. { 0, NULL, 0, 0 }
  2555. };
  2556. if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE, pmtx_intr_info))
  2557. csio_hw_fatal_err(hw);
  2558. }
  2559. /*
  2560. * PM RX interrupt handler.
  2561. */
  2562. static void csio_pmrx_intr_handler(struct csio_hw *hw)
  2563. {
  2564. static struct intr_info pmrx_intr_info[] = {
  2565. { ZERO_E_CMD_ERROR, "PMRX 0-length pcmd", -1, 1 },
  2566. { 0x3ffff0, "PMRX framing error", -1, 1 },
  2567. { OCSPI_PAR_ERROR, "PMRX ocspi parity error", -1, 1 },
  2568. { DB_OPTIONS_PAR_ERROR, "PMRX db_options parity error", -1,
  2569. 1 },
  2570. { IESPI_PAR_ERROR, "PMRX iespi parity error", -1, 1 },
  2571. { E_PCMD_PAR_ERROR, "PMRX e_pcmd parity error", -1, 1},
  2572. { 0, NULL, 0, 0 }
  2573. };
  2574. if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE, pmrx_intr_info))
  2575. csio_hw_fatal_err(hw);
  2576. }
  2577. /*
  2578. * CPL switch interrupt handler.
  2579. */
  2580. static void csio_cplsw_intr_handler(struct csio_hw *hw)
  2581. {
  2582. static struct intr_info cplsw_intr_info[] = {
  2583. { CIM_OP_MAP_PERR, "CPLSW CIM op_map parity error", -1, 1 },
  2584. { CIM_OVFL_ERROR, "CPLSW CIM overflow", -1, 1 },
  2585. { TP_FRAMING_ERROR, "CPLSW TP framing error", -1, 1 },
  2586. { SGE_FRAMING_ERROR, "CPLSW SGE framing error", -1, 1 },
  2587. { CIM_FRAMING_ERROR, "CPLSW CIM framing error", -1, 1 },
  2588. { ZERO_SWITCH_ERROR, "CPLSW no-switch error", -1, 1 },
  2589. { 0, NULL, 0, 0 }
  2590. };
  2591. if (csio_handle_intr_status(hw, CPL_INTR_CAUSE, cplsw_intr_info))
  2592. csio_hw_fatal_err(hw);
  2593. }
  2594. /*
  2595. * LE interrupt handler.
  2596. */
  2597. static void csio_le_intr_handler(struct csio_hw *hw)
  2598. {
  2599. static struct intr_info le_intr_info[] = {
  2600. { LIPMISS, "LE LIP miss", -1, 0 },
  2601. { LIP0, "LE 0 LIP error", -1, 0 },
  2602. { PARITYERR, "LE parity error", -1, 1 },
  2603. { UNKNOWNCMD, "LE unknown command", -1, 1 },
  2604. { REQQPARERR, "LE request queue parity error", -1, 1 },
  2605. { 0, NULL, 0, 0 }
  2606. };
  2607. if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE, le_intr_info))
  2608. csio_hw_fatal_err(hw);
  2609. }
  2610. /*
  2611. * MPS interrupt handler.
  2612. */
  2613. static void csio_mps_intr_handler(struct csio_hw *hw)
  2614. {
  2615. static struct intr_info mps_rx_intr_info[] = {
  2616. { 0xffffff, "MPS Rx parity error", -1, 1 },
  2617. { 0, NULL, 0, 0 }
  2618. };
  2619. static struct intr_info mps_tx_intr_info[] = {
  2620. { TPFIFO, "MPS Tx TP FIFO parity error", -1, 1 },
  2621. { NCSIFIFO, "MPS Tx NC-SI FIFO parity error", -1, 1 },
  2622. { TXDATAFIFO, "MPS Tx data FIFO parity error", -1, 1 },
  2623. { TXDESCFIFO, "MPS Tx desc FIFO parity error", -1, 1 },
  2624. { BUBBLE, "MPS Tx underflow", -1, 1 },
  2625. { SECNTERR, "MPS Tx SOP/EOP error", -1, 1 },
  2626. { FRMERR, "MPS Tx framing error", -1, 1 },
  2627. { 0, NULL, 0, 0 }
  2628. };
  2629. static struct intr_info mps_trc_intr_info[] = {
  2630. { FILTMEM, "MPS TRC filter parity error", -1, 1 },
  2631. { PKTFIFO, "MPS TRC packet FIFO parity error", -1, 1 },
  2632. { MISCPERR, "MPS TRC misc parity error", -1, 1 },
  2633. { 0, NULL, 0, 0 }
  2634. };
  2635. static struct intr_info mps_stat_sram_intr_info[] = {
  2636. { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
  2637. { 0, NULL, 0, 0 }
  2638. };
  2639. static struct intr_info mps_stat_tx_intr_info[] = {
  2640. { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
  2641. { 0, NULL, 0, 0 }
  2642. };
  2643. static struct intr_info mps_stat_rx_intr_info[] = {
  2644. { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
  2645. { 0, NULL, 0, 0 }
  2646. };
  2647. static struct intr_info mps_cls_intr_info[] = {
  2648. { MATCHSRAM, "MPS match SRAM parity error", -1, 1 },
  2649. { MATCHTCAM, "MPS match TCAM parity error", -1, 1 },
  2650. { HASHSRAM, "MPS hash SRAM parity error", -1, 1 },
  2651. { 0, NULL, 0, 0 }
  2652. };
  2653. int fat;
  2654. fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE,
  2655. mps_rx_intr_info) +
  2656. csio_handle_intr_status(hw, MPS_TX_INT_CAUSE,
  2657. mps_tx_intr_info) +
  2658. csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE,
  2659. mps_trc_intr_info) +
  2660. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM,
  2661. mps_stat_sram_intr_info) +
  2662. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO,
  2663. mps_stat_tx_intr_info) +
  2664. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO,
  2665. mps_stat_rx_intr_info) +
  2666. csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE,
  2667. mps_cls_intr_info);
  2668. csio_wr_reg32(hw, 0, MPS_INT_CAUSE);
  2669. csio_rd_reg32(hw, MPS_INT_CAUSE); /* flush */
  2670. if (fat)
  2671. csio_hw_fatal_err(hw);
  2672. }
  2673. #define MEM_INT_MASK (PERR_INT_CAUSE | ECC_CE_INT_CAUSE | ECC_UE_INT_CAUSE)
  2674. /*
  2675. * EDC/MC interrupt handler.
  2676. */
  2677. static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
  2678. {
  2679. static const char name[3][5] = { "EDC0", "EDC1", "MC" };
  2680. unsigned int addr, cnt_addr, v;
  2681. if (idx <= MEM_EDC1) {
  2682. addr = EDC_REG(EDC_INT_CAUSE, idx);
  2683. cnt_addr = EDC_REG(EDC_ECC_STATUS, idx);
  2684. } else {
  2685. addr = MC_INT_CAUSE;
  2686. cnt_addr = MC_ECC_STATUS;
  2687. }
  2688. v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
  2689. if (v & PERR_INT_CAUSE)
  2690. csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
  2691. if (v & ECC_CE_INT_CAUSE) {
  2692. uint32_t cnt = ECC_CECNT_GET(csio_rd_reg32(hw, cnt_addr));
  2693. csio_wr_reg32(hw, ECC_CECNT_MASK, cnt_addr);
  2694. csio_warn(hw, "%u %s correctable ECC data error%s\n",
  2695. cnt, name[idx], cnt > 1 ? "s" : "");
  2696. }
  2697. if (v & ECC_UE_INT_CAUSE)
  2698. csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
  2699. csio_wr_reg32(hw, v, addr);
  2700. if (v & (PERR_INT_CAUSE | ECC_UE_INT_CAUSE))
  2701. csio_hw_fatal_err(hw);
  2702. }
  2703. /*
  2704. * MA interrupt handler.
  2705. */
  2706. static void csio_ma_intr_handler(struct csio_hw *hw)
  2707. {
  2708. uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE);
  2709. if (status & MEM_PERR_INT_CAUSE)
  2710. csio_fatal(hw, "MA parity error, parity status %#x\n",
  2711. csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS));
  2712. if (status & MEM_WRAP_INT_CAUSE) {
  2713. v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS);
  2714. csio_fatal(hw,
  2715. "MA address wrap-around error by client %u to address %#x\n",
  2716. MEM_WRAP_CLIENT_NUM_GET(v), MEM_WRAP_ADDRESS_GET(v) << 4);
  2717. }
  2718. csio_wr_reg32(hw, status, MA_INT_CAUSE);
  2719. csio_hw_fatal_err(hw);
  2720. }
  2721. /*
  2722. * SMB interrupt handler.
  2723. */
  2724. static void csio_smb_intr_handler(struct csio_hw *hw)
  2725. {
  2726. static struct intr_info smb_intr_info[] = {
  2727. { MSTTXFIFOPARINT, "SMB master Tx FIFO parity error", -1, 1 },
  2728. { MSTRXFIFOPARINT, "SMB master Rx FIFO parity error", -1, 1 },
  2729. { SLVFIFOPARINT, "SMB slave FIFO parity error", -1, 1 },
  2730. { 0, NULL, 0, 0 }
  2731. };
  2732. if (csio_handle_intr_status(hw, SMB_INT_CAUSE, smb_intr_info))
  2733. csio_hw_fatal_err(hw);
  2734. }
  2735. /*
  2736. * NC-SI interrupt handler.
  2737. */
  2738. static void csio_ncsi_intr_handler(struct csio_hw *hw)
  2739. {
  2740. static struct intr_info ncsi_intr_info[] = {
  2741. { CIM_DM_PRTY_ERR, "NC-SI CIM parity error", -1, 1 },
  2742. { MPS_DM_PRTY_ERR, "NC-SI MPS parity error", -1, 1 },
  2743. { TXFIFO_PRTY_ERR, "NC-SI Tx FIFO parity error", -1, 1 },
  2744. { RXFIFO_PRTY_ERR, "NC-SI Rx FIFO parity error", -1, 1 },
  2745. { 0, NULL, 0, 0 }
  2746. };
  2747. if (csio_handle_intr_status(hw, NCSI_INT_CAUSE, ncsi_intr_info))
  2748. csio_hw_fatal_err(hw);
  2749. }
  2750. /*
  2751. * XGMAC interrupt handler.
  2752. */
  2753. static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
  2754. {
  2755. uint32_t v = csio_rd_reg32(hw, CSIO_MAC_INT_CAUSE_REG(hw, port));
  2756. v &= TXFIFO_PRTY_ERR | RXFIFO_PRTY_ERR;
  2757. if (!v)
  2758. return;
  2759. if (v & TXFIFO_PRTY_ERR)
  2760. csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
  2761. if (v & RXFIFO_PRTY_ERR)
  2762. csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
  2763. csio_wr_reg32(hw, v, CSIO_MAC_INT_CAUSE_REG(hw, port));
  2764. csio_hw_fatal_err(hw);
  2765. }
  2766. /*
  2767. * PL interrupt handler.
  2768. */
  2769. static void csio_pl_intr_handler(struct csio_hw *hw)
  2770. {
  2771. static struct intr_info pl_intr_info[] = {
  2772. { FATALPERR, "T4 fatal parity error", -1, 1 },
  2773. { PERRVFID, "PL VFID_MAP parity error", -1, 1 },
  2774. { 0, NULL, 0, 0 }
  2775. };
  2776. if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE, pl_intr_info))
  2777. csio_hw_fatal_err(hw);
  2778. }
  2779. /*
  2780. * csio_hw_slow_intr_handler - control path interrupt handler
  2781. * @hw: HW module
  2782. *
  2783. * Interrupt handler for non-data global interrupt events, e.g., errors.
  2784. * The designation 'slow' is because it involves register reads, while
  2785. * data interrupts typically don't involve any MMIOs.
  2786. */
  2787. int
  2788. csio_hw_slow_intr_handler(struct csio_hw *hw)
  2789. {
  2790. uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE);
  2791. if (!(cause & CSIO_GLBL_INTR_MASK)) {
  2792. CSIO_INC_STATS(hw, n_plint_unexp);
  2793. return 0;
  2794. }
  2795. csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
  2796. CSIO_INC_STATS(hw, n_plint_cnt);
  2797. if (cause & CIM)
  2798. csio_cim_intr_handler(hw);
  2799. if (cause & MPS)
  2800. csio_mps_intr_handler(hw);
  2801. if (cause & NCSI)
  2802. csio_ncsi_intr_handler(hw);
  2803. if (cause & PL)
  2804. csio_pl_intr_handler(hw);
  2805. if (cause & SMB)
  2806. csio_smb_intr_handler(hw);
  2807. if (cause & XGMAC0)
  2808. csio_xgmac_intr_handler(hw, 0);
  2809. if (cause & XGMAC1)
  2810. csio_xgmac_intr_handler(hw, 1);
  2811. if (cause & XGMAC_KR0)
  2812. csio_xgmac_intr_handler(hw, 2);
  2813. if (cause & XGMAC_KR1)
  2814. csio_xgmac_intr_handler(hw, 3);
  2815. if (cause & PCIE)
  2816. hw->chip_ops->chip_pcie_intr_handler(hw);
  2817. if (cause & MC)
  2818. csio_mem_intr_handler(hw, MEM_MC);
  2819. if (cause & EDC0)
  2820. csio_mem_intr_handler(hw, MEM_EDC0);
  2821. if (cause & EDC1)
  2822. csio_mem_intr_handler(hw, MEM_EDC1);
  2823. if (cause & LE)
  2824. csio_le_intr_handler(hw);
  2825. if (cause & TP)
  2826. csio_tp_intr_handler(hw);
  2827. if (cause & MA)
  2828. csio_ma_intr_handler(hw);
  2829. if (cause & PM_TX)
  2830. csio_pmtx_intr_handler(hw);
  2831. if (cause & PM_RX)
  2832. csio_pmrx_intr_handler(hw);
  2833. if (cause & ULP_RX)
  2834. csio_ulprx_intr_handler(hw);
  2835. if (cause & CPL_SWITCH)
  2836. csio_cplsw_intr_handler(hw);
  2837. if (cause & SGE)
  2838. csio_sge_intr_handler(hw);
  2839. if (cause & ULP_TX)
  2840. csio_ulptx_intr_handler(hw);
  2841. /* Clear the interrupts just processed for which we are the master. */
  2842. csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE);
  2843. csio_rd_reg32(hw, PL_INT_CAUSE); /* flush */
  2844. return 1;
  2845. }
  2846. /*****************************************************************************
  2847. * HW <--> mailbox interfacing routines.
  2848. ****************************************************************************/
  2849. /*
  2850. * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
  2851. *
  2852. * @data: Private data pointer.
  2853. *
  2854. * Called from worker thread context.
  2855. */
  2856. static void
  2857. csio_mberr_worker(void *data)
  2858. {
  2859. struct csio_hw *hw = (struct csio_hw *)data;
  2860. struct csio_mbm *mbm = &hw->mbm;
  2861. LIST_HEAD(cbfn_q);
  2862. struct csio_mb *mbp_next;
  2863. int rv;
  2864. del_timer_sync(&mbm->timer);
  2865. spin_lock_irq(&hw->lock);
  2866. if (list_empty(&mbm->cbfn_q)) {
  2867. spin_unlock_irq(&hw->lock);
  2868. return;
  2869. }
  2870. list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
  2871. mbm->stats.n_cbfnq = 0;
  2872. /* Try to start waiting mailboxes */
  2873. if (!list_empty(&mbm->req_q)) {
  2874. mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
  2875. list_del_init(&mbp_next->list);
  2876. rv = csio_mb_issue(hw, mbp_next);
  2877. if (rv != 0)
  2878. list_add_tail(&mbp_next->list, &mbm->req_q);
  2879. else
  2880. CSIO_DEC_STATS(mbm, n_activeq);
  2881. }
  2882. spin_unlock_irq(&hw->lock);
  2883. /* Now callback completions */
  2884. csio_mb_completions(hw, &cbfn_q);
  2885. }
  2886. /*
  2887. * csio_hw_mb_timer - Top-level Mailbox timeout handler.
  2888. *
  2889. * @data: private data pointer
  2890. *
  2891. **/
  2892. static void
  2893. csio_hw_mb_timer(uintptr_t data)
  2894. {
  2895. struct csio_hw *hw = (struct csio_hw *)data;
  2896. struct csio_mb *mbp = NULL;
  2897. spin_lock_irq(&hw->lock);
  2898. mbp = csio_mb_tmo_handler(hw);
  2899. spin_unlock_irq(&hw->lock);
  2900. /* Call back the function for the timed-out Mailbox */
  2901. if (mbp)
  2902. mbp->mb_cbfn(hw, mbp);
  2903. }
  2904. /*
  2905. * csio_hw_mbm_cleanup - Cleanup Mailbox module.
  2906. * @hw: HW module
  2907. *
  2908. * Called with lock held, should exit with lock held.
  2909. * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
  2910. * into a local queue. Drops lock and calls the completions. Holds
  2911. * lock and returns.
  2912. */
  2913. static void
  2914. csio_hw_mbm_cleanup(struct csio_hw *hw)
  2915. {
  2916. LIST_HEAD(cbfn_q);
  2917. csio_mb_cancel_all(hw, &cbfn_q);
  2918. spin_unlock_irq(&hw->lock);
  2919. csio_mb_completions(hw, &cbfn_q);
  2920. spin_lock_irq(&hw->lock);
  2921. }
  2922. /*****************************************************************************
  2923. * Event handling
  2924. ****************************************************************************/
  2925. int
  2926. csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  2927. uint16_t len)
  2928. {
  2929. struct csio_evt_msg *evt_entry = NULL;
  2930. if (type >= CSIO_EVT_MAX)
  2931. return -EINVAL;
  2932. if (len > CSIO_EVT_MSG_SIZE)
  2933. return -EINVAL;
  2934. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  2935. return -EINVAL;
  2936. if (list_empty(&hw->evt_free_q)) {
  2937. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  2938. type, len);
  2939. return -ENOMEM;
  2940. }
  2941. evt_entry = list_first_entry(&hw->evt_free_q,
  2942. struct csio_evt_msg, list);
  2943. list_del_init(&evt_entry->list);
  2944. /* copy event msg and queue the event */
  2945. evt_entry->type = type;
  2946. memcpy((void *)evt_entry->data, evt_msg, len);
  2947. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  2948. CSIO_DEC_STATS(hw, n_evt_freeq);
  2949. CSIO_INC_STATS(hw, n_evt_activeq);
  2950. return 0;
  2951. }
  2952. static int
  2953. csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  2954. uint16_t len, bool msg_sg)
  2955. {
  2956. struct csio_evt_msg *evt_entry = NULL;
  2957. struct csio_fl_dma_buf *fl_sg;
  2958. uint32_t off = 0;
  2959. unsigned long flags;
  2960. int n, ret = 0;
  2961. if (type >= CSIO_EVT_MAX)
  2962. return -EINVAL;
  2963. if (len > CSIO_EVT_MSG_SIZE)
  2964. return -EINVAL;
  2965. spin_lock_irqsave(&hw->lock, flags);
  2966. if (hw->flags & CSIO_HWF_FWEVT_STOP) {
  2967. ret = -EINVAL;
  2968. goto out;
  2969. }
  2970. if (list_empty(&hw->evt_free_q)) {
  2971. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  2972. type, len);
  2973. ret = -ENOMEM;
  2974. goto out;
  2975. }
  2976. evt_entry = list_first_entry(&hw->evt_free_q,
  2977. struct csio_evt_msg, list);
  2978. list_del_init(&evt_entry->list);
  2979. /* copy event msg and queue the event */
  2980. evt_entry->type = type;
  2981. /* If Payload in SG list*/
  2982. if (msg_sg) {
  2983. fl_sg = (struct csio_fl_dma_buf *) evt_msg;
  2984. for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
  2985. memcpy((void *)((uintptr_t)evt_entry->data + off),
  2986. fl_sg->flbufs[n].vaddr,
  2987. fl_sg->flbufs[n].len);
  2988. off += fl_sg->flbufs[n].len;
  2989. }
  2990. } else
  2991. memcpy((void *)evt_entry->data, evt_msg, len);
  2992. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  2993. CSIO_DEC_STATS(hw, n_evt_freeq);
  2994. CSIO_INC_STATS(hw, n_evt_activeq);
  2995. out:
  2996. spin_unlock_irqrestore(&hw->lock, flags);
  2997. return ret;
  2998. }
  2999. static void
  3000. csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
  3001. {
  3002. if (evt_entry) {
  3003. spin_lock_irq(&hw->lock);
  3004. list_del_init(&evt_entry->list);
  3005. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3006. CSIO_DEC_STATS(hw, n_evt_activeq);
  3007. CSIO_INC_STATS(hw, n_evt_freeq);
  3008. spin_unlock_irq(&hw->lock);
  3009. }
  3010. }
  3011. void
  3012. csio_evtq_flush(struct csio_hw *hw)
  3013. {
  3014. uint32_t count;
  3015. count = 30;
  3016. while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
  3017. spin_unlock_irq(&hw->lock);
  3018. msleep(2000);
  3019. spin_lock_irq(&hw->lock);
  3020. }
  3021. CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
  3022. }
  3023. static void
  3024. csio_evtq_stop(struct csio_hw *hw)
  3025. {
  3026. hw->flags |= CSIO_HWF_FWEVT_STOP;
  3027. }
  3028. static void
  3029. csio_evtq_start(struct csio_hw *hw)
  3030. {
  3031. hw->flags &= ~CSIO_HWF_FWEVT_STOP;
  3032. }
  3033. static void
  3034. csio_evtq_cleanup(struct csio_hw *hw)
  3035. {
  3036. struct list_head *evt_entry, *next_entry;
  3037. /* Release outstanding events from activeq to freeq*/
  3038. if (!list_empty(&hw->evt_active_q))
  3039. list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
  3040. hw->stats.n_evt_activeq = 0;
  3041. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3042. /* Freeup event entry */
  3043. list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
  3044. kfree(evt_entry);
  3045. CSIO_DEC_STATS(hw, n_evt_freeq);
  3046. }
  3047. hw->stats.n_evt_freeq = 0;
  3048. }
  3049. static void
  3050. csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
  3051. struct csio_fl_dma_buf *flb, void *priv)
  3052. {
  3053. __u8 op;
  3054. void *msg = NULL;
  3055. uint32_t msg_len = 0;
  3056. bool msg_sg = 0;
  3057. op = ((struct rss_header *) wr)->opcode;
  3058. if (op == CPL_FW6_PLD) {
  3059. CSIO_INC_STATS(hw, n_cpl_fw6_pld);
  3060. if (!flb || !flb->totlen) {
  3061. CSIO_INC_STATS(hw, n_cpl_unexp);
  3062. return;
  3063. }
  3064. msg = (void *) flb;
  3065. msg_len = flb->totlen;
  3066. msg_sg = 1;
  3067. } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
  3068. CSIO_INC_STATS(hw, n_cpl_fw6_msg);
  3069. /* skip RSS header */
  3070. msg = (void *)((uintptr_t)wr + sizeof(__be64));
  3071. msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
  3072. sizeof(struct cpl_fw4_msg);
  3073. } else {
  3074. csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
  3075. CSIO_INC_STATS(hw, n_cpl_unexp);
  3076. return;
  3077. }
  3078. /*
  3079. * Enqueue event to EventQ. Events processing happens
  3080. * in Event worker thread context
  3081. */
  3082. if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
  3083. (uint16_t)msg_len, msg_sg))
  3084. CSIO_INC_STATS(hw, n_evt_drop);
  3085. }
  3086. void
  3087. csio_evtq_worker(struct work_struct *work)
  3088. {
  3089. struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
  3090. struct list_head *evt_entry, *next_entry;
  3091. LIST_HEAD(evt_q);
  3092. struct csio_evt_msg *evt_msg;
  3093. struct cpl_fw6_msg *msg;
  3094. struct csio_rnode *rn;
  3095. int rv = 0;
  3096. uint8_t evtq_stop = 0;
  3097. csio_dbg(hw, "event worker thread active evts#%d\n",
  3098. hw->stats.n_evt_activeq);
  3099. spin_lock_irq(&hw->lock);
  3100. while (!list_empty(&hw->evt_active_q)) {
  3101. list_splice_tail_init(&hw->evt_active_q, &evt_q);
  3102. spin_unlock_irq(&hw->lock);
  3103. list_for_each_safe(evt_entry, next_entry, &evt_q) {
  3104. evt_msg = (struct csio_evt_msg *) evt_entry;
  3105. /* Drop events if queue is STOPPED */
  3106. spin_lock_irq(&hw->lock);
  3107. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3108. evtq_stop = 1;
  3109. spin_unlock_irq(&hw->lock);
  3110. if (evtq_stop) {
  3111. CSIO_INC_STATS(hw, n_evt_drop);
  3112. goto free_evt;
  3113. }
  3114. switch (evt_msg->type) {
  3115. case CSIO_EVT_FW:
  3116. msg = (struct cpl_fw6_msg *)(evt_msg->data);
  3117. if ((msg->opcode == CPL_FW6_MSG ||
  3118. msg->opcode == CPL_FW4_MSG) &&
  3119. !msg->type) {
  3120. rv = csio_mb_fwevt_handler(hw,
  3121. msg->data);
  3122. if (!rv)
  3123. break;
  3124. /* Handle any remaining fw events */
  3125. csio_fcoe_fwevt_handler(hw,
  3126. msg->opcode, msg->data);
  3127. } else if (msg->opcode == CPL_FW6_PLD) {
  3128. csio_fcoe_fwevt_handler(hw,
  3129. msg->opcode, msg->data);
  3130. } else {
  3131. csio_warn(hw,
  3132. "Unhandled FW msg op %x type %x\n",
  3133. msg->opcode, msg->type);
  3134. CSIO_INC_STATS(hw, n_evt_drop);
  3135. }
  3136. break;
  3137. case CSIO_EVT_MBX:
  3138. csio_mberr_worker(hw);
  3139. break;
  3140. case CSIO_EVT_DEV_LOSS:
  3141. memcpy(&rn, evt_msg->data, sizeof(rn));
  3142. csio_rnode_devloss_handler(rn);
  3143. break;
  3144. default:
  3145. csio_warn(hw, "Unhandled event %x on evtq\n",
  3146. evt_msg->type);
  3147. CSIO_INC_STATS(hw, n_evt_unexp);
  3148. break;
  3149. }
  3150. free_evt:
  3151. csio_free_evt(hw, evt_msg);
  3152. }
  3153. spin_lock_irq(&hw->lock);
  3154. }
  3155. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3156. spin_unlock_irq(&hw->lock);
  3157. }
  3158. int
  3159. csio_fwevtq_handler(struct csio_hw *hw)
  3160. {
  3161. int rv;
  3162. if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
  3163. CSIO_INC_STATS(hw, n_int_stray);
  3164. return -EINVAL;
  3165. }
  3166. rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
  3167. csio_process_fwevtq_entry, NULL);
  3168. return rv;
  3169. }
  3170. /****************************************************************************
  3171. * Entry points
  3172. ****************************************************************************/
  3173. /* Management module */
  3174. /*
  3175. * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
  3176. * mgmt - mgmt module
  3177. * @io_req - io request
  3178. *
  3179. * Return - 0:if given IO Req exists in active Q.
  3180. * -EINVAL :if lookup fails.
  3181. */
  3182. int
  3183. csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
  3184. {
  3185. struct list_head *tmp;
  3186. /* Lookup ioreq in the ACTIVEQ */
  3187. list_for_each(tmp, &mgmtm->active_q) {
  3188. if (io_req == (struct csio_ioreq *)tmp)
  3189. return 0;
  3190. }
  3191. return -EINVAL;
  3192. }
  3193. #define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
  3194. /*
  3195. * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
  3196. * @data - Event data.
  3197. *
  3198. * Return - none.
  3199. */
  3200. static void
  3201. csio_mgmt_tmo_handler(uintptr_t data)
  3202. {
  3203. struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
  3204. struct list_head *tmp;
  3205. struct csio_ioreq *io_req;
  3206. csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
  3207. spin_lock_irq(&mgmtm->hw->lock);
  3208. list_for_each(tmp, &mgmtm->active_q) {
  3209. io_req = (struct csio_ioreq *) tmp;
  3210. io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
  3211. if (!io_req->tmo) {
  3212. /* Dequeue the request from retry Q. */
  3213. tmp = csio_list_prev(tmp);
  3214. list_del_init(&io_req->sm.sm_list);
  3215. if (io_req->io_cbfn) {
  3216. /* io_req will be freed by completion handler */
  3217. io_req->wr_status = -ETIMEDOUT;
  3218. io_req->io_cbfn(mgmtm->hw, io_req);
  3219. } else {
  3220. CSIO_DB_ASSERT(0);
  3221. }
  3222. }
  3223. }
  3224. /* If retry queue is not empty, re-arm timer */
  3225. if (!list_empty(&mgmtm->active_q))
  3226. mod_timer(&mgmtm->mgmt_timer,
  3227. jiffies + msecs_to_jiffies(ECM_MIN_TMO));
  3228. spin_unlock_irq(&mgmtm->hw->lock);
  3229. }
  3230. static void
  3231. csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
  3232. {
  3233. struct csio_hw *hw = mgmtm->hw;
  3234. struct csio_ioreq *io_req;
  3235. struct list_head *tmp;
  3236. uint32_t count;
  3237. count = 30;
  3238. /* Wait for all outstanding req to complete gracefully */
  3239. while ((!list_empty(&mgmtm->active_q)) && count--) {
  3240. spin_unlock_irq(&hw->lock);
  3241. msleep(2000);
  3242. spin_lock_irq(&hw->lock);
  3243. }
  3244. /* release outstanding req from ACTIVEQ */
  3245. list_for_each(tmp, &mgmtm->active_q) {
  3246. io_req = (struct csio_ioreq *) tmp;
  3247. tmp = csio_list_prev(tmp);
  3248. list_del_init(&io_req->sm.sm_list);
  3249. mgmtm->stats.n_active--;
  3250. if (io_req->io_cbfn) {
  3251. /* io_req will be freed by completion handler */
  3252. io_req->wr_status = -ETIMEDOUT;
  3253. io_req->io_cbfn(mgmtm->hw, io_req);
  3254. }
  3255. }
  3256. }
  3257. /*
  3258. * csio_mgmt_init - Mgmt module init entry point
  3259. * @mgmtsm - mgmt module
  3260. * @hw - HW module
  3261. *
  3262. * Initialize mgmt timer, resource wait queue, active queue,
  3263. * completion q. Allocate Egress and Ingress
  3264. * WR queues and save off the queue index returned by the WR
  3265. * module for future use. Allocate and save off mgmt reqs in the
  3266. * mgmt_req_freelist for future use. Make sure their SM is initialized
  3267. * to uninit state.
  3268. * Returns: 0 - on success
  3269. * -ENOMEM - on error.
  3270. */
  3271. static int
  3272. csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
  3273. {
  3274. struct timer_list *timer = &mgmtm->mgmt_timer;
  3275. init_timer(timer);
  3276. timer->function = csio_mgmt_tmo_handler;
  3277. timer->data = (unsigned long)mgmtm;
  3278. INIT_LIST_HEAD(&mgmtm->active_q);
  3279. INIT_LIST_HEAD(&mgmtm->cbfn_q);
  3280. mgmtm->hw = hw;
  3281. /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
  3282. return 0;
  3283. }
  3284. /*
  3285. * csio_mgmtm_exit - MGMT module exit entry point
  3286. * @mgmtsm - mgmt module
  3287. *
  3288. * This function called during MGMT module uninit.
  3289. * Stop timers, free ioreqs allocated.
  3290. * Returns: None
  3291. *
  3292. */
  3293. static void
  3294. csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
  3295. {
  3296. del_timer_sync(&mgmtm->mgmt_timer);
  3297. }
  3298. /**
  3299. * csio_hw_start - Kicks off the HW State machine
  3300. * @hw: Pointer to HW module.
  3301. *
  3302. * It is assumed that the initialization is a synchronous operation.
  3303. * So when we return afer posting the event, the HW SM should be in
  3304. * the ready state, if there were no errors during init.
  3305. */
  3306. int
  3307. csio_hw_start(struct csio_hw *hw)
  3308. {
  3309. spin_lock_irq(&hw->lock);
  3310. csio_post_event(&hw->sm, CSIO_HWE_CFG);
  3311. spin_unlock_irq(&hw->lock);
  3312. if (csio_is_hw_ready(hw))
  3313. return 0;
  3314. else
  3315. return -EINVAL;
  3316. }
  3317. int
  3318. csio_hw_stop(struct csio_hw *hw)
  3319. {
  3320. csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
  3321. if (csio_is_hw_removing(hw))
  3322. return 0;
  3323. else
  3324. return -EINVAL;
  3325. }
  3326. /* Max reset retries */
  3327. #define CSIO_MAX_RESET_RETRIES 3
  3328. /**
  3329. * csio_hw_reset - Reset the hardware
  3330. * @hw: HW module.
  3331. *
  3332. * Caller should hold lock across this function.
  3333. */
  3334. int
  3335. csio_hw_reset(struct csio_hw *hw)
  3336. {
  3337. if (!csio_is_hw_master(hw))
  3338. return -EPERM;
  3339. if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
  3340. csio_dbg(hw, "Max hw reset attempts reached..");
  3341. return -EINVAL;
  3342. }
  3343. hw->rst_retries++;
  3344. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
  3345. if (csio_is_hw_ready(hw)) {
  3346. hw->rst_retries = 0;
  3347. hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
  3348. return 0;
  3349. } else
  3350. return -EINVAL;
  3351. }
  3352. /*
  3353. * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
  3354. * @hw: HW module.
  3355. */
  3356. static void
  3357. csio_hw_get_device_id(struct csio_hw *hw)
  3358. {
  3359. /* Is the adapter device id cached already ?*/
  3360. if (csio_is_dev_id_cached(hw))
  3361. return;
  3362. /* Get the PCI vendor & device id */
  3363. pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
  3364. &hw->params.pci.vendor_id);
  3365. pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
  3366. &hw->params.pci.device_id);
  3367. csio_dev_id_cached(hw);
  3368. hw->chip_id = (hw->params.pci.device_id & CSIO_HW_CHIP_MASK);
  3369. } /* csio_hw_get_device_id */
  3370. /*
  3371. * csio_hw_set_description - Set the model, description of the hw.
  3372. * @hw: HW module.
  3373. * @ven_id: PCI Vendor ID
  3374. * @dev_id: PCI Device ID
  3375. */
  3376. static void
  3377. csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
  3378. {
  3379. uint32_t adap_type, prot_type;
  3380. if (ven_id == CSIO_VENDOR_ID) {
  3381. prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
  3382. adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
  3383. if (prot_type == CSIO_T4_FCOE_ASIC) {
  3384. memcpy(hw->hw_ver,
  3385. csio_t4_fcoe_adapters[adap_type].model_no, 16);
  3386. memcpy(hw->model_desc,
  3387. csio_t4_fcoe_adapters[adap_type].description,
  3388. 32);
  3389. } else if (prot_type == CSIO_T5_FCOE_ASIC) {
  3390. memcpy(hw->hw_ver,
  3391. csio_t5_fcoe_adapters[adap_type].model_no, 16);
  3392. memcpy(hw->model_desc,
  3393. csio_t5_fcoe_adapters[adap_type].description,
  3394. 32);
  3395. } else {
  3396. char tempName[32] = "Chelsio FCoE Controller";
  3397. memcpy(hw->model_desc, tempName, 32);
  3398. }
  3399. }
  3400. } /* csio_hw_set_description */
  3401. /**
  3402. * csio_hw_init - Initialize HW module.
  3403. * @hw: Pointer to HW module.
  3404. *
  3405. * Initialize the members of the HW module.
  3406. */
  3407. int
  3408. csio_hw_init(struct csio_hw *hw)
  3409. {
  3410. int rv = -EINVAL;
  3411. uint32_t i;
  3412. uint16_t ven_id, dev_id;
  3413. struct csio_evt_msg *evt_entry;
  3414. INIT_LIST_HEAD(&hw->sm.sm_list);
  3415. csio_init_state(&hw->sm, csio_hws_uninit);
  3416. spin_lock_init(&hw->lock);
  3417. INIT_LIST_HEAD(&hw->sln_head);
  3418. /* Get the PCI vendor & device id */
  3419. csio_hw_get_device_id(hw);
  3420. strcpy(hw->name, CSIO_HW_NAME);
  3421. /* Initialize the HW chip ops with T4/T5 specific ops */
  3422. hw->chip_ops = csio_is_t4(hw->chip_id) ? &t4_ops : &t5_ops;
  3423. /* Set the model & its description */
  3424. ven_id = hw->params.pci.vendor_id;
  3425. dev_id = hw->params.pci.device_id;
  3426. csio_hw_set_description(hw, ven_id, dev_id);
  3427. /* Initialize default log level */
  3428. hw->params.log_level = (uint32_t) csio_dbg_level;
  3429. csio_set_fwevt_intr_idx(hw, -1);
  3430. csio_set_nondata_intr_idx(hw, -1);
  3431. /* Init all the modules: Mailbox, WorkRequest and Transport */
  3432. if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
  3433. goto err;
  3434. rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
  3435. if (rv)
  3436. goto err_mbm_exit;
  3437. rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
  3438. if (rv)
  3439. goto err_wrm_exit;
  3440. rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
  3441. if (rv)
  3442. goto err_scsim_exit;
  3443. /* Pre-allocate evtq and initialize them */
  3444. INIT_LIST_HEAD(&hw->evt_active_q);
  3445. INIT_LIST_HEAD(&hw->evt_free_q);
  3446. for (i = 0; i < csio_evtq_sz; i++) {
  3447. evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
  3448. if (!evt_entry) {
  3449. csio_err(hw, "Failed to initialize eventq");
  3450. goto err_evtq_cleanup;
  3451. }
  3452. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3453. CSIO_INC_STATS(hw, n_evt_freeq);
  3454. }
  3455. hw->dev_num = dev_num;
  3456. dev_num++;
  3457. return 0;
  3458. err_evtq_cleanup:
  3459. csio_evtq_cleanup(hw);
  3460. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3461. err_scsim_exit:
  3462. csio_scsim_exit(csio_hw_to_scsim(hw));
  3463. err_wrm_exit:
  3464. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3465. err_mbm_exit:
  3466. csio_mbm_exit(csio_hw_to_mbm(hw));
  3467. err:
  3468. return rv;
  3469. }
  3470. /**
  3471. * csio_hw_exit - Un-initialize HW module.
  3472. * @hw: Pointer to HW module.
  3473. *
  3474. */
  3475. void
  3476. csio_hw_exit(struct csio_hw *hw)
  3477. {
  3478. csio_evtq_cleanup(hw);
  3479. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3480. csio_scsim_exit(csio_hw_to_scsim(hw));
  3481. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3482. csio_mbm_exit(csio_hw_to_mbm(hw));
  3483. }