csio_hw.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435
  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_dbg_level = 0xFEFF;
  48. unsigned int csio_port_mask = 0xf;
  49. /* Default FW event queue entries. */
  50. static uint32_t csio_evtq_sz = CSIO_EVTQ_SIZE;
  51. /* Default MSI param level */
  52. int csio_msi = 2;
  53. /* FCoE function instances */
  54. static int dev_num;
  55. /* FCoE Adapter types & its description */
  56. static const struct csio_adap_desc csio_t5_fcoe_adapters[] = {
  57. {"T580-Dbg 10G", "Chelsio T580-Dbg 10G [FCoE]"},
  58. {"T520-CR 10G", "Chelsio T520-CR 10G [FCoE]"},
  59. {"T522-CR 10G/1G", "Chelsio T522-CR 10G/1G [FCoE]"},
  60. {"T540-CR 10G", "Chelsio T540-CR 10G [FCoE]"},
  61. {"T520-BCH 10G", "Chelsio T520-BCH 10G [FCoE]"},
  62. {"T540-BCH 10G", "Chelsio T540-BCH 10G [FCoE]"},
  63. {"T540-CH 10G", "Chelsio T540-CH 10G [FCoE]"},
  64. {"T520-SO 10G", "Chelsio T520-SO 10G [FCoE]"},
  65. {"T520-CX4 10G", "Chelsio T520-CX4 10G [FCoE]"},
  66. {"T520-BT 10G", "Chelsio T520-BT 10G [FCoE]"},
  67. {"T504-BT 1G", "Chelsio T504-BT 1G [FCoE]"},
  68. {"B520-SR 10G", "Chelsio B520-SR 10G [FCoE]"},
  69. {"B504-BT 1G", "Chelsio B504-BT 1G [FCoE]"},
  70. {"T580-CR 10G", "Chelsio T580-CR 10G [FCoE]"},
  71. {"T540-LP-CR 10G", "Chelsio T540-LP-CR 10G [FCoE]"},
  72. {"AMSTERDAM 10G", "Chelsio AMSTERDAM 10G [FCoE]"},
  73. {"T580-LP-CR 40G", "Chelsio T580-LP-CR 40G [FCoE]"},
  74. {"T520-LL-CR 10G", "Chelsio T520-LL-CR 10G [FCoE]"},
  75. {"T560-CR 40G", "Chelsio T560-CR 40G [FCoE]"},
  76. {"T580-CR 40G", "Chelsio T580-CR 40G [FCoE]"},
  77. {"T580-SO 40G", "Chelsio T580-SO 40G [FCoE]"},
  78. {"T502-BT 1G", "Chelsio T502-BT 1G [FCoE]"}
  79. };
  80. static void csio_mgmtm_cleanup(struct csio_mgmtm *);
  81. static void csio_hw_mbm_cleanup(struct csio_hw *);
  82. /* State machine forward declarations */
  83. static void csio_hws_uninit(struct csio_hw *, enum csio_hw_ev);
  84. static void csio_hws_configuring(struct csio_hw *, enum csio_hw_ev);
  85. static void csio_hws_initializing(struct csio_hw *, enum csio_hw_ev);
  86. static void csio_hws_ready(struct csio_hw *, enum csio_hw_ev);
  87. static void csio_hws_quiescing(struct csio_hw *, enum csio_hw_ev);
  88. static void csio_hws_quiesced(struct csio_hw *, enum csio_hw_ev);
  89. static void csio_hws_resetting(struct csio_hw *, enum csio_hw_ev);
  90. static void csio_hws_removing(struct csio_hw *, enum csio_hw_ev);
  91. static void csio_hws_pcierr(struct csio_hw *, enum csio_hw_ev);
  92. static void csio_hw_initialize(struct csio_hw *hw);
  93. static void csio_evtq_stop(struct csio_hw *hw);
  94. static void csio_evtq_start(struct csio_hw *hw);
  95. int csio_is_hw_ready(struct csio_hw *hw)
  96. {
  97. return csio_match_state(hw, csio_hws_ready);
  98. }
  99. int csio_is_hw_removing(struct csio_hw *hw)
  100. {
  101. return csio_match_state(hw, csio_hws_removing);
  102. }
  103. /*
  104. * csio_hw_wait_op_done_val - wait until an operation is completed
  105. * @hw: the HW module
  106. * @reg: the register to check for completion
  107. * @mask: a single-bit field within @reg that indicates completion
  108. * @polarity: the value of the field when the operation is completed
  109. * @attempts: number of check iterations
  110. * @delay: delay in usecs between iterations
  111. * @valp: where to store the value of the register at completion time
  112. *
  113. * Wait until an operation is completed by checking a bit in a register
  114. * up to @attempts times. If @valp is not NULL the value of the register
  115. * at the time it indicated completion is stored there. Returns 0 if the
  116. * operation completes and -EAGAIN otherwise.
  117. */
  118. int
  119. csio_hw_wait_op_done_val(struct csio_hw *hw, int reg, uint32_t mask,
  120. int polarity, int attempts, int delay, uint32_t *valp)
  121. {
  122. uint32_t val;
  123. while (1) {
  124. val = csio_rd_reg32(hw, reg);
  125. if (!!(val & mask) == polarity) {
  126. if (valp)
  127. *valp = val;
  128. return 0;
  129. }
  130. if (--attempts == 0)
  131. return -EAGAIN;
  132. if (delay)
  133. udelay(delay);
  134. }
  135. }
  136. /*
  137. * csio_hw_tp_wr_bits_indirect - set/clear bits in an indirect TP register
  138. * @hw: the adapter
  139. * @addr: the indirect TP register address
  140. * @mask: specifies the field within the register to modify
  141. * @val: new value for the field
  142. *
  143. * Sets a field of an indirect TP register to the given value.
  144. */
  145. void
  146. csio_hw_tp_wr_bits_indirect(struct csio_hw *hw, unsigned int addr,
  147. unsigned int mask, unsigned int val)
  148. {
  149. csio_wr_reg32(hw, addr, TP_PIO_ADDR_A);
  150. val |= csio_rd_reg32(hw, TP_PIO_DATA_A) & ~mask;
  151. csio_wr_reg32(hw, val, TP_PIO_DATA_A);
  152. }
  153. void
  154. csio_set_reg_field(struct csio_hw *hw, uint32_t reg, uint32_t mask,
  155. uint32_t value)
  156. {
  157. uint32_t val = csio_rd_reg32(hw, reg) & ~mask;
  158. csio_wr_reg32(hw, val | value, reg);
  159. /* Flush */
  160. csio_rd_reg32(hw, reg);
  161. }
  162. static int
  163. csio_memory_write(struct csio_hw *hw, int mtype, u32 addr, u32 len, u32 *buf)
  164. {
  165. return hw->chip_ops->chip_memory_rw(hw, MEMWIN_CSIOSTOR, mtype,
  166. addr, len, buf, 0);
  167. }
  168. /*
  169. * EEPROM reads take a few tens of us while writes can take a bit over 5 ms.
  170. */
  171. #define EEPROM_MAX_RD_POLL 40
  172. #define EEPROM_MAX_WR_POLL 6
  173. #define EEPROM_STAT_ADDR 0x7bfc
  174. #define VPD_BASE 0x400
  175. #define VPD_BASE_OLD 0
  176. #define VPD_LEN 1024
  177. #define VPD_INFO_FLD_HDR_SIZE 3
  178. /*
  179. * csio_hw_seeprom_read - read a serial EEPROM location
  180. * @hw: hw to read
  181. * @addr: EEPROM virtual address
  182. * @data: where to store the read data
  183. *
  184. * Read a 32-bit word from a location in serial EEPROM using the card's PCI
  185. * VPD capability. Note that this function must be called with a virtual
  186. * address.
  187. */
  188. static int
  189. csio_hw_seeprom_read(struct csio_hw *hw, uint32_t addr, uint32_t *data)
  190. {
  191. uint16_t val = 0;
  192. int attempts = EEPROM_MAX_RD_POLL;
  193. uint32_t base = hw->params.pci.vpd_cap_addr;
  194. if (addr >= EEPROMVSIZE || (addr & 3))
  195. return -EINVAL;
  196. pci_write_config_word(hw->pdev, base + PCI_VPD_ADDR, (uint16_t)addr);
  197. do {
  198. udelay(10);
  199. pci_read_config_word(hw->pdev, base + PCI_VPD_ADDR, &val);
  200. } while (!(val & PCI_VPD_ADDR_F) && --attempts);
  201. if (!(val & PCI_VPD_ADDR_F)) {
  202. csio_err(hw, "reading EEPROM address 0x%x failed\n", addr);
  203. return -EINVAL;
  204. }
  205. pci_read_config_dword(hw->pdev, base + PCI_VPD_DATA, data);
  206. *data = le32_to_cpu(*(__le32 *)data);
  207. return 0;
  208. }
  209. /*
  210. * Partial EEPROM Vital Product Data structure. Includes only the ID and
  211. * VPD-R sections.
  212. */
  213. struct t4_vpd_hdr {
  214. u8 id_tag;
  215. u8 id_len[2];
  216. u8 id_data[ID_LEN];
  217. u8 vpdr_tag;
  218. u8 vpdr_len[2];
  219. };
  220. /*
  221. * csio_hw_get_vpd_keyword_val - Locates an information field keyword in
  222. * the VPD
  223. * @v: Pointer to buffered vpd data structure
  224. * @kw: The keyword to search for
  225. *
  226. * Returns the value of the information field keyword or
  227. * -EINVAL otherwise.
  228. */
  229. static int
  230. csio_hw_get_vpd_keyword_val(const struct t4_vpd_hdr *v, const char *kw)
  231. {
  232. int32_t i;
  233. int32_t offset , len;
  234. const uint8_t *buf = &v->id_tag;
  235. const uint8_t *vpdr_len = &v->vpdr_tag;
  236. offset = sizeof(struct t4_vpd_hdr);
  237. len = (uint16_t)vpdr_len[1] + ((uint16_t)vpdr_len[2] << 8);
  238. if (len + sizeof(struct t4_vpd_hdr) > VPD_LEN)
  239. return -EINVAL;
  240. for (i = offset; (i + VPD_INFO_FLD_HDR_SIZE) <= (offset + len);) {
  241. if (memcmp(buf + i , kw, 2) == 0) {
  242. i += VPD_INFO_FLD_HDR_SIZE;
  243. return i;
  244. }
  245. i += VPD_INFO_FLD_HDR_SIZE + buf[i+2];
  246. }
  247. return -EINVAL;
  248. }
  249. static int
  250. csio_pci_capability(struct pci_dev *pdev, int cap, int *pos)
  251. {
  252. *pos = pci_find_capability(pdev, cap);
  253. if (*pos)
  254. return 0;
  255. return -1;
  256. }
  257. /*
  258. * csio_hw_get_vpd_params - read VPD parameters from VPD EEPROM
  259. * @hw: HW module
  260. * @p: where to store the parameters
  261. *
  262. * Reads card parameters stored in VPD EEPROM.
  263. */
  264. static int
  265. csio_hw_get_vpd_params(struct csio_hw *hw, struct csio_vpd *p)
  266. {
  267. int i, ret, ec, sn, addr;
  268. uint8_t *vpd, csum;
  269. const struct t4_vpd_hdr *v;
  270. /* To get around compilation warning from strstrip */
  271. char *s;
  272. if (csio_is_valid_vpd(hw))
  273. return 0;
  274. ret = csio_pci_capability(hw->pdev, PCI_CAP_ID_VPD,
  275. &hw->params.pci.vpd_cap_addr);
  276. if (ret)
  277. return -EINVAL;
  278. vpd = kzalloc(VPD_LEN, GFP_ATOMIC);
  279. if (vpd == NULL)
  280. return -ENOMEM;
  281. /*
  282. * Card information normally starts at VPD_BASE but early cards had
  283. * it at 0.
  284. */
  285. ret = csio_hw_seeprom_read(hw, VPD_BASE, (uint32_t *)(vpd));
  286. addr = *vpd == 0x82 ? VPD_BASE : VPD_BASE_OLD;
  287. for (i = 0; i < VPD_LEN; i += 4) {
  288. ret = csio_hw_seeprom_read(hw, addr + i, (uint32_t *)(vpd + i));
  289. if (ret) {
  290. kfree(vpd);
  291. return ret;
  292. }
  293. }
  294. /* Reset the VPD flag! */
  295. hw->flags &= (~CSIO_HWF_VPD_VALID);
  296. v = (const struct t4_vpd_hdr *)vpd;
  297. #define FIND_VPD_KW(var, name) do { \
  298. var = csio_hw_get_vpd_keyword_val(v, name); \
  299. if (var < 0) { \
  300. csio_err(hw, "missing VPD keyword " name "\n"); \
  301. kfree(vpd); \
  302. return -EINVAL; \
  303. } \
  304. } while (0)
  305. FIND_VPD_KW(i, "RV");
  306. for (csum = 0; i >= 0; i--)
  307. csum += vpd[i];
  308. if (csum) {
  309. csio_err(hw, "corrupted VPD EEPROM, actual csum %u\n", csum);
  310. kfree(vpd);
  311. return -EINVAL;
  312. }
  313. FIND_VPD_KW(ec, "EC");
  314. FIND_VPD_KW(sn, "SN");
  315. #undef FIND_VPD_KW
  316. memcpy(p->id, v->id_data, ID_LEN);
  317. s = strstrip(p->id);
  318. memcpy(p->ec, vpd + ec, EC_LEN);
  319. s = strstrip(p->ec);
  320. i = vpd[sn - VPD_INFO_FLD_HDR_SIZE + 2];
  321. memcpy(p->sn, vpd + sn, min(i, SERNUM_LEN));
  322. s = strstrip(p->sn);
  323. csio_valid_vpd_copied(hw);
  324. kfree(vpd);
  325. return 0;
  326. }
  327. /*
  328. * csio_hw_sf1_read - read data from the serial flash
  329. * @hw: the HW module
  330. * @byte_cnt: number of bytes to read
  331. * @cont: whether another operation will be chained
  332. * @lock: whether to lock SF for PL access only
  333. * @valp: where to store the read data
  334. *
  335. * Reads up to 4 bytes of data from the serial flash. The location of
  336. * the read needs to be specified prior to calling this by issuing the
  337. * appropriate commands to the serial flash.
  338. */
  339. static int
  340. csio_hw_sf1_read(struct csio_hw *hw, uint32_t byte_cnt, int32_t cont,
  341. int32_t lock, uint32_t *valp)
  342. {
  343. int ret;
  344. if (!byte_cnt || byte_cnt > 4)
  345. return -EINVAL;
  346. if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
  347. return -EBUSY;
  348. csio_wr_reg32(hw, SF_LOCK_V(lock) | SF_CONT_V(cont) |
  349. BYTECNT_V(byte_cnt - 1), SF_OP_A);
  350. ret = csio_hw_wait_op_done_val(hw, SF_OP_A, SF_BUSY_F, 0, SF_ATTEMPTS,
  351. 10, NULL);
  352. if (!ret)
  353. *valp = csio_rd_reg32(hw, SF_DATA_A);
  354. return ret;
  355. }
  356. /*
  357. * csio_hw_sf1_write - write data to the serial flash
  358. * @hw: the HW module
  359. * @byte_cnt: number of bytes to write
  360. * @cont: whether another operation will be chained
  361. * @lock: whether to lock SF for PL access only
  362. * @val: value to write
  363. *
  364. * Writes up to 4 bytes of data to the serial flash. The location of
  365. * the write needs to be specified prior to calling this by issuing the
  366. * appropriate commands to the serial flash.
  367. */
  368. static int
  369. csio_hw_sf1_write(struct csio_hw *hw, uint32_t byte_cnt, uint32_t cont,
  370. int32_t lock, uint32_t val)
  371. {
  372. if (!byte_cnt || byte_cnt > 4)
  373. return -EINVAL;
  374. if (csio_rd_reg32(hw, SF_OP_A) & SF_BUSY_F)
  375. return -EBUSY;
  376. csio_wr_reg32(hw, val, SF_DATA_A);
  377. csio_wr_reg32(hw, SF_CONT_V(cont) | BYTECNT_V(byte_cnt - 1) |
  378. OP_V(1) | SF_LOCK_V(lock), SF_OP_A);
  379. return csio_hw_wait_op_done_val(hw, SF_OP_A, SF_BUSY_F, 0, SF_ATTEMPTS,
  380. 10, NULL);
  381. }
  382. /*
  383. * csio_hw_flash_wait_op - wait for a flash operation to complete
  384. * @hw: the HW module
  385. * @attempts: max number of polls of the status register
  386. * @delay: delay between polls in ms
  387. *
  388. * Wait for a flash operation to complete by polling the status register.
  389. */
  390. static int
  391. csio_hw_flash_wait_op(struct csio_hw *hw, int32_t attempts, int32_t delay)
  392. {
  393. int ret;
  394. uint32_t status;
  395. while (1) {
  396. ret = csio_hw_sf1_write(hw, 1, 1, 1, SF_RD_STATUS);
  397. if (ret != 0)
  398. return ret;
  399. ret = csio_hw_sf1_read(hw, 1, 0, 1, &status);
  400. if (ret != 0)
  401. return ret;
  402. if (!(status & 1))
  403. return 0;
  404. if (--attempts == 0)
  405. return -EAGAIN;
  406. if (delay)
  407. msleep(delay);
  408. }
  409. }
  410. /*
  411. * csio_hw_read_flash - read words from serial flash
  412. * @hw: the HW module
  413. * @addr: the start address for the read
  414. * @nwords: how many 32-bit words to read
  415. * @data: where to store the read data
  416. * @byte_oriented: whether to store data as bytes or as words
  417. *
  418. * Read the specified number of 32-bit words from the serial flash.
  419. * If @byte_oriented is set the read data is stored as a byte array
  420. * (i.e., big-endian), otherwise as 32-bit words in the platform's
  421. * natural endianess.
  422. */
  423. static int
  424. csio_hw_read_flash(struct csio_hw *hw, uint32_t addr, uint32_t nwords,
  425. uint32_t *data, int32_t byte_oriented)
  426. {
  427. int ret;
  428. if (addr + nwords * sizeof(uint32_t) > hw->params.sf_size || (addr & 3))
  429. return -EINVAL;
  430. addr = swab32(addr) | SF_RD_DATA_FAST;
  431. ret = csio_hw_sf1_write(hw, 4, 1, 0, addr);
  432. if (ret != 0)
  433. return ret;
  434. ret = csio_hw_sf1_read(hw, 1, 1, 0, data);
  435. if (ret != 0)
  436. return ret;
  437. for ( ; nwords; nwords--, data++) {
  438. ret = csio_hw_sf1_read(hw, 4, nwords > 1, nwords == 1, data);
  439. if (nwords == 1)
  440. csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
  441. if (ret)
  442. return ret;
  443. if (byte_oriented)
  444. *data = (__force __u32) htonl(*data);
  445. }
  446. return 0;
  447. }
  448. /*
  449. * csio_hw_write_flash - write up to a page of data to the serial flash
  450. * @hw: the hw
  451. * @addr: the start address to write
  452. * @n: length of data to write in bytes
  453. * @data: the data to write
  454. *
  455. * Writes up to a page of data (256 bytes) to the serial flash starting
  456. * at the given address. All the data must be written to the same page.
  457. */
  458. static int
  459. csio_hw_write_flash(struct csio_hw *hw, uint32_t addr,
  460. uint32_t n, const uint8_t *data)
  461. {
  462. int ret = -EINVAL;
  463. uint32_t buf[64];
  464. uint32_t i, c, left, val, offset = addr & 0xff;
  465. if (addr >= hw->params.sf_size || offset + n > SF_PAGE_SIZE)
  466. return -EINVAL;
  467. val = swab32(addr) | SF_PROG_PAGE;
  468. ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
  469. if (ret != 0)
  470. goto unlock;
  471. ret = csio_hw_sf1_write(hw, 4, 1, 1, val);
  472. if (ret != 0)
  473. goto unlock;
  474. for (left = n; left; left -= c) {
  475. c = min(left, 4U);
  476. for (val = 0, i = 0; i < c; ++i)
  477. val = (val << 8) + *data++;
  478. ret = csio_hw_sf1_write(hw, c, c != left, 1, val);
  479. if (ret)
  480. goto unlock;
  481. }
  482. ret = csio_hw_flash_wait_op(hw, 8, 1);
  483. if (ret)
  484. goto unlock;
  485. csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
  486. /* Read the page to verify the write succeeded */
  487. ret = csio_hw_read_flash(hw, addr & ~0xff, ARRAY_SIZE(buf), buf, 1);
  488. if (ret)
  489. return ret;
  490. if (memcmp(data - n, (uint8_t *)buf + offset, n)) {
  491. csio_err(hw,
  492. "failed to correctly write the flash page at %#x\n",
  493. addr);
  494. return -EINVAL;
  495. }
  496. return 0;
  497. unlock:
  498. csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
  499. return ret;
  500. }
  501. /*
  502. * csio_hw_flash_erase_sectors - erase a range of flash sectors
  503. * @hw: the HW module
  504. * @start: the first sector to erase
  505. * @end: the last sector to erase
  506. *
  507. * Erases the sectors in the given inclusive range.
  508. */
  509. static int
  510. csio_hw_flash_erase_sectors(struct csio_hw *hw, int32_t start, int32_t end)
  511. {
  512. int ret = 0;
  513. while (start <= end) {
  514. ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
  515. if (ret != 0)
  516. goto out;
  517. ret = csio_hw_sf1_write(hw, 4, 0, 1,
  518. SF_ERASE_SECTOR | (start << 8));
  519. if (ret != 0)
  520. goto out;
  521. ret = csio_hw_flash_wait_op(hw, 14, 500);
  522. if (ret != 0)
  523. goto out;
  524. start++;
  525. }
  526. out:
  527. if (ret)
  528. csio_err(hw, "erase of flash sector %d failed, error %d\n",
  529. start, ret);
  530. csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
  531. return 0;
  532. }
  533. static void
  534. csio_hw_print_fw_version(struct csio_hw *hw, char *str)
  535. {
  536. csio_info(hw, "%s: %u.%u.%u.%u\n", str,
  537. FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
  538. FW_HDR_FW_VER_MINOR_G(hw->fwrev),
  539. FW_HDR_FW_VER_MICRO_G(hw->fwrev),
  540. FW_HDR_FW_VER_BUILD_G(hw->fwrev));
  541. }
  542. /*
  543. * csio_hw_get_fw_version - read the firmware version
  544. * @hw: HW module
  545. * @vers: where to place the version
  546. *
  547. * Reads the FW version from flash.
  548. */
  549. static int
  550. csio_hw_get_fw_version(struct csio_hw *hw, uint32_t *vers)
  551. {
  552. return csio_hw_read_flash(hw, FLASH_FW_START +
  553. offsetof(struct fw_hdr, fw_ver), 1,
  554. vers, 0);
  555. }
  556. /*
  557. * csio_hw_get_tp_version - read the TP microcode version
  558. * @hw: HW module
  559. * @vers: where to place the version
  560. *
  561. * Reads the TP microcode version from flash.
  562. */
  563. static int
  564. csio_hw_get_tp_version(struct csio_hw *hw, u32 *vers)
  565. {
  566. return csio_hw_read_flash(hw, FLASH_FW_START +
  567. offsetof(struct fw_hdr, tp_microcode_ver), 1,
  568. vers, 0);
  569. }
  570. /*
  571. * csio_hw_fw_dload - download firmware.
  572. * @hw: HW module
  573. * @fw_data: firmware image to write.
  574. * @size: image size
  575. *
  576. * Write the supplied firmware image to the card's serial flash.
  577. */
  578. static int
  579. csio_hw_fw_dload(struct csio_hw *hw, uint8_t *fw_data, uint32_t size)
  580. {
  581. uint32_t csum;
  582. int32_t addr;
  583. int ret;
  584. uint32_t i;
  585. uint8_t first_page[SF_PAGE_SIZE];
  586. const __be32 *p = (const __be32 *)fw_data;
  587. struct fw_hdr *hdr = (struct fw_hdr *)fw_data;
  588. uint32_t sf_sec_size;
  589. if ((!hw->params.sf_size) || (!hw->params.sf_nsec)) {
  590. csio_err(hw, "Serial Flash data invalid\n");
  591. return -EINVAL;
  592. }
  593. if (!size) {
  594. csio_err(hw, "FW image has no data\n");
  595. return -EINVAL;
  596. }
  597. if (size & 511) {
  598. csio_err(hw, "FW image size not multiple of 512 bytes\n");
  599. return -EINVAL;
  600. }
  601. if (ntohs(hdr->len512) * 512 != size) {
  602. csio_err(hw, "FW image size differs from size in FW header\n");
  603. return -EINVAL;
  604. }
  605. if (size > FLASH_FW_MAX_SIZE) {
  606. csio_err(hw, "FW image too large, max is %u bytes\n",
  607. FLASH_FW_MAX_SIZE);
  608. return -EINVAL;
  609. }
  610. for (csum = 0, i = 0; i < size / sizeof(csum); i++)
  611. csum += ntohl(p[i]);
  612. if (csum != 0xffffffff) {
  613. csio_err(hw, "corrupted firmware image, checksum %#x\n", csum);
  614. return -EINVAL;
  615. }
  616. sf_sec_size = hw->params.sf_size / hw->params.sf_nsec;
  617. i = DIV_ROUND_UP(size, sf_sec_size); /* # of sectors spanned */
  618. csio_dbg(hw, "Erasing sectors... start:%d end:%d\n",
  619. FLASH_FW_START_SEC, FLASH_FW_START_SEC + i - 1);
  620. ret = csio_hw_flash_erase_sectors(hw, FLASH_FW_START_SEC,
  621. FLASH_FW_START_SEC + i - 1);
  622. if (ret) {
  623. csio_err(hw, "Flash Erase failed\n");
  624. goto out;
  625. }
  626. /*
  627. * We write the correct version at the end so the driver can see a bad
  628. * version if the FW write fails. Start by writing a copy of the
  629. * first page with a bad version.
  630. */
  631. memcpy(first_page, fw_data, SF_PAGE_SIZE);
  632. ((struct fw_hdr *)first_page)->fw_ver = htonl(0xffffffff);
  633. ret = csio_hw_write_flash(hw, FLASH_FW_START, SF_PAGE_SIZE, first_page);
  634. if (ret)
  635. goto out;
  636. csio_dbg(hw, "Writing Flash .. start:%d end:%d\n",
  637. FW_IMG_START, FW_IMG_START + size);
  638. addr = FLASH_FW_START;
  639. for (size -= SF_PAGE_SIZE; size; size -= SF_PAGE_SIZE) {
  640. addr += SF_PAGE_SIZE;
  641. fw_data += SF_PAGE_SIZE;
  642. ret = csio_hw_write_flash(hw, addr, SF_PAGE_SIZE, fw_data);
  643. if (ret)
  644. goto out;
  645. }
  646. ret = csio_hw_write_flash(hw,
  647. FLASH_FW_START +
  648. offsetof(struct fw_hdr, fw_ver),
  649. sizeof(hdr->fw_ver),
  650. (const uint8_t *)&hdr->fw_ver);
  651. out:
  652. if (ret)
  653. csio_err(hw, "firmware download failed, error %d\n", ret);
  654. return ret;
  655. }
  656. static int
  657. csio_hw_get_flash_params(struct csio_hw *hw)
  658. {
  659. /* Table for non-Numonix supported flash parts. Numonix parts are left
  660. * to the preexisting code. All flash parts have 64KB sectors.
  661. */
  662. static struct flash_desc {
  663. u32 vendor_and_model_id;
  664. u32 size_mb;
  665. } supported_flash[] = {
  666. { 0x150201, 4 << 20 }, /* Spansion 4MB S25FL032P */
  667. };
  668. u32 part, manufacturer;
  669. u32 density, size = 0;
  670. u32 flashid = 0;
  671. int ret;
  672. ret = csio_hw_sf1_write(hw, 1, 1, 0, SF_RD_ID);
  673. if (!ret)
  674. ret = csio_hw_sf1_read(hw, 3, 0, 1, &flashid);
  675. csio_wr_reg32(hw, 0, SF_OP_A); /* unlock SF */
  676. if (ret)
  677. return ret;
  678. /* Check to see if it's one of our non-standard supported Flash parts.
  679. */
  680. for (part = 0; part < ARRAY_SIZE(supported_flash); part++)
  681. if (supported_flash[part].vendor_and_model_id == flashid) {
  682. hw->params.sf_size = supported_flash[part].size_mb;
  683. hw->params.sf_nsec =
  684. hw->params.sf_size / SF_SEC_SIZE;
  685. goto found;
  686. }
  687. /* Decode Flash part size. The code below looks repetative with
  688. * common encodings, but that's not guaranteed in the JEDEC
  689. * specification for the Read JADEC ID command. The only thing that
  690. * we're guaranteed by the JADEC specification is where the
  691. * Manufacturer ID is in the returned result. After that each
  692. * Manufacturer ~could~ encode things completely differently.
  693. * Note, all Flash parts must have 64KB sectors.
  694. */
  695. manufacturer = flashid & 0xff;
  696. switch (manufacturer) {
  697. case 0x20: { /* Micron/Numonix */
  698. /* This Density -> Size decoding table is taken from Micron
  699. * Data Sheets.
  700. */
  701. density = (flashid >> 16) & 0xff;
  702. switch (density) {
  703. case 0x14 ... 0x19: /* 1MB - 32MB */
  704. size = 1 << density;
  705. break;
  706. case 0x20: /* 64MB */
  707. size = 1 << 26;
  708. break;
  709. case 0x21: /* 128MB */
  710. size = 1 << 27;
  711. break;
  712. case 0x22: /* 256MB */
  713. size = 1 << 28;
  714. }
  715. break;
  716. }
  717. case 0x9d: { /* ISSI -- Integrated Silicon Solution, Inc. */
  718. /* This Density -> Size decoding table is taken from ISSI
  719. * Data Sheets.
  720. */
  721. density = (flashid >> 16) & 0xff;
  722. switch (density) {
  723. case 0x16: /* 32 MB */
  724. size = 1 << 25;
  725. break;
  726. case 0x17: /* 64MB */
  727. size = 1 << 26;
  728. }
  729. break;
  730. }
  731. case 0xc2: /* Macronix */
  732. case 0xef: /* Winbond */ {
  733. /* This Density -> Size decoding table is taken from
  734. * Macronix and Winbond Data Sheets.
  735. */
  736. density = (flashid >> 16) & 0xff;
  737. switch (density) {
  738. case 0x17: /* 8MB */
  739. case 0x18: /* 16MB */
  740. size = 1 << density;
  741. }
  742. }
  743. }
  744. /* If we didn't recognize the FLASH part, that's no real issue: the
  745. * Hardware/Software contract says that Hardware will _*ALWAYS*_
  746. * use a FLASH part which is at least 4MB in size and has 64KB
  747. * sectors. The unrecognized FLASH part is likely to be much larger
  748. * than 4MB, but that's all we really need.
  749. */
  750. if (size == 0) {
  751. csio_warn(hw, "Unknown Flash Part, ID = %#x, assuming 4MB\n",
  752. flashid);
  753. size = 1 << 22;
  754. }
  755. /* Store decoded Flash size */
  756. hw->params.sf_size = size;
  757. hw->params.sf_nsec = size / SF_SEC_SIZE;
  758. found:
  759. if (hw->params.sf_size < FLASH_MIN_SIZE)
  760. csio_warn(hw, "WARNING: Flash Part ID %#x, size %#x < %#x\n",
  761. flashid, hw->params.sf_size, FLASH_MIN_SIZE);
  762. return 0;
  763. }
  764. /*****************************************************************************/
  765. /* HW State machine assists */
  766. /*****************************************************************************/
  767. static int
  768. csio_hw_dev_ready(struct csio_hw *hw)
  769. {
  770. uint32_t reg;
  771. int cnt = 6;
  772. int src_pf;
  773. while (((reg = csio_rd_reg32(hw, PL_WHOAMI_A)) == 0xFFFFFFFF) &&
  774. (--cnt != 0))
  775. mdelay(100);
  776. if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
  777. src_pf = SOURCEPF_G(reg);
  778. else
  779. src_pf = T6_SOURCEPF_G(reg);
  780. if ((cnt == 0) && (((int32_t)(src_pf) < 0) ||
  781. (src_pf >= CSIO_MAX_PFN))) {
  782. csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
  783. return -EIO;
  784. }
  785. hw->pfn = src_pf;
  786. return 0;
  787. }
  788. /*
  789. * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
  790. * @hw: HW module
  791. * @state: Device state
  792. *
  793. * FW_HELLO_CMD has to be polled for completion.
  794. */
  795. static int
  796. csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
  797. {
  798. struct csio_mb *mbp;
  799. int rv = 0;
  800. enum fw_retval retval;
  801. uint8_t mpfn;
  802. char state_str[16];
  803. int retries = FW_CMD_HELLO_RETRIES;
  804. memset(state_str, 0, sizeof(state_str));
  805. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  806. if (!mbp) {
  807. rv = -ENOMEM;
  808. CSIO_INC_STATS(hw, n_err_nomem);
  809. goto out;
  810. }
  811. retry:
  812. csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
  813. hw->pfn, CSIO_MASTER_MAY, NULL);
  814. rv = csio_mb_issue(hw, mbp);
  815. if (rv) {
  816. csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
  817. goto out_free_mb;
  818. }
  819. csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
  820. if (retval != FW_SUCCESS) {
  821. csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
  822. rv = -EINVAL;
  823. goto out_free_mb;
  824. }
  825. /* Firmware has designated us to be master */
  826. if (hw->pfn == mpfn) {
  827. hw->flags |= CSIO_HWF_MASTER;
  828. } else if (*state == CSIO_DEV_STATE_UNINIT) {
  829. /*
  830. * If we're not the Master PF then we need to wait around for
  831. * the Master PF Driver to finish setting up the adapter.
  832. *
  833. * Note that we also do this wait if we're a non-Master-capable
  834. * PF and there is no current Master PF; a Master PF may show up
  835. * momentarily and we wouldn't want to fail pointlessly. (This
  836. * can happen when an OS loads lots of different drivers rapidly
  837. * at the same time). In this case, the Master PF returned by
  838. * the firmware will be PCIE_FW_MASTER_MASK so the test below
  839. * will work ...
  840. */
  841. int waiting = FW_CMD_HELLO_TIMEOUT;
  842. /*
  843. * Wait for the firmware to either indicate an error or
  844. * initialized state. If we see either of these we bail out
  845. * and report the issue to the caller. If we exhaust the
  846. * "hello timeout" and we haven't exhausted our retries, try
  847. * again. Otherwise bail with a timeout error.
  848. */
  849. for (;;) {
  850. uint32_t pcie_fw;
  851. spin_unlock_irq(&hw->lock);
  852. msleep(50);
  853. spin_lock_irq(&hw->lock);
  854. waiting -= 50;
  855. /*
  856. * If neither Error nor Initialialized are indicated
  857. * by the firmware keep waiting till we exaust our
  858. * timeout ... and then retry if we haven't exhausted
  859. * our retries ...
  860. */
  861. pcie_fw = csio_rd_reg32(hw, PCIE_FW_A);
  862. if (!(pcie_fw & (PCIE_FW_ERR_F|PCIE_FW_INIT_F))) {
  863. if (waiting <= 0) {
  864. if (retries-- > 0)
  865. goto retry;
  866. rv = -ETIMEDOUT;
  867. break;
  868. }
  869. continue;
  870. }
  871. /*
  872. * We either have an Error or Initialized condition
  873. * report errors preferentially.
  874. */
  875. if (state) {
  876. if (pcie_fw & PCIE_FW_ERR_F) {
  877. *state = CSIO_DEV_STATE_ERR;
  878. rv = -ETIMEDOUT;
  879. } else if (pcie_fw & PCIE_FW_INIT_F)
  880. *state = CSIO_DEV_STATE_INIT;
  881. }
  882. /*
  883. * If we arrived before a Master PF was selected and
  884. * there's not a valid Master PF, grab its identity
  885. * for our caller.
  886. */
  887. if (mpfn == PCIE_FW_MASTER_M &&
  888. (pcie_fw & PCIE_FW_MASTER_VLD_F))
  889. mpfn = PCIE_FW_MASTER_G(pcie_fw);
  890. break;
  891. }
  892. hw->flags &= ~CSIO_HWF_MASTER;
  893. }
  894. switch (*state) {
  895. case CSIO_DEV_STATE_UNINIT:
  896. strcpy(state_str, "Initializing");
  897. break;
  898. case CSIO_DEV_STATE_INIT:
  899. strcpy(state_str, "Initialized");
  900. break;
  901. case CSIO_DEV_STATE_ERR:
  902. strcpy(state_str, "Error");
  903. break;
  904. default:
  905. strcpy(state_str, "Unknown");
  906. break;
  907. }
  908. if (hw->pfn == mpfn)
  909. csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
  910. hw->pfn, state_str);
  911. else
  912. csio_info(hw,
  913. "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
  914. hw->pfn, mpfn, state_str);
  915. out_free_mb:
  916. mempool_free(mbp, hw->mb_mempool);
  917. out:
  918. return rv;
  919. }
  920. /*
  921. * csio_do_bye - Perform the BYE FW Mailbox command and process response.
  922. * @hw: HW module
  923. *
  924. */
  925. static int
  926. csio_do_bye(struct csio_hw *hw)
  927. {
  928. struct csio_mb *mbp;
  929. enum fw_retval retval;
  930. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  931. if (!mbp) {
  932. CSIO_INC_STATS(hw, n_err_nomem);
  933. return -ENOMEM;
  934. }
  935. csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  936. if (csio_mb_issue(hw, mbp)) {
  937. csio_err(hw, "Issue of BYE command failed\n");
  938. mempool_free(mbp, hw->mb_mempool);
  939. return -EINVAL;
  940. }
  941. retval = csio_mb_fw_retval(mbp);
  942. if (retval != FW_SUCCESS) {
  943. mempool_free(mbp, hw->mb_mempool);
  944. return -EINVAL;
  945. }
  946. mempool_free(mbp, hw->mb_mempool);
  947. return 0;
  948. }
  949. /*
  950. * csio_do_reset- Perform the device reset.
  951. * @hw: HW module
  952. * @fw_rst: FW reset
  953. *
  954. * If fw_rst is set, issues FW reset mbox cmd otherwise
  955. * does PIO reset.
  956. * Performs reset of the function.
  957. */
  958. static int
  959. csio_do_reset(struct csio_hw *hw, bool fw_rst)
  960. {
  961. struct csio_mb *mbp;
  962. enum fw_retval retval;
  963. if (!fw_rst) {
  964. /* PIO reset */
  965. csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
  966. mdelay(2000);
  967. return 0;
  968. }
  969. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  970. if (!mbp) {
  971. CSIO_INC_STATS(hw, n_err_nomem);
  972. return -ENOMEM;
  973. }
  974. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  975. PIORSTMODE_F | PIORST_F, 0, NULL);
  976. if (csio_mb_issue(hw, mbp)) {
  977. csio_err(hw, "Issue of RESET command failed.n");
  978. mempool_free(mbp, hw->mb_mempool);
  979. return -EINVAL;
  980. }
  981. retval = csio_mb_fw_retval(mbp);
  982. if (retval != FW_SUCCESS) {
  983. csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
  984. mempool_free(mbp, hw->mb_mempool);
  985. return -EINVAL;
  986. }
  987. mempool_free(mbp, hw->mb_mempool);
  988. return 0;
  989. }
  990. static int
  991. csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
  992. {
  993. struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
  994. uint16_t caps;
  995. caps = ntohs(rsp->fcoecaps);
  996. if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
  997. csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
  998. return -EINVAL;
  999. }
  1000. if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
  1001. csio_err(hw, "No FCoE Control Offload capability\n");
  1002. return -EINVAL;
  1003. }
  1004. return 0;
  1005. }
  1006. /*
  1007. * csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
  1008. * @hw: the HW module
  1009. * @mbox: mailbox to use for the FW RESET command (if desired)
  1010. * @force: force uP into RESET even if FW RESET command fails
  1011. *
  1012. * Issues a RESET command to firmware (if desired) with a HALT indication
  1013. * and then puts the microprocessor into RESET state. The RESET command
  1014. * will only be issued if a legitimate mailbox is provided (mbox <=
  1015. * PCIE_FW_MASTER_MASK).
  1016. *
  1017. * This is generally used in order for the host to safely manipulate the
  1018. * adapter without fear of conflicting with whatever the firmware might
  1019. * be doing. The only way out of this state is to RESTART the firmware
  1020. * ...
  1021. */
  1022. static int
  1023. csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
  1024. {
  1025. enum fw_retval retval = 0;
  1026. /*
  1027. * If a legitimate mailbox is provided, issue a RESET command
  1028. * with a HALT indication.
  1029. */
  1030. if (mbox <= PCIE_FW_MASTER_M) {
  1031. struct csio_mb *mbp;
  1032. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1033. if (!mbp) {
  1034. CSIO_INC_STATS(hw, n_err_nomem);
  1035. return -ENOMEM;
  1036. }
  1037. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1038. PIORSTMODE_F | PIORST_F, FW_RESET_CMD_HALT_F,
  1039. NULL);
  1040. if (csio_mb_issue(hw, mbp)) {
  1041. csio_err(hw, "Issue of RESET command failed!\n");
  1042. mempool_free(mbp, hw->mb_mempool);
  1043. return -EINVAL;
  1044. }
  1045. retval = csio_mb_fw_retval(mbp);
  1046. mempool_free(mbp, hw->mb_mempool);
  1047. }
  1048. /*
  1049. * Normally we won't complete the operation if the firmware RESET
  1050. * command fails but if our caller insists we'll go ahead and put the
  1051. * uP into RESET. This can be useful if the firmware is hung or even
  1052. * missing ... We'll have to take the risk of putting the uP into
  1053. * RESET without the cooperation of firmware in that case.
  1054. *
  1055. * We also force the firmware's HALT flag to be on in case we bypassed
  1056. * the firmware RESET command above or we're dealing with old firmware
  1057. * which doesn't have the HALT capability. This will serve as a flag
  1058. * for the incoming firmware to know that it's coming out of a HALT
  1059. * rather than a RESET ... if it's new enough to understand that ...
  1060. */
  1061. if (retval == 0 || force) {
  1062. csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, UPCRST_F);
  1063. csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F,
  1064. PCIE_FW_HALT_F);
  1065. }
  1066. /*
  1067. * And we always return the result of the firmware RESET command
  1068. * even when we force the uP into RESET ...
  1069. */
  1070. return retval ? -EINVAL : 0;
  1071. }
  1072. /*
  1073. * csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
  1074. * @hw: the HW module
  1075. * @reset: if we want to do a RESET to restart things
  1076. *
  1077. * Restart firmware previously halted by csio_hw_fw_halt(). On successful
  1078. * return the previous PF Master remains as the new PF Master and there
  1079. * is no need to issue a new HELLO command, etc.
  1080. *
  1081. * We do this in two ways:
  1082. *
  1083. * 1. If we're dealing with newer firmware we'll simply want to take
  1084. * the chip's microprocessor out of RESET. This will cause the
  1085. * firmware to start up from its start vector. And then we'll loop
  1086. * until the firmware indicates it's started again (PCIE_FW.HALT
  1087. * reset to 0) or we timeout.
  1088. *
  1089. * 2. If we're dealing with older firmware then we'll need to RESET
  1090. * the chip since older firmware won't recognize the PCIE_FW.HALT
  1091. * flag and automatically RESET itself on startup.
  1092. */
  1093. static int
  1094. csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
  1095. {
  1096. if (reset) {
  1097. /*
  1098. * Since we're directing the RESET instead of the firmware
  1099. * doing it automatically, we need to clear the PCIE_FW.HALT
  1100. * bit.
  1101. */
  1102. csio_set_reg_field(hw, PCIE_FW_A, PCIE_FW_HALT_F, 0);
  1103. /*
  1104. * If we've been given a valid mailbox, first try to get the
  1105. * firmware to do the RESET. If that works, great and we can
  1106. * return success. Otherwise, if we haven't been given a
  1107. * valid mailbox or the RESET command failed, fall back to
  1108. * hitting the chip with a hammer.
  1109. */
  1110. if (mbox <= PCIE_FW_MASTER_M) {
  1111. csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
  1112. msleep(100);
  1113. if (csio_do_reset(hw, true) == 0)
  1114. return 0;
  1115. }
  1116. csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
  1117. msleep(2000);
  1118. } else {
  1119. int ms;
  1120. csio_set_reg_field(hw, CIM_BOOT_CFG_A, UPCRST_F, 0);
  1121. for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
  1122. if (!(csio_rd_reg32(hw, PCIE_FW_A) & PCIE_FW_HALT_F))
  1123. return 0;
  1124. msleep(100);
  1125. ms += 100;
  1126. }
  1127. return -ETIMEDOUT;
  1128. }
  1129. return 0;
  1130. }
  1131. /*
  1132. * csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
  1133. * @hw: the HW module
  1134. * @mbox: mailbox to use for the FW RESET command (if desired)
  1135. * @fw_data: the firmware image to write
  1136. * @size: image size
  1137. * @force: force upgrade even if firmware doesn't cooperate
  1138. *
  1139. * Perform all of the steps necessary for upgrading an adapter's
  1140. * firmware image. Normally this requires the cooperation of the
  1141. * existing firmware in order to halt all existing activities
  1142. * but if an invalid mailbox token is passed in we skip that step
  1143. * (though we'll still put the adapter microprocessor into RESET in
  1144. * that case).
  1145. *
  1146. * On successful return the new firmware will have been loaded and
  1147. * the adapter will have been fully RESET losing all previous setup
  1148. * state. On unsuccessful return the adapter may be completely hosed ...
  1149. * positive errno indicates that the adapter is ~probably~ intact, a
  1150. * negative errno indicates that things are looking bad ...
  1151. */
  1152. static int
  1153. csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
  1154. const u8 *fw_data, uint32_t size, int32_t force)
  1155. {
  1156. const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
  1157. int reset, ret;
  1158. ret = csio_hw_fw_halt(hw, mbox, force);
  1159. if (ret != 0 && !force)
  1160. return ret;
  1161. ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
  1162. if (ret != 0)
  1163. return ret;
  1164. /*
  1165. * Older versions of the firmware don't understand the new
  1166. * PCIE_FW.HALT flag and so won't know to perform a RESET when they
  1167. * restart. So for newly loaded older firmware we'll have to do the
  1168. * RESET for it so it starts up on a clean slate. We can tell if
  1169. * the newly loaded firmware will handle this right by checking
  1170. * its header flags to see if it advertises the capability.
  1171. */
  1172. reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
  1173. return csio_hw_fw_restart(hw, mbox, reset);
  1174. }
  1175. /*
  1176. * csio_get_device_params - Get device parameters.
  1177. * @hw: HW module
  1178. *
  1179. */
  1180. static int
  1181. csio_get_device_params(struct csio_hw *hw)
  1182. {
  1183. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1184. struct csio_mb *mbp;
  1185. enum fw_retval retval;
  1186. u32 param[6];
  1187. int i, j = 0;
  1188. /* Initialize portids to -1 */
  1189. for (i = 0; i < CSIO_MAX_PPORTS; i++)
  1190. hw->pport[i].portid = -1;
  1191. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1192. if (!mbp) {
  1193. CSIO_INC_STATS(hw, n_err_nomem);
  1194. return -ENOMEM;
  1195. }
  1196. /* Get port vec information. */
  1197. param[0] = FW_PARAM_DEV(PORTVEC);
  1198. /* Get Core clock. */
  1199. param[1] = FW_PARAM_DEV(CCLK);
  1200. /* Get EQ id start and end. */
  1201. param[2] = FW_PARAM_PFVF(EQ_START);
  1202. param[3] = FW_PARAM_PFVF(EQ_END);
  1203. /* Get IQ id start and end. */
  1204. param[4] = FW_PARAM_PFVF(IQFLINT_START);
  1205. param[5] = FW_PARAM_PFVF(IQFLINT_END);
  1206. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1207. ARRAY_SIZE(param), param, NULL, false, NULL);
  1208. if (csio_mb_issue(hw, mbp)) {
  1209. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1210. mempool_free(mbp, hw->mb_mempool);
  1211. return -EINVAL;
  1212. }
  1213. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1214. ARRAY_SIZE(param), param);
  1215. if (retval != FW_SUCCESS) {
  1216. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1217. retval);
  1218. mempool_free(mbp, hw->mb_mempool);
  1219. return -EINVAL;
  1220. }
  1221. /* cache the information. */
  1222. hw->port_vec = param[0];
  1223. hw->vpd.cclk = param[1];
  1224. wrm->fw_eq_start = param[2];
  1225. wrm->fw_iq_start = param[4];
  1226. /* Using FW configured max iqs & eqs */
  1227. if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
  1228. !csio_is_hw_master(hw)) {
  1229. hw->cfg_niq = param[5] - param[4] + 1;
  1230. hw->cfg_neq = param[3] - param[2] + 1;
  1231. csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
  1232. hw->cfg_niq, hw->cfg_neq);
  1233. }
  1234. hw->port_vec &= csio_port_mask;
  1235. hw->num_pports = hweight32(hw->port_vec);
  1236. csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
  1237. hw->port_vec, hw->num_pports);
  1238. for (i = 0; i < hw->num_pports; i++) {
  1239. while ((hw->port_vec & (1 << j)) == 0)
  1240. j++;
  1241. hw->pport[i].portid = j++;
  1242. csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
  1243. }
  1244. mempool_free(mbp, hw->mb_mempool);
  1245. return 0;
  1246. }
  1247. /*
  1248. * csio_config_device_caps - Get and set device capabilities.
  1249. * @hw: HW module
  1250. *
  1251. */
  1252. static int
  1253. csio_config_device_caps(struct csio_hw *hw)
  1254. {
  1255. struct csio_mb *mbp;
  1256. enum fw_retval retval;
  1257. int rv = -EINVAL;
  1258. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1259. if (!mbp) {
  1260. CSIO_INC_STATS(hw, n_err_nomem);
  1261. return -ENOMEM;
  1262. }
  1263. /* Get device capabilities */
  1264. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
  1265. if (csio_mb_issue(hw, mbp)) {
  1266. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
  1267. goto out;
  1268. }
  1269. retval = csio_mb_fw_retval(mbp);
  1270. if (retval != FW_SUCCESS) {
  1271. csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
  1272. goto out;
  1273. }
  1274. /* Validate device capabilities */
  1275. rv = csio_hw_validate_caps(hw, mbp);
  1276. if (rv != 0)
  1277. goto out;
  1278. /* Don't config device capabilities if already configured */
  1279. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  1280. rv = 0;
  1281. goto out;
  1282. }
  1283. /* Write back desired device capabilities */
  1284. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
  1285. false, true, NULL);
  1286. if (csio_mb_issue(hw, mbp)) {
  1287. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
  1288. goto out;
  1289. }
  1290. retval = csio_mb_fw_retval(mbp);
  1291. if (retval != FW_SUCCESS) {
  1292. csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
  1293. goto out;
  1294. }
  1295. rv = 0;
  1296. out:
  1297. mempool_free(mbp, hw->mb_mempool);
  1298. return rv;
  1299. }
  1300. static inline enum cc_fec fwcap_to_cc_fec(fw_port_cap32_t fw_fec)
  1301. {
  1302. enum cc_fec cc_fec = 0;
  1303. if (fw_fec & FW_PORT_CAP32_FEC_RS)
  1304. cc_fec |= FEC_RS;
  1305. if (fw_fec & FW_PORT_CAP32_FEC_BASER_RS)
  1306. cc_fec |= FEC_BASER_RS;
  1307. return cc_fec;
  1308. }
  1309. static inline fw_port_cap32_t cc_to_fwcap_pause(enum cc_pause cc_pause)
  1310. {
  1311. fw_port_cap32_t fw_pause = 0;
  1312. if (cc_pause & PAUSE_RX)
  1313. fw_pause |= FW_PORT_CAP32_FC_RX;
  1314. if (cc_pause & PAUSE_TX)
  1315. fw_pause |= FW_PORT_CAP32_FC_TX;
  1316. return fw_pause;
  1317. }
  1318. static inline fw_port_cap32_t cc_to_fwcap_fec(enum cc_fec cc_fec)
  1319. {
  1320. fw_port_cap32_t fw_fec = 0;
  1321. if (cc_fec & FEC_RS)
  1322. fw_fec |= FW_PORT_CAP32_FEC_RS;
  1323. if (cc_fec & FEC_BASER_RS)
  1324. fw_fec |= FW_PORT_CAP32_FEC_BASER_RS;
  1325. return fw_fec;
  1326. }
  1327. /**
  1328. * fwcap_to_fwspeed - return highest speed in Port Capabilities
  1329. * @acaps: advertised Port Capabilities
  1330. *
  1331. * Get the highest speed for the port from the advertised Port
  1332. * Capabilities.
  1333. */
  1334. fw_port_cap32_t fwcap_to_fwspeed(fw_port_cap32_t acaps)
  1335. {
  1336. #define TEST_SPEED_RETURN(__caps_speed) \
  1337. do { \
  1338. if (acaps & FW_PORT_CAP32_SPEED_##__caps_speed) \
  1339. return FW_PORT_CAP32_SPEED_##__caps_speed; \
  1340. } while (0)
  1341. TEST_SPEED_RETURN(400G);
  1342. TEST_SPEED_RETURN(200G);
  1343. TEST_SPEED_RETURN(100G);
  1344. TEST_SPEED_RETURN(50G);
  1345. TEST_SPEED_RETURN(40G);
  1346. TEST_SPEED_RETURN(25G);
  1347. TEST_SPEED_RETURN(10G);
  1348. TEST_SPEED_RETURN(1G);
  1349. TEST_SPEED_RETURN(100M);
  1350. #undef TEST_SPEED_RETURN
  1351. return 0;
  1352. }
  1353. /**
  1354. * fwcaps16_to_caps32 - convert 16-bit Port Capabilities to 32-bits
  1355. * @caps16: a 16-bit Port Capabilities value
  1356. *
  1357. * Returns the equivalent 32-bit Port Capabilities value.
  1358. */
  1359. fw_port_cap32_t fwcaps16_to_caps32(fw_port_cap16_t caps16)
  1360. {
  1361. fw_port_cap32_t caps32 = 0;
  1362. #define CAP16_TO_CAP32(__cap) \
  1363. do { \
  1364. if (caps16 & FW_PORT_CAP_##__cap) \
  1365. caps32 |= FW_PORT_CAP32_##__cap; \
  1366. } while (0)
  1367. CAP16_TO_CAP32(SPEED_100M);
  1368. CAP16_TO_CAP32(SPEED_1G);
  1369. CAP16_TO_CAP32(SPEED_25G);
  1370. CAP16_TO_CAP32(SPEED_10G);
  1371. CAP16_TO_CAP32(SPEED_40G);
  1372. CAP16_TO_CAP32(SPEED_100G);
  1373. CAP16_TO_CAP32(FC_RX);
  1374. CAP16_TO_CAP32(FC_TX);
  1375. CAP16_TO_CAP32(ANEG);
  1376. CAP16_TO_CAP32(MDIAUTO);
  1377. CAP16_TO_CAP32(MDISTRAIGHT);
  1378. CAP16_TO_CAP32(FEC_RS);
  1379. CAP16_TO_CAP32(FEC_BASER_RS);
  1380. CAP16_TO_CAP32(802_3_PAUSE);
  1381. CAP16_TO_CAP32(802_3_ASM_DIR);
  1382. #undef CAP16_TO_CAP32
  1383. return caps32;
  1384. }
  1385. /**
  1386. * fwcaps32_to_caps16 - convert 32-bit Port Capabilities to 16-bits
  1387. * @caps32: a 32-bit Port Capabilities value
  1388. *
  1389. * Returns the equivalent 16-bit Port Capabilities value. Note that
  1390. * not all 32-bit Port Capabilities can be represented in the 16-bit
  1391. * Port Capabilities and some fields/values may not make it.
  1392. */
  1393. fw_port_cap16_t fwcaps32_to_caps16(fw_port_cap32_t caps32)
  1394. {
  1395. fw_port_cap16_t caps16 = 0;
  1396. #define CAP32_TO_CAP16(__cap) \
  1397. do { \
  1398. if (caps32 & FW_PORT_CAP32_##__cap) \
  1399. caps16 |= FW_PORT_CAP_##__cap; \
  1400. } while (0)
  1401. CAP32_TO_CAP16(SPEED_100M);
  1402. CAP32_TO_CAP16(SPEED_1G);
  1403. CAP32_TO_CAP16(SPEED_10G);
  1404. CAP32_TO_CAP16(SPEED_25G);
  1405. CAP32_TO_CAP16(SPEED_40G);
  1406. CAP32_TO_CAP16(SPEED_100G);
  1407. CAP32_TO_CAP16(FC_RX);
  1408. CAP32_TO_CAP16(FC_TX);
  1409. CAP32_TO_CAP16(802_3_PAUSE);
  1410. CAP32_TO_CAP16(802_3_ASM_DIR);
  1411. CAP32_TO_CAP16(ANEG);
  1412. CAP32_TO_CAP16(FORCE_PAUSE);
  1413. CAP32_TO_CAP16(MDIAUTO);
  1414. CAP32_TO_CAP16(MDISTRAIGHT);
  1415. CAP32_TO_CAP16(FEC_RS);
  1416. CAP32_TO_CAP16(FEC_BASER_RS);
  1417. #undef CAP32_TO_CAP16
  1418. return caps16;
  1419. }
  1420. /**
  1421. * lstatus_to_fwcap - translate old lstatus to 32-bit Port Capabilities
  1422. * @lstatus: old FW_PORT_ACTION_GET_PORT_INFO lstatus value
  1423. *
  1424. * Translates old FW_PORT_ACTION_GET_PORT_INFO lstatus field into new
  1425. * 32-bit Port Capabilities value.
  1426. */
  1427. fw_port_cap32_t lstatus_to_fwcap(u32 lstatus)
  1428. {
  1429. fw_port_cap32_t linkattr = 0;
  1430. /* The format of the Link Status in the old
  1431. * 16-bit Port Information message isn't the same as the
  1432. * 16-bit Port Capabilities bitfield used everywhere else.
  1433. */
  1434. if (lstatus & FW_PORT_CMD_RXPAUSE_F)
  1435. linkattr |= FW_PORT_CAP32_FC_RX;
  1436. if (lstatus & FW_PORT_CMD_TXPAUSE_F)
  1437. linkattr |= FW_PORT_CAP32_FC_TX;
  1438. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100M))
  1439. linkattr |= FW_PORT_CAP32_SPEED_100M;
  1440. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_1G))
  1441. linkattr |= FW_PORT_CAP32_SPEED_1G;
  1442. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_10G))
  1443. linkattr |= FW_PORT_CAP32_SPEED_10G;
  1444. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_25G))
  1445. linkattr |= FW_PORT_CAP32_SPEED_25G;
  1446. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_40G))
  1447. linkattr |= FW_PORT_CAP32_SPEED_40G;
  1448. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100G))
  1449. linkattr |= FW_PORT_CAP32_SPEED_100G;
  1450. return linkattr;
  1451. }
  1452. /**
  1453. * csio_init_link_config - initialize a link's SW state
  1454. * @lc: pointer to structure holding the link state
  1455. * @pcaps: link Port Capabilities
  1456. * @acaps: link current Advertised Port Capabilities
  1457. *
  1458. * Initializes the SW state maintained for each link, including the link's
  1459. * capabilities and default speed/flow-control/autonegotiation settings.
  1460. */
  1461. static void csio_init_link_config(struct link_config *lc, fw_port_cap32_t pcaps,
  1462. fw_port_cap32_t acaps)
  1463. {
  1464. lc->pcaps = pcaps;
  1465. lc->def_acaps = acaps;
  1466. lc->lpacaps = 0;
  1467. lc->speed_caps = 0;
  1468. lc->speed = 0;
  1469. lc->requested_fc = PAUSE_RX | PAUSE_TX;
  1470. lc->fc = lc->requested_fc;
  1471. /*
  1472. * For Forward Error Control, we default to whatever the Firmware
  1473. * tells us the Link is currently advertising.
  1474. */
  1475. lc->requested_fec = FEC_AUTO;
  1476. lc->fec = fwcap_to_cc_fec(lc->def_acaps);
  1477. /* If the Port is capable of Auto-Negtotiation, initialize it as
  1478. * "enabled" and copy over all of the Physical Port Capabilities
  1479. * to the Advertised Port Capabilities. Otherwise mark it as
  1480. * Auto-Negotiate disabled and select the highest supported speed
  1481. * for the link. Note parallel structure in t4_link_l1cfg_core()
  1482. * and t4_handle_get_port_info().
  1483. */
  1484. if (lc->pcaps & FW_PORT_CAP32_ANEG) {
  1485. lc->acaps = lc->pcaps & ADVERT_MASK;
  1486. lc->autoneg = AUTONEG_ENABLE;
  1487. lc->requested_fc |= PAUSE_AUTONEG;
  1488. } else {
  1489. lc->acaps = 0;
  1490. lc->autoneg = AUTONEG_DISABLE;
  1491. }
  1492. }
  1493. static void csio_link_l1cfg(struct link_config *lc, uint16_t fw_caps,
  1494. uint32_t *rcaps)
  1495. {
  1496. unsigned int fw_mdi = FW_PORT_CAP32_MDI_V(FW_PORT_CAP32_MDI_AUTO);
  1497. fw_port_cap32_t fw_fc, cc_fec, fw_fec, lrcap;
  1498. lc->link_ok = 0;
  1499. /*
  1500. * Convert driver coding of Pause Frame Flow Control settings into the
  1501. * Firmware's API.
  1502. */
  1503. fw_fc = cc_to_fwcap_pause(lc->requested_fc);
  1504. /*
  1505. * Convert Common Code Forward Error Control settings into the
  1506. * Firmware's API. If the current Requested FEC has "Automatic"
  1507. * (IEEE 802.3) specified, then we use whatever the Firmware
  1508. * sent us as part of it's IEEE 802.3-based interpratation of
  1509. * the Transceiver Module EPROM FEC parameters. Otherwise we
  1510. * use whatever is in the current Requested FEC settings.
  1511. */
  1512. if (lc->requested_fec & FEC_AUTO)
  1513. cc_fec = fwcap_to_cc_fec(lc->def_acaps);
  1514. else
  1515. cc_fec = lc->requested_fec;
  1516. fw_fec = cc_to_fwcap_fec(cc_fec);
  1517. /* Figure out what our Requested Port Capabilities are going to be.
  1518. * Note parallel structure in t4_handle_get_port_info() and
  1519. * init_link_config().
  1520. */
  1521. if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
  1522. lrcap = (lc->pcaps & ADVERT_MASK) | fw_fc | fw_fec;
  1523. lc->fc = lc->requested_fc & ~PAUSE_AUTONEG;
  1524. lc->fec = cc_fec;
  1525. } else if (lc->autoneg == AUTONEG_DISABLE) {
  1526. lrcap = lc->speed_caps | fw_fc | fw_fec | fw_mdi;
  1527. lc->fc = lc->requested_fc & ~PAUSE_AUTONEG;
  1528. lc->fec = cc_fec;
  1529. } else {
  1530. lrcap = lc->acaps | fw_fc | fw_fec | fw_mdi;
  1531. }
  1532. *rcaps = lrcap;
  1533. }
  1534. /*
  1535. * csio_enable_ports - Bring up all available ports.
  1536. * @hw: HW module.
  1537. *
  1538. */
  1539. static int
  1540. csio_enable_ports(struct csio_hw *hw)
  1541. {
  1542. struct csio_mb *mbp;
  1543. u16 fw_caps = FW_CAPS_UNKNOWN;
  1544. enum fw_retval retval;
  1545. uint8_t portid;
  1546. fw_port_cap32_t pcaps, acaps, rcaps;
  1547. int i;
  1548. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1549. if (!mbp) {
  1550. CSIO_INC_STATS(hw, n_err_nomem);
  1551. return -ENOMEM;
  1552. }
  1553. for (i = 0; i < hw->num_pports; i++) {
  1554. portid = hw->pport[i].portid;
  1555. if (fw_caps == FW_CAPS_UNKNOWN) {
  1556. u32 param, val;
  1557. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
  1558. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_PORT_CAPS32));
  1559. val = 1;
  1560. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1561. hw->pfn, 0, 1, &param, &val, true,
  1562. NULL);
  1563. if (csio_mb_issue(hw, mbp)) {
  1564. csio_err(hw, "failed to issue FW_PARAMS_CMD(r) port:%d\n",
  1565. portid);
  1566. mempool_free(mbp, hw->mb_mempool);
  1567. return -EINVAL;
  1568. }
  1569. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1570. 0, NULL);
  1571. fw_caps = retval ? FW_CAPS16 : FW_CAPS32;
  1572. }
  1573. /* Read PORT information */
  1574. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
  1575. false, 0, fw_caps, NULL);
  1576. if (csio_mb_issue(hw, mbp)) {
  1577. csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
  1578. portid);
  1579. mempool_free(mbp, hw->mb_mempool);
  1580. return -EINVAL;
  1581. }
  1582. csio_mb_process_read_port_rsp(hw, mbp, &retval, fw_caps,
  1583. &pcaps, &acaps);
  1584. if (retval != FW_SUCCESS) {
  1585. csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
  1586. portid, retval);
  1587. mempool_free(mbp, hw->mb_mempool);
  1588. return -EINVAL;
  1589. }
  1590. csio_init_link_config(&hw->pport[i].link_cfg, pcaps, acaps);
  1591. csio_link_l1cfg(&hw->pport[i].link_cfg, fw_caps, &rcaps);
  1592. /* Write back PORT information */
  1593. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
  1594. true, rcaps, fw_caps, NULL);
  1595. if (csio_mb_issue(hw, mbp)) {
  1596. csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
  1597. portid);
  1598. mempool_free(mbp, hw->mb_mempool);
  1599. return -EINVAL;
  1600. }
  1601. retval = csio_mb_fw_retval(mbp);
  1602. if (retval != FW_SUCCESS) {
  1603. csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
  1604. portid, retval);
  1605. mempool_free(mbp, hw->mb_mempool);
  1606. return -EINVAL;
  1607. }
  1608. } /* For all ports */
  1609. mempool_free(mbp, hw->mb_mempool);
  1610. return 0;
  1611. }
  1612. /*
  1613. * csio_get_fcoe_resinfo - Read fcoe fw resource info.
  1614. * @hw: HW module
  1615. * Issued with lock held.
  1616. */
  1617. static int
  1618. csio_get_fcoe_resinfo(struct csio_hw *hw)
  1619. {
  1620. struct csio_fcoe_res_info *res_info = &hw->fres_info;
  1621. struct fw_fcoe_res_info_cmd *rsp;
  1622. struct csio_mb *mbp;
  1623. enum fw_retval retval;
  1624. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1625. if (!mbp) {
  1626. CSIO_INC_STATS(hw, n_err_nomem);
  1627. return -ENOMEM;
  1628. }
  1629. /* Get FCoE FW resource information */
  1630. csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1631. if (csio_mb_issue(hw, mbp)) {
  1632. csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
  1633. mempool_free(mbp, hw->mb_mempool);
  1634. return -EINVAL;
  1635. }
  1636. rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
  1637. retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
  1638. if (retval != FW_SUCCESS) {
  1639. csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
  1640. retval);
  1641. mempool_free(mbp, hw->mb_mempool);
  1642. return -EINVAL;
  1643. }
  1644. res_info->e_d_tov = ntohs(rsp->e_d_tov);
  1645. res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
  1646. res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
  1647. res_info->r_r_tov = ntohs(rsp->r_r_tov);
  1648. res_info->max_xchgs = ntohl(rsp->max_xchgs);
  1649. res_info->max_ssns = ntohl(rsp->max_ssns);
  1650. res_info->used_xchgs = ntohl(rsp->used_xchgs);
  1651. res_info->used_ssns = ntohl(rsp->used_ssns);
  1652. res_info->max_fcfs = ntohl(rsp->max_fcfs);
  1653. res_info->max_vnps = ntohl(rsp->max_vnps);
  1654. res_info->used_fcfs = ntohl(rsp->used_fcfs);
  1655. res_info->used_vnps = ntohl(rsp->used_vnps);
  1656. csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
  1657. res_info->max_xchgs);
  1658. mempool_free(mbp, hw->mb_mempool);
  1659. return 0;
  1660. }
  1661. static int
  1662. csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
  1663. {
  1664. struct csio_mb *mbp;
  1665. enum fw_retval retval;
  1666. u32 _param[1];
  1667. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1668. if (!mbp) {
  1669. CSIO_INC_STATS(hw, n_err_nomem);
  1670. return -ENOMEM;
  1671. }
  1672. /*
  1673. * Find out whether we're dealing with a version of
  1674. * the firmware which has configuration file support.
  1675. */
  1676. _param[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  1677. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
  1678. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1679. ARRAY_SIZE(_param), _param, NULL, false, NULL);
  1680. if (csio_mb_issue(hw, mbp)) {
  1681. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1682. mempool_free(mbp, hw->mb_mempool);
  1683. return -EINVAL;
  1684. }
  1685. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1686. ARRAY_SIZE(_param), _param);
  1687. if (retval != FW_SUCCESS) {
  1688. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1689. retval);
  1690. mempool_free(mbp, hw->mb_mempool);
  1691. return -EINVAL;
  1692. }
  1693. mempool_free(mbp, hw->mb_mempool);
  1694. *param = _param[0];
  1695. return 0;
  1696. }
  1697. static int
  1698. csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
  1699. {
  1700. int ret = 0;
  1701. const struct firmware *cf;
  1702. struct pci_dev *pci_dev = hw->pdev;
  1703. struct device *dev = &pci_dev->dev;
  1704. unsigned int mtype = 0, maddr = 0;
  1705. uint32_t *cfg_data;
  1706. int value_to_add = 0;
  1707. const char *fw_cfg_file;
  1708. if (csio_is_t5(pci_dev->device & CSIO_HW_CHIP_MASK))
  1709. fw_cfg_file = FW_CFG_NAME_T5;
  1710. else
  1711. fw_cfg_file = FW_CFG_NAME_T6;
  1712. if (request_firmware(&cf, fw_cfg_file, dev) < 0) {
  1713. csio_err(hw, "could not find config file %s, err: %d\n",
  1714. fw_cfg_file, ret);
  1715. return -ENOENT;
  1716. }
  1717. if (cf->size%4 != 0)
  1718. value_to_add = 4 - (cf->size % 4);
  1719. cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
  1720. if (cfg_data == NULL) {
  1721. ret = -ENOMEM;
  1722. goto leave;
  1723. }
  1724. memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
  1725. if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
  1726. ret = -EINVAL;
  1727. goto leave;
  1728. }
  1729. mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
  1730. maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
  1731. ret = csio_memory_write(hw, mtype, maddr,
  1732. cf->size + value_to_add, cfg_data);
  1733. if ((ret == 0) && (value_to_add != 0)) {
  1734. union {
  1735. u32 word;
  1736. char buf[4];
  1737. } last;
  1738. size_t size = cf->size & ~0x3;
  1739. int i;
  1740. last.word = cfg_data[size >> 2];
  1741. for (i = value_to_add; i < 4; i++)
  1742. last.buf[i] = 0;
  1743. ret = csio_memory_write(hw, mtype, maddr + size, 4, &last.word);
  1744. }
  1745. if (ret == 0) {
  1746. csio_info(hw, "config file upgraded to %s\n", fw_cfg_file);
  1747. snprintf(path, 64, "%s%s", "/lib/firmware/", fw_cfg_file);
  1748. }
  1749. leave:
  1750. kfree(cfg_data);
  1751. release_firmware(cf);
  1752. return ret;
  1753. }
  1754. /*
  1755. * HW initialization: contact FW, obtain config, perform basic init.
  1756. *
  1757. * If the firmware we're dealing with has Configuration File support, then
  1758. * we use that to perform all configuration -- either using the configuration
  1759. * file stored in flash on the adapter or using a filesystem-local file
  1760. * if available.
  1761. *
  1762. * If we don't have configuration file support in the firmware, then we'll
  1763. * have to set things up the old fashioned way with hard-coded register
  1764. * writes and firmware commands ...
  1765. */
  1766. /*
  1767. * Attempt to initialize the HW via a Firmware Configuration File.
  1768. */
  1769. static int
  1770. csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
  1771. {
  1772. struct csio_mb *mbp = NULL;
  1773. struct fw_caps_config_cmd *caps_cmd;
  1774. unsigned int mtype, maddr;
  1775. int rv = -EINVAL;
  1776. uint32_t finiver = 0, finicsum = 0, cfcsum = 0;
  1777. char path[64];
  1778. char *config_name = NULL;
  1779. /*
  1780. * Reset device if necessary
  1781. */
  1782. if (reset) {
  1783. rv = csio_do_reset(hw, true);
  1784. if (rv != 0)
  1785. goto bye;
  1786. }
  1787. /*
  1788. * If we have a configuration file in host ,
  1789. * then use that. Otherwise, use the configuration file stored
  1790. * in the HW flash ...
  1791. */
  1792. spin_unlock_irq(&hw->lock);
  1793. rv = csio_hw_flash_config(hw, fw_cfg_param, path);
  1794. spin_lock_irq(&hw->lock);
  1795. if (rv != 0) {
  1796. /*
  1797. * config file was not found. Use default
  1798. * config file from flash.
  1799. */
  1800. config_name = "On FLASH";
  1801. mtype = FW_MEMTYPE_CF_FLASH;
  1802. maddr = hw->chip_ops->chip_flash_cfg_addr(hw);
  1803. } else {
  1804. config_name = path;
  1805. mtype = FW_PARAMS_PARAM_Y_G(*fw_cfg_param);
  1806. maddr = FW_PARAMS_PARAM_Z_G(*fw_cfg_param) << 16;
  1807. }
  1808. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1809. if (!mbp) {
  1810. CSIO_INC_STATS(hw, n_err_nomem);
  1811. return -ENOMEM;
  1812. }
  1813. /*
  1814. * Tell the firmware to process the indicated Configuration File.
  1815. * If there are no errors and the caller has provided return value
  1816. * pointers for the [fini] section version, checksum and computed
  1817. * checksum, pass those back to the caller.
  1818. */
  1819. caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
  1820. CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
  1821. caps_cmd->op_to_write =
  1822. htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  1823. FW_CMD_REQUEST_F |
  1824. FW_CMD_READ_F);
  1825. caps_cmd->cfvalid_to_len16 =
  1826. htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
  1827. FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
  1828. FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
  1829. FW_LEN16(*caps_cmd));
  1830. if (csio_mb_issue(hw, mbp)) {
  1831. rv = -EINVAL;
  1832. goto bye;
  1833. }
  1834. rv = csio_mb_fw_retval(mbp);
  1835. /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
  1836. * Configuration File in FLASH), our last gasp effort is to use the
  1837. * Firmware Configuration File which is embedded in the
  1838. * firmware. A very few early versions of the firmware didn't
  1839. * have one embedded but we can ignore those.
  1840. */
  1841. if (rv == ENOENT) {
  1842. CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
  1843. caps_cmd->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  1844. FW_CMD_REQUEST_F |
  1845. FW_CMD_READ_F);
  1846. caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
  1847. if (csio_mb_issue(hw, mbp)) {
  1848. rv = -EINVAL;
  1849. goto bye;
  1850. }
  1851. rv = csio_mb_fw_retval(mbp);
  1852. config_name = "Firmware Default";
  1853. }
  1854. if (rv != FW_SUCCESS)
  1855. goto bye;
  1856. finiver = ntohl(caps_cmd->finiver);
  1857. finicsum = ntohl(caps_cmd->finicsum);
  1858. cfcsum = ntohl(caps_cmd->cfcsum);
  1859. /*
  1860. * And now tell the firmware to use the configuration we just loaded.
  1861. */
  1862. caps_cmd->op_to_write =
  1863. htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
  1864. FW_CMD_REQUEST_F |
  1865. FW_CMD_WRITE_F);
  1866. caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
  1867. if (csio_mb_issue(hw, mbp)) {
  1868. rv = -EINVAL;
  1869. goto bye;
  1870. }
  1871. rv = csio_mb_fw_retval(mbp);
  1872. if (rv != FW_SUCCESS) {
  1873. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1874. goto bye;
  1875. }
  1876. if (finicsum != cfcsum) {
  1877. csio_warn(hw,
  1878. "Config File checksum mismatch: csum=%#x, computed=%#x\n",
  1879. finicsum, cfcsum);
  1880. }
  1881. /* Validate device capabilities */
  1882. rv = csio_hw_validate_caps(hw, mbp);
  1883. if (rv != 0)
  1884. goto bye;
  1885. mempool_free(mbp, hw->mb_mempool);
  1886. mbp = NULL;
  1887. /*
  1888. * Note that we're operating with parameters
  1889. * not supplied by the driver, rather than from hard-wired
  1890. * initialization constants buried in the driver.
  1891. */
  1892. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1893. /* device parameters */
  1894. rv = csio_get_device_params(hw);
  1895. if (rv != 0)
  1896. goto bye;
  1897. /* Configure SGE */
  1898. csio_wr_sge_init(hw);
  1899. /*
  1900. * And finally tell the firmware to initialize itself using the
  1901. * parameters from the Configuration File.
  1902. */
  1903. /* Post event to notify completion of configuration */
  1904. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1905. csio_info(hw, "Successfully configure using Firmware "
  1906. "Configuration File %s, version %#x, computed checksum %#x\n",
  1907. config_name, finiver, cfcsum);
  1908. return 0;
  1909. /*
  1910. * Something bad happened. Return the error ...
  1911. */
  1912. bye:
  1913. if (mbp)
  1914. mempool_free(mbp, hw->mb_mempool);
  1915. hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
  1916. csio_warn(hw, "Configuration file error %d\n", rv);
  1917. return rv;
  1918. }
  1919. /* Is the given firmware API compatible with the one the driver was compiled
  1920. * with?
  1921. */
  1922. static int fw_compatible(const struct fw_hdr *hdr1, const struct fw_hdr *hdr2)
  1923. {
  1924. /* short circuit if it's the exact same firmware version */
  1925. if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
  1926. return 1;
  1927. #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
  1928. if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
  1929. SAME_INTF(ri) && SAME_INTF(iscsi) && SAME_INTF(fcoe))
  1930. return 1;
  1931. #undef SAME_INTF
  1932. return 0;
  1933. }
  1934. /* The firmware in the filesystem is usable, but should it be installed?
  1935. * This routine explains itself in detail if it indicates the filesystem
  1936. * firmware should be installed.
  1937. */
  1938. static int csio_should_install_fs_fw(struct csio_hw *hw, int card_fw_usable,
  1939. int k, int c)
  1940. {
  1941. const char *reason;
  1942. if (!card_fw_usable) {
  1943. reason = "incompatible or unusable";
  1944. goto install;
  1945. }
  1946. if (k > c) {
  1947. reason = "older than the version supported with this driver";
  1948. goto install;
  1949. }
  1950. return 0;
  1951. install:
  1952. csio_err(hw, "firmware on card (%u.%u.%u.%u) is %s, "
  1953. "installing firmware %u.%u.%u.%u on card.\n",
  1954. FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
  1955. FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c), reason,
  1956. FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
  1957. FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
  1958. return 1;
  1959. }
  1960. static struct fw_info fw_info_array[] = {
  1961. {
  1962. .chip = CHELSIO_T5,
  1963. .fs_name = FW_CFG_NAME_T5,
  1964. .fw_mod_name = FW_FNAME_T5,
  1965. .fw_hdr = {
  1966. .chip = FW_HDR_CHIP_T5,
  1967. .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
  1968. .intfver_nic = FW_INTFVER(T5, NIC),
  1969. .intfver_vnic = FW_INTFVER(T5, VNIC),
  1970. .intfver_ri = FW_INTFVER(T5, RI),
  1971. .intfver_iscsi = FW_INTFVER(T5, ISCSI),
  1972. .intfver_fcoe = FW_INTFVER(T5, FCOE),
  1973. },
  1974. }, {
  1975. .chip = CHELSIO_T6,
  1976. .fs_name = FW_CFG_NAME_T6,
  1977. .fw_mod_name = FW_FNAME_T6,
  1978. .fw_hdr = {
  1979. .chip = FW_HDR_CHIP_T6,
  1980. .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
  1981. .intfver_nic = FW_INTFVER(T6, NIC),
  1982. .intfver_vnic = FW_INTFVER(T6, VNIC),
  1983. .intfver_ri = FW_INTFVER(T6, RI),
  1984. .intfver_iscsi = FW_INTFVER(T6, ISCSI),
  1985. .intfver_fcoe = FW_INTFVER(T6, FCOE),
  1986. },
  1987. }
  1988. };
  1989. static struct fw_info *find_fw_info(int chip)
  1990. {
  1991. int i;
  1992. for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
  1993. if (fw_info_array[i].chip == chip)
  1994. return &fw_info_array[i];
  1995. }
  1996. return NULL;
  1997. }
  1998. static int csio_hw_prep_fw(struct csio_hw *hw, struct fw_info *fw_info,
  1999. const u8 *fw_data, unsigned int fw_size,
  2000. struct fw_hdr *card_fw, enum csio_dev_state state,
  2001. int *reset)
  2002. {
  2003. int ret, card_fw_usable, fs_fw_usable;
  2004. const struct fw_hdr *fs_fw;
  2005. const struct fw_hdr *drv_fw;
  2006. drv_fw = &fw_info->fw_hdr;
  2007. /* Read the header of the firmware on the card */
  2008. ret = csio_hw_read_flash(hw, FLASH_FW_START,
  2009. sizeof(*card_fw) / sizeof(uint32_t),
  2010. (uint32_t *)card_fw, 1);
  2011. if (ret == 0) {
  2012. card_fw_usable = fw_compatible(drv_fw, (const void *)card_fw);
  2013. } else {
  2014. csio_err(hw,
  2015. "Unable to read card's firmware header: %d\n", ret);
  2016. card_fw_usable = 0;
  2017. }
  2018. if (fw_data != NULL) {
  2019. fs_fw = (const void *)fw_data;
  2020. fs_fw_usable = fw_compatible(drv_fw, fs_fw);
  2021. } else {
  2022. fs_fw = NULL;
  2023. fs_fw_usable = 0;
  2024. }
  2025. if (card_fw_usable && card_fw->fw_ver == drv_fw->fw_ver &&
  2026. (!fs_fw_usable || fs_fw->fw_ver == drv_fw->fw_ver)) {
  2027. /* Common case: the firmware on the card is an exact match and
  2028. * the filesystem one is an exact match too, or the filesystem
  2029. * one is absent/incompatible.
  2030. */
  2031. } else if (fs_fw_usable && state == CSIO_DEV_STATE_UNINIT &&
  2032. csio_should_install_fs_fw(hw, card_fw_usable,
  2033. be32_to_cpu(fs_fw->fw_ver),
  2034. be32_to_cpu(card_fw->fw_ver))) {
  2035. ret = csio_hw_fw_upgrade(hw, hw->pfn, fw_data,
  2036. fw_size, 0);
  2037. if (ret != 0) {
  2038. csio_err(hw,
  2039. "failed to install firmware: %d\n", ret);
  2040. goto bye;
  2041. }
  2042. /* Installed successfully, update the cached header too. */
  2043. memcpy(card_fw, fs_fw, sizeof(*card_fw));
  2044. card_fw_usable = 1;
  2045. *reset = 0; /* already reset as part of load_fw */
  2046. }
  2047. if (!card_fw_usable) {
  2048. uint32_t d, c, k;
  2049. d = be32_to_cpu(drv_fw->fw_ver);
  2050. c = be32_to_cpu(card_fw->fw_ver);
  2051. k = fs_fw ? be32_to_cpu(fs_fw->fw_ver) : 0;
  2052. csio_err(hw, "Cannot find a usable firmware: "
  2053. "chip state %d, "
  2054. "driver compiled with %d.%d.%d.%d, "
  2055. "card has %d.%d.%d.%d, filesystem has %d.%d.%d.%d\n",
  2056. state,
  2057. FW_HDR_FW_VER_MAJOR_G(d), FW_HDR_FW_VER_MINOR_G(d),
  2058. FW_HDR_FW_VER_MICRO_G(d), FW_HDR_FW_VER_BUILD_G(d),
  2059. FW_HDR_FW_VER_MAJOR_G(c), FW_HDR_FW_VER_MINOR_G(c),
  2060. FW_HDR_FW_VER_MICRO_G(c), FW_HDR_FW_VER_BUILD_G(c),
  2061. FW_HDR_FW_VER_MAJOR_G(k), FW_HDR_FW_VER_MINOR_G(k),
  2062. FW_HDR_FW_VER_MICRO_G(k), FW_HDR_FW_VER_BUILD_G(k));
  2063. ret = EINVAL;
  2064. goto bye;
  2065. }
  2066. /* We're using whatever's on the card and it's known to be good. */
  2067. hw->fwrev = be32_to_cpu(card_fw->fw_ver);
  2068. hw->tp_vers = be32_to_cpu(card_fw->tp_microcode_ver);
  2069. bye:
  2070. return ret;
  2071. }
  2072. /*
  2073. * Returns -EINVAL if attempts to flash the firmware failed,
  2074. * -ENOMEM if memory allocation failed else returns 0,
  2075. * if flashing was not attempted because the card had the
  2076. * latest firmware ECANCELED is returned
  2077. */
  2078. static int
  2079. csio_hw_flash_fw(struct csio_hw *hw, int *reset)
  2080. {
  2081. int ret = -ECANCELED;
  2082. const struct firmware *fw;
  2083. struct fw_info *fw_info;
  2084. struct fw_hdr *card_fw;
  2085. struct pci_dev *pci_dev = hw->pdev;
  2086. struct device *dev = &pci_dev->dev ;
  2087. const u8 *fw_data = NULL;
  2088. unsigned int fw_size = 0;
  2089. const char *fw_bin_file;
  2090. /* This is the firmware whose headers the driver was compiled
  2091. * against
  2092. */
  2093. fw_info = find_fw_info(CHELSIO_CHIP_VERSION(hw->chip_id));
  2094. if (fw_info == NULL) {
  2095. csio_err(hw,
  2096. "unable to get firmware info for chip %d.\n",
  2097. CHELSIO_CHIP_VERSION(hw->chip_id));
  2098. return -EINVAL;
  2099. }
  2100. /* allocate memory to read the header of the firmware on the
  2101. * card
  2102. */
  2103. card_fw = kmalloc(sizeof(*card_fw), GFP_KERNEL);
  2104. if (!card_fw)
  2105. return -ENOMEM;
  2106. if (csio_is_t5(pci_dev->device & CSIO_HW_CHIP_MASK))
  2107. fw_bin_file = FW_FNAME_T5;
  2108. else
  2109. fw_bin_file = FW_FNAME_T6;
  2110. if (request_firmware(&fw, fw_bin_file, dev) < 0) {
  2111. csio_err(hw, "could not find firmware image %s, err: %d\n",
  2112. fw_bin_file, ret);
  2113. } else {
  2114. fw_data = fw->data;
  2115. fw_size = fw->size;
  2116. }
  2117. /* upgrade FW logic */
  2118. ret = csio_hw_prep_fw(hw, fw_info, fw_data, fw_size, card_fw,
  2119. hw->fw_state, reset);
  2120. /* Cleaning up */
  2121. if (fw != NULL)
  2122. release_firmware(fw);
  2123. kfree(card_fw);
  2124. return ret;
  2125. }
  2126. static int csio_hw_check_fwver(struct csio_hw *hw)
  2127. {
  2128. if (csio_is_t6(hw->pdev->device & CSIO_HW_CHIP_MASK) &&
  2129. (hw->fwrev < CSIO_MIN_T6_FW)) {
  2130. csio_hw_print_fw_version(hw, "T6 unsupported fw");
  2131. return -1;
  2132. }
  2133. return 0;
  2134. }
  2135. /*
  2136. * csio_hw_configure - Configure HW
  2137. * @hw - HW module
  2138. *
  2139. */
  2140. static void
  2141. csio_hw_configure(struct csio_hw *hw)
  2142. {
  2143. int reset = 1;
  2144. int rv;
  2145. u32 param[1];
  2146. rv = csio_hw_dev_ready(hw);
  2147. if (rv != 0) {
  2148. CSIO_INC_STATS(hw, n_err_fatal);
  2149. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2150. goto out;
  2151. }
  2152. /* HW version */
  2153. hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV_A);
  2154. /* Needed for FW download */
  2155. rv = csio_hw_get_flash_params(hw);
  2156. if (rv != 0) {
  2157. csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
  2158. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2159. goto out;
  2160. }
  2161. /* Set PCIe completion timeout to 4 seconds */
  2162. if (pci_is_pcie(hw->pdev))
  2163. pcie_capability_clear_and_set_word(hw->pdev, PCI_EXP_DEVCTL2,
  2164. PCI_EXP_DEVCTL2_COMP_TIMEOUT, 0xd);
  2165. hw->chip_ops->chip_set_mem_win(hw, MEMWIN_CSIOSTOR);
  2166. rv = csio_hw_get_fw_version(hw, &hw->fwrev);
  2167. if (rv != 0)
  2168. goto out;
  2169. csio_hw_print_fw_version(hw, "Firmware revision");
  2170. rv = csio_do_hello(hw, &hw->fw_state);
  2171. if (rv != 0) {
  2172. CSIO_INC_STATS(hw, n_err_fatal);
  2173. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2174. goto out;
  2175. }
  2176. /* Read vpd */
  2177. rv = csio_hw_get_vpd_params(hw, &hw->vpd);
  2178. if (rv != 0)
  2179. goto out;
  2180. csio_hw_get_fw_version(hw, &hw->fwrev);
  2181. csio_hw_get_tp_version(hw, &hw->tp_vers);
  2182. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2183. /* Do firmware update */
  2184. spin_unlock_irq(&hw->lock);
  2185. rv = csio_hw_flash_fw(hw, &reset);
  2186. spin_lock_irq(&hw->lock);
  2187. if (rv != 0)
  2188. goto out;
  2189. rv = csio_hw_check_fwver(hw);
  2190. if (rv < 0)
  2191. goto out;
  2192. /* If the firmware doesn't support Configuration Files,
  2193. * return an error.
  2194. */
  2195. rv = csio_hw_check_fwconfig(hw, param);
  2196. if (rv != 0) {
  2197. csio_info(hw, "Firmware doesn't support "
  2198. "Firmware Configuration files\n");
  2199. goto out;
  2200. }
  2201. /* The firmware provides us with a memory buffer where we can
  2202. * load a Configuration File from the host if we want to
  2203. * override the Configuration File in flash.
  2204. */
  2205. rv = csio_hw_use_fwconfig(hw, reset, param);
  2206. if (rv == -ENOENT) {
  2207. csio_info(hw, "Could not initialize "
  2208. "adapter, error%d\n", rv);
  2209. goto out;
  2210. }
  2211. if (rv != 0) {
  2212. csio_info(hw, "Could not initialize "
  2213. "adapter, error%d\n", rv);
  2214. goto out;
  2215. }
  2216. } else {
  2217. rv = csio_hw_check_fwver(hw);
  2218. if (rv < 0)
  2219. goto out;
  2220. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  2221. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  2222. /* device parameters */
  2223. rv = csio_get_device_params(hw);
  2224. if (rv != 0)
  2225. goto out;
  2226. /* Get device capabilities */
  2227. rv = csio_config_device_caps(hw);
  2228. if (rv != 0)
  2229. goto out;
  2230. /* Configure SGE */
  2231. csio_wr_sge_init(hw);
  2232. /* Post event to notify completion of configuration */
  2233. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  2234. goto out;
  2235. }
  2236. } /* if not master */
  2237. out:
  2238. return;
  2239. }
  2240. /*
  2241. * csio_hw_initialize - Initialize HW
  2242. * @hw - HW module
  2243. *
  2244. */
  2245. static void
  2246. csio_hw_initialize(struct csio_hw *hw)
  2247. {
  2248. struct csio_mb *mbp;
  2249. enum fw_retval retval;
  2250. int rv;
  2251. int i;
  2252. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2253. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  2254. if (!mbp)
  2255. goto out;
  2256. csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  2257. if (csio_mb_issue(hw, mbp)) {
  2258. csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
  2259. goto free_and_out;
  2260. }
  2261. retval = csio_mb_fw_retval(mbp);
  2262. if (retval != FW_SUCCESS) {
  2263. csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
  2264. retval);
  2265. goto free_and_out;
  2266. }
  2267. mempool_free(mbp, hw->mb_mempool);
  2268. }
  2269. rv = csio_get_fcoe_resinfo(hw);
  2270. if (rv != 0) {
  2271. csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
  2272. goto out;
  2273. }
  2274. spin_unlock_irq(&hw->lock);
  2275. rv = csio_config_queues(hw);
  2276. spin_lock_irq(&hw->lock);
  2277. if (rv != 0) {
  2278. csio_err(hw, "Config of queues failed!: %d\n", rv);
  2279. goto out;
  2280. }
  2281. for (i = 0; i < hw->num_pports; i++)
  2282. hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
  2283. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2284. rv = csio_enable_ports(hw);
  2285. if (rv != 0) {
  2286. csio_err(hw, "Failed to enable ports: %d\n", rv);
  2287. goto out;
  2288. }
  2289. }
  2290. csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
  2291. return;
  2292. free_and_out:
  2293. mempool_free(mbp, hw->mb_mempool);
  2294. out:
  2295. return;
  2296. }
  2297. #define PF_INTR_MASK (PFSW_F | PFCIM_F)
  2298. /*
  2299. * csio_hw_intr_enable - Enable HW interrupts
  2300. * @hw: Pointer to HW module.
  2301. *
  2302. * Enable interrupts in HW registers.
  2303. */
  2304. static void
  2305. csio_hw_intr_enable(struct csio_hw *hw)
  2306. {
  2307. uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
  2308. u32 pf = 0;
  2309. uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE_A);
  2310. if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
  2311. pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
  2312. else
  2313. pf = T6_SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
  2314. /*
  2315. * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
  2316. * by FW, so do nothing for INTX.
  2317. */
  2318. if (hw->intr_mode == CSIO_IM_MSIX)
  2319. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
  2320. AIVEC_V(AIVEC_M), vec);
  2321. else if (hw->intr_mode == CSIO_IM_MSI)
  2322. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG_A),
  2323. AIVEC_V(AIVEC_M), 0);
  2324. csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE_A));
  2325. /* Turn on MB interrupts - this will internally flush PIO as well */
  2326. csio_mb_intr_enable(hw);
  2327. /* These are common registers - only a master can modify them */
  2328. if (csio_is_hw_master(hw)) {
  2329. /*
  2330. * Disable the Serial FLASH interrupt, if enabled!
  2331. */
  2332. pl &= (~SF_F);
  2333. csio_wr_reg32(hw, pl, PL_INT_ENABLE_A);
  2334. csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE_F |
  2335. EGRESS_SIZE_ERR_F | ERR_INVALID_CIDX_INC_F |
  2336. ERR_CPL_OPCODE_0_F | ERR_DROPPED_DB_F |
  2337. ERR_DATA_CPL_ON_HIGH_QID1_F |
  2338. ERR_DATA_CPL_ON_HIGH_QID0_F | ERR_BAD_DB_PIDX3_F |
  2339. ERR_BAD_DB_PIDX2_F | ERR_BAD_DB_PIDX1_F |
  2340. ERR_BAD_DB_PIDX0_F | ERR_ING_CTXT_PRIO_F |
  2341. ERR_EGR_CTXT_PRIO_F | INGRESS_SIZE_ERR_F,
  2342. SGE_INT_ENABLE3_A);
  2343. csio_set_reg_field(hw, PL_INT_MAP0_A, 0, 1 << pf);
  2344. }
  2345. hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
  2346. }
  2347. /*
  2348. * csio_hw_intr_disable - Disable HW interrupts
  2349. * @hw: Pointer to HW module.
  2350. *
  2351. * Turn off Mailbox and PCI_PF_CFG interrupts.
  2352. */
  2353. void
  2354. csio_hw_intr_disable(struct csio_hw *hw)
  2355. {
  2356. u32 pf = 0;
  2357. if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
  2358. pf = SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
  2359. else
  2360. pf = T6_SOURCEPF_G(csio_rd_reg32(hw, PL_WHOAMI_A));
  2361. if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
  2362. return;
  2363. hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
  2364. csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE_A));
  2365. if (csio_is_hw_master(hw))
  2366. csio_set_reg_field(hw, PL_INT_MAP0_A, 1 << pf, 0);
  2367. /* Turn off MB interrupts */
  2368. csio_mb_intr_disable(hw);
  2369. }
  2370. void
  2371. csio_hw_fatal_err(struct csio_hw *hw)
  2372. {
  2373. csio_set_reg_field(hw, SGE_CONTROL_A, GLOBALENABLE_F, 0);
  2374. csio_hw_intr_disable(hw);
  2375. /* Do not reset HW, we may need FW state for debugging */
  2376. csio_fatal(hw, "HW Fatal error encountered!\n");
  2377. }
  2378. /*****************************************************************************/
  2379. /* START: HW SM */
  2380. /*****************************************************************************/
  2381. /*
  2382. * csio_hws_uninit - Uninit state
  2383. * @hw - HW module
  2384. * @evt - Event
  2385. *
  2386. */
  2387. static void
  2388. csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
  2389. {
  2390. hw->prev_evt = hw->cur_evt;
  2391. hw->cur_evt = evt;
  2392. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2393. switch (evt) {
  2394. case CSIO_HWE_CFG:
  2395. csio_set_state(&hw->sm, csio_hws_configuring);
  2396. csio_hw_configure(hw);
  2397. break;
  2398. default:
  2399. CSIO_INC_STATS(hw, n_evt_unexp);
  2400. break;
  2401. }
  2402. }
  2403. /*
  2404. * csio_hws_configuring - Configuring state
  2405. * @hw - HW module
  2406. * @evt - Event
  2407. *
  2408. */
  2409. static void
  2410. csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
  2411. {
  2412. hw->prev_evt = hw->cur_evt;
  2413. hw->cur_evt = evt;
  2414. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2415. switch (evt) {
  2416. case CSIO_HWE_INIT:
  2417. csio_set_state(&hw->sm, csio_hws_initializing);
  2418. csio_hw_initialize(hw);
  2419. break;
  2420. case CSIO_HWE_INIT_DONE:
  2421. csio_set_state(&hw->sm, csio_hws_ready);
  2422. /* Fan out event to all lnode SMs */
  2423. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2424. break;
  2425. case CSIO_HWE_FATAL:
  2426. csio_set_state(&hw->sm, csio_hws_uninit);
  2427. break;
  2428. case CSIO_HWE_PCI_REMOVE:
  2429. csio_do_bye(hw);
  2430. break;
  2431. default:
  2432. CSIO_INC_STATS(hw, n_evt_unexp);
  2433. break;
  2434. }
  2435. }
  2436. /*
  2437. * csio_hws_initializing - Initialiazing state
  2438. * @hw - HW module
  2439. * @evt - Event
  2440. *
  2441. */
  2442. static void
  2443. csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
  2444. {
  2445. hw->prev_evt = hw->cur_evt;
  2446. hw->cur_evt = evt;
  2447. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2448. switch (evt) {
  2449. case CSIO_HWE_INIT_DONE:
  2450. csio_set_state(&hw->sm, csio_hws_ready);
  2451. /* Fan out event to all lnode SMs */
  2452. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2453. /* Enable interrupts */
  2454. csio_hw_intr_enable(hw);
  2455. break;
  2456. case CSIO_HWE_FATAL:
  2457. csio_set_state(&hw->sm, csio_hws_uninit);
  2458. break;
  2459. case CSIO_HWE_PCI_REMOVE:
  2460. csio_do_bye(hw);
  2461. break;
  2462. default:
  2463. CSIO_INC_STATS(hw, n_evt_unexp);
  2464. break;
  2465. }
  2466. }
  2467. /*
  2468. * csio_hws_ready - Ready state
  2469. * @hw - HW module
  2470. * @evt - Event
  2471. *
  2472. */
  2473. static void
  2474. csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
  2475. {
  2476. /* Remember the event */
  2477. hw->evtflag = evt;
  2478. hw->prev_evt = hw->cur_evt;
  2479. hw->cur_evt = evt;
  2480. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2481. switch (evt) {
  2482. case CSIO_HWE_HBA_RESET:
  2483. case CSIO_HWE_FW_DLOAD:
  2484. case CSIO_HWE_SUSPEND:
  2485. case CSIO_HWE_PCI_REMOVE:
  2486. case CSIO_HWE_PCIERR_DETECTED:
  2487. csio_set_state(&hw->sm, csio_hws_quiescing);
  2488. /* cleanup all outstanding cmds */
  2489. if (evt == CSIO_HWE_HBA_RESET ||
  2490. evt == CSIO_HWE_PCIERR_DETECTED)
  2491. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
  2492. else
  2493. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
  2494. csio_hw_intr_disable(hw);
  2495. csio_hw_mbm_cleanup(hw);
  2496. csio_evtq_stop(hw);
  2497. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
  2498. csio_evtq_flush(hw);
  2499. csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
  2500. csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
  2501. break;
  2502. case CSIO_HWE_FATAL:
  2503. csio_set_state(&hw->sm, csio_hws_uninit);
  2504. break;
  2505. default:
  2506. CSIO_INC_STATS(hw, n_evt_unexp);
  2507. break;
  2508. }
  2509. }
  2510. /*
  2511. * csio_hws_quiescing - Quiescing state
  2512. * @hw - HW module
  2513. * @evt - Event
  2514. *
  2515. */
  2516. static void
  2517. csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
  2518. {
  2519. hw->prev_evt = hw->cur_evt;
  2520. hw->cur_evt = evt;
  2521. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2522. switch (evt) {
  2523. case CSIO_HWE_QUIESCED:
  2524. switch (hw->evtflag) {
  2525. case CSIO_HWE_FW_DLOAD:
  2526. csio_set_state(&hw->sm, csio_hws_resetting);
  2527. /* Download firmware */
  2528. /* Fall through */
  2529. case CSIO_HWE_HBA_RESET:
  2530. csio_set_state(&hw->sm, csio_hws_resetting);
  2531. /* Start reset of the HBA */
  2532. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
  2533. csio_wr_destroy_queues(hw, false);
  2534. csio_do_reset(hw, false);
  2535. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
  2536. break;
  2537. case CSIO_HWE_PCI_REMOVE:
  2538. csio_set_state(&hw->sm, csio_hws_removing);
  2539. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
  2540. csio_wr_destroy_queues(hw, true);
  2541. /* Now send the bye command */
  2542. csio_do_bye(hw);
  2543. break;
  2544. case CSIO_HWE_SUSPEND:
  2545. csio_set_state(&hw->sm, csio_hws_quiesced);
  2546. break;
  2547. case CSIO_HWE_PCIERR_DETECTED:
  2548. csio_set_state(&hw->sm, csio_hws_pcierr);
  2549. csio_wr_destroy_queues(hw, false);
  2550. break;
  2551. default:
  2552. CSIO_INC_STATS(hw, n_evt_unexp);
  2553. break;
  2554. }
  2555. break;
  2556. default:
  2557. CSIO_INC_STATS(hw, n_evt_unexp);
  2558. break;
  2559. }
  2560. }
  2561. /*
  2562. * csio_hws_quiesced - Quiesced state
  2563. * @hw - HW module
  2564. * @evt - Event
  2565. *
  2566. */
  2567. static void
  2568. csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
  2569. {
  2570. hw->prev_evt = hw->cur_evt;
  2571. hw->cur_evt = evt;
  2572. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2573. switch (evt) {
  2574. case CSIO_HWE_RESUME:
  2575. csio_set_state(&hw->sm, csio_hws_configuring);
  2576. csio_hw_configure(hw);
  2577. break;
  2578. default:
  2579. CSIO_INC_STATS(hw, n_evt_unexp);
  2580. break;
  2581. }
  2582. }
  2583. /*
  2584. * csio_hws_resetting - HW Resetting state
  2585. * @hw - HW module
  2586. * @evt - Event
  2587. *
  2588. */
  2589. static void
  2590. csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
  2591. {
  2592. hw->prev_evt = hw->cur_evt;
  2593. hw->cur_evt = evt;
  2594. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2595. switch (evt) {
  2596. case CSIO_HWE_HBA_RESET_DONE:
  2597. csio_evtq_start(hw);
  2598. csio_set_state(&hw->sm, csio_hws_configuring);
  2599. csio_hw_configure(hw);
  2600. break;
  2601. default:
  2602. CSIO_INC_STATS(hw, n_evt_unexp);
  2603. break;
  2604. }
  2605. }
  2606. /*
  2607. * csio_hws_removing - PCI Hotplug removing state
  2608. * @hw - HW module
  2609. * @evt - Event
  2610. *
  2611. */
  2612. static void
  2613. csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
  2614. {
  2615. hw->prev_evt = hw->cur_evt;
  2616. hw->cur_evt = evt;
  2617. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2618. switch (evt) {
  2619. case CSIO_HWE_HBA_RESET:
  2620. if (!csio_is_hw_master(hw))
  2621. break;
  2622. /*
  2623. * The BYE should have alerady been issued, so we cant
  2624. * use the mailbox interface. Hence we use the PL_RST
  2625. * register directly.
  2626. */
  2627. csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
  2628. csio_wr_reg32(hw, PIORSTMODE_F | PIORST_F, PL_RST_A);
  2629. mdelay(2000);
  2630. break;
  2631. /* Should never receive any new events */
  2632. default:
  2633. CSIO_INC_STATS(hw, n_evt_unexp);
  2634. break;
  2635. }
  2636. }
  2637. /*
  2638. * csio_hws_pcierr - PCI Error state
  2639. * @hw - HW module
  2640. * @evt - Event
  2641. *
  2642. */
  2643. static void
  2644. csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
  2645. {
  2646. hw->prev_evt = hw->cur_evt;
  2647. hw->cur_evt = evt;
  2648. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2649. switch (evt) {
  2650. case CSIO_HWE_PCIERR_SLOT_RESET:
  2651. csio_evtq_start(hw);
  2652. csio_set_state(&hw->sm, csio_hws_configuring);
  2653. csio_hw_configure(hw);
  2654. break;
  2655. default:
  2656. CSIO_INC_STATS(hw, n_evt_unexp);
  2657. break;
  2658. }
  2659. }
  2660. /*****************************************************************************/
  2661. /* END: HW SM */
  2662. /*****************************************************************************/
  2663. /*
  2664. * csio_handle_intr_status - table driven interrupt handler
  2665. * @hw: HW instance
  2666. * @reg: the interrupt status register to process
  2667. * @acts: table of interrupt actions
  2668. *
  2669. * A table driven interrupt handler that applies a set of masks to an
  2670. * interrupt status word and performs the corresponding actions if the
  2671. * interrupts described by the mask have occured. The actions include
  2672. * optionally emitting a warning or alert message. The table is terminated
  2673. * by an entry specifying mask 0. Returns the number of fatal interrupt
  2674. * conditions.
  2675. */
  2676. int
  2677. csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
  2678. const struct intr_info *acts)
  2679. {
  2680. int fatal = 0;
  2681. unsigned int mask = 0;
  2682. unsigned int status = csio_rd_reg32(hw, reg);
  2683. for ( ; acts->mask; ++acts) {
  2684. if (!(status & acts->mask))
  2685. continue;
  2686. if (acts->fatal) {
  2687. fatal++;
  2688. csio_fatal(hw, "Fatal %s (0x%x)\n",
  2689. acts->msg, status & acts->mask);
  2690. } else if (acts->msg)
  2691. csio_info(hw, "%s (0x%x)\n",
  2692. acts->msg, status & acts->mask);
  2693. mask |= acts->mask;
  2694. }
  2695. status &= mask;
  2696. if (status) /* clear processed interrupts */
  2697. csio_wr_reg32(hw, status, reg);
  2698. return fatal;
  2699. }
  2700. /*
  2701. * TP interrupt handler.
  2702. */
  2703. static void csio_tp_intr_handler(struct csio_hw *hw)
  2704. {
  2705. static struct intr_info tp_intr_info[] = {
  2706. { 0x3fffffff, "TP parity error", -1, 1 },
  2707. { FLMTXFLSTEMPTY_F, "TP out of Tx pages", -1, 1 },
  2708. { 0, NULL, 0, 0 }
  2709. };
  2710. if (csio_handle_intr_status(hw, TP_INT_CAUSE_A, tp_intr_info))
  2711. csio_hw_fatal_err(hw);
  2712. }
  2713. /*
  2714. * SGE interrupt handler.
  2715. */
  2716. static void csio_sge_intr_handler(struct csio_hw *hw)
  2717. {
  2718. uint64_t v;
  2719. static struct intr_info sge_intr_info[] = {
  2720. { ERR_CPL_EXCEED_IQE_SIZE_F,
  2721. "SGE received CPL exceeding IQE size", -1, 1 },
  2722. { ERR_INVALID_CIDX_INC_F,
  2723. "SGE GTS CIDX increment too large", -1, 0 },
  2724. { ERR_CPL_OPCODE_0_F, "SGE received 0-length CPL", -1, 0 },
  2725. { ERR_DROPPED_DB_F, "SGE doorbell dropped", -1, 0 },
  2726. { ERR_DATA_CPL_ON_HIGH_QID1_F | ERR_DATA_CPL_ON_HIGH_QID0_F,
  2727. "SGE IQID > 1023 received CPL for FL", -1, 0 },
  2728. { ERR_BAD_DB_PIDX3_F, "SGE DBP 3 pidx increment too large", -1,
  2729. 0 },
  2730. { ERR_BAD_DB_PIDX2_F, "SGE DBP 2 pidx increment too large", -1,
  2731. 0 },
  2732. { ERR_BAD_DB_PIDX1_F, "SGE DBP 1 pidx increment too large", -1,
  2733. 0 },
  2734. { ERR_BAD_DB_PIDX0_F, "SGE DBP 0 pidx increment too large", -1,
  2735. 0 },
  2736. { ERR_ING_CTXT_PRIO_F,
  2737. "SGE too many priority ingress contexts", -1, 0 },
  2738. { ERR_EGR_CTXT_PRIO_F,
  2739. "SGE too many priority egress contexts", -1, 0 },
  2740. { INGRESS_SIZE_ERR_F, "SGE illegal ingress QID", -1, 0 },
  2741. { EGRESS_SIZE_ERR_F, "SGE illegal egress QID", -1, 0 },
  2742. { 0, NULL, 0, 0 }
  2743. };
  2744. v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1_A) |
  2745. ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2_A) << 32);
  2746. if (v) {
  2747. csio_fatal(hw, "SGE parity error (%#llx)\n",
  2748. (unsigned long long)v);
  2749. csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
  2750. SGE_INT_CAUSE1_A);
  2751. csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2_A);
  2752. }
  2753. v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info);
  2754. if (csio_handle_intr_status(hw, SGE_INT_CAUSE3_A, sge_intr_info) ||
  2755. v != 0)
  2756. csio_hw_fatal_err(hw);
  2757. }
  2758. #define CIM_OBQ_INTR (OBQULP0PARERR_F | OBQULP1PARERR_F | OBQULP2PARERR_F |\
  2759. OBQULP3PARERR_F | OBQSGEPARERR_F | OBQNCSIPARERR_F)
  2760. #define CIM_IBQ_INTR (IBQTP0PARERR_F | IBQTP1PARERR_F | IBQULPPARERR_F |\
  2761. IBQSGEHIPARERR_F | IBQSGELOPARERR_F | IBQNCSIPARERR_F)
  2762. /*
  2763. * CIM interrupt handler.
  2764. */
  2765. static void csio_cim_intr_handler(struct csio_hw *hw)
  2766. {
  2767. static struct intr_info cim_intr_info[] = {
  2768. { PREFDROPINT_F, "CIM control register prefetch drop", -1, 1 },
  2769. { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
  2770. { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
  2771. { MBUPPARERR_F, "CIM mailbox uP parity error", -1, 1 },
  2772. { MBHOSTPARERR_F, "CIM mailbox host parity error", -1, 1 },
  2773. { TIEQINPARERRINT_F, "CIM TIEQ outgoing parity error", -1, 1 },
  2774. { TIEQOUTPARERRINT_F, "CIM TIEQ incoming parity error", -1, 1 },
  2775. { 0, NULL, 0, 0 }
  2776. };
  2777. static struct intr_info cim_upintr_info[] = {
  2778. { RSVDSPACEINT_F, "CIM reserved space access", -1, 1 },
  2779. { ILLTRANSINT_F, "CIM illegal transaction", -1, 1 },
  2780. { ILLWRINT_F, "CIM illegal write", -1, 1 },
  2781. { ILLRDINT_F, "CIM illegal read", -1, 1 },
  2782. { ILLRDBEINT_F, "CIM illegal read BE", -1, 1 },
  2783. { ILLWRBEINT_F, "CIM illegal write BE", -1, 1 },
  2784. { SGLRDBOOTINT_F, "CIM single read from boot space", -1, 1 },
  2785. { SGLWRBOOTINT_F, "CIM single write to boot space", -1, 1 },
  2786. { BLKWRBOOTINT_F, "CIM block write to boot space", -1, 1 },
  2787. { SGLRDFLASHINT_F, "CIM single read from flash space", -1, 1 },
  2788. { SGLWRFLASHINT_F, "CIM single write to flash space", -1, 1 },
  2789. { BLKWRFLASHINT_F, "CIM block write to flash space", -1, 1 },
  2790. { SGLRDEEPROMINT_F, "CIM single EEPROM read", -1, 1 },
  2791. { SGLWREEPROMINT_F, "CIM single EEPROM write", -1, 1 },
  2792. { BLKRDEEPROMINT_F, "CIM block EEPROM read", -1, 1 },
  2793. { BLKWREEPROMINT_F, "CIM block EEPROM write", -1, 1 },
  2794. { SGLRDCTLINT_F, "CIM single read from CTL space", -1, 1 },
  2795. { SGLWRCTLINT_F, "CIM single write to CTL space", -1, 1 },
  2796. { BLKRDCTLINT_F, "CIM block read from CTL space", -1, 1 },
  2797. { BLKWRCTLINT_F, "CIM block write to CTL space", -1, 1 },
  2798. { SGLRDPLINT_F, "CIM single read from PL space", -1, 1 },
  2799. { SGLWRPLINT_F, "CIM single write to PL space", -1, 1 },
  2800. { BLKRDPLINT_F, "CIM block read from PL space", -1, 1 },
  2801. { BLKWRPLINT_F, "CIM block write to PL space", -1, 1 },
  2802. { REQOVRLOOKUPINT_F, "CIM request FIFO overwrite", -1, 1 },
  2803. { RSPOVRLOOKUPINT_F, "CIM response FIFO overwrite", -1, 1 },
  2804. { TIMEOUTINT_F, "CIM PIF timeout", -1, 1 },
  2805. { TIMEOUTMAINT_F, "CIM PIF MA timeout", -1, 1 },
  2806. { 0, NULL, 0, 0 }
  2807. };
  2808. int fat;
  2809. fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE_A,
  2810. cim_intr_info) +
  2811. csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE_A,
  2812. cim_upintr_info);
  2813. if (fat)
  2814. csio_hw_fatal_err(hw);
  2815. }
  2816. /*
  2817. * ULP RX interrupt handler.
  2818. */
  2819. static void csio_ulprx_intr_handler(struct csio_hw *hw)
  2820. {
  2821. static struct intr_info ulprx_intr_info[] = {
  2822. { 0x1800000, "ULPRX context error", -1, 1 },
  2823. { 0x7fffff, "ULPRX parity error", -1, 1 },
  2824. { 0, NULL, 0, 0 }
  2825. };
  2826. if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE_A, ulprx_intr_info))
  2827. csio_hw_fatal_err(hw);
  2828. }
  2829. /*
  2830. * ULP TX interrupt handler.
  2831. */
  2832. static void csio_ulptx_intr_handler(struct csio_hw *hw)
  2833. {
  2834. static struct intr_info ulptx_intr_info[] = {
  2835. { PBL_BOUND_ERR_CH3_F, "ULPTX channel 3 PBL out of bounds", -1,
  2836. 0 },
  2837. { PBL_BOUND_ERR_CH2_F, "ULPTX channel 2 PBL out of bounds", -1,
  2838. 0 },
  2839. { PBL_BOUND_ERR_CH1_F, "ULPTX channel 1 PBL out of bounds", -1,
  2840. 0 },
  2841. { PBL_BOUND_ERR_CH0_F, "ULPTX channel 0 PBL out of bounds", -1,
  2842. 0 },
  2843. { 0xfffffff, "ULPTX parity error", -1, 1 },
  2844. { 0, NULL, 0, 0 }
  2845. };
  2846. if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE_A, ulptx_intr_info))
  2847. csio_hw_fatal_err(hw);
  2848. }
  2849. /*
  2850. * PM TX interrupt handler.
  2851. */
  2852. static void csio_pmtx_intr_handler(struct csio_hw *hw)
  2853. {
  2854. static struct intr_info pmtx_intr_info[] = {
  2855. { PCMD_LEN_OVFL0_F, "PMTX channel 0 pcmd too large", -1, 1 },
  2856. { PCMD_LEN_OVFL1_F, "PMTX channel 1 pcmd too large", -1, 1 },
  2857. { PCMD_LEN_OVFL2_F, "PMTX channel 2 pcmd too large", -1, 1 },
  2858. { ZERO_C_CMD_ERROR_F, "PMTX 0-length pcmd", -1, 1 },
  2859. { 0xffffff0, "PMTX framing error", -1, 1 },
  2860. { OESPI_PAR_ERROR_F, "PMTX oespi parity error", -1, 1 },
  2861. { DB_OPTIONS_PAR_ERROR_F, "PMTX db_options parity error", -1,
  2862. 1 },
  2863. { ICSPI_PAR_ERROR_F, "PMTX icspi parity error", -1, 1 },
  2864. { PMTX_C_PCMD_PAR_ERROR_F, "PMTX c_pcmd parity error", -1, 1},
  2865. { 0, NULL, 0, 0 }
  2866. };
  2867. if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE_A, pmtx_intr_info))
  2868. csio_hw_fatal_err(hw);
  2869. }
  2870. /*
  2871. * PM RX interrupt handler.
  2872. */
  2873. static void csio_pmrx_intr_handler(struct csio_hw *hw)
  2874. {
  2875. static struct intr_info pmrx_intr_info[] = {
  2876. { ZERO_E_CMD_ERROR_F, "PMRX 0-length pcmd", -1, 1 },
  2877. { 0x3ffff0, "PMRX framing error", -1, 1 },
  2878. { OCSPI_PAR_ERROR_F, "PMRX ocspi parity error", -1, 1 },
  2879. { DB_OPTIONS_PAR_ERROR_F, "PMRX db_options parity error", -1,
  2880. 1 },
  2881. { IESPI_PAR_ERROR_F, "PMRX iespi parity error", -1, 1 },
  2882. { PMRX_E_PCMD_PAR_ERROR_F, "PMRX e_pcmd parity error", -1, 1},
  2883. { 0, NULL, 0, 0 }
  2884. };
  2885. if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE_A, pmrx_intr_info))
  2886. csio_hw_fatal_err(hw);
  2887. }
  2888. /*
  2889. * CPL switch interrupt handler.
  2890. */
  2891. static void csio_cplsw_intr_handler(struct csio_hw *hw)
  2892. {
  2893. static struct intr_info cplsw_intr_info[] = {
  2894. { CIM_OP_MAP_PERR_F, "CPLSW CIM op_map parity error", -1, 1 },
  2895. { CIM_OVFL_ERROR_F, "CPLSW CIM overflow", -1, 1 },
  2896. { TP_FRAMING_ERROR_F, "CPLSW TP framing error", -1, 1 },
  2897. { SGE_FRAMING_ERROR_F, "CPLSW SGE framing error", -1, 1 },
  2898. { CIM_FRAMING_ERROR_F, "CPLSW CIM framing error", -1, 1 },
  2899. { ZERO_SWITCH_ERROR_F, "CPLSW no-switch error", -1, 1 },
  2900. { 0, NULL, 0, 0 }
  2901. };
  2902. if (csio_handle_intr_status(hw, CPL_INTR_CAUSE_A, cplsw_intr_info))
  2903. csio_hw_fatal_err(hw);
  2904. }
  2905. /*
  2906. * LE interrupt handler.
  2907. */
  2908. static void csio_le_intr_handler(struct csio_hw *hw)
  2909. {
  2910. enum chip_type chip = CHELSIO_CHIP_VERSION(hw->chip_id);
  2911. static struct intr_info le_intr_info[] = {
  2912. { LIPMISS_F, "LE LIP miss", -1, 0 },
  2913. { LIP0_F, "LE 0 LIP error", -1, 0 },
  2914. { PARITYERR_F, "LE parity error", -1, 1 },
  2915. { UNKNOWNCMD_F, "LE unknown command", -1, 1 },
  2916. { REQQPARERR_F, "LE request queue parity error", -1, 1 },
  2917. { 0, NULL, 0, 0 }
  2918. };
  2919. static struct intr_info t6_le_intr_info[] = {
  2920. { T6_LIPMISS_F, "LE LIP miss", -1, 0 },
  2921. { T6_LIP0_F, "LE 0 LIP error", -1, 0 },
  2922. { TCAMINTPERR_F, "LE parity error", -1, 1 },
  2923. { T6_UNKNOWNCMD_F, "LE unknown command", -1, 1 },
  2924. { SSRAMINTPERR_F, "LE request queue parity error", -1, 1 },
  2925. { 0, NULL, 0, 0 }
  2926. };
  2927. if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE_A,
  2928. (chip == CHELSIO_T5) ?
  2929. le_intr_info : t6_le_intr_info))
  2930. csio_hw_fatal_err(hw);
  2931. }
  2932. /*
  2933. * MPS interrupt handler.
  2934. */
  2935. static void csio_mps_intr_handler(struct csio_hw *hw)
  2936. {
  2937. static struct intr_info mps_rx_intr_info[] = {
  2938. { 0xffffff, "MPS Rx parity error", -1, 1 },
  2939. { 0, NULL, 0, 0 }
  2940. };
  2941. static struct intr_info mps_tx_intr_info[] = {
  2942. { TPFIFO_V(TPFIFO_M), "MPS Tx TP FIFO parity error", -1, 1 },
  2943. { NCSIFIFO_F, "MPS Tx NC-SI FIFO parity error", -1, 1 },
  2944. { TXDATAFIFO_V(TXDATAFIFO_M), "MPS Tx data FIFO parity error",
  2945. -1, 1 },
  2946. { TXDESCFIFO_V(TXDESCFIFO_M), "MPS Tx desc FIFO parity error",
  2947. -1, 1 },
  2948. { BUBBLE_F, "MPS Tx underflow", -1, 1 },
  2949. { SECNTERR_F, "MPS Tx SOP/EOP error", -1, 1 },
  2950. { FRMERR_F, "MPS Tx framing error", -1, 1 },
  2951. { 0, NULL, 0, 0 }
  2952. };
  2953. static struct intr_info mps_trc_intr_info[] = {
  2954. { FILTMEM_V(FILTMEM_M), "MPS TRC filter parity error", -1, 1 },
  2955. { PKTFIFO_V(PKTFIFO_M), "MPS TRC packet FIFO parity error",
  2956. -1, 1 },
  2957. { MISCPERR_F, "MPS TRC misc parity error", -1, 1 },
  2958. { 0, NULL, 0, 0 }
  2959. };
  2960. static struct intr_info mps_stat_sram_intr_info[] = {
  2961. { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
  2962. { 0, NULL, 0, 0 }
  2963. };
  2964. static struct intr_info mps_stat_tx_intr_info[] = {
  2965. { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
  2966. { 0, NULL, 0, 0 }
  2967. };
  2968. static struct intr_info mps_stat_rx_intr_info[] = {
  2969. { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
  2970. { 0, NULL, 0, 0 }
  2971. };
  2972. static struct intr_info mps_cls_intr_info[] = {
  2973. { MATCHSRAM_F, "MPS match SRAM parity error", -1, 1 },
  2974. { MATCHTCAM_F, "MPS match TCAM parity error", -1, 1 },
  2975. { HASHSRAM_F, "MPS hash SRAM parity error", -1, 1 },
  2976. { 0, NULL, 0, 0 }
  2977. };
  2978. int fat;
  2979. fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE_A,
  2980. mps_rx_intr_info) +
  2981. csio_handle_intr_status(hw, MPS_TX_INT_CAUSE_A,
  2982. mps_tx_intr_info) +
  2983. csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE_A,
  2984. mps_trc_intr_info) +
  2985. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM_A,
  2986. mps_stat_sram_intr_info) +
  2987. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO_A,
  2988. mps_stat_tx_intr_info) +
  2989. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO_A,
  2990. mps_stat_rx_intr_info) +
  2991. csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE_A,
  2992. mps_cls_intr_info);
  2993. csio_wr_reg32(hw, 0, MPS_INT_CAUSE_A);
  2994. csio_rd_reg32(hw, MPS_INT_CAUSE_A); /* flush */
  2995. if (fat)
  2996. csio_hw_fatal_err(hw);
  2997. }
  2998. #define MEM_INT_MASK (PERR_INT_CAUSE_F | ECC_CE_INT_CAUSE_F | \
  2999. ECC_UE_INT_CAUSE_F)
  3000. /*
  3001. * EDC/MC interrupt handler.
  3002. */
  3003. static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
  3004. {
  3005. static const char name[3][5] = { "EDC0", "EDC1", "MC" };
  3006. unsigned int addr, cnt_addr, v;
  3007. if (idx <= MEM_EDC1) {
  3008. addr = EDC_REG(EDC_INT_CAUSE_A, idx);
  3009. cnt_addr = EDC_REG(EDC_ECC_STATUS_A, idx);
  3010. } else {
  3011. addr = MC_INT_CAUSE_A;
  3012. cnt_addr = MC_ECC_STATUS_A;
  3013. }
  3014. v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
  3015. if (v & PERR_INT_CAUSE_F)
  3016. csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
  3017. if (v & ECC_CE_INT_CAUSE_F) {
  3018. uint32_t cnt = ECC_CECNT_G(csio_rd_reg32(hw, cnt_addr));
  3019. csio_wr_reg32(hw, ECC_CECNT_V(ECC_CECNT_M), cnt_addr);
  3020. csio_warn(hw, "%u %s correctable ECC data error%s\n",
  3021. cnt, name[idx], cnt > 1 ? "s" : "");
  3022. }
  3023. if (v & ECC_UE_INT_CAUSE_F)
  3024. csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
  3025. csio_wr_reg32(hw, v, addr);
  3026. if (v & (PERR_INT_CAUSE_F | ECC_UE_INT_CAUSE_F))
  3027. csio_hw_fatal_err(hw);
  3028. }
  3029. /*
  3030. * MA interrupt handler.
  3031. */
  3032. static void csio_ma_intr_handler(struct csio_hw *hw)
  3033. {
  3034. uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE_A);
  3035. if (status & MEM_PERR_INT_CAUSE_F)
  3036. csio_fatal(hw, "MA parity error, parity status %#x\n",
  3037. csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS_A));
  3038. if (status & MEM_WRAP_INT_CAUSE_F) {
  3039. v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS_A);
  3040. csio_fatal(hw,
  3041. "MA address wrap-around error by client %u to address %#x\n",
  3042. MEM_WRAP_CLIENT_NUM_G(v), MEM_WRAP_ADDRESS_G(v) << 4);
  3043. }
  3044. csio_wr_reg32(hw, status, MA_INT_CAUSE_A);
  3045. csio_hw_fatal_err(hw);
  3046. }
  3047. /*
  3048. * SMB interrupt handler.
  3049. */
  3050. static void csio_smb_intr_handler(struct csio_hw *hw)
  3051. {
  3052. static struct intr_info smb_intr_info[] = {
  3053. { MSTTXFIFOPARINT_F, "SMB master Tx FIFO parity error", -1, 1 },
  3054. { MSTRXFIFOPARINT_F, "SMB master Rx FIFO parity error", -1, 1 },
  3055. { SLVFIFOPARINT_F, "SMB slave FIFO parity error", -1, 1 },
  3056. { 0, NULL, 0, 0 }
  3057. };
  3058. if (csio_handle_intr_status(hw, SMB_INT_CAUSE_A, smb_intr_info))
  3059. csio_hw_fatal_err(hw);
  3060. }
  3061. /*
  3062. * NC-SI interrupt handler.
  3063. */
  3064. static void csio_ncsi_intr_handler(struct csio_hw *hw)
  3065. {
  3066. static struct intr_info ncsi_intr_info[] = {
  3067. { CIM_DM_PRTY_ERR_F, "NC-SI CIM parity error", -1, 1 },
  3068. { MPS_DM_PRTY_ERR_F, "NC-SI MPS parity error", -1, 1 },
  3069. { TXFIFO_PRTY_ERR_F, "NC-SI Tx FIFO parity error", -1, 1 },
  3070. { RXFIFO_PRTY_ERR_F, "NC-SI Rx FIFO parity error", -1, 1 },
  3071. { 0, NULL, 0, 0 }
  3072. };
  3073. if (csio_handle_intr_status(hw, NCSI_INT_CAUSE_A, ncsi_intr_info))
  3074. csio_hw_fatal_err(hw);
  3075. }
  3076. /*
  3077. * XGMAC interrupt handler.
  3078. */
  3079. static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
  3080. {
  3081. uint32_t v = csio_rd_reg32(hw, T5_PORT_REG(port, MAC_PORT_INT_CAUSE_A));
  3082. v &= TXFIFO_PRTY_ERR_F | RXFIFO_PRTY_ERR_F;
  3083. if (!v)
  3084. return;
  3085. if (v & TXFIFO_PRTY_ERR_F)
  3086. csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
  3087. if (v & RXFIFO_PRTY_ERR_F)
  3088. csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
  3089. csio_wr_reg32(hw, v, T5_PORT_REG(port, MAC_PORT_INT_CAUSE_A));
  3090. csio_hw_fatal_err(hw);
  3091. }
  3092. /*
  3093. * PL interrupt handler.
  3094. */
  3095. static void csio_pl_intr_handler(struct csio_hw *hw)
  3096. {
  3097. static struct intr_info pl_intr_info[] = {
  3098. { FATALPERR_F, "T4 fatal parity error", -1, 1 },
  3099. { PERRVFID_F, "PL VFID_MAP parity error", -1, 1 },
  3100. { 0, NULL, 0, 0 }
  3101. };
  3102. if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE_A, pl_intr_info))
  3103. csio_hw_fatal_err(hw);
  3104. }
  3105. /*
  3106. * csio_hw_slow_intr_handler - control path interrupt handler
  3107. * @hw: HW module
  3108. *
  3109. * Interrupt handler for non-data global interrupt events, e.g., errors.
  3110. * The designation 'slow' is because it involves register reads, while
  3111. * data interrupts typically don't involve any MMIOs.
  3112. */
  3113. int
  3114. csio_hw_slow_intr_handler(struct csio_hw *hw)
  3115. {
  3116. uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE_A);
  3117. if (!(cause & CSIO_GLBL_INTR_MASK)) {
  3118. CSIO_INC_STATS(hw, n_plint_unexp);
  3119. return 0;
  3120. }
  3121. csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
  3122. CSIO_INC_STATS(hw, n_plint_cnt);
  3123. if (cause & CIM_F)
  3124. csio_cim_intr_handler(hw);
  3125. if (cause & MPS_F)
  3126. csio_mps_intr_handler(hw);
  3127. if (cause & NCSI_F)
  3128. csio_ncsi_intr_handler(hw);
  3129. if (cause & PL_F)
  3130. csio_pl_intr_handler(hw);
  3131. if (cause & SMB_F)
  3132. csio_smb_intr_handler(hw);
  3133. if (cause & XGMAC0_F)
  3134. csio_xgmac_intr_handler(hw, 0);
  3135. if (cause & XGMAC1_F)
  3136. csio_xgmac_intr_handler(hw, 1);
  3137. if (cause & XGMAC_KR0_F)
  3138. csio_xgmac_intr_handler(hw, 2);
  3139. if (cause & XGMAC_KR1_F)
  3140. csio_xgmac_intr_handler(hw, 3);
  3141. if (cause & PCIE_F)
  3142. hw->chip_ops->chip_pcie_intr_handler(hw);
  3143. if (cause & MC_F)
  3144. csio_mem_intr_handler(hw, MEM_MC);
  3145. if (cause & EDC0_F)
  3146. csio_mem_intr_handler(hw, MEM_EDC0);
  3147. if (cause & EDC1_F)
  3148. csio_mem_intr_handler(hw, MEM_EDC1);
  3149. if (cause & LE_F)
  3150. csio_le_intr_handler(hw);
  3151. if (cause & TP_F)
  3152. csio_tp_intr_handler(hw);
  3153. if (cause & MA_F)
  3154. csio_ma_intr_handler(hw);
  3155. if (cause & PM_TX_F)
  3156. csio_pmtx_intr_handler(hw);
  3157. if (cause & PM_RX_F)
  3158. csio_pmrx_intr_handler(hw);
  3159. if (cause & ULP_RX_F)
  3160. csio_ulprx_intr_handler(hw);
  3161. if (cause & CPL_SWITCH_F)
  3162. csio_cplsw_intr_handler(hw);
  3163. if (cause & SGE_F)
  3164. csio_sge_intr_handler(hw);
  3165. if (cause & ULP_TX_F)
  3166. csio_ulptx_intr_handler(hw);
  3167. /* Clear the interrupts just processed for which we are the master. */
  3168. csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE_A);
  3169. csio_rd_reg32(hw, PL_INT_CAUSE_A); /* flush */
  3170. return 1;
  3171. }
  3172. /*****************************************************************************
  3173. * HW <--> mailbox interfacing routines.
  3174. ****************************************************************************/
  3175. /*
  3176. * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
  3177. *
  3178. * @data: Private data pointer.
  3179. *
  3180. * Called from worker thread context.
  3181. */
  3182. static void
  3183. csio_mberr_worker(void *data)
  3184. {
  3185. struct csio_hw *hw = (struct csio_hw *)data;
  3186. struct csio_mbm *mbm = &hw->mbm;
  3187. LIST_HEAD(cbfn_q);
  3188. struct csio_mb *mbp_next;
  3189. int rv;
  3190. del_timer_sync(&mbm->timer);
  3191. spin_lock_irq(&hw->lock);
  3192. if (list_empty(&mbm->cbfn_q)) {
  3193. spin_unlock_irq(&hw->lock);
  3194. return;
  3195. }
  3196. list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
  3197. mbm->stats.n_cbfnq = 0;
  3198. /* Try to start waiting mailboxes */
  3199. if (!list_empty(&mbm->req_q)) {
  3200. mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
  3201. list_del_init(&mbp_next->list);
  3202. rv = csio_mb_issue(hw, mbp_next);
  3203. if (rv != 0)
  3204. list_add_tail(&mbp_next->list, &mbm->req_q);
  3205. else
  3206. CSIO_DEC_STATS(mbm, n_activeq);
  3207. }
  3208. spin_unlock_irq(&hw->lock);
  3209. /* Now callback completions */
  3210. csio_mb_completions(hw, &cbfn_q);
  3211. }
  3212. /*
  3213. * csio_hw_mb_timer - Top-level Mailbox timeout handler.
  3214. *
  3215. * @data: private data pointer
  3216. *
  3217. **/
  3218. static void
  3219. csio_hw_mb_timer(struct timer_list *t)
  3220. {
  3221. struct csio_mbm *mbm = from_timer(mbm, t, timer);
  3222. struct csio_hw *hw = mbm->hw;
  3223. struct csio_mb *mbp = NULL;
  3224. spin_lock_irq(&hw->lock);
  3225. mbp = csio_mb_tmo_handler(hw);
  3226. spin_unlock_irq(&hw->lock);
  3227. /* Call back the function for the timed-out Mailbox */
  3228. if (mbp)
  3229. mbp->mb_cbfn(hw, mbp);
  3230. }
  3231. /*
  3232. * csio_hw_mbm_cleanup - Cleanup Mailbox module.
  3233. * @hw: HW module
  3234. *
  3235. * Called with lock held, should exit with lock held.
  3236. * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
  3237. * into a local queue. Drops lock and calls the completions. Holds
  3238. * lock and returns.
  3239. */
  3240. static void
  3241. csio_hw_mbm_cleanup(struct csio_hw *hw)
  3242. {
  3243. LIST_HEAD(cbfn_q);
  3244. csio_mb_cancel_all(hw, &cbfn_q);
  3245. spin_unlock_irq(&hw->lock);
  3246. csio_mb_completions(hw, &cbfn_q);
  3247. spin_lock_irq(&hw->lock);
  3248. }
  3249. /*****************************************************************************
  3250. * Event handling
  3251. ****************************************************************************/
  3252. int
  3253. csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  3254. uint16_t len)
  3255. {
  3256. struct csio_evt_msg *evt_entry = NULL;
  3257. if (type >= CSIO_EVT_MAX)
  3258. return -EINVAL;
  3259. if (len > CSIO_EVT_MSG_SIZE)
  3260. return -EINVAL;
  3261. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3262. return -EINVAL;
  3263. if (list_empty(&hw->evt_free_q)) {
  3264. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  3265. type, len);
  3266. return -ENOMEM;
  3267. }
  3268. evt_entry = list_first_entry(&hw->evt_free_q,
  3269. struct csio_evt_msg, list);
  3270. list_del_init(&evt_entry->list);
  3271. /* copy event msg and queue the event */
  3272. evt_entry->type = type;
  3273. memcpy((void *)evt_entry->data, evt_msg, len);
  3274. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  3275. CSIO_DEC_STATS(hw, n_evt_freeq);
  3276. CSIO_INC_STATS(hw, n_evt_activeq);
  3277. return 0;
  3278. }
  3279. static int
  3280. csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  3281. uint16_t len, bool msg_sg)
  3282. {
  3283. struct csio_evt_msg *evt_entry = NULL;
  3284. struct csio_fl_dma_buf *fl_sg;
  3285. uint32_t off = 0;
  3286. unsigned long flags;
  3287. int n, ret = 0;
  3288. if (type >= CSIO_EVT_MAX)
  3289. return -EINVAL;
  3290. if (len > CSIO_EVT_MSG_SIZE)
  3291. return -EINVAL;
  3292. spin_lock_irqsave(&hw->lock, flags);
  3293. if (hw->flags & CSIO_HWF_FWEVT_STOP) {
  3294. ret = -EINVAL;
  3295. goto out;
  3296. }
  3297. if (list_empty(&hw->evt_free_q)) {
  3298. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  3299. type, len);
  3300. ret = -ENOMEM;
  3301. goto out;
  3302. }
  3303. evt_entry = list_first_entry(&hw->evt_free_q,
  3304. struct csio_evt_msg, list);
  3305. list_del_init(&evt_entry->list);
  3306. /* copy event msg and queue the event */
  3307. evt_entry->type = type;
  3308. /* If Payload in SG list*/
  3309. if (msg_sg) {
  3310. fl_sg = (struct csio_fl_dma_buf *) evt_msg;
  3311. for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
  3312. memcpy((void *)((uintptr_t)evt_entry->data + off),
  3313. fl_sg->flbufs[n].vaddr,
  3314. fl_sg->flbufs[n].len);
  3315. off += fl_sg->flbufs[n].len;
  3316. }
  3317. } else
  3318. memcpy((void *)evt_entry->data, evt_msg, len);
  3319. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  3320. CSIO_DEC_STATS(hw, n_evt_freeq);
  3321. CSIO_INC_STATS(hw, n_evt_activeq);
  3322. out:
  3323. spin_unlock_irqrestore(&hw->lock, flags);
  3324. return ret;
  3325. }
  3326. static void
  3327. csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
  3328. {
  3329. if (evt_entry) {
  3330. spin_lock_irq(&hw->lock);
  3331. list_del_init(&evt_entry->list);
  3332. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3333. CSIO_DEC_STATS(hw, n_evt_activeq);
  3334. CSIO_INC_STATS(hw, n_evt_freeq);
  3335. spin_unlock_irq(&hw->lock);
  3336. }
  3337. }
  3338. void
  3339. csio_evtq_flush(struct csio_hw *hw)
  3340. {
  3341. uint32_t count;
  3342. count = 30;
  3343. while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
  3344. spin_unlock_irq(&hw->lock);
  3345. msleep(2000);
  3346. spin_lock_irq(&hw->lock);
  3347. }
  3348. CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
  3349. }
  3350. static void
  3351. csio_evtq_stop(struct csio_hw *hw)
  3352. {
  3353. hw->flags |= CSIO_HWF_FWEVT_STOP;
  3354. }
  3355. static void
  3356. csio_evtq_start(struct csio_hw *hw)
  3357. {
  3358. hw->flags &= ~CSIO_HWF_FWEVT_STOP;
  3359. }
  3360. static void
  3361. csio_evtq_cleanup(struct csio_hw *hw)
  3362. {
  3363. struct list_head *evt_entry, *next_entry;
  3364. /* Release outstanding events from activeq to freeq*/
  3365. if (!list_empty(&hw->evt_active_q))
  3366. list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
  3367. hw->stats.n_evt_activeq = 0;
  3368. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3369. /* Freeup event entry */
  3370. list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
  3371. kfree(evt_entry);
  3372. CSIO_DEC_STATS(hw, n_evt_freeq);
  3373. }
  3374. hw->stats.n_evt_freeq = 0;
  3375. }
  3376. static void
  3377. csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
  3378. struct csio_fl_dma_buf *flb, void *priv)
  3379. {
  3380. __u8 op;
  3381. void *msg = NULL;
  3382. uint32_t msg_len = 0;
  3383. bool msg_sg = 0;
  3384. op = ((struct rss_header *) wr)->opcode;
  3385. if (op == CPL_FW6_PLD) {
  3386. CSIO_INC_STATS(hw, n_cpl_fw6_pld);
  3387. if (!flb || !flb->totlen) {
  3388. CSIO_INC_STATS(hw, n_cpl_unexp);
  3389. return;
  3390. }
  3391. msg = (void *) flb;
  3392. msg_len = flb->totlen;
  3393. msg_sg = 1;
  3394. } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
  3395. CSIO_INC_STATS(hw, n_cpl_fw6_msg);
  3396. /* skip RSS header */
  3397. msg = (void *)((uintptr_t)wr + sizeof(__be64));
  3398. msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
  3399. sizeof(struct cpl_fw4_msg);
  3400. } else {
  3401. csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
  3402. CSIO_INC_STATS(hw, n_cpl_unexp);
  3403. return;
  3404. }
  3405. /*
  3406. * Enqueue event to EventQ. Events processing happens
  3407. * in Event worker thread context
  3408. */
  3409. if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
  3410. (uint16_t)msg_len, msg_sg))
  3411. CSIO_INC_STATS(hw, n_evt_drop);
  3412. }
  3413. void
  3414. csio_evtq_worker(struct work_struct *work)
  3415. {
  3416. struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
  3417. struct list_head *evt_entry, *next_entry;
  3418. LIST_HEAD(evt_q);
  3419. struct csio_evt_msg *evt_msg;
  3420. struct cpl_fw6_msg *msg;
  3421. struct csio_rnode *rn;
  3422. int rv = 0;
  3423. uint8_t evtq_stop = 0;
  3424. csio_dbg(hw, "event worker thread active evts#%d\n",
  3425. hw->stats.n_evt_activeq);
  3426. spin_lock_irq(&hw->lock);
  3427. while (!list_empty(&hw->evt_active_q)) {
  3428. list_splice_tail_init(&hw->evt_active_q, &evt_q);
  3429. spin_unlock_irq(&hw->lock);
  3430. list_for_each_safe(evt_entry, next_entry, &evt_q) {
  3431. evt_msg = (struct csio_evt_msg *) evt_entry;
  3432. /* Drop events if queue is STOPPED */
  3433. spin_lock_irq(&hw->lock);
  3434. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3435. evtq_stop = 1;
  3436. spin_unlock_irq(&hw->lock);
  3437. if (evtq_stop) {
  3438. CSIO_INC_STATS(hw, n_evt_drop);
  3439. goto free_evt;
  3440. }
  3441. switch (evt_msg->type) {
  3442. case CSIO_EVT_FW:
  3443. msg = (struct cpl_fw6_msg *)(evt_msg->data);
  3444. if ((msg->opcode == CPL_FW6_MSG ||
  3445. msg->opcode == CPL_FW4_MSG) &&
  3446. !msg->type) {
  3447. rv = csio_mb_fwevt_handler(hw,
  3448. msg->data);
  3449. if (!rv)
  3450. break;
  3451. /* Handle any remaining fw events */
  3452. csio_fcoe_fwevt_handler(hw,
  3453. msg->opcode, msg->data);
  3454. } else if (msg->opcode == CPL_FW6_PLD) {
  3455. csio_fcoe_fwevt_handler(hw,
  3456. msg->opcode, msg->data);
  3457. } else {
  3458. csio_warn(hw,
  3459. "Unhandled FW msg op %x type %x\n",
  3460. msg->opcode, msg->type);
  3461. CSIO_INC_STATS(hw, n_evt_drop);
  3462. }
  3463. break;
  3464. case CSIO_EVT_MBX:
  3465. csio_mberr_worker(hw);
  3466. break;
  3467. case CSIO_EVT_DEV_LOSS:
  3468. memcpy(&rn, evt_msg->data, sizeof(rn));
  3469. csio_rnode_devloss_handler(rn);
  3470. break;
  3471. default:
  3472. csio_warn(hw, "Unhandled event %x on evtq\n",
  3473. evt_msg->type);
  3474. CSIO_INC_STATS(hw, n_evt_unexp);
  3475. break;
  3476. }
  3477. free_evt:
  3478. csio_free_evt(hw, evt_msg);
  3479. }
  3480. spin_lock_irq(&hw->lock);
  3481. }
  3482. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3483. spin_unlock_irq(&hw->lock);
  3484. }
  3485. int
  3486. csio_fwevtq_handler(struct csio_hw *hw)
  3487. {
  3488. int rv;
  3489. if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
  3490. CSIO_INC_STATS(hw, n_int_stray);
  3491. return -EINVAL;
  3492. }
  3493. rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
  3494. csio_process_fwevtq_entry, NULL);
  3495. return rv;
  3496. }
  3497. /****************************************************************************
  3498. * Entry points
  3499. ****************************************************************************/
  3500. /* Management module */
  3501. /*
  3502. * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
  3503. * mgmt - mgmt module
  3504. * @io_req - io request
  3505. *
  3506. * Return - 0:if given IO Req exists in active Q.
  3507. * -EINVAL :if lookup fails.
  3508. */
  3509. int
  3510. csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
  3511. {
  3512. struct list_head *tmp;
  3513. /* Lookup ioreq in the ACTIVEQ */
  3514. list_for_each(tmp, &mgmtm->active_q) {
  3515. if (io_req == (struct csio_ioreq *)tmp)
  3516. return 0;
  3517. }
  3518. return -EINVAL;
  3519. }
  3520. #define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
  3521. /*
  3522. * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
  3523. * @data - Event data.
  3524. *
  3525. * Return - none.
  3526. */
  3527. static void
  3528. csio_mgmt_tmo_handler(struct timer_list *t)
  3529. {
  3530. struct csio_mgmtm *mgmtm = from_timer(mgmtm, t, mgmt_timer);
  3531. struct list_head *tmp;
  3532. struct csio_ioreq *io_req;
  3533. csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
  3534. spin_lock_irq(&mgmtm->hw->lock);
  3535. list_for_each(tmp, &mgmtm->active_q) {
  3536. io_req = (struct csio_ioreq *) tmp;
  3537. io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
  3538. if (!io_req->tmo) {
  3539. /* Dequeue the request from retry Q. */
  3540. tmp = csio_list_prev(tmp);
  3541. list_del_init(&io_req->sm.sm_list);
  3542. if (io_req->io_cbfn) {
  3543. /* io_req will be freed by completion handler */
  3544. io_req->wr_status = -ETIMEDOUT;
  3545. io_req->io_cbfn(mgmtm->hw, io_req);
  3546. } else {
  3547. CSIO_DB_ASSERT(0);
  3548. }
  3549. }
  3550. }
  3551. /* If retry queue is not empty, re-arm timer */
  3552. if (!list_empty(&mgmtm->active_q))
  3553. mod_timer(&mgmtm->mgmt_timer,
  3554. jiffies + msecs_to_jiffies(ECM_MIN_TMO));
  3555. spin_unlock_irq(&mgmtm->hw->lock);
  3556. }
  3557. static void
  3558. csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
  3559. {
  3560. struct csio_hw *hw = mgmtm->hw;
  3561. struct csio_ioreq *io_req;
  3562. struct list_head *tmp;
  3563. uint32_t count;
  3564. count = 30;
  3565. /* Wait for all outstanding req to complete gracefully */
  3566. while ((!list_empty(&mgmtm->active_q)) && count--) {
  3567. spin_unlock_irq(&hw->lock);
  3568. msleep(2000);
  3569. spin_lock_irq(&hw->lock);
  3570. }
  3571. /* release outstanding req from ACTIVEQ */
  3572. list_for_each(tmp, &mgmtm->active_q) {
  3573. io_req = (struct csio_ioreq *) tmp;
  3574. tmp = csio_list_prev(tmp);
  3575. list_del_init(&io_req->sm.sm_list);
  3576. mgmtm->stats.n_active--;
  3577. if (io_req->io_cbfn) {
  3578. /* io_req will be freed by completion handler */
  3579. io_req->wr_status = -ETIMEDOUT;
  3580. io_req->io_cbfn(mgmtm->hw, io_req);
  3581. }
  3582. }
  3583. }
  3584. /*
  3585. * csio_mgmt_init - Mgmt module init entry point
  3586. * @mgmtsm - mgmt module
  3587. * @hw - HW module
  3588. *
  3589. * Initialize mgmt timer, resource wait queue, active queue,
  3590. * completion q. Allocate Egress and Ingress
  3591. * WR queues and save off the queue index returned by the WR
  3592. * module for future use. Allocate and save off mgmt reqs in the
  3593. * mgmt_req_freelist for future use. Make sure their SM is initialized
  3594. * to uninit state.
  3595. * Returns: 0 - on success
  3596. * -ENOMEM - on error.
  3597. */
  3598. static int
  3599. csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
  3600. {
  3601. timer_setup(&mgmtm->mgmt_timer, csio_mgmt_tmo_handler, 0);
  3602. INIT_LIST_HEAD(&mgmtm->active_q);
  3603. INIT_LIST_HEAD(&mgmtm->cbfn_q);
  3604. mgmtm->hw = hw;
  3605. /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
  3606. return 0;
  3607. }
  3608. /*
  3609. * csio_mgmtm_exit - MGMT module exit entry point
  3610. * @mgmtsm - mgmt module
  3611. *
  3612. * This function called during MGMT module uninit.
  3613. * Stop timers, free ioreqs allocated.
  3614. * Returns: None
  3615. *
  3616. */
  3617. static void
  3618. csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
  3619. {
  3620. del_timer_sync(&mgmtm->mgmt_timer);
  3621. }
  3622. /**
  3623. * csio_hw_start - Kicks off the HW State machine
  3624. * @hw: Pointer to HW module.
  3625. *
  3626. * It is assumed that the initialization is a synchronous operation.
  3627. * So when we return afer posting the event, the HW SM should be in
  3628. * the ready state, if there were no errors during init.
  3629. */
  3630. int
  3631. csio_hw_start(struct csio_hw *hw)
  3632. {
  3633. spin_lock_irq(&hw->lock);
  3634. csio_post_event(&hw->sm, CSIO_HWE_CFG);
  3635. spin_unlock_irq(&hw->lock);
  3636. if (csio_is_hw_ready(hw))
  3637. return 0;
  3638. else if (csio_match_state(hw, csio_hws_uninit))
  3639. return -EINVAL;
  3640. else
  3641. return -ENODEV;
  3642. }
  3643. int
  3644. csio_hw_stop(struct csio_hw *hw)
  3645. {
  3646. csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
  3647. if (csio_is_hw_removing(hw))
  3648. return 0;
  3649. else
  3650. return -EINVAL;
  3651. }
  3652. /* Max reset retries */
  3653. #define CSIO_MAX_RESET_RETRIES 3
  3654. /**
  3655. * csio_hw_reset - Reset the hardware
  3656. * @hw: HW module.
  3657. *
  3658. * Caller should hold lock across this function.
  3659. */
  3660. int
  3661. csio_hw_reset(struct csio_hw *hw)
  3662. {
  3663. if (!csio_is_hw_master(hw))
  3664. return -EPERM;
  3665. if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
  3666. csio_dbg(hw, "Max hw reset attempts reached..");
  3667. return -EINVAL;
  3668. }
  3669. hw->rst_retries++;
  3670. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
  3671. if (csio_is_hw_ready(hw)) {
  3672. hw->rst_retries = 0;
  3673. hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
  3674. return 0;
  3675. } else
  3676. return -EINVAL;
  3677. }
  3678. /*
  3679. * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
  3680. * @hw: HW module.
  3681. */
  3682. static void
  3683. csio_hw_get_device_id(struct csio_hw *hw)
  3684. {
  3685. /* Is the adapter device id cached already ?*/
  3686. if (csio_is_dev_id_cached(hw))
  3687. return;
  3688. /* Get the PCI vendor & device id */
  3689. pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
  3690. &hw->params.pci.vendor_id);
  3691. pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
  3692. &hw->params.pci.device_id);
  3693. csio_dev_id_cached(hw);
  3694. hw->chip_id = (hw->params.pci.device_id & CSIO_HW_CHIP_MASK);
  3695. } /* csio_hw_get_device_id */
  3696. /*
  3697. * csio_hw_set_description - Set the model, description of the hw.
  3698. * @hw: HW module.
  3699. * @ven_id: PCI Vendor ID
  3700. * @dev_id: PCI Device ID
  3701. */
  3702. static void
  3703. csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
  3704. {
  3705. uint32_t adap_type, prot_type;
  3706. if (ven_id == CSIO_VENDOR_ID) {
  3707. prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
  3708. adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
  3709. if (prot_type == CSIO_T5_FCOE_ASIC) {
  3710. memcpy(hw->hw_ver,
  3711. csio_t5_fcoe_adapters[adap_type].model_no, 16);
  3712. memcpy(hw->model_desc,
  3713. csio_t5_fcoe_adapters[adap_type].description,
  3714. 32);
  3715. } else {
  3716. char tempName[32] = "Chelsio FCoE Controller";
  3717. memcpy(hw->model_desc, tempName, 32);
  3718. }
  3719. }
  3720. } /* csio_hw_set_description */
  3721. /**
  3722. * csio_hw_init - Initialize HW module.
  3723. * @hw: Pointer to HW module.
  3724. *
  3725. * Initialize the members of the HW module.
  3726. */
  3727. int
  3728. csio_hw_init(struct csio_hw *hw)
  3729. {
  3730. int rv = -EINVAL;
  3731. uint32_t i;
  3732. uint16_t ven_id, dev_id;
  3733. struct csio_evt_msg *evt_entry;
  3734. INIT_LIST_HEAD(&hw->sm.sm_list);
  3735. csio_init_state(&hw->sm, csio_hws_uninit);
  3736. spin_lock_init(&hw->lock);
  3737. INIT_LIST_HEAD(&hw->sln_head);
  3738. /* Get the PCI vendor & device id */
  3739. csio_hw_get_device_id(hw);
  3740. strcpy(hw->name, CSIO_HW_NAME);
  3741. /* Initialize the HW chip ops T5 specific ops */
  3742. hw->chip_ops = &t5_ops;
  3743. /* Set the model & its description */
  3744. ven_id = hw->params.pci.vendor_id;
  3745. dev_id = hw->params.pci.device_id;
  3746. csio_hw_set_description(hw, ven_id, dev_id);
  3747. /* Initialize default log level */
  3748. hw->params.log_level = (uint32_t) csio_dbg_level;
  3749. csio_set_fwevt_intr_idx(hw, -1);
  3750. csio_set_nondata_intr_idx(hw, -1);
  3751. /* Init all the modules: Mailbox, WorkRequest and Transport */
  3752. if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
  3753. goto err;
  3754. rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
  3755. if (rv)
  3756. goto err_mbm_exit;
  3757. rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
  3758. if (rv)
  3759. goto err_wrm_exit;
  3760. rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
  3761. if (rv)
  3762. goto err_scsim_exit;
  3763. /* Pre-allocate evtq and initialize them */
  3764. INIT_LIST_HEAD(&hw->evt_active_q);
  3765. INIT_LIST_HEAD(&hw->evt_free_q);
  3766. for (i = 0; i < csio_evtq_sz; i++) {
  3767. evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
  3768. if (!evt_entry) {
  3769. rv = -ENOMEM;
  3770. csio_err(hw, "Failed to initialize eventq");
  3771. goto err_evtq_cleanup;
  3772. }
  3773. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3774. CSIO_INC_STATS(hw, n_evt_freeq);
  3775. }
  3776. hw->dev_num = dev_num;
  3777. dev_num++;
  3778. return 0;
  3779. err_evtq_cleanup:
  3780. csio_evtq_cleanup(hw);
  3781. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3782. err_scsim_exit:
  3783. csio_scsim_exit(csio_hw_to_scsim(hw));
  3784. err_wrm_exit:
  3785. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3786. err_mbm_exit:
  3787. csio_mbm_exit(csio_hw_to_mbm(hw));
  3788. err:
  3789. return rv;
  3790. }
  3791. /**
  3792. * csio_hw_exit - Un-initialize HW module.
  3793. * @hw: Pointer to HW module.
  3794. *
  3795. */
  3796. void
  3797. csio_hw_exit(struct csio_hw *hw)
  3798. {
  3799. csio_evtq_cleanup(hw);
  3800. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3801. csio_scsim_exit(csio_hw_to_scsim(hw));
  3802. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3803. csio_mbm_exit(csio_hw_to_mbm(hw));
  3804. }