csio_hw.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967
  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. /*****************************************************************************/
  738. /* HW State machine assists */
  739. /*****************************************************************************/
  740. static int
  741. csio_hw_dev_ready(struct csio_hw *hw)
  742. {
  743. uint32_t reg;
  744. int cnt = 6;
  745. while (((reg = csio_rd_reg32(hw, PL_WHOAMI)) == 0xFFFFFFFF) &&
  746. (--cnt != 0))
  747. mdelay(100);
  748. if ((cnt == 0) && (((int32_t)(SOURCEPF_GET(reg)) < 0) ||
  749. (SOURCEPF_GET(reg) >= CSIO_MAX_PFN))) {
  750. csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
  751. return -EIO;
  752. }
  753. hw->pfn = SOURCEPF_GET(reg);
  754. return 0;
  755. }
  756. /*
  757. * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
  758. * @hw: HW module
  759. * @state: Device state
  760. *
  761. * FW_HELLO_CMD has to be polled for completion.
  762. */
  763. static int
  764. csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
  765. {
  766. struct csio_mb *mbp;
  767. int rv = 0;
  768. enum csio_dev_master master;
  769. enum fw_retval retval;
  770. uint8_t mpfn;
  771. char state_str[16];
  772. int retries = FW_CMD_HELLO_RETRIES;
  773. memset(state_str, 0, sizeof(state_str));
  774. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  775. if (!mbp) {
  776. rv = -ENOMEM;
  777. CSIO_INC_STATS(hw, n_err_nomem);
  778. goto out;
  779. }
  780. master = csio_force_master ? CSIO_MASTER_MUST : CSIO_MASTER_MAY;
  781. retry:
  782. csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
  783. hw->pfn, master, NULL);
  784. rv = csio_mb_issue(hw, mbp);
  785. if (rv) {
  786. csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
  787. goto out_free_mb;
  788. }
  789. csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
  790. if (retval != FW_SUCCESS) {
  791. csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
  792. rv = -EINVAL;
  793. goto out_free_mb;
  794. }
  795. /* Firmware has designated us to be master */
  796. if (hw->pfn == mpfn) {
  797. hw->flags |= CSIO_HWF_MASTER;
  798. } else if (*state == CSIO_DEV_STATE_UNINIT) {
  799. /*
  800. * If we're not the Master PF then we need to wait around for
  801. * the Master PF Driver to finish setting up the adapter.
  802. *
  803. * Note that we also do this wait if we're a non-Master-capable
  804. * PF and there is no current Master PF; a Master PF may show up
  805. * momentarily and we wouldn't want to fail pointlessly. (This
  806. * can happen when an OS loads lots of different drivers rapidly
  807. * at the same time). In this case, the Master PF returned by
  808. * the firmware will be PCIE_FW_MASTER_MASK so the test below
  809. * will work ...
  810. */
  811. int waiting = FW_CMD_HELLO_TIMEOUT;
  812. /*
  813. * Wait for the firmware to either indicate an error or
  814. * initialized state. If we see either of these we bail out
  815. * and report the issue to the caller. If we exhaust the
  816. * "hello timeout" and we haven't exhausted our retries, try
  817. * again. Otherwise bail with a timeout error.
  818. */
  819. for (;;) {
  820. uint32_t pcie_fw;
  821. spin_unlock_irq(&hw->lock);
  822. msleep(50);
  823. spin_lock_irq(&hw->lock);
  824. waiting -= 50;
  825. /*
  826. * If neither Error nor Initialialized are indicated
  827. * by the firmware keep waiting till we exaust our
  828. * timeout ... and then retry if we haven't exhausted
  829. * our retries ...
  830. */
  831. pcie_fw = csio_rd_reg32(hw, PCIE_FW);
  832. if (!(pcie_fw & (PCIE_FW_ERR|PCIE_FW_INIT))) {
  833. if (waiting <= 0) {
  834. if (retries-- > 0)
  835. goto retry;
  836. rv = -ETIMEDOUT;
  837. break;
  838. }
  839. continue;
  840. }
  841. /*
  842. * We either have an Error or Initialized condition
  843. * report errors preferentially.
  844. */
  845. if (state) {
  846. if (pcie_fw & PCIE_FW_ERR) {
  847. *state = CSIO_DEV_STATE_ERR;
  848. rv = -ETIMEDOUT;
  849. } else if (pcie_fw & PCIE_FW_INIT)
  850. *state = CSIO_DEV_STATE_INIT;
  851. }
  852. /*
  853. * If we arrived before a Master PF was selected and
  854. * there's not a valid Master PF, grab its identity
  855. * for our caller.
  856. */
  857. if (mpfn == PCIE_FW_MASTER_MASK &&
  858. (pcie_fw & PCIE_FW_MASTER_VLD))
  859. mpfn = PCIE_FW_MASTER_GET(pcie_fw);
  860. break;
  861. }
  862. hw->flags &= ~CSIO_HWF_MASTER;
  863. }
  864. switch (*state) {
  865. case CSIO_DEV_STATE_UNINIT:
  866. strcpy(state_str, "Initializing");
  867. break;
  868. case CSIO_DEV_STATE_INIT:
  869. strcpy(state_str, "Initialized");
  870. break;
  871. case CSIO_DEV_STATE_ERR:
  872. strcpy(state_str, "Error");
  873. break;
  874. default:
  875. strcpy(state_str, "Unknown");
  876. break;
  877. }
  878. if (hw->pfn == mpfn)
  879. csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
  880. hw->pfn, state_str);
  881. else
  882. csio_info(hw,
  883. "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
  884. hw->pfn, mpfn, state_str);
  885. out_free_mb:
  886. mempool_free(mbp, hw->mb_mempool);
  887. out:
  888. return rv;
  889. }
  890. /*
  891. * csio_do_bye - Perform the BYE FW Mailbox command and process response.
  892. * @hw: HW module
  893. *
  894. */
  895. static int
  896. csio_do_bye(struct csio_hw *hw)
  897. {
  898. struct csio_mb *mbp;
  899. enum fw_retval retval;
  900. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  901. if (!mbp) {
  902. CSIO_INC_STATS(hw, n_err_nomem);
  903. return -ENOMEM;
  904. }
  905. csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  906. if (csio_mb_issue(hw, mbp)) {
  907. csio_err(hw, "Issue of BYE command failed\n");
  908. mempool_free(mbp, hw->mb_mempool);
  909. return -EINVAL;
  910. }
  911. retval = csio_mb_fw_retval(mbp);
  912. if (retval != FW_SUCCESS) {
  913. mempool_free(mbp, hw->mb_mempool);
  914. return -EINVAL;
  915. }
  916. mempool_free(mbp, hw->mb_mempool);
  917. return 0;
  918. }
  919. /*
  920. * csio_do_reset- Perform the device reset.
  921. * @hw: HW module
  922. * @fw_rst: FW reset
  923. *
  924. * If fw_rst is set, issues FW reset mbox cmd otherwise
  925. * does PIO reset.
  926. * Performs reset of the function.
  927. */
  928. static int
  929. csio_do_reset(struct csio_hw *hw, bool fw_rst)
  930. {
  931. struct csio_mb *mbp;
  932. enum fw_retval retval;
  933. if (!fw_rst) {
  934. /* PIO reset */
  935. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  936. mdelay(2000);
  937. return 0;
  938. }
  939. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  940. if (!mbp) {
  941. CSIO_INC_STATS(hw, n_err_nomem);
  942. return -ENOMEM;
  943. }
  944. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  945. PIORSTMODE | PIORST, 0, NULL);
  946. if (csio_mb_issue(hw, mbp)) {
  947. csio_err(hw, "Issue of RESET command failed.n");
  948. mempool_free(mbp, hw->mb_mempool);
  949. return -EINVAL;
  950. }
  951. retval = csio_mb_fw_retval(mbp);
  952. if (retval != FW_SUCCESS) {
  953. csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
  954. mempool_free(mbp, hw->mb_mempool);
  955. return -EINVAL;
  956. }
  957. mempool_free(mbp, hw->mb_mempool);
  958. return 0;
  959. }
  960. static int
  961. csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
  962. {
  963. struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
  964. uint16_t caps;
  965. caps = ntohs(rsp->fcoecaps);
  966. if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
  967. csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
  968. return -EINVAL;
  969. }
  970. if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
  971. csio_err(hw, "No FCoE Control Offload capability\n");
  972. return -EINVAL;
  973. }
  974. return 0;
  975. }
  976. /*
  977. * csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
  978. * @hw: the HW module
  979. * @mbox: mailbox to use for the FW RESET command (if desired)
  980. * @force: force uP into RESET even if FW RESET command fails
  981. *
  982. * Issues a RESET command to firmware (if desired) with a HALT indication
  983. * and then puts the microprocessor into RESET state. The RESET command
  984. * will only be issued if a legitimate mailbox is provided (mbox <=
  985. * PCIE_FW_MASTER_MASK).
  986. *
  987. * This is generally used in order for the host to safely manipulate the
  988. * adapter without fear of conflicting with whatever the firmware might
  989. * be doing. The only way out of this state is to RESTART the firmware
  990. * ...
  991. */
  992. static int
  993. csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
  994. {
  995. enum fw_retval retval = 0;
  996. /*
  997. * If a legitimate mailbox is provided, issue a RESET command
  998. * with a HALT indication.
  999. */
  1000. if (mbox <= PCIE_FW_MASTER_MASK) {
  1001. struct csio_mb *mbp;
  1002. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1003. if (!mbp) {
  1004. CSIO_INC_STATS(hw, n_err_nomem);
  1005. return -ENOMEM;
  1006. }
  1007. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1008. PIORSTMODE | PIORST, FW_RESET_CMD_HALT(1),
  1009. NULL);
  1010. if (csio_mb_issue(hw, mbp)) {
  1011. csio_err(hw, "Issue of RESET command failed!\n");
  1012. mempool_free(mbp, hw->mb_mempool);
  1013. return -EINVAL;
  1014. }
  1015. retval = csio_mb_fw_retval(mbp);
  1016. mempool_free(mbp, hw->mb_mempool);
  1017. }
  1018. /*
  1019. * Normally we won't complete the operation if the firmware RESET
  1020. * command fails but if our caller insists we'll go ahead and put the
  1021. * uP into RESET. This can be useful if the firmware is hung or even
  1022. * missing ... We'll have to take the risk of putting the uP into
  1023. * RESET without the cooperation of firmware in that case.
  1024. *
  1025. * We also force the firmware's HALT flag to be on in case we bypassed
  1026. * the firmware RESET command above or we're dealing with old firmware
  1027. * which doesn't have the HALT capability. This will serve as a flag
  1028. * for the incoming firmware to know that it's coming out of a HALT
  1029. * rather than a RESET ... if it's new enough to understand that ...
  1030. */
  1031. if (retval == 0 || force) {
  1032. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, UPCRST);
  1033. csio_set_reg_field(hw, PCIE_FW, PCIE_FW_HALT, PCIE_FW_HALT);
  1034. }
  1035. /*
  1036. * And we always return the result of the firmware RESET command
  1037. * even when we force the uP into RESET ...
  1038. */
  1039. return retval ? -EINVAL : 0;
  1040. }
  1041. /*
  1042. * csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
  1043. * @hw: the HW module
  1044. * @reset: if we want to do a RESET to restart things
  1045. *
  1046. * Restart firmware previously halted by csio_hw_fw_halt(). On successful
  1047. * return the previous PF Master remains as the new PF Master and there
  1048. * is no need to issue a new HELLO command, etc.
  1049. *
  1050. * We do this in two ways:
  1051. *
  1052. * 1. If we're dealing with newer firmware we'll simply want to take
  1053. * the chip's microprocessor out of RESET. This will cause the
  1054. * firmware to start up from its start vector. And then we'll loop
  1055. * until the firmware indicates it's started again (PCIE_FW.HALT
  1056. * reset to 0) or we timeout.
  1057. *
  1058. * 2. If we're dealing with older firmware then we'll need to RESET
  1059. * the chip since older firmware won't recognize the PCIE_FW.HALT
  1060. * flag and automatically RESET itself on startup.
  1061. */
  1062. static int
  1063. csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
  1064. {
  1065. if (reset) {
  1066. /*
  1067. * Since we're directing the RESET instead of the firmware
  1068. * doing it automatically, we need to clear the PCIE_FW.HALT
  1069. * bit.
  1070. */
  1071. csio_set_reg_field(hw, PCIE_FW, PCIE_FW_HALT, 0);
  1072. /*
  1073. * If we've been given a valid mailbox, first try to get the
  1074. * firmware to do the RESET. If that works, great and we can
  1075. * return success. Otherwise, if we haven't been given a
  1076. * valid mailbox or the RESET command failed, fall back to
  1077. * hitting the chip with a hammer.
  1078. */
  1079. if (mbox <= PCIE_FW_MASTER_MASK) {
  1080. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, 0);
  1081. msleep(100);
  1082. if (csio_do_reset(hw, true) == 0)
  1083. return 0;
  1084. }
  1085. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  1086. msleep(2000);
  1087. } else {
  1088. int ms;
  1089. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, 0);
  1090. for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
  1091. if (!(csio_rd_reg32(hw, PCIE_FW) & PCIE_FW_HALT))
  1092. return 0;
  1093. msleep(100);
  1094. ms += 100;
  1095. }
  1096. return -ETIMEDOUT;
  1097. }
  1098. return 0;
  1099. }
  1100. /*
  1101. * csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
  1102. * @hw: the HW module
  1103. * @mbox: mailbox to use for the FW RESET command (if desired)
  1104. * @fw_data: the firmware image to write
  1105. * @size: image size
  1106. * @force: force upgrade even if firmware doesn't cooperate
  1107. *
  1108. * Perform all of the steps necessary for upgrading an adapter's
  1109. * firmware image. Normally this requires the cooperation of the
  1110. * existing firmware in order to halt all existing activities
  1111. * but if an invalid mailbox token is passed in we skip that step
  1112. * (though we'll still put the adapter microprocessor into RESET in
  1113. * that case).
  1114. *
  1115. * On successful return the new firmware will have been loaded and
  1116. * the adapter will have been fully RESET losing all previous setup
  1117. * state. On unsuccessful return the adapter may be completely hosed ...
  1118. * positive errno indicates that the adapter is ~probably~ intact, a
  1119. * negative errno indicates that things are looking bad ...
  1120. */
  1121. static int
  1122. csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
  1123. const u8 *fw_data, uint32_t size, int32_t force)
  1124. {
  1125. const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
  1126. int reset, ret;
  1127. ret = csio_hw_fw_halt(hw, mbox, force);
  1128. if (ret != 0 && !force)
  1129. return ret;
  1130. ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
  1131. if (ret != 0)
  1132. return ret;
  1133. /*
  1134. * Older versions of the firmware don't understand the new
  1135. * PCIE_FW.HALT flag and so won't know to perform a RESET when they
  1136. * restart. So for newly loaded older firmware we'll have to do the
  1137. * RESET for it so it starts up on a clean slate. We can tell if
  1138. * the newly loaded firmware will handle this right by checking
  1139. * its header flags to see if it advertises the capability.
  1140. */
  1141. reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
  1142. return csio_hw_fw_restart(hw, mbox, reset);
  1143. }
  1144. /*
  1145. * csio_hw_fw_config_file - setup an adapter via a Configuration File
  1146. * @hw: the HW module
  1147. * @mbox: mailbox to use for the FW command
  1148. * @mtype: the memory type where the Configuration File is located
  1149. * @maddr: the memory address where the Configuration File is located
  1150. * @finiver: return value for CF [fini] version
  1151. * @finicsum: return value for CF [fini] checksum
  1152. * @cfcsum: return value for CF computed checksum
  1153. *
  1154. * Issue a command to get the firmware to process the Configuration
  1155. * File located at the specified mtype/maddress. If the Configuration
  1156. * File is processed successfully and return value pointers are
  1157. * provided, the Configuration File "[fini] section version and
  1158. * checksum values will be returned along with the computed checksum.
  1159. * It's up to the caller to decide how it wants to respond to the
  1160. * checksums not matching but it recommended that a prominant warning
  1161. * be emitted in order to help people rapidly identify changed or
  1162. * corrupted Configuration Files.
  1163. *
  1164. * Also note that it's possible to modify things like "niccaps",
  1165. * "toecaps",etc. between processing the Configuration File and telling
  1166. * the firmware to use the new configuration. Callers which want to
  1167. * do this will need to "hand-roll" their own CAPS_CONFIGS commands for
  1168. * Configuration Files if they want to do this.
  1169. */
  1170. static int
  1171. csio_hw_fw_config_file(struct csio_hw *hw,
  1172. unsigned int mtype, unsigned int maddr,
  1173. uint32_t *finiver, uint32_t *finicsum, uint32_t *cfcsum)
  1174. {
  1175. struct csio_mb *mbp;
  1176. struct fw_caps_config_cmd *caps_cmd;
  1177. int rv = -EINVAL;
  1178. enum fw_retval ret;
  1179. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1180. if (!mbp) {
  1181. CSIO_INC_STATS(hw, n_err_nomem);
  1182. return -ENOMEM;
  1183. }
  1184. /*
  1185. * Tell the firmware to process the indicated Configuration File.
  1186. * If there are no errors and the caller has provided return value
  1187. * pointers for the [fini] section version, checksum and computed
  1188. * checksum, pass those back to the caller.
  1189. */
  1190. caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
  1191. CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
  1192. caps_cmd->op_to_write =
  1193. htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  1194. FW_CMD_REQUEST |
  1195. FW_CMD_READ);
  1196. caps_cmd->cfvalid_to_len16 =
  1197. htonl(FW_CAPS_CONFIG_CMD_CFVALID |
  1198. FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
  1199. FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
  1200. FW_LEN16(*caps_cmd));
  1201. if (csio_mb_issue(hw, mbp)) {
  1202. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
  1203. goto out;
  1204. }
  1205. ret = csio_mb_fw_retval(mbp);
  1206. if (ret != FW_SUCCESS) {
  1207. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1208. goto out;
  1209. }
  1210. if (finiver)
  1211. *finiver = ntohl(caps_cmd->finiver);
  1212. if (finicsum)
  1213. *finicsum = ntohl(caps_cmd->finicsum);
  1214. if (cfcsum)
  1215. *cfcsum = ntohl(caps_cmd->cfcsum);
  1216. /* Validate device capabilities */
  1217. if (csio_hw_validate_caps(hw, mbp)) {
  1218. rv = -ENOENT;
  1219. goto out;
  1220. }
  1221. /*
  1222. * And now tell the firmware to use the configuration we just loaded.
  1223. */
  1224. caps_cmd->op_to_write =
  1225. htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  1226. FW_CMD_REQUEST |
  1227. FW_CMD_WRITE);
  1228. caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
  1229. if (csio_mb_issue(hw, mbp)) {
  1230. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
  1231. goto out;
  1232. }
  1233. ret = csio_mb_fw_retval(mbp);
  1234. if (ret != FW_SUCCESS) {
  1235. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1236. goto out;
  1237. }
  1238. rv = 0;
  1239. out:
  1240. mempool_free(mbp, hw->mb_mempool);
  1241. return rv;
  1242. }
  1243. /*
  1244. * csio_get_device_params - Get device parameters.
  1245. * @hw: HW module
  1246. *
  1247. */
  1248. static int
  1249. csio_get_device_params(struct csio_hw *hw)
  1250. {
  1251. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1252. struct csio_mb *mbp;
  1253. enum fw_retval retval;
  1254. u32 param[6];
  1255. int i, j = 0;
  1256. /* Initialize portids to -1 */
  1257. for (i = 0; i < CSIO_MAX_PPORTS; i++)
  1258. hw->pport[i].portid = -1;
  1259. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1260. if (!mbp) {
  1261. CSIO_INC_STATS(hw, n_err_nomem);
  1262. return -ENOMEM;
  1263. }
  1264. /* Get port vec information. */
  1265. param[0] = FW_PARAM_DEV(PORTVEC);
  1266. /* Get Core clock. */
  1267. param[1] = FW_PARAM_DEV(CCLK);
  1268. /* Get EQ id start and end. */
  1269. param[2] = FW_PARAM_PFVF(EQ_START);
  1270. param[3] = FW_PARAM_PFVF(EQ_END);
  1271. /* Get IQ id start and end. */
  1272. param[4] = FW_PARAM_PFVF(IQFLINT_START);
  1273. param[5] = FW_PARAM_PFVF(IQFLINT_END);
  1274. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1275. ARRAY_SIZE(param), param, NULL, false, NULL);
  1276. if (csio_mb_issue(hw, mbp)) {
  1277. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1278. mempool_free(mbp, hw->mb_mempool);
  1279. return -EINVAL;
  1280. }
  1281. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1282. ARRAY_SIZE(param), param);
  1283. if (retval != FW_SUCCESS) {
  1284. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1285. retval);
  1286. mempool_free(mbp, hw->mb_mempool);
  1287. return -EINVAL;
  1288. }
  1289. /* cache the information. */
  1290. hw->port_vec = param[0];
  1291. hw->vpd.cclk = param[1];
  1292. wrm->fw_eq_start = param[2];
  1293. wrm->fw_iq_start = param[4];
  1294. /* Using FW configured max iqs & eqs */
  1295. if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
  1296. !csio_is_hw_master(hw)) {
  1297. hw->cfg_niq = param[5] - param[4] + 1;
  1298. hw->cfg_neq = param[3] - param[2] + 1;
  1299. csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
  1300. hw->cfg_niq, hw->cfg_neq);
  1301. }
  1302. hw->port_vec &= csio_port_mask;
  1303. hw->num_pports = hweight32(hw->port_vec);
  1304. csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
  1305. hw->port_vec, hw->num_pports);
  1306. for (i = 0; i < hw->num_pports; i++) {
  1307. while ((hw->port_vec & (1 << j)) == 0)
  1308. j++;
  1309. hw->pport[i].portid = j++;
  1310. csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
  1311. }
  1312. mempool_free(mbp, hw->mb_mempool);
  1313. return 0;
  1314. }
  1315. /*
  1316. * csio_config_device_caps - Get and set device capabilities.
  1317. * @hw: HW module
  1318. *
  1319. */
  1320. static int
  1321. csio_config_device_caps(struct csio_hw *hw)
  1322. {
  1323. struct csio_mb *mbp;
  1324. enum fw_retval retval;
  1325. int rv = -EINVAL;
  1326. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1327. if (!mbp) {
  1328. CSIO_INC_STATS(hw, n_err_nomem);
  1329. return -ENOMEM;
  1330. }
  1331. /* Get device capabilities */
  1332. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
  1333. if (csio_mb_issue(hw, mbp)) {
  1334. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
  1335. goto out;
  1336. }
  1337. retval = csio_mb_fw_retval(mbp);
  1338. if (retval != FW_SUCCESS) {
  1339. csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
  1340. goto out;
  1341. }
  1342. /* Validate device capabilities */
  1343. if (csio_hw_validate_caps(hw, mbp))
  1344. goto out;
  1345. /* Don't config device capabilities if already configured */
  1346. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  1347. rv = 0;
  1348. goto out;
  1349. }
  1350. /* Write back desired device capabilities */
  1351. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
  1352. false, true, NULL);
  1353. if (csio_mb_issue(hw, mbp)) {
  1354. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
  1355. goto out;
  1356. }
  1357. retval = csio_mb_fw_retval(mbp);
  1358. if (retval != FW_SUCCESS) {
  1359. csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
  1360. goto out;
  1361. }
  1362. rv = 0;
  1363. out:
  1364. mempool_free(mbp, hw->mb_mempool);
  1365. return rv;
  1366. }
  1367. /*
  1368. * csio_enable_ports - Bring up all available ports.
  1369. * @hw: HW module.
  1370. *
  1371. */
  1372. static int
  1373. csio_enable_ports(struct csio_hw *hw)
  1374. {
  1375. struct csio_mb *mbp;
  1376. enum fw_retval retval;
  1377. uint8_t portid;
  1378. int i;
  1379. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1380. if (!mbp) {
  1381. CSIO_INC_STATS(hw, n_err_nomem);
  1382. return -ENOMEM;
  1383. }
  1384. for (i = 0; i < hw->num_pports; i++) {
  1385. portid = hw->pport[i].portid;
  1386. /* Read PORT information */
  1387. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
  1388. false, 0, 0, NULL);
  1389. if (csio_mb_issue(hw, mbp)) {
  1390. csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
  1391. portid);
  1392. mempool_free(mbp, hw->mb_mempool);
  1393. return -EINVAL;
  1394. }
  1395. csio_mb_process_read_port_rsp(hw, mbp, &retval,
  1396. &hw->pport[i].pcap);
  1397. if (retval != FW_SUCCESS) {
  1398. csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
  1399. portid, retval);
  1400. mempool_free(mbp, hw->mb_mempool);
  1401. return -EINVAL;
  1402. }
  1403. /* Write back PORT information */
  1404. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid, true,
  1405. (PAUSE_RX | PAUSE_TX), hw->pport[i].pcap, NULL);
  1406. if (csio_mb_issue(hw, mbp)) {
  1407. csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
  1408. portid);
  1409. mempool_free(mbp, hw->mb_mempool);
  1410. return -EINVAL;
  1411. }
  1412. retval = csio_mb_fw_retval(mbp);
  1413. if (retval != FW_SUCCESS) {
  1414. csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
  1415. portid, retval);
  1416. mempool_free(mbp, hw->mb_mempool);
  1417. return -EINVAL;
  1418. }
  1419. } /* For all ports */
  1420. mempool_free(mbp, hw->mb_mempool);
  1421. return 0;
  1422. }
  1423. /*
  1424. * csio_get_fcoe_resinfo - Read fcoe fw resource info.
  1425. * @hw: HW module
  1426. * Issued with lock held.
  1427. */
  1428. static int
  1429. csio_get_fcoe_resinfo(struct csio_hw *hw)
  1430. {
  1431. struct csio_fcoe_res_info *res_info = &hw->fres_info;
  1432. struct fw_fcoe_res_info_cmd *rsp;
  1433. struct csio_mb *mbp;
  1434. enum fw_retval retval;
  1435. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1436. if (!mbp) {
  1437. CSIO_INC_STATS(hw, n_err_nomem);
  1438. return -ENOMEM;
  1439. }
  1440. /* Get FCoE FW resource information */
  1441. csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1442. if (csio_mb_issue(hw, mbp)) {
  1443. csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
  1444. mempool_free(mbp, hw->mb_mempool);
  1445. return -EINVAL;
  1446. }
  1447. rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
  1448. retval = FW_CMD_RETVAL_GET(ntohl(rsp->retval_len16));
  1449. if (retval != FW_SUCCESS) {
  1450. csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
  1451. retval);
  1452. mempool_free(mbp, hw->mb_mempool);
  1453. return -EINVAL;
  1454. }
  1455. res_info->e_d_tov = ntohs(rsp->e_d_tov);
  1456. res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
  1457. res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
  1458. res_info->r_r_tov = ntohs(rsp->r_r_tov);
  1459. res_info->max_xchgs = ntohl(rsp->max_xchgs);
  1460. res_info->max_ssns = ntohl(rsp->max_ssns);
  1461. res_info->used_xchgs = ntohl(rsp->used_xchgs);
  1462. res_info->used_ssns = ntohl(rsp->used_ssns);
  1463. res_info->max_fcfs = ntohl(rsp->max_fcfs);
  1464. res_info->max_vnps = ntohl(rsp->max_vnps);
  1465. res_info->used_fcfs = ntohl(rsp->used_fcfs);
  1466. res_info->used_vnps = ntohl(rsp->used_vnps);
  1467. csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
  1468. res_info->max_xchgs);
  1469. mempool_free(mbp, hw->mb_mempool);
  1470. return 0;
  1471. }
  1472. static int
  1473. csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
  1474. {
  1475. struct csio_mb *mbp;
  1476. enum fw_retval retval;
  1477. u32 _param[1];
  1478. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1479. if (!mbp) {
  1480. CSIO_INC_STATS(hw, n_err_nomem);
  1481. return -ENOMEM;
  1482. }
  1483. /*
  1484. * Find out whether we're dealing with a version of
  1485. * the firmware which has configuration file support.
  1486. */
  1487. _param[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  1488. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CF));
  1489. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1490. ARRAY_SIZE(_param), _param, NULL, false, NULL);
  1491. if (csio_mb_issue(hw, mbp)) {
  1492. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1493. mempool_free(mbp, hw->mb_mempool);
  1494. return -EINVAL;
  1495. }
  1496. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1497. ARRAY_SIZE(_param), _param);
  1498. if (retval != FW_SUCCESS) {
  1499. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1500. retval);
  1501. mempool_free(mbp, hw->mb_mempool);
  1502. return -EINVAL;
  1503. }
  1504. mempool_free(mbp, hw->mb_mempool);
  1505. *param = _param[0];
  1506. return 0;
  1507. }
  1508. static int
  1509. csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
  1510. {
  1511. int ret = 0;
  1512. const struct firmware *cf;
  1513. struct pci_dev *pci_dev = hw->pdev;
  1514. struct device *dev = &pci_dev->dev;
  1515. unsigned int mtype = 0, maddr = 0;
  1516. uint32_t *cfg_data;
  1517. int value_to_add = 0;
  1518. if (request_firmware(&cf, CSIO_CF_FNAME(hw), dev) < 0) {
  1519. csio_err(hw, "could not find config file %s, err: %d\n",
  1520. CSIO_CF_FNAME(hw), ret);
  1521. return -ENOENT;
  1522. }
  1523. if (cf->size%4 != 0)
  1524. value_to_add = 4 - (cf->size % 4);
  1525. cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
  1526. if (cfg_data == NULL) {
  1527. ret = -ENOMEM;
  1528. goto leave;
  1529. }
  1530. memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
  1531. if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
  1532. ret = -EINVAL;
  1533. goto leave;
  1534. }
  1535. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1536. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1537. ret = csio_memory_write(hw, mtype, maddr,
  1538. cf->size + value_to_add, cfg_data);
  1539. if ((ret == 0) && (value_to_add != 0)) {
  1540. union {
  1541. u32 word;
  1542. char buf[4];
  1543. } last;
  1544. size_t size = cf->size & ~0x3;
  1545. int i;
  1546. last.word = cfg_data[size >> 2];
  1547. for (i = value_to_add; i < 4; i++)
  1548. last.buf[i] = 0;
  1549. ret = csio_memory_write(hw, mtype, maddr + size, 4, &last.word);
  1550. }
  1551. if (ret == 0) {
  1552. csio_info(hw, "config file upgraded to %s\n",
  1553. CSIO_CF_FNAME(hw));
  1554. snprintf(path, 64, "%s%s", "/lib/firmware/", CSIO_CF_FNAME(hw));
  1555. }
  1556. leave:
  1557. kfree(cfg_data);
  1558. release_firmware(cf);
  1559. return ret;
  1560. }
  1561. /*
  1562. * HW initialization: contact FW, obtain config, perform basic init.
  1563. *
  1564. * If the firmware we're dealing with has Configuration File support, then
  1565. * we use that to perform all configuration -- either using the configuration
  1566. * file stored in flash on the adapter or using a filesystem-local file
  1567. * if available.
  1568. *
  1569. * If we don't have configuration file support in the firmware, then we'll
  1570. * have to set things up the old fashioned way with hard-coded register
  1571. * writes and firmware commands ...
  1572. */
  1573. /*
  1574. * Attempt to initialize the HW via a Firmware Configuration File.
  1575. */
  1576. static int
  1577. csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
  1578. {
  1579. unsigned int mtype, maddr;
  1580. int rv;
  1581. uint32_t finiver = 0, finicsum = 0, cfcsum = 0;
  1582. int using_flash;
  1583. char path[64];
  1584. /*
  1585. * Reset device if necessary
  1586. */
  1587. if (reset) {
  1588. rv = csio_do_reset(hw, true);
  1589. if (rv != 0)
  1590. goto bye;
  1591. }
  1592. /*
  1593. * If we have a configuration file in host ,
  1594. * then use that. Otherwise, use the configuration file stored
  1595. * in the HW flash ...
  1596. */
  1597. spin_unlock_irq(&hw->lock);
  1598. rv = csio_hw_flash_config(hw, fw_cfg_param, path);
  1599. spin_lock_irq(&hw->lock);
  1600. if (rv != 0) {
  1601. if (rv == -ENOENT) {
  1602. /*
  1603. * config file was not found. Use default
  1604. * config file from flash.
  1605. */
  1606. mtype = FW_MEMTYPE_CF_FLASH;
  1607. maddr = hw->chip_ops->chip_flash_cfg_addr(hw);
  1608. using_flash = 1;
  1609. } else {
  1610. /*
  1611. * we revert back to the hardwired config if
  1612. * flashing failed.
  1613. */
  1614. goto bye;
  1615. }
  1616. } else {
  1617. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1618. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1619. using_flash = 0;
  1620. }
  1621. hw->cfg_store = (uint8_t)mtype;
  1622. /*
  1623. * Issue a Capability Configuration command to the firmware to get it
  1624. * to parse the Configuration File.
  1625. */
  1626. rv = csio_hw_fw_config_file(hw, mtype, maddr, &finiver,
  1627. &finicsum, &cfcsum);
  1628. if (rv != 0)
  1629. goto bye;
  1630. hw->cfg_finiver = finiver;
  1631. hw->cfg_finicsum = finicsum;
  1632. hw->cfg_cfcsum = cfcsum;
  1633. hw->cfg_csum_status = true;
  1634. if (finicsum != cfcsum) {
  1635. csio_warn(hw,
  1636. "Config File checksum mismatch: csum=%#x, computed=%#x\n",
  1637. finicsum, cfcsum);
  1638. hw->cfg_csum_status = false;
  1639. }
  1640. /*
  1641. * Note that we're operating with parameters
  1642. * not supplied by the driver, rather than from hard-wired
  1643. * initialization constants buried in the driver.
  1644. */
  1645. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1646. /* device parameters */
  1647. rv = csio_get_device_params(hw);
  1648. if (rv != 0)
  1649. goto bye;
  1650. /* Configure SGE */
  1651. csio_wr_sge_init(hw);
  1652. /*
  1653. * And finally tell the firmware to initialize itself using the
  1654. * parameters from the Configuration File.
  1655. */
  1656. /* Post event to notify completion of configuration */
  1657. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1658. csio_info(hw,
  1659. "Firmware Configuration File %s, version %#x, computed checksum %#x\n",
  1660. (using_flash ? "in device FLASH" : path), finiver, cfcsum);
  1661. return 0;
  1662. /*
  1663. * Something bad happened. Return the error ...
  1664. */
  1665. bye:
  1666. hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
  1667. csio_dbg(hw, "Configuration file error %d\n", rv);
  1668. return rv;
  1669. }
  1670. /*
  1671. * Attempt to initialize the adapter via hard-coded, driver supplied
  1672. * parameters ...
  1673. */
  1674. static int
  1675. csio_hw_no_fwconfig(struct csio_hw *hw, int reset)
  1676. {
  1677. int rv;
  1678. /*
  1679. * Reset device if necessary
  1680. */
  1681. if (reset) {
  1682. rv = csio_do_reset(hw, true);
  1683. if (rv != 0)
  1684. goto out;
  1685. }
  1686. /* Get and set device capabilities */
  1687. rv = csio_config_device_caps(hw);
  1688. if (rv != 0)
  1689. goto out;
  1690. /* device parameters */
  1691. rv = csio_get_device_params(hw);
  1692. if (rv != 0)
  1693. goto out;
  1694. /* Configure SGE */
  1695. csio_wr_sge_init(hw);
  1696. /* Post event to notify completion of configuration */
  1697. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1698. out:
  1699. return rv;
  1700. }
  1701. /*
  1702. * Returns -EINVAL if attempts to flash the firmware failed
  1703. * else returns 0,
  1704. * if flashing was not attempted because the card had the
  1705. * latest firmware ECANCELED is returned
  1706. */
  1707. static int
  1708. csio_hw_flash_fw(struct csio_hw *hw)
  1709. {
  1710. int ret = -ECANCELED;
  1711. const struct firmware *fw;
  1712. const struct fw_hdr *hdr;
  1713. u32 fw_ver;
  1714. struct pci_dev *pci_dev = hw->pdev;
  1715. struct device *dev = &pci_dev->dev ;
  1716. if (request_firmware(&fw, CSIO_FW_FNAME(hw), dev) < 0) {
  1717. csio_err(hw, "could not find firmware image %s, err: %d\n",
  1718. CSIO_FW_FNAME(hw), ret);
  1719. return -EINVAL;
  1720. }
  1721. hdr = (const struct fw_hdr *)fw->data;
  1722. fw_ver = ntohl(hdr->fw_ver);
  1723. if (FW_HDR_FW_VER_MAJOR_GET(fw_ver) != FW_VERSION_MAJOR(hw))
  1724. return -EINVAL; /* wrong major version, won't do */
  1725. /*
  1726. * If the flash FW is unusable or we found something newer, load it.
  1727. */
  1728. if (FW_HDR_FW_VER_MAJOR_GET(hw->fwrev) != FW_VERSION_MAJOR(hw) ||
  1729. fw_ver > hw->fwrev) {
  1730. ret = csio_hw_fw_upgrade(hw, hw->pfn, fw->data, fw->size,
  1731. /*force=*/false);
  1732. if (!ret)
  1733. csio_info(hw,
  1734. "firmware upgraded to version %pI4 from %s\n",
  1735. &hdr->fw_ver, CSIO_FW_FNAME(hw));
  1736. else
  1737. csio_err(hw, "firmware upgrade failed! err=%d\n", ret);
  1738. } else
  1739. ret = -EINVAL;
  1740. release_firmware(fw);
  1741. return ret;
  1742. }
  1743. /*
  1744. * csio_hw_configure - Configure HW
  1745. * @hw - HW module
  1746. *
  1747. */
  1748. static void
  1749. csio_hw_configure(struct csio_hw *hw)
  1750. {
  1751. int reset = 1;
  1752. int rv;
  1753. u32 param[1];
  1754. rv = csio_hw_dev_ready(hw);
  1755. if (rv != 0) {
  1756. CSIO_INC_STATS(hw, n_err_fatal);
  1757. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  1758. goto out;
  1759. }
  1760. /* HW version */
  1761. hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV);
  1762. /* Needed for FW download */
  1763. rv = csio_hw_get_flash_params(hw);
  1764. if (rv != 0) {
  1765. csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
  1766. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  1767. goto out;
  1768. }
  1769. /* Set PCIe completion timeout to 4 seconds */
  1770. if (pci_is_pcie(hw->pdev))
  1771. pcie_capability_clear_and_set_word(hw->pdev, PCI_EXP_DEVCTL2,
  1772. PCI_EXP_DEVCTL2_COMP_TIMEOUT, 0xd);
  1773. hw->chip_ops->chip_set_mem_win(hw, MEMWIN_CSIOSTOR);
  1774. rv = csio_hw_get_fw_version(hw, &hw->fwrev);
  1775. if (rv != 0)
  1776. goto out;
  1777. csio_hw_print_fw_version(hw, "Firmware revision");
  1778. rv = csio_do_hello(hw, &hw->fw_state);
  1779. if (rv != 0) {
  1780. CSIO_INC_STATS(hw, n_err_fatal);
  1781. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  1782. goto out;
  1783. }
  1784. /* Read vpd */
  1785. rv = csio_hw_get_vpd_params(hw, &hw->vpd);
  1786. if (rv != 0)
  1787. goto out;
  1788. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  1789. rv = csio_hw_check_fw_version(hw);
  1790. if (rv == -EINVAL) {
  1791. /* Do firmware update */
  1792. spin_unlock_irq(&hw->lock);
  1793. rv = csio_hw_flash_fw(hw);
  1794. spin_lock_irq(&hw->lock);
  1795. if (rv == 0) {
  1796. reset = 0;
  1797. /*
  1798. * Note that the chip was reset as part of the
  1799. * firmware upgrade so we don't reset it again
  1800. * below and grab the new firmware version.
  1801. */
  1802. rv = csio_hw_check_fw_version(hw);
  1803. }
  1804. }
  1805. /*
  1806. * If the firmware doesn't support Configuration
  1807. * Files, use the old Driver-based, hard-wired
  1808. * initialization. Otherwise, try using the
  1809. * Configuration File support and fall back to the
  1810. * Driver-based initialization if there's no
  1811. * Configuration File found.
  1812. */
  1813. if (csio_hw_check_fwconfig(hw, param) == 0) {
  1814. rv = csio_hw_use_fwconfig(hw, reset, param);
  1815. if (rv == -ENOENT)
  1816. goto out;
  1817. if (rv != 0) {
  1818. csio_info(hw,
  1819. "No Configuration File present "
  1820. "on adapter. Using hard-wired "
  1821. "configuration parameters.\n");
  1822. rv = csio_hw_no_fwconfig(hw, reset);
  1823. }
  1824. } else {
  1825. rv = csio_hw_no_fwconfig(hw, reset);
  1826. }
  1827. if (rv != 0)
  1828. goto out;
  1829. } else {
  1830. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  1831. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1832. /* device parameters */
  1833. rv = csio_get_device_params(hw);
  1834. if (rv != 0)
  1835. goto out;
  1836. /* Get device capabilities */
  1837. rv = csio_config_device_caps(hw);
  1838. if (rv != 0)
  1839. goto out;
  1840. /* Configure SGE */
  1841. csio_wr_sge_init(hw);
  1842. /* Post event to notify completion of configuration */
  1843. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1844. goto out;
  1845. }
  1846. } /* if not master */
  1847. out:
  1848. return;
  1849. }
  1850. /*
  1851. * csio_hw_initialize - Initialize HW
  1852. * @hw - HW module
  1853. *
  1854. */
  1855. static void
  1856. csio_hw_initialize(struct csio_hw *hw)
  1857. {
  1858. struct csio_mb *mbp;
  1859. enum fw_retval retval;
  1860. int rv;
  1861. int i;
  1862. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  1863. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1864. if (!mbp)
  1865. goto out;
  1866. csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1867. if (csio_mb_issue(hw, mbp)) {
  1868. csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
  1869. goto free_and_out;
  1870. }
  1871. retval = csio_mb_fw_retval(mbp);
  1872. if (retval != FW_SUCCESS) {
  1873. csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
  1874. retval);
  1875. goto free_and_out;
  1876. }
  1877. mempool_free(mbp, hw->mb_mempool);
  1878. }
  1879. rv = csio_get_fcoe_resinfo(hw);
  1880. if (rv != 0) {
  1881. csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
  1882. goto out;
  1883. }
  1884. spin_unlock_irq(&hw->lock);
  1885. rv = csio_config_queues(hw);
  1886. spin_lock_irq(&hw->lock);
  1887. if (rv != 0) {
  1888. csio_err(hw, "Config of queues failed!: %d\n", rv);
  1889. goto out;
  1890. }
  1891. for (i = 0; i < hw->num_pports; i++)
  1892. hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
  1893. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  1894. rv = csio_enable_ports(hw);
  1895. if (rv != 0) {
  1896. csio_err(hw, "Failed to enable ports: %d\n", rv);
  1897. goto out;
  1898. }
  1899. }
  1900. csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
  1901. return;
  1902. free_and_out:
  1903. mempool_free(mbp, hw->mb_mempool);
  1904. out:
  1905. return;
  1906. }
  1907. #define PF_INTR_MASK (PFSW | PFCIM)
  1908. /*
  1909. * csio_hw_intr_enable - Enable HW interrupts
  1910. * @hw: Pointer to HW module.
  1911. *
  1912. * Enable interrupts in HW registers.
  1913. */
  1914. static void
  1915. csio_hw_intr_enable(struct csio_hw *hw)
  1916. {
  1917. uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
  1918. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  1919. uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE);
  1920. /*
  1921. * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
  1922. * by FW, so do nothing for INTX.
  1923. */
  1924. if (hw->intr_mode == CSIO_IM_MSIX)
  1925. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  1926. AIVEC(AIVEC_MASK), vec);
  1927. else if (hw->intr_mode == CSIO_IM_MSI)
  1928. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  1929. AIVEC(AIVEC_MASK), 0);
  1930. csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE));
  1931. /* Turn on MB interrupts - this will internally flush PIO as well */
  1932. csio_mb_intr_enable(hw);
  1933. /* These are common registers - only a master can modify them */
  1934. if (csio_is_hw_master(hw)) {
  1935. /*
  1936. * Disable the Serial FLASH interrupt, if enabled!
  1937. */
  1938. pl &= (~SF);
  1939. csio_wr_reg32(hw, pl, PL_INT_ENABLE);
  1940. csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE |
  1941. EGRESS_SIZE_ERR | ERR_INVALID_CIDX_INC |
  1942. ERR_CPL_OPCODE_0 | ERR_DROPPED_DB |
  1943. ERR_DATA_CPL_ON_HIGH_QID1 |
  1944. ERR_DATA_CPL_ON_HIGH_QID0 | ERR_BAD_DB_PIDX3 |
  1945. ERR_BAD_DB_PIDX2 | ERR_BAD_DB_PIDX1 |
  1946. ERR_BAD_DB_PIDX0 | ERR_ING_CTXT_PRIO |
  1947. ERR_EGR_CTXT_PRIO | INGRESS_SIZE_ERR,
  1948. SGE_INT_ENABLE3);
  1949. csio_set_reg_field(hw, PL_INT_MAP0, 0, 1 << pf);
  1950. }
  1951. hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
  1952. }
  1953. /*
  1954. * csio_hw_intr_disable - Disable HW interrupts
  1955. * @hw: Pointer to HW module.
  1956. *
  1957. * Turn off Mailbox and PCI_PF_CFG interrupts.
  1958. */
  1959. void
  1960. csio_hw_intr_disable(struct csio_hw *hw)
  1961. {
  1962. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  1963. if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
  1964. return;
  1965. hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
  1966. csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE));
  1967. if (csio_is_hw_master(hw))
  1968. csio_set_reg_field(hw, PL_INT_MAP0, 1 << pf, 0);
  1969. /* Turn off MB interrupts */
  1970. csio_mb_intr_disable(hw);
  1971. }
  1972. void
  1973. csio_hw_fatal_err(struct csio_hw *hw)
  1974. {
  1975. csio_set_reg_field(hw, SGE_CONTROL, GLOBALENABLE, 0);
  1976. csio_hw_intr_disable(hw);
  1977. /* Do not reset HW, we may need FW state for debugging */
  1978. csio_fatal(hw, "HW Fatal error encountered!\n");
  1979. }
  1980. /*****************************************************************************/
  1981. /* START: HW SM */
  1982. /*****************************************************************************/
  1983. /*
  1984. * csio_hws_uninit - Uninit state
  1985. * @hw - HW module
  1986. * @evt - Event
  1987. *
  1988. */
  1989. static void
  1990. csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
  1991. {
  1992. hw->prev_evt = hw->cur_evt;
  1993. hw->cur_evt = evt;
  1994. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  1995. switch (evt) {
  1996. case CSIO_HWE_CFG:
  1997. csio_set_state(&hw->sm, csio_hws_configuring);
  1998. csio_hw_configure(hw);
  1999. break;
  2000. default:
  2001. CSIO_INC_STATS(hw, n_evt_unexp);
  2002. break;
  2003. }
  2004. }
  2005. /*
  2006. * csio_hws_configuring - Configuring state
  2007. * @hw - HW module
  2008. * @evt - Event
  2009. *
  2010. */
  2011. static void
  2012. csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
  2013. {
  2014. hw->prev_evt = hw->cur_evt;
  2015. hw->cur_evt = evt;
  2016. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2017. switch (evt) {
  2018. case CSIO_HWE_INIT:
  2019. csio_set_state(&hw->sm, csio_hws_initializing);
  2020. csio_hw_initialize(hw);
  2021. break;
  2022. case CSIO_HWE_INIT_DONE:
  2023. csio_set_state(&hw->sm, csio_hws_ready);
  2024. /* Fan out event to all lnode SMs */
  2025. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2026. break;
  2027. case CSIO_HWE_FATAL:
  2028. csio_set_state(&hw->sm, csio_hws_uninit);
  2029. break;
  2030. case CSIO_HWE_PCI_REMOVE:
  2031. csio_do_bye(hw);
  2032. break;
  2033. default:
  2034. CSIO_INC_STATS(hw, n_evt_unexp);
  2035. break;
  2036. }
  2037. }
  2038. /*
  2039. * csio_hws_initializing - Initialiazing state
  2040. * @hw - HW module
  2041. * @evt - Event
  2042. *
  2043. */
  2044. static void
  2045. csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
  2046. {
  2047. hw->prev_evt = hw->cur_evt;
  2048. hw->cur_evt = evt;
  2049. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2050. switch (evt) {
  2051. case CSIO_HWE_INIT_DONE:
  2052. csio_set_state(&hw->sm, csio_hws_ready);
  2053. /* Fan out event to all lnode SMs */
  2054. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2055. /* Enable interrupts */
  2056. csio_hw_intr_enable(hw);
  2057. break;
  2058. case CSIO_HWE_FATAL:
  2059. csio_set_state(&hw->sm, csio_hws_uninit);
  2060. break;
  2061. case CSIO_HWE_PCI_REMOVE:
  2062. csio_do_bye(hw);
  2063. break;
  2064. default:
  2065. CSIO_INC_STATS(hw, n_evt_unexp);
  2066. break;
  2067. }
  2068. }
  2069. /*
  2070. * csio_hws_ready - Ready state
  2071. * @hw - HW module
  2072. * @evt - Event
  2073. *
  2074. */
  2075. static void
  2076. csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
  2077. {
  2078. /* Remember the event */
  2079. hw->evtflag = evt;
  2080. hw->prev_evt = hw->cur_evt;
  2081. hw->cur_evt = evt;
  2082. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2083. switch (evt) {
  2084. case CSIO_HWE_HBA_RESET:
  2085. case CSIO_HWE_FW_DLOAD:
  2086. case CSIO_HWE_SUSPEND:
  2087. case CSIO_HWE_PCI_REMOVE:
  2088. case CSIO_HWE_PCIERR_DETECTED:
  2089. csio_set_state(&hw->sm, csio_hws_quiescing);
  2090. /* cleanup all outstanding cmds */
  2091. if (evt == CSIO_HWE_HBA_RESET ||
  2092. evt == CSIO_HWE_PCIERR_DETECTED)
  2093. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
  2094. else
  2095. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
  2096. csio_hw_intr_disable(hw);
  2097. csio_hw_mbm_cleanup(hw);
  2098. csio_evtq_stop(hw);
  2099. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
  2100. csio_evtq_flush(hw);
  2101. csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
  2102. csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
  2103. break;
  2104. case CSIO_HWE_FATAL:
  2105. csio_set_state(&hw->sm, csio_hws_uninit);
  2106. break;
  2107. default:
  2108. CSIO_INC_STATS(hw, n_evt_unexp);
  2109. break;
  2110. }
  2111. }
  2112. /*
  2113. * csio_hws_quiescing - Quiescing state
  2114. * @hw - HW module
  2115. * @evt - Event
  2116. *
  2117. */
  2118. static void
  2119. csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
  2120. {
  2121. hw->prev_evt = hw->cur_evt;
  2122. hw->cur_evt = evt;
  2123. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2124. switch (evt) {
  2125. case CSIO_HWE_QUIESCED:
  2126. switch (hw->evtflag) {
  2127. case CSIO_HWE_FW_DLOAD:
  2128. csio_set_state(&hw->sm, csio_hws_resetting);
  2129. /* Download firmware */
  2130. /* Fall through */
  2131. case CSIO_HWE_HBA_RESET:
  2132. csio_set_state(&hw->sm, csio_hws_resetting);
  2133. /* Start reset of the HBA */
  2134. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
  2135. csio_wr_destroy_queues(hw, false);
  2136. csio_do_reset(hw, false);
  2137. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
  2138. break;
  2139. case CSIO_HWE_PCI_REMOVE:
  2140. csio_set_state(&hw->sm, csio_hws_removing);
  2141. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
  2142. csio_wr_destroy_queues(hw, true);
  2143. /* Now send the bye command */
  2144. csio_do_bye(hw);
  2145. break;
  2146. case CSIO_HWE_SUSPEND:
  2147. csio_set_state(&hw->sm, csio_hws_quiesced);
  2148. break;
  2149. case CSIO_HWE_PCIERR_DETECTED:
  2150. csio_set_state(&hw->sm, csio_hws_pcierr);
  2151. csio_wr_destroy_queues(hw, false);
  2152. break;
  2153. default:
  2154. CSIO_INC_STATS(hw, n_evt_unexp);
  2155. break;
  2156. }
  2157. break;
  2158. default:
  2159. CSIO_INC_STATS(hw, n_evt_unexp);
  2160. break;
  2161. }
  2162. }
  2163. /*
  2164. * csio_hws_quiesced - Quiesced state
  2165. * @hw - HW module
  2166. * @evt - Event
  2167. *
  2168. */
  2169. static void
  2170. csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
  2171. {
  2172. hw->prev_evt = hw->cur_evt;
  2173. hw->cur_evt = evt;
  2174. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2175. switch (evt) {
  2176. case CSIO_HWE_RESUME:
  2177. csio_set_state(&hw->sm, csio_hws_configuring);
  2178. csio_hw_configure(hw);
  2179. break;
  2180. default:
  2181. CSIO_INC_STATS(hw, n_evt_unexp);
  2182. break;
  2183. }
  2184. }
  2185. /*
  2186. * csio_hws_resetting - HW Resetting state
  2187. * @hw - HW module
  2188. * @evt - Event
  2189. *
  2190. */
  2191. static void
  2192. csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
  2193. {
  2194. hw->prev_evt = hw->cur_evt;
  2195. hw->cur_evt = evt;
  2196. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2197. switch (evt) {
  2198. case CSIO_HWE_HBA_RESET_DONE:
  2199. csio_evtq_start(hw);
  2200. csio_set_state(&hw->sm, csio_hws_configuring);
  2201. csio_hw_configure(hw);
  2202. break;
  2203. default:
  2204. CSIO_INC_STATS(hw, n_evt_unexp);
  2205. break;
  2206. }
  2207. }
  2208. /*
  2209. * csio_hws_removing - PCI Hotplug removing state
  2210. * @hw - HW module
  2211. * @evt - Event
  2212. *
  2213. */
  2214. static void
  2215. csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
  2216. {
  2217. hw->prev_evt = hw->cur_evt;
  2218. hw->cur_evt = evt;
  2219. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2220. switch (evt) {
  2221. case CSIO_HWE_HBA_RESET:
  2222. if (!csio_is_hw_master(hw))
  2223. break;
  2224. /*
  2225. * The BYE should have alerady been issued, so we cant
  2226. * use the mailbox interface. Hence we use the PL_RST
  2227. * register directly.
  2228. */
  2229. csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
  2230. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  2231. mdelay(2000);
  2232. break;
  2233. /* Should never receive any new events */
  2234. default:
  2235. CSIO_INC_STATS(hw, n_evt_unexp);
  2236. break;
  2237. }
  2238. }
  2239. /*
  2240. * csio_hws_pcierr - PCI Error state
  2241. * @hw - HW module
  2242. * @evt - Event
  2243. *
  2244. */
  2245. static void
  2246. csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
  2247. {
  2248. hw->prev_evt = hw->cur_evt;
  2249. hw->cur_evt = evt;
  2250. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2251. switch (evt) {
  2252. case CSIO_HWE_PCIERR_SLOT_RESET:
  2253. csio_evtq_start(hw);
  2254. csio_set_state(&hw->sm, csio_hws_configuring);
  2255. csio_hw_configure(hw);
  2256. break;
  2257. default:
  2258. CSIO_INC_STATS(hw, n_evt_unexp);
  2259. break;
  2260. }
  2261. }
  2262. /*****************************************************************************/
  2263. /* END: HW SM */
  2264. /*****************************************************************************/
  2265. /*
  2266. * csio_handle_intr_status - table driven interrupt handler
  2267. * @hw: HW instance
  2268. * @reg: the interrupt status register to process
  2269. * @acts: table of interrupt actions
  2270. *
  2271. * A table driven interrupt handler that applies a set of masks to an
  2272. * interrupt status word and performs the corresponding actions if the
  2273. * interrupts described by the mask have occured. The actions include
  2274. * optionally emitting a warning or alert message. The table is terminated
  2275. * by an entry specifying mask 0. Returns the number of fatal interrupt
  2276. * conditions.
  2277. */
  2278. int
  2279. csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
  2280. const struct intr_info *acts)
  2281. {
  2282. int fatal = 0;
  2283. unsigned int mask = 0;
  2284. unsigned int status = csio_rd_reg32(hw, reg);
  2285. for ( ; acts->mask; ++acts) {
  2286. if (!(status & acts->mask))
  2287. continue;
  2288. if (acts->fatal) {
  2289. fatal++;
  2290. csio_fatal(hw, "Fatal %s (0x%x)\n",
  2291. acts->msg, status & acts->mask);
  2292. } else if (acts->msg)
  2293. csio_info(hw, "%s (0x%x)\n",
  2294. acts->msg, status & acts->mask);
  2295. mask |= acts->mask;
  2296. }
  2297. status &= mask;
  2298. if (status) /* clear processed interrupts */
  2299. csio_wr_reg32(hw, status, reg);
  2300. return fatal;
  2301. }
  2302. /*
  2303. * TP interrupt handler.
  2304. */
  2305. static void csio_tp_intr_handler(struct csio_hw *hw)
  2306. {
  2307. static struct intr_info tp_intr_info[] = {
  2308. { 0x3fffffff, "TP parity error", -1, 1 },
  2309. { FLMTXFLSTEMPTY, "TP out of Tx pages", -1, 1 },
  2310. { 0, NULL, 0, 0 }
  2311. };
  2312. if (csio_handle_intr_status(hw, TP_INT_CAUSE, tp_intr_info))
  2313. csio_hw_fatal_err(hw);
  2314. }
  2315. /*
  2316. * SGE interrupt handler.
  2317. */
  2318. static void csio_sge_intr_handler(struct csio_hw *hw)
  2319. {
  2320. uint64_t v;
  2321. static struct intr_info sge_intr_info[] = {
  2322. { ERR_CPL_EXCEED_IQE_SIZE,
  2323. "SGE received CPL exceeding IQE size", -1, 1 },
  2324. { ERR_INVALID_CIDX_INC,
  2325. "SGE GTS CIDX increment too large", -1, 0 },
  2326. { ERR_CPL_OPCODE_0, "SGE received 0-length CPL", -1, 0 },
  2327. { ERR_DROPPED_DB, "SGE doorbell dropped", -1, 0 },
  2328. { ERR_DATA_CPL_ON_HIGH_QID1 | ERR_DATA_CPL_ON_HIGH_QID0,
  2329. "SGE IQID > 1023 received CPL for FL", -1, 0 },
  2330. { ERR_BAD_DB_PIDX3, "SGE DBP 3 pidx increment too large", -1,
  2331. 0 },
  2332. { ERR_BAD_DB_PIDX2, "SGE DBP 2 pidx increment too large", -1,
  2333. 0 },
  2334. { ERR_BAD_DB_PIDX1, "SGE DBP 1 pidx increment too large", -1,
  2335. 0 },
  2336. { ERR_BAD_DB_PIDX0, "SGE DBP 0 pidx increment too large", -1,
  2337. 0 },
  2338. { ERR_ING_CTXT_PRIO,
  2339. "SGE too many priority ingress contexts", -1, 0 },
  2340. { ERR_EGR_CTXT_PRIO,
  2341. "SGE too many priority egress contexts", -1, 0 },
  2342. { INGRESS_SIZE_ERR, "SGE illegal ingress QID", -1, 0 },
  2343. { EGRESS_SIZE_ERR, "SGE illegal egress QID", -1, 0 },
  2344. { 0, NULL, 0, 0 }
  2345. };
  2346. v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1) |
  2347. ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2) << 32);
  2348. if (v) {
  2349. csio_fatal(hw, "SGE parity error (%#llx)\n",
  2350. (unsigned long long)v);
  2351. csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
  2352. SGE_INT_CAUSE1);
  2353. csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2);
  2354. }
  2355. v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info);
  2356. if (csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info) ||
  2357. v != 0)
  2358. csio_hw_fatal_err(hw);
  2359. }
  2360. #define CIM_OBQ_INTR (OBQULP0PARERR | OBQULP1PARERR | OBQULP2PARERR |\
  2361. OBQULP3PARERR | OBQSGEPARERR | OBQNCSIPARERR)
  2362. #define CIM_IBQ_INTR (IBQTP0PARERR | IBQTP1PARERR | IBQULPPARERR |\
  2363. IBQSGEHIPARERR | IBQSGELOPARERR | IBQNCSIPARERR)
  2364. /*
  2365. * CIM interrupt handler.
  2366. */
  2367. static void csio_cim_intr_handler(struct csio_hw *hw)
  2368. {
  2369. static struct intr_info cim_intr_info[] = {
  2370. { PREFDROPINT, "CIM control register prefetch drop", -1, 1 },
  2371. { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
  2372. { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
  2373. { MBUPPARERR, "CIM mailbox uP parity error", -1, 1 },
  2374. { MBHOSTPARERR, "CIM mailbox host parity error", -1, 1 },
  2375. { TIEQINPARERRINT, "CIM TIEQ outgoing parity error", -1, 1 },
  2376. { TIEQOUTPARERRINT, "CIM TIEQ incoming parity error", -1, 1 },
  2377. { 0, NULL, 0, 0 }
  2378. };
  2379. static struct intr_info cim_upintr_info[] = {
  2380. { RSVDSPACEINT, "CIM reserved space access", -1, 1 },
  2381. { ILLTRANSINT, "CIM illegal transaction", -1, 1 },
  2382. { ILLWRINT, "CIM illegal write", -1, 1 },
  2383. { ILLRDINT, "CIM illegal read", -1, 1 },
  2384. { ILLRDBEINT, "CIM illegal read BE", -1, 1 },
  2385. { ILLWRBEINT, "CIM illegal write BE", -1, 1 },
  2386. { SGLRDBOOTINT, "CIM single read from boot space", -1, 1 },
  2387. { SGLWRBOOTINT, "CIM single write to boot space", -1, 1 },
  2388. { BLKWRBOOTINT, "CIM block write to boot space", -1, 1 },
  2389. { SGLRDFLASHINT, "CIM single read from flash space", -1, 1 },
  2390. { SGLWRFLASHINT, "CIM single write to flash space", -1, 1 },
  2391. { BLKWRFLASHINT, "CIM block write to flash space", -1, 1 },
  2392. { SGLRDEEPROMINT, "CIM single EEPROM read", -1, 1 },
  2393. { SGLWREEPROMINT, "CIM single EEPROM write", -1, 1 },
  2394. { BLKRDEEPROMINT, "CIM block EEPROM read", -1, 1 },
  2395. { BLKWREEPROMINT, "CIM block EEPROM write", -1, 1 },
  2396. { SGLRDCTLINT , "CIM single read from CTL space", -1, 1 },
  2397. { SGLWRCTLINT , "CIM single write to CTL space", -1, 1 },
  2398. { BLKRDCTLINT , "CIM block read from CTL space", -1, 1 },
  2399. { BLKWRCTLINT , "CIM block write to CTL space", -1, 1 },
  2400. { SGLRDPLINT , "CIM single read from PL space", -1, 1 },
  2401. { SGLWRPLINT , "CIM single write to PL space", -1, 1 },
  2402. { BLKRDPLINT , "CIM block read from PL space", -1, 1 },
  2403. { BLKWRPLINT , "CIM block write to PL space", -1, 1 },
  2404. { REQOVRLOOKUPINT , "CIM request FIFO overwrite", -1, 1 },
  2405. { RSPOVRLOOKUPINT , "CIM response FIFO overwrite", -1, 1 },
  2406. { TIMEOUTINT , "CIM PIF timeout", -1, 1 },
  2407. { TIMEOUTMAINT , "CIM PIF MA timeout", -1, 1 },
  2408. { 0, NULL, 0, 0 }
  2409. };
  2410. int fat;
  2411. fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE,
  2412. cim_intr_info) +
  2413. csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE,
  2414. cim_upintr_info);
  2415. if (fat)
  2416. csio_hw_fatal_err(hw);
  2417. }
  2418. /*
  2419. * ULP RX interrupt handler.
  2420. */
  2421. static void csio_ulprx_intr_handler(struct csio_hw *hw)
  2422. {
  2423. static struct intr_info ulprx_intr_info[] = {
  2424. { 0x1800000, "ULPRX context error", -1, 1 },
  2425. { 0x7fffff, "ULPRX parity error", -1, 1 },
  2426. { 0, NULL, 0, 0 }
  2427. };
  2428. if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE, ulprx_intr_info))
  2429. csio_hw_fatal_err(hw);
  2430. }
  2431. /*
  2432. * ULP TX interrupt handler.
  2433. */
  2434. static void csio_ulptx_intr_handler(struct csio_hw *hw)
  2435. {
  2436. static struct intr_info ulptx_intr_info[] = {
  2437. { PBL_BOUND_ERR_CH3, "ULPTX channel 3 PBL out of bounds", -1,
  2438. 0 },
  2439. { PBL_BOUND_ERR_CH2, "ULPTX channel 2 PBL out of bounds", -1,
  2440. 0 },
  2441. { PBL_BOUND_ERR_CH1, "ULPTX channel 1 PBL out of bounds", -1,
  2442. 0 },
  2443. { PBL_BOUND_ERR_CH0, "ULPTX channel 0 PBL out of bounds", -1,
  2444. 0 },
  2445. { 0xfffffff, "ULPTX parity error", -1, 1 },
  2446. { 0, NULL, 0, 0 }
  2447. };
  2448. if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE, ulptx_intr_info))
  2449. csio_hw_fatal_err(hw);
  2450. }
  2451. /*
  2452. * PM TX interrupt handler.
  2453. */
  2454. static void csio_pmtx_intr_handler(struct csio_hw *hw)
  2455. {
  2456. static struct intr_info pmtx_intr_info[] = {
  2457. { PCMD_LEN_OVFL0, "PMTX channel 0 pcmd too large", -1, 1 },
  2458. { PCMD_LEN_OVFL1, "PMTX channel 1 pcmd too large", -1, 1 },
  2459. { PCMD_LEN_OVFL2, "PMTX channel 2 pcmd too large", -1, 1 },
  2460. { ZERO_C_CMD_ERROR, "PMTX 0-length pcmd", -1, 1 },
  2461. { 0xffffff0, "PMTX framing error", -1, 1 },
  2462. { OESPI_PAR_ERROR, "PMTX oespi parity error", -1, 1 },
  2463. { DB_OPTIONS_PAR_ERROR, "PMTX db_options parity error", -1,
  2464. 1 },
  2465. { ICSPI_PAR_ERROR, "PMTX icspi parity error", -1, 1 },
  2466. { C_PCMD_PAR_ERROR, "PMTX c_pcmd parity error", -1, 1},
  2467. { 0, NULL, 0, 0 }
  2468. };
  2469. if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE, pmtx_intr_info))
  2470. csio_hw_fatal_err(hw);
  2471. }
  2472. /*
  2473. * PM RX interrupt handler.
  2474. */
  2475. static void csio_pmrx_intr_handler(struct csio_hw *hw)
  2476. {
  2477. static struct intr_info pmrx_intr_info[] = {
  2478. { ZERO_E_CMD_ERROR, "PMRX 0-length pcmd", -1, 1 },
  2479. { 0x3ffff0, "PMRX framing error", -1, 1 },
  2480. { OCSPI_PAR_ERROR, "PMRX ocspi parity error", -1, 1 },
  2481. { DB_OPTIONS_PAR_ERROR, "PMRX db_options parity error", -1,
  2482. 1 },
  2483. { IESPI_PAR_ERROR, "PMRX iespi parity error", -1, 1 },
  2484. { E_PCMD_PAR_ERROR, "PMRX e_pcmd parity error", -1, 1},
  2485. { 0, NULL, 0, 0 }
  2486. };
  2487. if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE, pmrx_intr_info))
  2488. csio_hw_fatal_err(hw);
  2489. }
  2490. /*
  2491. * CPL switch interrupt handler.
  2492. */
  2493. static void csio_cplsw_intr_handler(struct csio_hw *hw)
  2494. {
  2495. static struct intr_info cplsw_intr_info[] = {
  2496. { CIM_OP_MAP_PERR, "CPLSW CIM op_map parity error", -1, 1 },
  2497. { CIM_OVFL_ERROR, "CPLSW CIM overflow", -1, 1 },
  2498. { TP_FRAMING_ERROR, "CPLSW TP framing error", -1, 1 },
  2499. { SGE_FRAMING_ERROR, "CPLSW SGE framing error", -1, 1 },
  2500. { CIM_FRAMING_ERROR, "CPLSW CIM framing error", -1, 1 },
  2501. { ZERO_SWITCH_ERROR, "CPLSW no-switch error", -1, 1 },
  2502. { 0, NULL, 0, 0 }
  2503. };
  2504. if (csio_handle_intr_status(hw, CPL_INTR_CAUSE, cplsw_intr_info))
  2505. csio_hw_fatal_err(hw);
  2506. }
  2507. /*
  2508. * LE interrupt handler.
  2509. */
  2510. static void csio_le_intr_handler(struct csio_hw *hw)
  2511. {
  2512. static struct intr_info le_intr_info[] = {
  2513. { LIPMISS, "LE LIP miss", -1, 0 },
  2514. { LIP0, "LE 0 LIP error", -1, 0 },
  2515. { PARITYERR, "LE parity error", -1, 1 },
  2516. { UNKNOWNCMD, "LE unknown command", -1, 1 },
  2517. { REQQPARERR, "LE request queue parity error", -1, 1 },
  2518. { 0, NULL, 0, 0 }
  2519. };
  2520. if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE, le_intr_info))
  2521. csio_hw_fatal_err(hw);
  2522. }
  2523. /*
  2524. * MPS interrupt handler.
  2525. */
  2526. static void csio_mps_intr_handler(struct csio_hw *hw)
  2527. {
  2528. static struct intr_info mps_rx_intr_info[] = {
  2529. { 0xffffff, "MPS Rx parity error", -1, 1 },
  2530. { 0, NULL, 0, 0 }
  2531. };
  2532. static struct intr_info mps_tx_intr_info[] = {
  2533. { TPFIFO, "MPS Tx TP FIFO parity error", -1, 1 },
  2534. { NCSIFIFO, "MPS Tx NC-SI FIFO parity error", -1, 1 },
  2535. { TXDATAFIFO, "MPS Tx data FIFO parity error", -1, 1 },
  2536. { TXDESCFIFO, "MPS Tx desc FIFO parity error", -1, 1 },
  2537. { BUBBLE, "MPS Tx underflow", -1, 1 },
  2538. { SECNTERR, "MPS Tx SOP/EOP error", -1, 1 },
  2539. { FRMERR, "MPS Tx framing error", -1, 1 },
  2540. { 0, NULL, 0, 0 }
  2541. };
  2542. static struct intr_info mps_trc_intr_info[] = {
  2543. { FILTMEM, "MPS TRC filter parity error", -1, 1 },
  2544. { PKTFIFO, "MPS TRC packet FIFO parity error", -1, 1 },
  2545. { MISCPERR, "MPS TRC misc parity error", -1, 1 },
  2546. { 0, NULL, 0, 0 }
  2547. };
  2548. static struct intr_info mps_stat_sram_intr_info[] = {
  2549. { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
  2550. { 0, NULL, 0, 0 }
  2551. };
  2552. static struct intr_info mps_stat_tx_intr_info[] = {
  2553. { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
  2554. { 0, NULL, 0, 0 }
  2555. };
  2556. static struct intr_info mps_stat_rx_intr_info[] = {
  2557. { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
  2558. { 0, NULL, 0, 0 }
  2559. };
  2560. static struct intr_info mps_cls_intr_info[] = {
  2561. { MATCHSRAM, "MPS match SRAM parity error", -1, 1 },
  2562. { MATCHTCAM, "MPS match TCAM parity error", -1, 1 },
  2563. { HASHSRAM, "MPS hash SRAM parity error", -1, 1 },
  2564. { 0, NULL, 0, 0 }
  2565. };
  2566. int fat;
  2567. fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE,
  2568. mps_rx_intr_info) +
  2569. csio_handle_intr_status(hw, MPS_TX_INT_CAUSE,
  2570. mps_tx_intr_info) +
  2571. csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE,
  2572. mps_trc_intr_info) +
  2573. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM,
  2574. mps_stat_sram_intr_info) +
  2575. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO,
  2576. mps_stat_tx_intr_info) +
  2577. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO,
  2578. mps_stat_rx_intr_info) +
  2579. csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE,
  2580. mps_cls_intr_info);
  2581. csio_wr_reg32(hw, 0, MPS_INT_CAUSE);
  2582. csio_rd_reg32(hw, MPS_INT_CAUSE); /* flush */
  2583. if (fat)
  2584. csio_hw_fatal_err(hw);
  2585. }
  2586. #define MEM_INT_MASK (PERR_INT_CAUSE | ECC_CE_INT_CAUSE | ECC_UE_INT_CAUSE)
  2587. /*
  2588. * EDC/MC interrupt handler.
  2589. */
  2590. static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
  2591. {
  2592. static const char name[3][5] = { "EDC0", "EDC1", "MC" };
  2593. unsigned int addr, cnt_addr, v;
  2594. if (idx <= MEM_EDC1) {
  2595. addr = EDC_REG(EDC_INT_CAUSE, idx);
  2596. cnt_addr = EDC_REG(EDC_ECC_STATUS, idx);
  2597. } else {
  2598. addr = MC_INT_CAUSE;
  2599. cnt_addr = MC_ECC_STATUS;
  2600. }
  2601. v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
  2602. if (v & PERR_INT_CAUSE)
  2603. csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
  2604. if (v & ECC_CE_INT_CAUSE) {
  2605. uint32_t cnt = ECC_CECNT_GET(csio_rd_reg32(hw, cnt_addr));
  2606. csio_wr_reg32(hw, ECC_CECNT_MASK, cnt_addr);
  2607. csio_warn(hw, "%u %s correctable ECC data error%s\n",
  2608. cnt, name[idx], cnt > 1 ? "s" : "");
  2609. }
  2610. if (v & ECC_UE_INT_CAUSE)
  2611. csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
  2612. csio_wr_reg32(hw, v, addr);
  2613. if (v & (PERR_INT_CAUSE | ECC_UE_INT_CAUSE))
  2614. csio_hw_fatal_err(hw);
  2615. }
  2616. /*
  2617. * MA interrupt handler.
  2618. */
  2619. static void csio_ma_intr_handler(struct csio_hw *hw)
  2620. {
  2621. uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE);
  2622. if (status & MEM_PERR_INT_CAUSE)
  2623. csio_fatal(hw, "MA parity error, parity status %#x\n",
  2624. csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS));
  2625. if (status & MEM_WRAP_INT_CAUSE) {
  2626. v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS);
  2627. csio_fatal(hw,
  2628. "MA address wrap-around error by client %u to address %#x\n",
  2629. MEM_WRAP_CLIENT_NUM_GET(v), MEM_WRAP_ADDRESS_GET(v) << 4);
  2630. }
  2631. csio_wr_reg32(hw, status, MA_INT_CAUSE);
  2632. csio_hw_fatal_err(hw);
  2633. }
  2634. /*
  2635. * SMB interrupt handler.
  2636. */
  2637. static void csio_smb_intr_handler(struct csio_hw *hw)
  2638. {
  2639. static struct intr_info smb_intr_info[] = {
  2640. { MSTTXFIFOPARINT, "SMB master Tx FIFO parity error", -1, 1 },
  2641. { MSTRXFIFOPARINT, "SMB master Rx FIFO parity error", -1, 1 },
  2642. { SLVFIFOPARINT, "SMB slave FIFO parity error", -1, 1 },
  2643. { 0, NULL, 0, 0 }
  2644. };
  2645. if (csio_handle_intr_status(hw, SMB_INT_CAUSE, smb_intr_info))
  2646. csio_hw_fatal_err(hw);
  2647. }
  2648. /*
  2649. * NC-SI interrupt handler.
  2650. */
  2651. static void csio_ncsi_intr_handler(struct csio_hw *hw)
  2652. {
  2653. static struct intr_info ncsi_intr_info[] = {
  2654. { CIM_DM_PRTY_ERR, "NC-SI CIM parity error", -1, 1 },
  2655. { MPS_DM_PRTY_ERR, "NC-SI MPS parity error", -1, 1 },
  2656. { TXFIFO_PRTY_ERR, "NC-SI Tx FIFO parity error", -1, 1 },
  2657. { RXFIFO_PRTY_ERR, "NC-SI Rx FIFO parity error", -1, 1 },
  2658. { 0, NULL, 0, 0 }
  2659. };
  2660. if (csio_handle_intr_status(hw, NCSI_INT_CAUSE, ncsi_intr_info))
  2661. csio_hw_fatal_err(hw);
  2662. }
  2663. /*
  2664. * XGMAC interrupt handler.
  2665. */
  2666. static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
  2667. {
  2668. uint32_t v = csio_rd_reg32(hw, CSIO_MAC_INT_CAUSE_REG(hw, port));
  2669. v &= TXFIFO_PRTY_ERR | RXFIFO_PRTY_ERR;
  2670. if (!v)
  2671. return;
  2672. if (v & TXFIFO_PRTY_ERR)
  2673. csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
  2674. if (v & RXFIFO_PRTY_ERR)
  2675. csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
  2676. csio_wr_reg32(hw, v, CSIO_MAC_INT_CAUSE_REG(hw, port));
  2677. csio_hw_fatal_err(hw);
  2678. }
  2679. /*
  2680. * PL interrupt handler.
  2681. */
  2682. static void csio_pl_intr_handler(struct csio_hw *hw)
  2683. {
  2684. static struct intr_info pl_intr_info[] = {
  2685. { FATALPERR, "T4 fatal parity error", -1, 1 },
  2686. { PERRVFID, "PL VFID_MAP parity error", -1, 1 },
  2687. { 0, NULL, 0, 0 }
  2688. };
  2689. if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE, pl_intr_info))
  2690. csio_hw_fatal_err(hw);
  2691. }
  2692. /*
  2693. * csio_hw_slow_intr_handler - control path interrupt handler
  2694. * @hw: HW module
  2695. *
  2696. * Interrupt handler for non-data global interrupt events, e.g., errors.
  2697. * The designation 'slow' is because it involves register reads, while
  2698. * data interrupts typically don't involve any MMIOs.
  2699. */
  2700. int
  2701. csio_hw_slow_intr_handler(struct csio_hw *hw)
  2702. {
  2703. uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE);
  2704. if (!(cause & CSIO_GLBL_INTR_MASK)) {
  2705. CSIO_INC_STATS(hw, n_plint_unexp);
  2706. return 0;
  2707. }
  2708. csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
  2709. CSIO_INC_STATS(hw, n_plint_cnt);
  2710. if (cause & CIM)
  2711. csio_cim_intr_handler(hw);
  2712. if (cause & MPS)
  2713. csio_mps_intr_handler(hw);
  2714. if (cause & NCSI)
  2715. csio_ncsi_intr_handler(hw);
  2716. if (cause & PL)
  2717. csio_pl_intr_handler(hw);
  2718. if (cause & SMB)
  2719. csio_smb_intr_handler(hw);
  2720. if (cause & XGMAC0)
  2721. csio_xgmac_intr_handler(hw, 0);
  2722. if (cause & XGMAC1)
  2723. csio_xgmac_intr_handler(hw, 1);
  2724. if (cause & XGMAC_KR0)
  2725. csio_xgmac_intr_handler(hw, 2);
  2726. if (cause & XGMAC_KR1)
  2727. csio_xgmac_intr_handler(hw, 3);
  2728. if (cause & PCIE)
  2729. hw->chip_ops->chip_pcie_intr_handler(hw);
  2730. if (cause & MC)
  2731. csio_mem_intr_handler(hw, MEM_MC);
  2732. if (cause & EDC0)
  2733. csio_mem_intr_handler(hw, MEM_EDC0);
  2734. if (cause & EDC1)
  2735. csio_mem_intr_handler(hw, MEM_EDC1);
  2736. if (cause & LE)
  2737. csio_le_intr_handler(hw);
  2738. if (cause & TP)
  2739. csio_tp_intr_handler(hw);
  2740. if (cause & MA)
  2741. csio_ma_intr_handler(hw);
  2742. if (cause & PM_TX)
  2743. csio_pmtx_intr_handler(hw);
  2744. if (cause & PM_RX)
  2745. csio_pmrx_intr_handler(hw);
  2746. if (cause & ULP_RX)
  2747. csio_ulprx_intr_handler(hw);
  2748. if (cause & CPL_SWITCH)
  2749. csio_cplsw_intr_handler(hw);
  2750. if (cause & SGE)
  2751. csio_sge_intr_handler(hw);
  2752. if (cause & ULP_TX)
  2753. csio_ulptx_intr_handler(hw);
  2754. /* Clear the interrupts just processed for which we are the master. */
  2755. csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE);
  2756. csio_rd_reg32(hw, PL_INT_CAUSE); /* flush */
  2757. return 1;
  2758. }
  2759. /*****************************************************************************
  2760. * HW <--> mailbox interfacing routines.
  2761. ****************************************************************************/
  2762. /*
  2763. * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
  2764. *
  2765. * @data: Private data pointer.
  2766. *
  2767. * Called from worker thread context.
  2768. */
  2769. static void
  2770. csio_mberr_worker(void *data)
  2771. {
  2772. struct csio_hw *hw = (struct csio_hw *)data;
  2773. struct csio_mbm *mbm = &hw->mbm;
  2774. LIST_HEAD(cbfn_q);
  2775. struct csio_mb *mbp_next;
  2776. int rv;
  2777. del_timer_sync(&mbm->timer);
  2778. spin_lock_irq(&hw->lock);
  2779. if (list_empty(&mbm->cbfn_q)) {
  2780. spin_unlock_irq(&hw->lock);
  2781. return;
  2782. }
  2783. list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
  2784. mbm->stats.n_cbfnq = 0;
  2785. /* Try to start waiting mailboxes */
  2786. if (!list_empty(&mbm->req_q)) {
  2787. mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
  2788. list_del_init(&mbp_next->list);
  2789. rv = csio_mb_issue(hw, mbp_next);
  2790. if (rv != 0)
  2791. list_add_tail(&mbp_next->list, &mbm->req_q);
  2792. else
  2793. CSIO_DEC_STATS(mbm, n_activeq);
  2794. }
  2795. spin_unlock_irq(&hw->lock);
  2796. /* Now callback completions */
  2797. csio_mb_completions(hw, &cbfn_q);
  2798. }
  2799. /*
  2800. * csio_hw_mb_timer - Top-level Mailbox timeout handler.
  2801. *
  2802. * @data: private data pointer
  2803. *
  2804. **/
  2805. static void
  2806. csio_hw_mb_timer(uintptr_t data)
  2807. {
  2808. struct csio_hw *hw = (struct csio_hw *)data;
  2809. struct csio_mb *mbp = NULL;
  2810. spin_lock_irq(&hw->lock);
  2811. mbp = csio_mb_tmo_handler(hw);
  2812. spin_unlock_irq(&hw->lock);
  2813. /* Call back the function for the timed-out Mailbox */
  2814. if (mbp)
  2815. mbp->mb_cbfn(hw, mbp);
  2816. }
  2817. /*
  2818. * csio_hw_mbm_cleanup - Cleanup Mailbox module.
  2819. * @hw: HW module
  2820. *
  2821. * Called with lock held, should exit with lock held.
  2822. * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
  2823. * into a local queue. Drops lock and calls the completions. Holds
  2824. * lock and returns.
  2825. */
  2826. static void
  2827. csio_hw_mbm_cleanup(struct csio_hw *hw)
  2828. {
  2829. LIST_HEAD(cbfn_q);
  2830. csio_mb_cancel_all(hw, &cbfn_q);
  2831. spin_unlock_irq(&hw->lock);
  2832. csio_mb_completions(hw, &cbfn_q);
  2833. spin_lock_irq(&hw->lock);
  2834. }
  2835. /*****************************************************************************
  2836. * Event handling
  2837. ****************************************************************************/
  2838. int
  2839. csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  2840. uint16_t len)
  2841. {
  2842. struct csio_evt_msg *evt_entry = NULL;
  2843. if (type >= CSIO_EVT_MAX)
  2844. return -EINVAL;
  2845. if (len > CSIO_EVT_MSG_SIZE)
  2846. return -EINVAL;
  2847. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  2848. return -EINVAL;
  2849. if (list_empty(&hw->evt_free_q)) {
  2850. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  2851. type, len);
  2852. return -ENOMEM;
  2853. }
  2854. evt_entry = list_first_entry(&hw->evt_free_q,
  2855. struct csio_evt_msg, list);
  2856. list_del_init(&evt_entry->list);
  2857. /* copy event msg and queue the event */
  2858. evt_entry->type = type;
  2859. memcpy((void *)evt_entry->data, evt_msg, len);
  2860. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  2861. CSIO_DEC_STATS(hw, n_evt_freeq);
  2862. CSIO_INC_STATS(hw, n_evt_activeq);
  2863. return 0;
  2864. }
  2865. static int
  2866. csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  2867. uint16_t len, bool msg_sg)
  2868. {
  2869. struct csio_evt_msg *evt_entry = NULL;
  2870. struct csio_fl_dma_buf *fl_sg;
  2871. uint32_t off = 0;
  2872. unsigned long flags;
  2873. int n, ret = 0;
  2874. if (type >= CSIO_EVT_MAX)
  2875. return -EINVAL;
  2876. if (len > CSIO_EVT_MSG_SIZE)
  2877. return -EINVAL;
  2878. spin_lock_irqsave(&hw->lock, flags);
  2879. if (hw->flags & CSIO_HWF_FWEVT_STOP) {
  2880. ret = -EINVAL;
  2881. goto out;
  2882. }
  2883. if (list_empty(&hw->evt_free_q)) {
  2884. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  2885. type, len);
  2886. ret = -ENOMEM;
  2887. goto out;
  2888. }
  2889. evt_entry = list_first_entry(&hw->evt_free_q,
  2890. struct csio_evt_msg, list);
  2891. list_del_init(&evt_entry->list);
  2892. /* copy event msg and queue the event */
  2893. evt_entry->type = type;
  2894. /* If Payload in SG list*/
  2895. if (msg_sg) {
  2896. fl_sg = (struct csio_fl_dma_buf *) evt_msg;
  2897. for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
  2898. memcpy((void *)((uintptr_t)evt_entry->data + off),
  2899. fl_sg->flbufs[n].vaddr,
  2900. fl_sg->flbufs[n].len);
  2901. off += fl_sg->flbufs[n].len;
  2902. }
  2903. } else
  2904. memcpy((void *)evt_entry->data, evt_msg, len);
  2905. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  2906. CSIO_DEC_STATS(hw, n_evt_freeq);
  2907. CSIO_INC_STATS(hw, n_evt_activeq);
  2908. out:
  2909. spin_unlock_irqrestore(&hw->lock, flags);
  2910. return ret;
  2911. }
  2912. static void
  2913. csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
  2914. {
  2915. if (evt_entry) {
  2916. spin_lock_irq(&hw->lock);
  2917. list_del_init(&evt_entry->list);
  2918. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  2919. CSIO_DEC_STATS(hw, n_evt_activeq);
  2920. CSIO_INC_STATS(hw, n_evt_freeq);
  2921. spin_unlock_irq(&hw->lock);
  2922. }
  2923. }
  2924. void
  2925. csio_evtq_flush(struct csio_hw *hw)
  2926. {
  2927. uint32_t count;
  2928. count = 30;
  2929. while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
  2930. spin_unlock_irq(&hw->lock);
  2931. msleep(2000);
  2932. spin_lock_irq(&hw->lock);
  2933. }
  2934. CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
  2935. }
  2936. static void
  2937. csio_evtq_stop(struct csio_hw *hw)
  2938. {
  2939. hw->flags |= CSIO_HWF_FWEVT_STOP;
  2940. }
  2941. static void
  2942. csio_evtq_start(struct csio_hw *hw)
  2943. {
  2944. hw->flags &= ~CSIO_HWF_FWEVT_STOP;
  2945. }
  2946. static void
  2947. csio_evtq_cleanup(struct csio_hw *hw)
  2948. {
  2949. struct list_head *evt_entry, *next_entry;
  2950. /* Release outstanding events from activeq to freeq*/
  2951. if (!list_empty(&hw->evt_active_q))
  2952. list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
  2953. hw->stats.n_evt_activeq = 0;
  2954. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  2955. /* Freeup event entry */
  2956. list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
  2957. kfree(evt_entry);
  2958. CSIO_DEC_STATS(hw, n_evt_freeq);
  2959. }
  2960. hw->stats.n_evt_freeq = 0;
  2961. }
  2962. static void
  2963. csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
  2964. struct csio_fl_dma_buf *flb, void *priv)
  2965. {
  2966. __u8 op;
  2967. void *msg = NULL;
  2968. uint32_t msg_len = 0;
  2969. bool msg_sg = 0;
  2970. op = ((struct rss_header *) wr)->opcode;
  2971. if (op == CPL_FW6_PLD) {
  2972. CSIO_INC_STATS(hw, n_cpl_fw6_pld);
  2973. if (!flb || !flb->totlen) {
  2974. CSIO_INC_STATS(hw, n_cpl_unexp);
  2975. return;
  2976. }
  2977. msg = (void *) flb;
  2978. msg_len = flb->totlen;
  2979. msg_sg = 1;
  2980. } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
  2981. CSIO_INC_STATS(hw, n_cpl_fw6_msg);
  2982. /* skip RSS header */
  2983. msg = (void *)((uintptr_t)wr + sizeof(__be64));
  2984. msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
  2985. sizeof(struct cpl_fw4_msg);
  2986. } else {
  2987. csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
  2988. CSIO_INC_STATS(hw, n_cpl_unexp);
  2989. return;
  2990. }
  2991. /*
  2992. * Enqueue event to EventQ. Events processing happens
  2993. * in Event worker thread context
  2994. */
  2995. if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
  2996. (uint16_t)msg_len, msg_sg))
  2997. CSIO_INC_STATS(hw, n_evt_drop);
  2998. }
  2999. void
  3000. csio_evtq_worker(struct work_struct *work)
  3001. {
  3002. struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
  3003. struct list_head *evt_entry, *next_entry;
  3004. LIST_HEAD(evt_q);
  3005. struct csio_evt_msg *evt_msg;
  3006. struct cpl_fw6_msg *msg;
  3007. struct csio_rnode *rn;
  3008. int rv = 0;
  3009. uint8_t evtq_stop = 0;
  3010. csio_dbg(hw, "event worker thread active evts#%d\n",
  3011. hw->stats.n_evt_activeq);
  3012. spin_lock_irq(&hw->lock);
  3013. while (!list_empty(&hw->evt_active_q)) {
  3014. list_splice_tail_init(&hw->evt_active_q, &evt_q);
  3015. spin_unlock_irq(&hw->lock);
  3016. list_for_each_safe(evt_entry, next_entry, &evt_q) {
  3017. evt_msg = (struct csio_evt_msg *) evt_entry;
  3018. /* Drop events if queue is STOPPED */
  3019. spin_lock_irq(&hw->lock);
  3020. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3021. evtq_stop = 1;
  3022. spin_unlock_irq(&hw->lock);
  3023. if (evtq_stop) {
  3024. CSIO_INC_STATS(hw, n_evt_drop);
  3025. goto free_evt;
  3026. }
  3027. switch (evt_msg->type) {
  3028. case CSIO_EVT_FW:
  3029. msg = (struct cpl_fw6_msg *)(evt_msg->data);
  3030. if ((msg->opcode == CPL_FW6_MSG ||
  3031. msg->opcode == CPL_FW4_MSG) &&
  3032. !msg->type) {
  3033. rv = csio_mb_fwevt_handler(hw,
  3034. msg->data);
  3035. if (!rv)
  3036. break;
  3037. /* Handle any remaining fw events */
  3038. csio_fcoe_fwevt_handler(hw,
  3039. msg->opcode, msg->data);
  3040. } else if (msg->opcode == CPL_FW6_PLD) {
  3041. csio_fcoe_fwevt_handler(hw,
  3042. msg->opcode, msg->data);
  3043. } else {
  3044. csio_warn(hw,
  3045. "Unhandled FW msg op %x type %x\n",
  3046. msg->opcode, msg->type);
  3047. CSIO_INC_STATS(hw, n_evt_drop);
  3048. }
  3049. break;
  3050. case CSIO_EVT_MBX:
  3051. csio_mberr_worker(hw);
  3052. break;
  3053. case CSIO_EVT_DEV_LOSS:
  3054. memcpy(&rn, evt_msg->data, sizeof(rn));
  3055. csio_rnode_devloss_handler(rn);
  3056. break;
  3057. default:
  3058. csio_warn(hw, "Unhandled event %x on evtq\n",
  3059. evt_msg->type);
  3060. CSIO_INC_STATS(hw, n_evt_unexp);
  3061. break;
  3062. }
  3063. free_evt:
  3064. csio_free_evt(hw, evt_msg);
  3065. }
  3066. spin_lock_irq(&hw->lock);
  3067. }
  3068. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3069. spin_unlock_irq(&hw->lock);
  3070. }
  3071. int
  3072. csio_fwevtq_handler(struct csio_hw *hw)
  3073. {
  3074. int rv;
  3075. if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
  3076. CSIO_INC_STATS(hw, n_int_stray);
  3077. return -EINVAL;
  3078. }
  3079. rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
  3080. csio_process_fwevtq_entry, NULL);
  3081. return rv;
  3082. }
  3083. /****************************************************************************
  3084. * Entry points
  3085. ****************************************************************************/
  3086. /* Management module */
  3087. /*
  3088. * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
  3089. * mgmt - mgmt module
  3090. * @io_req - io request
  3091. *
  3092. * Return - 0:if given IO Req exists in active Q.
  3093. * -EINVAL :if lookup fails.
  3094. */
  3095. int
  3096. csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
  3097. {
  3098. struct list_head *tmp;
  3099. /* Lookup ioreq in the ACTIVEQ */
  3100. list_for_each(tmp, &mgmtm->active_q) {
  3101. if (io_req == (struct csio_ioreq *)tmp)
  3102. return 0;
  3103. }
  3104. return -EINVAL;
  3105. }
  3106. #define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
  3107. /*
  3108. * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
  3109. * @data - Event data.
  3110. *
  3111. * Return - none.
  3112. */
  3113. static void
  3114. csio_mgmt_tmo_handler(uintptr_t data)
  3115. {
  3116. struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
  3117. struct list_head *tmp;
  3118. struct csio_ioreq *io_req;
  3119. csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
  3120. spin_lock_irq(&mgmtm->hw->lock);
  3121. list_for_each(tmp, &mgmtm->active_q) {
  3122. io_req = (struct csio_ioreq *) tmp;
  3123. io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
  3124. if (!io_req->tmo) {
  3125. /* Dequeue the request from retry Q. */
  3126. tmp = csio_list_prev(tmp);
  3127. list_del_init(&io_req->sm.sm_list);
  3128. if (io_req->io_cbfn) {
  3129. /* io_req will be freed by completion handler */
  3130. io_req->wr_status = -ETIMEDOUT;
  3131. io_req->io_cbfn(mgmtm->hw, io_req);
  3132. } else {
  3133. CSIO_DB_ASSERT(0);
  3134. }
  3135. }
  3136. }
  3137. /* If retry queue is not empty, re-arm timer */
  3138. if (!list_empty(&mgmtm->active_q))
  3139. mod_timer(&mgmtm->mgmt_timer,
  3140. jiffies + msecs_to_jiffies(ECM_MIN_TMO));
  3141. spin_unlock_irq(&mgmtm->hw->lock);
  3142. }
  3143. static void
  3144. csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
  3145. {
  3146. struct csio_hw *hw = mgmtm->hw;
  3147. struct csio_ioreq *io_req;
  3148. struct list_head *tmp;
  3149. uint32_t count;
  3150. count = 30;
  3151. /* Wait for all outstanding req to complete gracefully */
  3152. while ((!list_empty(&mgmtm->active_q)) && count--) {
  3153. spin_unlock_irq(&hw->lock);
  3154. msleep(2000);
  3155. spin_lock_irq(&hw->lock);
  3156. }
  3157. /* release outstanding req from ACTIVEQ */
  3158. list_for_each(tmp, &mgmtm->active_q) {
  3159. io_req = (struct csio_ioreq *) tmp;
  3160. tmp = csio_list_prev(tmp);
  3161. list_del_init(&io_req->sm.sm_list);
  3162. mgmtm->stats.n_active--;
  3163. if (io_req->io_cbfn) {
  3164. /* io_req will be freed by completion handler */
  3165. io_req->wr_status = -ETIMEDOUT;
  3166. io_req->io_cbfn(mgmtm->hw, io_req);
  3167. }
  3168. }
  3169. }
  3170. /*
  3171. * csio_mgmt_init - Mgmt module init entry point
  3172. * @mgmtsm - mgmt module
  3173. * @hw - HW module
  3174. *
  3175. * Initialize mgmt timer, resource wait queue, active queue,
  3176. * completion q. Allocate Egress and Ingress
  3177. * WR queues and save off the queue index returned by the WR
  3178. * module for future use. Allocate and save off mgmt reqs in the
  3179. * mgmt_req_freelist for future use. Make sure their SM is initialized
  3180. * to uninit state.
  3181. * Returns: 0 - on success
  3182. * -ENOMEM - on error.
  3183. */
  3184. static int
  3185. csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
  3186. {
  3187. struct timer_list *timer = &mgmtm->mgmt_timer;
  3188. init_timer(timer);
  3189. timer->function = csio_mgmt_tmo_handler;
  3190. timer->data = (unsigned long)mgmtm;
  3191. INIT_LIST_HEAD(&mgmtm->active_q);
  3192. INIT_LIST_HEAD(&mgmtm->cbfn_q);
  3193. mgmtm->hw = hw;
  3194. /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
  3195. return 0;
  3196. }
  3197. /*
  3198. * csio_mgmtm_exit - MGMT module exit entry point
  3199. * @mgmtsm - mgmt module
  3200. *
  3201. * This function called during MGMT module uninit.
  3202. * Stop timers, free ioreqs allocated.
  3203. * Returns: None
  3204. *
  3205. */
  3206. static void
  3207. csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
  3208. {
  3209. del_timer_sync(&mgmtm->mgmt_timer);
  3210. }
  3211. /**
  3212. * csio_hw_start - Kicks off the HW State machine
  3213. * @hw: Pointer to HW module.
  3214. *
  3215. * It is assumed that the initialization is a synchronous operation.
  3216. * So when we return afer posting the event, the HW SM should be in
  3217. * the ready state, if there were no errors during init.
  3218. */
  3219. int
  3220. csio_hw_start(struct csio_hw *hw)
  3221. {
  3222. spin_lock_irq(&hw->lock);
  3223. csio_post_event(&hw->sm, CSIO_HWE_CFG);
  3224. spin_unlock_irq(&hw->lock);
  3225. if (csio_is_hw_ready(hw))
  3226. return 0;
  3227. else
  3228. return -EINVAL;
  3229. }
  3230. int
  3231. csio_hw_stop(struct csio_hw *hw)
  3232. {
  3233. csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
  3234. if (csio_is_hw_removing(hw))
  3235. return 0;
  3236. else
  3237. return -EINVAL;
  3238. }
  3239. /* Max reset retries */
  3240. #define CSIO_MAX_RESET_RETRIES 3
  3241. /**
  3242. * csio_hw_reset - Reset the hardware
  3243. * @hw: HW module.
  3244. *
  3245. * Caller should hold lock across this function.
  3246. */
  3247. int
  3248. csio_hw_reset(struct csio_hw *hw)
  3249. {
  3250. if (!csio_is_hw_master(hw))
  3251. return -EPERM;
  3252. if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
  3253. csio_dbg(hw, "Max hw reset attempts reached..");
  3254. return -EINVAL;
  3255. }
  3256. hw->rst_retries++;
  3257. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
  3258. if (csio_is_hw_ready(hw)) {
  3259. hw->rst_retries = 0;
  3260. hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
  3261. return 0;
  3262. } else
  3263. return -EINVAL;
  3264. }
  3265. /*
  3266. * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
  3267. * @hw: HW module.
  3268. */
  3269. static void
  3270. csio_hw_get_device_id(struct csio_hw *hw)
  3271. {
  3272. /* Is the adapter device id cached already ?*/
  3273. if (csio_is_dev_id_cached(hw))
  3274. return;
  3275. /* Get the PCI vendor & device id */
  3276. pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
  3277. &hw->params.pci.vendor_id);
  3278. pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
  3279. &hw->params.pci.device_id);
  3280. csio_dev_id_cached(hw);
  3281. hw->chip_id = (hw->params.pci.device_id & CSIO_HW_CHIP_MASK);
  3282. } /* csio_hw_get_device_id */
  3283. /*
  3284. * csio_hw_set_description - Set the model, description of the hw.
  3285. * @hw: HW module.
  3286. * @ven_id: PCI Vendor ID
  3287. * @dev_id: PCI Device ID
  3288. */
  3289. static void
  3290. csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
  3291. {
  3292. uint32_t adap_type, prot_type;
  3293. if (ven_id == CSIO_VENDOR_ID) {
  3294. prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
  3295. adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
  3296. if (prot_type == CSIO_T4_FCOE_ASIC) {
  3297. memcpy(hw->hw_ver,
  3298. csio_t4_fcoe_adapters[adap_type].model_no, 16);
  3299. memcpy(hw->model_desc,
  3300. csio_t4_fcoe_adapters[adap_type].description,
  3301. 32);
  3302. } else if (prot_type == CSIO_T5_FCOE_ASIC) {
  3303. memcpy(hw->hw_ver,
  3304. csio_t5_fcoe_adapters[adap_type].model_no, 16);
  3305. memcpy(hw->model_desc,
  3306. csio_t5_fcoe_adapters[adap_type].description,
  3307. 32);
  3308. } else {
  3309. char tempName[32] = "Chelsio FCoE Controller";
  3310. memcpy(hw->model_desc, tempName, 32);
  3311. }
  3312. }
  3313. } /* csio_hw_set_description */
  3314. /**
  3315. * csio_hw_init - Initialize HW module.
  3316. * @hw: Pointer to HW module.
  3317. *
  3318. * Initialize the members of the HW module.
  3319. */
  3320. int
  3321. csio_hw_init(struct csio_hw *hw)
  3322. {
  3323. int rv = -EINVAL;
  3324. uint32_t i;
  3325. uint16_t ven_id, dev_id;
  3326. struct csio_evt_msg *evt_entry;
  3327. INIT_LIST_HEAD(&hw->sm.sm_list);
  3328. csio_init_state(&hw->sm, csio_hws_uninit);
  3329. spin_lock_init(&hw->lock);
  3330. INIT_LIST_HEAD(&hw->sln_head);
  3331. /* Get the PCI vendor & device id */
  3332. csio_hw_get_device_id(hw);
  3333. strcpy(hw->name, CSIO_HW_NAME);
  3334. /* Initialize the HW chip ops with T4/T5 specific ops */
  3335. hw->chip_ops = csio_is_t4(hw->chip_id) ? &t4_ops : &t5_ops;
  3336. /* Set the model & its description */
  3337. ven_id = hw->params.pci.vendor_id;
  3338. dev_id = hw->params.pci.device_id;
  3339. csio_hw_set_description(hw, ven_id, dev_id);
  3340. /* Initialize default log level */
  3341. hw->params.log_level = (uint32_t) csio_dbg_level;
  3342. csio_set_fwevt_intr_idx(hw, -1);
  3343. csio_set_nondata_intr_idx(hw, -1);
  3344. /* Init all the modules: Mailbox, WorkRequest and Transport */
  3345. if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
  3346. goto err;
  3347. rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
  3348. if (rv)
  3349. goto err_mbm_exit;
  3350. rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
  3351. if (rv)
  3352. goto err_wrm_exit;
  3353. rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
  3354. if (rv)
  3355. goto err_scsim_exit;
  3356. /* Pre-allocate evtq and initialize them */
  3357. INIT_LIST_HEAD(&hw->evt_active_q);
  3358. INIT_LIST_HEAD(&hw->evt_free_q);
  3359. for (i = 0; i < csio_evtq_sz; i++) {
  3360. evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
  3361. if (!evt_entry) {
  3362. csio_err(hw, "Failed to initialize eventq");
  3363. goto err_evtq_cleanup;
  3364. }
  3365. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3366. CSIO_INC_STATS(hw, n_evt_freeq);
  3367. }
  3368. hw->dev_num = dev_num;
  3369. dev_num++;
  3370. return 0;
  3371. err_evtq_cleanup:
  3372. csio_evtq_cleanup(hw);
  3373. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3374. err_scsim_exit:
  3375. csio_scsim_exit(csio_hw_to_scsim(hw));
  3376. err_wrm_exit:
  3377. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3378. err_mbm_exit:
  3379. csio_mbm_exit(csio_hw_to_mbm(hw));
  3380. err:
  3381. return rv;
  3382. }
  3383. /**
  3384. * csio_hw_exit - Un-initialize HW module.
  3385. * @hw: Pointer to HW module.
  3386. *
  3387. */
  3388. void
  3389. csio_hw_exit(struct csio_hw *hw)
  3390. {
  3391. csio_evtq_cleanup(hw);
  3392. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3393. csio_scsim_exit(csio_hw_to_scsim(hw));
  3394. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3395. csio_mbm_exit(csio_hw_to_mbm(hw));
  3396. }