mpi3mr_fw.c 171 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Broadcom MPI3 Storage Controllers
  4. *
  5. * Copyright (C) 2017-2022 Broadcom Inc.
  6. * (mailto: [email protected])
  7. *
  8. */
  9. #include "mpi3mr.h"
  10. #include <linux/io-64-nonatomic-lo-hi.h>
  11. static int
  12. mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type, u32 reset_reason);
  13. static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc);
  14. static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
  15. struct mpi3_ioc_facts_data *facts_data);
  16. static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc,
  17. struct mpi3mr_drv_cmd *drv_cmd);
  18. static int poll_queues;
  19. module_param(poll_queues, int, 0444);
  20. MODULE_PARM_DESC(poll_queues, "Number of queues for io_uring poll mode. (Range 1 - 126)");
  21. #if defined(writeq) && defined(CONFIG_64BIT)
  22. static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
  23. {
  24. writeq(b, addr);
  25. }
  26. #else
  27. static inline void mpi3mr_writeq(__u64 b, volatile void __iomem *addr)
  28. {
  29. __u64 data_out = b;
  30. writel((u32)(data_out), addr);
  31. writel((u32)(data_out >> 32), (addr + 4));
  32. }
  33. #endif
  34. static inline bool
  35. mpi3mr_check_req_qfull(struct op_req_qinfo *op_req_q)
  36. {
  37. u16 pi, ci, max_entries;
  38. bool is_qfull = false;
  39. pi = op_req_q->pi;
  40. ci = READ_ONCE(op_req_q->ci);
  41. max_entries = op_req_q->num_requests;
  42. if ((ci == (pi + 1)) || ((!ci) && (pi == (max_entries - 1))))
  43. is_qfull = true;
  44. return is_qfull;
  45. }
  46. static void mpi3mr_sync_irqs(struct mpi3mr_ioc *mrioc)
  47. {
  48. u16 i, max_vectors;
  49. max_vectors = mrioc->intr_info_count;
  50. for (i = 0; i < max_vectors; i++)
  51. synchronize_irq(pci_irq_vector(mrioc->pdev, i));
  52. }
  53. void mpi3mr_ioc_disable_intr(struct mpi3mr_ioc *mrioc)
  54. {
  55. mrioc->intr_enabled = 0;
  56. mpi3mr_sync_irqs(mrioc);
  57. }
  58. void mpi3mr_ioc_enable_intr(struct mpi3mr_ioc *mrioc)
  59. {
  60. mrioc->intr_enabled = 1;
  61. }
  62. static void mpi3mr_cleanup_isr(struct mpi3mr_ioc *mrioc)
  63. {
  64. u16 i;
  65. mpi3mr_ioc_disable_intr(mrioc);
  66. if (!mrioc->intr_info)
  67. return;
  68. for (i = 0; i < mrioc->intr_info_count; i++)
  69. free_irq(pci_irq_vector(mrioc->pdev, i),
  70. (mrioc->intr_info + i));
  71. kfree(mrioc->intr_info);
  72. mrioc->intr_info = NULL;
  73. mrioc->intr_info_count = 0;
  74. mrioc->is_intr_info_set = false;
  75. pci_free_irq_vectors(mrioc->pdev);
  76. }
  77. void mpi3mr_add_sg_single(void *paddr, u8 flags, u32 length,
  78. dma_addr_t dma_addr)
  79. {
  80. struct mpi3_sge_common *sgel = paddr;
  81. sgel->flags = flags;
  82. sgel->length = cpu_to_le32(length);
  83. sgel->address = cpu_to_le64(dma_addr);
  84. }
  85. void mpi3mr_build_zero_len_sge(void *paddr)
  86. {
  87. u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
  88. mpi3mr_add_sg_single(paddr, sgl_flags, 0, -1);
  89. }
  90. void *mpi3mr_get_reply_virt_addr(struct mpi3mr_ioc *mrioc,
  91. dma_addr_t phys_addr)
  92. {
  93. if (!phys_addr)
  94. return NULL;
  95. if ((phys_addr < mrioc->reply_buf_dma) ||
  96. (phys_addr > mrioc->reply_buf_dma_max_address))
  97. return NULL;
  98. return mrioc->reply_buf + (phys_addr - mrioc->reply_buf_dma);
  99. }
  100. void *mpi3mr_get_sensebuf_virt_addr(struct mpi3mr_ioc *mrioc,
  101. dma_addr_t phys_addr)
  102. {
  103. if (!phys_addr)
  104. return NULL;
  105. return mrioc->sense_buf + (phys_addr - mrioc->sense_buf_dma);
  106. }
  107. static void mpi3mr_repost_reply_buf(struct mpi3mr_ioc *mrioc,
  108. u64 reply_dma)
  109. {
  110. u32 old_idx = 0;
  111. unsigned long flags;
  112. spin_lock_irqsave(&mrioc->reply_free_queue_lock, flags);
  113. old_idx = mrioc->reply_free_queue_host_index;
  114. mrioc->reply_free_queue_host_index = (
  115. (mrioc->reply_free_queue_host_index ==
  116. (mrioc->reply_free_qsz - 1)) ? 0 :
  117. (mrioc->reply_free_queue_host_index + 1));
  118. mrioc->reply_free_q[old_idx] = cpu_to_le64(reply_dma);
  119. writel(mrioc->reply_free_queue_host_index,
  120. &mrioc->sysif_regs->reply_free_host_index);
  121. spin_unlock_irqrestore(&mrioc->reply_free_queue_lock, flags);
  122. }
  123. void mpi3mr_repost_sense_buf(struct mpi3mr_ioc *mrioc,
  124. u64 sense_buf_dma)
  125. {
  126. u32 old_idx = 0;
  127. unsigned long flags;
  128. spin_lock_irqsave(&mrioc->sbq_lock, flags);
  129. old_idx = mrioc->sbq_host_index;
  130. mrioc->sbq_host_index = ((mrioc->sbq_host_index ==
  131. (mrioc->sense_buf_q_sz - 1)) ? 0 :
  132. (mrioc->sbq_host_index + 1));
  133. mrioc->sense_buf_q[old_idx] = cpu_to_le64(sense_buf_dma);
  134. writel(mrioc->sbq_host_index,
  135. &mrioc->sysif_regs->sense_buffer_free_host_index);
  136. spin_unlock_irqrestore(&mrioc->sbq_lock, flags);
  137. }
  138. static void mpi3mr_print_event_data(struct mpi3mr_ioc *mrioc,
  139. struct mpi3_event_notification_reply *event_reply)
  140. {
  141. char *desc = NULL;
  142. u16 event;
  143. event = event_reply->event;
  144. switch (event) {
  145. case MPI3_EVENT_LOG_DATA:
  146. desc = "Log Data";
  147. break;
  148. case MPI3_EVENT_CHANGE:
  149. desc = "Event Change";
  150. break;
  151. case MPI3_EVENT_GPIO_INTERRUPT:
  152. desc = "GPIO Interrupt";
  153. break;
  154. case MPI3_EVENT_CABLE_MGMT:
  155. desc = "Cable Management";
  156. break;
  157. case MPI3_EVENT_ENERGY_PACK_CHANGE:
  158. desc = "Energy Pack Change";
  159. break;
  160. case MPI3_EVENT_DEVICE_ADDED:
  161. {
  162. struct mpi3_device_page0 *event_data =
  163. (struct mpi3_device_page0 *)event_reply->event_data;
  164. ioc_info(mrioc, "Device Added: dev=0x%04x Form=0x%x\n",
  165. event_data->dev_handle, event_data->device_form);
  166. return;
  167. }
  168. case MPI3_EVENT_DEVICE_INFO_CHANGED:
  169. {
  170. struct mpi3_device_page0 *event_data =
  171. (struct mpi3_device_page0 *)event_reply->event_data;
  172. ioc_info(mrioc, "Device Info Changed: dev=0x%04x Form=0x%x\n",
  173. event_data->dev_handle, event_data->device_form);
  174. return;
  175. }
  176. case MPI3_EVENT_DEVICE_STATUS_CHANGE:
  177. {
  178. struct mpi3_event_data_device_status_change *event_data =
  179. (struct mpi3_event_data_device_status_change *)event_reply->event_data;
  180. ioc_info(mrioc, "Device status Change: dev=0x%04x RC=0x%x\n",
  181. event_data->dev_handle, event_data->reason_code);
  182. return;
  183. }
  184. case MPI3_EVENT_SAS_DISCOVERY:
  185. {
  186. struct mpi3_event_data_sas_discovery *event_data =
  187. (struct mpi3_event_data_sas_discovery *)event_reply->event_data;
  188. ioc_info(mrioc, "SAS Discovery: (%s) status (0x%08x)\n",
  189. (event_data->reason_code == MPI3_EVENT_SAS_DISC_RC_STARTED) ?
  190. "start" : "stop",
  191. le32_to_cpu(event_data->discovery_status));
  192. return;
  193. }
  194. case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
  195. desc = "SAS Broadcast Primitive";
  196. break;
  197. case MPI3_EVENT_SAS_NOTIFY_PRIMITIVE:
  198. desc = "SAS Notify Primitive";
  199. break;
  200. case MPI3_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  201. desc = "SAS Init Device Status Change";
  202. break;
  203. case MPI3_EVENT_SAS_INIT_TABLE_OVERFLOW:
  204. desc = "SAS Init Table Overflow";
  205. break;
  206. case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  207. desc = "SAS Topology Change List";
  208. break;
  209. case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
  210. desc = "Enclosure Device Status Change";
  211. break;
  212. case MPI3_EVENT_ENCL_DEVICE_ADDED:
  213. desc = "Enclosure Added";
  214. break;
  215. case MPI3_EVENT_HARD_RESET_RECEIVED:
  216. desc = "Hard Reset Received";
  217. break;
  218. case MPI3_EVENT_SAS_PHY_COUNTER:
  219. desc = "SAS PHY Counter";
  220. break;
  221. case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
  222. desc = "SAS Device Discovery Error";
  223. break;
  224. case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
  225. desc = "PCIE Topology Change List";
  226. break;
  227. case MPI3_EVENT_PCIE_ENUMERATION:
  228. {
  229. struct mpi3_event_data_pcie_enumeration *event_data =
  230. (struct mpi3_event_data_pcie_enumeration *)event_reply->event_data;
  231. ioc_info(mrioc, "PCIE Enumeration: (%s)",
  232. (event_data->reason_code ==
  233. MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" : "stop");
  234. if (event_data->enumeration_status)
  235. ioc_info(mrioc, "enumeration_status(0x%08x)\n",
  236. le32_to_cpu(event_data->enumeration_status));
  237. return;
  238. }
  239. case MPI3_EVENT_PREPARE_FOR_RESET:
  240. desc = "Prepare For Reset";
  241. break;
  242. }
  243. if (!desc)
  244. return;
  245. ioc_info(mrioc, "%s\n", desc);
  246. }
  247. static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc,
  248. struct mpi3_default_reply *def_reply)
  249. {
  250. struct mpi3_event_notification_reply *event_reply =
  251. (struct mpi3_event_notification_reply *)def_reply;
  252. mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count);
  253. mpi3mr_print_event_data(mrioc, event_reply);
  254. mpi3mr_os_handle_events(mrioc, event_reply);
  255. }
  256. static struct mpi3mr_drv_cmd *
  257. mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag,
  258. struct mpi3_default_reply *def_reply)
  259. {
  260. u16 idx;
  261. switch (host_tag) {
  262. case MPI3MR_HOSTTAG_INITCMDS:
  263. return &mrioc->init_cmds;
  264. case MPI3MR_HOSTTAG_CFG_CMDS:
  265. return &mrioc->cfg_cmds;
  266. case MPI3MR_HOSTTAG_BSG_CMDS:
  267. return &mrioc->bsg_cmds;
  268. case MPI3MR_HOSTTAG_BLK_TMS:
  269. return &mrioc->host_tm_cmds;
  270. case MPI3MR_HOSTTAG_PEL_ABORT:
  271. return &mrioc->pel_abort_cmd;
  272. case MPI3MR_HOSTTAG_PEL_WAIT:
  273. return &mrioc->pel_cmds;
  274. case MPI3MR_HOSTTAG_TRANSPORT_CMDS:
  275. return &mrioc->transport_cmds;
  276. case MPI3MR_HOSTTAG_INVALID:
  277. if (def_reply && def_reply->function ==
  278. MPI3_FUNCTION_EVENT_NOTIFICATION)
  279. mpi3mr_handle_events(mrioc, def_reply);
  280. return NULL;
  281. default:
  282. break;
  283. }
  284. if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
  285. host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) {
  286. idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
  287. return &mrioc->dev_rmhs_cmds[idx];
  288. }
  289. if (host_tag >= MPI3MR_HOSTTAG_EVTACKCMD_MIN &&
  290. host_tag <= MPI3MR_HOSTTAG_EVTACKCMD_MAX) {
  291. idx = host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
  292. return &mrioc->evtack_cmds[idx];
  293. }
  294. return NULL;
  295. }
  296. static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
  297. struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
  298. {
  299. u16 reply_desc_type, host_tag = 0;
  300. u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
  301. u32 ioc_loginfo = 0;
  302. struct mpi3_status_reply_descriptor *status_desc;
  303. struct mpi3_address_reply_descriptor *addr_desc;
  304. struct mpi3_success_reply_descriptor *success_desc;
  305. struct mpi3_default_reply *def_reply = NULL;
  306. struct mpi3mr_drv_cmd *cmdptr = NULL;
  307. struct mpi3_scsi_io_reply *scsi_reply;
  308. u8 *sense_buf = NULL;
  309. *reply_dma = 0;
  310. reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
  311. MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
  312. switch (reply_desc_type) {
  313. case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
  314. status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
  315. host_tag = le16_to_cpu(status_desc->host_tag);
  316. ioc_status = le16_to_cpu(status_desc->ioc_status);
  317. if (ioc_status &
  318. MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
  319. ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
  320. ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
  321. break;
  322. case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
  323. addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
  324. *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
  325. def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
  326. if (!def_reply)
  327. goto out;
  328. host_tag = le16_to_cpu(def_reply->host_tag);
  329. ioc_status = le16_to_cpu(def_reply->ioc_status);
  330. if (ioc_status &
  331. MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
  332. ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
  333. ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
  334. if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
  335. scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
  336. sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
  337. le64_to_cpu(scsi_reply->sense_data_buffer_address));
  338. }
  339. break;
  340. case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
  341. success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
  342. host_tag = le16_to_cpu(success_desc->host_tag);
  343. break;
  344. default:
  345. break;
  346. }
  347. cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
  348. if (cmdptr) {
  349. if (cmdptr->state & MPI3MR_CMD_PENDING) {
  350. cmdptr->state |= MPI3MR_CMD_COMPLETE;
  351. cmdptr->ioc_loginfo = ioc_loginfo;
  352. cmdptr->ioc_status = ioc_status;
  353. cmdptr->state &= ~MPI3MR_CMD_PENDING;
  354. if (def_reply) {
  355. cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
  356. memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
  357. mrioc->reply_sz);
  358. }
  359. if (sense_buf && cmdptr->sensebuf) {
  360. cmdptr->is_sense = 1;
  361. memcpy(cmdptr->sensebuf, sense_buf,
  362. MPI3MR_SENSE_BUF_SZ);
  363. }
  364. if (cmdptr->is_waiting) {
  365. complete(&cmdptr->done);
  366. cmdptr->is_waiting = 0;
  367. } else if (cmdptr->callback)
  368. cmdptr->callback(mrioc, cmdptr);
  369. }
  370. }
  371. out:
  372. if (sense_buf)
  373. mpi3mr_repost_sense_buf(mrioc,
  374. le64_to_cpu(scsi_reply->sense_data_buffer_address));
  375. }
  376. int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
  377. {
  378. u32 exp_phase = mrioc->admin_reply_ephase;
  379. u32 admin_reply_ci = mrioc->admin_reply_ci;
  380. u32 num_admin_replies = 0;
  381. u64 reply_dma = 0;
  382. struct mpi3_default_reply_descriptor *reply_desc;
  383. if (!atomic_add_unless(&mrioc->admin_reply_q_in_use, 1, 1))
  384. return 0;
  385. reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
  386. admin_reply_ci;
  387. if ((le16_to_cpu(reply_desc->reply_flags) &
  388. MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
  389. atomic_dec(&mrioc->admin_reply_q_in_use);
  390. return 0;
  391. }
  392. do {
  393. if (mrioc->unrecoverable)
  394. break;
  395. mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
  396. mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
  397. if (reply_dma)
  398. mpi3mr_repost_reply_buf(mrioc, reply_dma);
  399. num_admin_replies++;
  400. if (++admin_reply_ci == mrioc->num_admin_replies) {
  401. admin_reply_ci = 0;
  402. exp_phase ^= 1;
  403. }
  404. reply_desc =
  405. (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
  406. admin_reply_ci;
  407. if ((le16_to_cpu(reply_desc->reply_flags) &
  408. MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
  409. break;
  410. } while (1);
  411. writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
  412. mrioc->admin_reply_ci = admin_reply_ci;
  413. mrioc->admin_reply_ephase = exp_phase;
  414. atomic_dec(&mrioc->admin_reply_q_in_use);
  415. return num_admin_replies;
  416. }
  417. /**
  418. * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to
  419. * queue's consumer index from operational reply descriptor queue.
  420. * @op_reply_q: op_reply_qinfo object
  421. * @reply_ci: operational reply descriptor's queue consumer index
  422. *
  423. * Returns reply descriptor frame address
  424. */
  425. static inline struct mpi3_default_reply_descriptor *
  426. mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci)
  427. {
  428. void *segment_base_addr;
  429. struct segments *segments = op_reply_q->q_segments;
  430. struct mpi3_default_reply_descriptor *reply_desc = NULL;
  431. segment_base_addr =
  432. segments[reply_ci / op_reply_q->segment_qd].segment;
  433. reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr +
  434. (reply_ci % op_reply_q->segment_qd);
  435. return reply_desc;
  436. }
  437. /**
  438. * mpi3mr_process_op_reply_q - Operational reply queue handler
  439. * @mrioc: Adapter instance reference
  440. * @op_reply_q: Operational reply queue info
  441. *
  442. * Checks the specific operational reply queue and drains the
  443. * reply queue entries until the queue is empty and process the
  444. * individual reply descriptors.
  445. *
  446. * Return: 0 if queue is already processed,or number of reply
  447. * descriptors processed.
  448. */
  449. int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc,
  450. struct op_reply_qinfo *op_reply_q)
  451. {
  452. struct op_req_qinfo *op_req_q;
  453. u32 exp_phase;
  454. u32 reply_ci;
  455. u32 num_op_reply = 0;
  456. u64 reply_dma = 0;
  457. struct mpi3_default_reply_descriptor *reply_desc;
  458. u16 req_q_idx = 0, reply_qidx;
  459. reply_qidx = op_reply_q->qid - 1;
  460. if (!atomic_add_unless(&op_reply_q->in_use, 1, 1))
  461. return 0;
  462. exp_phase = op_reply_q->ephase;
  463. reply_ci = op_reply_q->ci;
  464. reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
  465. if ((le16_to_cpu(reply_desc->reply_flags) &
  466. MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
  467. atomic_dec(&op_reply_q->in_use);
  468. return 0;
  469. }
  470. do {
  471. if (mrioc->unrecoverable)
  472. break;
  473. req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1;
  474. op_req_q = &mrioc->req_qinfo[req_q_idx];
  475. WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci));
  476. mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma,
  477. reply_qidx);
  478. atomic_dec(&op_reply_q->pend_ios);
  479. if (reply_dma)
  480. mpi3mr_repost_reply_buf(mrioc, reply_dma);
  481. num_op_reply++;
  482. if (++reply_ci == op_reply_q->num_replies) {
  483. reply_ci = 0;
  484. exp_phase ^= 1;
  485. }
  486. reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
  487. if ((le16_to_cpu(reply_desc->reply_flags) &
  488. MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
  489. break;
  490. #ifndef CONFIG_PREEMPT_RT
  491. /*
  492. * Exit completion loop to avoid CPU lockup
  493. * Ensure remaining completion happens from threaded ISR.
  494. */
  495. if (num_op_reply > mrioc->max_host_ios) {
  496. op_reply_q->enable_irq_poll = true;
  497. break;
  498. }
  499. #endif
  500. } while (1);
  501. writel(reply_ci,
  502. &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index);
  503. op_reply_q->ci = reply_ci;
  504. op_reply_q->ephase = exp_phase;
  505. atomic_dec(&op_reply_q->in_use);
  506. return num_op_reply;
  507. }
  508. /**
  509. * mpi3mr_blk_mq_poll - Operational reply queue handler
  510. * @shost: SCSI Host reference
  511. * @queue_num: Request queue number (w.r.t OS it is hardware context number)
  512. *
  513. * Checks the specific operational reply queue and drains the
  514. * reply queue entries until the queue is empty and process the
  515. * individual reply descriptors.
  516. *
  517. * Return: 0 if queue is already processed,or number of reply
  518. * descriptors processed.
  519. */
  520. int mpi3mr_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
  521. {
  522. int num_entries = 0;
  523. struct mpi3mr_ioc *mrioc;
  524. mrioc = (struct mpi3mr_ioc *)shost->hostdata;
  525. if ((mrioc->reset_in_progress || mrioc->prepare_for_reset ||
  526. mrioc->unrecoverable))
  527. return 0;
  528. num_entries = mpi3mr_process_op_reply_q(mrioc,
  529. &mrioc->op_reply_qinfo[queue_num]);
  530. return num_entries;
  531. }
  532. static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
  533. {
  534. struct mpi3mr_intr_info *intr_info = privdata;
  535. struct mpi3mr_ioc *mrioc;
  536. u16 midx;
  537. u32 num_admin_replies = 0, num_op_reply = 0;
  538. if (!intr_info)
  539. return IRQ_NONE;
  540. mrioc = intr_info->mrioc;
  541. if (!mrioc->intr_enabled)
  542. return IRQ_NONE;
  543. midx = intr_info->msix_index;
  544. if (!midx)
  545. num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
  546. if (intr_info->op_reply_q)
  547. num_op_reply = mpi3mr_process_op_reply_q(mrioc,
  548. intr_info->op_reply_q);
  549. if (num_admin_replies || num_op_reply)
  550. return IRQ_HANDLED;
  551. else
  552. return IRQ_NONE;
  553. }
  554. #ifndef CONFIG_PREEMPT_RT
  555. static irqreturn_t mpi3mr_isr(int irq, void *privdata)
  556. {
  557. struct mpi3mr_intr_info *intr_info = privdata;
  558. int ret;
  559. if (!intr_info)
  560. return IRQ_NONE;
  561. /* Call primary ISR routine */
  562. ret = mpi3mr_isr_primary(irq, privdata);
  563. /*
  564. * If more IOs are expected, schedule IRQ polling thread.
  565. * Otherwise exit from ISR.
  566. */
  567. if (!intr_info->op_reply_q)
  568. return ret;
  569. if (!intr_info->op_reply_q->enable_irq_poll ||
  570. !atomic_read(&intr_info->op_reply_q->pend_ios))
  571. return ret;
  572. disable_irq_nosync(intr_info->os_irq);
  573. return IRQ_WAKE_THREAD;
  574. }
  575. /**
  576. * mpi3mr_isr_poll - Reply queue polling routine
  577. * @irq: IRQ
  578. * @privdata: Interrupt info
  579. *
  580. * poll for pending I/O completions in a loop until pending I/Os
  581. * present or controller queue depth I/Os are processed.
  582. *
  583. * Return: IRQ_NONE or IRQ_HANDLED
  584. */
  585. static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
  586. {
  587. struct mpi3mr_intr_info *intr_info = privdata;
  588. struct mpi3mr_ioc *mrioc;
  589. u16 midx;
  590. u32 num_op_reply = 0;
  591. if (!intr_info || !intr_info->op_reply_q)
  592. return IRQ_NONE;
  593. mrioc = intr_info->mrioc;
  594. midx = intr_info->msix_index;
  595. /* Poll for pending IOs completions */
  596. do {
  597. if (!mrioc->intr_enabled || mrioc->unrecoverable)
  598. break;
  599. if (!midx)
  600. mpi3mr_process_admin_reply_q(mrioc);
  601. if (intr_info->op_reply_q)
  602. num_op_reply +=
  603. mpi3mr_process_op_reply_q(mrioc,
  604. intr_info->op_reply_q);
  605. usleep_range(MPI3MR_IRQ_POLL_SLEEP, 10 * MPI3MR_IRQ_POLL_SLEEP);
  606. } while (atomic_read(&intr_info->op_reply_q->pend_ios) &&
  607. (num_op_reply < mrioc->max_host_ios));
  608. intr_info->op_reply_q->enable_irq_poll = false;
  609. enable_irq(intr_info->os_irq);
  610. return IRQ_HANDLED;
  611. }
  612. #endif
  613. /**
  614. * mpi3mr_request_irq - Request IRQ and register ISR
  615. * @mrioc: Adapter instance reference
  616. * @index: IRQ vector index
  617. *
  618. * Request threaded ISR with primary ISR and secondary
  619. *
  620. * Return: 0 on success and non zero on failures.
  621. */
  622. static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
  623. {
  624. struct pci_dev *pdev = mrioc->pdev;
  625. struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
  626. int retval = 0;
  627. intr_info->mrioc = mrioc;
  628. intr_info->msix_index = index;
  629. intr_info->op_reply_q = NULL;
  630. snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
  631. mrioc->driver_name, mrioc->id, index);
  632. #ifndef CONFIG_PREEMPT_RT
  633. retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
  634. mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
  635. #else
  636. retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr_primary,
  637. NULL, IRQF_SHARED, intr_info->name, intr_info);
  638. #endif
  639. if (retval) {
  640. ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
  641. intr_info->name, pci_irq_vector(pdev, index));
  642. return retval;
  643. }
  644. intr_info->os_irq = pci_irq_vector(pdev, index);
  645. return retval;
  646. }
  647. static void mpi3mr_calc_poll_queues(struct mpi3mr_ioc *mrioc, u16 max_vectors)
  648. {
  649. if (!mrioc->requested_poll_qcount)
  650. return;
  651. /* Reserved for Admin and Default Queue */
  652. if (max_vectors > 2 &&
  653. (mrioc->requested_poll_qcount < max_vectors - 2)) {
  654. ioc_info(mrioc,
  655. "enabled polled queues (%d) msix (%d)\n",
  656. mrioc->requested_poll_qcount, max_vectors);
  657. } else {
  658. ioc_info(mrioc,
  659. "disabled polled queues (%d) msix (%d) because of no resources for default queue\n",
  660. mrioc->requested_poll_qcount, max_vectors);
  661. mrioc->requested_poll_qcount = 0;
  662. }
  663. }
  664. /**
  665. * mpi3mr_setup_isr - Setup ISR for the controller
  666. * @mrioc: Adapter instance reference
  667. * @setup_one: Request one IRQ or more
  668. *
  669. * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
  670. *
  671. * Return: 0 on success and non zero on failures.
  672. */
  673. static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
  674. {
  675. unsigned int irq_flags = PCI_IRQ_MSIX;
  676. int max_vectors, min_vec;
  677. int retval;
  678. int i;
  679. struct irq_affinity desc = { .pre_vectors = 1, .post_vectors = 1 };
  680. if (mrioc->is_intr_info_set)
  681. return 0;
  682. mpi3mr_cleanup_isr(mrioc);
  683. if (setup_one || reset_devices) {
  684. max_vectors = 1;
  685. retval = pci_alloc_irq_vectors(mrioc->pdev,
  686. 1, max_vectors, irq_flags);
  687. if (retval < 0) {
  688. ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
  689. retval);
  690. goto out_failed;
  691. }
  692. } else {
  693. max_vectors =
  694. min_t(int, mrioc->cpu_count + 1 +
  695. mrioc->requested_poll_qcount, mrioc->msix_count);
  696. mpi3mr_calc_poll_queues(mrioc, max_vectors);
  697. ioc_info(mrioc,
  698. "MSI-X vectors supported: %d, no of cores: %d,",
  699. mrioc->msix_count, mrioc->cpu_count);
  700. ioc_info(mrioc,
  701. "MSI-x vectors requested: %d poll_queues %d\n",
  702. max_vectors, mrioc->requested_poll_qcount);
  703. desc.post_vectors = mrioc->requested_poll_qcount;
  704. min_vec = desc.pre_vectors + desc.post_vectors;
  705. irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
  706. retval = pci_alloc_irq_vectors_affinity(mrioc->pdev,
  707. min_vec, max_vectors, irq_flags, &desc);
  708. if (retval < 0) {
  709. ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
  710. retval);
  711. goto out_failed;
  712. }
  713. /*
  714. * If only one MSI-x is allocated, then MSI-x 0 will be shared
  715. * between Admin queue and operational queue
  716. */
  717. if (retval == min_vec)
  718. mrioc->op_reply_q_offset = 0;
  719. else if (retval != (max_vectors)) {
  720. ioc_info(mrioc,
  721. "allocated vectors (%d) are less than configured (%d)\n",
  722. retval, max_vectors);
  723. }
  724. max_vectors = retval;
  725. mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0;
  726. mpi3mr_calc_poll_queues(mrioc, max_vectors);
  727. }
  728. mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
  729. GFP_KERNEL);
  730. if (!mrioc->intr_info) {
  731. retval = -ENOMEM;
  732. pci_free_irq_vectors(mrioc->pdev);
  733. goto out_failed;
  734. }
  735. for (i = 0; i < max_vectors; i++) {
  736. retval = mpi3mr_request_irq(mrioc, i);
  737. if (retval) {
  738. mrioc->intr_info_count = i;
  739. goto out_failed;
  740. }
  741. }
  742. if (reset_devices || !setup_one)
  743. mrioc->is_intr_info_set = true;
  744. mrioc->intr_info_count = max_vectors;
  745. mpi3mr_ioc_enable_intr(mrioc);
  746. return 0;
  747. out_failed:
  748. mpi3mr_cleanup_isr(mrioc);
  749. return retval;
  750. }
  751. static const struct {
  752. enum mpi3mr_iocstate value;
  753. char *name;
  754. } mrioc_states[] = {
  755. { MRIOC_STATE_READY, "ready" },
  756. { MRIOC_STATE_FAULT, "fault" },
  757. { MRIOC_STATE_RESET, "reset" },
  758. { MRIOC_STATE_BECOMING_READY, "becoming ready" },
  759. { MRIOC_STATE_RESET_REQUESTED, "reset requested" },
  760. { MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
  761. };
  762. static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
  763. {
  764. int i;
  765. char *name = NULL;
  766. for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
  767. if (mrioc_states[i].value == mrioc_state) {
  768. name = mrioc_states[i].name;
  769. break;
  770. }
  771. }
  772. return name;
  773. }
  774. /* Reset reason to name mapper structure*/
  775. static const struct {
  776. enum mpi3mr_reset_reason value;
  777. char *name;
  778. } mpi3mr_reset_reason_codes[] = {
  779. { MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
  780. { MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
  781. { MPI3MR_RESET_FROM_APP, "application invocation" },
  782. { MPI3MR_RESET_FROM_EH_HOS, "error handling" },
  783. { MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
  784. { MPI3MR_RESET_FROM_APP_TIMEOUT, "application command timeout" },
  785. { MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
  786. { MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
  787. { MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
  788. { MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
  789. { MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
  790. { MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
  791. { MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
  792. {
  793. MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
  794. "create request queue timeout"
  795. },
  796. {
  797. MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
  798. "create reply queue timeout"
  799. },
  800. { MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
  801. { MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
  802. { MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
  803. { MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
  804. {
  805. MPI3MR_RESET_FROM_CIACTVRST_TIMER,
  806. "component image activation timeout"
  807. },
  808. {
  809. MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
  810. "get package version timeout"
  811. },
  812. { MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
  813. { MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
  814. { MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronous reset" },
  815. { MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT, "configuration request timeout"},
  816. { MPI3MR_RESET_FROM_SAS_TRANSPORT_TIMEOUT, "timeout of a SAS transport layer request" },
  817. };
  818. /**
  819. * mpi3mr_reset_rc_name - get reset reason code name
  820. * @reason_code: reset reason code value
  821. *
  822. * Map reset reason to an NULL terminated ASCII string
  823. *
  824. * Return: name corresponding to reset reason value or NULL.
  825. */
  826. static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
  827. {
  828. int i;
  829. char *name = NULL;
  830. for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) {
  831. if (mpi3mr_reset_reason_codes[i].value == reason_code) {
  832. name = mpi3mr_reset_reason_codes[i].name;
  833. break;
  834. }
  835. }
  836. return name;
  837. }
  838. /* Reset type to name mapper structure*/
  839. static const struct {
  840. u16 reset_type;
  841. char *name;
  842. } mpi3mr_reset_types[] = {
  843. { MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
  844. { MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
  845. };
  846. /**
  847. * mpi3mr_reset_type_name - get reset type name
  848. * @reset_type: reset type value
  849. *
  850. * Map reset type to an NULL terminated ASCII string
  851. *
  852. * Return: name corresponding to reset type value or NULL.
  853. */
  854. static const char *mpi3mr_reset_type_name(u16 reset_type)
  855. {
  856. int i;
  857. char *name = NULL;
  858. for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) {
  859. if (mpi3mr_reset_types[i].reset_type == reset_type) {
  860. name = mpi3mr_reset_types[i].name;
  861. break;
  862. }
  863. }
  864. return name;
  865. }
  866. /**
  867. * mpi3mr_print_fault_info - Display fault information
  868. * @mrioc: Adapter instance reference
  869. *
  870. * Display the controller fault information if there is a
  871. * controller fault.
  872. *
  873. * Return: Nothing.
  874. */
  875. void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
  876. {
  877. u32 ioc_status, code, code1, code2, code3;
  878. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  879. if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
  880. code = readl(&mrioc->sysif_regs->fault);
  881. code1 = readl(&mrioc->sysif_regs->fault_info[0]);
  882. code2 = readl(&mrioc->sysif_regs->fault_info[1]);
  883. code3 = readl(&mrioc->sysif_regs->fault_info[2]);
  884. ioc_info(mrioc,
  885. "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
  886. code, code1, code2, code3);
  887. }
  888. }
  889. /**
  890. * mpi3mr_get_iocstate - Get IOC State
  891. * @mrioc: Adapter instance reference
  892. *
  893. * Return a proper IOC state enum based on the IOC status and
  894. * IOC configuration and unrcoverable state of the controller.
  895. *
  896. * Return: Current IOC state.
  897. */
  898. enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
  899. {
  900. u32 ioc_status, ioc_config;
  901. u8 ready, enabled;
  902. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  903. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  904. if (mrioc->unrecoverable)
  905. return MRIOC_STATE_UNRECOVERABLE;
  906. if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
  907. return MRIOC_STATE_FAULT;
  908. ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
  909. enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
  910. if (ready && enabled)
  911. return MRIOC_STATE_READY;
  912. if ((!ready) && (!enabled))
  913. return MRIOC_STATE_RESET;
  914. if ((!ready) && (enabled))
  915. return MRIOC_STATE_BECOMING_READY;
  916. return MRIOC_STATE_RESET_REQUESTED;
  917. }
  918. /**
  919. * mpi3mr_clear_reset_history - clear reset history
  920. * @mrioc: Adapter instance reference
  921. *
  922. * Write the reset history bit in IOC status to clear the bit,
  923. * if it is already set.
  924. *
  925. * Return: Nothing.
  926. */
  927. static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
  928. {
  929. u32 ioc_status;
  930. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  931. if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
  932. writel(ioc_status, &mrioc->sysif_regs->ioc_status);
  933. }
  934. /**
  935. * mpi3mr_issue_and_process_mur - Message unit Reset handler
  936. * @mrioc: Adapter instance reference
  937. * @reset_reason: Reset reason code
  938. *
  939. * Issue Message unit Reset to the controller and wait for it to
  940. * be complete.
  941. *
  942. * Return: 0 on success, -1 on failure.
  943. */
  944. static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
  945. u32 reset_reason)
  946. {
  947. u32 ioc_config, timeout, ioc_status;
  948. int retval = -1;
  949. ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
  950. if (mrioc->unrecoverable) {
  951. ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
  952. return retval;
  953. }
  954. mpi3mr_clear_reset_history(mrioc);
  955. writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
  956. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  957. ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
  958. writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
  959. timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
  960. do {
  961. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  962. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
  963. mpi3mr_clear_reset_history(mrioc);
  964. break;
  965. }
  966. if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
  967. mpi3mr_print_fault_info(mrioc);
  968. break;
  969. }
  970. msleep(100);
  971. } while (--timeout);
  972. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  973. if (timeout && !((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
  974. (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
  975. (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
  976. retval = 0;
  977. ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
  978. (!retval) ? "successful" : "failed", ioc_status, ioc_config);
  979. return retval;
  980. }
  981. /**
  982. * mpi3mr_revalidate_factsdata - validate IOCFacts parameters
  983. * during reset/resume
  984. * @mrioc: Adapter instance reference
  985. *
  986. * Return zero if the new IOCFacts parameters value is compatible with
  987. * older values else return -EPERM
  988. */
  989. static int
  990. mpi3mr_revalidate_factsdata(struct mpi3mr_ioc *mrioc)
  991. {
  992. void *removepend_bitmap;
  993. if (mrioc->facts.reply_sz > mrioc->reply_sz) {
  994. ioc_err(mrioc,
  995. "cannot increase reply size from %d to %d\n",
  996. mrioc->reply_sz, mrioc->facts.reply_sz);
  997. return -EPERM;
  998. }
  999. if (mrioc->facts.max_op_reply_q < mrioc->num_op_reply_q) {
  1000. ioc_err(mrioc,
  1001. "cannot reduce number of operational reply queues from %d to %d\n",
  1002. mrioc->num_op_reply_q,
  1003. mrioc->facts.max_op_reply_q);
  1004. return -EPERM;
  1005. }
  1006. if (mrioc->facts.max_op_req_q < mrioc->num_op_req_q) {
  1007. ioc_err(mrioc,
  1008. "cannot reduce number of operational request queues from %d to %d\n",
  1009. mrioc->num_op_req_q, mrioc->facts.max_op_req_q);
  1010. return -EPERM;
  1011. }
  1012. if ((mrioc->sas_transport_enabled) && (mrioc->facts.ioc_capabilities &
  1013. MPI3_IOCFACTS_CAPABILITY_MULTIPATH_ENABLED))
  1014. ioc_err(mrioc,
  1015. "critical error: multipath capability is enabled at the\n"
  1016. "\tcontroller while sas transport support is enabled at the\n"
  1017. "\tdriver, please reboot the system or reload the driver\n");
  1018. if (mrioc->facts.max_devhandle > mrioc->dev_handle_bitmap_bits) {
  1019. removepend_bitmap = bitmap_zalloc(mrioc->facts.max_devhandle,
  1020. GFP_KERNEL);
  1021. if (!removepend_bitmap) {
  1022. ioc_err(mrioc,
  1023. "failed to increase removepend_bitmap bits from %d to %d\n",
  1024. mrioc->dev_handle_bitmap_bits,
  1025. mrioc->facts.max_devhandle);
  1026. return -EPERM;
  1027. }
  1028. bitmap_free(mrioc->removepend_bitmap);
  1029. mrioc->removepend_bitmap = removepend_bitmap;
  1030. ioc_info(mrioc,
  1031. "increased bits of dev_handle_bitmap from %d to %d\n",
  1032. mrioc->dev_handle_bitmap_bits,
  1033. mrioc->facts.max_devhandle);
  1034. mrioc->dev_handle_bitmap_bits = mrioc->facts.max_devhandle;
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * mpi3mr_bring_ioc_ready - Bring controller to ready state
  1040. * @mrioc: Adapter instance reference
  1041. *
  1042. * Set Enable IOC bit in IOC configuration register and wait for
  1043. * the controller to become ready.
  1044. *
  1045. * Return: 0 on success, appropriate error on failure.
  1046. */
  1047. static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
  1048. {
  1049. u32 ioc_config, ioc_status, timeout, host_diagnostic;
  1050. int retval = 0;
  1051. enum mpi3mr_iocstate ioc_state;
  1052. u64 base_info;
  1053. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  1054. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  1055. base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
  1056. ioc_info(mrioc, "ioc_status(0x%08x), ioc_config(0x%08x), ioc_info(0x%016llx) at the bringup\n",
  1057. ioc_status, ioc_config, base_info);
  1058. /*The timeout value is in 2sec unit, changing it to seconds*/
  1059. mrioc->ready_timeout =
  1060. ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
  1061. MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
  1062. ioc_info(mrioc, "ready timeout: %d seconds\n", mrioc->ready_timeout);
  1063. ioc_state = mpi3mr_get_iocstate(mrioc);
  1064. ioc_info(mrioc, "controller is in %s state during detection\n",
  1065. mpi3mr_iocstate_name(ioc_state));
  1066. if (ioc_state == MRIOC_STATE_BECOMING_READY ||
  1067. ioc_state == MRIOC_STATE_RESET_REQUESTED) {
  1068. timeout = mrioc->ready_timeout * 10;
  1069. do {
  1070. msleep(100);
  1071. } while (--timeout);
  1072. if (!pci_device_is_present(mrioc->pdev)) {
  1073. mrioc->unrecoverable = 1;
  1074. ioc_err(mrioc,
  1075. "controller is not present while waiting to reset\n");
  1076. retval = -1;
  1077. goto out_device_not_present;
  1078. }
  1079. ioc_state = mpi3mr_get_iocstate(mrioc);
  1080. ioc_info(mrioc,
  1081. "controller is in %s state after waiting to reset\n",
  1082. mpi3mr_iocstate_name(ioc_state));
  1083. }
  1084. if (ioc_state == MRIOC_STATE_READY) {
  1085. ioc_info(mrioc, "issuing message unit reset (MUR) to bring to reset state\n");
  1086. retval = mpi3mr_issue_and_process_mur(mrioc,
  1087. MPI3MR_RESET_FROM_BRINGUP);
  1088. ioc_state = mpi3mr_get_iocstate(mrioc);
  1089. if (retval)
  1090. ioc_err(mrioc,
  1091. "message unit reset failed with error %d current state %s\n",
  1092. retval, mpi3mr_iocstate_name(ioc_state));
  1093. }
  1094. if (ioc_state != MRIOC_STATE_RESET) {
  1095. if (ioc_state == MRIOC_STATE_FAULT) {
  1096. timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
  1097. mpi3mr_print_fault_info(mrioc);
  1098. do {
  1099. host_diagnostic =
  1100. readl(&mrioc->sysif_regs->host_diagnostic);
  1101. if (!(host_diagnostic &
  1102. MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
  1103. break;
  1104. if (!pci_device_is_present(mrioc->pdev)) {
  1105. mrioc->unrecoverable = 1;
  1106. ioc_err(mrioc, "controller is not present at the bringup\n");
  1107. goto out_device_not_present;
  1108. }
  1109. msleep(100);
  1110. } while (--timeout);
  1111. }
  1112. mpi3mr_print_fault_info(mrioc);
  1113. ioc_info(mrioc, "issuing soft reset to bring to reset state\n");
  1114. retval = mpi3mr_issue_reset(mrioc,
  1115. MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
  1116. MPI3MR_RESET_FROM_BRINGUP);
  1117. if (retval) {
  1118. ioc_err(mrioc,
  1119. "soft reset failed with error %d\n", retval);
  1120. goto out_failed;
  1121. }
  1122. }
  1123. ioc_state = mpi3mr_get_iocstate(mrioc);
  1124. if (ioc_state != MRIOC_STATE_RESET) {
  1125. ioc_err(mrioc,
  1126. "cannot bring controller to reset state, current state: %s\n",
  1127. mpi3mr_iocstate_name(ioc_state));
  1128. goto out_failed;
  1129. }
  1130. mpi3mr_clear_reset_history(mrioc);
  1131. retval = mpi3mr_setup_admin_qpair(mrioc);
  1132. if (retval) {
  1133. ioc_err(mrioc, "failed to setup admin queues: error %d\n",
  1134. retval);
  1135. goto out_failed;
  1136. }
  1137. ioc_info(mrioc, "bringing controller to ready state\n");
  1138. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  1139. ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
  1140. writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
  1141. timeout = mrioc->ready_timeout * 10;
  1142. do {
  1143. ioc_state = mpi3mr_get_iocstate(mrioc);
  1144. if (ioc_state == MRIOC_STATE_READY) {
  1145. ioc_info(mrioc,
  1146. "successfully transitioned to %s state\n",
  1147. mpi3mr_iocstate_name(ioc_state));
  1148. return 0;
  1149. }
  1150. if (!pci_device_is_present(mrioc->pdev)) {
  1151. mrioc->unrecoverable = 1;
  1152. ioc_err(mrioc,
  1153. "controller is not present at the bringup\n");
  1154. retval = -1;
  1155. goto out_device_not_present;
  1156. }
  1157. msleep(100);
  1158. } while (--timeout);
  1159. out_failed:
  1160. ioc_state = mpi3mr_get_iocstate(mrioc);
  1161. ioc_err(mrioc,
  1162. "failed to bring to ready state, current state: %s\n",
  1163. mpi3mr_iocstate_name(ioc_state));
  1164. out_device_not_present:
  1165. return retval;
  1166. }
  1167. /**
  1168. * mpi3mr_soft_reset_success - Check softreset is success or not
  1169. * @ioc_status: IOC status register value
  1170. * @ioc_config: IOC config register value
  1171. *
  1172. * Check whether the soft reset is successful or not based on
  1173. * IOC status and IOC config register values.
  1174. *
  1175. * Return: True when the soft reset is success, false otherwise.
  1176. */
  1177. static inline bool
  1178. mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config)
  1179. {
  1180. if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
  1181. (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
  1182. return true;
  1183. return false;
  1184. }
  1185. /**
  1186. * mpi3mr_diagfault_success - Check diag fault is success or not
  1187. * @mrioc: Adapter reference
  1188. * @ioc_status: IOC status register value
  1189. *
  1190. * Check whether the controller hit diag reset fault code.
  1191. *
  1192. * Return: True when there is diag fault, false otherwise.
  1193. */
  1194. static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc,
  1195. u32 ioc_status)
  1196. {
  1197. u32 fault;
  1198. if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
  1199. return false;
  1200. fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
  1201. if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) {
  1202. mpi3mr_print_fault_info(mrioc);
  1203. return true;
  1204. }
  1205. return false;
  1206. }
  1207. /**
  1208. * mpi3mr_set_diagsave - Set diag save bit for snapdump
  1209. * @mrioc: Adapter reference
  1210. *
  1211. * Set diag save bit in IOC configuration register to enable
  1212. * snapdump.
  1213. *
  1214. * Return: Nothing.
  1215. */
  1216. static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
  1217. {
  1218. u32 ioc_config;
  1219. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  1220. ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
  1221. writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
  1222. }
  1223. /**
  1224. * mpi3mr_issue_reset - Issue reset to the controller
  1225. * @mrioc: Adapter reference
  1226. * @reset_type: Reset type
  1227. * @reset_reason: Reset reason code
  1228. *
  1229. * Unlock the host diagnostic registers and write the specific
  1230. * reset type to that, wait for reset acknowledgment from the
  1231. * controller, if the reset is not successful retry for the
  1232. * predefined number of times.
  1233. *
  1234. * Return: 0 on success, non-zero on failure.
  1235. */
  1236. static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
  1237. u32 reset_reason)
  1238. {
  1239. int retval = -1;
  1240. u8 unlock_retry_count = 0;
  1241. u32 host_diagnostic, ioc_status, ioc_config;
  1242. u32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
  1243. if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
  1244. (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
  1245. return retval;
  1246. if (mrioc->unrecoverable)
  1247. return retval;
  1248. if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) {
  1249. retval = 0;
  1250. return retval;
  1251. }
  1252. ioc_info(mrioc, "%s reset due to %s(0x%x)\n",
  1253. mpi3mr_reset_type_name(reset_type),
  1254. mpi3mr_reset_rc_name(reset_reason), reset_reason);
  1255. mpi3mr_clear_reset_history(mrioc);
  1256. do {
  1257. ioc_info(mrioc,
  1258. "Write magic sequence to unlock host diag register (retry=%d)\n",
  1259. ++unlock_retry_count);
  1260. if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
  1261. ioc_err(mrioc,
  1262. "%s reset failed due to unlock failure, host_diagnostic(0x%08x)\n",
  1263. mpi3mr_reset_type_name(reset_type),
  1264. host_diagnostic);
  1265. mrioc->unrecoverable = 1;
  1266. return retval;
  1267. }
  1268. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH,
  1269. &mrioc->sysif_regs->write_sequence);
  1270. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST,
  1271. &mrioc->sysif_regs->write_sequence);
  1272. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
  1273. &mrioc->sysif_regs->write_sequence);
  1274. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD,
  1275. &mrioc->sysif_regs->write_sequence);
  1276. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH,
  1277. &mrioc->sysif_regs->write_sequence);
  1278. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH,
  1279. &mrioc->sysif_regs->write_sequence);
  1280. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH,
  1281. &mrioc->sysif_regs->write_sequence);
  1282. usleep_range(1000, 1100);
  1283. host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
  1284. ioc_info(mrioc,
  1285. "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
  1286. unlock_retry_count, host_diagnostic);
  1287. } while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
  1288. writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
  1289. writel(host_diagnostic | reset_type,
  1290. &mrioc->sysif_regs->host_diagnostic);
  1291. switch (reset_type) {
  1292. case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET:
  1293. do {
  1294. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  1295. ioc_config =
  1296. readl(&mrioc->sysif_regs->ioc_configuration);
  1297. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
  1298. && mpi3mr_soft_reset_success(ioc_status, ioc_config)
  1299. ) {
  1300. mpi3mr_clear_reset_history(mrioc);
  1301. retval = 0;
  1302. break;
  1303. }
  1304. msleep(100);
  1305. } while (--timeout);
  1306. mpi3mr_print_fault_info(mrioc);
  1307. break;
  1308. case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT:
  1309. do {
  1310. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  1311. if (mpi3mr_diagfault_success(mrioc, ioc_status)) {
  1312. retval = 0;
  1313. break;
  1314. }
  1315. msleep(100);
  1316. } while (--timeout);
  1317. break;
  1318. default:
  1319. break;
  1320. }
  1321. writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
  1322. &mrioc->sysif_regs->write_sequence);
  1323. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  1324. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  1325. ioc_info(mrioc,
  1326. "ioc_status/ioc_onfig after %s reset is (0x%x)/(0x%x)\n",
  1327. (!retval)?"successful":"failed", ioc_status,
  1328. ioc_config);
  1329. if (retval)
  1330. mrioc->unrecoverable = 1;
  1331. return retval;
  1332. }
  1333. /**
  1334. * mpi3mr_admin_request_post - Post request to admin queue
  1335. * @mrioc: Adapter reference
  1336. * @admin_req: MPI3 request
  1337. * @admin_req_sz: Request size
  1338. * @ignore_reset: Ignore reset in process
  1339. *
  1340. * Post the MPI3 request into admin request queue and
  1341. * inform the controller, if the queue is full return
  1342. * appropriate error.
  1343. *
  1344. * Return: 0 on success, non-zero on failure.
  1345. */
  1346. int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
  1347. u16 admin_req_sz, u8 ignore_reset)
  1348. {
  1349. u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
  1350. int retval = 0;
  1351. unsigned long flags;
  1352. u8 *areq_entry;
  1353. if (mrioc->unrecoverable) {
  1354. ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
  1355. return -EFAULT;
  1356. }
  1357. spin_lock_irqsave(&mrioc->admin_req_lock, flags);
  1358. areq_pi = mrioc->admin_req_pi;
  1359. areq_ci = mrioc->admin_req_ci;
  1360. max_entries = mrioc->num_admin_req;
  1361. if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
  1362. (areq_pi == (max_entries - 1)))) {
  1363. ioc_err(mrioc, "AdminReqQ full condition detected\n");
  1364. retval = -EAGAIN;
  1365. goto out;
  1366. }
  1367. if (!ignore_reset && mrioc->reset_in_progress) {
  1368. ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
  1369. retval = -EAGAIN;
  1370. goto out;
  1371. }
  1372. areq_entry = (u8 *)mrioc->admin_req_base +
  1373. (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
  1374. memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
  1375. memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
  1376. if (++areq_pi == max_entries)
  1377. areq_pi = 0;
  1378. mrioc->admin_req_pi = areq_pi;
  1379. writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
  1380. out:
  1381. spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
  1382. return retval;
  1383. }
  1384. /**
  1385. * mpi3mr_free_op_req_q_segments - free request memory segments
  1386. * @mrioc: Adapter instance reference
  1387. * @q_idx: operational request queue index
  1388. *
  1389. * Free memory segments allocated for operational request queue
  1390. *
  1391. * Return: Nothing.
  1392. */
  1393. static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
  1394. {
  1395. u16 j;
  1396. int size;
  1397. struct segments *segments;
  1398. segments = mrioc->req_qinfo[q_idx].q_segments;
  1399. if (!segments)
  1400. return;
  1401. if (mrioc->enable_segqueue) {
  1402. size = MPI3MR_OP_REQ_Q_SEG_SIZE;
  1403. if (mrioc->req_qinfo[q_idx].q_segment_list) {
  1404. dma_free_coherent(&mrioc->pdev->dev,
  1405. MPI3MR_MAX_SEG_LIST_SIZE,
  1406. mrioc->req_qinfo[q_idx].q_segment_list,
  1407. mrioc->req_qinfo[q_idx].q_segment_list_dma);
  1408. mrioc->req_qinfo[q_idx].q_segment_list = NULL;
  1409. }
  1410. } else
  1411. size = mrioc->req_qinfo[q_idx].segment_qd *
  1412. mrioc->facts.op_req_sz;
  1413. for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) {
  1414. if (!segments[j].segment)
  1415. continue;
  1416. dma_free_coherent(&mrioc->pdev->dev,
  1417. size, segments[j].segment, segments[j].segment_dma);
  1418. segments[j].segment = NULL;
  1419. }
  1420. kfree(mrioc->req_qinfo[q_idx].q_segments);
  1421. mrioc->req_qinfo[q_idx].q_segments = NULL;
  1422. mrioc->req_qinfo[q_idx].qid = 0;
  1423. }
  1424. /**
  1425. * mpi3mr_free_op_reply_q_segments - free reply memory segments
  1426. * @mrioc: Adapter instance reference
  1427. * @q_idx: operational reply queue index
  1428. *
  1429. * Free memory segments allocated for operational reply queue
  1430. *
  1431. * Return: Nothing.
  1432. */
  1433. static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
  1434. {
  1435. u16 j;
  1436. int size;
  1437. struct segments *segments;
  1438. segments = mrioc->op_reply_qinfo[q_idx].q_segments;
  1439. if (!segments)
  1440. return;
  1441. if (mrioc->enable_segqueue) {
  1442. size = MPI3MR_OP_REP_Q_SEG_SIZE;
  1443. if (mrioc->op_reply_qinfo[q_idx].q_segment_list) {
  1444. dma_free_coherent(&mrioc->pdev->dev,
  1445. MPI3MR_MAX_SEG_LIST_SIZE,
  1446. mrioc->op_reply_qinfo[q_idx].q_segment_list,
  1447. mrioc->op_reply_qinfo[q_idx].q_segment_list_dma);
  1448. mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
  1449. }
  1450. } else
  1451. size = mrioc->op_reply_qinfo[q_idx].segment_qd *
  1452. mrioc->op_reply_desc_sz;
  1453. for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) {
  1454. if (!segments[j].segment)
  1455. continue;
  1456. dma_free_coherent(&mrioc->pdev->dev,
  1457. size, segments[j].segment, segments[j].segment_dma);
  1458. segments[j].segment = NULL;
  1459. }
  1460. kfree(mrioc->op_reply_qinfo[q_idx].q_segments);
  1461. mrioc->op_reply_qinfo[q_idx].q_segments = NULL;
  1462. mrioc->op_reply_qinfo[q_idx].qid = 0;
  1463. }
  1464. /**
  1465. * mpi3mr_delete_op_reply_q - delete operational reply queue
  1466. * @mrioc: Adapter instance reference
  1467. * @qidx: operational reply queue index
  1468. *
  1469. * Delete operatinal reply queue by issuing MPI request
  1470. * through admin queue.
  1471. *
  1472. * Return: 0 on success, non-zero on failure.
  1473. */
  1474. static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
  1475. {
  1476. struct mpi3_delete_reply_queue_request delq_req;
  1477. struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
  1478. int retval = 0;
  1479. u16 reply_qid = 0, midx;
  1480. reply_qid = op_reply_q->qid;
  1481. midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
  1482. if (!reply_qid) {
  1483. retval = -1;
  1484. ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n");
  1485. goto out;
  1486. }
  1487. (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount-- :
  1488. mrioc->active_poll_qcount--;
  1489. memset(&delq_req, 0, sizeof(delq_req));
  1490. mutex_lock(&mrioc->init_cmds.mutex);
  1491. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  1492. retval = -1;
  1493. ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n");
  1494. mutex_unlock(&mrioc->init_cmds.mutex);
  1495. goto out;
  1496. }
  1497. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  1498. mrioc->init_cmds.is_waiting = 1;
  1499. mrioc->init_cmds.callback = NULL;
  1500. delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  1501. delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
  1502. delq_req.queue_id = cpu_to_le16(reply_qid);
  1503. init_completion(&mrioc->init_cmds.done);
  1504. retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req),
  1505. 1);
  1506. if (retval) {
  1507. ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n");
  1508. goto out_unlock;
  1509. }
  1510. wait_for_completion_timeout(&mrioc->init_cmds.done,
  1511. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  1512. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  1513. ioc_err(mrioc, "delete reply queue timed out\n");
  1514. mpi3mr_check_rh_fault_ioc(mrioc,
  1515. MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
  1516. retval = -1;
  1517. goto out_unlock;
  1518. }
  1519. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  1520. != MPI3_IOCSTATUS_SUCCESS) {
  1521. ioc_err(mrioc,
  1522. "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  1523. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  1524. mrioc->init_cmds.ioc_loginfo);
  1525. retval = -1;
  1526. goto out_unlock;
  1527. }
  1528. mrioc->intr_info[midx].op_reply_q = NULL;
  1529. mpi3mr_free_op_reply_q_segments(mrioc, qidx);
  1530. out_unlock:
  1531. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  1532. mutex_unlock(&mrioc->init_cmds.mutex);
  1533. out:
  1534. return retval;
  1535. }
  1536. /**
  1537. * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool
  1538. * @mrioc: Adapter instance reference
  1539. * @qidx: request queue index
  1540. *
  1541. * Allocate segmented memory pools for operational reply
  1542. * queue.
  1543. *
  1544. * Return: 0 on success, non-zero on failure.
  1545. */
  1546. static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
  1547. {
  1548. struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
  1549. int i, size;
  1550. u64 *q_segment_list_entry = NULL;
  1551. struct segments *segments;
  1552. if (mrioc->enable_segqueue) {
  1553. op_reply_q->segment_qd =
  1554. MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz;
  1555. size = MPI3MR_OP_REP_Q_SEG_SIZE;
  1556. op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
  1557. MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma,
  1558. GFP_KERNEL);
  1559. if (!op_reply_q->q_segment_list)
  1560. return -ENOMEM;
  1561. q_segment_list_entry = (u64 *)op_reply_q->q_segment_list;
  1562. } else {
  1563. op_reply_q->segment_qd = op_reply_q->num_replies;
  1564. size = op_reply_q->num_replies * mrioc->op_reply_desc_sz;
  1565. }
  1566. op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies,
  1567. op_reply_q->segment_qd);
  1568. op_reply_q->q_segments = kcalloc(op_reply_q->num_segments,
  1569. sizeof(struct segments), GFP_KERNEL);
  1570. if (!op_reply_q->q_segments)
  1571. return -ENOMEM;
  1572. segments = op_reply_q->q_segments;
  1573. for (i = 0; i < op_reply_q->num_segments; i++) {
  1574. segments[i].segment =
  1575. dma_alloc_coherent(&mrioc->pdev->dev,
  1576. size, &segments[i].segment_dma, GFP_KERNEL);
  1577. if (!segments[i].segment)
  1578. return -ENOMEM;
  1579. if (mrioc->enable_segqueue)
  1580. q_segment_list_entry[i] =
  1581. (unsigned long)segments[i].segment_dma;
  1582. }
  1583. return 0;
  1584. }
  1585. /**
  1586. * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool.
  1587. * @mrioc: Adapter instance reference
  1588. * @qidx: request queue index
  1589. *
  1590. * Allocate segmented memory pools for operational request
  1591. * queue.
  1592. *
  1593. * Return: 0 on success, non-zero on failure.
  1594. */
  1595. static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
  1596. {
  1597. struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
  1598. int i, size;
  1599. u64 *q_segment_list_entry = NULL;
  1600. struct segments *segments;
  1601. if (mrioc->enable_segqueue) {
  1602. op_req_q->segment_qd =
  1603. MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz;
  1604. size = MPI3MR_OP_REQ_Q_SEG_SIZE;
  1605. op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
  1606. MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma,
  1607. GFP_KERNEL);
  1608. if (!op_req_q->q_segment_list)
  1609. return -ENOMEM;
  1610. q_segment_list_entry = (u64 *)op_req_q->q_segment_list;
  1611. } else {
  1612. op_req_q->segment_qd = op_req_q->num_requests;
  1613. size = op_req_q->num_requests * mrioc->facts.op_req_sz;
  1614. }
  1615. op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests,
  1616. op_req_q->segment_qd);
  1617. op_req_q->q_segments = kcalloc(op_req_q->num_segments,
  1618. sizeof(struct segments), GFP_KERNEL);
  1619. if (!op_req_q->q_segments)
  1620. return -ENOMEM;
  1621. segments = op_req_q->q_segments;
  1622. for (i = 0; i < op_req_q->num_segments; i++) {
  1623. segments[i].segment =
  1624. dma_alloc_coherent(&mrioc->pdev->dev,
  1625. size, &segments[i].segment_dma, GFP_KERNEL);
  1626. if (!segments[i].segment)
  1627. return -ENOMEM;
  1628. if (mrioc->enable_segqueue)
  1629. q_segment_list_entry[i] =
  1630. (unsigned long)segments[i].segment_dma;
  1631. }
  1632. return 0;
  1633. }
  1634. /**
  1635. * mpi3mr_create_op_reply_q - create operational reply queue
  1636. * @mrioc: Adapter instance reference
  1637. * @qidx: operational reply queue index
  1638. *
  1639. * Create operatinal reply queue by issuing MPI request
  1640. * through admin queue.
  1641. *
  1642. * Return: 0 on success, non-zero on failure.
  1643. */
  1644. static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
  1645. {
  1646. struct mpi3_create_reply_queue_request create_req;
  1647. struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
  1648. int retval = 0;
  1649. u16 reply_qid = 0, midx;
  1650. reply_qid = op_reply_q->qid;
  1651. midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
  1652. if (reply_qid) {
  1653. retval = -1;
  1654. ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n",
  1655. reply_qid);
  1656. return retval;
  1657. }
  1658. reply_qid = qidx + 1;
  1659. op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
  1660. if (!mrioc->pdev->revision)
  1661. op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD4K;
  1662. op_reply_q->ci = 0;
  1663. op_reply_q->ephase = 1;
  1664. atomic_set(&op_reply_q->pend_ios, 0);
  1665. atomic_set(&op_reply_q->in_use, 0);
  1666. op_reply_q->enable_irq_poll = false;
  1667. if (!op_reply_q->q_segments) {
  1668. retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx);
  1669. if (retval) {
  1670. mpi3mr_free_op_reply_q_segments(mrioc, qidx);
  1671. goto out;
  1672. }
  1673. }
  1674. memset(&create_req, 0, sizeof(create_req));
  1675. mutex_lock(&mrioc->init_cmds.mutex);
  1676. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  1677. retval = -1;
  1678. ioc_err(mrioc, "CreateRepQ: Init command is in use\n");
  1679. goto out_unlock;
  1680. }
  1681. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  1682. mrioc->init_cmds.is_waiting = 1;
  1683. mrioc->init_cmds.callback = NULL;
  1684. create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  1685. create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
  1686. create_req.queue_id = cpu_to_le16(reply_qid);
  1687. if (midx < (mrioc->intr_info_count - mrioc->requested_poll_qcount))
  1688. op_reply_q->qtype = MPI3MR_DEFAULT_QUEUE;
  1689. else
  1690. op_reply_q->qtype = MPI3MR_POLL_QUEUE;
  1691. if (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) {
  1692. create_req.flags =
  1693. MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
  1694. create_req.msix_index =
  1695. cpu_to_le16(mrioc->intr_info[midx].msix_index);
  1696. } else {
  1697. create_req.msix_index = cpu_to_le16(mrioc->intr_info_count - 1);
  1698. ioc_info(mrioc, "create reply queue(polled): for qid(%d), midx(%d)\n",
  1699. reply_qid, midx);
  1700. if (!mrioc->active_poll_qcount)
  1701. disable_irq_nosync(pci_irq_vector(mrioc->pdev,
  1702. mrioc->intr_info_count - 1));
  1703. }
  1704. if (mrioc->enable_segqueue) {
  1705. create_req.flags |=
  1706. MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
  1707. create_req.base_address = cpu_to_le64(
  1708. op_reply_q->q_segment_list_dma);
  1709. } else
  1710. create_req.base_address = cpu_to_le64(
  1711. op_reply_q->q_segments[0].segment_dma);
  1712. create_req.size = cpu_to_le16(op_reply_q->num_replies);
  1713. init_completion(&mrioc->init_cmds.done);
  1714. retval = mpi3mr_admin_request_post(mrioc, &create_req,
  1715. sizeof(create_req), 1);
  1716. if (retval) {
  1717. ioc_err(mrioc, "CreateRepQ: Admin Post failed\n");
  1718. goto out_unlock;
  1719. }
  1720. wait_for_completion_timeout(&mrioc->init_cmds.done,
  1721. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  1722. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  1723. ioc_err(mrioc, "create reply queue timed out\n");
  1724. mpi3mr_check_rh_fault_ioc(mrioc,
  1725. MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
  1726. retval = -1;
  1727. goto out_unlock;
  1728. }
  1729. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  1730. != MPI3_IOCSTATUS_SUCCESS) {
  1731. ioc_err(mrioc,
  1732. "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  1733. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  1734. mrioc->init_cmds.ioc_loginfo);
  1735. retval = -1;
  1736. goto out_unlock;
  1737. }
  1738. op_reply_q->qid = reply_qid;
  1739. if (midx < mrioc->intr_info_count)
  1740. mrioc->intr_info[midx].op_reply_q = op_reply_q;
  1741. (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount++ :
  1742. mrioc->active_poll_qcount++;
  1743. out_unlock:
  1744. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  1745. mutex_unlock(&mrioc->init_cmds.mutex);
  1746. out:
  1747. return retval;
  1748. }
  1749. /**
  1750. * mpi3mr_create_op_req_q - create operational request queue
  1751. * @mrioc: Adapter instance reference
  1752. * @idx: operational request queue index
  1753. * @reply_qid: Reply queue ID
  1754. *
  1755. * Create operatinal request queue by issuing MPI request
  1756. * through admin queue.
  1757. *
  1758. * Return: 0 on success, non-zero on failure.
  1759. */
  1760. static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx,
  1761. u16 reply_qid)
  1762. {
  1763. struct mpi3_create_request_queue_request create_req;
  1764. struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx;
  1765. int retval = 0;
  1766. u16 req_qid = 0;
  1767. req_qid = op_req_q->qid;
  1768. if (req_qid) {
  1769. retval = -1;
  1770. ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n",
  1771. req_qid);
  1772. return retval;
  1773. }
  1774. req_qid = idx + 1;
  1775. op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD;
  1776. op_req_q->ci = 0;
  1777. op_req_q->pi = 0;
  1778. op_req_q->reply_qid = reply_qid;
  1779. spin_lock_init(&op_req_q->q_lock);
  1780. if (!op_req_q->q_segments) {
  1781. retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx);
  1782. if (retval) {
  1783. mpi3mr_free_op_req_q_segments(mrioc, idx);
  1784. goto out;
  1785. }
  1786. }
  1787. memset(&create_req, 0, sizeof(create_req));
  1788. mutex_lock(&mrioc->init_cmds.mutex);
  1789. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  1790. retval = -1;
  1791. ioc_err(mrioc, "CreateReqQ: Init command is in use\n");
  1792. goto out_unlock;
  1793. }
  1794. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  1795. mrioc->init_cmds.is_waiting = 1;
  1796. mrioc->init_cmds.callback = NULL;
  1797. create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  1798. create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
  1799. create_req.queue_id = cpu_to_le16(req_qid);
  1800. if (mrioc->enable_segqueue) {
  1801. create_req.flags =
  1802. MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
  1803. create_req.base_address = cpu_to_le64(
  1804. op_req_q->q_segment_list_dma);
  1805. } else
  1806. create_req.base_address = cpu_to_le64(
  1807. op_req_q->q_segments[0].segment_dma);
  1808. create_req.reply_queue_id = cpu_to_le16(reply_qid);
  1809. create_req.size = cpu_to_le16(op_req_q->num_requests);
  1810. init_completion(&mrioc->init_cmds.done);
  1811. retval = mpi3mr_admin_request_post(mrioc, &create_req,
  1812. sizeof(create_req), 1);
  1813. if (retval) {
  1814. ioc_err(mrioc, "CreateReqQ: Admin Post failed\n");
  1815. goto out_unlock;
  1816. }
  1817. wait_for_completion_timeout(&mrioc->init_cmds.done,
  1818. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  1819. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  1820. ioc_err(mrioc, "create request queue timed out\n");
  1821. mpi3mr_check_rh_fault_ioc(mrioc,
  1822. MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
  1823. retval = -1;
  1824. goto out_unlock;
  1825. }
  1826. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  1827. != MPI3_IOCSTATUS_SUCCESS) {
  1828. ioc_err(mrioc,
  1829. "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  1830. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  1831. mrioc->init_cmds.ioc_loginfo);
  1832. retval = -1;
  1833. goto out_unlock;
  1834. }
  1835. op_req_q->qid = req_qid;
  1836. out_unlock:
  1837. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  1838. mutex_unlock(&mrioc->init_cmds.mutex);
  1839. out:
  1840. return retval;
  1841. }
  1842. /**
  1843. * mpi3mr_create_op_queues - create operational queue pairs
  1844. * @mrioc: Adapter instance reference
  1845. *
  1846. * Allocate memory for operational queue meta data and call
  1847. * create request and reply queue functions.
  1848. *
  1849. * Return: 0 on success, non-zero on failures.
  1850. */
  1851. static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc)
  1852. {
  1853. int retval = 0;
  1854. u16 num_queues = 0, i = 0, msix_count_op_q = 1;
  1855. num_queues = min_t(int, mrioc->facts.max_op_reply_q,
  1856. mrioc->facts.max_op_req_q);
  1857. msix_count_op_q =
  1858. mrioc->intr_info_count - mrioc->op_reply_q_offset;
  1859. if (!mrioc->num_queues)
  1860. mrioc->num_queues = min_t(int, num_queues, msix_count_op_q);
  1861. /*
  1862. * During reset set the num_queues to the number of queues
  1863. * that was set before the reset.
  1864. */
  1865. num_queues = mrioc->num_op_reply_q ?
  1866. mrioc->num_op_reply_q : mrioc->num_queues;
  1867. ioc_info(mrioc, "trying to create %d operational queue pairs\n",
  1868. num_queues);
  1869. if (!mrioc->req_qinfo) {
  1870. mrioc->req_qinfo = kcalloc(num_queues,
  1871. sizeof(struct op_req_qinfo), GFP_KERNEL);
  1872. if (!mrioc->req_qinfo) {
  1873. retval = -1;
  1874. goto out_failed;
  1875. }
  1876. mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) *
  1877. num_queues, GFP_KERNEL);
  1878. if (!mrioc->op_reply_qinfo) {
  1879. retval = -1;
  1880. goto out_failed;
  1881. }
  1882. }
  1883. if (mrioc->enable_segqueue)
  1884. ioc_info(mrioc,
  1885. "allocating operational queues through segmented queues\n");
  1886. for (i = 0; i < num_queues; i++) {
  1887. if (mpi3mr_create_op_reply_q(mrioc, i)) {
  1888. ioc_err(mrioc, "Cannot create OP RepQ %d\n", i);
  1889. break;
  1890. }
  1891. if (mpi3mr_create_op_req_q(mrioc, i,
  1892. mrioc->op_reply_qinfo[i].qid)) {
  1893. ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i);
  1894. mpi3mr_delete_op_reply_q(mrioc, i);
  1895. break;
  1896. }
  1897. }
  1898. if (i == 0) {
  1899. /* Not even one queue is created successfully*/
  1900. retval = -1;
  1901. goto out_failed;
  1902. }
  1903. mrioc->num_op_reply_q = mrioc->num_op_req_q = i;
  1904. ioc_info(mrioc,
  1905. "successfully created %d operational queue pairs(default/polled) queue = (%d/%d)\n",
  1906. mrioc->num_op_reply_q, mrioc->default_qcount,
  1907. mrioc->active_poll_qcount);
  1908. return retval;
  1909. out_failed:
  1910. kfree(mrioc->req_qinfo);
  1911. mrioc->req_qinfo = NULL;
  1912. kfree(mrioc->op_reply_qinfo);
  1913. mrioc->op_reply_qinfo = NULL;
  1914. return retval;
  1915. }
  1916. /**
  1917. * mpi3mr_op_request_post - Post request to operational queue
  1918. * @mrioc: Adapter reference
  1919. * @op_req_q: Operational request queue info
  1920. * @req: MPI3 request
  1921. *
  1922. * Post the MPI3 request into operational request queue and
  1923. * inform the controller, if the queue is full return
  1924. * appropriate error.
  1925. *
  1926. * Return: 0 on success, non-zero on failure.
  1927. */
  1928. int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc,
  1929. struct op_req_qinfo *op_req_q, u8 *req)
  1930. {
  1931. u16 pi = 0, max_entries, reply_qidx = 0, midx;
  1932. int retval = 0;
  1933. unsigned long flags;
  1934. u8 *req_entry;
  1935. void *segment_base_addr;
  1936. u16 req_sz = mrioc->facts.op_req_sz;
  1937. struct segments *segments = op_req_q->q_segments;
  1938. reply_qidx = op_req_q->reply_qid - 1;
  1939. if (mrioc->unrecoverable)
  1940. return -EFAULT;
  1941. spin_lock_irqsave(&op_req_q->q_lock, flags);
  1942. pi = op_req_q->pi;
  1943. max_entries = op_req_q->num_requests;
  1944. if (mpi3mr_check_req_qfull(op_req_q)) {
  1945. midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(
  1946. reply_qidx, mrioc->op_reply_q_offset);
  1947. mpi3mr_process_op_reply_q(mrioc, mrioc->intr_info[midx].op_reply_q);
  1948. if (mpi3mr_check_req_qfull(op_req_q)) {
  1949. retval = -EAGAIN;
  1950. goto out;
  1951. }
  1952. }
  1953. if (mrioc->reset_in_progress) {
  1954. ioc_err(mrioc, "OpReqQ submit reset in progress\n");
  1955. retval = -EAGAIN;
  1956. goto out;
  1957. }
  1958. segment_base_addr = segments[pi / op_req_q->segment_qd].segment;
  1959. req_entry = (u8 *)segment_base_addr +
  1960. ((pi % op_req_q->segment_qd) * req_sz);
  1961. memset(req_entry, 0, req_sz);
  1962. memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ);
  1963. if (++pi == max_entries)
  1964. pi = 0;
  1965. op_req_q->pi = pi;
  1966. #ifndef CONFIG_PREEMPT_RT
  1967. if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios)
  1968. > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT)
  1969. mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true;
  1970. #else
  1971. atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios);
  1972. #endif
  1973. writel(op_req_q->pi,
  1974. &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index);
  1975. out:
  1976. spin_unlock_irqrestore(&op_req_q->q_lock, flags);
  1977. return retval;
  1978. }
  1979. /**
  1980. * mpi3mr_check_rh_fault_ioc - check reset history and fault
  1981. * controller
  1982. * @mrioc: Adapter instance reference
  1983. * @reason_code: reason code for the fault.
  1984. *
  1985. * This routine will save snapdump and fault the controller with
  1986. * the given reason code if it is not already in the fault or
  1987. * not asynchronosuly reset. This will be used to handle
  1988. * initilaization time faults/resets/timeout as in those cases
  1989. * immediate soft reset invocation is not required.
  1990. *
  1991. * Return: None.
  1992. */
  1993. void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code)
  1994. {
  1995. u32 ioc_status, host_diagnostic, timeout;
  1996. if (mrioc->unrecoverable) {
  1997. ioc_err(mrioc, "controller is unrecoverable\n");
  1998. return;
  1999. }
  2000. if (!pci_device_is_present(mrioc->pdev)) {
  2001. mrioc->unrecoverable = 1;
  2002. ioc_err(mrioc, "controller is not present\n");
  2003. return;
  2004. }
  2005. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  2006. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
  2007. (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
  2008. mpi3mr_print_fault_info(mrioc);
  2009. return;
  2010. }
  2011. mpi3mr_set_diagsave(mrioc);
  2012. mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
  2013. reason_code);
  2014. timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
  2015. do {
  2016. host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
  2017. if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
  2018. break;
  2019. msleep(100);
  2020. } while (--timeout);
  2021. }
  2022. /**
  2023. * mpi3mr_sync_timestamp - Issue time stamp sync request
  2024. * @mrioc: Adapter reference
  2025. *
  2026. * Issue IO unit control MPI request to synchornize firmware
  2027. * timestamp with host time.
  2028. *
  2029. * Return: 0 on success, non-zero on failure.
  2030. */
  2031. static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc)
  2032. {
  2033. ktime_t current_time;
  2034. struct mpi3_iounit_control_request iou_ctrl;
  2035. int retval = 0;
  2036. memset(&iou_ctrl, 0, sizeof(iou_ctrl));
  2037. mutex_lock(&mrioc->init_cmds.mutex);
  2038. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  2039. retval = -1;
  2040. ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n");
  2041. mutex_unlock(&mrioc->init_cmds.mutex);
  2042. goto out;
  2043. }
  2044. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  2045. mrioc->init_cmds.is_waiting = 1;
  2046. mrioc->init_cmds.callback = NULL;
  2047. iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  2048. iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
  2049. iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP;
  2050. current_time = ktime_get_real();
  2051. iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time));
  2052. init_completion(&mrioc->init_cmds.done);
  2053. retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl,
  2054. sizeof(iou_ctrl), 0);
  2055. if (retval) {
  2056. ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n");
  2057. goto out_unlock;
  2058. }
  2059. wait_for_completion_timeout(&mrioc->init_cmds.done,
  2060. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  2061. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  2062. ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n");
  2063. mrioc->init_cmds.is_waiting = 0;
  2064. if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
  2065. mpi3mr_soft_reset_handler(mrioc,
  2066. MPI3MR_RESET_FROM_TSU_TIMEOUT, 1);
  2067. retval = -1;
  2068. goto out_unlock;
  2069. }
  2070. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  2071. != MPI3_IOCSTATUS_SUCCESS) {
  2072. ioc_err(mrioc,
  2073. "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  2074. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  2075. mrioc->init_cmds.ioc_loginfo);
  2076. retval = -1;
  2077. goto out_unlock;
  2078. }
  2079. out_unlock:
  2080. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  2081. mutex_unlock(&mrioc->init_cmds.mutex);
  2082. out:
  2083. return retval;
  2084. }
  2085. /**
  2086. * mpi3mr_print_pkg_ver - display controller fw package version
  2087. * @mrioc: Adapter reference
  2088. *
  2089. * Retrieve firmware package version from the component image
  2090. * header of the controller flash and display it.
  2091. *
  2092. * Return: 0 on success and non-zero on failure.
  2093. */
  2094. static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc)
  2095. {
  2096. struct mpi3_ci_upload_request ci_upload;
  2097. int retval = -1;
  2098. void *data = NULL;
  2099. dma_addr_t data_dma;
  2100. struct mpi3_ci_manifest_mpi *manifest;
  2101. u32 data_len = sizeof(struct mpi3_ci_manifest_mpi);
  2102. u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
  2103. data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
  2104. GFP_KERNEL);
  2105. if (!data)
  2106. return -ENOMEM;
  2107. memset(&ci_upload, 0, sizeof(ci_upload));
  2108. mutex_lock(&mrioc->init_cmds.mutex);
  2109. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  2110. ioc_err(mrioc, "sending get package version failed due to command in use\n");
  2111. mutex_unlock(&mrioc->init_cmds.mutex);
  2112. goto out;
  2113. }
  2114. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  2115. mrioc->init_cmds.is_waiting = 1;
  2116. mrioc->init_cmds.callback = NULL;
  2117. ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  2118. ci_upload.function = MPI3_FUNCTION_CI_UPLOAD;
  2119. ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
  2120. ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST);
  2121. ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE);
  2122. ci_upload.segment_size = cpu_to_le32(data_len);
  2123. mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len,
  2124. data_dma);
  2125. init_completion(&mrioc->init_cmds.done);
  2126. retval = mpi3mr_admin_request_post(mrioc, &ci_upload,
  2127. sizeof(ci_upload), 1);
  2128. if (retval) {
  2129. ioc_err(mrioc, "posting get package version failed\n");
  2130. goto out_unlock;
  2131. }
  2132. wait_for_completion_timeout(&mrioc->init_cmds.done,
  2133. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  2134. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  2135. ioc_err(mrioc, "get package version timed out\n");
  2136. mpi3mr_check_rh_fault_ioc(mrioc,
  2137. MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
  2138. retval = -1;
  2139. goto out_unlock;
  2140. }
  2141. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  2142. == MPI3_IOCSTATUS_SUCCESS) {
  2143. manifest = (struct mpi3_ci_manifest_mpi *) data;
  2144. if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) {
  2145. ioc_info(mrioc,
  2146. "firmware package version(%d.%d.%d.%d.%05d-%05d)\n",
  2147. manifest->package_version.gen_major,
  2148. manifest->package_version.gen_minor,
  2149. manifest->package_version.phase_major,
  2150. manifest->package_version.phase_minor,
  2151. manifest->package_version.customer_id,
  2152. manifest->package_version.build_num);
  2153. }
  2154. }
  2155. retval = 0;
  2156. out_unlock:
  2157. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  2158. mutex_unlock(&mrioc->init_cmds.mutex);
  2159. out:
  2160. if (data)
  2161. dma_free_coherent(&mrioc->pdev->dev, data_len, data,
  2162. data_dma);
  2163. return retval;
  2164. }
  2165. /**
  2166. * mpi3mr_watchdog_work - watchdog thread to monitor faults
  2167. * @work: work struct
  2168. *
  2169. * Watch dog work periodically executed (1 second interval) to
  2170. * monitor firmware fault and to issue periodic timer sync to
  2171. * the firmware.
  2172. *
  2173. * Return: Nothing.
  2174. */
  2175. static void mpi3mr_watchdog_work(struct work_struct *work)
  2176. {
  2177. struct mpi3mr_ioc *mrioc =
  2178. container_of(work, struct mpi3mr_ioc, watchdog_work.work);
  2179. unsigned long flags;
  2180. enum mpi3mr_iocstate ioc_state;
  2181. u32 fault, host_diagnostic, ioc_status;
  2182. u32 reset_reason = MPI3MR_RESET_FROM_FAULT_WATCH;
  2183. if (mrioc->reset_in_progress)
  2184. return;
  2185. if (!mrioc->unrecoverable && !pci_device_is_present(mrioc->pdev)) {
  2186. ioc_err(mrioc, "watchdog could not detect the controller\n");
  2187. mrioc->unrecoverable = 1;
  2188. }
  2189. if (mrioc->unrecoverable) {
  2190. ioc_err(mrioc,
  2191. "flush pending commands for unrecoverable controller\n");
  2192. mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
  2193. return;
  2194. }
  2195. if (mrioc->ts_update_counter++ >= MPI3MR_TSUPDATE_INTERVAL) {
  2196. mrioc->ts_update_counter = 0;
  2197. mpi3mr_sync_timestamp(mrioc);
  2198. }
  2199. if ((mrioc->prepare_for_reset) &&
  2200. ((mrioc->prepare_for_reset_timeout_counter++) >=
  2201. MPI3MR_PREPARE_FOR_RESET_TIMEOUT)) {
  2202. mpi3mr_soft_reset_handler(mrioc,
  2203. MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1);
  2204. return;
  2205. }
  2206. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  2207. if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
  2208. mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0);
  2209. return;
  2210. }
  2211. /*Check for fault state every one second and issue Soft reset*/
  2212. ioc_state = mpi3mr_get_iocstate(mrioc);
  2213. if (ioc_state != MRIOC_STATE_FAULT)
  2214. goto schedule_work;
  2215. fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
  2216. host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
  2217. if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
  2218. if (!mrioc->diagsave_timeout) {
  2219. mpi3mr_print_fault_info(mrioc);
  2220. ioc_warn(mrioc, "diag save in progress\n");
  2221. }
  2222. if ((mrioc->diagsave_timeout++) <= MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
  2223. goto schedule_work;
  2224. }
  2225. mpi3mr_print_fault_info(mrioc);
  2226. mrioc->diagsave_timeout = 0;
  2227. switch (fault) {
  2228. case MPI3_SYSIF_FAULT_CODE_COMPLETE_RESET_NEEDED:
  2229. case MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED:
  2230. ioc_warn(mrioc,
  2231. "controller requires system power cycle, marking controller as unrecoverable\n");
  2232. mrioc->unrecoverable = 1;
  2233. goto schedule_work;
  2234. case MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS:
  2235. goto schedule_work;
  2236. case MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET:
  2237. reset_reason = MPI3MR_RESET_FROM_CIACTIV_FAULT;
  2238. break;
  2239. default:
  2240. break;
  2241. }
  2242. mpi3mr_soft_reset_handler(mrioc, reset_reason, 0);
  2243. return;
  2244. schedule_work:
  2245. spin_lock_irqsave(&mrioc->watchdog_lock, flags);
  2246. if (mrioc->watchdog_work_q)
  2247. queue_delayed_work(mrioc->watchdog_work_q,
  2248. &mrioc->watchdog_work,
  2249. msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
  2250. spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
  2251. return;
  2252. }
  2253. /**
  2254. * mpi3mr_start_watchdog - Start watchdog
  2255. * @mrioc: Adapter instance reference
  2256. *
  2257. * Create and start the watchdog thread to monitor controller
  2258. * faults.
  2259. *
  2260. * Return: Nothing.
  2261. */
  2262. void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc)
  2263. {
  2264. if (mrioc->watchdog_work_q)
  2265. return;
  2266. INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work);
  2267. snprintf(mrioc->watchdog_work_q_name,
  2268. sizeof(mrioc->watchdog_work_q_name), "watchdog_%s%d", mrioc->name,
  2269. mrioc->id);
  2270. mrioc->watchdog_work_q =
  2271. create_singlethread_workqueue(mrioc->watchdog_work_q_name);
  2272. if (!mrioc->watchdog_work_q) {
  2273. ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__);
  2274. return;
  2275. }
  2276. if (mrioc->watchdog_work_q)
  2277. queue_delayed_work(mrioc->watchdog_work_q,
  2278. &mrioc->watchdog_work,
  2279. msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
  2280. }
  2281. /**
  2282. * mpi3mr_stop_watchdog - Stop watchdog
  2283. * @mrioc: Adapter instance reference
  2284. *
  2285. * Stop the watchdog thread created to monitor controller
  2286. * faults.
  2287. *
  2288. * Return: Nothing.
  2289. */
  2290. void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc)
  2291. {
  2292. unsigned long flags;
  2293. struct workqueue_struct *wq;
  2294. spin_lock_irqsave(&mrioc->watchdog_lock, flags);
  2295. wq = mrioc->watchdog_work_q;
  2296. mrioc->watchdog_work_q = NULL;
  2297. spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
  2298. if (wq) {
  2299. if (!cancel_delayed_work_sync(&mrioc->watchdog_work))
  2300. flush_workqueue(wq);
  2301. destroy_workqueue(wq);
  2302. }
  2303. }
  2304. /**
  2305. * mpi3mr_setup_admin_qpair - Setup admin queue pair
  2306. * @mrioc: Adapter instance reference
  2307. *
  2308. * Allocate memory for admin queue pair if required and register
  2309. * the admin queue with the controller.
  2310. *
  2311. * Return: 0 on success, non-zero on failures.
  2312. */
  2313. static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
  2314. {
  2315. int retval = 0;
  2316. u32 num_admin_entries = 0;
  2317. mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
  2318. mrioc->num_admin_req = mrioc->admin_req_q_sz /
  2319. MPI3MR_ADMIN_REQ_FRAME_SZ;
  2320. mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
  2321. mrioc->admin_req_base = NULL;
  2322. mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
  2323. mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
  2324. MPI3MR_ADMIN_REPLY_FRAME_SZ;
  2325. mrioc->admin_reply_ci = 0;
  2326. mrioc->admin_reply_ephase = 1;
  2327. mrioc->admin_reply_base = NULL;
  2328. atomic_set(&mrioc->admin_reply_q_in_use, 0);
  2329. if (!mrioc->admin_req_base) {
  2330. mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
  2331. mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
  2332. if (!mrioc->admin_req_base) {
  2333. retval = -1;
  2334. goto out_failed;
  2335. }
  2336. mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
  2337. mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
  2338. GFP_KERNEL);
  2339. if (!mrioc->admin_reply_base) {
  2340. retval = -1;
  2341. goto out_failed;
  2342. }
  2343. }
  2344. num_admin_entries = (mrioc->num_admin_replies << 16) |
  2345. (mrioc->num_admin_req);
  2346. writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
  2347. mpi3mr_writeq(mrioc->admin_req_dma,
  2348. &mrioc->sysif_regs->admin_request_queue_address);
  2349. mpi3mr_writeq(mrioc->admin_reply_dma,
  2350. &mrioc->sysif_regs->admin_reply_queue_address);
  2351. writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
  2352. writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
  2353. return retval;
  2354. out_failed:
  2355. if (mrioc->admin_reply_base) {
  2356. dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
  2357. mrioc->admin_reply_base, mrioc->admin_reply_dma);
  2358. mrioc->admin_reply_base = NULL;
  2359. }
  2360. if (mrioc->admin_req_base) {
  2361. dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
  2362. mrioc->admin_req_base, mrioc->admin_req_dma);
  2363. mrioc->admin_req_base = NULL;
  2364. }
  2365. return retval;
  2366. }
  2367. /**
  2368. * mpi3mr_issue_iocfacts - Send IOC Facts
  2369. * @mrioc: Adapter instance reference
  2370. * @facts_data: Cached IOC facts data
  2371. *
  2372. * Issue IOC Facts MPI request through admin queue and wait for
  2373. * the completion of it or time out.
  2374. *
  2375. * Return: 0 on success, non-zero on failures.
  2376. */
  2377. static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
  2378. struct mpi3_ioc_facts_data *facts_data)
  2379. {
  2380. struct mpi3_ioc_facts_request iocfacts_req;
  2381. void *data = NULL;
  2382. dma_addr_t data_dma;
  2383. u32 data_len = sizeof(*facts_data);
  2384. int retval = 0;
  2385. u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
  2386. data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
  2387. GFP_KERNEL);
  2388. if (!data) {
  2389. retval = -1;
  2390. goto out;
  2391. }
  2392. memset(&iocfacts_req, 0, sizeof(iocfacts_req));
  2393. mutex_lock(&mrioc->init_cmds.mutex);
  2394. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  2395. retval = -1;
  2396. ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
  2397. mutex_unlock(&mrioc->init_cmds.mutex);
  2398. goto out;
  2399. }
  2400. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  2401. mrioc->init_cmds.is_waiting = 1;
  2402. mrioc->init_cmds.callback = NULL;
  2403. iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  2404. iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
  2405. mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
  2406. data_dma);
  2407. init_completion(&mrioc->init_cmds.done);
  2408. retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
  2409. sizeof(iocfacts_req), 1);
  2410. if (retval) {
  2411. ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
  2412. goto out_unlock;
  2413. }
  2414. wait_for_completion_timeout(&mrioc->init_cmds.done,
  2415. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  2416. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  2417. ioc_err(mrioc, "ioc_facts timed out\n");
  2418. mpi3mr_check_rh_fault_ioc(mrioc,
  2419. MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
  2420. retval = -1;
  2421. goto out_unlock;
  2422. }
  2423. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  2424. != MPI3_IOCSTATUS_SUCCESS) {
  2425. ioc_err(mrioc,
  2426. "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  2427. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  2428. mrioc->init_cmds.ioc_loginfo);
  2429. retval = -1;
  2430. goto out_unlock;
  2431. }
  2432. memcpy(facts_data, (u8 *)data, data_len);
  2433. mpi3mr_process_factsdata(mrioc, facts_data);
  2434. out_unlock:
  2435. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  2436. mutex_unlock(&mrioc->init_cmds.mutex);
  2437. out:
  2438. if (data)
  2439. dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
  2440. return retval;
  2441. }
  2442. /**
  2443. * mpi3mr_check_reset_dma_mask - Process IOC facts data
  2444. * @mrioc: Adapter instance reference
  2445. *
  2446. * Check whether the new DMA mask requested through IOCFacts by
  2447. * firmware needs to be set, if so set it .
  2448. *
  2449. * Return: 0 on success, non-zero on failure.
  2450. */
  2451. static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
  2452. {
  2453. struct pci_dev *pdev = mrioc->pdev;
  2454. int r;
  2455. u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
  2456. if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
  2457. return 0;
  2458. ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
  2459. mrioc->dma_mask, facts_dma_mask);
  2460. r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
  2461. if (r) {
  2462. ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
  2463. facts_dma_mask, r);
  2464. return r;
  2465. }
  2466. mrioc->dma_mask = facts_dma_mask;
  2467. return r;
  2468. }
  2469. /**
  2470. * mpi3mr_process_factsdata - Process IOC facts data
  2471. * @mrioc: Adapter instance reference
  2472. * @facts_data: Cached IOC facts data
  2473. *
  2474. * Convert IOC facts data into cpu endianness and cache it in
  2475. * the driver .
  2476. *
  2477. * Return: Nothing.
  2478. */
  2479. static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
  2480. struct mpi3_ioc_facts_data *facts_data)
  2481. {
  2482. u32 ioc_config, req_sz, facts_flags;
  2483. if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
  2484. (sizeof(*facts_data) / 4)) {
  2485. ioc_warn(mrioc,
  2486. "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
  2487. sizeof(*facts_data),
  2488. le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
  2489. }
  2490. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  2491. req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
  2492. MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
  2493. if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
  2494. ioc_err(mrioc,
  2495. "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
  2496. req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
  2497. }
  2498. memset(&mrioc->facts, 0, sizeof(mrioc->facts));
  2499. facts_flags = le32_to_cpu(facts_data->flags);
  2500. mrioc->facts.op_req_sz = req_sz;
  2501. mrioc->op_reply_desc_sz = 1 << ((ioc_config &
  2502. MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
  2503. MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
  2504. mrioc->facts.ioc_num = facts_data->ioc_number;
  2505. mrioc->facts.who_init = facts_data->who_init;
  2506. mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
  2507. mrioc->facts.personality = (facts_flags &
  2508. MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
  2509. mrioc->facts.dma_mask = (facts_flags &
  2510. MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
  2511. MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
  2512. mrioc->facts.protocol_flags = facts_data->protocol_flags;
  2513. mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
  2514. mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_requests);
  2515. mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
  2516. mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
  2517. mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
  2518. mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
  2519. mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
  2520. mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
  2521. mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds);
  2522. mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds);
  2523. mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
  2524. mrioc->facts.max_pcie_switches =
  2525. le16_to_cpu(facts_data->max_pcie_switches);
  2526. mrioc->facts.max_sasexpanders =
  2527. le16_to_cpu(facts_data->max_sas_expanders);
  2528. mrioc->facts.max_sasinitiators =
  2529. le16_to_cpu(facts_data->max_sas_initiators);
  2530. mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
  2531. mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
  2532. mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
  2533. mrioc->facts.max_op_req_q =
  2534. le16_to_cpu(facts_data->max_operational_request_queues);
  2535. mrioc->facts.max_op_reply_q =
  2536. le16_to_cpu(facts_data->max_operational_reply_queues);
  2537. mrioc->facts.ioc_capabilities =
  2538. le32_to_cpu(facts_data->ioc_capabilities);
  2539. mrioc->facts.fw_ver.build_num =
  2540. le16_to_cpu(facts_data->fw_version.build_num);
  2541. mrioc->facts.fw_ver.cust_id =
  2542. le16_to_cpu(facts_data->fw_version.customer_id);
  2543. mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
  2544. mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
  2545. mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
  2546. mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
  2547. mrioc->msix_count = min_t(int, mrioc->msix_count,
  2548. mrioc->facts.max_msix_vectors);
  2549. mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
  2550. mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
  2551. mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
  2552. mrioc->facts.shutdown_timeout =
  2553. le16_to_cpu(facts_data->shutdown_timeout);
  2554. mrioc->facts.max_dev_per_tg =
  2555. facts_data->max_devices_per_throttle_group;
  2556. mrioc->facts.io_throttle_data_length =
  2557. le16_to_cpu(facts_data->io_throttle_data_length);
  2558. mrioc->facts.max_io_throttle_group =
  2559. le16_to_cpu(facts_data->max_io_throttle_group);
  2560. mrioc->facts.io_throttle_low = le16_to_cpu(facts_data->io_throttle_low);
  2561. mrioc->facts.io_throttle_high =
  2562. le16_to_cpu(facts_data->io_throttle_high);
  2563. /* Store in 512b block count */
  2564. if (mrioc->facts.io_throttle_data_length)
  2565. mrioc->io_throttle_data_length =
  2566. (mrioc->facts.io_throttle_data_length * 2 * 4);
  2567. else
  2568. /* set the length to 1MB + 1K to disable throttle */
  2569. mrioc->io_throttle_data_length = MPI3MR_MAX_SECTORS + 2;
  2570. mrioc->io_throttle_high = (mrioc->facts.io_throttle_high * 2 * 1024);
  2571. mrioc->io_throttle_low = (mrioc->facts.io_throttle_low * 2 * 1024);
  2572. ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
  2573. mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
  2574. mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
  2575. ioc_info(mrioc,
  2576. "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n",
  2577. mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
  2578. mrioc->facts.max_msix_vectors, mrioc->facts.max_perids);
  2579. ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
  2580. mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
  2581. mrioc->facts.sge_mod_shift);
  2582. ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
  2583. mrioc->facts.dma_mask, (facts_flags &
  2584. MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
  2585. ioc_info(mrioc,
  2586. "max_dev_per_throttle_group(%d), max_throttle_groups(%d)\n",
  2587. mrioc->facts.max_dev_per_tg, mrioc->facts.max_io_throttle_group);
  2588. ioc_info(mrioc,
  2589. "io_throttle_data_len(%dKiB), io_throttle_high(%dMiB), io_throttle_low(%dMiB)\n",
  2590. mrioc->facts.io_throttle_data_length * 4,
  2591. mrioc->facts.io_throttle_high, mrioc->facts.io_throttle_low);
  2592. }
  2593. /**
  2594. * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
  2595. * @mrioc: Adapter instance reference
  2596. *
  2597. * Allocate and initialize the reply free buffers, sense
  2598. * buffers, reply free queue and sense buffer queue.
  2599. *
  2600. * Return: 0 on success, non-zero on failures.
  2601. */
  2602. static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
  2603. {
  2604. int retval = 0;
  2605. u32 sz, i;
  2606. if (mrioc->init_cmds.reply)
  2607. return retval;
  2608. mrioc->init_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
  2609. if (!mrioc->init_cmds.reply)
  2610. goto out_failed;
  2611. mrioc->bsg_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
  2612. if (!mrioc->bsg_cmds.reply)
  2613. goto out_failed;
  2614. mrioc->transport_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
  2615. if (!mrioc->transport_cmds.reply)
  2616. goto out_failed;
  2617. for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
  2618. mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->reply_sz,
  2619. GFP_KERNEL);
  2620. if (!mrioc->dev_rmhs_cmds[i].reply)
  2621. goto out_failed;
  2622. }
  2623. for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
  2624. mrioc->evtack_cmds[i].reply = kzalloc(mrioc->reply_sz,
  2625. GFP_KERNEL);
  2626. if (!mrioc->evtack_cmds[i].reply)
  2627. goto out_failed;
  2628. }
  2629. mrioc->host_tm_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
  2630. if (!mrioc->host_tm_cmds.reply)
  2631. goto out_failed;
  2632. mrioc->pel_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
  2633. if (!mrioc->pel_cmds.reply)
  2634. goto out_failed;
  2635. mrioc->pel_abort_cmd.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
  2636. if (!mrioc->pel_abort_cmd.reply)
  2637. goto out_failed;
  2638. mrioc->dev_handle_bitmap_bits = mrioc->facts.max_devhandle;
  2639. mrioc->removepend_bitmap = bitmap_zalloc(mrioc->dev_handle_bitmap_bits,
  2640. GFP_KERNEL);
  2641. if (!mrioc->removepend_bitmap)
  2642. goto out_failed;
  2643. mrioc->devrem_bitmap = bitmap_zalloc(MPI3MR_NUM_DEVRMCMD, GFP_KERNEL);
  2644. if (!mrioc->devrem_bitmap)
  2645. goto out_failed;
  2646. mrioc->evtack_cmds_bitmap = bitmap_zalloc(MPI3MR_NUM_EVTACKCMD,
  2647. GFP_KERNEL);
  2648. if (!mrioc->evtack_cmds_bitmap)
  2649. goto out_failed;
  2650. mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
  2651. mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
  2652. mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
  2653. mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
  2654. /* reply buffer pool, 16 byte align */
  2655. sz = mrioc->num_reply_bufs * mrioc->reply_sz;
  2656. mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
  2657. &mrioc->pdev->dev, sz, 16, 0);
  2658. if (!mrioc->reply_buf_pool) {
  2659. ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
  2660. goto out_failed;
  2661. }
  2662. mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
  2663. &mrioc->reply_buf_dma);
  2664. if (!mrioc->reply_buf)
  2665. goto out_failed;
  2666. mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
  2667. /* reply free queue, 8 byte align */
  2668. sz = mrioc->reply_free_qsz * 8;
  2669. mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
  2670. &mrioc->pdev->dev, sz, 8, 0);
  2671. if (!mrioc->reply_free_q_pool) {
  2672. ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
  2673. goto out_failed;
  2674. }
  2675. mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
  2676. GFP_KERNEL, &mrioc->reply_free_q_dma);
  2677. if (!mrioc->reply_free_q)
  2678. goto out_failed;
  2679. /* sense buffer pool, 4 byte align */
  2680. sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
  2681. mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
  2682. &mrioc->pdev->dev, sz, 4, 0);
  2683. if (!mrioc->sense_buf_pool) {
  2684. ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
  2685. goto out_failed;
  2686. }
  2687. mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
  2688. &mrioc->sense_buf_dma);
  2689. if (!mrioc->sense_buf)
  2690. goto out_failed;
  2691. /* sense buffer queue, 8 byte align */
  2692. sz = mrioc->sense_buf_q_sz * 8;
  2693. mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
  2694. &mrioc->pdev->dev, sz, 8, 0);
  2695. if (!mrioc->sense_buf_q_pool) {
  2696. ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
  2697. goto out_failed;
  2698. }
  2699. mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
  2700. GFP_KERNEL, &mrioc->sense_buf_q_dma);
  2701. if (!mrioc->sense_buf_q)
  2702. goto out_failed;
  2703. return retval;
  2704. out_failed:
  2705. retval = -1;
  2706. return retval;
  2707. }
  2708. /**
  2709. * mpimr_initialize_reply_sbuf_queues - initialize reply sense
  2710. * buffers
  2711. * @mrioc: Adapter instance reference
  2712. *
  2713. * Helper function to initialize reply and sense buffers along
  2714. * with some debug prints.
  2715. *
  2716. * Return: None.
  2717. */
  2718. static void mpimr_initialize_reply_sbuf_queues(struct mpi3mr_ioc *mrioc)
  2719. {
  2720. u32 sz, i;
  2721. dma_addr_t phy_addr;
  2722. sz = mrioc->num_reply_bufs * mrioc->reply_sz;
  2723. ioc_info(mrioc,
  2724. "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
  2725. mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->reply_sz,
  2726. (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
  2727. sz = mrioc->reply_free_qsz * 8;
  2728. ioc_info(mrioc,
  2729. "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
  2730. mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
  2731. (unsigned long long)mrioc->reply_free_q_dma);
  2732. sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
  2733. ioc_info(mrioc,
  2734. "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
  2735. mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ,
  2736. (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
  2737. sz = mrioc->sense_buf_q_sz * 8;
  2738. ioc_info(mrioc,
  2739. "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
  2740. mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
  2741. (unsigned long long)mrioc->sense_buf_q_dma);
  2742. /* initialize Reply buffer Queue */
  2743. for (i = 0, phy_addr = mrioc->reply_buf_dma;
  2744. i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->reply_sz)
  2745. mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
  2746. mrioc->reply_free_q[i] = cpu_to_le64(0);
  2747. /* initialize Sense Buffer Queue */
  2748. for (i = 0, phy_addr = mrioc->sense_buf_dma;
  2749. i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ)
  2750. mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
  2751. mrioc->sense_buf_q[i] = cpu_to_le64(0);
  2752. }
  2753. /**
  2754. * mpi3mr_issue_iocinit - Send IOC Init
  2755. * @mrioc: Adapter instance reference
  2756. *
  2757. * Issue IOC Init MPI request through admin queue and wait for
  2758. * the completion of it or time out.
  2759. *
  2760. * Return: 0 on success, non-zero on failures.
  2761. */
  2762. static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
  2763. {
  2764. struct mpi3_ioc_init_request iocinit_req;
  2765. struct mpi3_driver_info_layout *drv_info;
  2766. dma_addr_t data_dma;
  2767. u32 data_len = sizeof(*drv_info);
  2768. int retval = 0;
  2769. ktime_t current_time;
  2770. drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
  2771. GFP_KERNEL);
  2772. if (!drv_info) {
  2773. retval = -1;
  2774. goto out;
  2775. }
  2776. mpimr_initialize_reply_sbuf_queues(mrioc);
  2777. drv_info->information_length = cpu_to_le32(data_len);
  2778. strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
  2779. strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
  2780. strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
  2781. strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
  2782. strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
  2783. strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE,
  2784. sizeof(drv_info->driver_release_date));
  2785. drv_info->driver_capabilities = 0;
  2786. memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
  2787. sizeof(mrioc->driver_info));
  2788. memset(&iocinit_req, 0, sizeof(iocinit_req));
  2789. mutex_lock(&mrioc->init_cmds.mutex);
  2790. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  2791. retval = -1;
  2792. ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
  2793. mutex_unlock(&mrioc->init_cmds.mutex);
  2794. goto out;
  2795. }
  2796. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  2797. mrioc->init_cmds.is_waiting = 1;
  2798. mrioc->init_cmds.callback = NULL;
  2799. iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  2800. iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
  2801. iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
  2802. iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
  2803. iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
  2804. iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
  2805. iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
  2806. iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
  2807. iocinit_req.reply_free_queue_address =
  2808. cpu_to_le64(mrioc->reply_free_q_dma);
  2809. iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ);
  2810. iocinit_req.sense_buffer_free_queue_depth =
  2811. cpu_to_le16(mrioc->sense_buf_q_sz);
  2812. iocinit_req.sense_buffer_free_queue_address =
  2813. cpu_to_le64(mrioc->sense_buf_q_dma);
  2814. iocinit_req.driver_information_address = cpu_to_le64(data_dma);
  2815. current_time = ktime_get_real();
  2816. iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
  2817. init_completion(&mrioc->init_cmds.done);
  2818. retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
  2819. sizeof(iocinit_req), 1);
  2820. if (retval) {
  2821. ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
  2822. goto out_unlock;
  2823. }
  2824. wait_for_completion_timeout(&mrioc->init_cmds.done,
  2825. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  2826. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  2827. mpi3mr_check_rh_fault_ioc(mrioc,
  2828. MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
  2829. ioc_err(mrioc, "ioc_init timed out\n");
  2830. retval = -1;
  2831. goto out_unlock;
  2832. }
  2833. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  2834. != MPI3_IOCSTATUS_SUCCESS) {
  2835. ioc_err(mrioc,
  2836. "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  2837. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  2838. mrioc->init_cmds.ioc_loginfo);
  2839. retval = -1;
  2840. goto out_unlock;
  2841. }
  2842. mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
  2843. writel(mrioc->reply_free_queue_host_index,
  2844. &mrioc->sysif_regs->reply_free_host_index);
  2845. mrioc->sbq_host_index = mrioc->num_sense_bufs;
  2846. writel(mrioc->sbq_host_index,
  2847. &mrioc->sysif_regs->sense_buffer_free_host_index);
  2848. out_unlock:
  2849. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  2850. mutex_unlock(&mrioc->init_cmds.mutex);
  2851. out:
  2852. if (drv_info)
  2853. dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
  2854. data_dma);
  2855. return retval;
  2856. }
  2857. /**
  2858. * mpi3mr_unmask_events - Unmask events in event mask bitmap
  2859. * @mrioc: Adapter instance reference
  2860. * @event: MPI event ID
  2861. *
  2862. * Un mask the specific event by resetting the event_mask
  2863. * bitmap.
  2864. *
  2865. * Return: 0 on success, non-zero on failures.
  2866. */
  2867. static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event)
  2868. {
  2869. u32 desired_event;
  2870. u8 word;
  2871. if (event >= 128)
  2872. return;
  2873. desired_event = (1 << (event % 32));
  2874. word = event / 32;
  2875. mrioc->event_masks[word] &= ~desired_event;
  2876. }
  2877. /**
  2878. * mpi3mr_issue_event_notification - Send event notification
  2879. * @mrioc: Adapter instance reference
  2880. *
  2881. * Issue event notification MPI request through admin queue and
  2882. * wait for the completion of it or time out.
  2883. *
  2884. * Return: 0 on success, non-zero on failures.
  2885. */
  2886. static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc)
  2887. {
  2888. struct mpi3_event_notification_request evtnotify_req;
  2889. int retval = 0;
  2890. u8 i;
  2891. memset(&evtnotify_req, 0, sizeof(evtnotify_req));
  2892. mutex_lock(&mrioc->init_cmds.mutex);
  2893. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  2894. retval = -1;
  2895. ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n");
  2896. mutex_unlock(&mrioc->init_cmds.mutex);
  2897. goto out;
  2898. }
  2899. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  2900. mrioc->init_cmds.is_waiting = 1;
  2901. mrioc->init_cmds.callback = NULL;
  2902. evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  2903. evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION;
  2904. for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  2905. evtnotify_req.event_masks[i] =
  2906. cpu_to_le32(mrioc->event_masks[i]);
  2907. init_completion(&mrioc->init_cmds.done);
  2908. retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req,
  2909. sizeof(evtnotify_req), 1);
  2910. if (retval) {
  2911. ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n");
  2912. goto out_unlock;
  2913. }
  2914. wait_for_completion_timeout(&mrioc->init_cmds.done,
  2915. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  2916. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  2917. ioc_err(mrioc, "event notification timed out\n");
  2918. mpi3mr_check_rh_fault_ioc(mrioc,
  2919. MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
  2920. retval = -1;
  2921. goto out_unlock;
  2922. }
  2923. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  2924. != MPI3_IOCSTATUS_SUCCESS) {
  2925. ioc_err(mrioc,
  2926. "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  2927. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  2928. mrioc->init_cmds.ioc_loginfo);
  2929. retval = -1;
  2930. goto out_unlock;
  2931. }
  2932. out_unlock:
  2933. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  2934. mutex_unlock(&mrioc->init_cmds.mutex);
  2935. out:
  2936. return retval;
  2937. }
  2938. /**
  2939. * mpi3mr_process_event_ack - Process event acknowledgment
  2940. * @mrioc: Adapter instance reference
  2941. * @event: MPI3 event ID
  2942. * @event_ctx: event context
  2943. *
  2944. * Send event acknowledgment through admin queue and wait for
  2945. * it to complete.
  2946. *
  2947. * Return: 0 on success, non-zero on failures.
  2948. */
  2949. int mpi3mr_process_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
  2950. u32 event_ctx)
  2951. {
  2952. struct mpi3_event_ack_request evtack_req;
  2953. int retval = 0;
  2954. memset(&evtack_req, 0, sizeof(evtack_req));
  2955. mutex_lock(&mrioc->init_cmds.mutex);
  2956. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  2957. retval = -1;
  2958. ioc_err(mrioc, "Send EvtAck: Init command is in use\n");
  2959. mutex_unlock(&mrioc->init_cmds.mutex);
  2960. goto out;
  2961. }
  2962. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  2963. mrioc->init_cmds.is_waiting = 1;
  2964. mrioc->init_cmds.callback = NULL;
  2965. evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  2966. evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
  2967. evtack_req.event = event;
  2968. evtack_req.event_context = cpu_to_le32(event_ctx);
  2969. init_completion(&mrioc->init_cmds.done);
  2970. retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
  2971. sizeof(evtack_req), 1);
  2972. if (retval) {
  2973. ioc_err(mrioc, "Send EvtAck: Admin Post failed\n");
  2974. goto out_unlock;
  2975. }
  2976. wait_for_completion_timeout(&mrioc->init_cmds.done,
  2977. (MPI3MR_INTADMCMD_TIMEOUT * HZ));
  2978. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  2979. ioc_err(mrioc, "Issue EvtNotify: command timed out\n");
  2980. if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
  2981. mpi3mr_soft_reset_handler(mrioc,
  2982. MPI3MR_RESET_FROM_EVTACK_TIMEOUT, 1);
  2983. retval = -1;
  2984. goto out_unlock;
  2985. }
  2986. if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
  2987. != MPI3_IOCSTATUS_SUCCESS) {
  2988. ioc_err(mrioc,
  2989. "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  2990. (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
  2991. mrioc->init_cmds.ioc_loginfo);
  2992. retval = -1;
  2993. goto out_unlock;
  2994. }
  2995. out_unlock:
  2996. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  2997. mutex_unlock(&mrioc->init_cmds.mutex);
  2998. out:
  2999. return retval;
  3000. }
  3001. /**
  3002. * mpi3mr_alloc_chain_bufs - Allocate chain buffers
  3003. * @mrioc: Adapter instance reference
  3004. *
  3005. * Allocate chain buffers and set a bitmap to indicate free
  3006. * chain buffers. Chain buffers are used to pass the SGE
  3007. * information along with MPI3 SCSI IO requests for host I/O.
  3008. *
  3009. * Return: 0 on success, non-zero on failure
  3010. */
  3011. static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
  3012. {
  3013. int retval = 0;
  3014. u32 sz, i;
  3015. u16 num_chains;
  3016. if (mrioc->chain_sgl_list)
  3017. return retval;
  3018. num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
  3019. if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION
  3020. | SHOST_DIX_TYPE1_PROTECTION
  3021. | SHOST_DIX_TYPE2_PROTECTION
  3022. | SHOST_DIX_TYPE3_PROTECTION))
  3023. num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR);
  3024. mrioc->chain_buf_count = num_chains;
  3025. sz = sizeof(struct chain_element) * num_chains;
  3026. mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
  3027. if (!mrioc->chain_sgl_list)
  3028. goto out_failed;
  3029. sz = MPI3MR_PAGE_SIZE_4K;
  3030. mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
  3031. &mrioc->pdev->dev, sz, 16, 0);
  3032. if (!mrioc->chain_buf_pool) {
  3033. ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
  3034. goto out_failed;
  3035. }
  3036. for (i = 0; i < num_chains; i++) {
  3037. mrioc->chain_sgl_list[i].addr =
  3038. dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
  3039. &mrioc->chain_sgl_list[i].dma_addr);
  3040. if (!mrioc->chain_sgl_list[i].addr)
  3041. goto out_failed;
  3042. }
  3043. mrioc->chain_bitmap = bitmap_zalloc(num_chains, GFP_KERNEL);
  3044. if (!mrioc->chain_bitmap)
  3045. goto out_failed;
  3046. return retval;
  3047. out_failed:
  3048. retval = -1;
  3049. return retval;
  3050. }
  3051. /**
  3052. * mpi3mr_port_enable_complete - Mark port enable complete
  3053. * @mrioc: Adapter instance reference
  3054. * @drv_cmd: Internal command tracker
  3055. *
  3056. * Call back for asynchronous port enable request sets the
  3057. * driver command to indicate port enable request is complete.
  3058. *
  3059. * Return: Nothing
  3060. */
  3061. static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc,
  3062. struct mpi3mr_drv_cmd *drv_cmd)
  3063. {
  3064. drv_cmd->callback = NULL;
  3065. mrioc->scan_started = 0;
  3066. if (drv_cmd->state & MPI3MR_CMD_RESET)
  3067. mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
  3068. else
  3069. mrioc->scan_failed = drv_cmd->ioc_status;
  3070. drv_cmd->state = MPI3MR_CMD_NOTUSED;
  3071. }
  3072. /**
  3073. * mpi3mr_issue_port_enable - Issue Port Enable
  3074. * @mrioc: Adapter instance reference
  3075. * @async: Flag to wait for completion or not
  3076. *
  3077. * Issue Port Enable MPI request through admin queue and if the
  3078. * async flag is not set wait for the completion of the port
  3079. * enable or time out.
  3080. *
  3081. * Return: 0 on success, non-zero on failures.
  3082. */
  3083. int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async)
  3084. {
  3085. struct mpi3_port_enable_request pe_req;
  3086. int retval = 0;
  3087. u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
  3088. memset(&pe_req, 0, sizeof(pe_req));
  3089. mutex_lock(&mrioc->init_cmds.mutex);
  3090. if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
  3091. retval = -1;
  3092. ioc_err(mrioc, "Issue PortEnable: Init command is in use\n");
  3093. mutex_unlock(&mrioc->init_cmds.mutex);
  3094. goto out;
  3095. }
  3096. mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
  3097. if (async) {
  3098. mrioc->init_cmds.is_waiting = 0;
  3099. mrioc->init_cmds.callback = mpi3mr_port_enable_complete;
  3100. } else {
  3101. mrioc->init_cmds.is_waiting = 1;
  3102. mrioc->init_cmds.callback = NULL;
  3103. init_completion(&mrioc->init_cmds.done);
  3104. }
  3105. pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
  3106. pe_req.function = MPI3_FUNCTION_PORT_ENABLE;
  3107. retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1);
  3108. if (retval) {
  3109. ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n");
  3110. goto out_unlock;
  3111. }
  3112. if (async) {
  3113. mutex_unlock(&mrioc->init_cmds.mutex);
  3114. goto out;
  3115. }
  3116. wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ));
  3117. if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
  3118. ioc_err(mrioc, "port enable timed out\n");
  3119. retval = -1;
  3120. mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT);
  3121. goto out_unlock;
  3122. }
  3123. mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds);
  3124. out_unlock:
  3125. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  3126. mutex_unlock(&mrioc->init_cmds.mutex);
  3127. out:
  3128. return retval;
  3129. }
  3130. /* Protocol type to name mapper structure */
  3131. static const struct {
  3132. u8 protocol;
  3133. char *name;
  3134. } mpi3mr_protocols[] = {
  3135. { MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" },
  3136. { MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" },
  3137. { MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" },
  3138. };
  3139. /* Capability to name mapper structure*/
  3140. static const struct {
  3141. u32 capability;
  3142. char *name;
  3143. } mpi3mr_capabilities[] = {
  3144. { MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE, "RAID" },
  3145. { MPI3_IOCFACTS_CAPABILITY_MULTIPATH_ENABLED, "MultiPath" },
  3146. };
  3147. /**
  3148. * mpi3mr_print_ioc_info - Display controller information
  3149. * @mrioc: Adapter instance reference
  3150. *
  3151. * Display controller personalit, capability, supported
  3152. * protocols etc.
  3153. *
  3154. * Return: Nothing
  3155. */
  3156. static void
  3157. mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc)
  3158. {
  3159. int i = 0, bytes_written = 0;
  3160. char personality[16];
  3161. char protocol[50] = {0};
  3162. char capabilities[100] = {0};
  3163. struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
  3164. switch (mrioc->facts.personality) {
  3165. case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
  3166. strncpy(personality, "Enhanced HBA", sizeof(personality));
  3167. break;
  3168. case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
  3169. strncpy(personality, "RAID", sizeof(personality));
  3170. break;
  3171. default:
  3172. strncpy(personality, "Unknown", sizeof(personality));
  3173. break;
  3174. }
  3175. ioc_info(mrioc, "Running in %s Personality", personality);
  3176. ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n",
  3177. fwver->gen_major, fwver->gen_minor, fwver->ph_major,
  3178. fwver->ph_minor, fwver->cust_id, fwver->build_num);
  3179. for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) {
  3180. if (mrioc->facts.protocol_flags &
  3181. mpi3mr_protocols[i].protocol) {
  3182. bytes_written += scnprintf(protocol + bytes_written,
  3183. sizeof(protocol) - bytes_written, "%s%s",
  3184. bytes_written ? "," : "",
  3185. mpi3mr_protocols[i].name);
  3186. }
  3187. }
  3188. bytes_written = 0;
  3189. for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) {
  3190. if (mrioc->facts.protocol_flags &
  3191. mpi3mr_capabilities[i].capability) {
  3192. bytes_written += scnprintf(capabilities + bytes_written,
  3193. sizeof(capabilities) - bytes_written, "%s%s",
  3194. bytes_written ? "," : "",
  3195. mpi3mr_capabilities[i].name);
  3196. }
  3197. }
  3198. ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n",
  3199. protocol, capabilities);
  3200. }
  3201. /**
  3202. * mpi3mr_cleanup_resources - Free PCI resources
  3203. * @mrioc: Adapter instance reference
  3204. *
  3205. * Unmap PCI device memory and disable PCI device.
  3206. *
  3207. * Return: 0 on success and non-zero on failure.
  3208. */
  3209. void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
  3210. {
  3211. struct pci_dev *pdev = mrioc->pdev;
  3212. mpi3mr_cleanup_isr(mrioc);
  3213. if (mrioc->sysif_regs) {
  3214. iounmap((void __iomem *)mrioc->sysif_regs);
  3215. mrioc->sysif_regs = NULL;
  3216. }
  3217. if (pci_is_enabled(pdev)) {
  3218. if (mrioc->bars)
  3219. pci_release_selected_regions(pdev, mrioc->bars);
  3220. pci_disable_device(pdev);
  3221. }
  3222. }
  3223. /**
  3224. * mpi3mr_setup_resources - Enable PCI resources
  3225. * @mrioc: Adapter instance reference
  3226. *
  3227. * Enable PCI device memory, MSI-x registers and set DMA mask.
  3228. *
  3229. * Return: 0 on success and non-zero on failure.
  3230. */
  3231. int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
  3232. {
  3233. struct pci_dev *pdev = mrioc->pdev;
  3234. u32 memap_sz = 0;
  3235. int i, retval = 0, capb = 0;
  3236. u16 message_control;
  3237. u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
  3238. (((dma_get_required_mask(&pdev->dev) > DMA_BIT_MASK(32)) &&
  3239. (sizeof(dma_addr_t) > 4)) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
  3240. if (pci_enable_device_mem(pdev)) {
  3241. ioc_err(mrioc, "pci_enable_device_mem: failed\n");
  3242. retval = -ENODEV;
  3243. goto out_failed;
  3244. }
  3245. capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3246. if (!capb) {
  3247. ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
  3248. retval = -ENODEV;
  3249. goto out_failed;
  3250. }
  3251. mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  3252. if (pci_request_selected_regions(pdev, mrioc->bars,
  3253. mrioc->driver_name)) {
  3254. ioc_err(mrioc, "pci_request_selected_regions: failed\n");
  3255. retval = -ENODEV;
  3256. goto out_failed;
  3257. }
  3258. for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
  3259. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  3260. mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
  3261. memap_sz = pci_resource_len(pdev, i);
  3262. mrioc->sysif_regs =
  3263. ioremap(mrioc->sysif_regs_phys, memap_sz);
  3264. break;
  3265. }
  3266. }
  3267. pci_set_master(pdev);
  3268. retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
  3269. if (retval) {
  3270. if (dma_mask != DMA_BIT_MASK(32)) {
  3271. ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
  3272. dma_mask = DMA_BIT_MASK(32);
  3273. retval = dma_set_mask_and_coherent(&pdev->dev,
  3274. dma_mask);
  3275. }
  3276. if (retval) {
  3277. mrioc->dma_mask = 0;
  3278. ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
  3279. goto out_failed;
  3280. }
  3281. }
  3282. mrioc->dma_mask = dma_mask;
  3283. if (!mrioc->sysif_regs) {
  3284. ioc_err(mrioc,
  3285. "Unable to map adapter memory or resource not found\n");
  3286. retval = -EINVAL;
  3287. goto out_failed;
  3288. }
  3289. pci_read_config_word(pdev, capb + 2, &message_control);
  3290. mrioc->msix_count = (message_control & 0x3FF) + 1;
  3291. pci_save_state(pdev);
  3292. pci_set_drvdata(pdev, mrioc->shost);
  3293. mpi3mr_ioc_disable_intr(mrioc);
  3294. ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  3295. (unsigned long long)mrioc->sysif_regs_phys,
  3296. mrioc->sysif_regs, memap_sz);
  3297. ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
  3298. mrioc->msix_count);
  3299. if (!reset_devices && poll_queues > 0)
  3300. mrioc->requested_poll_qcount = min_t(int, poll_queues,
  3301. mrioc->msix_count - 2);
  3302. return retval;
  3303. out_failed:
  3304. mpi3mr_cleanup_resources(mrioc);
  3305. return retval;
  3306. }
  3307. /**
  3308. * mpi3mr_enable_events - Enable required events
  3309. * @mrioc: Adapter instance reference
  3310. *
  3311. * This routine unmasks the events required by the driver by
  3312. * sennding appropriate event mask bitmapt through an event
  3313. * notification request.
  3314. *
  3315. * Return: 0 on success and non-zero on failure.
  3316. */
  3317. static int mpi3mr_enable_events(struct mpi3mr_ioc *mrioc)
  3318. {
  3319. int retval = 0;
  3320. u32 i;
  3321. for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3322. mrioc->event_masks[i] = -1;
  3323. mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED);
  3324. mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED);
  3325. mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
  3326. mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
  3327. mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_ADDED);
  3328. mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  3329. mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY);
  3330. mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
  3331. mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
  3332. mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
  3333. mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION);
  3334. mpi3mr_unmask_events(mrioc, MPI3_EVENT_PREPARE_FOR_RESET);
  3335. mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT);
  3336. mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE);
  3337. retval = mpi3mr_issue_event_notification(mrioc);
  3338. if (retval)
  3339. ioc_err(mrioc, "failed to issue event notification %d\n",
  3340. retval);
  3341. return retval;
  3342. }
  3343. /**
  3344. * mpi3mr_init_ioc - Initialize the controller
  3345. * @mrioc: Adapter instance reference
  3346. *
  3347. * This the controller initialization routine, executed either
  3348. * after soft reset or from pci probe callback.
  3349. * Setup the required resources, memory map the controller
  3350. * registers, create admin and operational reply queue pairs,
  3351. * allocate required memory for reply pool, sense buffer pool,
  3352. * issue IOC init request to the firmware, unmask the events and
  3353. * issue port enable to discover SAS/SATA/NVMe devies and RAID
  3354. * volumes.
  3355. *
  3356. * Return: 0 on success and non-zero on failure.
  3357. */
  3358. int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc)
  3359. {
  3360. int retval = 0;
  3361. u8 retry = 0;
  3362. struct mpi3_ioc_facts_data facts_data;
  3363. u32 sz;
  3364. retry_init:
  3365. retval = mpi3mr_bring_ioc_ready(mrioc);
  3366. if (retval) {
  3367. ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
  3368. retval);
  3369. goto out_failed_noretry;
  3370. }
  3371. retval = mpi3mr_setup_isr(mrioc, 1);
  3372. if (retval) {
  3373. ioc_err(mrioc, "Failed to setup ISR error %d\n",
  3374. retval);
  3375. goto out_failed_noretry;
  3376. }
  3377. retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
  3378. if (retval) {
  3379. ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
  3380. retval);
  3381. goto out_failed;
  3382. }
  3383. mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
  3384. mrioc->num_io_throttle_group = mrioc->facts.max_io_throttle_group;
  3385. atomic_set(&mrioc->pend_large_data_sz, 0);
  3386. if (reset_devices)
  3387. mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
  3388. MPI3MR_HOST_IOS_KDUMP);
  3389. if (!(mrioc->facts.ioc_capabilities &
  3390. MPI3_IOCFACTS_CAPABILITY_MULTIPATH_ENABLED)) {
  3391. mrioc->sas_transport_enabled = 1;
  3392. mrioc->scsi_device_channel = 1;
  3393. mrioc->shost->max_channel = 1;
  3394. mrioc->shost->transportt = mpi3mr_transport_template;
  3395. }
  3396. mrioc->reply_sz = mrioc->facts.reply_sz;
  3397. retval = mpi3mr_check_reset_dma_mask(mrioc);
  3398. if (retval) {
  3399. ioc_err(mrioc, "Resetting dma mask failed %d\n",
  3400. retval);
  3401. goto out_failed_noretry;
  3402. }
  3403. mpi3mr_print_ioc_info(mrioc);
  3404. if (!mrioc->cfg_page) {
  3405. dprint_init(mrioc, "allocating config page buffers\n");
  3406. mrioc->cfg_page_sz = MPI3MR_DEFAULT_CFG_PAGE_SZ;
  3407. mrioc->cfg_page = dma_alloc_coherent(&mrioc->pdev->dev,
  3408. mrioc->cfg_page_sz, &mrioc->cfg_page_dma, GFP_KERNEL);
  3409. if (!mrioc->cfg_page) {
  3410. retval = -1;
  3411. goto out_failed_noretry;
  3412. }
  3413. }
  3414. if (!mrioc->init_cmds.reply) {
  3415. retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
  3416. if (retval) {
  3417. ioc_err(mrioc,
  3418. "%s :Failed to allocated reply sense buffers %d\n",
  3419. __func__, retval);
  3420. goto out_failed_noretry;
  3421. }
  3422. }
  3423. if (!mrioc->chain_sgl_list) {
  3424. retval = mpi3mr_alloc_chain_bufs(mrioc);
  3425. if (retval) {
  3426. ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
  3427. retval);
  3428. goto out_failed_noretry;
  3429. }
  3430. }
  3431. retval = mpi3mr_issue_iocinit(mrioc);
  3432. if (retval) {
  3433. ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
  3434. retval);
  3435. goto out_failed;
  3436. }
  3437. retval = mpi3mr_print_pkg_ver(mrioc);
  3438. if (retval) {
  3439. ioc_err(mrioc, "failed to get package version\n");
  3440. goto out_failed;
  3441. }
  3442. retval = mpi3mr_setup_isr(mrioc, 0);
  3443. if (retval) {
  3444. ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
  3445. retval);
  3446. goto out_failed_noretry;
  3447. }
  3448. retval = mpi3mr_create_op_queues(mrioc);
  3449. if (retval) {
  3450. ioc_err(mrioc, "Failed to create OpQueues error %d\n",
  3451. retval);
  3452. goto out_failed;
  3453. }
  3454. if (!mrioc->pel_seqnum_virt) {
  3455. dprint_init(mrioc, "allocating memory for pel_seqnum_virt\n");
  3456. mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
  3457. mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
  3458. mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
  3459. GFP_KERNEL);
  3460. if (!mrioc->pel_seqnum_virt) {
  3461. retval = -ENOMEM;
  3462. goto out_failed_noretry;
  3463. }
  3464. }
  3465. if (!mrioc->throttle_groups && mrioc->num_io_throttle_group) {
  3466. dprint_init(mrioc, "allocating memory for throttle groups\n");
  3467. sz = sizeof(struct mpi3mr_throttle_group_info);
  3468. mrioc->throttle_groups = kcalloc(mrioc->num_io_throttle_group, sz, GFP_KERNEL);
  3469. if (!mrioc->throttle_groups) {
  3470. retval = -1;
  3471. goto out_failed_noretry;
  3472. }
  3473. }
  3474. retval = mpi3mr_enable_events(mrioc);
  3475. if (retval) {
  3476. ioc_err(mrioc, "failed to enable events %d\n",
  3477. retval);
  3478. goto out_failed;
  3479. }
  3480. ioc_info(mrioc, "controller initialization completed successfully\n");
  3481. return retval;
  3482. out_failed:
  3483. if (retry < 2) {
  3484. retry++;
  3485. ioc_warn(mrioc, "retrying controller initialization, retry_count:%d\n",
  3486. retry);
  3487. mpi3mr_memset_buffers(mrioc);
  3488. goto retry_init;
  3489. }
  3490. retval = -1;
  3491. out_failed_noretry:
  3492. ioc_err(mrioc, "controller initialization failed\n");
  3493. mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
  3494. MPI3MR_RESET_FROM_CTLR_CLEANUP);
  3495. mrioc->unrecoverable = 1;
  3496. return retval;
  3497. }
  3498. /**
  3499. * mpi3mr_reinit_ioc - Re-Initialize the controller
  3500. * @mrioc: Adapter instance reference
  3501. * @is_resume: Called from resume or reset path
  3502. *
  3503. * This the controller re-initialization routine, executed from
  3504. * the soft reset handler or resume callback. Creates
  3505. * operational reply queue pairs, allocate required memory for
  3506. * reply pool, sense buffer pool, issue IOC init request to the
  3507. * firmware, unmask the events and issue port enable to discover
  3508. * SAS/SATA/NVMe devices and RAID volumes.
  3509. *
  3510. * Return: 0 on success and non-zero on failure.
  3511. */
  3512. int mpi3mr_reinit_ioc(struct mpi3mr_ioc *mrioc, u8 is_resume)
  3513. {
  3514. int retval = 0;
  3515. u8 retry = 0;
  3516. struct mpi3_ioc_facts_data facts_data;
  3517. u32 pe_timeout, ioc_status;
  3518. retry_init:
  3519. pe_timeout =
  3520. (MPI3MR_PORTENABLE_TIMEOUT / MPI3MR_PORTENABLE_POLL_INTERVAL);
  3521. dprint_reset(mrioc, "bringing up the controller to ready state\n");
  3522. retval = mpi3mr_bring_ioc_ready(mrioc);
  3523. if (retval) {
  3524. ioc_err(mrioc, "failed to bring to ready state\n");
  3525. goto out_failed_noretry;
  3526. }
  3527. if (is_resume) {
  3528. dprint_reset(mrioc, "setting up single ISR\n");
  3529. retval = mpi3mr_setup_isr(mrioc, 1);
  3530. if (retval) {
  3531. ioc_err(mrioc, "failed to setup ISR\n");
  3532. goto out_failed_noretry;
  3533. }
  3534. } else
  3535. mpi3mr_ioc_enable_intr(mrioc);
  3536. dprint_reset(mrioc, "getting ioc_facts\n");
  3537. retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
  3538. if (retval) {
  3539. ioc_err(mrioc, "failed to get ioc_facts\n");
  3540. goto out_failed;
  3541. }
  3542. dprint_reset(mrioc, "validating ioc_facts\n");
  3543. retval = mpi3mr_revalidate_factsdata(mrioc);
  3544. if (retval) {
  3545. ioc_err(mrioc, "failed to revalidate ioc_facts data\n");
  3546. goto out_failed_noretry;
  3547. }
  3548. mpi3mr_print_ioc_info(mrioc);
  3549. dprint_reset(mrioc, "sending ioc_init\n");
  3550. retval = mpi3mr_issue_iocinit(mrioc);
  3551. if (retval) {
  3552. ioc_err(mrioc, "failed to send ioc_init\n");
  3553. goto out_failed;
  3554. }
  3555. dprint_reset(mrioc, "getting package version\n");
  3556. retval = mpi3mr_print_pkg_ver(mrioc);
  3557. if (retval) {
  3558. ioc_err(mrioc, "failed to get package version\n");
  3559. goto out_failed;
  3560. }
  3561. if (is_resume) {
  3562. dprint_reset(mrioc, "setting up multiple ISR\n");
  3563. retval = mpi3mr_setup_isr(mrioc, 0);
  3564. if (retval) {
  3565. ioc_err(mrioc, "failed to re-setup ISR\n");
  3566. goto out_failed_noretry;
  3567. }
  3568. }
  3569. dprint_reset(mrioc, "creating operational queue pairs\n");
  3570. retval = mpi3mr_create_op_queues(mrioc);
  3571. if (retval) {
  3572. ioc_err(mrioc, "failed to create operational queue pairs\n");
  3573. goto out_failed;
  3574. }
  3575. if (!mrioc->pel_seqnum_virt) {
  3576. dprint_reset(mrioc, "allocating memory for pel_seqnum_virt\n");
  3577. mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
  3578. mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
  3579. mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
  3580. GFP_KERNEL);
  3581. if (!mrioc->pel_seqnum_virt) {
  3582. retval = -ENOMEM;
  3583. goto out_failed_noretry;
  3584. }
  3585. }
  3586. if (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q) {
  3587. ioc_err(mrioc,
  3588. "cannot create minimum number of operational queues expected:%d created:%d\n",
  3589. mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q);
  3590. retval = -1;
  3591. goto out_failed_noretry;
  3592. }
  3593. dprint_reset(mrioc, "enabling events\n");
  3594. retval = mpi3mr_enable_events(mrioc);
  3595. if (retval) {
  3596. ioc_err(mrioc, "failed to enable events\n");
  3597. goto out_failed;
  3598. }
  3599. mrioc->device_refresh_on = 1;
  3600. mpi3mr_add_event_wait_for_device_refresh(mrioc);
  3601. ioc_info(mrioc, "sending port enable\n");
  3602. retval = mpi3mr_issue_port_enable(mrioc, 1);
  3603. if (retval) {
  3604. ioc_err(mrioc, "failed to issue port enable\n");
  3605. goto out_failed;
  3606. }
  3607. do {
  3608. ssleep(MPI3MR_PORTENABLE_POLL_INTERVAL);
  3609. if (mrioc->init_cmds.state == MPI3MR_CMD_NOTUSED)
  3610. break;
  3611. if (!pci_device_is_present(mrioc->pdev))
  3612. mrioc->unrecoverable = 1;
  3613. if (mrioc->unrecoverable) {
  3614. retval = -1;
  3615. goto out_failed_noretry;
  3616. }
  3617. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  3618. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
  3619. (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
  3620. mpi3mr_print_fault_info(mrioc);
  3621. mrioc->init_cmds.is_waiting = 0;
  3622. mrioc->init_cmds.callback = NULL;
  3623. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  3624. goto out_failed;
  3625. }
  3626. } while (--pe_timeout);
  3627. if (!pe_timeout) {
  3628. ioc_err(mrioc, "port enable timed out\n");
  3629. mpi3mr_check_rh_fault_ioc(mrioc,
  3630. MPI3MR_RESET_FROM_PE_TIMEOUT);
  3631. mrioc->init_cmds.is_waiting = 0;
  3632. mrioc->init_cmds.callback = NULL;
  3633. mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
  3634. goto out_failed;
  3635. } else if (mrioc->scan_failed) {
  3636. ioc_err(mrioc,
  3637. "port enable failed with status=0x%04x\n",
  3638. mrioc->scan_failed);
  3639. } else
  3640. ioc_info(mrioc, "port enable completed successfully\n");
  3641. ioc_info(mrioc, "controller %s completed successfully\n",
  3642. (is_resume)?"resume":"re-initialization");
  3643. return retval;
  3644. out_failed:
  3645. if (retry < 2) {
  3646. retry++;
  3647. ioc_warn(mrioc, "retrying controller %s, retry_count:%d\n",
  3648. (is_resume)?"resume":"re-initialization", retry);
  3649. mpi3mr_memset_buffers(mrioc);
  3650. goto retry_init;
  3651. }
  3652. retval = -1;
  3653. out_failed_noretry:
  3654. ioc_err(mrioc, "controller %s is failed\n",
  3655. (is_resume)?"resume":"re-initialization");
  3656. mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
  3657. MPI3MR_RESET_FROM_CTLR_CLEANUP);
  3658. mrioc->unrecoverable = 1;
  3659. return retval;
  3660. }
  3661. /**
  3662. * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's
  3663. * segments
  3664. * @mrioc: Adapter instance reference
  3665. * @qidx: Operational reply queue index
  3666. *
  3667. * Return: Nothing.
  3668. */
  3669. static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
  3670. {
  3671. struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
  3672. struct segments *segments;
  3673. int i, size;
  3674. if (!op_reply_q->q_segments)
  3675. return;
  3676. size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz;
  3677. segments = op_reply_q->q_segments;
  3678. for (i = 0; i < op_reply_q->num_segments; i++)
  3679. memset(segments[i].segment, 0, size);
  3680. }
  3681. /**
  3682. * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's
  3683. * segments
  3684. * @mrioc: Adapter instance reference
  3685. * @qidx: Operational request queue index
  3686. *
  3687. * Return: Nothing.
  3688. */
  3689. static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
  3690. {
  3691. struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
  3692. struct segments *segments;
  3693. int i, size;
  3694. if (!op_req_q->q_segments)
  3695. return;
  3696. size = op_req_q->segment_qd * mrioc->facts.op_req_sz;
  3697. segments = op_req_q->q_segments;
  3698. for (i = 0; i < op_req_q->num_segments; i++)
  3699. memset(segments[i].segment, 0, size);
  3700. }
  3701. /**
  3702. * mpi3mr_memset_buffers - memset memory for a controller
  3703. * @mrioc: Adapter instance reference
  3704. *
  3705. * clear all the memory allocated for a controller, typically
  3706. * called post reset to reuse the memory allocated during the
  3707. * controller init.
  3708. *
  3709. * Return: Nothing.
  3710. */
  3711. void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)
  3712. {
  3713. u16 i;
  3714. struct mpi3mr_throttle_group_info *tg;
  3715. mrioc->change_count = 0;
  3716. mrioc->active_poll_qcount = 0;
  3717. mrioc->default_qcount = 0;
  3718. if (mrioc->admin_req_base)
  3719. memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz);
  3720. if (mrioc->admin_reply_base)
  3721. memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz);
  3722. atomic_set(&mrioc->admin_reply_q_in_use, 0);
  3723. if (mrioc->init_cmds.reply) {
  3724. memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply));
  3725. memset(mrioc->bsg_cmds.reply, 0,
  3726. sizeof(*mrioc->bsg_cmds.reply));
  3727. memset(mrioc->host_tm_cmds.reply, 0,
  3728. sizeof(*mrioc->host_tm_cmds.reply));
  3729. memset(mrioc->pel_cmds.reply, 0,
  3730. sizeof(*mrioc->pel_cmds.reply));
  3731. memset(mrioc->pel_abort_cmd.reply, 0,
  3732. sizeof(*mrioc->pel_abort_cmd.reply));
  3733. memset(mrioc->transport_cmds.reply, 0,
  3734. sizeof(*mrioc->transport_cmds.reply));
  3735. for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
  3736. memset(mrioc->dev_rmhs_cmds[i].reply, 0,
  3737. sizeof(*mrioc->dev_rmhs_cmds[i].reply));
  3738. for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
  3739. memset(mrioc->evtack_cmds[i].reply, 0,
  3740. sizeof(*mrioc->evtack_cmds[i].reply));
  3741. bitmap_clear(mrioc->removepend_bitmap, 0,
  3742. mrioc->dev_handle_bitmap_bits);
  3743. bitmap_clear(mrioc->devrem_bitmap, 0, MPI3MR_NUM_DEVRMCMD);
  3744. bitmap_clear(mrioc->evtack_cmds_bitmap, 0,
  3745. MPI3MR_NUM_EVTACKCMD);
  3746. }
  3747. for (i = 0; i < mrioc->num_queues; i++) {
  3748. mrioc->op_reply_qinfo[i].qid = 0;
  3749. mrioc->op_reply_qinfo[i].ci = 0;
  3750. mrioc->op_reply_qinfo[i].num_replies = 0;
  3751. mrioc->op_reply_qinfo[i].ephase = 0;
  3752. atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
  3753. atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0);
  3754. mpi3mr_memset_op_reply_q_buffers(mrioc, i);
  3755. mrioc->req_qinfo[i].ci = 0;
  3756. mrioc->req_qinfo[i].pi = 0;
  3757. mrioc->req_qinfo[i].num_requests = 0;
  3758. mrioc->req_qinfo[i].qid = 0;
  3759. mrioc->req_qinfo[i].reply_qid = 0;
  3760. spin_lock_init(&mrioc->req_qinfo[i].q_lock);
  3761. mpi3mr_memset_op_req_q_buffers(mrioc, i);
  3762. }
  3763. atomic_set(&mrioc->pend_large_data_sz, 0);
  3764. if (mrioc->throttle_groups) {
  3765. tg = mrioc->throttle_groups;
  3766. for (i = 0; i < mrioc->num_io_throttle_group; i++, tg++) {
  3767. tg->id = 0;
  3768. tg->fw_qd = 0;
  3769. tg->modified_qd = 0;
  3770. tg->io_divert = 0;
  3771. tg->need_qd_reduction = 0;
  3772. tg->high = 0;
  3773. tg->low = 0;
  3774. tg->qd_reduction = 0;
  3775. atomic_set(&tg->pend_large_data_sz, 0);
  3776. }
  3777. }
  3778. }
  3779. /**
  3780. * mpi3mr_free_mem - Free memory allocated for a controller
  3781. * @mrioc: Adapter instance reference
  3782. *
  3783. * Free all the memory allocated for a controller.
  3784. *
  3785. * Return: Nothing.
  3786. */
  3787. void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
  3788. {
  3789. u16 i;
  3790. struct mpi3mr_intr_info *intr_info;
  3791. mpi3mr_free_enclosure_list(mrioc);
  3792. if (mrioc->sense_buf_pool) {
  3793. if (mrioc->sense_buf)
  3794. dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
  3795. mrioc->sense_buf_dma);
  3796. dma_pool_destroy(mrioc->sense_buf_pool);
  3797. mrioc->sense_buf = NULL;
  3798. mrioc->sense_buf_pool = NULL;
  3799. }
  3800. if (mrioc->sense_buf_q_pool) {
  3801. if (mrioc->sense_buf_q)
  3802. dma_pool_free(mrioc->sense_buf_q_pool,
  3803. mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
  3804. dma_pool_destroy(mrioc->sense_buf_q_pool);
  3805. mrioc->sense_buf_q = NULL;
  3806. mrioc->sense_buf_q_pool = NULL;
  3807. }
  3808. if (mrioc->reply_buf_pool) {
  3809. if (mrioc->reply_buf)
  3810. dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
  3811. mrioc->reply_buf_dma);
  3812. dma_pool_destroy(mrioc->reply_buf_pool);
  3813. mrioc->reply_buf = NULL;
  3814. mrioc->reply_buf_pool = NULL;
  3815. }
  3816. if (mrioc->reply_free_q_pool) {
  3817. if (mrioc->reply_free_q)
  3818. dma_pool_free(mrioc->reply_free_q_pool,
  3819. mrioc->reply_free_q, mrioc->reply_free_q_dma);
  3820. dma_pool_destroy(mrioc->reply_free_q_pool);
  3821. mrioc->reply_free_q = NULL;
  3822. mrioc->reply_free_q_pool = NULL;
  3823. }
  3824. for (i = 0; i < mrioc->num_op_req_q; i++)
  3825. mpi3mr_free_op_req_q_segments(mrioc, i);
  3826. for (i = 0; i < mrioc->num_op_reply_q; i++)
  3827. mpi3mr_free_op_reply_q_segments(mrioc, i);
  3828. for (i = 0; i < mrioc->intr_info_count; i++) {
  3829. intr_info = mrioc->intr_info + i;
  3830. intr_info->op_reply_q = NULL;
  3831. }
  3832. kfree(mrioc->req_qinfo);
  3833. mrioc->req_qinfo = NULL;
  3834. mrioc->num_op_req_q = 0;
  3835. kfree(mrioc->op_reply_qinfo);
  3836. mrioc->op_reply_qinfo = NULL;
  3837. mrioc->num_op_reply_q = 0;
  3838. kfree(mrioc->init_cmds.reply);
  3839. mrioc->init_cmds.reply = NULL;
  3840. kfree(mrioc->bsg_cmds.reply);
  3841. mrioc->bsg_cmds.reply = NULL;
  3842. kfree(mrioc->host_tm_cmds.reply);
  3843. mrioc->host_tm_cmds.reply = NULL;
  3844. kfree(mrioc->pel_cmds.reply);
  3845. mrioc->pel_cmds.reply = NULL;
  3846. kfree(mrioc->pel_abort_cmd.reply);
  3847. mrioc->pel_abort_cmd.reply = NULL;
  3848. for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
  3849. kfree(mrioc->evtack_cmds[i].reply);
  3850. mrioc->evtack_cmds[i].reply = NULL;
  3851. }
  3852. bitmap_free(mrioc->removepend_bitmap);
  3853. mrioc->removepend_bitmap = NULL;
  3854. bitmap_free(mrioc->devrem_bitmap);
  3855. mrioc->devrem_bitmap = NULL;
  3856. bitmap_free(mrioc->evtack_cmds_bitmap);
  3857. mrioc->evtack_cmds_bitmap = NULL;
  3858. bitmap_free(mrioc->chain_bitmap);
  3859. mrioc->chain_bitmap = NULL;
  3860. kfree(mrioc->transport_cmds.reply);
  3861. mrioc->transport_cmds.reply = NULL;
  3862. for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
  3863. kfree(mrioc->dev_rmhs_cmds[i].reply);
  3864. mrioc->dev_rmhs_cmds[i].reply = NULL;
  3865. }
  3866. if (mrioc->chain_buf_pool) {
  3867. for (i = 0; i < mrioc->chain_buf_count; i++) {
  3868. if (mrioc->chain_sgl_list[i].addr) {
  3869. dma_pool_free(mrioc->chain_buf_pool,
  3870. mrioc->chain_sgl_list[i].addr,
  3871. mrioc->chain_sgl_list[i].dma_addr);
  3872. mrioc->chain_sgl_list[i].addr = NULL;
  3873. }
  3874. }
  3875. dma_pool_destroy(mrioc->chain_buf_pool);
  3876. mrioc->chain_buf_pool = NULL;
  3877. }
  3878. kfree(mrioc->chain_sgl_list);
  3879. mrioc->chain_sgl_list = NULL;
  3880. if (mrioc->admin_reply_base) {
  3881. dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
  3882. mrioc->admin_reply_base, mrioc->admin_reply_dma);
  3883. mrioc->admin_reply_base = NULL;
  3884. }
  3885. if (mrioc->admin_req_base) {
  3886. dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
  3887. mrioc->admin_req_base, mrioc->admin_req_dma);
  3888. mrioc->admin_req_base = NULL;
  3889. }
  3890. if (mrioc->cfg_page) {
  3891. dma_free_coherent(&mrioc->pdev->dev, mrioc->cfg_page_sz,
  3892. mrioc->cfg_page, mrioc->cfg_page_dma);
  3893. mrioc->cfg_page = NULL;
  3894. }
  3895. if (mrioc->pel_seqnum_virt) {
  3896. dma_free_coherent(&mrioc->pdev->dev, mrioc->pel_seqnum_sz,
  3897. mrioc->pel_seqnum_virt, mrioc->pel_seqnum_dma);
  3898. mrioc->pel_seqnum_virt = NULL;
  3899. }
  3900. kfree(mrioc->throttle_groups);
  3901. mrioc->throttle_groups = NULL;
  3902. kfree(mrioc->logdata_buf);
  3903. mrioc->logdata_buf = NULL;
  3904. }
  3905. /**
  3906. * mpi3mr_issue_ioc_shutdown - shutdown controller
  3907. * @mrioc: Adapter instance reference
  3908. *
  3909. * Send shutodwn notification to the controller and wait for the
  3910. * shutdown_timeout for it to be completed.
  3911. *
  3912. * Return: Nothing.
  3913. */
  3914. static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
  3915. {
  3916. u32 ioc_config, ioc_status;
  3917. u8 retval = 1;
  3918. u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
  3919. ioc_info(mrioc, "Issuing shutdown Notification\n");
  3920. if (mrioc->unrecoverable) {
  3921. ioc_warn(mrioc,
  3922. "IOC is unrecoverable shutdown is not issued\n");
  3923. return;
  3924. }
  3925. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  3926. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
  3927. == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
  3928. ioc_info(mrioc, "shutdown already in progress\n");
  3929. return;
  3930. }
  3931. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  3932. ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
  3933. ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
  3934. writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
  3935. if (mrioc->facts.shutdown_timeout)
  3936. timeout = mrioc->facts.shutdown_timeout * 10;
  3937. do {
  3938. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  3939. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
  3940. == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
  3941. retval = 0;
  3942. break;
  3943. }
  3944. msleep(100);
  3945. } while (--timeout);
  3946. ioc_status = readl(&mrioc->sysif_regs->ioc_status);
  3947. ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
  3948. if (retval) {
  3949. if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
  3950. == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
  3951. ioc_warn(mrioc,
  3952. "shutdown still in progress after timeout\n");
  3953. }
  3954. ioc_info(mrioc,
  3955. "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
  3956. (!retval) ? "successful" : "failed", ioc_status,
  3957. ioc_config);
  3958. }
  3959. /**
  3960. * mpi3mr_cleanup_ioc - Cleanup controller
  3961. * @mrioc: Adapter instance reference
  3962. *
  3963. * controller cleanup handler, Message unit reset or soft reset
  3964. * and shutdown notification is issued to the controller.
  3965. *
  3966. * Return: Nothing.
  3967. */
  3968. void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)
  3969. {
  3970. enum mpi3mr_iocstate ioc_state;
  3971. dprint_exit(mrioc, "cleaning up the controller\n");
  3972. mpi3mr_ioc_disable_intr(mrioc);
  3973. ioc_state = mpi3mr_get_iocstate(mrioc);
  3974. if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
  3975. (ioc_state == MRIOC_STATE_READY)) {
  3976. if (mpi3mr_issue_and_process_mur(mrioc,
  3977. MPI3MR_RESET_FROM_CTLR_CLEANUP))
  3978. mpi3mr_issue_reset(mrioc,
  3979. MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
  3980. MPI3MR_RESET_FROM_MUR_FAILURE);
  3981. mpi3mr_issue_ioc_shutdown(mrioc);
  3982. }
  3983. dprint_exit(mrioc, "controller cleanup completed\n");
  3984. }
  3985. /**
  3986. * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
  3987. * @mrioc: Adapter instance reference
  3988. * @cmdptr: Internal command tracker
  3989. *
  3990. * Complete an internal driver commands with state indicating it
  3991. * is completed due to reset.
  3992. *
  3993. * Return: Nothing.
  3994. */
  3995. static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc,
  3996. struct mpi3mr_drv_cmd *cmdptr)
  3997. {
  3998. if (cmdptr->state & MPI3MR_CMD_PENDING) {
  3999. cmdptr->state |= MPI3MR_CMD_RESET;
  4000. cmdptr->state &= ~MPI3MR_CMD_PENDING;
  4001. if (cmdptr->is_waiting) {
  4002. complete(&cmdptr->done);
  4003. cmdptr->is_waiting = 0;
  4004. } else if (cmdptr->callback)
  4005. cmdptr->callback(mrioc, cmdptr);
  4006. }
  4007. }
  4008. /**
  4009. * mpi3mr_flush_drv_cmds - Flush internaldriver commands
  4010. * @mrioc: Adapter instance reference
  4011. *
  4012. * Flush all internal driver commands post reset
  4013. *
  4014. * Return: Nothing.
  4015. */
  4016. void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc)
  4017. {
  4018. struct mpi3mr_drv_cmd *cmdptr;
  4019. u8 i;
  4020. cmdptr = &mrioc->init_cmds;
  4021. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4022. cmdptr = &mrioc->cfg_cmds;
  4023. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4024. cmdptr = &mrioc->bsg_cmds;
  4025. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4026. cmdptr = &mrioc->host_tm_cmds;
  4027. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4028. for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
  4029. cmdptr = &mrioc->dev_rmhs_cmds[i];
  4030. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4031. }
  4032. for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
  4033. cmdptr = &mrioc->evtack_cmds[i];
  4034. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4035. }
  4036. cmdptr = &mrioc->pel_cmds;
  4037. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4038. cmdptr = &mrioc->pel_abort_cmd;
  4039. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4040. cmdptr = &mrioc->transport_cmds;
  4041. mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
  4042. }
  4043. /**
  4044. * mpi3mr_pel_wait_post - Issue PEL Wait
  4045. * @mrioc: Adapter instance reference
  4046. * @drv_cmd: Internal command tracker
  4047. *
  4048. * Issue PEL Wait MPI request through admin queue and return.
  4049. *
  4050. * Return: Nothing.
  4051. */
  4052. static void mpi3mr_pel_wait_post(struct mpi3mr_ioc *mrioc,
  4053. struct mpi3mr_drv_cmd *drv_cmd)
  4054. {
  4055. struct mpi3_pel_req_action_wait pel_wait;
  4056. mrioc->pel_abort_requested = false;
  4057. memset(&pel_wait, 0, sizeof(pel_wait));
  4058. drv_cmd->state = MPI3MR_CMD_PENDING;
  4059. drv_cmd->is_waiting = 0;
  4060. drv_cmd->callback = mpi3mr_pel_wait_complete;
  4061. drv_cmd->ioc_status = 0;
  4062. drv_cmd->ioc_loginfo = 0;
  4063. pel_wait.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
  4064. pel_wait.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
  4065. pel_wait.action = MPI3_PEL_ACTION_WAIT;
  4066. pel_wait.starting_sequence_number = cpu_to_le32(mrioc->pel_newest_seqnum);
  4067. pel_wait.locale = cpu_to_le16(mrioc->pel_locale);
  4068. pel_wait.class = cpu_to_le16(mrioc->pel_class);
  4069. pel_wait.wait_time = MPI3_PEL_WAITTIME_INFINITE_WAIT;
  4070. dprint_bsg_info(mrioc, "sending pel_wait seqnum(%d), class(%d), locale(0x%08x)\n",
  4071. mrioc->pel_newest_seqnum, mrioc->pel_class, mrioc->pel_locale);
  4072. if (mpi3mr_admin_request_post(mrioc, &pel_wait, sizeof(pel_wait), 0)) {
  4073. dprint_bsg_err(mrioc,
  4074. "Issuing PELWait: Admin post failed\n");
  4075. drv_cmd->state = MPI3MR_CMD_NOTUSED;
  4076. drv_cmd->callback = NULL;
  4077. drv_cmd->retry_count = 0;
  4078. mrioc->pel_enabled = false;
  4079. }
  4080. }
  4081. /**
  4082. * mpi3mr_pel_get_seqnum_post - Issue PEL Get Sequence number
  4083. * @mrioc: Adapter instance reference
  4084. * @drv_cmd: Internal command tracker
  4085. *
  4086. * Issue PEL get sequence number MPI request through admin queue
  4087. * and return.
  4088. *
  4089. * Return: 0 on success, non-zero on failure.
  4090. */
  4091. int mpi3mr_pel_get_seqnum_post(struct mpi3mr_ioc *mrioc,
  4092. struct mpi3mr_drv_cmd *drv_cmd)
  4093. {
  4094. struct mpi3_pel_req_action_get_sequence_numbers pel_getseq_req;
  4095. u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
  4096. int retval = 0;
  4097. memset(&pel_getseq_req, 0, sizeof(pel_getseq_req));
  4098. mrioc->pel_cmds.state = MPI3MR_CMD_PENDING;
  4099. mrioc->pel_cmds.is_waiting = 0;
  4100. mrioc->pel_cmds.ioc_status = 0;
  4101. mrioc->pel_cmds.ioc_loginfo = 0;
  4102. mrioc->pel_cmds.callback = mpi3mr_pel_get_seqnum_complete;
  4103. pel_getseq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
  4104. pel_getseq_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
  4105. pel_getseq_req.action = MPI3_PEL_ACTION_GET_SEQNUM;
  4106. mpi3mr_add_sg_single(&pel_getseq_req.sgl, sgl_flags,
  4107. mrioc->pel_seqnum_sz, mrioc->pel_seqnum_dma);
  4108. retval = mpi3mr_admin_request_post(mrioc, &pel_getseq_req,
  4109. sizeof(pel_getseq_req), 0);
  4110. if (retval) {
  4111. if (drv_cmd) {
  4112. drv_cmd->state = MPI3MR_CMD_NOTUSED;
  4113. drv_cmd->callback = NULL;
  4114. drv_cmd->retry_count = 0;
  4115. }
  4116. mrioc->pel_enabled = false;
  4117. }
  4118. return retval;
  4119. }
  4120. /**
  4121. * mpi3mr_pel_wait_complete - PELWait Completion callback
  4122. * @mrioc: Adapter instance reference
  4123. * @drv_cmd: Internal command tracker
  4124. *
  4125. * This is a callback handler for the PELWait request and
  4126. * firmware completes a PELWait request when it is aborted or a
  4127. * new PEL entry is available. This sends AEN to the application
  4128. * and if the PELwait completion is not due to PELAbort then
  4129. * this will send a request for new PEL Sequence number
  4130. *
  4131. * Return: Nothing.
  4132. */
  4133. static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc,
  4134. struct mpi3mr_drv_cmd *drv_cmd)
  4135. {
  4136. struct mpi3_pel_reply *pel_reply = NULL;
  4137. u16 ioc_status, pe_log_status;
  4138. bool do_retry = false;
  4139. if (drv_cmd->state & MPI3MR_CMD_RESET)
  4140. goto cleanup_drv_cmd;
  4141. ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
  4142. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4143. ioc_err(mrioc, "%s: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
  4144. __func__, ioc_status, drv_cmd->ioc_loginfo);
  4145. dprint_bsg_err(mrioc,
  4146. "pel_wait: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
  4147. ioc_status, drv_cmd->ioc_loginfo);
  4148. do_retry = true;
  4149. }
  4150. if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
  4151. pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
  4152. if (!pel_reply) {
  4153. dprint_bsg_err(mrioc,
  4154. "pel_wait: failed due to no reply\n");
  4155. goto out_failed;
  4156. }
  4157. pe_log_status = le16_to_cpu(pel_reply->pe_log_status);
  4158. if ((pe_log_status != MPI3_PEL_STATUS_SUCCESS) &&
  4159. (pe_log_status != MPI3_PEL_STATUS_ABORTED)) {
  4160. ioc_err(mrioc, "%s: Failed pe_log_status(0x%04x)\n",
  4161. __func__, pe_log_status);
  4162. dprint_bsg_err(mrioc,
  4163. "pel_wait: failed due to pel_log_status(0x%04x)\n",
  4164. pe_log_status);
  4165. do_retry = true;
  4166. }
  4167. if (do_retry) {
  4168. if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
  4169. drv_cmd->retry_count++;
  4170. dprint_bsg_err(mrioc, "pel_wait: retrying(%d)\n",
  4171. drv_cmd->retry_count);
  4172. mpi3mr_pel_wait_post(mrioc, drv_cmd);
  4173. return;
  4174. }
  4175. dprint_bsg_err(mrioc,
  4176. "pel_wait: failed after all retries(%d)\n",
  4177. drv_cmd->retry_count);
  4178. goto out_failed;
  4179. }
  4180. atomic64_inc(&event_counter);
  4181. if (!mrioc->pel_abort_requested) {
  4182. mrioc->pel_cmds.retry_count = 0;
  4183. mpi3mr_pel_get_seqnum_post(mrioc, &mrioc->pel_cmds);
  4184. }
  4185. return;
  4186. out_failed:
  4187. mrioc->pel_enabled = false;
  4188. cleanup_drv_cmd:
  4189. drv_cmd->state = MPI3MR_CMD_NOTUSED;
  4190. drv_cmd->callback = NULL;
  4191. drv_cmd->retry_count = 0;
  4192. }
  4193. /**
  4194. * mpi3mr_pel_get_seqnum_complete - PELGetSeqNum Completion callback
  4195. * @mrioc: Adapter instance reference
  4196. * @drv_cmd: Internal command tracker
  4197. *
  4198. * This is a callback handler for the PEL get sequence number
  4199. * request and a new PEL wait request will be issued to the
  4200. * firmware from this
  4201. *
  4202. * Return: Nothing.
  4203. */
  4204. void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc,
  4205. struct mpi3mr_drv_cmd *drv_cmd)
  4206. {
  4207. struct mpi3_pel_reply *pel_reply = NULL;
  4208. struct mpi3_pel_seq *pel_seqnum_virt;
  4209. u16 ioc_status;
  4210. bool do_retry = false;
  4211. pel_seqnum_virt = (struct mpi3_pel_seq *)mrioc->pel_seqnum_virt;
  4212. if (drv_cmd->state & MPI3MR_CMD_RESET)
  4213. goto cleanup_drv_cmd;
  4214. ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
  4215. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4216. dprint_bsg_err(mrioc,
  4217. "pel_get_seqnum: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
  4218. ioc_status, drv_cmd->ioc_loginfo);
  4219. do_retry = true;
  4220. }
  4221. if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
  4222. pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
  4223. if (!pel_reply) {
  4224. dprint_bsg_err(mrioc,
  4225. "pel_get_seqnum: failed due to no reply\n");
  4226. goto out_failed;
  4227. }
  4228. if (le16_to_cpu(pel_reply->pe_log_status) != MPI3_PEL_STATUS_SUCCESS) {
  4229. dprint_bsg_err(mrioc,
  4230. "pel_get_seqnum: failed due to pel_log_status(0x%04x)\n",
  4231. le16_to_cpu(pel_reply->pe_log_status));
  4232. do_retry = true;
  4233. }
  4234. if (do_retry) {
  4235. if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
  4236. drv_cmd->retry_count++;
  4237. dprint_bsg_err(mrioc,
  4238. "pel_get_seqnum: retrying(%d)\n",
  4239. drv_cmd->retry_count);
  4240. mpi3mr_pel_get_seqnum_post(mrioc, drv_cmd);
  4241. return;
  4242. }
  4243. dprint_bsg_err(mrioc,
  4244. "pel_get_seqnum: failed after all retries(%d)\n",
  4245. drv_cmd->retry_count);
  4246. goto out_failed;
  4247. }
  4248. mrioc->pel_newest_seqnum = le32_to_cpu(pel_seqnum_virt->newest) + 1;
  4249. drv_cmd->retry_count = 0;
  4250. mpi3mr_pel_wait_post(mrioc, drv_cmd);
  4251. return;
  4252. out_failed:
  4253. mrioc->pel_enabled = false;
  4254. cleanup_drv_cmd:
  4255. drv_cmd->state = MPI3MR_CMD_NOTUSED;
  4256. drv_cmd->callback = NULL;
  4257. drv_cmd->retry_count = 0;
  4258. }
  4259. /**
  4260. * mpi3mr_soft_reset_handler - Reset the controller
  4261. * @mrioc: Adapter instance reference
  4262. * @reset_reason: Reset reason code
  4263. * @snapdump: Flag to generate snapdump in firmware or not
  4264. *
  4265. * This is an handler for recovering controller by issuing soft
  4266. * reset are diag fault reset. This is a blocking function and
  4267. * when one reset is executed if any other resets they will be
  4268. * blocked. All BSG requests will be blocked during the reset. If
  4269. * controller reset is successful then the controller will be
  4270. * reinitalized, otherwise the controller will be marked as not
  4271. * recoverable
  4272. *
  4273. * In snapdump bit is set, the controller is issued with diag
  4274. * fault reset so that the firmware can create a snap dump and
  4275. * post that the firmware will result in F000 fault and the
  4276. * driver will issue soft reset to recover from that.
  4277. *
  4278. * Return: 0 on success, non-zero on failure.
  4279. */
  4280. int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
  4281. u32 reset_reason, u8 snapdump)
  4282. {
  4283. int retval = 0, i;
  4284. unsigned long flags;
  4285. u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
  4286. /* Block the reset handler until diag save in progress*/
  4287. dprint_reset(mrioc,
  4288. "soft_reset_handler: check and block on diagsave_timeout(%d)\n",
  4289. mrioc->diagsave_timeout);
  4290. while (mrioc->diagsave_timeout)
  4291. ssleep(1);
  4292. /*
  4293. * Block new resets until the currently executing one is finished and
  4294. * return the status of the existing reset for all blocked resets
  4295. */
  4296. dprint_reset(mrioc, "soft_reset_handler: acquiring reset_mutex\n");
  4297. if (!mutex_trylock(&mrioc->reset_mutex)) {
  4298. ioc_info(mrioc,
  4299. "controller reset triggered by %s is blocked due to another reset in progress\n",
  4300. mpi3mr_reset_rc_name(reset_reason));
  4301. do {
  4302. ssleep(1);
  4303. } while (mrioc->reset_in_progress == 1);
  4304. ioc_info(mrioc,
  4305. "returning previous reset result(%d) for the reset triggered by %s\n",
  4306. mrioc->prev_reset_result,
  4307. mpi3mr_reset_rc_name(reset_reason));
  4308. return mrioc->prev_reset_result;
  4309. }
  4310. ioc_info(mrioc, "controller reset is triggered by %s\n",
  4311. mpi3mr_reset_rc_name(reset_reason));
  4312. mrioc->device_refresh_on = 0;
  4313. mrioc->reset_in_progress = 1;
  4314. mrioc->stop_bsgs = 1;
  4315. mrioc->prev_reset_result = -1;
  4316. if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
  4317. (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) &&
  4318. (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
  4319. for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  4320. mrioc->event_masks[i] = -1;
  4321. dprint_reset(mrioc, "soft_reset_handler: masking events\n");
  4322. mpi3mr_issue_event_notification(mrioc);
  4323. }
  4324. mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT);
  4325. mpi3mr_ioc_disable_intr(mrioc);
  4326. if (snapdump) {
  4327. mpi3mr_set_diagsave(mrioc);
  4328. retval = mpi3mr_issue_reset(mrioc,
  4329. MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
  4330. if (!retval) {
  4331. do {
  4332. host_diagnostic =
  4333. readl(&mrioc->sysif_regs->host_diagnostic);
  4334. if (!(host_diagnostic &
  4335. MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
  4336. break;
  4337. msleep(100);
  4338. } while (--timeout);
  4339. }
  4340. }
  4341. retval = mpi3mr_issue_reset(mrioc,
  4342. MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
  4343. if (retval) {
  4344. ioc_err(mrioc, "Failed to issue soft reset to the ioc\n");
  4345. goto out;
  4346. }
  4347. if (mrioc->num_io_throttle_group !=
  4348. mrioc->facts.max_io_throttle_group) {
  4349. ioc_err(mrioc,
  4350. "max io throttle group doesn't match old(%d), new(%d)\n",
  4351. mrioc->num_io_throttle_group,
  4352. mrioc->facts.max_io_throttle_group);
  4353. retval = -EPERM;
  4354. goto out;
  4355. }
  4356. mpi3mr_flush_delayed_cmd_lists(mrioc);
  4357. mpi3mr_flush_drv_cmds(mrioc);
  4358. bitmap_clear(mrioc->devrem_bitmap, 0, MPI3MR_NUM_DEVRMCMD);
  4359. bitmap_clear(mrioc->removepend_bitmap, 0,
  4360. mrioc->dev_handle_bitmap_bits);
  4361. bitmap_clear(mrioc->evtack_cmds_bitmap, 0, MPI3MR_NUM_EVTACKCMD);
  4362. mpi3mr_flush_host_io(mrioc);
  4363. mpi3mr_cleanup_fwevt_list(mrioc);
  4364. mpi3mr_invalidate_devhandles(mrioc);
  4365. mpi3mr_free_enclosure_list(mrioc);
  4366. if (mrioc->prepare_for_reset) {
  4367. mrioc->prepare_for_reset = 0;
  4368. mrioc->prepare_for_reset_timeout_counter = 0;
  4369. }
  4370. mpi3mr_memset_buffers(mrioc);
  4371. retval = mpi3mr_reinit_ioc(mrioc, 0);
  4372. if (retval) {
  4373. pr_err(IOCNAME "reinit after soft reset failed: reason %d\n",
  4374. mrioc->name, reset_reason);
  4375. goto out;
  4376. }
  4377. ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
  4378. out:
  4379. if (!retval) {
  4380. mrioc->diagsave_timeout = 0;
  4381. mrioc->reset_in_progress = 0;
  4382. mrioc->pel_abort_requested = 0;
  4383. if (mrioc->pel_enabled) {
  4384. mrioc->pel_cmds.retry_count = 0;
  4385. mpi3mr_pel_wait_post(mrioc, &mrioc->pel_cmds);
  4386. }
  4387. mrioc->device_refresh_on = 0;
  4388. mrioc->ts_update_counter = 0;
  4389. spin_lock_irqsave(&mrioc->watchdog_lock, flags);
  4390. if (mrioc->watchdog_work_q)
  4391. queue_delayed_work(mrioc->watchdog_work_q,
  4392. &mrioc->watchdog_work,
  4393. msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
  4394. spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
  4395. mrioc->stop_bsgs = 0;
  4396. if (mrioc->pel_enabled)
  4397. atomic64_inc(&event_counter);
  4398. } else {
  4399. mpi3mr_issue_reset(mrioc,
  4400. MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
  4401. mrioc->device_refresh_on = 0;
  4402. mrioc->unrecoverable = 1;
  4403. mrioc->reset_in_progress = 0;
  4404. retval = -1;
  4405. mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
  4406. }
  4407. mrioc->prev_reset_result = retval;
  4408. mutex_unlock(&mrioc->reset_mutex);
  4409. ioc_info(mrioc, "controller reset is %s\n",
  4410. ((retval == 0) ? "successful" : "failed"));
  4411. return retval;
  4412. }
  4413. /**
  4414. * mpi3mr_free_config_dma_memory - free memory for config page
  4415. * @mrioc: Adapter instance reference
  4416. * @mem_desc: memory descriptor structure
  4417. *
  4418. * Check whether the size of the buffer specified by the memory
  4419. * descriptor is greater than the default page size if so then
  4420. * free the memory pointed by the descriptor.
  4421. *
  4422. * Return: Nothing.
  4423. */
  4424. static void mpi3mr_free_config_dma_memory(struct mpi3mr_ioc *mrioc,
  4425. struct dma_memory_desc *mem_desc)
  4426. {
  4427. if ((mem_desc->size > mrioc->cfg_page_sz) && mem_desc->addr) {
  4428. dma_free_coherent(&mrioc->pdev->dev, mem_desc->size,
  4429. mem_desc->addr, mem_desc->dma_addr);
  4430. mem_desc->addr = NULL;
  4431. }
  4432. }
  4433. /**
  4434. * mpi3mr_alloc_config_dma_memory - Alloc memory for config page
  4435. * @mrioc: Adapter instance reference
  4436. * @mem_desc: Memory descriptor to hold dma memory info
  4437. *
  4438. * This function allocates new dmaable memory or provides the
  4439. * default config page dmaable memory based on the memory size
  4440. * described by the descriptor.
  4441. *
  4442. * Return: 0 on success, non-zero on failure.
  4443. */
  4444. static int mpi3mr_alloc_config_dma_memory(struct mpi3mr_ioc *mrioc,
  4445. struct dma_memory_desc *mem_desc)
  4446. {
  4447. if (mem_desc->size > mrioc->cfg_page_sz) {
  4448. mem_desc->addr = dma_alloc_coherent(&mrioc->pdev->dev,
  4449. mem_desc->size, &mem_desc->dma_addr, GFP_KERNEL);
  4450. if (!mem_desc->addr)
  4451. return -ENOMEM;
  4452. } else {
  4453. mem_desc->addr = mrioc->cfg_page;
  4454. mem_desc->dma_addr = mrioc->cfg_page_dma;
  4455. memset(mem_desc->addr, 0, mrioc->cfg_page_sz);
  4456. }
  4457. return 0;
  4458. }
  4459. /**
  4460. * mpi3mr_post_cfg_req - Issue config requests and wait
  4461. * @mrioc: Adapter instance reference
  4462. * @cfg_req: Configuration request
  4463. * @timeout: Timeout in seconds
  4464. * @ioc_status: Pointer to return ioc status
  4465. *
  4466. * A generic function for posting MPI3 configuration request to
  4467. * the firmware. This blocks for the completion of request for
  4468. * timeout seconds and if the request times out this function
  4469. * faults the controller with proper reason code.
  4470. *
  4471. * On successful completion of the request this function returns
  4472. * appropriate ioc status from the firmware back to the caller.
  4473. *
  4474. * Return: 0 on success, non-zero on failure.
  4475. */
  4476. static int mpi3mr_post_cfg_req(struct mpi3mr_ioc *mrioc,
  4477. struct mpi3_config_request *cfg_req, int timeout, u16 *ioc_status)
  4478. {
  4479. int retval = 0;
  4480. mutex_lock(&mrioc->cfg_cmds.mutex);
  4481. if (mrioc->cfg_cmds.state & MPI3MR_CMD_PENDING) {
  4482. retval = -1;
  4483. ioc_err(mrioc, "sending config request failed due to command in use\n");
  4484. mutex_unlock(&mrioc->cfg_cmds.mutex);
  4485. goto out;
  4486. }
  4487. mrioc->cfg_cmds.state = MPI3MR_CMD_PENDING;
  4488. mrioc->cfg_cmds.is_waiting = 1;
  4489. mrioc->cfg_cmds.callback = NULL;
  4490. mrioc->cfg_cmds.ioc_status = 0;
  4491. mrioc->cfg_cmds.ioc_loginfo = 0;
  4492. cfg_req->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_CFG_CMDS);
  4493. cfg_req->function = MPI3_FUNCTION_CONFIG;
  4494. init_completion(&mrioc->cfg_cmds.done);
  4495. dprint_cfg_info(mrioc, "posting config request\n");
  4496. if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
  4497. dprint_dump(cfg_req, sizeof(struct mpi3_config_request),
  4498. "mpi3_cfg_req");
  4499. retval = mpi3mr_admin_request_post(mrioc, cfg_req, sizeof(*cfg_req), 1);
  4500. if (retval) {
  4501. ioc_err(mrioc, "posting config request failed\n");
  4502. goto out_unlock;
  4503. }
  4504. wait_for_completion_timeout(&mrioc->cfg_cmds.done, (timeout * HZ));
  4505. if (!(mrioc->cfg_cmds.state & MPI3MR_CMD_COMPLETE)) {
  4506. mpi3mr_check_rh_fault_ioc(mrioc,
  4507. MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT);
  4508. ioc_err(mrioc, "config request timed out\n");
  4509. retval = -1;
  4510. goto out_unlock;
  4511. }
  4512. *ioc_status = mrioc->cfg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
  4513. if ((*ioc_status) != MPI3_IOCSTATUS_SUCCESS)
  4514. dprint_cfg_err(mrioc,
  4515. "cfg_page request returned with ioc_status(0x%04x), log_info(0x%08x)\n",
  4516. *ioc_status, mrioc->cfg_cmds.ioc_loginfo);
  4517. out_unlock:
  4518. mrioc->cfg_cmds.state = MPI3MR_CMD_NOTUSED;
  4519. mutex_unlock(&mrioc->cfg_cmds.mutex);
  4520. out:
  4521. return retval;
  4522. }
  4523. /**
  4524. * mpi3mr_process_cfg_req - config page request processor
  4525. * @mrioc: Adapter instance reference
  4526. * @cfg_req: Configuration request
  4527. * @cfg_hdr: Configuration page header
  4528. * @timeout: Timeout in seconds
  4529. * @ioc_status: Pointer to return ioc status
  4530. * @cfg_buf: Memory pointer to copy config page or header
  4531. * @cfg_buf_sz: Size of the memory to get config page or header
  4532. *
  4533. * This is handler for config page read, write and config page
  4534. * header read operations.
  4535. *
  4536. * This function expects the cfg_req to be populated with page
  4537. * type, page number, action for the header read and with page
  4538. * address for all other operations.
  4539. *
  4540. * The cfg_hdr can be passed as null for reading required header
  4541. * details for read/write pages the cfg_hdr should point valid
  4542. * configuration page header.
  4543. *
  4544. * This allocates dmaable memory based on the size of the config
  4545. * buffer and set the SGE of the cfg_req.
  4546. *
  4547. * For write actions, the config page data has to be passed in
  4548. * the cfg_buf and size of the data has to be mentioned in the
  4549. * cfg_buf_sz.
  4550. *
  4551. * For read/header actions, on successful completion of the
  4552. * request with successful ioc_status the data will be copied
  4553. * into the cfg_buf limited to a minimum of actual page size and
  4554. * cfg_buf_sz
  4555. *
  4556. *
  4557. * Return: 0 on success, non-zero on failure.
  4558. */
  4559. static int mpi3mr_process_cfg_req(struct mpi3mr_ioc *mrioc,
  4560. struct mpi3_config_request *cfg_req,
  4561. struct mpi3_config_page_header *cfg_hdr, int timeout, u16 *ioc_status,
  4562. void *cfg_buf, u32 cfg_buf_sz)
  4563. {
  4564. struct dma_memory_desc mem_desc;
  4565. int retval = -1;
  4566. u8 invalid_action = 0;
  4567. u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
  4568. memset(&mem_desc, 0, sizeof(struct dma_memory_desc));
  4569. if (cfg_req->action == MPI3_CONFIG_ACTION_PAGE_HEADER)
  4570. mem_desc.size = sizeof(struct mpi3_config_page_header);
  4571. else {
  4572. if (!cfg_hdr) {
  4573. ioc_err(mrioc, "null config header passed for config action(%d), page_type(0x%02x), page_num(%d)\n",
  4574. cfg_req->action, cfg_req->page_type,
  4575. cfg_req->page_number);
  4576. goto out;
  4577. }
  4578. switch (cfg_hdr->page_attribute & MPI3_CONFIG_PAGEATTR_MASK) {
  4579. case MPI3_CONFIG_PAGEATTR_READ_ONLY:
  4580. if (cfg_req->action
  4581. != MPI3_CONFIG_ACTION_READ_CURRENT)
  4582. invalid_action = 1;
  4583. break;
  4584. case MPI3_CONFIG_PAGEATTR_CHANGEABLE:
  4585. if ((cfg_req->action ==
  4586. MPI3_CONFIG_ACTION_READ_PERSISTENT) ||
  4587. (cfg_req->action ==
  4588. MPI3_CONFIG_ACTION_WRITE_PERSISTENT))
  4589. invalid_action = 1;
  4590. break;
  4591. case MPI3_CONFIG_PAGEATTR_PERSISTENT:
  4592. default:
  4593. break;
  4594. }
  4595. if (invalid_action) {
  4596. ioc_err(mrioc,
  4597. "config action(%d) is not allowed for page_type(0x%02x), page_num(%d) with page_attribute(0x%02x)\n",
  4598. cfg_req->action, cfg_req->page_type,
  4599. cfg_req->page_number, cfg_hdr->page_attribute);
  4600. goto out;
  4601. }
  4602. mem_desc.size = le16_to_cpu(cfg_hdr->page_length) * 4;
  4603. cfg_req->page_length = cfg_hdr->page_length;
  4604. cfg_req->page_version = cfg_hdr->page_version;
  4605. }
  4606. if (mpi3mr_alloc_config_dma_memory(mrioc, &mem_desc))
  4607. goto out;
  4608. mpi3mr_add_sg_single(&cfg_req->sgl, sgl_flags, mem_desc.size,
  4609. mem_desc.dma_addr);
  4610. if ((cfg_req->action == MPI3_CONFIG_ACTION_WRITE_PERSISTENT) ||
  4611. (cfg_req->action == MPI3_CONFIG_ACTION_WRITE_CURRENT)) {
  4612. memcpy(mem_desc.addr, cfg_buf, min_t(u16, mem_desc.size,
  4613. cfg_buf_sz));
  4614. dprint_cfg_info(mrioc, "config buffer to be written\n");
  4615. if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
  4616. dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf");
  4617. }
  4618. if (mpi3mr_post_cfg_req(mrioc, cfg_req, timeout, ioc_status))
  4619. goto out;
  4620. retval = 0;
  4621. if ((*ioc_status == MPI3_IOCSTATUS_SUCCESS) &&
  4622. (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_PERSISTENT) &&
  4623. (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_CURRENT)) {
  4624. memcpy(cfg_buf, mem_desc.addr, min_t(u16, mem_desc.size,
  4625. cfg_buf_sz));
  4626. dprint_cfg_info(mrioc, "config buffer read\n");
  4627. if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
  4628. dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf");
  4629. }
  4630. out:
  4631. mpi3mr_free_config_dma_memory(mrioc, &mem_desc);
  4632. return retval;
  4633. }
  4634. /**
  4635. * mpi3mr_cfg_get_dev_pg0 - Read current device page0
  4636. * @mrioc: Adapter instance reference
  4637. * @ioc_status: Pointer to return ioc status
  4638. * @dev_pg0: Pointer to return device page 0
  4639. * @pg_sz: Size of the memory allocated to the page pointer
  4640. * @form: The form to be used for addressing the page
  4641. * @form_spec: Form specific information like device handle
  4642. *
  4643. * This is handler for config page read for a specific device
  4644. * page0. The ioc_status has the controller returned ioc_status.
  4645. * This routine doesn't check ioc_status to decide whether the
  4646. * page read is success or not and it is the callers
  4647. * responsibility.
  4648. *
  4649. * Return: 0 on success, non-zero on failure.
  4650. */
  4651. int mpi3mr_cfg_get_dev_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
  4652. struct mpi3_device_page0 *dev_pg0, u16 pg_sz, u32 form, u32 form_spec)
  4653. {
  4654. struct mpi3_config_page_header cfg_hdr;
  4655. struct mpi3_config_request cfg_req;
  4656. u32 page_address;
  4657. memset(dev_pg0, 0, pg_sz);
  4658. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4659. memset(&cfg_req, 0, sizeof(cfg_req));
  4660. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4661. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4662. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DEVICE;
  4663. cfg_req.page_number = 0;
  4664. cfg_req.page_address = 0;
  4665. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4666. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4667. ioc_err(mrioc, "device page0 header read failed\n");
  4668. goto out_failed;
  4669. }
  4670. if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4671. ioc_err(mrioc, "device page0 header read failed with ioc_status(0x%04x)\n",
  4672. *ioc_status);
  4673. goto out_failed;
  4674. }
  4675. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  4676. page_address = ((form & MPI3_DEVICE_PGAD_FORM_MASK) |
  4677. (form_spec & MPI3_DEVICE_PGAD_HANDLE_MASK));
  4678. cfg_req.page_address = cpu_to_le32(page_address);
  4679. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  4680. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, dev_pg0, pg_sz)) {
  4681. ioc_err(mrioc, "device page0 read failed\n");
  4682. goto out_failed;
  4683. }
  4684. return 0;
  4685. out_failed:
  4686. return -1;
  4687. }
  4688. /**
  4689. * mpi3mr_cfg_get_sas_phy_pg0 - Read current SAS Phy page0
  4690. * @mrioc: Adapter instance reference
  4691. * @ioc_status: Pointer to return ioc status
  4692. * @phy_pg0: Pointer to return SAS Phy page 0
  4693. * @pg_sz: Size of the memory allocated to the page pointer
  4694. * @form: The form to be used for addressing the page
  4695. * @form_spec: Form specific information like phy number
  4696. *
  4697. * This is handler for config page read for a specific SAS Phy
  4698. * page0. The ioc_status has the controller returned ioc_status.
  4699. * This routine doesn't check ioc_status to decide whether the
  4700. * page read is success or not and it is the callers
  4701. * responsibility.
  4702. *
  4703. * Return: 0 on success, non-zero on failure.
  4704. */
  4705. int mpi3mr_cfg_get_sas_phy_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
  4706. struct mpi3_sas_phy_page0 *phy_pg0, u16 pg_sz, u32 form,
  4707. u32 form_spec)
  4708. {
  4709. struct mpi3_config_page_header cfg_hdr;
  4710. struct mpi3_config_request cfg_req;
  4711. u32 page_address;
  4712. memset(phy_pg0, 0, pg_sz);
  4713. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4714. memset(&cfg_req, 0, sizeof(cfg_req));
  4715. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4716. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4717. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_PHY;
  4718. cfg_req.page_number = 0;
  4719. cfg_req.page_address = 0;
  4720. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4721. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4722. ioc_err(mrioc, "sas phy page0 header read failed\n");
  4723. goto out_failed;
  4724. }
  4725. if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4726. ioc_err(mrioc, "sas phy page0 header read failed with ioc_status(0x%04x)\n",
  4727. *ioc_status);
  4728. goto out_failed;
  4729. }
  4730. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  4731. page_address = ((form & MPI3_SAS_PHY_PGAD_FORM_MASK) |
  4732. (form_spec & MPI3_SAS_PHY_PGAD_PHY_NUMBER_MASK));
  4733. cfg_req.page_address = cpu_to_le32(page_address);
  4734. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  4735. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, phy_pg0, pg_sz)) {
  4736. ioc_err(mrioc, "sas phy page0 read failed\n");
  4737. goto out_failed;
  4738. }
  4739. return 0;
  4740. out_failed:
  4741. return -1;
  4742. }
  4743. /**
  4744. * mpi3mr_cfg_get_sas_phy_pg1 - Read current SAS Phy page1
  4745. * @mrioc: Adapter instance reference
  4746. * @ioc_status: Pointer to return ioc status
  4747. * @phy_pg1: Pointer to return SAS Phy page 1
  4748. * @pg_sz: Size of the memory allocated to the page pointer
  4749. * @form: The form to be used for addressing the page
  4750. * @form_spec: Form specific information like phy number
  4751. *
  4752. * This is handler for config page read for a specific SAS Phy
  4753. * page1. The ioc_status has the controller returned ioc_status.
  4754. * This routine doesn't check ioc_status to decide whether the
  4755. * page read is success or not and it is the callers
  4756. * responsibility.
  4757. *
  4758. * Return: 0 on success, non-zero on failure.
  4759. */
  4760. int mpi3mr_cfg_get_sas_phy_pg1(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
  4761. struct mpi3_sas_phy_page1 *phy_pg1, u16 pg_sz, u32 form,
  4762. u32 form_spec)
  4763. {
  4764. struct mpi3_config_page_header cfg_hdr;
  4765. struct mpi3_config_request cfg_req;
  4766. u32 page_address;
  4767. memset(phy_pg1, 0, pg_sz);
  4768. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4769. memset(&cfg_req, 0, sizeof(cfg_req));
  4770. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4771. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4772. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_PHY;
  4773. cfg_req.page_number = 1;
  4774. cfg_req.page_address = 0;
  4775. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4776. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4777. ioc_err(mrioc, "sas phy page1 header read failed\n");
  4778. goto out_failed;
  4779. }
  4780. if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4781. ioc_err(mrioc, "sas phy page1 header read failed with ioc_status(0x%04x)\n",
  4782. *ioc_status);
  4783. goto out_failed;
  4784. }
  4785. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  4786. page_address = ((form & MPI3_SAS_PHY_PGAD_FORM_MASK) |
  4787. (form_spec & MPI3_SAS_PHY_PGAD_PHY_NUMBER_MASK));
  4788. cfg_req.page_address = cpu_to_le32(page_address);
  4789. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  4790. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, phy_pg1, pg_sz)) {
  4791. ioc_err(mrioc, "sas phy page1 read failed\n");
  4792. goto out_failed;
  4793. }
  4794. return 0;
  4795. out_failed:
  4796. return -1;
  4797. }
  4798. /**
  4799. * mpi3mr_cfg_get_sas_exp_pg0 - Read current SAS Expander page0
  4800. * @mrioc: Adapter instance reference
  4801. * @ioc_status: Pointer to return ioc status
  4802. * @exp_pg0: Pointer to return SAS Expander page 0
  4803. * @pg_sz: Size of the memory allocated to the page pointer
  4804. * @form: The form to be used for addressing the page
  4805. * @form_spec: Form specific information like device handle
  4806. *
  4807. * This is handler for config page read for a specific SAS
  4808. * Expander page0. The ioc_status has the controller returned
  4809. * ioc_status. This routine doesn't check ioc_status to decide
  4810. * whether the page read is success or not and it is the callers
  4811. * responsibility.
  4812. *
  4813. * Return: 0 on success, non-zero on failure.
  4814. */
  4815. int mpi3mr_cfg_get_sas_exp_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
  4816. struct mpi3_sas_expander_page0 *exp_pg0, u16 pg_sz, u32 form,
  4817. u32 form_spec)
  4818. {
  4819. struct mpi3_config_page_header cfg_hdr;
  4820. struct mpi3_config_request cfg_req;
  4821. u32 page_address;
  4822. memset(exp_pg0, 0, pg_sz);
  4823. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4824. memset(&cfg_req, 0, sizeof(cfg_req));
  4825. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4826. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4827. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_EXPANDER;
  4828. cfg_req.page_number = 0;
  4829. cfg_req.page_address = 0;
  4830. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4831. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4832. ioc_err(mrioc, "expander page0 header read failed\n");
  4833. goto out_failed;
  4834. }
  4835. if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4836. ioc_err(mrioc, "expander page0 header read failed with ioc_status(0x%04x)\n",
  4837. *ioc_status);
  4838. goto out_failed;
  4839. }
  4840. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  4841. page_address = ((form & MPI3_SAS_EXPAND_PGAD_FORM_MASK) |
  4842. (form_spec & (MPI3_SAS_EXPAND_PGAD_PHYNUM_MASK |
  4843. MPI3_SAS_EXPAND_PGAD_HANDLE_MASK)));
  4844. cfg_req.page_address = cpu_to_le32(page_address);
  4845. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  4846. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, exp_pg0, pg_sz)) {
  4847. ioc_err(mrioc, "expander page0 read failed\n");
  4848. goto out_failed;
  4849. }
  4850. return 0;
  4851. out_failed:
  4852. return -1;
  4853. }
  4854. /**
  4855. * mpi3mr_cfg_get_sas_exp_pg1 - Read current SAS Expander page1
  4856. * @mrioc: Adapter instance reference
  4857. * @ioc_status: Pointer to return ioc status
  4858. * @exp_pg1: Pointer to return SAS Expander page 1
  4859. * @pg_sz: Size of the memory allocated to the page pointer
  4860. * @form: The form to be used for addressing the page
  4861. * @form_spec: Form specific information like phy number
  4862. *
  4863. * This is handler for config page read for a specific SAS
  4864. * Expander page1. The ioc_status has the controller returned
  4865. * ioc_status. This routine doesn't check ioc_status to decide
  4866. * whether the page read is success or not and it is the callers
  4867. * responsibility.
  4868. *
  4869. * Return: 0 on success, non-zero on failure.
  4870. */
  4871. int mpi3mr_cfg_get_sas_exp_pg1(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
  4872. struct mpi3_sas_expander_page1 *exp_pg1, u16 pg_sz, u32 form,
  4873. u32 form_spec)
  4874. {
  4875. struct mpi3_config_page_header cfg_hdr;
  4876. struct mpi3_config_request cfg_req;
  4877. u32 page_address;
  4878. memset(exp_pg1, 0, pg_sz);
  4879. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4880. memset(&cfg_req, 0, sizeof(cfg_req));
  4881. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4882. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4883. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_EXPANDER;
  4884. cfg_req.page_number = 1;
  4885. cfg_req.page_address = 0;
  4886. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4887. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4888. ioc_err(mrioc, "expander page1 header read failed\n");
  4889. goto out_failed;
  4890. }
  4891. if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4892. ioc_err(mrioc, "expander page1 header read failed with ioc_status(0x%04x)\n",
  4893. *ioc_status);
  4894. goto out_failed;
  4895. }
  4896. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  4897. page_address = ((form & MPI3_SAS_EXPAND_PGAD_FORM_MASK) |
  4898. (form_spec & (MPI3_SAS_EXPAND_PGAD_PHYNUM_MASK |
  4899. MPI3_SAS_EXPAND_PGAD_HANDLE_MASK)));
  4900. cfg_req.page_address = cpu_to_le32(page_address);
  4901. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  4902. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, exp_pg1, pg_sz)) {
  4903. ioc_err(mrioc, "expander page1 read failed\n");
  4904. goto out_failed;
  4905. }
  4906. return 0;
  4907. out_failed:
  4908. return -1;
  4909. }
  4910. /**
  4911. * mpi3mr_cfg_get_enclosure_pg0 - Read current Enclosure page0
  4912. * @mrioc: Adapter instance reference
  4913. * @ioc_status: Pointer to return ioc status
  4914. * @encl_pg0: Pointer to return Enclosure page 0
  4915. * @pg_sz: Size of the memory allocated to the page pointer
  4916. * @form: The form to be used for addressing the page
  4917. * @form_spec: Form specific information like device handle
  4918. *
  4919. * This is handler for config page read for a specific Enclosure
  4920. * page0. The ioc_status has the controller returned ioc_status.
  4921. * This routine doesn't check ioc_status to decide whether the
  4922. * page read is success or not and it is the callers
  4923. * responsibility.
  4924. *
  4925. * Return: 0 on success, non-zero on failure.
  4926. */
  4927. int mpi3mr_cfg_get_enclosure_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
  4928. struct mpi3_enclosure_page0 *encl_pg0, u16 pg_sz, u32 form,
  4929. u32 form_spec)
  4930. {
  4931. struct mpi3_config_page_header cfg_hdr;
  4932. struct mpi3_config_request cfg_req;
  4933. u32 page_address;
  4934. memset(encl_pg0, 0, pg_sz);
  4935. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4936. memset(&cfg_req, 0, sizeof(cfg_req));
  4937. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4938. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4939. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_ENCLOSURE;
  4940. cfg_req.page_number = 0;
  4941. cfg_req.page_address = 0;
  4942. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4943. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4944. ioc_err(mrioc, "enclosure page0 header read failed\n");
  4945. goto out_failed;
  4946. }
  4947. if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4948. ioc_err(mrioc, "enclosure page0 header read failed with ioc_status(0x%04x)\n",
  4949. *ioc_status);
  4950. goto out_failed;
  4951. }
  4952. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  4953. page_address = ((form & MPI3_ENCLOS_PGAD_FORM_MASK) |
  4954. (form_spec & MPI3_ENCLOS_PGAD_HANDLE_MASK));
  4955. cfg_req.page_address = cpu_to_le32(page_address);
  4956. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  4957. MPI3MR_INTADMCMD_TIMEOUT, ioc_status, encl_pg0, pg_sz)) {
  4958. ioc_err(mrioc, "enclosure page0 read failed\n");
  4959. goto out_failed;
  4960. }
  4961. return 0;
  4962. out_failed:
  4963. return -1;
  4964. }
  4965. /**
  4966. * mpi3mr_cfg_get_sas_io_unit_pg0 - Read current SASIOUnit page0
  4967. * @mrioc: Adapter instance reference
  4968. * @sas_io_unit_pg0: Pointer to return SAS IO Unit page 0
  4969. * @pg_sz: Size of the memory allocated to the page pointer
  4970. *
  4971. * This is handler for config page read for the SAS IO Unit
  4972. * page0. This routine checks ioc_status to decide whether the
  4973. * page read is success or not.
  4974. *
  4975. * Return: 0 on success, non-zero on failure.
  4976. */
  4977. int mpi3mr_cfg_get_sas_io_unit_pg0(struct mpi3mr_ioc *mrioc,
  4978. struct mpi3_sas_io_unit_page0 *sas_io_unit_pg0, u16 pg_sz)
  4979. {
  4980. struct mpi3_config_page_header cfg_hdr;
  4981. struct mpi3_config_request cfg_req;
  4982. u16 ioc_status = 0;
  4983. memset(sas_io_unit_pg0, 0, pg_sz);
  4984. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  4985. memset(&cfg_req, 0, sizeof(cfg_req));
  4986. cfg_req.function = MPI3_FUNCTION_CONFIG;
  4987. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  4988. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT;
  4989. cfg_req.page_number = 0;
  4990. cfg_req.page_address = 0;
  4991. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  4992. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  4993. ioc_err(mrioc, "sas io unit page0 header read failed\n");
  4994. goto out_failed;
  4995. }
  4996. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  4997. ioc_err(mrioc, "sas io unit page0 header read failed with ioc_status(0x%04x)\n",
  4998. ioc_status);
  4999. goto out_failed;
  5000. }
  5001. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  5002. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  5003. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg0, pg_sz)) {
  5004. ioc_err(mrioc, "sas io unit page0 read failed\n");
  5005. goto out_failed;
  5006. }
  5007. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5008. ioc_err(mrioc, "sas io unit page0 read failed with ioc_status(0x%04x)\n",
  5009. ioc_status);
  5010. goto out_failed;
  5011. }
  5012. return 0;
  5013. out_failed:
  5014. return -1;
  5015. }
  5016. /**
  5017. * mpi3mr_cfg_get_sas_io_unit_pg1 - Read current SASIOUnit page1
  5018. * @mrioc: Adapter instance reference
  5019. * @sas_io_unit_pg1: Pointer to return SAS IO Unit page 1
  5020. * @pg_sz: Size of the memory allocated to the page pointer
  5021. *
  5022. * This is handler for config page read for the SAS IO Unit
  5023. * page1. This routine checks ioc_status to decide whether the
  5024. * page read is success or not.
  5025. *
  5026. * Return: 0 on success, non-zero on failure.
  5027. */
  5028. int mpi3mr_cfg_get_sas_io_unit_pg1(struct mpi3mr_ioc *mrioc,
  5029. struct mpi3_sas_io_unit_page1 *sas_io_unit_pg1, u16 pg_sz)
  5030. {
  5031. struct mpi3_config_page_header cfg_hdr;
  5032. struct mpi3_config_request cfg_req;
  5033. u16 ioc_status = 0;
  5034. memset(sas_io_unit_pg1, 0, pg_sz);
  5035. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  5036. memset(&cfg_req, 0, sizeof(cfg_req));
  5037. cfg_req.function = MPI3_FUNCTION_CONFIG;
  5038. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  5039. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT;
  5040. cfg_req.page_number = 1;
  5041. cfg_req.page_address = 0;
  5042. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  5043. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  5044. ioc_err(mrioc, "sas io unit page1 header read failed\n");
  5045. goto out_failed;
  5046. }
  5047. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5048. ioc_err(mrioc, "sas io unit page1 header read failed with ioc_status(0x%04x)\n",
  5049. ioc_status);
  5050. goto out_failed;
  5051. }
  5052. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  5053. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  5054. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) {
  5055. ioc_err(mrioc, "sas io unit page1 read failed\n");
  5056. goto out_failed;
  5057. }
  5058. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5059. ioc_err(mrioc, "sas io unit page1 read failed with ioc_status(0x%04x)\n",
  5060. ioc_status);
  5061. goto out_failed;
  5062. }
  5063. return 0;
  5064. out_failed:
  5065. return -1;
  5066. }
  5067. /**
  5068. * mpi3mr_cfg_set_sas_io_unit_pg1 - Write SASIOUnit page1
  5069. * @mrioc: Adapter instance reference
  5070. * @sas_io_unit_pg1: Pointer to the SAS IO Unit page 1 to write
  5071. * @pg_sz: Size of the memory allocated to the page pointer
  5072. *
  5073. * This is handler for config page write for the SAS IO Unit
  5074. * page1. This routine checks ioc_status to decide whether the
  5075. * page read is success or not. This will modify both current
  5076. * and persistent page.
  5077. *
  5078. * Return: 0 on success, non-zero on failure.
  5079. */
  5080. int mpi3mr_cfg_set_sas_io_unit_pg1(struct mpi3mr_ioc *mrioc,
  5081. struct mpi3_sas_io_unit_page1 *sas_io_unit_pg1, u16 pg_sz)
  5082. {
  5083. struct mpi3_config_page_header cfg_hdr;
  5084. struct mpi3_config_request cfg_req;
  5085. u16 ioc_status = 0;
  5086. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  5087. memset(&cfg_req, 0, sizeof(cfg_req));
  5088. cfg_req.function = MPI3_FUNCTION_CONFIG;
  5089. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  5090. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT;
  5091. cfg_req.page_number = 1;
  5092. cfg_req.page_address = 0;
  5093. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  5094. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  5095. ioc_err(mrioc, "sas io unit page1 header read failed\n");
  5096. goto out_failed;
  5097. }
  5098. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5099. ioc_err(mrioc, "sas io unit page1 header read failed with ioc_status(0x%04x)\n",
  5100. ioc_status);
  5101. goto out_failed;
  5102. }
  5103. cfg_req.action = MPI3_CONFIG_ACTION_WRITE_CURRENT;
  5104. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  5105. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) {
  5106. ioc_err(mrioc, "sas io unit page1 write current failed\n");
  5107. goto out_failed;
  5108. }
  5109. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5110. ioc_err(mrioc, "sas io unit page1 write current failed with ioc_status(0x%04x)\n",
  5111. ioc_status);
  5112. goto out_failed;
  5113. }
  5114. cfg_req.action = MPI3_CONFIG_ACTION_WRITE_PERSISTENT;
  5115. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  5116. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) {
  5117. ioc_err(mrioc, "sas io unit page1 write persistent failed\n");
  5118. goto out_failed;
  5119. }
  5120. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5121. ioc_err(mrioc, "sas io unit page1 write persistent failed with ioc_status(0x%04x)\n",
  5122. ioc_status);
  5123. goto out_failed;
  5124. }
  5125. return 0;
  5126. out_failed:
  5127. return -1;
  5128. }
  5129. /**
  5130. * mpi3mr_cfg_get_driver_pg1 - Read current Driver page1
  5131. * @mrioc: Adapter instance reference
  5132. * @driver_pg1: Pointer to return Driver page 1
  5133. * @pg_sz: Size of the memory allocated to the page pointer
  5134. *
  5135. * This is handler for config page read for the Driver page1.
  5136. * This routine checks ioc_status to decide whether the page
  5137. * read is success or not.
  5138. *
  5139. * Return: 0 on success, non-zero on failure.
  5140. */
  5141. int mpi3mr_cfg_get_driver_pg1(struct mpi3mr_ioc *mrioc,
  5142. struct mpi3_driver_page1 *driver_pg1, u16 pg_sz)
  5143. {
  5144. struct mpi3_config_page_header cfg_hdr;
  5145. struct mpi3_config_request cfg_req;
  5146. u16 ioc_status = 0;
  5147. memset(driver_pg1, 0, pg_sz);
  5148. memset(&cfg_hdr, 0, sizeof(cfg_hdr));
  5149. memset(&cfg_req, 0, sizeof(cfg_req));
  5150. cfg_req.function = MPI3_FUNCTION_CONFIG;
  5151. cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
  5152. cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DRIVER;
  5153. cfg_req.page_number = 1;
  5154. cfg_req.page_address = 0;
  5155. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
  5156. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
  5157. ioc_err(mrioc, "driver page1 header read failed\n");
  5158. goto out_failed;
  5159. }
  5160. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5161. ioc_err(mrioc, "driver page1 header read failed with ioc_status(0x%04x)\n",
  5162. ioc_status);
  5163. goto out_failed;
  5164. }
  5165. cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
  5166. if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
  5167. MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, driver_pg1, pg_sz)) {
  5168. ioc_err(mrioc, "driver page1 read failed\n");
  5169. goto out_failed;
  5170. }
  5171. if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
  5172. ioc_err(mrioc, "driver page1 read failed with ioc_status(0x%04x)\n",
  5173. ioc_status);
  5174. goto out_failed;
  5175. }
  5176. return 0;
  5177. out_failed:
  5178. return -1;
  5179. }