mpt3sas_base.c 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970
  1. /*
  2. * This is the Fusion MPT base driver providing common API layer interface
  3. * for access to MPT (Message Passing Technology) firmware.
  4. *
  5. * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
  6. * Copyright (C) 2012-2014 LSI Corporation
  7. * Copyright (C) 2013-2014 Avago Technologies
  8. * (mailto: [email protected])
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * NO WARRANTY
  21. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  22. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  23. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  24. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  25. * solely responsible for determining the appropriateness of using and
  26. * distributing the Program and assumes all risks associated with its
  27. * exercise of rights under this Agreement, including but not limited to
  28. * the risks and costs of program errors, damage to or loss of data,
  29. * programs or equipment, and unavailability or interruption of operations.
  30. * DISCLAIMER OF LIABILITY
  31. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/kdev_t.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/delay.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/io.h>
  56. #include <linux/time.h>
  57. #include <linux/ktime.h>
  58. #include <linux/kthread.h>
  59. #include <asm/page.h> /* To get host page size per arch */
  60. #include <linux/aer.h>
  61. #include "mpt3sas_base.h"
  62. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  63. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  64. /* maximum controller queue depth */
  65. #define MAX_HBA_QUEUE_DEPTH 30000
  66. #define MAX_CHAIN_DEPTH 100000
  67. static int max_queue_depth = -1;
  68. module_param(max_queue_depth, int, 0444);
  69. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  70. static int max_sgl_entries = -1;
  71. module_param(max_sgl_entries, int, 0444);
  72. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  73. static int msix_disable = -1;
  74. module_param(msix_disable, int, 0444);
  75. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  76. static int smp_affinity_enable = 1;
  77. module_param(smp_affinity_enable, int, 0444);
  78. MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
  79. static int max_msix_vectors = -1;
  80. module_param(max_msix_vectors, int, 0444);
  81. MODULE_PARM_DESC(max_msix_vectors,
  82. " max msix vectors");
  83. static int irqpoll_weight = -1;
  84. module_param(irqpoll_weight, int, 0444);
  85. MODULE_PARM_DESC(irqpoll_weight,
  86. "irq poll weight (default= one fourth of HBA queue depth)");
  87. static int mpt3sas_fwfault_debug;
  88. MODULE_PARM_DESC(mpt3sas_fwfault_debug,
  89. " enable detection of firmware fault and halt firmware - (default=0)");
  90. static int perf_mode = -1;
  91. module_param(perf_mode, int, 0444);
  92. MODULE_PARM_DESC(perf_mode,
  93. "Performance mode (only for Aero/Sea Generation), options:\n\t\t"
  94. "0 - balanced: high iops mode is enabled &\n\t\t"
  95. "interrupt coalescing is enabled only on high iops queues,\n\t\t"
  96. "1 - iops: high iops mode is disabled &\n\t\t"
  97. "interrupt coalescing is enabled on all queues,\n\t\t"
  98. "2 - latency: high iops mode is disabled &\n\t\t"
  99. "interrupt coalescing is enabled on all queues with timeout value 0xA,\n"
  100. "\t\tdefault - default perf_mode is 'balanced'"
  101. );
  102. static int poll_queues;
  103. module_param(poll_queues, int, 0444);
  104. MODULE_PARM_DESC(poll_queues, "Number of queues to be use for io_uring poll mode.\n\t\t"
  105. "This parameter is effective only if host_tagset_enable=1. &\n\t\t"
  106. "when poll_queues are enabled then &\n\t\t"
  107. "perf_mode is set to latency mode. &\n\t\t"
  108. );
  109. enum mpt3sas_perf_mode {
  110. MPT_PERF_MODE_DEFAULT = -1,
  111. MPT_PERF_MODE_BALANCED = 0,
  112. MPT_PERF_MODE_IOPS = 1,
  113. MPT_PERF_MODE_LATENCY = 2,
  114. };
  115. static int
  116. _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc,
  117. u32 ioc_state, int timeout);
  118. static int
  119. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
  120. static void
  121. _base_clear_outstanding_commands(struct MPT3SAS_ADAPTER *ioc);
  122. static u32
  123. _base_readl_ext_retry(const volatile void __iomem *addr);
  124. /**
  125. * mpt3sas_base_check_cmd_timeout - Function
  126. * to check timeout and command termination due
  127. * to Host reset.
  128. *
  129. * @ioc: per adapter object.
  130. * @status: Status of issued command.
  131. * @mpi_request:mf request pointer.
  132. * @sz: size of buffer.
  133. *
  134. * Return: 1/0 Reset to be done or Not
  135. */
  136. u8
  137. mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER *ioc,
  138. u8 status, void *mpi_request, int sz)
  139. {
  140. u8 issue_reset = 0;
  141. if (!(status & MPT3_CMD_RESET))
  142. issue_reset = 1;
  143. ioc_err(ioc, "Command %s\n",
  144. issue_reset == 0 ? "terminated due to Host Reset" : "Timeout");
  145. _debug_dump_mf(mpi_request, sz);
  146. return issue_reset;
  147. }
  148. /**
  149. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  150. * @val: ?
  151. * @kp: ?
  152. *
  153. * Return: ?
  154. */
  155. static int
  156. _scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
  157. {
  158. int ret = param_set_int(val, kp);
  159. struct MPT3SAS_ADAPTER *ioc;
  160. if (ret)
  161. return ret;
  162. /* global ioc spinlock to protect controller list on list operations */
  163. pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
  164. spin_lock(&gioc_lock);
  165. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  166. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  167. spin_unlock(&gioc_lock);
  168. return 0;
  169. }
  170. module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
  171. param_get_int, &mpt3sas_fwfault_debug, 0644);
  172. /**
  173. * _base_readl_aero - retry readl for max three times.
  174. * @addr: MPT Fusion system interface register address
  175. *
  176. * Retry the readl() for max three times if it gets zero value
  177. * while reading the system interface register.
  178. */
  179. static inline u32
  180. _base_readl_aero(const volatile void __iomem *addr)
  181. {
  182. u32 i = 0, ret_val;
  183. do {
  184. ret_val = readl(addr);
  185. i++;
  186. } while (ret_val == 0 && i < 3);
  187. return ret_val;
  188. }
  189. static u32
  190. _base_readl_ext_retry(const volatile void __iomem *addr)
  191. {
  192. u32 i, ret_val;
  193. for (i = 0 ; i < 30 ; i++) {
  194. ret_val = readl(addr);
  195. if (ret_val != 0)
  196. break;
  197. }
  198. return ret_val;
  199. }
  200. static inline u32
  201. _base_readl(const volatile void __iomem *addr)
  202. {
  203. return readl(addr);
  204. }
  205. /**
  206. * _base_clone_reply_to_sys_mem - copies reply to reply free iomem
  207. * in BAR0 space.
  208. *
  209. * @ioc: per adapter object
  210. * @reply: reply message frame(lower 32bit addr)
  211. * @index: System request message index.
  212. */
  213. static void
  214. _base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER *ioc, u32 reply,
  215. u32 index)
  216. {
  217. /*
  218. * 256 is offset within sys register.
  219. * 256 offset MPI frame starts. Max MPI frame supported is 32.
  220. * 32 * 128 = 4K. From here, Clone of reply free for mcpu starts
  221. */
  222. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  223. void __iomem *reply_free_iomem = (void __iomem *)ioc->chip +
  224. MPI_FRAME_START_OFFSET +
  225. (cmd_credit * ioc->request_sz) + (index * sizeof(u32));
  226. writel(reply, reply_free_iomem);
  227. }
  228. /**
  229. * _base_clone_mpi_to_sys_mem - Writes/copies MPI frames
  230. * to system/BAR0 region.
  231. *
  232. * @dst_iomem: Pointer to the destination location in BAR0 space.
  233. * @src: Pointer to the Source data.
  234. * @size: Size of data to be copied.
  235. */
  236. static void
  237. _base_clone_mpi_to_sys_mem(void *dst_iomem, void *src, u32 size)
  238. {
  239. int i;
  240. u32 *src_virt_mem = (u32 *)src;
  241. for (i = 0; i < size/4; i++)
  242. writel((u32)src_virt_mem[i],
  243. (void __iomem *)dst_iomem + (i * 4));
  244. }
  245. /**
  246. * _base_clone_to_sys_mem - Writes/copies data to system/BAR0 region
  247. *
  248. * @dst_iomem: Pointer to the destination location in BAR0 space.
  249. * @src: Pointer to the Source data.
  250. * @size: Size of data to be copied.
  251. */
  252. static void
  253. _base_clone_to_sys_mem(void __iomem *dst_iomem, void *src, u32 size)
  254. {
  255. int i;
  256. u32 *src_virt_mem = (u32 *)(src);
  257. for (i = 0; i < size/4; i++)
  258. writel((u32)src_virt_mem[i],
  259. (void __iomem *)dst_iomem + (i * 4));
  260. }
  261. /**
  262. * _base_get_chain - Calculates and Returns virtual chain address
  263. * for the provided smid in BAR0 space.
  264. *
  265. * @ioc: per adapter object
  266. * @smid: system request message index
  267. * @sge_chain_count: Scatter gather chain count.
  268. *
  269. * Return: the chain address.
  270. */
  271. static inline void __iomem*
  272. _base_get_chain(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  273. u8 sge_chain_count)
  274. {
  275. void __iomem *base_chain, *chain_virt;
  276. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  277. base_chain = (void __iomem *)ioc->chip + MPI_FRAME_START_OFFSET +
  278. (cmd_credit * ioc->request_sz) +
  279. REPLY_FREE_POOL_SIZE;
  280. chain_virt = base_chain + (smid * ioc->facts.MaxChainDepth *
  281. ioc->request_sz) + (sge_chain_count * ioc->request_sz);
  282. return chain_virt;
  283. }
  284. /**
  285. * _base_get_chain_phys - Calculates and Returns physical address
  286. * in BAR0 for scatter gather chains, for
  287. * the provided smid.
  288. *
  289. * @ioc: per adapter object
  290. * @smid: system request message index
  291. * @sge_chain_count: Scatter gather chain count.
  292. *
  293. * Return: Physical chain address.
  294. */
  295. static inline phys_addr_t
  296. _base_get_chain_phys(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  297. u8 sge_chain_count)
  298. {
  299. phys_addr_t base_chain_phys, chain_phys;
  300. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  301. base_chain_phys = ioc->chip_phys + MPI_FRAME_START_OFFSET +
  302. (cmd_credit * ioc->request_sz) +
  303. REPLY_FREE_POOL_SIZE;
  304. chain_phys = base_chain_phys + (smid * ioc->facts.MaxChainDepth *
  305. ioc->request_sz) + (sge_chain_count * ioc->request_sz);
  306. return chain_phys;
  307. }
  308. /**
  309. * _base_get_buffer_bar0 - Calculates and Returns BAR0 mapped Host
  310. * buffer address for the provided smid.
  311. * (Each smid can have 64K starts from 17024)
  312. *
  313. * @ioc: per adapter object
  314. * @smid: system request message index
  315. *
  316. * Return: Pointer to buffer location in BAR0.
  317. */
  318. static void __iomem *
  319. _base_get_buffer_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  320. {
  321. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  322. // Added extra 1 to reach end of chain.
  323. void __iomem *chain_end = _base_get_chain(ioc,
  324. cmd_credit + 1,
  325. ioc->facts.MaxChainDepth);
  326. return chain_end + (smid * 64 * 1024);
  327. }
  328. /**
  329. * _base_get_buffer_phys_bar0 - Calculates and Returns BAR0 mapped
  330. * Host buffer Physical address for the provided smid.
  331. * (Each smid can have 64K starts from 17024)
  332. *
  333. * @ioc: per adapter object
  334. * @smid: system request message index
  335. *
  336. * Return: Pointer to buffer location in BAR0.
  337. */
  338. static phys_addr_t
  339. _base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  340. {
  341. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  342. phys_addr_t chain_end_phys = _base_get_chain_phys(ioc,
  343. cmd_credit + 1,
  344. ioc->facts.MaxChainDepth);
  345. return chain_end_phys + (smid * 64 * 1024);
  346. }
  347. /**
  348. * _base_get_chain_buffer_dma_to_chain_buffer - Iterates chain
  349. * lookup list and Provides chain_buffer
  350. * address for the matching dma address.
  351. * (Each smid can have 64K starts from 17024)
  352. *
  353. * @ioc: per adapter object
  354. * @chain_buffer_dma: Chain buffer dma address.
  355. *
  356. * Return: Pointer to chain buffer. Or Null on Failure.
  357. */
  358. static void *
  359. _base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER *ioc,
  360. dma_addr_t chain_buffer_dma)
  361. {
  362. u16 index, j;
  363. struct chain_tracker *ct;
  364. for (index = 0; index < ioc->scsiio_depth; index++) {
  365. for (j = 0; j < ioc->chains_needed_per_io; j++) {
  366. ct = &ioc->chain_lookup[index].chains_per_smid[j];
  367. if (ct && ct->chain_buffer_dma == chain_buffer_dma)
  368. return ct->chain_buffer;
  369. }
  370. }
  371. ioc_info(ioc, "Provided chain_buffer_dma address is not in the lookup list\n");
  372. return NULL;
  373. }
  374. /**
  375. * _clone_sg_entries - MPI EP's scsiio and config requests
  376. * are handled here. Base function for
  377. * double buffering, before submitting
  378. * the requests.
  379. *
  380. * @ioc: per adapter object.
  381. * @mpi_request: mf request pointer.
  382. * @smid: system request message index.
  383. */
  384. static void _clone_sg_entries(struct MPT3SAS_ADAPTER *ioc,
  385. void *mpi_request, u16 smid)
  386. {
  387. Mpi2SGESimple32_t *sgel, *sgel_next;
  388. u32 sgl_flags, sge_chain_count = 0;
  389. bool is_write = false;
  390. u16 i = 0;
  391. void __iomem *buffer_iomem;
  392. phys_addr_t buffer_iomem_phys;
  393. void __iomem *buff_ptr;
  394. phys_addr_t buff_ptr_phys;
  395. void __iomem *dst_chain_addr[MCPU_MAX_CHAINS_PER_IO];
  396. void *src_chain_addr[MCPU_MAX_CHAINS_PER_IO];
  397. phys_addr_t dst_addr_phys;
  398. MPI2RequestHeader_t *request_hdr;
  399. struct scsi_cmnd *scmd;
  400. struct scatterlist *sg_scmd = NULL;
  401. int is_scsiio_req = 0;
  402. request_hdr = (MPI2RequestHeader_t *) mpi_request;
  403. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
  404. Mpi25SCSIIORequest_t *scsiio_request =
  405. (Mpi25SCSIIORequest_t *)mpi_request;
  406. sgel = (Mpi2SGESimple32_t *) &scsiio_request->SGL;
  407. is_scsiio_req = 1;
  408. } else if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
  409. Mpi2ConfigRequest_t *config_req =
  410. (Mpi2ConfigRequest_t *)mpi_request;
  411. sgel = (Mpi2SGESimple32_t *) &config_req->PageBufferSGE;
  412. } else
  413. return;
  414. /* From smid we can get scsi_cmd, once we have sg_scmd,
  415. * we just need to get sg_virt and sg_next to get virtual
  416. * address associated with sgel->Address.
  417. */
  418. if (is_scsiio_req) {
  419. /* Get scsi_cmd using smid */
  420. scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  421. if (scmd == NULL) {
  422. ioc_err(ioc, "scmd is NULL\n");
  423. return;
  424. }
  425. /* Get sg_scmd from scmd provided */
  426. sg_scmd = scsi_sglist(scmd);
  427. }
  428. /*
  429. * 0 - 255 System register
  430. * 256 - 4352 MPI Frame. (This is based on maxCredit 32)
  431. * 4352 - 4864 Reply_free pool (512 byte is reserved
  432. * considering maxCredit 32. Reply need extra
  433. * room, for mCPU case kept four times of
  434. * maxCredit).
  435. * 4864 - 17152 SGE chain element. (32cmd * 3 chain of
  436. * 128 byte size = 12288)
  437. * 17152 - x Host buffer mapped with smid.
  438. * (Each smid can have 64K Max IO.)
  439. * BAR0+Last 1K MSIX Addr and Data
  440. * Total size in use 2113664 bytes of 4MB BAR0
  441. */
  442. buffer_iomem = _base_get_buffer_bar0(ioc, smid);
  443. buffer_iomem_phys = _base_get_buffer_phys_bar0(ioc, smid);
  444. buff_ptr = buffer_iomem;
  445. buff_ptr_phys = buffer_iomem_phys;
  446. WARN_ON(buff_ptr_phys > U32_MAX);
  447. if (le32_to_cpu(sgel->FlagsLength) &
  448. (MPI2_SGE_FLAGS_HOST_TO_IOC << MPI2_SGE_FLAGS_SHIFT))
  449. is_write = true;
  450. for (i = 0; i < MPT_MIN_PHYS_SEGMENTS + ioc->facts.MaxChainDepth; i++) {
  451. sgl_flags =
  452. (le32_to_cpu(sgel->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT);
  453. switch (sgl_flags & MPI2_SGE_FLAGS_ELEMENT_MASK) {
  454. case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
  455. /*
  456. * Helper function which on passing
  457. * chain_buffer_dma returns chain_buffer. Get
  458. * the virtual address for sgel->Address
  459. */
  460. sgel_next =
  461. _base_get_chain_buffer_dma_to_chain_buffer(ioc,
  462. le32_to_cpu(sgel->Address));
  463. if (sgel_next == NULL)
  464. return;
  465. /*
  466. * This is coping 128 byte chain
  467. * frame (not a host buffer)
  468. */
  469. dst_chain_addr[sge_chain_count] =
  470. _base_get_chain(ioc,
  471. smid, sge_chain_count);
  472. src_chain_addr[sge_chain_count] =
  473. (void *) sgel_next;
  474. dst_addr_phys = _base_get_chain_phys(ioc,
  475. smid, sge_chain_count);
  476. WARN_ON(dst_addr_phys > U32_MAX);
  477. sgel->Address =
  478. cpu_to_le32(lower_32_bits(dst_addr_phys));
  479. sgel = sgel_next;
  480. sge_chain_count++;
  481. break;
  482. case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
  483. if (is_write) {
  484. if (is_scsiio_req) {
  485. _base_clone_to_sys_mem(buff_ptr,
  486. sg_virt(sg_scmd),
  487. (le32_to_cpu(sgel->FlagsLength) &
  488. 0x00ffffff));
  489. /*
  490. * FIXME: this relies on a a zero
  491. * PCI mem_offset.
  492. */
  493. sgel->Address =
  494. cpu_to_le32((u32)buff_ptr_phys);
  495. } else {
  496. _base_clone_to_sys_mem(buff_ptr,
  497. ioc->config_vaddr,
  498. (le32_to_cpu(sgel->FlagsLength) &
  499. 0x00ffffff));
  500. sgel->Address =
  501. cpu_to_le32((u32)buff_ptr_phys);
  502. }
  503. }
  504. buff_ptr += (le32_to_cpu(sgel->FlagsLength) &
  505. 0x00ffffff);
  506. buff_ptr_phys += (le32_to_cpu(sgel->FlagsLength) &
  507. 0x00ffffff);
  508. if ((le32_to_cpu(sgel->FlagsLength) &
  509. (MPI2_SGE_FLAGS_END_OF_BUFFER
  510. << MPI2_SGE_FLAGS_SHIFT)))
  511. goto eob_clone_chain;
  512. else {
  513. /*
  514. * Every single element in MPT will have
  515. * associated sg_next. Better to sanity that
  516. * sg_next is not NULL, but it will be a bug
  517. * if it is null.
  518. */
  519. if (is_scsiio_req) {
  520. sg_scmd = sg_next(sg_scmd);
  521. if (sg_scmd)
  522. sgel++;
  523. else
  524. goto eob_clone_chain;
  525. }
  526. }
  527. break;
  528. }
  529. }
  530. eob_clone_chain:
  531. for (i = 0; i < sge_chain_count; i++) {
  532. if (is_scsiio_req)
  533. _base_clone_to_sys_mem(dst_chain_addr[i],
  534. src_chain_addr[i], ioc->request_sz);
  535. }
  536. }
  537. /**
  538. * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
  539. * @arg: input argument, used to derive ioc
  540. *
  541. * Return:
  542. * 0 if controller is removed from pci subsystem.
  543. * -1 for other case.
  544. */
  545. static int mpt3sas_remove_dead_ioc_func(void *arg)
  546. {
  547. struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
  548. struct pci_dev *pdev;
  549. if (!ioc)
  550. return -1;
  551. pdev = ioc->pdev;
  552. if (!pdev)
  553. return -1;
  554. pci_stop_and_remove_bus_device_locked(pdev);
  555. return 0;
  556. }
  557. /**
  558. * _base_sync_drv_fw_timestamp - Sync Drive-Fw TimeStamp.
  559. * @ioc: Per Adapter Object
  560. *
  561. * Return: nothing.
  562. */
  563. static void _base_sync_drv_fw_timestamp(struct MPT3SAS_ADAPTER *ioc)
  564. {
  565. Mpi26IoUnitControlRequest_t *mpi_request;
  566. Mpi26IoUnitControlReply_t *mpi_reply;
  567. u16 smid;
  568. ktime_t current_time;
  569. u64 TimeStamp = 0;
  570. u8 issue_reset = 0;
  571. mutex_lock(&ioc->scsih_cmds.mutex);
  572. if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
  573. ioc_err(ioc, "scsih_cmd in use %s\n", __func__);
  574. goto out;
  575. }
  576. ioc->scsih_cmds.status = MPT3_CMD_PENDING;
  577. smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  578. if (!smid) {
  579. ioc_err(ioc, "Failed obtaining a smid %s\n", __func__);
  580. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  581. goto out;
  582. }
  583. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  584. ioc->scsih_cmds.smid = smid;
  585. memset(mpi_request, 0, sizeof(Mpi26IoUnitControlRequest_t));
  586. mpi_request->Function = MPI2_FUNCTION_IO_UNIT_CONTROL;
  587. mpi_request->Operation = MPI26_CTRL_OP_SET_IOC_PARAMETER;
  588. mpi_request->IOCParameter = MPI26_SET_IOC_PARAMETER_SYNC_TIMESTAMP;
  589. current_time = ktime_get_real();
  590. TimeStamp = ktime_to_ms(current_time);
  591. mpi_request->Reserved7 = cpu_to_le32(TimeStamp >> 32);
  592. mpi_request->IOCParameterValue = cpu_to_le32(TimeStamp & 0xFFFFFFFF);
  593. init_completion(&ioc->scsih_cmds.done);
  594. ioc->put_smid_default(ioc, smid);
  595. dinitprintk(ioc, ioc_info(ioc,
  596. "Io Unit Control Sync TimeStamp (sending), @time %lld ms\n",
  597. TimeStamp));
  598. wait_for_completion_timeout(&ioc->scsih_cmds.done,
  599. MPT3SAS_TIMESYNC_TIMEOUT_SECONDS*HZ);
  600. if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
  601. mpt3sas_check_cmd_timeout(ioc,
  602. ioc->scsih_cmds.status, mpi_request,
  603. sizeof(Mpi2SasIoUnitControlRequest_t)/4, issue_reset);
  604. goto issue_host_reset;
  605. }
  606. if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
  607. mpi_reply = ioc->scsih_cmds.reply;
  608. dinitprintk(ioc, ioc_info(ioc,
  609. "Io Unit Control sync timestamp (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
  610. le16_to_cpu(mpi_reply->IOCStatus),
  611. le32_to_cpu(mpi_reply->IOCLogInfo)));
  612. }
  613. issue_host_reset:
  614. if (issue_reset)
  615. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  616. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  617. out:
  618. mutex_unlock(&ioc->scsih_cmds.mutex);
  619. }
  620. /**
  621. * _base_fault_reset_work - workq handling ioc fault conditions
  622. * @work: input argument, used to derive ioc
  623. *
  624. * Context: sleep.
  625. */
  626. static void
  627. _base_fault_reset_work(struct work_struct *work)
  628. {
  629. struct MPT3SAS_ADAPTER *ioc =
  630. container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
  631. unsigned long flags;
  632. u32 doorbell;
  633. int rc;
  634. struct task_struct *p;
  635. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  636. if ((ioc->shost_recovery && (ioc->ioc_coredump_loop == 0)) ||
  637. ioc->pci_error_recovery)
  638. goto rearm_timer;
  639. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  640. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  641. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  642. ioc_err(ioc, "SAS host is non-operational !!!!\n");
  643. /* It may be possible that EEH recovery can resolve some of
  644. * pci bus failure issues rather removing the dead ioc function
  645. * by considering controller is in a non-operational state. So
  646. * here priority is given to the EEH recovery. If it doesn't
  647. * not resolve this issue, mpt3sas driver will consider this
  648. * controller to non-operational state and remove the dead ioc
  649. * function.
  650. */
  651. if (ioc->non_operational_loop++ < 5) {
  652. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
  653. flags);
  654. goto rearm_timer;
  655. }
  656. /*
  657. * Call _scsih_flush_pending_cmds callback so that we flush all
  658. * pending commands back to OS. This call is required to avoid
  659. * deadlock at block layer. Dead IOC will fail to do diag reset,
  660. * and this call is safe since dead ioc will never return any
  661. * command back from HW.
  662. */
  663. mpt3sas_base_pause_mq_polling(ioc);
  664. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  665. /*
  666. * Set remove_host flag early since kernel thread will
  667. * take some time to execute.
  668. */
  669. ioc->remove_host = 1;
  670. /*Remove the Dead Host */
  671. p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
  672. "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
  673. if (IS_ERR(p))
  674. ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
  675. __func__);
  676. else
  677. ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
  678. __func__);
  679. return; /* don't rearm timer */
  680. }
  681. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_COREDUMP) {
  682. u8 timeout = (ioc->manu_pg11.CoreDumpTOSec) ?
  683. ioc->manu_pg11.CoreDumpTOSec :
  684. MPT3SAS_DEFAULT_COREDUMP_TIMEOUT_SECONDS;
  685. timeout /= (FAULT_POLLING_INTERVAL/1000);
  686. if (ioc->ioc_coredump_loop == 0) {
  687. mpt3sas_print_coredump_info(ioc,
  688. doorbell & MPI2_DOORBELL_DATA_MASK);
  689. /* do not accept any IOs and disable the interrupts */
  690. spin_lock_irqsave(
  691. &ioc->ioc_reset_in_progress_lock, flags);
  692. ioc->shost_recovery = 1;
  693. spin_unlock_irqrestore(
  694. &ioc->ioc_reset_in_progress_lock, flags);
  695. mpt3sas_base_mask_interrupts(ioc);
  696. mpt3sas_base_pause_mq_polling(ioc);
  697. _base_clear_outstanding_commands(ioc);
  698. }
  699. ioc_info(ioc, "%s: CoreDump loop %d.",
  700. __func__, ioc->ioc_coredump_loop);
  701. /* Wait until CoreDump completes or times out */
  702. if (ioc->ioc_coredump_loop++ < timeout) {
  703. spin_lock_irqsave(
  704. &ioc->ioc_reset_in_progress_lock, flags);
  705. goto rearm_timer;
  706. }
  707. }
  708. if (ioc->ioc_coredump_loop) {
  709. if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_COREDUMP)
  710. ioc_err(ioc, "%s: CoreDump completed. LoopCount: %d",
  711. __func__, ioc->ioc_coredump_loop);
  712. else
  713. ioc_err(ioc, "%s: CoreDump Timed out. LoopCount: %d",
  714. __func__, ioc->ioc_coredump_loop);
  715. ioc->ioc_coredump_loop = MPT3SAS_COREDUMP_LOOP_DONE;
  716. }
  717. ioc->non_operational_loop = 0;
  718. if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
  719. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  720. ioc_warn(ioc, "%s: hard reset: %s\n",
  721. __func__, rc == 0 ? "success" : "failed");
  722. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  723. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  724. mpt3sas_print_fault_code(ioc, doorbell &
  725. MPI2_DOORBELL_DATA_MASK);
  726. } else if ((doorbell & MPI2_IOC_STATE_MASK) ==
  727. MPI2_IOC_STATE_COREDUMP)
  728. mpt3sas_print_coredump_info(ioc, doorbell &
  729. MPI2_DOORBELL_DATA_MASK);
  730. if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
  731. MPI2_IOC_STATE_OPERATIONAL)
  732. return; /* don't rearm timer */
  733. }
  734. ioc->ioc_coredump_loop = 0;
  735. if (ioc->time_sync_interval &&
  736. ++ioc->timestamp_update_count >= ioc->time_sync_interval) {
  737. ioc->timestamp_update_count = 0;
  738. _base_sync_drv_fw_timestamp(ioc);
  739. }
  740. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  741. rearm_timer:
  742. if (ioc->fault_reset_work_q)
  743. queue_delayed_work(ioc->fault_reset_work_q,
  744. &ioc->fault_reset_work,
  745. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  746. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  747. }
  748. /**
  749. * mpt3sas_base_start_watchdog - start the fault_reset_work_q
  750. * @ioc: per adapter object
  751. *
  752. * Context: sleep.
  753. */
  754. void
  755. mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
  756. {
  757. unsigned long flags;
  758. if (ioc->fault_reset_work_q)
  759. return;
  760. ioc->timestamp_update_count = 0;
  761. /* initialize fault polling */
  762. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  763. snprintf(ioc->fault_reset_work_q_name,
  764. sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
  765. ioc->driver_name, ioc->id);
  766. ioc->fault_reset_work_q =
  767. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  768. if (!ioc->fault_reset_work_q) {
  769. ioc_err(ioc, "%s: failed (line=%d)\n", __func__, __LINE__);
  770. return;
  771. }
  772. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  773. if (ioc->fault_reset_work_q)
  774. queue_delayed_work(ioc->fault_reset_work_q,
  775. &ioc->fault_reset_work,
  776. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  777. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  778. }
  779. /**
  780. * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
  781. * @ioc: per adapter object
  782. *
  783. * Context: sleep.
  784. */
  785. void
  786. mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
  787. {
  788. unsigned long flags;
  789. struct workqueue_struct *wq;
  790. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  791. wq = ioc->fault_reset_work_q;
  792. ioc->fault_reset_work_q = NULL;
  793. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  794. if (wq) {
  795. if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
  796. flush_workqueue(wq);
  797. destroy_workqueue(wq);
  798. }
  799. }
  800. /**
  801. * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
  802. * @ioc: per adapter object
  803. * @fault_code: fault code
  804. */
  805. void
  806. mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc, u16 fault_code)
  807. {
  808. ioc_err(ioc, "fault_state(0x%04x)!\n", fault_code);
  809. }
  810. /**
  811. * mpt3sas_base_coredump_info - verbose translation of firmware CoreDump state
  812. * @ioc: per adapter object
  813. * @fault_code: fault code
  814. *
  815. * Return: nothing.
  816. */
  817. void
  818. mpt3sas_base_coredump_info(struct MPT3SAS_ADAPTER *ioc, u16 fault_code)
  819. {
  820. ioc_err(ioc, "coredump_state(0x%04x)!\n", fault_code);
  821. }
  822. /**
  823. * mpt3sas_base_wait_for_coredump_completion - Wait until coredump
  824. * completes or times out
  825. * @ioc: per adapter object
  826. * @caller: caller function name
  827. *
  828. * Return: 0 for success, non-zero for failure.
  829. */
  830. int
  831. mpt3sas_base_wait_for_coredump_completion(struct MPT3SAS_ADAPTER *ioc,
  832. const char *caller)
  833. {
  834. u8 timeout = (ioc->manu_pg11.CoreDumpTOSec) ?
  835. ioc->manu_pg11.CoreDumpTOSec :
  836. MPT3SAS_DEFAULT_COREDUMP_TIMEOUT_SECONDS;
  837. int ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_FAULT,
  838. timeout);
  839. if (ioc_state)
  840. ioc_err(ioc,
  841. "%s: CoreDump timed out. (ioc_state=0x%x)\n",
  842. caller, ioc_state);
  843. else
  844. ioc_info(ioc,
  845. "%s: CoreDump completed. (ioc_state=0x%x)\n",
  846. caller, ioc_state);
  847. return ioc_state;
  848. }
  849. /**
  850. * mpt3sas_halt_firmware - halt's mpt controller firmware
  851. * @ioc: per adapter object
  852. *
  853. * For debugging timeout related issues. Writing 0xCOFFEE00
  854. * to the doorbell register will halt controller firmware. With
  855. * the purpose to stop both driver and firmware, the enduser can
  856. * obtain a ring buffer from controller UART.
  857. */
  858. void
  859. mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
  860. {
  861. u32 doorbell;
  862. if (!ioc->fwfault_debug)
  863. return;
  864. dump_stack();
  865. doorbell = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
  866. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  867. mpt3sas_print_fault_code(ioc, doorbell &
  868. MPI2_DOORBELL_DATA_MASK);
  869. } else if ((doorbell & MPI2_IOC_STATE_MASK) ==
  870. MPI2_IOC_STATE_COREDUMP) {
  871. mpt3sas_print_coredump_info(ioc, doorbell &
  872. MPI2_DOORBELL_DATA_MASK);
  873. } else {
  874. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  875. ioc_err(ioc, "Firmware is halted due to command timeout\n");
  876. }
  877. if (ioc->fwfault_debug == 2)
  878. for (;;)
  879. ;
  880. else
  881. panic("panic in %s\n", __func__);
  882. }
  883. /**
  884. * _base_sas_ioc_info - verbose translation of the ioc status
  885. * @ioc: per adapter object
  886. * @mpi_reply: reply mf payload returned from firmware
  887. * @request_hdr: request mf
  888. */
  889. static void
  890. _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  891. MPI2RequestHeader_t *request_hdr)
  892. {
  893. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  894. MPI2_IOCSTATUS_MASK;
  895. char *desc = NULL;
  896. u16 frame_sz;
  897. char *func_str = NULL;
  898. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  899. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  900. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  901. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  902. return;
  903. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  904. return;
  905. /*
  906. * Older Firmware version doesn't support driver trigger pages.
  907. * So, skip displaying 'config invalid type' type
  908. * of error message.
  909. */
  910. if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
  911. Mpi2ConfigRequest_t *rqst = (Mpi2ConfigRequest_t *)request_hdr;
  912. if ((rqst->ExtPageType ==
  913. MPI2_CONFIG_EXTPAGETYPE_DRIVER_PERSISTENT_TRIGGER) &&
  914. !(ioc->logging_level & MPT_DEBUG_CONFIG)) {
  915. return;
  916. }
  917. }
  918. switch (ioc_status) {
  919. /****************************************************************************
  920. * Common IOCStatus values for all replies
  921. ****************************************************************************/
  922. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  923. desc = "invalid function";
  924. break;
  925. case MPI2_IOCSTATUS_BUSY:
  926. desc = "busy";
  927. break;
  928. case MPI2_IOCSTATUS_INVALID_SGL:
  929. desc = "invalid sgl";
  930. break;
  931. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  932. desc = "internal error";
  933. break;
  934. case MPI2_IOCSTATUS_INVALID_VPID:
  935. desc = "invalid vpid";
  936. break;
  937. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  938. desc = "insufficient resources";
  939. break;
  940. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  941. desc = "insufficient power";
  942. break;
  943. case MPI2_IOCSTATUS_INVALID_FIELD:
  944. desc = "invalid field";
  945. break;
  946. case MPI2_IOCSTATUS_INVALID_STATE:
  947. desc = "invalid state";
  948. break;
  949. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  950. desc = "op state not supported";
  951. break;
  952. /****************************************************************************
  953. * Config IOCStatus values
  954. ****************************************************************************/
  955. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  956. desc = "config invalid action";
  957. break;
  958. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  959. desc = "config invalid type";
  960. break;
  961. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  962. desc = "config invalid page";
  963. break;
  964. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  965. desc = "config invalid data";
  966. break;
  967. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  968. desc = "config no defaults";
  969. break;
  970. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  971. desc = "config can't commit";
  972. break;
  973. /****************************************************************************
  974. * SCSI IO Reply
  975. ****************************************************************************/
  976. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  977. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  978. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  979. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  980. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  981. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  982. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  983. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  984. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  985. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  986. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  987. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  988. break;
  989. /****************************************************************************
  990. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  991. ****************************************************************************/
  992. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  993. desc = "eedp guard error";
  994. break;
  995. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  996. desc = "eedp ref tag error";
  997. break;
  998. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  999. desc = "eedp app tag error";
  1000. break;
  1001. /****************************************************************************
  1002. * SCSI Target values
  1003. ****************************************************************************/
  1004. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  1005. desc = "target invalid io index";
  1006. break;
  1007. case MPI2_IOCSTATUS_TARGET_ABORTED:
  1008. desc = "target aborted";
  1009. break;
  1010. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  1011. desc = "target no conn retryable";
  1012. break;
  1013. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  1014. desc = "target no connection";
  1015. break;
  1016. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  1017. desc = "target xfer count mismatch";
  1018. break;
  1019. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  1020. desc = "target data offset error";
  1021. break;
  1022. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  1023. desc = "target too much write data";
  1024. break;
  1025. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  1026. desc = "target iu too short";
  1027. break;
  1028. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  1029. desc = "target ack nak timeout";
  1030. break;
  1031. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  1032. desc = "target nak received";
  1033. break;
  1034. /****************************************************************************
  1035. * Serial Attached SCSI values
  1036. ****************************************************************************/
  1037. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  1038. desc = "smp request failed";
  1039. break;
  1040. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  1041. desc = "smp data overrun";
  1042. break;
  1043. /****************************************************************************
  1044. * Diagnostic Buffer Post / Diagnostic Release values
  1045. ****************************************************************************/
  1046. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  1047. desc = "diagnostic released";
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. if (!desc)
  1053. return;
  1054. switch (request_hdr->Function) {
  1055. case MPI2_FUNCTION_CONFIG:
  1056. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  1057. func_str = "config_page";
  1058. break;
  1059. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  1060. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  1061. func_str = "task_mgmt";
  1062. break;
  1063. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  1064. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  1065. func_str = "sas_iounit_ctl";
  1066. break;
  1067. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  1068. frame_sz = sizeof(Mpi2SepRequest_t);
  1069. func_str = "enclosure";
  1070. break;
  1071. case MPI2_FUNCTION_IOC_INIT:
  1072. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  1073. func_str = "ioc_init";
  1074. break;
  1075. case MPI2_FUNCTION_PORT_ENABLE:
  1076. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  1077. func_str = "port_enable";
  1078. break;
  1079. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  1080. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  1081. func_str = "smp_passthru";
  1082. break;
  1083. case MPI2_FUNCTION_NVME_ENCAPSULATED:
  1084. frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
  1085. ioc->sge_size;
  1086. func_str = "nvme_encapsulated";
  1087. break;
  1088. default:
  1089. frame_sz = 32;
  1090. func_str = "unknown";
  1091. break;
  1092. }
  1093. ioc_warn(ioc, "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
  1094. desc, ioc_status, request_hdr, func_str);
  1095. _debug_dump_mf(request_hdr, frame_sz/4);
  1096. }
  1097. /**
  1098. * _base_display_event_data - verbose translation of firmware asyn events
  1099. * @ioc: per adapter object
  1100. * @mpi_reply: reply mf payload returned from firmware
  1101. */
  1102. static void
  1103. _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
  1104. Mpi2EventNotificationReply_t *mpi_reply)
  1105. {
  1106. char *desc = NULL;
  1107. u16 event;
  1108. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  1109. return;
  1110. event = le16_to_cpu(mpi_reply->Event);
  1111. switch (event) {
  1112. case MPI2_EVENT_LOG_DATA:
  1113. desc = "Log Data";
  1114. break;
  1115. case MPI2_EVENT_STATE_CHANGE:
  1116. desc = "Status Change";
  1117. break;
  1118. case MPI2_EVENT_HARD_RESET_RECEIVED:
  1119. desc = "Hard Reset Received";
  1120. break;
  1121. case MPI2_EVENT_EVENT_CHANGE:
  1122. desc = "Event Change";
  1123. break;
  1124. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1125. desc = "Device Status Change";
  1126. break;
  1127. case MPI2_EVENT_IR_OPERATION_STATUS:
  1128. if (!ioc->hide_ir_msg)
  1129. desc = "IR Operation Status";
  1130. break;
  1131. case MPI2_EVENT_SAS_DISCOVERY:
  1132. {
  1133. Mpi2EventDataSasDiscovery_t *event_data =
  1134. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  1135. ioc_info(ioc, "Discovery: (%s)",
  1136. event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED ?
  1137. "start" : "stop");
  1138. if (event_data->DiscoveryStatus)
  1139. pr_cont(" discovery_status(0x%08x)",
  1140. le32_to_cpu(event_data->DiscoveryStatus));
  1141. pr_cont("\n");
  1142. return;
  1143. }
  1144. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  1145. desc = "SAS Broadcast Primitive";
  1146. break;
  1147. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  1148. desc = "SAS Init Device Status Change";
  1149. break;
  1150. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  1151. desc = "SAS Init Table Overflow";
  1152. break;
  1153. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  1154. desc = "SAS Topology Change List";
  1155. break;
  1156. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  1157. desc = "SAS Enclosure Device Status Change";
  1158. break;
  1159. case MPI2_EVENT_IR_VOLUME:
  1160. if (!ioc->hide_ir_msg)
  1161. desc = "IR Volume";
  1162. break;
  1163. case MPI2_EVENT_IR_PHYSICAL_DISK:
  1164. if (!ioc->hide_ir_msg)
  1165. desc = "IR Physical Disk";
  1166. break;
  1167. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  1168. if (!ioc->hide_ir_msg)
  1169. desc = "IR Configuration Change List";
  1170. break;
  1171. case MPI2_EVENT_LOG_ENTRY_ADDED:
  1172. if (!ioc->hide_ir_msg)
  1173. desc = "Log Entry Added";
  1174. break;
  1175. case MPI2_EVENT_TEMP_THRESHOLD:
  1176. desc = "Temperature Threshold";
  1177. break;
  1178. case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
  1179. desc = "Cable Event";
  1180. break;
  1181. case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
  1182. desc = "SAS Device Discovery Error";
  1183. break;
  1184. case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
  1185. desc = "PCIE Device Status Change";
  1186. break;
  1187. case MPI2_EVENT_PCIE_ENUMERATION:
  1188. {
  1189. Mpi26EventDataPCIeEnumeration_t *event_data =
  1190. (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
  1191. ioc_info(ioc, "PCIE Enumeration: (%s)",
  1192. event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED ?
  1193. "start" : "stop");
  1194. if (event_data->EnumerationStatus)
  1195. pr_cont("enumeration_status(0x%08x)",
  1196. le32_to_cpu(event_data->EnumerationStatus));
  1197. pr_cont("\n");
  1198. return;
  1199. }
  1200. case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
  1201. desc = "PCIE Topology Change List";
  1202. break;
  1203. }
  1204. if (!desc)
  1205. return;
  1206. ioc_info(ioc, "%s\n", desc);
  1207. }
  1208. /**
  1209. * _base_sas_log_info - verbose translation of firmware log info
  1210. * @ioc: per adapter object
  1211. * @log_info: log info
  1212. */
  1213. static void
  1214. _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc, u32 log_info)
  1215. {
  1216. union loginfo_type {
  1217. u32 loginfo;
  1218. struct {
  1219. u32 subcode:16;
  1220. u32 code:8;
  1221. u32 originator:4;
  1222. u32 bus_type:4;
  1223. } dw;
  1224. };
  1225. union loginfo_type sas_loginfo;
  1226. char *originator_str = NULL;
  1227. sas_loginfo.loginfo = log_info;
  1228. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  1229. return;
  1230. /* each nexus loss loginfo */
  1231. if (log_info == 0x31170000)
  1232. return;
  1233. /* eat the loginfos associated with task aborts */
  1234. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  1235. 0x31140000 || log_info == 0x31130000))
  1236. return;
  1237. switch (sas_loginfo.dw.originator) {
  1238. case 0:
  1239. originator_str = "IOP";
  1240. break;
  1241. case 1:
  1242. originator_str = "PL";
  1243. break;
  1244. case 2:
  1245. if (!ioc->hide_ir_msg)
  1246. originator_str = "IR";
  1247. else
  1248. originator_str = "WarpDrive";
  1249. break;
  1250. }
  1251. ioc_warn(ioc, "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
  1252. log_info,
  1253. originator_str, sas_loginfo.dw.code, sas_loginfo.dw.subcode);
  1254. }
  1255. /**
  1256. * _base_display_reply_info - handle reply descriptors depending on IOC Status
  1257. * @ioc: per adapter object
  1258. * @smid: system request message index
  1259. * @msix_index: MSIX table index supplied by the OS
  1260. * @reply: reply message frame (lower 32bit addr)
  1261. */
  1262. static void
  1263. _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  1264. u32 reply)
  1265. {
  1266. MPI2DefaultReply_t *mpi_reply;
  1267. u16 ioc_status;
  1268. u32 loginfo = 0;
  1269. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1270. if (unlikely(!mpi_reply)) {
  1271. ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
  1272. __FILE__, __LINE__, __func__);
  1273. return;
  1274. }
  1275. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  1276. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  1277. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  1278. _base_sas_ioc_info(ioc, mpi_reply,
  1279. mpt3sas_base_get_msg_frame(ioc, smid));
  1280. }
  1281. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  1282. loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
  1283. _base_sas_log_info(ioc, loginfo);
  1284. }
  1285. if (ioc_status || loginfo) {
  1286. ioc_status &= MPI2_IOCSTATUS_MASK;
  1287. mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
  1288. }
  1289. }
  1290. /**
  1291. * mpt3sas_base_done - base internal command completion routine
  1292. * @ioc: per adapter object
  1293. * @smid: system request message index
  1294. * @msix_index: MSIX table index supplied by the OS
  1295. * @reply: reply message frame(lower 32bit addr)
  1296. *
  1297. * Return:
  1298. * 1 meaning mf should be freed from _base_interrupt
  1299. * 0 means the mf is freed from this function.
  1300. */
  1301. u8
  1302. mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  1303. u32 reply)
  1304. {
  1305. MPI2DefaultReply_t *mpi_reply;
  1306. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1307. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  1308. return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
  1309. if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
  1310. return 1;
  1311. ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
  1312. if (mpi_reply) {
  1313. ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
  1314. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1315. }
  1316. ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
  1317. complete(&ioc->base_cmds.done);
  1318. return 1;
  1319. }
  1320. /**
  1321. * _base_async_event - main callback handler for firmware asyn events
  1322. * @ioc: per adapter object
  1323. * @msix_index: MSIX table index supplied by the OS
  1324. * @reply: reply message frame(lower 32bit addr)
  1325. *
  1326. * Return:
  1327. * 1 meaning mf should be freed from _base_interrupt
  1328. * 0 means the mf is freed from this function.
  1329. */
  1330. static u8
  1331. _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  1332. {
  1333. Mpi2EventNotificationReply_t *mpi_reply;
  1334. Mpi2EventAckRequest_t *ack_request;
  1335. u16 smid;
  1336. struct _event_ack_list *delayed_event_ack;
  1337. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1338. if (!mpi_reply)
  1339. return 1;
  1340. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  1341. return 1;
  1342. _base_display_event_data(ioc, mpi_reply);
  1343. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  1344. goto out;
  1345. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  1346. if (!smid) {
  1347. delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
  1348. GFP_ATOMIC);
  1349. if (!delayed_event_ack)
  1350. goto out;
  1351. INIT_LIST_HEAD(&delayed_event_ack->list);
  1352. delayed_event_ack->Event = mpi_reply->Event;
  1353. delayed_event_ack->EventContext = mpi_reply->EventContext;
  1354. list_add_tail(&delayed_event_ack->list,
  1355. &ioc->delayed_event_ack_list);
  1356. dewtprintk(ioc,
  1357. ioc_info(ioc, "DELAYED: EVENT ACK: event (0x%04x)\n",
  1358. le16_to_cpu(mpi_reply->Event)));
  1359. goto out;
  1360. }
  1361. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1362. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  1363. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  1364. ack_request->Event = mpi_reply->Event;
  1365. ack_request->EventContext = mpi_reply->EventContext;
  1366. ack_request->VF_ID = 0; /* TODO */
  1367. ack_request->VP_ID = 0;
  1368. ioc->put_smid_default(ioc, smid);
  1369. out:
  1370. /* scsih callback handler */
  1371. mpt3sas_scsih_event_callback(ioc, msix_index, reply);
  1372. /* ctl callback handler */
  1373. mpt3sas_ctl_event_callback(ioc, msix_index, reply);
  1374. return 1;
  1375. }
  1376. static struct scsiio_tracker *
  1377. _get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1378. {
  1379. struct scsi_cmnd *cmd;
  1380. if (WARN_ON(!smid) ||
  1381. WARN_ON(smid >= ioc->hi_priority_smid))
  1382. return NULL;
  1383. cmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  1384. if (cmd)
  1385. return scsi_cmd_priv(cmd);
  1386. return NULL;
  1387. }
  1388. /**
  1389. * _base_get_cb_idx - obtain the callback index
  1390. * @ioc: per adapter object
  1391. * @smid: system request message index
  1392. *
  1393. * Return: callback index.
  1394. */
  1395. static u8
  1396. _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1397. {
  1398. int i;
  1399. u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
  1400. u8 cb_idx = 0xFF;
  1401. if (smid < ioc->hi_priority_smid) {
  1402. struct scsiio_tracker *st;
  1403. if (smid < ctl_smid) {
  1404. st = _get_st_from_smid(ioc, smid);
  1405. if (st)
  1406. cb_idx = st->cb_idx;
  1407. } else if (smid == ctl_smid)
  1408. cb_idx = ioc->ctl_cb_idx;
  1409. } else if (smid < ioc->internal_smid) {
  1410. i = smid - ioc->hi_priority_smid;
  1411. cb_idx = ioc->hpr_lookup[i].cb_idx;
  1412. } else if (smid <= ioc->hba_queue_depth) {
  1413. i = smid - ioc->internal_smid;
  1414. cb_idx = ioc->internal_lookup[i].cb_idx;
  1415. }
  1416. return cb_idx;
  1417. }
  1418. /**
  1419. * mpt3sas_base_pause_mq_polling - pause polling on the mq poll queues
  1420. * when driver is flushing out the IOs.
  1421. * @ioc: per adapter object
  1422. *
  1423. * Pause polling on the mq poll (io uring) queues when driver is flushing
  1424. * out the IOs. Otherwise we may see the race condition of completing the same
  1425. * IO from two paths.
  1426. *
  1427. * Returns nothing.
  1428. */
  1429. void
  1430. mpt3sas_base_pause_mq_polling(struct MPT3SAS_ADAPTER *ioc)
  1431. {
  1432. int iopoll_q_count =
  1433. ioc->reply_queue_count - ioc->iopoll_q_start_index;
  1434. int qid;
  1435. for (qid = 0; qid < iopoll_q_count; qid++)
  1436. atomic_set(&ioc->io_uring_poll_queues[qid].pause, 1);
  1437. /*
  1438. * wait for current poll to complete.
  1439. */
  1440. for (qid = 0; qid < iopoll_q_count; qid++) {
  1441. while (atomic_read(&ioc->io_uring_poll_queues[qid].busy)) {
  1442. cpu_relax();
  1443. udelay(500);
  1444. }
  1445. }
  1446. }
  1447. /**
  1448. * mpt3sas_base_resume_mq_polling - Resume polling on mq poll queues.
  1449. * @ioc: per adapter object
  1450. *
  1451. * Returns nothing.
  1452. */
  1453. void
  1454. mpt3sas_base_resume_mq_polling(struct MPT3SAS_ADAPTER *ioc)
  1455. {
  1456. int iopoll_q_count =
  1457. ioc->reply_queue_count - ioc->iopoll_q_start_index;
  1458. int qid;
  1459. for (qid = 0; qid < iopoll_q_count; qid++)
  1460. atomic_set(&ioc->io_uring_poll_queues[qid].pause, 0);
  1461. }
  1462. /**
  1463. * mpt3sas_base_mask_interrupts - disable interrupts
  1464. * @ioc: per adapter object
  1465. *
  1466. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  1467. */
  1468. void
  1469. mpt3sas_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  1470. {
  1471. u32 him_register;
  1472. ioc->mask_interrupts = 1;
  1473. him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
  1474. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  1475. writel(him_register, &ioc->chip->HostInterruptMask);
  1476. ioc->base_readl(&ioc->chip->HostInterruptMask);
  1477. }
  1478. /**
  1479. * mpt3sas_base_unmask_interrupts - enable interrupts
  1480. * @ioc: per adapter object
  1481. *
  1482. * Enabling only Reply Interrupts
  1483. */
  1484. void
  1485. mpt3sas_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  1486. {
  1487. u32 him_register;
  1488. him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
  1489. him_register &= ~MPI2_HIM_RIM;
  1490. writel(him_register, &ioc->chip->HostInterruptMask);
  1491. ioc->mask_interrupts = 0;
  1492. }
  1493. union reply_descriptor {
  1494. u64 word;
  1495. struct {
  1496. u32 low;
  1497. u32 high;
  1498. } u;
  1499. };
  1500. static u32 base_mod64(u64 dividend, u32 divisor)
  1501. {
  1502. u32 remainder;
  1503. if (!divisor)
  1504. pr_err("mpt3sas: DIVISOR is zero, in div fn\n");
  1505. remainder = do_div(dividend, divisor);
  1506. return remainder;
  1507. }
  1508. /**
  1509. * _base_process_reply_queue - Process reply descriptors from reply
  1510. * descriptor post queue.
  1511. * @reply_q: per IRQ's reply queue object.
  1512. *
  1513. * Return: number of reply descriptors processed from reply
  1514. * descriptor queue.
  1515. */
  1516. static int
  1517. _base_process_reply_queue(struct adapter_reply_queue *reply_q)
  1518. {
  1519. union reply_descriptor rd;
  1520. u64 completed_cmds;
  1521. u8 request_descript_type;
  1522. u16 smid;
  1523. u8 cb_idx;
  1524. u32 reply;
  1525. u8 msix_index = reply_q->msix_index;
  1526. struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
  1527. Mpi2ReplyDescriptorsUnion_t *rpf;
  1528. u8 rc;
  1529. completed_cmds = 0;
  1530. if (!atomic_add_unless(&reply_q->busy, 1, 1))
  1531. return completed_cmds;
  1532. rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
  1533. request_descript_type = rpf->Default.ReplyFlags
  1534. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1535. if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  1536. atomic_dec(&reply_q->busy);
  1537. return completed_cmds;
  1538. }
  1539. cb_idx = 0xFF;
  1540. do {
  1541. rd.word = le64_to_cpu(rpf->Words);
  1542. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  1543. goto out;
  1544. reply = 0;
  1545. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  1546. if (request_descript_type ==
  1547. MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
  1548. request_descript_type ==
  1549. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
  1550. request_descript_type ==
  1551. MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
  1552. cb_idx = _base_get_cb_idx(ioc, smid);
  1553. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  1554. (likely(mpt_callbacks[cb_idx] != NULL))) {
  1555. rc = mpt_callbacks[cb_idx](ioc, smid,
  1556. msix_index, 0);
  1557. if (rc)
  1558. mpt3sas_base_free_smid(ioc, smid);
  1559. }
  1560. } else if (request_descript_type ==
  1561. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  1562. reply = le32_to_cpu(
  1563. rpf->AddressReply.ReplyFrameAddress);
  1564. if (reply > ioc->reply_dma_max_address ||
  1565. reply < ioc->reply_dma_min_address)
  1566. reply = 0;
  1567. if (smid) {
  1568. cb_idx = _base_get_cb_idx(ioc, smid);
  1569. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  1570. (likely(mpt_callbacks[cb_idx] != NULL))) {
  1571. rc = mpt_callbacks[cb_idx](ioc, smid,
  1572. msix_index, reply);
  1573. if (reply)
  1574. _base_display_reply_info(ioc,
  1575. smid, msix_index, reply);
  1576. if (rc)
  1577. mpt3sas_base_free_smid(ioc,
  1578. smid);
  1579. }
  1580. } else {
  1581. _base_async_event(ioc, msix_index, reply);
  1582. }
  1583. /* reply free queue handling */
  1584. if (reply) {
  1585. ioc->reply_free_host_index =
  1586. (ioc->reply_free_host_index ==
  1587. (ioc->reply_free_queue_depth - 1)) ?
  1588. 0 : ioc->reply_free_host_index + 1;
  1589. ioc->reply_free[ioc->reply_free_host_index] =
  1590. cpu_to_le32(reply);
  1591. if (ioc->is_mcpu_endpoint)
  1592. _base_clone_reply_to_sys_mem(ioc,
  1593. reply,
  1594. ioc->reply_free_host_index);
  1595. writel(ioc->reply_free_host_index,
  1596. &ioc->chip->ReplyFreeHostIndex);
  1597. }
  1598. }
  1599. rpf->Words = cpu_to_le64(ULLONG_MAX);
  1600. reply_q->reply_post_host_index =
  1601. (reply_q->reply_post_host_index ==
  1602. (ioc->reply_post_queue_depth - 1)) ? 0 :
  1603. reply_q->reply_post_host_index + 1;
  1604. request_descript_type =
  1605. reply_q->reply_post_free[reply_q->reply_post_host_index].
  1606. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1607. completed_cmds++;
  1608. /* Update the reply post host index after continuously
  1609. * processing the threshold number of Reply Descriptors.
  1610. * So that FW can find enough entries to post the Reply
  1611. * Descriptors in the reply descriptor post queue.
  1612. */
  1613. if (completed_cmds >= ioc->thresh_hold) {
  1614. if (ioc->combined_reply_queue) {
  1615. writel(reply_q->reply_post_host_index |
  1616. ((msix_index & 7) <<
  1617. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1618. ioc->replyPostRegisterIndex[msix_index/8]);
  1619. } else {
  1620. writel(reply_q->reply_post_host_index |
  1621. (msix_index <<
  1622. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1623. &ioc->chip->ReplyPostHostIndex);
  1624. }
  1625. if (!reply_q->is_iouring_poll_q &&
  1626. !reply_q->irq_poll_scheduled) {
  1627. reply_q->irq_poll_scheduled = true;
  1628. irq_poll_sched(&reply_q->irqpoll);
  1629. }
  1630. atomic_dec(&reply_q->busy);
  1631. return completed_cmds;
  1632. }
  1633. if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1634. goto out;
  1635. if (!reply_q->reply_post_host_index)
  1636. rpf = reply_q->reply_post_free;
  1637. else
  1638. rpf++;
  1639. } while (1);
  1640. out:
  1641. if (!completed_cmds) {
  1642. atomic_dec(&reply_q->busy);
  1643. return completed_cmds;
  1644. }
  1645. if (ioc->is_warpdrive) {
  1646. writel(reply_q->reply_post_host_index,
  1647. ioc->reply_post_host_index[msix_index]);
  1648. atomic_dec(&reply_q->busy);
  1649. return completed_cmds;
  1650. }
  1651. /* Update Reply Post Host Index.
  1652. * For those HBA's which support combined reply queue feature
  1653. * 1. Get the correct Supplemental Reply Post Host Index Register.
  1654. * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
  1655. * Index Register address bank i.e replyPostRegisterIndex[],
  1656. * 2. Then update this register with new reply host index value
  1657. * in ReplyPostIndex field and the MSIxIndex field with
  1658. * msix_index value reduced to a value between 0 and 7,
  1659. * using a modulo 8 operation. Since each Supplemental Reply Post
  1660. * Host Index Register supports 8 MSI-X vectors.
  1661. *
  1662. * For other HBA's just update the Reply Post Host Index register with
  1663. * new reply host index value in ReplyPostIndex Field and msix_index
  1664. * value in MSIxIndex field.
  1665. */
  1666. if (ioc->combined_reply_queue)
  1667. writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
  1668. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1669. ioc->replyPostRegisterIndex[msix_index/8]);
  1670. else
  1671. writel(reply_q->reply_post_host_index | (msix_index <<
  1672. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1673. &ioc->chip->ReplyPostHostIndex);
  1674. atomic_dec(&reply_q->busy);
  1675. return completed_cmds;
  1676. }
  1677. /**
  1678. * mpt3sas_blk_mq_poll - poll the blk mq poll queue
  1679. * @shost: Scsi_Host object
  1680. * @queue_num: hw ctx queue number
  1681. *
  1682. * Return number of entries that has been processed from poll queue.
  1683. */
  1684. int mpt3sas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
  1685. {
  1686. struct MPT3SAS_ADAPTER *ioc =
  1687. (struct MPT3SAS_ADAPTER *)shost->hostdata;
  1688. struct adapter_reply_queue *reply_q;
  1689. int num_entries = 0;
  1690. int qid = queue_num - ioc->iopoll_q_start_index;
  1691. if (atomic_read(&ioc->io_uring_poll_queues[qid].pause) ||
  1692. !atomic_add_unless(&ioc->io_uring_poll_queues[qid].busy, 1, 1))
  1693. return 0;
  1694. reply_q = ioc->io_uring_poll_queues[qid].reply_q;
  1695. num_entries = _base_process_reply_queue(reply_q);
  1696. atomic_dec(&ioc->io_uring_poll_queues[qid].busy);
  1697. return num_entries;
  1698. }
  1699. /**
  1700. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  1701. * @irq: irq number (not used)
  1702. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  1703. *
  1704. * Return: IRQ_HANDLED if processed, else IRQ_NONE.
  1705. */
  1706. static irqreturn_t
  1707. _base_interrupt(int irq, void *bus_id)
  1708. {
  1709. struct adapter_reply_queue *reply_q = bus_id;
  1710. struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
  1711. if (ioc->mask_interrupts)
  1712. return IRQ_NONE;
  1713. if (reply_q->irq_poll_scheduled)
  1714. return IRQ_HANDLED;
  1715. return ((_base_process_reply_queue(reply_q) > 0) ?
  1716. IRQ_HANDLED : IRQ_NONE);
  1717. }
  1718. /**
  1719. * _base_irqpoll - IRQ poll callback handler
  1720. * @irqpoll: irq_poll object
  1721. * @budget: irq poll weight
  1722. *
  1723. * Return: number of reply descriptors processed
  1724. */
  1725. static int
  1726. _base_irqpoll(struct irq_poll *irqpoll, int budget)
  1727. {
  1728. struct adapter_reply_queue *reply_q;
  1729. int num_entries = 0;
  1730. reply_q = container_of(irqpoll, struct adapter_reply_queue,
  1731. irqpoll);
  1732. if (reply_q->irq_line_enable) {
  1733. disable_irq_nosync(reply_q->os_irq);
  1734. reply_q->irq_line_enable = false;
  1735. }
  1736. num_entries = _base_process_reply_queue(reply_q);
  1737. if (num_entries < budget) {
  1738. irq_poll_complete(irqpoll);
  1739. reply_q->irq_poll_scheduled = false;
  1740. reply_q->irq_line_enable = true;
  1741. enable_irq(reply_q->os_irq);
  1742. /*
  1743. * Go for one more round of processing the
  1744. * reply descriptor post queue in case the HBA
  1745. * Firmware has posted some reply descriptors
  1746. * while reenabling the IRQ.
  1747. */
  1748. _base_process_reply_queue(reply_q);
  1749. }
  1750. return num_entries;
  1751. }
  1752. /**
  1753. * _base_init_irqpolls - initliaze IRQ polls
  1754. * @ioc: per adapter object
  1755. *
  1756. * Return: nothing
  1757. */
  1758. static void
  1759. _base_init_irqpolls(struct MPT3SAS_ADAPTER *ioc)
  1760. {
  1761. struct adapter_reply_queue *reply_q, *next;
  1762. if (list_empty(&ioc->reply_queue_list))
  1763. return;
  1764. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  1765. if (reply_q->is_iouring_poll_q)
  1766. continue;
  1767. irq_poll_init(&reply_q->irqpoll,
  1768. ioc->hba_queue_depth/4, _base_irqpoll);
  1769. reply_q->irq_poll_scheduled = false;
  1770. reply_q->irq_line_enable = true;
  1771. reply_q->os_irq = pci_irq_vector(ioc->pdev,
  1772. reply_q->msix_index);
  1773. }
  1774. }
  1775. /**
  1776. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  1777. * @ioc: per adapter object
  1778. *
  1779. * Return: Whether or not MSI/X is enabled.
  1780. */
  1781. static inline int
  1782. _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
  1783. {
  1784. return (ioc->facts.IOCCapabilities &
  1785. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  1786. }
  1787. /**
  1788. * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
  1789. * @ioc: per adapter object
  1790. * @poll: poll over reply descriptor pools incase interrupt for
  1791. * timed-out SCSI command got delayed
  1792. * Context: non-ISR context
  1793. *
  1794. * Called when a Task Management request has completed.
  1795. */
  1796. void
  1797. mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc, u8 poll)
  1798. {
  1799. struct adapter_reply_queue *reply_q;
  1800. /* If MSIX capability is turned off
  1801. * then multi-queues are not enabled
  1802. */
  1803. if (!_base_is_controller_msix_enabled(ioc))
  1804. return;
  1805. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  1806. if (ioc->shost_recovery || ioc->remove_host ||
  1807. ioc->pci_error_recovery)
  1808. return;
  1809. /* TMs are on msix_index == 0 */
  1810. if (reply_q->msix_index == 0)
  1811. continue;
  1812. if (reply_q->is_iouring_poll_q) {
  1813. _base_process_reply_queue(reply_q);
  1814. continue;
  1815. }
  1816. synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
  1817. if (reply_q->irq_poll_scheduled) {
  1818. /* Calling irq_poll_disable will wait for any pending
  1819. * callbacks to have completed.
  1820. */
  1821. irq_poll_disable(&reply_q->irqpoll);
  1822. irq_poll_enable(&reply_q->irqpoll);
  1823. /* check how the scheduled poll has ended,
  1824. * clean up only if necessary
  1825. */
  1826. if (reply_q->irq_poll_scheduled) {
  1827. reply_q->irq_poll_scheduled = false;
  1828. reply_q->irq_line_enable = true;
  1829. enable_irq(reply_q->os_irq);
  1830. }
  1831. }
  1832. if (poll)
  1833. _base_process_reply_queue(reply_q);
  1834. }
  1835. }
  1836. /**
  1837. * mpt3sas_base_release_callback_handler - clear interrupt callback handler
  1838. * @cb_idx: callback index
  1839. */
  1840. void
  1841. mpt3sas_base_release_callback_handler(u8 cb_idx)
  1842. {
  1843. mpt_callbacks[cb_idx] = NULL;
  1844. }
  1845. /**
  1846. * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
  1847. * @cb_func: callback function
  1848. *
  1849. * Return: Index of @cb_func.
  1850. */
  1851. u8
  1852. mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  1853. {
  1854. u8 cb_idx;
  1855. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  1856. if (mpt_callbacks[cb_idx] == NULL)
  1857. break;
  1858. mpt_callbacks[cb_idx] = cb_func;
  1859. return cb_idx;
  1860. }
  1861. /**
  1862. * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
  1863. */
  1864. void
  1865. mpt3sas_base_initialize_callback_handler(void)
  1866. {
  1867. u8 cb_idx;
  1868. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  1869. mpt3sas_base_release_callback_handler(cb_idx);
  1870. }
  1871. /**
  1872. * _base_build_zero_len_sge - build zero length sg entry
  1873. * @ioc: per adapter object
  1874. * @paddr: virtual address for SGE
  1875. *
  1876. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1877. * something to use if the target device goes brain dead and tries
  1878. * to send data even when none is asked for.
  1879. */
  1880. static void
  1881. _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1882. {
  1883. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  1884. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  1885. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  1886. MPI2_SGE_FLAGS_SHIFT);
  1887. ioc->base_add_sg_single(paddr, flags_length, -1);
  1888. }
  1889. /**
  1890. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1891. * @paddr: virtual address for SGE
  1892. * @flags_length: SGE flags and data transfer length
  1893. * @dma_addr: Physical address
  1894. */
  1895. static void
  1896. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1897. {
  1898. Mpi2SGESimple32_t *sgel = paddr;
  1899. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1900. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1901. sgel->FlagsLength = cpu_to_le32(flags_length);
  1902. sgel->Address = cpu_to_le32(dma_addr);
  1903. }
  1904. /**
  1905. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1906. * @paddr: virtual address for SGE
  1907. * @flags_length: SGE flags and data transfer length
  1908. * @dma_addr: Physical address
  1909. */
  1910. static void
  1911. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1912. {
  1913. Mpi2SGESimple64_t *sgel = paddr;
  1914. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1915. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1916. sgel->FlagsLength = cpu_to_le32(flags_length);
  1917. sgel->Address = cpu_to_le64(dma_addr);
  1918. }
  1919. /**
  1920. * _base_get_chain_buffer_tracker - obtain chain tracker
  1921. * @ioc: per adapter object
  1922. * @scmd: SCSI commands of the IO request
  1923. *
  1924. * Return: chain tracker from chain_lookup table using key as
  1925. * smid and smid's chain_offset.
  1926. */
  1927. static struct chain_tracker *
  1928. _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc,
  1929. struct scsi_cmnd *scmd)
  1930. {
  1931. struct chain_tracker *chain_req;
  1932. struct scsiio_tracker *st = scsi_cmd_priv(scmd);
  1933. u16 smid = st->smid;
  1934. u8 chain_offset =
  1935. atomic_read(&ioc->chain_lookup[smid - 1].chain_offset);
  1936. if (chain_offset == ioc->chains_needed_per_io)
  1937. return NULL;
  1938. chain_req = &ioc->chain_lookup[smid - 1].chains_per_smid[chain_offset];
  1939. atomic_inc(&ioc->chain_lookup[smid - 1].chain_offset);
  1940. return chain_req;
  1941. }
  1942. /**
  1943. * _base_build_sg - build generic sg
  1944. * @ioc: per adapter object
  1945. * @psge: virtual address for SGE
  1946. * @data_out_dma: physical address for WRITES
  1947. * @data_out_sz: data xfer size for WRITES
  1948. * @data_in_dma: physical address for READS
  1949. * @data_in_sz: data xfer size for READS
  1950. */
  1951. static void
  1952. _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1953. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1954. size_t data_in_sz)
  1955. {
  1956. u32 sgl_flags;
  1957. if (!data_out_sz && !data_in_sz) {
  1958. _base_build_zero_len_sge(ioc, psge);
  1959. return;
  1960. }
  1961. if (data_out_sz && data_in_sz) {
  1962. /* WRITE sgel first */
  1963. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1964. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1965. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1966. ioc->base_add_sg_single(psge, sgl_flags |
  1967. data_out_sz, data_out_dma);
  1968. /* incr sgel */
  1969. psge += ioc->sge_size;
  1970. /* READ sgel last */
  1971. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1972. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1973. MPI2_SGE_FLAGS_END_OF_LIST);
  1974. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1975. ioc->base_add_sg_single(psge, sgl_flags |
  1976. data_in_sz, data_in_dma);
  1977. } else if (data_out_sz) /* WRITE */ {
  1978. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1979. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1980. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1981. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1982. ioc->base_add_sg_single(psge, sgl_flags |
  1983. data_out_sz, data_out_dma);
  1984. } else if (data_in_sz) /* READ */ {
  1985. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1986. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1987. MPI2_SGE_FLAGS_END_OF_LIST);
  1988. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1989. ioc->base_add_sg_single(psge, sgl_flags |
  1990. data_in_sz, data_in_dma);
  1991. }
  1992. }
  1993. /* IEEE format sgls */
  1994. /**
  1995. * _base_build_nvme_prp - This function is called for NVMe end devices to build
  1996. * a native SGL (NVMe PRP).
  1997. * @ioc: per adapter object
  1998. * @smid: system request message index for getting asscociated SGL
  1999. * @nvme_encap_request: the NVMe request msg frame pointer
  2000. * @data_out_dma: physical address for WRITES
  2001. * @data_out_sz: data xfer size for WRITES
  2002. * @data_in_dma: physical address for READS
  2003. * @data_in_sz: data xfer size for READS
  2004. *
  2005. * The native SGL is built starting in the first PRP
  2006. * entry of the NVMe message (PRP1). If the data buffer is small enough to be
  2007. * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
  2008. * used to describe a larger data buffer. If the data buffer is too large to
  2009. * describe using the two PRP entriess inside the NVMe message, then PRP1
  2010. * describes the first data memory segment, and PRP2 contains a pointer to a PRP
  2011. * list located elsewhere in memory to describe the remaining data memory
  2012. * segments. The PRP list will be contiguous.
  2013. *
  2014. * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
  2015. * consists of a list of PRP entries to describe a number of noncontigous
  2016. * physical memory segments as a single memory buffer, just as a SGL does. Note
  2017. * however, that this function is only used by the IOCTL call, so the memory
  2018. * given will be guaranteed to be contiguous. There is no need to translate
  2019. * non-contiguous SGL into a PRP in this case. All PRPs will describe
  2020. * contiguous space that is one page size each.
  2021. *
  2022. * Each NVMe message contains two PRP entries. The first (PRP1) either contains
  2023. * a PRP list pointer or a PRP element, depending upon the command. PRP2
  2024. * contains the second PRP element if the memory being described fits within 2
  2025. * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
  2026. *
  2027. * A PRP list pointer contains the address of a PRP list, structured as a linear
  2028. * array of PRP entries. Each PRP entry in this list describes a segment of
  2029. * physical memory.
  2030. *
  2031. * Each 64-bit PRP entry comprises an address and an offset field. The address
  2032. * always points at the beginning of a 4KB physical memory page, and the offset
  2033. * describes where within that 4KB page the memory segment begins. Only the
  2034. * first element in a PRP list may contain a non-zero offset, implying that all
  2035. * memory segments following the first begin at the start of a 4KB page.
  2036. *
  2037. * Each PRP element normally describes 4KB of physical memory, with exceptions
  2038. * for the first and last elements in the list. If the memory being described
  2039. * by the list begins at a non-zero offset within the first 4KB page, then the
  2040. * first PRP element will contain a non-zero offset indicating where the region
  2041. * begins within the 4KB page. The last memory segment may end before the end
  2042. * of the 4KB segment, depending upon the overall size of the memory being
  2043. * described by the PRP list.
  2044. *
  2045. * Since PRP entries lack any indication of size, the overall data buffer length
  2046. * is used to determine where the end of the data memory buffer is located, and
  2047. * how many PRP entries are required to describe it.
  2048. */
  2049. static void
  2050. _base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2051. Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
  2052. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  2053. size_t data_in_sz)
  2054. {
  2055. int prp_size = NVME_PRP_SIZE;
  2056. __le64 *prp_entry, *prp1_entry, *prp2_entry;
  2057. __le64 *prp_page;
  2058. dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
  2059. u32 offset, entry_len;
  2060. u32 page_mask_result, page_mask;
  2061. size_t length;
  2062. struct mpt3sas_nvme_cmd *nvme_cmd =
  2063. (void *)nvme_encap_request->NVMe_Command;
  2064. /*
  2065. * Not all commands require a data transfer. If no data, just return
  2066. * without constructing any PRP.
  2067. */
  2068. if (!data_in_sz && !data_out_sz)
  2069. return;
  2070. prp1_entry = &nvme_cmd->prp1;
  2071. prp2_entry = &nvme_cmd->prp2;
  2072. prp_entry = prp1_entry;
  2073. /*
  2074. * For the PRP entries, use the specially allocated buffer of
  2075. * contiguous memory.
  2076. */
  2077. prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
  2078. prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
  2079. /*
  2080. * Check if we are within 1 entry of a page boundary we don't
  2081. * want our first entry to be a PRP List entry.
  2082. */
  2083. page_mask = ioc->page_size - 1;
  2084. page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
  2085. if (!page_mask_result) {
  2086. /* Bump up to next page boundary. */
  2087. prp_page = (__le64 *)((u8 *)prp_page + prp_size);
  2088. prp_page_dma = prp_page_dma + prp_size;
  2089. }
  2090. /*
  2091. * Set PRP physical pointer, which initially points to the current PRP
  2092. * DMA memory page.
  2093. */
  2094. prp_entry_dma = prp_page_dma;
  2095. /* Get physical address and length of the data buffer. */
  2096. if (data_in_sz) {
  2097. dma_addr = data_in_dma;
  2098. length = data_in_sz;
  2099. } else {
  2100. dma_addr = data_out_dma;
  2101. length = data_out_sz;
  2102. }
  2103. /* Loop while the length is not zero. */
  2104. while (length) {
  2105. /*
  2106. * Check if we need to put a list pointer here if we are at
  2107. * page boundary - prp_size (8 bytes).
  2108. */
  2109. page_mask_result = (prp_entry_dma + prp_size) & page_mask;
  2110. if (!page_mask_result) {
  2111. /*
  2112. * This is the last entry in a PRP List, so we need to
  2113. * put a PRP list pointer here. What this does is:
  2114. * - bump the current memory pointer to the next
  2115. * address, which will be the next full page.
  2116. * - set the PRP Entry to point to that page. This
  2117. * is now the PRP List pointer.
  2118. * - bump the PRP Entry pointer the start of the
  2119. * next page. Since all of this PRP memory is
  2120. * contiguous, no need to get a new page - it's
  2121. * just the next address.
  2122. */
  2123. prp_entry_dma++;
  2124. *prp_entry = cpu_to_le64(prp_entry_dma);
  2125. prp_entry++;
  2126. }
  2127. /* Need to handle if entry will be part of a page. */
  2128. offset = dma_addr & page_mask;
  2129. entry_len = ioc->page_size - offset;
  2130. if (prp_entry == prp1_entry) {
  2131. /*
  2132. * Must fill in the first PRP pointer (PRP1) before
  2133. * moving on.
  2134. */
  2135. *prp1_entry = cpu_to_le64(dma_addr);
  2136. /*
  2137. * Now point to the second PRP entry within the
  2138. * command (PRP2).
  2139. */
  2140. prp_entry = prp2_entry;
  2141. } else if (prp_entry == prp2_entry) {
  2142. /*
  2143. * Should the PRP2 entry be a PRP List pointer or just
  2144. * a regular PRP pointer? If there is more than one
  2145. * more page of data, must use a PRP List pointer.
  2146. */
  2147. if (length > ioc->page_size) {
  2148. /*
  2149. * PRP2 will contain a PRP List pointer because
  2150. * more PRP's are needed with this command. The
  2151. * list will start at the beginning of the
  2152. * contiguous buffer.
  2153. */
  2154. *prp2_entry = cpu_to_le64(prp_entry_dma);
  2155. /*
  2156. * The next PRP Entry will be the start of the
  2157. * first PRP List.
  2158. */
  2159. prp_entry = prp_page;
  2160. } else {
  2161. /*
  2162. * After this, the PRP Entries are complete.
  2163. * This command uses 2 PRP's and no PRP list.
  2164. */
  2165. *prp2_entry = cpu_to_le64(dma_addr);
  2166. }
  2167. } else {
  2168. /*
  2169. * Put entry in list and bump the addresses.
  2170. *
  2171. * After PRP1 and PRP2 are filled in, this will fill in
  2172. * all remaining PRP entries in a PRP List, one per
  2173. * each time through the loop.
  2174. */
  2175. *prp_entry = cpu_to_le64(dma_addr);
  2176. prp_entry++;
  2177. prp_entry_dma++;
  2178. }
  2179. /*
  2180. * Bump the phys address of the command's data buffer by the
  2181. * entry_len.
  2182. */
  2183. dma_addr += entry_len;
  2184. /* Decrement length accounting for last partial page. */
  2185. if (entry_len > length)
  2186. length = 0;
  2187. else
  2188. length -= entry_len;
  2189. }
  2190. }
  2191. /**
  2192. * base_make_prp_nvme - Prepare PRPs (Physical Region Page) -
  2193. * SGLs specific to NVMe drives only
  2194. *
  2195. * @ioc: per adapter object
  2196. * @scmd: SCSI command from the mid-layer
  2197. * @mpi_request: mpi request
  2198. * @smid: msg Index
  2199. * @sge_count: scatter gather element count.
  2200. *
  2201. * Return: true: PRPs are built
  2202. * false: IEEE SGLs needs to be built
  2203. */
  2204. static void
  2205. base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
  2206. struct scsi_cmnd *scmd,
  2207. Mpi25SCSIIORequest_t *mpi_request,
  2208. u16 smid, int sge_count)
  2209. {
  2210. int sge_len, num_prp_in_chain = 0;
  2211. Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
  2212. __le64 *curr_buff;
  2213. dma_addr_t msg_dma, sge_addr, offset;
  2214. u32 page_mask, page_mask_result;
  2215. struct scatterlist *sg_scmd;
  2216. u32 first_prp_len;
  2217. int data_len = scsi_bufflen(scmd);
  2218. u32 nvme_pg_size;
  2219. nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
  2220. /*
  2221. * Nvme has a very convoluted prp format. One prp is required
  2222. * for each page or partial page. Driver need to split up OS sg_list
  2223. * entries if it is longer than one page or cross a page
  2224. * boundary. Driver also have to insert a PRP list pointer entry as
  2225. * the last entry in each physical page of the PRP list.
  2226. *
  2227. * NOTE: The first PRP "entry" is actually placed in the first
  2228. * SGL entry in the main message as IEEE 64 format. The 2nd
  2229. * entry in the main message is the chain element, and the rest
  2230. * of the PRP entries are built in the contiguous pcie buffer.
  2231. */
  2232. page_mask = nvme_pg_size - 1;
  2233. /*
  2234. * Native SGL is needed.
  2235. * Put a chain element in main message frame that points to the first
  2236. * chain buffer.
  2237. *
  2238. * NOTE: The ChainOffset field must be 0 when using a chain pointer to
  2239. * a native SGL.
  2240. */
  2241. /* Set main message chain element pointer */
  2242. main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
  2243. /*
  2244. * For NVMe the chain element needs to be the 2nd SG entry in the main
  2245. * message.
  2246. */
  2247. main_chain_element = (Mpi25IeeeSgeChain64_t *)
  2248. ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
  2249. /*
  2250. * For the PRP entries, use the specially allocated buffer of
  2251. * contiguous memory. Normal chain buffers can't be used
  2252. * because each chain buffer would need to be the size of an OS
  2253. * page (4k).
  2254. */
  2255. curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
  2256. msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
  2257. main_chain_element->Address = cpu_to_le64(msg_dma);
  2258. main_chain_element->NextChainOffset = 0;
  2259. main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  2260. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  2261. MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
  2262. /* Build first prp, sge need not to be page aligned*/
  2263. ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
  2264. sg_scmd = scsi_sglist(scmd);
  2265. sge_addr = sg_dma_address(sg_scmd);
  2266. sge_len = sg_dma_len(sg_scmd);
  2267. offset = sge_addr & page_mask;
  2268. first_prp_len = nvme_pg_size - offset;
  2269. ptr_first_sgl->Address = cpu_to_le64(sge_addr);
  2270. ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
  2271. data_len -= first_prp_len;
  2272. if (sge_len > first_prp_len) {
  2273. sge_addr += first_prp_len;
  2274. sge_len -= first_prp_len;
  2275. } else if (data_len && (sge_len == first_prp_len)) {
  2276. sg_scmd = sg_next(sg_scmd);
  2277. sge_addr = sg_dma_address(sg_scmd);
  2278. sge_len = sg_dma_len(sg_scmd);
  2279. }
  2280. for (;;) {
  2281. offset = sge_addr & page_mask;
  2282. /* Put PRP pointer due to page boundary*/
  2283. page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
  2284. if (unlikely(!page_mask_result)) {
  2285. scmd_printk(KERN_NOTICE,
  2286. scmd, "page boundary curr_buff: 0x%p\n",
  2287. curr_buff);
  2288. msg_dma += 8;
  2289. *curr_buff = cpu_to_le64(msg_dma);
  2290. curr_buff++;
  2291. num_prp_in_chain++;
  2292. }
  2293. *curr_buff = cpu_to_le64(sge_addr);
  2294. curr_buff++;
  2295. msg_dma += 8;
  2296. num_prp_in_chain++;
  2297. sge_addr += nvme_pg_size;
  2298. sge_len -= nvme_pg_size;
  2299. data_len -= nvme_pg_size;
  2300. if (data_len <= 0)
  2301. break;
  2302. if (sge_len > 0)
  2303. continue;
  2304. sg_scmd = sg_next(sg_scmd);
  2305. sge_addr = sg_dma_address(sg_scmd);
  2306. sge_len = sg_dma_len(sg_scmd);
  2307. }
  2308. main_chain_element->Length =
  2309. cpu_to_le32(num_prp_in_chain * sizeof(u64));
  2310. return;
  2311. }
  2312. static bool
  2313. base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
  2314. struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
  2315. {
  2316. u32 data_length = 0;
  2317. bool build_prp = true;
  2318. data_length = scsi_bufflen(scmd);
  2319. if (pcie_device &&
  2320. (mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info))) {
  2321. build_prp = false;
  2322. return build_prp;
  2323. }
  2324. /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
  2325. * we built IEEE SGL
  2326. */
  2327. if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
  2328. build_prp = false;
  2329. return build_prp;
  2330. }
  2331. /**
  2332. * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
  2333. * determine if the driver needs to build a native SGL. If so, that native
  2334. * SGL is built in the special contiguous buffers allocated especially for
  2335. * PCIe SGL creation. If the driver will not build a native SGL, return
  2336. * TRUE and a normal IEEE SGL will be built. Currently this routine
  2337. * supports NVMe.
  2338. * @ioc: per adapter object
  2339. * @mpi_request: mf request pointer
  2340. * @smid: system request message index
  2341. * @scmd: scsi command
  2342. * @pcie_device: points to the PCIe device's info
  2343. *
  2344. * Return: 0 if native SGL was built, 1 if no SGL was built
  2345. */
  2346. static int
  2347. _base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
  2348. Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
  2349. struct _pcie_device *pcie_device)
  2350. {
  2351. int sges_left;
  2352. /* Get the SG list pointer and info. */
  2353. sges_left = scsi_dma_map(scmd);
  2354. if (sges_left < 0)
  2355. return 1;
  2356. /* Check if we need to build a native SG list. */
  2357. if (!base_is_prp_possible(ioc, pcie_device,
  2358. scmd, sges_left)) {
  2359. /* We built a native SG list, just return. */
  2360. goto out;
  2361. }
  2362. /*
  2363. * Build native NVMe PRP.
  2364. */
  2365. base_make_prp_nvme(ioc, scmd, mpi_request,
  2366. smid, sges_left);
  2367. return 0;
  2368. out:
  2369. scsi_dma_unmap(scmd);
  2370. return 1;
  2371. }
  2372. /**
  2373. * _base_add_sg_single_ieee - add sg element for IEEE format
  2374. * @paddr: virtual address for SGE
  2375. * @flags: SGE flags
  2376. * @chain_offset: number of 128 byte elements from start of segment
  2377. * @length: data transfer length
  2378. * @dma_addr: Physical address
  2379. */
  2380. static void
  2381. _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
  2382. dma_addr_t dma_addr)
  2383. {
  2384. Mpi25IeeeSgeChain64_t *sgel = paddr;
  2385. sgel->Flags = flags;
  2386. sgel->NextChainOffset = chain_offset;
  2387. sgel->Length = cpu_to_le32(length);
  2388. sgel->Address = cpu_to_le64(dma_addr);
  2389. }
  2390. /**
  2391. * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
  2392. * @ioc: per adapter object
  2393. * @paddr: virtual address for SGE
  2394. *
  2395. * Create a zero length scatter gather entry to insure the IOCs hardware has
  2396. * something to use if the target device goes brain dead and tries
  2397. * to send data even when none is asked for.
  2398. */
  2399. static void
  2400. _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  2401. {
  2402. u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2403. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  2404. MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
  2405. _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
  2406. }
  2407. /**
  2408. * _base_build_sg_scmd - main sg creation routine
  2409. * pcie_device is unused here!
  2410. * @ioc: per adapter object
  2411. * @scmd: scsi command
  2412. * @smid: system request message index
  2413. * @unused: unused pcie_device pointer
  2414. * Context: none.
  2415. *
  2416. * The main routine that builds scatter gather table from a given
  2417. * scsi request sent via the .queuecommand main handler.
  2418. *
  2419. * Return: 0 success, anything else error
  2420. */
  2421. static int
  2422. _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
  2423. struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
  2424. {
  2425. Mpi2SCSIIORequest_t *mpi_request;
  2426. dma_addr_t chain_dma;
  2427. struct scatterlist *sg_scmd;
  2428. void *sg_local, *chain;
  2429. u32 chain_offset;
  2430. u32 chain_length;
  2431. u32 chain_flags;
  2432. int sges_left;
  2433. u32 sges_in_segment;
  2434. u32 sgl_flags;
  2435. u32 sgl_flags_last_element;
  2436. u32 sgl_flags_end_buffer;
  2437. struct chain_tracker *chain_req;
  2438. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2439. /* init scatter gather flags */
  2440. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  2441. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2442. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  2443. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  2444. << MPI2_SGE_FLAGS_SHIFT;
  2445. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  2446. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  2447. << MPI2_SGE_FLAGS_SHIFT;
  2448. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  2449. sg_scmd = scsi_sglist(scmd);
  2450. sges_left = scsi_dma_map(scmd);
  2451. if (sges_left < 0)
  2452. return -ENOMEM;
  2453. sg_local = &mpi_request->SGL;
  2454. sges_in_segment = ioc->max_sges_in_main_message;
  2455. if (sges_left <= sges_in_segment)
  2456. goto fill_in_last_segment;
  2457. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  2458. (sges_in_segment * ioc->sge_size))/4;
  2459. /* fill in main message segment when there is a chain following */
  2460. while (sges_in_segment) {
  2461. if (sges_in_segment == 1)
  2462. ioc->base_add_sg_single(sg_local,
  2463. sgl_flags_last_element | sg_dma_len(sg_scmd),
  2464. sg_dma_address(sg_scmd));
  2465. else
  2466. ioc->base_add_sg_single(sg_local, sgl_flags |
  2467. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2468. sg_scmd = sg_next(sg_scmd);
  2469. sg_local += ioc->sge_size;
  2470. sges_left--;
  2471. sges_in_segment--;
  2472. }
  2473. /* initializing the chain flags and pointers */
  2474. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  2475. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2476. if (!chain_req)
  2477. return -1;
  2478. chain = chain_req->chain_buffer;
  2479. chain_dma = chain_req->chain_buffer_dma;
  2480. do {
  2481. sges_in_segment = (sges_left <=
  2482. ioc->max_sges_in_chain_message) ? sges_left :
  2483. ioc->max_sges_in_chain_message;
  2484. chain_offset = (sges_left == sges_in_segment) ?
  2485. 0 : (sges_in_segment * ioc->sge_size)/4;
  2486. chain_length = sges_in_segment * ioc->sge_size;
  2487. if (chain_offset) {
  2488. chain_offset = chain_offset <<
  2489. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  2490. chain_length += ioc->sge_size;
  2491. }
  2492. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  2493. chain_length, chain_dma);
  2494. sg_local = chain;
  2495. if (!chain_offset)
  2496. goto fill_in_last_segment;
  2497. /* fill in chain segments */
  2498. while (sges_in_segment) {
  2499. if (sges_in_segment == 1)
  2500. ioc->base_add_sg_single(sg_local,
  2501. sgl_flags_last_element |
  2502. sg_dma_len(sg_scmd),
  2503. sg_dma_address(sg_scmd));
  2504. else
  2505. ioc->base_add_sg_single(sg_local, sgl_flags |
  2506. sg_dma_len(sg_scmd),
  2507. sg_dma_address(sg_scmd));
  2508. sg_scmd = sg_next(sg_scmd);
  2509. sg_local += ioc->sge_size;
  2510. sges_left--;
  2511. sges_in_segment--;
  2512. }
  2513. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2514. if (!chain_req)
  2515. return -1;
  2516. chain = chain_req->chain_buffer;
  2517. chain_dma = chain_req->chain_buffer_dma;
  2518. } while (1);
  2519. fill_in_last_segment:
  2520. /* fill the last segment */
  2521. while (sges_left) {
  2522. if (sges_left == 1)
  2523. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  2524. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2525. else
  2526. ioc->base_add_sg_single(sg_local, sgl_flags |
  2527. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2528. sg_scmd = sg_next(sg_scmd);
  2529. sg_local += ioc->sge_size;
  2530. sges_left--;
  2531. }
  2532. return 0;
  2533. }
  2534. /**
  2535. * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
  2536. * @ioc: per adapter object
  2537. * @scmd: scsi command
  2538. * @smid: system request message index
  2539. * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
  2540. * constructed on need.
  2541. * Context: none.
  2542. *
  2543. * The main routine that builds scatter gather table from a given
  2544. * scsi request sent via the .queuecommand main handler.
  2545. *
  2546. * Return: 0 success, anything else error
  2547. */
  2548. static int
  2549. _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
  2550. struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
  2551. {
  2552. Mpi25SCSIIORequest_t *mpi_request;
  2553. dma_addr_t chain_dma;
  2554. struct scatterlist *sg_scmd;
  2555. void *sg_local, *chain;
  2556. u32 chain_offset;
  2557. u32 chain_length;
  2558. int sges_left;
  2559. u32 sges_in_segment;
  2560. u8 simple_sgl_flags;
  2561. u8 simple_sgl_flags_last;
  2562. u8 chain_sgl_flags;
  2563. struct chain_tracker *chain_req;
  2564. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2565. /* init scatter gather flags */
  2566. simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2567. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2568. simple_sgl_flags_last = simple_sgl_flags |
  2569. MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  2570. chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  2571. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2572. /* Check if we need to build a native SG list. */
  2573. if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
  2574. smid, scmd, pcie_device) == 0)) {
  2575. /* We built a native SG list, just return. */
  2576. return 0;
  2577. }
  2578. sg_scmd = scsi_sglist(scmd);
  2579. sges_left = scsi_dma_map(scmd);
  2580. if (sges_left < 0)
  2581. return -ENOMEM;
  2582. sg_local = &mpi_request->SGL;
  2583. sges_in_segment = (ioc->request_sz -
  2584. offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
  2585. if (sges_left <= sges_in_segment)
  2586. goto fill_in_last_segment;
  2587. mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
  2588. (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
  2589. /* fill in main message segment when there is a chain following */
  2590. while (sges_in_segment > 1) {
  2591. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  2592. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2593. sg_scmd = sg_next(sg_scmd);
  2594. sg_local += ioc->sge_size_ieee;
  2595. sges_left--;
  2596. sges_in_segment--;
  2597. }
  2598. /* initializing the pointers */
  2599. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2600. if (!chain_req)
  2601. return -1;
  2602. chain = chain_req->chain_buffer;
  2603. chain_dma = chain_req->chain_buffer_dma;
  2604. do {
  2605. sges_in_segment = (sges_left <=
  2606. ioc->max_sges_in_chain_message) ? sges_left :
  2607. ioc->max_sges_in_chain_message;
  2608. chain_offset = (sges_left == sges_in_segment) ?
  2609. 0 : sges_in_segment;
  2610. chain_length = sges_in_segment * ioc->sge_size_ieee;
  2611. if (chain_offset)
  2612. chain_length += ioc->sge_size_ieee;
  2613. _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
  2614. chain_offset, chain_length, chain_dma);
  2615. sg_local = chain;
  2616. if (!chain_offset)
  2617. goto fill_in_last_segment;
  2618. /* fill in chain segments */
  2619. while (sges_in_segment) {
  2620. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  2621. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2622. sg_scmd = sg_next(sg_scmd);
  2623. sg_local += ioc->sge_size_ieee;
  2624. sges_left--;
  2625. sges_in_segment--;
  2626. }
  2627. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2628. if (!chain_req)
  2629. return -1;
  2630. chain = chain_req->chain_buffer;
  2631. chain_dma = chain_req->chain_buffer_dma;
  2632. } while (1);
  2633. fill_in_last_segment:
  2634. /* fill the last segment */
  2635. while (sges_left > 0) {
  2636. if (sges_left == 1)
  2637. _base_add_sg_single_ieee(sg_local,
  2638. simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
  2639. sg_dma_address(sg_scmd));
  2640. else
  2641. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  2642. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2643. sg_scmd = sg_next(sg_scmd);
  2644. sg_local += ioc->sge_size_ieee;
  2645. sges_left--;
  2646. }
  2647. return 0;
  2648. }
  2649. /**
  2650. * _base_build_sg_ieee - build generic sg for IEEE format
  2651. * @ioc: per adapter object
  2652. * @psge: virtual address for SGE
  2653. * @data_out_dma: physical address for WRITES
  2654. * @data_out_sz: data xfer size for WRITES
  2655. * @data_in_dma: physical address for READS
  2656. * @data_in_sz: data xfer size for READS
  2657. */
  2658. static void
  2659. _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
  2660. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  2661. size_t data_in_sz)
  2662. {
  2663. u8 sgl_flags;
  2664. if (!data_out_sz && !data_in_sz) {
  2665. _base_build_zero_len_sge_ieee(ioc, psge);
  2666. return;
  2667. }
  2668. if (data_out_sz && data_in_sz) {
  2669. /* WRITE sgel first */
  2670. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2671. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2672. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  2673. data_out_dma);
  2674. /* incr sgel */
  2675. psge += ioc->sge_size_ieee;
  2676. /* READ sgel last */
  2677. sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  2678. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  2679. data_in_dma);
  2680. } else if (data_out_sz) /* WRITE */ {
  2681. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2682. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  2683. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2684. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  2685. data_out_dma);
  2686. } else if (data_in_sz) /* READ */ {
  2687. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2688. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  2689. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2690. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  2691. data_in_dma);
  2692. }
  2693. }
  2694. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  2695. /**
  2696. * _base_config_dma_addressing - set dma addressing
  2697. * @ioc: per adapter object
  2698. * @pdev: PCI device struct
  2699. *
  2700. * Return: 0 for success, non-zero for failure.
  2701. */
  2702. static int
  2703. _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
  2704. {
  2705. struct sysinfo s;
  2706. u64 coherent_dma_mask, dma_mask;
  2707. if (ioc->is_mcpu_endpoint || sizeof(dma_addr_t) == 4) {
  2708. ioc->dma_mask = 32;
  2709. coherent_dma_mask = dma_mask = DMA_BIT_MASK(32);
  2710. /* Set 63 bit DMA mask for all SAS3 and SAS35 controllers */
  2711. } else if (ioc->hba_mpi_version_belonged > MPI2_VERSION) {
  2712. ioc->dma_mask = 63;
  2713. coherent_dma_mask = dma_mask = DMA_BIT_MASK(63);
  2714. } else {
  2715. ioc->dma_mask = 64;
  2716. coherent_dma_mask = dma_mask = DMA_BIT_MASK(64);
  2717. }
  2718. if (ioc->use_32bit_dma)
  2719. coherent_dma_mask = DMA_BIT_MASK(32);
  2720. if (dma_set_mask(&pdev->dev, dma_mask) ||
  2721. dma_set_coherent_mask(&pdev->dev, coherent_dma_mask))
  2722. return -ENODEV;
  2723. if (ioc->dma_mask > 32) {
  2724. ioc->base_add_sg_single = &_base_add_sg_single_64;
  2725. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  2726. } else {
  2727. ioc->base_add_sg_single = &_base_add_sg_single_32;
  2728. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  2729. }
  2730. si_meminfo(&s);
  2731. ioc_info(ioc, "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
  2732. ioc->dma_mask, convert_to_kb(s.totalram));
  2733. return 0;
  2734. }
  2735. /**
  2736. * _base_check_enable_msix - checks MSIX capabable.
  2737. * @ioc: per adapter object
  2738. *
  2739. * Check to see if card is capable of MSIX, and set number
  2740. * of available msix vectors
  2741. */
  2742. static int
  2743. _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  2744. {
  2745. int base;
  2746. u16 message_control;
  2747. /* Check whether controller SAS2008 B0 controller,
  2748. * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
  2749. */
  2750. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
  2751. ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
  2752. return -EINVAL;
  2753. }
  2754. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  2755. if (!base) {
  2756. dfailprintk(ioc, ioc_info(ioc, "msix not supported\n"));
  2757. return -EINVAL;
  2758. }
  2759. /* get msix vector count */
  2760. /* NUMA_IO not supported for older controllers */
  2761. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
  2762. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
  2763. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
  2764. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
  2765. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
  2766. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
  2767. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
  2768. ioc->msix_vector_count = 1;
  2769. else {
  2770. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  2771. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  2772. }
  2773. dinitprintk(ioc, ioc_info(ioc, "msix is supported, vector_count(%d)\n",
  2774. ioc->msix_vector_count));
  2775. return 0;
  2776. }
  2777. /**
  2778. * mpt3sas_base_free_irq - free irq
  2779. * @ioc: per adapter object
  2780. *
  2781. * Freeing respective reply_queue from the list.
  2782. */
  2783. void
  2784. mpt3sas_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
  2785. {
  2786. unsigned int irq;
  2787. struct adapter_reply_queue *reply_q, *next;
  2788. if (list_empty(&ioc->reply_queue_list))
  2789. return;
  2790. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  2791. list_del(&reply_q->list);
  2792. if (reply_q->is_iouring_poll_q) {
  2793. kfree(reply_q);
  2794. continue;
  2795. }
  2796. if (ioc->smp_affinity_enable) {
  2797. irq = pci_irq_vector(ioc->pdev, reply_q->msix_index);
  2798. irq_update_affinity_hint(irq, NULL);
  2799. }
  2800. free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
  2801. reply_q);
  2802. kfree(reply_q);
  2803. }
  2804. }
  2805. /**
  2806. * _base_request_irq - request irq
  2807. * @ioc: per adapter object
  2808. * @index: msix index into vector table
  2809. *
  2810. * Inserting respective reply_queue into the list.
  2811. */
  2812. static int
  2813. _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
  2814. {
  2815. struct pci_dev *pdev = ioc->pdev;
  2816. struct adapter_reply_queue *reply_q;
  2817. int r, qid;
  2818. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  2819. if (!reply_q) {
  2820. ioc_err(ioc, "unable to allocate memory %zu!\n",
  2821. sizeof(struct adapter_reply_queue));
  2822. return -ENOMEM;
  2823. }
  2824. reply_q->ioc = ioc;
  2825. reply_q->msix_index = index;
  2826. atomic_set(&reply_q->busy, 0);
  2827. if (index >= ioc->iopoll_q_start_index) {
  2828. qid = index - ioc->iopoll_q_start_index;
  2829. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-mq-poll%d",
  2830. ioc->driver_name, ioc->id, qid);
  2831. reply_q->is_iouring_poll_q = 1;
  2832. ioc->io_uring_poll_queues[qid].reply_q = reply_q;
  2833. goto out;
  2834. }
  2835. if (ioc->msix_enable)
  2836. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  2837. ioc->driver_name, ioc->id, index);
  2838. else
  2839. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  2840. ioc->driver_name, ioc->id);
  2841. r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
  2842. IRQF_SHARED, reply_q->name, reply_q);
  2843. if (r) {
  2844. pr_err("%s: unable to allocate interrupt %d!\n",
  2845. reply_q->name, pci_irq_vector(pdev, index));
  2846. kfree(reply_q);
  2847. return -EBUSY;
  2848. }
  2849. out:
  2850. INIT_LIST_HEAD(&reply_q->list);
  2851. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  2852. return 0;
  2853. }
  2854. /**
  2855. * _base_assign_reply_queues - assigning msix index for each cpu
  2856. * @ioc: per adapter object
  2857. *
  2858. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  2859. */
  2860. static void
  2861. _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  2862. {
  2863. unsigned int cpu, nr_cpus, nr_msix, index = 0, irq;
  2864. struct adapter_reply_queue *reply_q;
  2865. int iopoll_q_count = ioc->reply_queue_count -
  2866. ioc->iopoll_q_start_index;
  2867. const struct cpumask *mask;
  2868. if (!_base_is_controller_msix_enabled(ioc))
  2869. return;
  2870. if (ioc->msix_load_balance)
  2871. return;
  2872. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  2873. nr_cpus = num_online_cpus();
  2874. nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
  2875. ioc->facts.MaxMSIxVectors);
  2876. if (!nr_msix)
  2877. return;
  2878. if (ioc->smp_affinity_enable) {
  2879. /*
  2880. * set irq affinity to local numa node for those irqs
  2881. * corresponding to high iops queues.
  2882. */
  2883. if (ioc->high_iops_queues) {
  2884. mask = cpumask_of_node(dev_to_node(&ioc->pdev->dev));
  2885. for (index = 0; index < ioc->high_iops_queues;
  2886. index++) {
  2887. irq = pci_irq_vector(ioc->pdev, index);
  2888. irq_set_affinity_and_hint(irq, mask);
  2889. }
  2890. }
  2891. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  2892. const cpumask_t *mask;
  2893. if (reply_q->msix_index < ioc->high_iops_queues ||
  2894. reply_q->msix_index >= ioc->iopoll_q_start_index)
  2895. continue;
  2896. mask = pci_irq_get_affinity(ioc->pdev,
  2897. reply_q->msix_index);
  2898. if (!mask) {
  2899. ioc_warn(ioc, "no affinity for msi %x\n",
  2900. reply_q->msix_index);
  2901. goto fall_back;
  2902. }
  2903. for_each_cpu_and(cpu, mask, cpu_online_mask) {
  2904. if (cpu >= ioc->cpu_msix_table_sz)
  2905. break;
  2906. ioc->cpu_msix_table[cpu] = reply_q->msix_index;
  2907. }
  2908. }
  2909. return;
  2910. }
  2911. fall_back:
  2912. cpu = cpumask_first(cpu_online_mask);
  2913. nr_msix -= (ioc->high_iops_queues - iopoll_q_count);
  2914. index = 0;
  2915. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  2916. unsigned int i, group = nr_cpus / nr_msix;
  2917. if (reply_q->msix_index < ioc->high_iops_queues ||
  2918. reply_q->msix_index >= ioc->iopoll_q_start_index)
  2919. continue;
  2920. if (cpu >= nr_cpus)
  2921. break;
  2922. if (index < nr_cpus % nr_msix)
  2923. group++;
  2924. for (i = 0 ; i < group ; i++) {
  2925. ioc->cpu_msix_table[cpu] = reply_q->msix_index;
  2926. cpu = cpumask_next(cpu, cpu_online_mask);
  2927. }
  2928. index++;
  2929. }
  2930. }
  2931. /**
  2932. * _base_check_and_enable_high_iops_queues - enable high iops mode
  2933. * @ioc: per adapter object
  2934. * @hba_msix_vector_count: msix vectors supported by HBA
  2935. *
  2936. * Enable high iops queues only if
  2937. * - HBA is a SEA/AERO controller and
  2938. * - MSI-Xs vector supported by the HBA is 128 and
  2939. * - total CPU count in the system >=16 and
  2940. * - loaded driver with default max_msix_vectors module parameter and
  2941. * - system booted in non kdump mode
  2942. *
  2943. * Return: nothing.
  2944. */
  2945. static void
  2946. _base_check_and_enable_high_iops_queues(struct MPT3SAS_ADAPTER *ioc,
  2947. int hba_msix_vector_count)
  2948. {
  2949. u16 lnksta, speed;
  2950. /*
  2951. * Disable high iops queues if io uring poll queues are enabled.
  2952. */
  2953. if (perf_mode == MPT_PERF_MODE_IOPS ||
  2954. perf_mode == MPT_PERF_MODE_LATENCY ||
  2955. ioc->io_uring_poll_queues) {
  2956. ioc->high_iops_queues = 0;
  2957. return;
  2958. }
  2959. if (perf_mode == MPT_PERF_MODE_DEFAULT) {
  2960. pcie_capability_read_word(ioc->pdev, PCI_EXP_LNKSTA, &lnksta);
  2961. speed = lnksta & PCI_EXP_LNKSTA_CLS;
  2962. if (speed < 0x4) {
  2963. ioc->high_iops_queues = 0;
  2964. return;
  2965. }
  2966. }
  2967. if (!reset_devices && ioc->is_aero_ioc &&
  2968. hba_msix_vector_count == MPT3SAS_GEN35_MAX_MSIX_QUEUES &&
  2969. num_online_cpus() >= MPT3SAS_HIGH_IOPS_REPLY_QUEUES &&
  2970. max_msix_vectors == -1)
  2971. ioc->high_iops_queues = MPT3SAS_HIGH_IOPS_REPLY_QUEUES;
  2972. else
  2973. ioc->high_iops_queues = 0;
  2974. }
  2975. /**
  2976. * mpt3sas_base_disable_msix - disables msix
  2977. * @ioc: per adapter object
  2978. *
  2979. */
  2980. void
  2981. mpt3sas_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
  2982. {
  2983. if (!ioc->msix_enable)
  2984. return;
  2985. pci_free_irq_vectors(ioc->pdev);
  2986. ioc->msix_enable = 0;
  2987. kfree(ioc->io_uring_poll_queues);
  2988. }
  2989. /**
  2990. * _base_alloc_irq_vectors - allocate msix vectors
  2991. * @ioc: per adapter object
  2992. *
  2993. */
  2994. static int
  2995. _base_alloc_irq_vectors(struct MPT3SAS_ADAPTER *ioc)
  2996. {
  2997. int i, irq_flags = PCI_IRQ_MSIX;
  2998. struct irq_affinity desc = { .pre_vectors = ioc->high_iops_queues };
  2999. struct irq_affinity *descp = &desc;
  3000. /*
  3001. * Don't allocate msix vectors for poll_queues.
  3002. * msix_vectors is always within a range of FW supported reply queue.
  3003. */
  3004. int nr_msix_vectors = ioc->iopoll_q_start_index;
  3005. if (ioc->smp_affinity_enable)
  3006. irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
  3007. else
  3008. descp = NULL;
  3009. ioc_info(ioc, " %d %d %d\n", ioc->high_iops_queues,
  3010. ioc->reply_queue_count, nr_msix_vectors);
  3011. i = pci_alloc_irq_vectors_affinity(ioc->pdev,
  3012. ioc->high_iops_queues,
  3013. nr_msix_vectors, irq_flags, descp);
  3014. return i;
  3015. }
  3016. /**
  3017. * _base_enable_msix - enables msix, failback to io_apic
  3018. * @ioc: per adapter object
  3019. *
  3020. */
  3021. static int
  3022. _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  3023. {
  3024. int r;
  3025. int i, local_max_msix_vectors;
  3026. u8 try_msix = 0;
  3027. int iopoll_q_count = 0;
  3028. ioc->msix_load_balance = false;
  3029. if (msix_disable == -1 || msix_disable == 0)
  3030. try_msix = 1;
  3031. if (!try_msix)
  3032. goto try_ioapic;
  3033. if (_base_check_enable_msix(ioc) != 0)
  3034. goto try_ioapic;
  3035. ioc_info(ioc, "MSI-X vectors supported: %d\n", ioc->msix_vector_count);
  3036. pr_info("\t no of cores: %d, max_msix_vectors: %d\n",
  3037. ioc->cpu_count, max_msix_vectors);
  3038. ioc->reply_queue_count =
  3039. min_t(int, ioc->cpu_count, ioc->msix_vector_count);
  3040. if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
  3041. local_max_msix_vectors = (reset_devices) ? 1 : 8;
  3042. else
  3043. local_max_msix_vectors = max_msix_vectors;
  3044. if (local_max_msix_vectors == 0)
  3045. goto try_ioapic;
  3046. /*
  3047. * Enable msix_load_balance only if combined reply queue mode is
  3048. * disabled on SAS3 & above generation HBA devices.
  3049. */
  3050. if (!ioc->combined_reply_queue &&
  3051. ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  3052. ioc_info(ioc,
  3053. "combined ReplyQueue is off, Enabling msix load balance\n");
  3054. ioc->msix_load_balance = true;
  3055. }
  3056. /*
  3057. * smp affinity setting is not need when msix load balance
  3058. * is enabled.
  3059. */
  3060. if (ioc->msix_load_balance)
  3061. ioc->smp_affinity_enable = 0;
  3062. if (!ioc->smp_affinity_enable || ioc->reply_queue_count <= 1)
  3063. ioc->shost->host_tagset = 0;
  3064. /*
  3065. * Enable io uring poll queues only if host_tagset is enabled.
  3066. */
  3067. if (ioc->shost->host_tagset)
  3068. iopoll_q_count = poll_queues;
  3069. if (iopoll_q_count) {
  3070. ioc->io_uring_poll_queues = kcalloc(iopoll_q_count,
  3071. sizeof(struct io_uring_poll_queue), GFP_KERNEL);
  3072. if (!ioc->io_uring_poll_queues)
  3073. iopoll_q_count = 0;
  3074. }
  3075. if (ioc->is_aero_ioc)
  3076. _base_check_and_enable_high_iops_queues(ioc,
  3077. ioc->msix_vector_count);
  3078. /*
  3079. * Add high iops queues count to reply queue count if high iops queues
  3080. * are enabled.
  3081. */
  3082. ioc->reply_queue_count = min_t(int,
  3083. ioc->reply_queue_count + ioc->high_iops_queues,
  3084. ioc->msix_vector_count);
  3085. /*
  3086. * Adjust the reply queue count incase reply queue count
  3087. * exceeds the user provided MSIx vectors count.
  3088. */
  3089. if (local_max_msix_vectors > 0)
  3090. ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
  3091. ioc->reply_queue_count);
  3092. /*
  3093. * Add io uring poll queues count to reply queues count
  3094. * if io uring is enabled in driver.
  3095. */
  3096. if (iopoll_q_count) {
  3097. if (ioc->reply_queue_count < (iopoll_q_count + MPT3_MIN_IRQS))
  3098. iopoll_q_count = 0;
  3099. ioc->reply_queue_count = min_t(int,
  3100. ioc->reply_queue_count + iopoll_q_count,
  3101. ioc->msix_vector_count);
  3102. }
  3103. /*
  3104. * Starting index of io uring poll queues in reply queue list.
  3105. */
  3106. ioc->iopoll_q_start_index =
  3107. ioc->reply_queue_count - iopoll_q_count;
  3108. r = _base_alloc_irq_vectors(ioc);
  3109. if (r < 0) {
  3110. ioc_info(ioc, "pci_alloc_irq_vectors failed (r=%d) !!!\n", r);
  3111. goto try_ioapic;
  3112. }
  3113. /*
  3114. * Adjust the reply queue count if the allocated
  3115. * MSIx vectors is less then the requested number
  3116. * of MSIx vectors.
  3117. */
  3118. if (r < ioc->iopoll_q_start_index) {
  3119. ioc->reply_queue_count = r + iopoll_q_count;
  3120. ioc->iopoll_q_start_index =
  3121. ioc->reply_queue_count - iopoll_q_count;
  3122. }
  3123. ioc->msix_enable = 1;
  3124. for (i = 0; i < ioc->reply_queue_count; i++) {
  3125. r = _base_request_irq(ioc, i);
  3126. if (r) {
  3127. mpt3sas_base_free_irq(ioc);
  3128. mpt3sas_base_disable_msix(ioc);
  3129. goto try_ioapic;
  3130. }
  3131. }
  3132. ioc_info(ioc, "High IOPs queues : %s\n",
  3133. ioc->high_iops_queues ? "enabled" : "disabled");
  3134. return 0;
  3135. /* failback to io_apic interrupt routing */
  3136. try_ioapic:
  3137. ioc->high_iops_queues = 0;
  3138. ioc_info(ioc, "High IOPs queues : disabled\n");
  3139. ioc->reply_queue_count = 1;
  3140. ioc->iopoll_q_start_index = ioc->reply_queue_count - 0;
  3141. r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
  3142. if (r < 0) {
  3143. dfailprintk(ioc,
  3144. ioc_info(ioc, "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
  3145. r));
  3146. } else
  3147. r = _base_request_irq(ioc, 0);
  3148. return r;
  3149. }
  3150. /**
  3151. * mpt3sas_base_unmap_resources - free controller resources
  3152. * @ioc: per adapter object
  3153. */
  3154. static void
  3155. mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
  3156. {
  3157. struct pci_dev *pdev = ioc->pdev;
  3158. dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  3159. mpt3sas_base_free_irq(ioc);
  3160. mpt3sas_base_disable_msix(ioc);
  3161. kfree(ioc->replyPostRegisterIndex);
  3162. ioc->replyPostRegisterIndex = NULL;
  3163. if (ioc->chip_phys) {
  3164. iounmap(ioc->chip);
  3165. ioc->chip_phys = 0;
  3166. }
  3167. if (pci_is_enabled(pdev)) {
  3168. pci_release_selected_regions(ioc->pdev, ioc->bars);
  3169. pci_disable_pcie_error_reporting(pdev);
  3170. pci_disable_device(pdev);
  3171. }
  3172. }
  3173. static int
  3174. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
  3175. /**
  3176. * mpt3sas_base_check_for_fault_and_issue_reset - check if IOC is in fault state
  3177. * and if it is in fault state then issue diag reset.
  3178. * @ioc: per adapter object
  3179. *
  3180. * Return: 0 for success, non-zero for failure.
  3181. */
  3182. int
  3183. mpt3sas_base_check_for_fault_and_issue_reset(struct MPT3SAS_ADAPTER *ioc)
  3184. {
  3185. u32 ioc_state;
  3186. int rc = -EFAULT;
  3187. dinitprintk(ioc, pr_info("%s\n", __func__));
  3188. if (ioc->pci_error_recovery)
  3189. return 0;
  3190. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  3191. dhsprintk(ioc, pr_info("%s: ioc_state(0x%08x)\n", __func__, ioc_state));
  3192. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3193. mpt3sas_print_fault_code(ioc, ioc_state &
  3194. MPI2_DOORBELL_DATA_MASK);
  3195. mpt3sas_base_mask_interrupts(ioc);
  3196. rc = _base_diag_reset(ioc);
  3197. } else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
  3198. MPI2_IOC_STATE_COREDUMP) {
  3199. mpt3sas_print_coredump_info(ioc, ioc_state &
  3200. MPI2_DOORBELL_DATA_MASK);
  3201. mpt3sas_base_wait_for_coredump_completion(ioc, __func__);
  3202. mpt3sas_base_mask_interrupts(ioc);
  3203. rc = _base_diag_reset(ioc);
  3204. }
  3205. return rc;
  3206. }
  3207. /**
  3208. * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
  3209. * @ioc: per adapter object
  3210. *
  3211. * Return: 0 for success, non-zero for failure.
  3212. */
  3213. int
  3214. mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
  3215. {
  3216. struct pci_dev *pdev = ioc->pdev;
  3217. u32 memap_sz;
  3218. u32 pio_sz;
  3219. int i, r = 0, rc;
  3220. u64 pio_chip = 0;
  3221. phys_addr_t chip_phys = 0;
  3222. struct adapter_reply_queue *reply_q;
  3223. int iopoll_q_count = 0;
  3224. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  3225. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  3226. if (pci_enable_device_mem(pdev)) {
  3227. ioc_warn(ioc, "pci_enable_device_mem: failed\n");
  3228. ioc->bars = 0;
  3229. return -ENODEV;
  3230. }
  3231. if (pci_request_selected_regions(pdev, ioc->bars,
  3232. ioc->driver_name)) {
  3233. ioc_warn(ioc, "pci_request_selected_regions: failed\n");
  3234. ioc->bars = 0;
  3235. r = -ENODEV;
  3236. goto out_fail;
  3237. }
  3238. /* AER (Advanced Error Reporting) hooks */
  3239. pci_enable_pcie_error_reporting(pdev);
  3240. pci_set_master(pdev);
  3241. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  3242. ioc_warn(ioc, "no suitable DMA mask for %s\n", pci_name(pdev));
  3243. r = -ENODEV;
  3244. goto out_fail;
  3245. }
  3246. for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
  3247. (!memap_sz || !pio_sz); i++) {
  3248. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  3249. if (pio_sz)
  3250. continue;
  3251. pio_chip = (u64)pci_resource_start(pdev, i);
  3252. pio_sz = pci_resource_len(pdev, i);
  3253. } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  3254. if (memap_sz)
  3255. continue;
  3256. ioc->chip_phys = pci_resource_start(pdev, i);
  3257. chip_phys = ioc->chip_phys;
  3258. memap_sz = pci_resource_len(pdev, i);
  3259. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  3260. }
  3261. }
  3262. if (ioc->chip == NULL) {
  3263. ioc_err(ioc,
  3264. "unable to map adapter memory! or resource not found\n");
  3265. r = -EINVAL;
  3266. goto out_fail;
  3267. }
  3268. mpt3sas_base_mask_interrupts(ioc);
  3269. r = _base_get_ioc_facts(ioc);
  3270. if (r) {
  3271. rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
  3272. if (rc || (_base_get_ioc_facts(ioc)))
  3273. goto out_fail;
  3274. }
  3275. if (!ioc->rdpq_array_enable_assigned) {
  3276. ioc->rdpq_array_enable = ioc->rdpq_array_capable;
  3277. ioc->rdpq_array_enable_assigned = 1;
  3278. }
  3279. r = _base_enable_msix(ioc);
  3280. if (r)
  3281. goto out_fail;
  3282. iopoll_q_count = ioc->reply_queue_count - ioc->iopoll_q_start_index;
  3283. for (i = 0; i < iopoll_q_count; i++) {
  3284. atomic_set(&ioc->io_uring_poll_queues[i].busy, 0);
  3285. atomic_set(&ioc->io_uring_poll_queues[i].pause, 0);
  3286. }
  3287. if (!ioc->is_driver_loading)
  3288. _base_init_irqpolls(ioc);
  3289. /* Use the Combined reply queue feature only for SAS3 C0 & higher
  3290. * revision HBAs and also only when reply queue count is greater than 8
  3291. */
  3292. if (ioc->combined_reply_queue) {
  3293. /* Determine the Supplemental Reply Post Host Index Registers
  3294. * Addresse. Supplemental Reply Post Host Index Registers
  3295. * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
  3296. * each register is at offset bytes of
  3297. * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
  3298. */
  3299. ioc->replyPostRegisterIndex = kcalloc(
  3300. ioc->combined_reply_index_count,
  3301. sizeof(resource_size_t *), GFP_KERNEL);
  3302. if (!ioc->replyPostRegisterIndex) {
  3303. ioc_err(ioc,
  3304. "allocation for replyPostRegisterIndex failed!\n");
  3305. r = -ENOMEM;
  3306. goto out_fail;
  3307. }
  3308. for (i = 0; i < ioc->combined_reply_index_count; i++) {
  3309. ioc->replyPostRegisterIndex[i] =
  3310. (resource_size_t __iomem *)
  3311. ((u8 __force *)&ioc->chip->Doorbell +
  3312. MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
  3313. (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
  3314. }
  3315. }
  3316. if (ioc->is_warpdrive) {
  3317. ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
  3318. &ioc->chip->ReplyPostHostIndex;
  3319. for (i = 1; i < ioc->cpu_msix_table_sz; i++)
  3320. ioc->reply_post_host_index[i] =
  3321. (resource_size_t __iomem *)
  3322. ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
  3323. * 4)));
  3324. }
  3325. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3326. if (reply_q->msix_index >= ioc->iopoll_q_start_index) {
  3327. pr_info("%s: enabled: index: %d\n",
  3328. reply_q->name, reply_q->msix_index);
  3329. continue;
  3330. }
  3331. pr_info("%s: %s enabled: IRQ %d\n",
  3332. reply_q->name,
  3333. ioc->msix_enable ? "PCI-MSI-X" : "IO-APIC",
  3334. pci_irq_vector(ioc->pdev, reply_q->msix_index));
  3335. }
  3336. ioc_info(ioc, "iomem(%pap), mapped(0x%p), size(%d)\n",
  3337. &chip_phys, ioc->chip, memap_sz);
  3338. ioc_info(ioc, "ioport(0x%016llx), size(%d)\n",
  3339. (unsigned long long)pio_chip, pio_sz);
  3340. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  3341. pci_save_state(pdev);
  3342. return 0;
  3343. out_fail:
  3344. mpt3sas_base_unmap_resources(ioc);
  3345. return r;
  3346. }
  3347. /**
  3348. * mpt3sas_base_get_msg_frame - obtain request mf pointer
  3349. * @ioc: per adapter object
  3350. * @smid: system request message index(smid zero is invalid)
  3351. *
  3352. * Return: virt pointer to message frame.
  3353. */
  3354. void *
  3355. mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3356. {
  3357. return (void *)(ioc->request + (smid * ioc->request_sz));
  3358. }
  3359. /**
  3360. * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
  3361. * @ioc: per adapter object
  3362. * @smid: system request message index
  3363. *
  3364. * Return: virt pointer to sense buffer.
  3365. */
  3366. void *
  3367. mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3368. {
  3369. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  3370. }
  3371. /**
  3372. * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
  3373. * @ioc: per adapter object
  3374. * @smid: system request message index
  3375. *
  3376. * Return: phys pointer to the low 32bit address of the sense buffer.
  3377. */
  3378. __le32
  3379. mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3380. {
  3381. return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
  3382. SCSI_SENSE_BUFFERSIZE));
  3383. }
  3384. /**
  3385. * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
  3386. * @ioc: per adapter object
  3387. * @smid: system request message index
  3388. *
  3389. * Return: virt pointer to a PCIe SGL.
  3390. */
  3391. void *
  3392. mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3393. {
  3394. return (void *)(ioc->pcie_sg_lookup[smid - 1].pcie_sgl);
  3395. }
  3396. /**
  3397. * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
  3398. * @ioc: per adapter object
  3399. * @smid: system request message index
  3400. *
  3401. * Return: phys pointer to the address of the PCIe buffer.
  3402. */
  3403. dma_addr_t
  3404. mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3405. {
  3406. return ioc->pcie_sg_lookup[smid - 1].pcie_sgl_dma;
  3407. }
  3408. /**
  3409. * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
  3410. * @ioc: per adapter object
  3411. * @phys_addr: lower 32 physical addr of the reply
  3412. *
  3413. * Converts 32bit lower physical addr into a virt address.
  3414. */
  3415. void *
  3416. mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
  3417. {
  3418. if (!phys_addr)
  3419. return NULL;
  3420. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  3421. }
  3422. /**
  3423. * _base_get_msix_index - get the msix index
  3424. * @ioc: per adapter object
  3425. * @scmd: scsi_cmnd object
  3426. *
  3427. * Return: msix index of general reply queues,
  3428. * i.e. reply queue on which IO request's reply
  3429. * should be posted by the HBA firmware.
  3430. */
  3431. static inline u8
  3432. _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc,
  3433. struct scsi_cmnd *scmd)
  3434. {
  3435. /* Enables reply_queue load balancing */
  3436. if (ioc->msix_load_balance)
  3437. return ioc->reply_queue_count ?
  3438. base_mod64(atomic64_add_return(1,
  3439. &ioc->total_io_cnt), ioc->reply_queue_count) : 0;
  3440. if (scmd && ioc->shost->nr_hw_queues > 1) {
  3441. u32 tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
  3442. return blk_mq_unique_tag_to_hwq(tag) +
  3443. ioc->high_iops_queues;
  3444. }
  3445. return ioc->cpu_msix_table[raw_smp_processor_id()];
  3446. }
  3447. /**
  3448. * _base_get_high_iops_msix_index - get the msix index of
  3449. * high iops queues
  3450. * @ioc: per adapter object
  3451. * @scmd: scsi_cmnd object
  3452. *
  3453. * Return: msix index of high iops reply queues.
  3454. * i.e. high iops reply queue on which IO request's
  3455. * reply should be posted by the HBA firmware.
  3456. */
  3457. static inline u8
  3458. _base_get_high_iops_msix_index(struct MPT3SAS_ADAPTER *ioc,
  3459. struct scsi_cmnd *scmd)
  3460. {
  3461. /**
  3462. * Round robin the IO interrupts among the high iops
  3463. * reply queues in terms of batch count 16 when outstanding
  3464. * IOs on the target device is >=8.
  3465. */
  3466. if (scsi_device_busy(scmd->device) > MPT3SAS_DEVICE_HIGH_IOPS_DEPTH)
  3467. return base_mod64((
  3468. atomic64_add_return(1, &ioc->high_iops_outstanding) /
  3469. MPT3SAS_HIGH_IOPS_BATCH_COUNT),
  3470. MPT3SAS_HIGH_IOPS_REPLY_QUEUES);
  3471. return _base_get_msix_index(ioc, scmd);
  3472. }
  3473. /**
  3474. * mpt3sas_base_get_smid - obtain a free smid from internal queue
  3475. * @ioc: per adapter object
  3476. * @cb_idx: callback index
  3477. *
  3478. * Return: smid (zero is invalid)
  3479. */
  3480. u16
  3481. mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  3482. {
  3483. unsigned long flags;
  3484. struct request_tracker *request;
  3485. u16 smid;
  3486. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3487. if (list_empty(&ioc->internal_free_list)) {
  3488. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3489. ioc_err(ioc, "%s: smid not available\n", __func__);
  3490. return 0;
  3491. }
  3492. request = list_entry(ioc->internal_free_list.next,
  3493. struct request_tracker, tracker_list);
  3494. request->cb_idx = cb_idx;
  3495. smid = request->smid;
  3496. list_del(&request->tracker_list);
  3497. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3498. return smid;
  3499. }
  3500. /**
  3501. * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  3502. * @ioc: per adapter object
  3503. * @cb_idx: callback index
  3504. * @scmd: pointer to scsi command object
  3505. *
  3506. * Return: smid (zero is invalid)
  3507. */
  3508. u16
  3509. mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
  3510. struct scsi_cmnd *scmd)
  3511. {
  3512. struct scsiio_tracker *request = scsi_cmd_priv(scmd);
  3513. u16 smid;
  3514. u32 tag, unique_tag;
  3515. unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
  3516. tag = blk_mq_unique_tag_to_tag(unique_tag);
  3517. /*
  3518. * Store hw queue number corresponding to the tag.
  3519. * This hw queue number is used later to determine
  3520. * the unique_tag using the logic below. This unique_tag
  3521. * is used to retrieve the scmd pointer corresponding
  3522. * to tag using scsi_host_find_tag() API.
  3523. *
  3524. * tag = smid - 1;
  3525. * unique_tag = ioc->io_queue_num[tag] << BLK_MQ_UNIQUE_TAG_BITS | tag;
  3526. */
  3527. ioc->io_queue_num[tag] = blk_mq_unique_tag_to_hwq(unique_tag);
  3528. smid = tag + 1;
  3529. request->cb_idx = cb_idx;
  3530. request->smid = smid;
  3531. request->scmd = scmd;
  3532. INIT_LIST_HEAD(&request->chain_list);
  3533. return smid;
  3534. }
  3535. /**
  3536. * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  3537. * @ioc: per adapter object
  3538. * @cb_idx: callback index
  3539. *
  3540. * Return: smid (zero is invalid)
  3541. */
  3542. u16
  3543. mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  3544. {
  3545. unsigned long flags;
  3546. struct request_tracker *request;
  3547. u16 smid;
  3548. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3549. if (list_empty(&ioc->hpr_free_list)) {
  3550. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3551. return 0;
  3552. }
  3553. request = list_entry(ioc->hpr_free_list.next,
  3554. struct request_tracker, tracker_list);
  3555. request->cb_idx = cb_idx;
  3556. smid = request->smid;
  3557. list_del(&request->tracker_list);
  3558. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3559. return smid;
  3560. }
  3561. static void
  3562. _base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
  3563. {
  3564. /*
  3565. * See _wait_for_commands_to_complete() call with regards to this code.
  3566. */
  3567. if (ioc->shost_recovery && ioc->pending_io_count) {
  3568. ioc->pending_io_count = scsi_host_busy(ioc->shost);
  3569. if (ioc->pending_io_count == 0)
  3570. wake_up(&ioc->reset_wq);
  3571. }
  3572. }
  3573. void mpt3sas_base_clear_st(struct MPT3SAS_ADAPTER *ioc,
  3574. struct scsiio_tracker *st)
  3575. {
  3576. if (WARN_ON(st->smid == 0))
  3577. return;
  3578. st->cb_idx = 0xFF;
  3579. st->direct_io = 0;
  3580. st->scmd = NULL;
  3581. atomic_set(&ioc->chain_lookup[st->smid - 1].chain_offset, 0);
  3582. st->smid = 0;
  3583. }
  3584. /**
  3585. * mpt3sas_base_free_smid - put smid back on free_list
  3586. * @ioc: per adapter object
  3587. * @smid: system request message index
  3588. */
  3589. void
  3590. mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3591. {
  3592. unsigned long flags;
  3593. int i;
  3594. if (smid < ioc->hi_priority_smid) {
  3595. struct scsiio_tracker *st;
  3596. void *request;
  3597. st = _get_st_from_smid(ioc, smid);
  3598. if (!st) {
  3599. _base_recovery_check(ioc);
  3600. return;
  3601. }
  3602. /* Clear MPI request frame */
  3603. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3604. memset(request, 0, ioc->request_sz);
  3605. mpt3sas_base_clear_st(ioc, st);
  3606. _base_recovery_check(ioc);
  3607. ioc->io_queue_num[smid - 1] = 0;
  3608. return;
  3609. }
  3610. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3611. if (smid < ioc->internal_smid) {
  3612. /* hi-priority */
  3613. i = smid - ioc->hi_priority_smid;
  3614. ioc->hpr_lookup[i].cb_idx = 0xFF;
  3615. list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
  3616. } else if (smid <= ioc->hba_queue_depth) {
  3617. /* internal queue */
  3618. i = smid - ioc->internal_smid;
  3619. ioc->internal_lookup[i].cb_idx = 0xFF;
  3620. list_add(&ioc->internal_lookup[i].tracker_list,
  3621. &ioc->internal_free_list);
  3622. }
  3623. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3624. }
  3625. /**
  3626. * _base_mpi_ep_writeq - 32 bit write to MMIO
  3627. * @b: data payload
  3628. * @addr: address in MMIO space
  3629. * @writeq_lock: spin lock
  3630. *
  3631. * This special handling for MPI EP to take care of 32 bit
  3632. * environment where its not quarenteed to send the entire word
  3633. * in one transfer.
  3634. */
  3635. static inline void
  3636. _base_mpi_ep_writeq(__u64 b, volatile void __iomem *addr,
  3637. spinlock_t *writeq_lock)
  3638. {
  3639. unsigned long flags;
  3640. spin_lock_irqsave(writeq_lock, flags);
  3641. __raw_writel((u32)(b), addr);
  3642. __raw_writel((u32)(b >> 32), (addr + 4));
  3643. spin_unlock_irqrestore(writeq_lock, flags);
  3644. }
  3645. /**
  3646. * _base_writeq - 64 bit write to MMIO
  3647. * @b: data payload
  3648. * @addr: address in MMIO space
  3649. * @writeq_lock: spin lock
  3650. *
  3651. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  3652. * care of 32 bit environment where its not quarenteed to send the entire word
  3653. * in one transfer.
  3654. */
  3655. #if defined(writeq) && defined(CONFIG_64BIT)
  3656. static inline void
  3657. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  3658. {
  3659. wmb();
  3660. __raw_writeq(b, addr);
  3661. barrier();
  3662. }
  3663. #else
  3664. static inline void
  3665. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  3666. {
  3667. _base_mpi_ep_writeq(b, addr, writeq_lock);
  3668. }
  3669. #endif
  3670. /**
  3671. * _base_set_and_get_msix_index - get the msix index and assign to msix_io
  3672. * variable of scsi tracker
  3673. * @ioc: per adapter object
  3674. * @smid: system request message index
  3675. *
  3676. * Return: msix index.
  3677. */
  3678. static u8
  3679. _base_set_and_get_msix_index(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3680. {
  3681. struct scsiio_tracker *st = NULL;
  3682. if (smid < ioc->hi_priority_smid)
  3683. st = _get_st_from_smid(ioc, smid);
  3684. if (st == NULL)
  3685. return _base_get_msix_index(ioc, NULL);
  3686. st->msix_io = ioc->get_msix_index_for_smlio(ioc, st->scmd);
  3687. return st->msix_io;
  3688. }
  3689. /**
  3690. * _base_put_smid_mpi_ep_scsi_io - send SCSI_IO request to firmware
  3691. * @ioc: per adapter object
  3692. * @smid: system request message index
  3693. * @handle: device handle
  3694. */
  3695. static void
  3696. _base_put_smid_mpi_ep_scsi_io(struct MPT3SAS_ADAPTER *ioc,
  3697. u16 smid, u16 handle)
  3698. {
  3699. Mpi2RequestDescriptorUnion_t descriptor;
  3700. u64 *request = (u64 *)&descriptor;
  3701. void *mpi_req_iomem;
  3702. __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
  3703. _clone_sg_entries(ioc, (void *) mfp, smid);
  3704. mpi_req_iomem = (void __force *)ioc->chip +
  3705. MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
  3706. _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
  3707. ioc->request_sz);
  3708. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  3709. descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3710. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  3711. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  3712. descriptor.SCSIIO.LMID = 0;
  3713. _base_mpi_ep_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3714. &ioc->scsi_lookup_lock);
  3715. }
  3716. /**
  3717. * _base_put_smid_scsi_io - send SCSI_IO request to firmware
  3718. * @ioc: per adapter object
  3719. * @smid: system request message index
  3720. * @handle: device handle
  3721. */
  3722. static void
  3723. _base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
  3724. {
  3725. Mpi2RequestDescriptorUnion_t descriptor;
  3726. u64 *request = (u64 *)&descriptor;
  3727. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  3728. descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3729. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  3730. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  3731. descriptor.SCSIIO.LMID = 0;
  3732. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3733. &ioc->scsi_lookup_lock);
  3734. }
  3735. /**
  3736. * _base_put_smid_fast_path - send fast path request to firmware
  3737. * @ioc: per adapter object
  3738. * @smid: system request message index
  3739. * @handle: device handle
  3740. */
  3741. static void
  3742. _base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3743. u16 handle)
  3744. {
  3745. Mpi2RequestDescriptorUnion_t descriptor;
  3746. u64 *request = (u64 *)&descriptor;
  3747. descriptor.SCSIIO.RequestFlags =
  3748. MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  3749. descriptor.SCSIIO.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3750. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  3751. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  3752. descriptor.SCSIIO.LMID = 0;
  3753. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3754. &ioc->scsi_lookup_lock);
  3755. }
  3756. /**
  3757. * _base_put_smid_hi_priority - send Task Management request to firmware
  3758. * @ioc: per adapter object
  3759. * @smid: system request message index
  3760. * @msix_task: msix_task will be same as msix of IO in case of task abort else 0
  3761. */
  3762. static void
  3763. _base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3764. u16 msix_task)
  3765. {
  3766. Mpi2RequestDescriptorUnion_t descriptor;
  3767. void *mpi_req_iomem;
  3768. u64 *request;
  3769. if (ioc->is_mcpu_endpoint) {
  3770. __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
  3771. /* TBD 256 is offset within sys register. */
  3772. mpi_req_iomem = (void __force *)ioc->chip
  3773. + MPI_FRAME_START_OFFSET
  3774. + (smid * ioc->request_sz);
  3775. _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
  3776. ioc->request_sz);
  3777. }
  3778. request = (u64 *)&descriptor;
  3779. descriptor.HighPriority.RequestFlags =
  3780. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  3781. descriptor.HighPriority.MSIxIndex = msix_task;
  3782. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  3783. descriptor.HighPriority.LMID = 0;
  3784. descriptor.HighPriority.Reserved1 = 0;
  3785. if (ioc->is_mcpu_endpoint)
  3786. _base_mpi_ep_writeq(*request,
  3787. &ioc->chip->RequestDescriptorPostLow,
  3788. &ioc->scsi_lookup_lock);
  3789. else
  3790. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3791. &ioc->scsi_lookup_lock);
  3792. }
  3793. /**
  3794. * mpt3sas_base_put_smid_nvme_encap - send NVMe encapsulated request to
  3795. * firmware
  3796. * @ioc: per adapter object
  3797. * @smid: system request message index
  3798. */
  3799. void
  3800. mpt3sas_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3801. {
  3802. Mpi2RequestDescriptorUnion_t descriptor;
  3803. u64 *request = (u64 *)&descriptor;
  3804. descriptor.Default.RequestFlags =
  3805. MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
  3806. descriptor.Default.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3807. descriptor.Default.SMID = cpu_to_le16(smid);
  3808. descriptor.Default.LMID = 0;
  3809. descriptor.Default.DescriptorTypeDependent = 0;
  3810. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3811. &ioc->scsi_lookup_lock);
  3812. }
  3813. /**
  3814. * _base_put_smid_default - Default, primarily used for config pages
  3815. * @ioc: per adapter object
  3816. * @smid: system request message index
  3817. */
  3818. static void
  3819. _base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3820. {
  3821. Mpi2RequestDescriptorUnion_t descriptor;
  3822. void *mpi_req_iomem;
  3823. u64 *request;
  3824. if (ioc->is_mcpu_endpoint) {
  3825. __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
  3826. _clone_sg_entries(ioc, (void *) mfp, smid);
  3827. /* TBD 256 is offset within sys register */
  3828. mpi_req_iomem = (void __force *)ioc->chip +
  3829. MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
  3830. _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
  3831. ioc->request_sz);
  3832. }
  3833. request = (u64 *)&descriptor;
  3834. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  3835. descriptor.Default.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3836. descriptor.Default.SMID = cpu_to_le16(smid);
  3837. descriptor.Default.LMID = 0;
  3838. descriptor.Default.DescriptorTypeDependent = 0;
  3839. if (ioc->is_mcpu_endpoint)
  3840. _base_mpi_ep_writeq(*request,
  3841. &ioc->chip->RequestDescriptorPostLow,
  3842. &ioc->scsi_lookup_lock);
  3843. else
  3844. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3845. &ioc->scsi_lookup_lock);
  3846. }
  3847. /**
  3848. * _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
  3849. * Atomic Request Descriptor
  3850. * @ioc: per adapter object
  3851. * @smid: system request message index
  3852. * @handle: device handle, unused in this function, for function type match
  3853. *
  3854. * Return: nothing.
  3855. */
  3856. static void
  3857. _base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3858. u16 handle)
  3859. {
  3860. Mpi26AtomicRequestDescriptor_t descriptor;
  3861. u32 *request = (u32 *)&descriptor;
  3862. descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  3863. descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3864. descriptor.SMID = cpu_to_le16(smid);
  3865. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  3866. }
  3867. /**
  3868. * _base_put_smid_fast_path_atomic - send fast path request to firmware
  3869. * using Atomic Request Descriptor
  3870. * @ioc: per adapter object
  3871. * @smid: system request message index
  3872. * @handle: device handle, unused in this function, for function type match
  3873. * Return: nothing
  3874. */
  3875. static void
  3876. _base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3877. u16 handle)
  3878. {
  3879. Mpi26AtomicRequestDescriptor_t descriptor;
  3880. u32 *request = (u32 *)&descriptor;
  3881. descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  3882. descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3883. descriptor.SMID = cpu_to_le16(smid);
  3884. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  3885. }
  3886. /**
  3887. * _base_put_smid_hi_priority_atomic - send Task Management request to
  3888. * firmware using Atomic Request Descriptor
  3889. * @ioc: per adapter object
  3890. * @smid: system request message index
  3891. * @msix_task: msix_task will be same as msix of IO in case of task abort else 0
  3892. *
  3893. * Return: nothing.
  3894. */
  3895. static void
  3896. _base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3897. u16 msix_task)
  3898. {
  3899. Mpi26AtomicRequestDescriptor_t descriptor;
  3900. u32 *request = (u32 *)&descriptor;
  3901. descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  3902. descriptor.MSIxIndex = msix_task;
  3903. descriptor.SMID = cpu_to_le16(smid);
  3904. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  3905. }
  3906. /**
  3907. * _base_put_smid_default_atomic - Default, primarily used for config pages
  3908. * use Atomic Request Descriptor
  3909. * @ioc: per adapter object
  3910. * @smid: system request message index
  3911. *
  3912. * Return: nothing.
  3913. */
  3914. static void
  3915. _base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3916. {
  3917. Mpi26AtomicRequestDescriptor_t descriptor;
  3918. u32 *request = (u32 *)&descriptor;
  3919. descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  3920. descriptor.MSIxIndex = _base_set_and_get_msix_index(ioc, smid);
  3921. descriptor.SMID = cpu_to_le16(smid);
  3922. writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
  3923. }
  3924. /**
  3925. * _base_display_OEMs_branding - Display branding string
  3926. * @ioc: per adapter object
  3927. */
  3928. static void
  3929. _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
  3930. {
  3931. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  3932. return;
  3933. switch (ioc->pdev->subsystem_vendor) {
  3934. case PCI_VENDOR_ID_INTEL:
  3935. switch (ioc->pdev->device) {
  3936. case MPI2_MFGPAGE_DEVID_SAS2008:
  3937. switch (ioc->pdev->subsystem_device) {
  3938. case MPT2SAS_INTEL_RMS2LL080_SSDID:
  3939. ioc_info(ioc, "%s\n",
  3940. MPT2SAS_INTEL_RMS2LL080_BRANDING);
  3941. break;
  3942. case MPT2SAS_INTEL_RMS2LL040_SSDID:
  3943. ioc_info(ioc, "%s\n",
  3944. MPT2SAS_INTEL_RMS2LL040_BRANDING);
  3945. break;
  3946. case MPT2SAS_INTEL_SSD910_SSDID:
  3947. ioc_info(ioc, "%s\n",
  3948. MPT2SAS_INTEL_SSD910_BRANDING);
  3949. break;
  3950. default:
  3951. ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
  3952. ioc->pdev->subsystem_device);
  3953. break;
  3954. }
  3955. break;
  3956. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  3957. switch (ioc->pdev->subsystem_device) {
  3958. case MPT2SAS_INTEL_RS25GB008_SSDID:
  3959. ioc_info(ioc, "%s\n",
  3960. MPT2SAS_INTEL_RS25GB008_BRANDING);
  3961. break;
  3962. case MPT2SAS_INTEL_RMS25JB080_SSDID:
  3963. ioc_info(ioc, "%s\n",
  3964. MPT2SAS_INTEL_RMS25JB080_BRANDING);
  3965. break;
  3966. case MPT2SAS_INTEL_RMS25JB040_SSDID:
  3967. ioc_info(ioc, "%s\n",
  3968. MPT2SAS_INTEL_RMS25JB040_BRANDING);
  3969. break;
  3970. case MPT2SAS_INTEL_RMS25KB080_SSDID:
  3971. ioc_info(ioc, "%s\n",
  3972. MPT2SAS_INTEL_RMS25KB080_BRANDING);
  3973. break;
  3974. case MPT2SAS_INTEL_RMS25KB040_SSDID:
  3975. ioc_info(ioc, "%s\n",
  3976. MPT2SAS_INTEL_RMS25KB040_BRANDING);
  3977. break;
  3978. case MPT2SAS_INTEL_RMS25LB040_SSDID:
  3979. ioc_info(ioc, "%s\n",
  3980. MPT2SAS_INTEL_RMS25LB040_BRANDING);
  3981. break;
  3982. case MPT2SAS_INTEL_RMS25LB080_SSDID:
  3983. ioc_info(ioc, "%s\n",
  3984. MPT2SAS_INTEL_RMS25LB080_BRANDING);
  3985. break;
  3986. default:
  3987. ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
  3988. ioc->pdev->subsystem_device);
  3989. break;
  3990. }
  3991. break;
  3992. case MPI25_MFGPAGE_DEVID_SAS3008:
  3993. switch (ioc->pdev->subsystem_device) {
  3994. case MPT3SAS_INTEL_RMS3JC080_SSDID:
  3995. ioc_info(ioc, "%s\n",
  3996. MPT3SAS_INTEL_RMS3JC080_BRANDING);
  3997. break;
  3998. case MPT3SAS_INTEL_RS3GC008_SSDID:
  3999. ioc_info(ioc, "%s\n",
  4000. MPT3SAS_INTEL_RS3GC008_BRANDING);
  4001. break;
  4002. case MPT3SAS_INTEL_RS3FC044_SSDID:
  4003. ioc_info(ioc, "%s\n",
  4004. MPT3SAS_INTEL_RS3FC044_BRANDING);
  4005. break;
  4006. case MPT3SAS_INTEL_RS3UC080_SSDID:
  4007. ioc_info(ioc, "%s\n",
  4008. MPT3SAS_INTEL_RS3UC080_BRANDING);
  4009. break;
  4010. default:
  4011. ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
  4012. ioc->pdev->subsystem_device);
  4013. break;
  4014. }
  4015. break;
  4016. default:
  4017. ioc_info(ioc, "Intel(R) Controller: Subsystem ID: 0x%X\n",
  4018. ioc->pdev->subsystem_device);
  4019. break;
  4020. }
  4021. break;
  4022. case PCI_VENDOR_ID_DELL:
  4023. switch (ioc->pdev->device) {
  4024. case MPI2_MFGPAGE_DEVID_SAS2008:
  4025. switch (ioc->pdev->subsystem_device) {
  4026. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  4027. ioc_info(ioc, "%s\n",
  4028. MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
  4029. break;
  4030. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  4031. ioc_info(ioc, "%s\n",
  4032. MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
  4033. break;
  4034. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  4035. ioc_info(ioc, "%s\n",
  4036. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
  4037. break;
  4038. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  4039. ioc_info(ioc, "%s\n",
  4040. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
  4041. break;
  4042. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  4043. ioc_info(ioc, "%s\n",
  4044. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
  4045. break;
  4046. case MPT2SAS_DELL_PERC_H200_SSDID:
  4047. ioc_info(ioc, "%s\n",
  4048. MPT2SAS_DELL_PERC_H200_BRANDING);
  4049. break;
  4050. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  4051. ioc_info(ioc, "%s\n",
  4052. MPT2SAS_DELL_6GBPS_SAS_BRANDING);
  4053. break;
  4054. default:
  4055. ioc_info(ioc, "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
  4056. ioc->pdev->subsystem_device);
  4057. break;
  4058. }
  4059. break;
  4060. case MPI25_MFGPAGE_DEVID_SAS3008:
  4061. switch (ioc->pdev->subsystem_device) {
  4062. case MPT3SAS_DELL_12G_HBA_SSDID:
  4063. ioc_info(ioc, "%s\n",
  4064. MPT3SAS_DELL_12G_HBA_BRANDING);
  4065. break;
  4066. default:
  4067. ioc_info(ioc, "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
  4068. ioc->pdev->subsystem_device);
  4069. break;
  4070. }
  4071. break;
  4072. default:
  4073. ioc_info(ioc, "Dell HBA: Subsystem ID: 0x%X\n",
  4074. ioc->pdev->subsystem_device);
  4075. break;
  4076. }
  4077. break;
  4078. case PCI_VENDOR_ID_CISCO:
  4079. switch (ioc->pdev->device) {
  4080. case MPI25_MFGPAGE_DEVID_SAS3008:
  4081. switch (ioc->pdev->subsystem_device) {
  4082. case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
  4083. ioc_info(ioc, "%s\n",
  4084. MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
  4085. break;
  4086. case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
  4087. ioc_info(ioc, "%s\n",
  4088. MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
  4089. break;
  4090. case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
  4091. ioc_info(ioc, "%s\n",
  4092. MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
  4093. break;
  4094. default:
  4095. ioc_info(ioc, "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
  4096. ioc->pdev->subsystem_device);
  4097. break;
  4098. }
  4099. break;
  4100. case MPI25_MFGPAGE_DEVID_SAS3108_1:
  4101. switch (ioc->pdev->subsystem_device) {
  4102. case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
  4103. ioc_info(ioc, "%s\n",
  4104. MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
  4105. break;
  4106. case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
  4107. ioc_info(ioc, "%s\n",
  4108. MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING);
  4109. break;
  4110. default:
  4111. ioc_info(ioc, "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
  4112. ioc->pdev->subsystem_device);
  4113. break;
  4114. }
  4115. break;
  4116. default:
  4117. ioc_info(ioc, "Cisco SAS HBA: Subsystem ID: 0x%X\n",
  4118. ioc->pdev->subsystem_device);
  4119. break;
  4120. }
  4121. break;
  4122. case MPT2SAS_HP_3PAR_SSVID:
  4123. switch (ioc->pdev->device) {
  4124. case MPI2_MFGPAGE_DEVID_SAS2004:
  4125. switch (ioc->pdev->subsystem_device) {
  4126. case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
  4127. ioc_info(ioc, "%s\n",
  4128. MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
  4129. break;
  4130. default:
  4131. ioc_info(ioc, "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
  4132. ioc->pdev->subsystem_device);
  4133. break;
  4134. }
  4135. break;
  4136. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  4137. switch (ioc->pdev->subsystem_device) {
  4138. case MPT2SAS_HP_2_4_INTERNAL_SSDID:
  4139. ioc_info(ioc, "%s\n",
  4140. MPT2SAS_HP_2_4_INTERNAL_BRANDING);
  4141. break;
  4142. case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
  4143. ioc_info(ioc, "%s\n",
  4144. MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
  4145. break;
  4146. case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
  4147. ioc_info(ioc, "%s\n",
  4148. MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
  4149. break;
  4150. case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
  4151. ioc_info(ioc, "%s\n",
  4152. MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
  4153. break;
  4154. default:
  4155. ioc_info(ioc, "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
  4156. ioc->pdev->subsystem_device);
  4157. break;
  4158. }
  4159. break;
  4160. default:
  4161. ioc_info(ioc, "HP SAS HBA: Subsystem ID: 0x%X\n",
  4162. ioc->pdev->subsystem_device);
  4163. break;
  4164. }
  4165. break;
  4166. default:
  4167. break;
  4168. }
  4169. }
  4170. /**
  4171. * _base_display_fwpkg_version - sends FWUpload request to pull FWPkg
  4172. * version from FW Image Header.
  4173. * @ioc: per adapter object
  4174. *
  4175. * Return: 0 for success, non-zero for failure.
  4176. */
  4177. static int
  4178. _base_display_fwpkg_version(struct MPT3SAS_ADAPTER *ioc)
  4179. {
  4180. Mpi2FWImageHeader_t *fw_img_hdr;
  4181. Mpi26ComponentImageHeader_t *cmp_img_hdr;
  4182. Mpi25FWUploadRequest_t *mpi_request;
  4183. Mpi2FWUploadReply_t mpi_reply;
  4184. int r = 0, issue_diag_reset = 0;
  4185. u32 package_version = 0;
  4186. void *fwpkg_data = NULL;
  4187. dma_addr_t fwpkg_data_dma;
  4188. u16 smid, ioc_status;
  4189. size_t data_length;
  4190. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  4191. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  4192. ioc_err(ioc, "%s: internal command already in use\n", __func__);
  4193. return -EAGAIN;
  4194. }
  4195. data_length = sizeof(Mpi2FWImageHeader_t);
  4196. fwpkg_data = dma_alloc_coherent(&ioc->pdev->dev, data_length,
  4197. &fwpkg_data_dma, GFP_KERNEL);
  4198. if (!fwpkg_data) {
  4199. ioc_err(ioc,
  4200. "Memory allocation for fwpkg data failed at %s:%d/%s()!\n",
  4201. __FILE__, __LINE__, __func__);
  4202. return -ENOMEM;
  4203. }
  4204. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  4205. if (!smid) {
  4206. ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
  4207. r = -EAGAIN;
  4208. goto out;
  4209. }
  4210. ioc->base_cmds.status = MPT3_CMD_PENDING;
  4211. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4212. ioc->base_cmds.smid = smid;
  4213. memset(mpi_request, 0, sizeof(Mpi25FWUploadRequest_t));
  4214. mpi_request->Function = MPI2_FUNCTION_FW_UPLOAD;
  4215. mpi_request->ImageType = MPI2_FW_UPLOAD_ITYPE_FW_FLASH;
  4216. mpi_request->ImageSize = cpu_to_le32(data_length);
  4217. ioc->build_sg(ioc, &mpi_request->SGL, 0, 0, fwpkg_data_dma,
  4218. data_length);
  4219. init_completion(&ioc->base_cmds.done);
  4220. ioc->put_smid_default(ioc, smid);
  4221. /* Wait for 15 seconds */
  4222. wait_for_completion_timeout(&ioc->base_cmds.done,
  4223. FW_IMG_HDR_READ_TIMEOUT*HZ);
  4224. ioc_info(ioc, "%s: complete\n", __func__);
  4225. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  4226. ioc_err(ioc, "%s: timeout\n", __func__);
  4227. _debug_dump_mf(mpi_request,
  4228. sizeof(Mpi25FWUploadRequest_t)/4);
  4229. issue_diag_reset = 1;
  4230. } else {
  4231. memset(&mpi_reply, 0, sizeof(Mpi2FWUploadReply_t));
  4232. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID) {
  4233. memcpy(&mpi_reply, ioc->base_cmds.reply,
  4234. sizeof(Mpi2FWUploadReply_t));
  4235. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4236. MPI2_IOCSTATUS_MASK;
  4237. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  4238. fw_img_hdr = (Mpi2FWImageHeader_t *)fwpkg_data;
  4239. if (le32_to_cpu(fw_img_hdr->Signature) ==
  4240. MPI26_IMAGE_HEADER_SIGNATURE0_MPI26) {
  4241. cmp_img_hdr =
  4242. (Mpi26ComponentImageHeader_t *)
  4243. (fwpkg_data);
  4244. package_version =
  4245. le32_to_cpu(
  4246. cmp_img_hdr->ApplicationSpecific);
  4247. } else
  4248. package_version =
  4249. le32_to_cpu(
  4250. fw_img_hdr->PackageVersion.Word);
  4251. if (package_version)
  4252. ioc_info(ioc,
  4253. "FW Package Ver(%02d.%02d.%02d.%02d)\n",
  4254. ((package_version) & 0xFF000000) >> 24,
  4255. ((package_version) & 0x00FF0000) >> 16,
  4256. ((package_version) & 0x0000FF00) >> 8,
  4257. (package_version) & 0x000000FF);
  4258. } else {
  4259. _debug_dump_mf(&mpi_reply,
  4260. sizeof(Mpi2FWUploadReply_t)/4);
  4261. }
  4262. }
  4263. }
  4264. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4265. out:
  4266. if (fwpkg_data)
  4267. dma_free_coherent(&ioc->pdev->dev, data_length, fwpkg_data,
  4268. fwpkg_data_dma);
  4269. if (issue_diag_reset) {
  4270. if (ioc->drv_internal_flags & MPT_DRV_INTERNAL_FIRST_PE_ISSUED)
  4271. return -EFAULT;
  4272. if (mpt3sas_base_check_for_fault_and_issue_reset(ioc))
  4273. return -EFAULT;
  4274. r = -EAGAIN;
  4275. }
  4276. return r;
  4277. }
  4278. /**
  4279. * _base_display_ioc_capabilities - Display IOC's capabilities.
  4280. * @ioc: per adapter object
  4281. */
  4282. static void
  4283. _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
  4284. {
  4285. int i = 0;
  4286. char desc[17] = {0};
  4287. u32 iounit_pg1_flags;
  4288. u32 bios_version;
  4289. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  4290. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  4291. ioc_info(ioc, "%s: FWVersion(%02d.%02d.%02d.%02d), ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  4292. desc,
  4293. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  4294. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  4295. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  4296. ioc->facts.FWVersion.Word & 0x000000FF,
  4297. ioc->pdev->revision,
  4298. (bios_version & 0xFF000000) >> 24,
  4299. (bios_version & 0x00FF0000) >> 16,
  4300. (bios_version & 0x0000FF00) >> 8,
  4301. bios_version & 0x000000FF);
  4302. _base_display_OEMs_branding(ioc);
  4303. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
  4304. pr_info("%sNVMe", i ? "," : "");
  4305. i++;
  4306. }
  4307. ioc_info(ioc, "Protocol=(");
  4308. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  4309. pr_cont("Initiator");
  4310. i++;
  4311. }
  4312. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  4313. pr_cont("%sTarget", i ? "," : "");
  4314. i++;
  4315. }
  4316. i = 0;
  4317. pr_cont("), Capabilities=(");
  4318. if (!ioc->hide_ir_msg) {
  4319. if (ioc->facts.IOCCapabilities &
  4320. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  4321. pr_cont("Raid");
  4322. i++;
  4323. }
  4324. }
  4325. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  4326. pr_cont("%sTLR", i ? "," : "");
  4327. i++;
  4328. }
  4329. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  4330. pr_cont("%sMulticast", i ? "," : "");
  4331. i++;
  4332. }
  4333. if (ioc->facts.IOCCapabilities &
  4334. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  4335. pr_cont("%sBIDI Target", i ? "," : "");
  4336. i++;
  4337. }
  4338. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  4339. pr_cont("%sEEDP", i ? "," : "");
  4340. i++;
  4341. }
  4342. if (ioc->facts.IOCCapabilities &
  4343. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  4344. pr_cont("%sSnapshot Buffer", i ? "," : "");
  4345. i++;
  4346. }
  4347. if (ioc->facts.IOCCapabilities &
  4348. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  4349. pr_cont("%sDiag Trace Buffer", i ? "," : "");
  4350. i++;
  4351. }
  4352. if (ioc->facts.IOCCapabilities &
  4353. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  4354. pr_cont("%sDiag Extended Buffer", i ? "," : "");
  4355. i++;
  4356. }
  4357. if (ioc->facts.IOCCapabilities &
  4358. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  4359. pr_cont("%sTask Set Full", i ? "," : "");
  4360. i++;
  4361. }
  4362. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  4363. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  4364. pr_cont("%sNCQ", i ? "," : "");
  4365. i++;
  4366. }
  4367. pr_cont(")\n");
  4368. }
  4369. /**
  4370. * mpt3sas_base_update_missing_delay - change the missing delay timers
  4371. * @ioc: per adapter object
  4372. * @device_missing_delay: amount of time till device is reported missing
  4373. * @io_missing_delay: interval IO is returned when there is a missing device
  4374. *
  4375. * Passed on the command line, this function will modify the device missing
  4376. * delay, as well as the io missing delay. This should be called at driver
  4377. * load time.
  4378. */
  4379. void
  4380. mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
  4381. u16 device_missing_delay, u8 io_missing_delay)
  4382. {
  4383. u16 dmd, dmd_new, dmd_orignal;
  4384. u8 io_missing_delay_original;
  4385. u16 sz;
  4386. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  4387. Mpi2ConfigReply_t mpi_reply;
  4388. u8 num_phys = 0;
  4389. u16 ioc_status;
  4390. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  4391. if (!num_phys)
  4392. return;
  4393. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  4394. sizeof(Mpi2SasIOUnit1PhyData_t));
  4395. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  4396. if (!sas_iounit_pg1) {
  4397. ioc_err(ioc, "failure at %s:%d/%s()!\n",
  4398. __FILE__, __LINE__, __func__);
  4399. goto out;
  4400. }
  4401. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  4402. sas_iounit_pg1, sz))) {
  4403. ioc_err(ioc, "failure at %s:%d/%s()!\n",
  4404. __FILE__, __LINE__, __func__);
  4405. goto out;
  4406. }
  4407. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4408. MPI2_IOCSTATUS_MASK;
  4409. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4410. ioc_err(ioc, "failure at %s:%d/%s()!\n",
  4411. __FILE__, __LINE__, __func__);
  4412. goto out;
  4413. }
  4414. /* device missing delay */
  4415. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  4416. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  4417. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  4418. else
  4419. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  4420. dmd_orignal = dmd;
  4421. if (device_missing_delay > 0x7F) {
  4422. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  4423. device_missing_delay;
  4424. dmd = dmd / 16;
  4425. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  4426. } else
  4427. dmd = device_missing_delay;
  4428. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  4429. /* io missing delay */
  4430. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  4431. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  4432. if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  4433. sz)) {
  4434. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  4435. dmd_new = (dmd &
  4436. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  4437. else
  4438. dmd_new =
  4439. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  4440. ioc_info(ioc, "device_missing_delay: old(%d), new(%d)\n",
  4441. dmd_orignal, dmd_new);
  4442. ioc_info(ioc, "ioc_missing_delay: old(%d), new(%d)\n",
  4443. io_missing_delay_original,
  4444. io_missing_delay);
  4445. ioc->device_missing_delay = dmd_new;
  4446. ioc->io_missing_delay = io_missing_delay;
  4447. }
  4448. out:
  4449. kfree(sas_iounit_pg1);
  4450. }
  4451. /**
  4452. * _base_update_ioc_page1_inlinewith_perf_mode - Update IOC Page1 fields
  4453. * according to performance mode.
  4454. * @ioc : per adapter object
  4455. *
  4456. * Return: zero on success; otherwise return EAGAIN error code asking the
  4457. * caller to retry.
  4458. */
  4459. static int
  4460. _base_update_ioc_page1_inlinewith_perf_mode(struct MPT3SAS_ADAPTER *ioc)
  4461. {
  4462. Mpi2IOCPage1_t ioc_pg1;
  4463. Mpi2ConfigReply_t mpi_reply;
  4464. int rc;
  4465. rc = mpt3sas_config_get_ioc_pg1(ioc, &mpi_reply, &ioc->ioc_pg1_copy);
  4466. if (rc)
  4467. return rc;
  4468. memcpy(&ioc_pg1, &ioc->ioc_pg1_copy, sizeof(Mpi2IOCPage1_t));
  4469. switch (perf_mode) {
  4470. case MPT_PERF_MODE_DEFAULT:
  4471. case MPT_PERF_MODE_BALANCED:
  4472. if (ioc->high_iops_queues) {
  4473. ioc_info(ioc,
  4474. "Enable interrupt coalescing only for first\t"
  4475. "%d reply queues\n",
  4476. MPT3SAS_HIGH_IOPS_REPLY_QUEUES);
  4477. /*
  4478. * If 31st bit is zero then interrupt coalescing is
  4479. * enabled for all reply descriptor post queues.
  4480. * If 31st bit is set to one then user can
  4481. * enable/disable interrupt coalescing on per reply
  4482. * descriptor post queue group(8) basis. So to enable
  4483. * interrupt coalescing only on first reply descriptor
  4484. * post queue group 31st bit and zero th bit is enabled.
  4485. */
  4486. ioc_pg1.ProductSpecific = cpu_to_le32(0x80000000 |
  4487. ((1 << MPT3SAS_HIGH_IOPS_REPLY_QUEUES/8) - 1));
  4488. rc = mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
  4489. if (rc)
  4490. return rc;
  4491. ioc_info(ioc, "performance mode: balanced\n");
  4492. return 0;
  4493. }
  4494. fallthrough;
  4495. case MPT_PERF_MODE_LATENCY:
  4496. /*
  4497. * Enable interrupt coalescing on all reply queues
  4498. * with timeout value 0xA
  4499. */
  4500. ioc_pg1.CoalescingTimeout = cpu_to_le32(0xa);
  4501. ioc_pg1.Flags |= cpu_to_le32(MPI2_IOCPAGE1_REPLY_COALESCING);
  4502. ioc_pg1.ProductSpecific = 0;
  4503. rc = mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
  4504. if (rc)
  4505. return rc;
  4506. ioc_info(ioc, "performance mode: latency\n");
  4507. break;
  4508. case MPT_PERF_MODE_IOPS:
  4509. /*
  4510. * Enable interrupt coalescing on all reply queues.
  4511. */
  4512. ioc_info(ioc,
  4513. "performance mode: iops with coalescing timeout: 0x%x\n",
  4514. le32_to_cpu(ioc_pg1.CoalescingTimeout));
  4515. ioc_pg1.Flags |= cpu_to_le32(MPI2_IOCPAGE1_REPLY_COALESCING);
  4516. ioc_pg1.ProductSpecific = 0;
  4517. rc = mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply, &ioc_pg1);
  4518. if (rc)
  4519. return rc;
  4520. break;
  4521. }
  4522. return 0;
  4523. }
  4524. /**
  4525. * _base_get_event_diag_triggers - get event diag trigger values from
  4526. * persistent pages
  4527. * @ioc : per adapter object
  4528. *
  4529. * Return: nothing.
  4530. */
  4531. static int
  4532. _base_get_event_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
  4533. {
  4534. Mpi26DriverTriggerPage2_t trigger_pg2;
  4535. struct SL_WH_EVENT_TRIGGER_T *event_tg;
  4536. MPI26_DRIVER_MPI_EVENT_TIGGER_ENTRY *mpi_event_tg;
  4537. Mpi2ConfigReply_t mpi_reply;
  4538. int r = 0, i = 0;
  4539. u16 count = 0;
  4540. u16 ioc_status;
  4541. r = mpt3sas_config_get_driver_trigger_pg2(ioc, &mpi_reply,
  4542. &trigger_pg2);
  4543. if (r)
  4544. return r;
  4545. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4546. MPI2_IOCSTATUS_MASK;
  4547. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4548. dinitprintk(ioc,
  4549. ioc_err(ioc,
  4550. "%s: Failed to get trigger pg2, ioc_status(0x%04x)\n",
  4551. __func__, ioc_status));
  4552. return 0;
  4553. }
  4554. if (le16_to_cpu(trigger_pg2.NumMPIEventTrigger)) {
  4555. count = le16_to_cpu(trigger_pg2.NumMPIEventTrigger);
  4556. count = min_t(u16, NUM_VALID_ENTRIES, count);
  4557. ioc->diag_trigger_event.ValidEntries = count;
  4558. event_tg = &ioc->diag_trigger_event.EventTriggerEntry[0];
  4559. mpi_event_tg = &trigger_pg2.MPIEventTriggers[0];
  4560. for (i = 0; i < count; i++) {
  4561. event_tg->EventValue = le16_to_cpu(
  4562. mpi_event_tg->MPIEventCode);
  4563. event_tg->LogEntryQualifier = le16_to_cpu(
  4564. mpi_event_tg->MPIEventCodeSpecific);
  4565. event_tg++;
  4566. mpi_event_tg++;
  4567. }
  4568. }
  4569. return 0;
  4570. }
  4571. /**
  4572. * _base_get_scsi_diag_triggers - get scsi diag trigger values from
  4573. * persistent pages
  4574. * @ioc : per adapter object
  4575. *
  4576. * Return: 0 on success; otherwise return failure status.
  4577. */
  4578. static int
  4579. _base_get_scsi_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
  4580. {
  4581. Mpi26DriverTriggerPage3_t trigger_pg3;
  4582. struct SL_WH_SCSI_TRIGGER_T *scsi_tg;
  4583. MPI26_DRIVER_SCSI_SENSE_TIGGER_ENTRY *mpi_scsi_tg;
  4584. Mpi2ConfigReply_t mpi_reply;
  4585. int r = 0, i = 0;
  4586. u16 count = 0;
  4587. u16 ioc_status;
  4588. r = mpt3sas_config_get_driver_trigger_pg3(ioc, &mpi_reply,
  4589. &trigger_pg3);
  4590. if (r)
  4591. return r;
  4592. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4593. MPI2_IOCSTATUS_MASK;
  4594. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4595. dinitprintk(ioc,
  4596. ioc_err(ioc,
  4597. "%s: Failed to get trigger pg3, ioc_status(0x%04x)\n",
  4598. __func__, ioc_status));
  4599. return 0;
  4600. }
  4601. if (le16_to_cpu(trigger_pg3.NumSCSISenseTrigger)) {
  4602. count = le16_to_cpu(trigger_pg3.NumSCSISenseTrigger);
  4603. count = min_t(u16, NUM_VALID_ENTRIES, count);
  4604. ioc->diag_trigger_scsi.ValidEntries = count;
  4605. scsi_tg = &ioc->diag_trigger_scsi.SCSITriggerEntry[0];
  4606. mpi_scsi_tg = &trigger_pg3.SCSISenseTriggers[0];
  4607. for (i = 0; i < count; i++) {
  4608. scsi_tg->ASCQ = mpi_scsi_tg->ASCQ;
  4609. scsi_tg->ASC = mpi_scsi_tg->ASC;
  4610. scsi_tg->SenseKey = mpi_scsi_tg->SenseKey;
  4611. scsi_tg++;
  4612. mpi_scsi_tg++;
  4613. }
  4614. }
  4615. return 0;
  4616. }
  4617. /**
  4618. * _base_get_mpi_diag_triggers - get mpi diag trigger values from
  4619. * persistent pages
  4620. * @ioc : per adapter object
  4621. *
  4622. * Return: 0 on success; otherwise return failure status.
  4623. */
  4624. static int
  4625. _base_get_mpi_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
  4626. {
  4627. Mpi26DriverTriggerPage4_t trigger_pg4;
  4628. struct SL_WH_MPI_TRIGGER_T *status_tg;
  4629. MPI26_DRIVER_IOCSTATUS_LOGINFO_TIGGER_ENTRY *mpi_status_tg;
  4630. Mpi2ConfigReply_t mpi_reply;
  4631. int r = 0, i = 0;
  4632. u16 count = 0;
  4633. u16 ioc_status;
  4634. r = mpt3sas_config_get_driver_trigger_pg4(ioc, &mpi_reply,
  4635. &trigger_pg4);
  4636. if (r)
  4637. return r;
  4638. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4639. MPI2_IOCSTATUS_MASK;
  4640. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4641. dinitprintk(ioc,
  4642. ioc_err(ioc,
  4643. "%s: Failed to get trigger pg4, ioc_status(0x%04x)\n",
  4644. __func__, ioc_status));
  4645. return 0;
  4646. }
  4647. if (le16_to_cpu(trigger_pg4.NumIOCStatusLogInfoTrigger)) {
  4648. count = le16_to_cpu(trigger_pg4.NumIOCStatusLogInfoTrigger);
  4649. count = min_t(u16, NUM_VALID_ENTRIES, count);
  4650. ioc->diag_trigger_mpi.ValidEntries = count;
  4651. status_tg = &ioc->diag_trigger_mpi.MPITriggerEntry[0];
  4652. mpi_status_tg = &trigger_pg4.IOCStatusLoginfoTriggers[0];
  4653. for (i = 0; i < count; i++) {
  4654. status_tg->IOCStatus = le16_to_cpu(
  4655. mpi_status_tg->IOCStatus);
  4656. status_tg->IocLogInfo = le32_to_cpu(
  4657. mpi_status_tg->LogInfo);
  4658. status_tg++;
  4659. mpi_status_tg++;
  4660. }
  4661. }
  4662. return 0;
  4663. }
  4664. /**
  4665. * _base_get_master_diag_triggers - get master diag trigger values from
  4666. * persistent pages
  4667. * @ioc : per adapter object
  4668. *
  4669. * Return: nothing.
  4670. */
  4671. static int
  4672. _base_get_master_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
  4673. {
  4674. Mpi26DriverTriggerPage1_t trigger_pg1;
  4675. Mpi2ConfigReply_t mpi_reply;
  4676. int r;
  4677. u16 ioc_status;
  4678. r = mpt3sas_config_get_driver_trigger_pg1(ioc, &mpi_reply,
  4679. &trigger_pg1);
  4680. if (r)
  4681. return r;
  4682. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4683. MPI2_IOCSTATUS_MASK;
  4684. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4685. dinitprintk(ioc,
  4686. ioc_err(ioc,
  4687. "%s: Failed to get trigger pg1, ioc_status(0x%04x)\n",
  4688. __func__, ioc_status));
  4689. return 0;
  4690. }
  4691. if (le16_to_cpu(trigger_pg1.NumMasterTrigger))
  4692. ioc->diag_trigger_master.MasterData |=
  4693. le32_to_cpu(
  4694. trigger_pg1.MasterTriggers[0].MasterTriggerFlags);
  4695. return 0;
  4696. }
  4697. /**
  4698. * _base_check_for_trigger_pages_support - checks whether HBA FW supports
  4699. * driver trigger pages or not
  4700. * @ioc : per adapter object
  4701. * @trigger_flags : address where trigger page0's TriggerFlags value is copied
  4702. *
  4703. * Return: trigger flags mask if HBA FW supports driver trigger pages;
  4704. * otherwise returns %-EFAULT if driver trigger pages are not supported by FW or
  4705. * return EAGAIN if diag reset occurred due to FW fault and asking the
  4706. * caller to retry the command.
  4707. *
  4708. */
  4709. static int
  4710. _base_check_for_trigger_pages_support(struct MPT3SAS_ADAPTER *ioc, u32 *trigger_flags)
  4711. {
  4712. Mpi26DriverTriggerPage0_t trigger_pg0;
  4713. int r = 0;
  4714. Mpi2ConfigReply_t mpi_reply;
  4715. u16 ioc_status;
  4716. r = mpt3sas_config_get_driver_trigger_pg0(ioc, &mpi_reply,
  4717. &trigger_pg0);
  4718. if (r)
  4719. return r;
  4720. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4721. MPI2_IOCSTATUS_MASK;
  4722. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4723. return -EFAULT;
  4724. *trigger_flags = le16_to_cpu(trigger_pg0.TriggerFlags);
  4725. return 0;
  4726. }
  4727. /**
  4728. * _base_get_diag_triggers - Retrieve diag trigger values from
  4729. * persistent pages.
  4730. * @ioc : per adapter object
  4731. *
  4732. * Return: zero on success; otherwise return EAGAIN error codes
  4733. * asking the caller to retry.
  4734. */
  4735. static int
  4736. _base_get_diag_triggers(struct MPT3SAS_ADAPTER *ioc)
  4737. {
  4738. int trigger_flags;
  4739. int r;
  4740. /*
  4741. * Default setting of master trigger.
  4742. */
  4743. ioc->diag_trigger_master.MasterData =
  4744. (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
  4745. r = _base_check_for_trigger_pages_support(ioc, &trigger_flags);
  4746. if (r) {
  4747. if (r == -EAGAIN)
  4748. return r;
  4749. /*
  4750. * Don't go for error handling when FW doesn't support
  4751. * driver trigger pages.
  4752. */
  4753. return 0;
  4754. }
  4755. ioc->supports_trigger_pages = 1;
  4756. /*
  4757. * Retrieve master diag trigger values from driver trigger pg1
  4758. * if master trigger bit enabled in TriggerFlags.
  4759. */
  4760. if ((u16)trigger_flags &
  4761. MPI26_DRIVER_TRIGGER0_FLAG_MASTER_TRIGGER_VALID) {
  4762. r = _base_get_master_diag_triggers(ioc);
  4763. if (r)
  4764. return r;
  4765. }
  4766. /*
  4767. * Retrieve event diag trigger values from driver trigger pg2
  4768. * if event trigger bit enabled in TriggerFlags.
  4769. */
  4770. if ((u16)trigger_flags &
  4771. MPI26_DRIVER_TRIGGER0_FLAG_MPI_EVENT_TRIGGER_VALID) {
  4772. r = _base_get_event_diag_triggers(ioc);
  4773. if (r)
  4774. return r;
  4775. }
  4776. /*
  4777. * Retrieve scsi diag trigger values from driver trigger pg3
  4778. * if scsi trigger bit enabled in TriggerFlags.
  4779. */
  4780. if ((u16)trigger_flags &
  4781. MPI26_DRIVER_TRIGGER0_FLAG_SCSI_SENSE_TRIGGER_VALID) {
  4782. r = _base_get_scsi_diag_triggers(ioc);
  4783. if (r)
  4784. return r;
  4785. }
  4786. /*
  4787. * Retrieve mpi error diag trigger values from driver trigger pg4
  4788. * if loginfo trigger bit enabled in TriggerFlags.
  4789. */
  4790. if ((u16)trigger_flags &
  4791. MPI26_DRIVER_TRIGGER0_FLAG_LOGINFO_TRIGGER_VALID) {
  4792. r = _base_get_mpi_diag_triggers(ioc);
  4793. if (r)
  4794. return r;
  4795. }
  4796. return 0;
  4797. }
  4798. /**
  4799. * _base_update_diag_trigger_pages - Update the driver trigger pages after
  4800. * online FW update, in case updated FW supports driver
  4801. * trigger pages.
  4802. * @ioc : per adapter object
  4803. *
  4804. * Return: nothing.
  4805. */
  4806. static void
  4807. _base_update_diag_trigger_pages(struct MPT3SAS_ADAPTER *ioc)
  4808. {
  4809. if (ioc->diag_trigger_master.MasterData)
  4810. mpt3sas_config_update_driver_trigger_pg1(ioc,
  4811. &ioc->diag_trigger_master, 1);
  4812. if (ioc->diag_trigger_event.ValidEntries)
  4813. mpt3sas_config_update_driver_trigger_pg2(ioc,
  4814. &ioc->diag_trigger_event, 1);
  4815. if (ioc->diag_trigger_scsi.ValidEntries)
  4816. mpt3sas_config_update_driver_trigger_pg3(ioc,
  4817. &ioc->diag_trigger_scsi, 1);
  4818. if (ioc->diag_trigger_mpi.ValidEntries)
  4819. mpt3sas_config_update_driver_trigger_pg4(ioc,
  4820. &ioc->diag_trigger_mpi, 1);
  4821. }
  4822. /**
  4823. * _base_assign_fw_reported_qd - Get FW reported QD for SAS/SATA devices.
  4824. * - On failure set default QD values.
  4825. * @ioc : per adapter object
  4826. *
  4827. * Returns 0 for success, non-zero for failure.
  4828. *
  4829. */
  4830. static int _base_assign_fw_reported_qd(struct MPT3SAS_ADAPTER *ioc)
  4831. {
  4832. Mpi2ConfigReply_t mpi_reply;
  4833. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  4834. Mpi26PCIeIOUnitPage1_t pcie_iounit_pg1;
  4835. u16 depth;
  4836. int sz;
  4837. int rc = 0;
  4838. ioc->max_wideport_qd = MPT3SAS_SAS_QUEUE_DEPTH;
  4839. ioc->max_narrowport_qd = MPT3SAS_SAS_QUEUE_DEPTH;
  4840. ioc->max_sata_qd = MPT3SAS_SATA_QUEUE_DEPTH;
  4841. ioc->max_nvme_qd = MPT3SAS_NVME_QUEUE_DEPTH;
  4842. if (!ioc->is_gen35_ioc)
  4843. goto out;
  4844. /* sas iounit page 1 */
  4845. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData);
  4846. sas_iounit_pg1 = kzalloc(sizeof(Mpi2SasIOUnitPage1_t), GFP_KERNEL);
  4847. if (!sas_iounit_pg1) {
  4848. pr_err("%s: failure at %s:%d/%s()!\n",
  4849. ioc->name, __FILE__, __LINE__, __func__);
  4850. return rc;
  4851. }
  4852. rc = mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  4853. sas_iounit_pg1, sz);
  4854. if (rc) {
  4855. pr_err("%s: failure at %s:%d/%s()!\n",
  4856. ioc->name, __FILE__, __LINE__, __func__);
  4857. goto out;
  4858. }
  4859. depth = le16_to_cpu(sas_iounit_pg1->SASWideMaxQueueDepth);
  4860. ioc->max_wideport_qd = (depth ? depth : MPT3SAS_SAS_QUEUE_DEPTH);
  4861. depth = le16_to_cpu(sas_iounit_pg1->SASNarrowMaxQueueDepth);
  4862. ioc->max_narrowport_qd = (depth ? depth : MPT3SAS_SAS_QUEUE_DEPTH);
  4863. depth = sas_iounit_pg1->SATAMaxQDepth;
  4864. ioc->max_sata_qd = (depth ? depth : MPT3SAS_SATA_QUEUE_DEPTH);
  4865. /* pcie iounit page 1 */
  4866. rc = mpt3sas_config_get_pcie_iounit_pg1(ioc, &mpi_reply,
  4867. &pcie_iounit_pg1, sizeof(Mpi26PCIeIOUnitPage1_t));
  4868. if (rc) {
  4869. pr_err("%s: failure at %s:%d/%s()!\n",
  4870. ioc->name, __FILE__, __LINE__, __func__);
  4871. goto out;
  4872. }
  4873. ioc->max_nvme_qd = (le16_to_cpu(pcie_iounit_pg1.NVMeMaxQueueDepth)) ?
  4874. (le16_to_cpu(pcie_iounit_pg1.NVMeMaxQueueDepth)) :
  4875. MPT3SAS_NVME_QUEUE_DEPTH;
  4876. out:
  4877. dinitprintk(ioc, pr_err(
  4878. "MaxWidePortQD: 0x%x MaxNarrowPortQD: 0x%x MaxSataQD: 0x%x MaxNvmeQD: 0x%x\n",
  4879. ioc->max_wideport_qd, ioc->max_narrowport_qd,
  4880. ioc->max_sata_qd, ioc->max_nvme_qd));
  4881. kfree(sas_iounit_pg1);
  4882. return rc;
  4883. }
  4884. /**
  4885. * mpt3sas_atto_validate_nvram - validate the ATTO nvram read from mfg pg1
  4886. *
  4887. * @ioc : per adapter object
  4888. * @n : ptr to the ATTO nvram structure
  4889. * Return: 0 for success, non-zero for failure.
  4890. */
  4891. static int
  4892. mpt3sas_atto_validate_nvram(struct MPT3SAS_ADAPTER *ioc,
  4893. struct ATTO_SAS_NVRAM *n)
  4894. {
  4895. int r = -EINVAL;
  4896. union ATTO_SAS_ADDRESS *s1;
  4897. u32 len;
  4898. u8 *pb;
  4899. u8 ckSum;
  4900. /* validate nvram checksum */
  4901. pb = (u8 *) n;
  4902. ckSum = ATTO_SASNVR_CKSUM_SEED;
  4903. len = sizeof(struct ATTO_SAS_NVRAM);
  4904. while (len--)
  4905. ckSum = ckSum + pb[len];
  4906. if (ckSum) {
  4907. ioc_err(ioc, "Invalid ATTO NVRAM checksum\n");
  4908. return r;
  4909. }
  4910. s1 = (union ATTO_SAS_ADDRESS *) n->SasAddr;
  4911. if (n->Signature[0] != 'E'
  4912. || n->Signature[1] != 'S'
  4913. || n->Signature[2] != 'A'
  4914. || n->Signature[3] != 'S')
  4915. ioc_err(ioc, "Invalid ATTO NVRAM signature\n");
  4916. else if (n->Version > ATTO_SASNVR_VERSION)
  4917. ioc_info(ioc, "Invalid ATTO NVRAM version");
  4918. else if ((n->SasAddr[7] & (ATTO_SAS_ADDR_ALIGN - 1))
  4919. || s1->b[0] != 0x50
  4920. || s1->b[1] != 0x01
  4921. || s1->b[2] != 0x08
  4922. || (s1->b[3] & 0xF0) != 0x60
  4923. || ((s1->b[3] & 0x0F) | le32_to_cpu(s1->d[1])) == 0) {
  4924. ioc_err(ioc, "Invalid ATTO SAS address\n");
  4925. } else
  4926. r = 0;
  4927. return r;
  4928. }
  4929. /**
  4930. * mpt3sas_atto_get_sas_addr - get the ATTO SAS address from mfg page 1
  4931. *
  4932. * @ioc : per adapter object
  4933. * @*sas_addr : return sas address
  4934. * Return: 0 for success, non-zero for failure.
  4935. */
  4936. static int
  4937. mpt3sas_atto_get_sas_addr(struct MPT3SAS_ADAPTER *ioc, union ATTO_SAS_ADDRESS *sas_addr)
  4938. {
  4939. Mpi2ManufacturingPage1_t mfg_pg1;
  4940. Mpi2ConfigReply_t mpi_reply;
  4941. struct ATTO_SAS_NVRAM *nvram;
  4942. int r;
  4943. __be64 addr;
  4944. r = mpt3sas_config_get_manufacturing_pg1(ioc, &mpi_reply, &mfg_pg1);
  4945. if (r) {
  4946. ioc_err(ioc, "Failed to read manufacturing page 1\n");
  4947. return r;
  4948. }
  4949. /* validate nvram */
  4950. nvram = (struct ATTO_SAS_NVRAM *) mfg_pg1.VPD;
  4951. r = mpt3sas_atto_validate_nvram(ioc, nvram);
  4952. if (r)
  4953. return r;
  4954. addr = *((__be64 *) nvram->SasAddr);
  4955. sas_addr->q = cpu_to_le64(be64_to_cpu(addr));
  4956. return r;
  4957. }
  4958. /**
  4959. * mpt3sas_atto_init - perform initializaion for ATTO branded
  4960. * adapter.
  4961. * @ioc : per adapter object
  4962. *5
  4963. * Return: 0 for success, non-zero for failure.
  4964. */
  4965. static int
  4966. mpt3sas_atto_init(struct MPT3SAS_ADAPTER *ioc)
  4967. {
  4968. int sz = 0;
  4969. Mpi2BiosPage4_t *bios_pg4 = NULL;
  4970. Mpi2ConfigReply_t mpi_reply;
  4971. int r;
  4972. int ix;
  4973. union ATTO_SAS_ADDRESS sas_addr;
  4974. union ATTO_SAS_ADDRESS temp;
  4975. union ATTO_SAS_ADDRESS bias;
  4976. r = mpt3sas_atto_get_sas_addr(ioc, &sas_addr);
  4977. if (r)
  4978. return r;
  4979. /* get header first to get size */
  4980. r = mpt3sas_config_get_bios_pg4(ioc, &mpi_reply, NULL, 0);
  4981. if (r) {
  4982. ioc_err(ioc, "Failed to read ATTO bios page 4 header.\n");
  4983. return r;
  4984. }
  4985. sz = mpi_reply.Header.PageLength * sizeof(u32);
  4986. bios_pg4 = kzalloc(sz, GFP_KERNEL);
  4987. if (!bios_pg4) {
  4988. ioc_err(ioc, "Failed to allocate memory for ATTO bios page.\n");
  4989. return -ENOMEM;
  4990. }
  4991. /* read bios page 4 */
  4992. r = mpt3sas_config_get_bios_pg4(ioc, &mpi_reply, bios_pg4, sz);
  4993. if (r) {
  4994. ioc_err(ioc, "Failed to read ATTO bios page 4\n");
  4995. goto out;
  4996. }
  4997. /* Update bios page 4 with the ATTO WWID */
  4998. bias.q = sas_addr.q;
  4999. bias.b[7] += ATTO_SAS_ADDR_DEVNAME_BIAS;
  5000. for (ix = 0; ix < bios_pg4->NumPhys; ix++) {
  5001. temp.q = sas_addr.q;
  5002. temp.b[7] += ix;
  5003. bios_pg4->Phy[ix].ReassignmentWWID = temp.q;
  5004. bios_pg4->Phy[ix].ReassignmentDeviceName = bias.q;
  5005. }
  5006. r = mpt3sas_config_set_bios_pg4(ioc, &mpi_reply, bios_pg4, sz);
  5007. out:
  5008. kfree(bios_pg4);
  5009. return r;
  5010. }
  5011. /**
  5012. * _base_static_config_pages - static start of day config pages
  5013. * @ioc: per adapter object
  5014. */
  5015. static int
  5016. _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
  5017. {
  5018. Mpi2ConfigReply_t mpi_reply;
  5019. u32 iounit_pg1_flags;
  5020. int tg_flags = 0;
  5021. int rc;
  5022. ioc->nvme_abort_timeout = 30;
  5023. rc = mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply,
  5024. &ioc->manu_pg0);
  5025. if (rc)
  5026. return rc;
  5027. if (ioc->ir_firmware) {
  5028. rc = mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  5029. &ioc->manu_pg10);
  5030. if (rc)
  5031. return rc;
  5032. }
  5033. if (ioc->pdev->vendor == MPI2_MFGPAGE_VENDORID_ATTO) {
  5034. rc = mpt3sas_atto_init(ioc);
  5035. if (rc)
  5036. return rc;
  5037. }
  5038. /*
  5039. * Ensure correct T10 PI operation if vendor left EEDPTagMode
  5040. * flag unset in NVDATA.
  5041. */
  5042. rc = mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply,
  5043. &ioc->manu_pg11);
  5044. if (rc)
  5045. return rc;
  5046. if (!ioc->is_gen35_ioc && ioc->manu_pg11.EEDPTagMode == 0) {
  5047. pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
  5048. ioc->name);
  5049. ioc->manu_pg11.EEDPTagMode &= ~0x3;
  5050. ioc->manu_pg11.EEDPTagMode |= 0x1;
  5051. mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
  5052. &ioc->manu_pg11);
  5053. }
  5054. if (ioc->manu_pg11.AddlFlags2 & NVME_TASK_MNGT_CUSTOM_MASK)
  5055. ioc->tm_custom_handling = 1;
  5056. else {
  5057. ioc->tm_custom_handling = 0;
  5058. if (ioc->manu_pg11.NVMeAbortTO < NVME_TASK_ABORT_MIN_TIMEOUT)
  5059. ioc->nvme_abort_timeout = NVME_TASK_ABORT_MIN_TIMEOUT;
  5060. else if (ioc->manu_pg11.NVMeAbortTO >
  5061. NVME_TASK_ABORT_MAX_TIMEOUT)
  5062. ioc->nvme_abort_timeout = NVME_TASK_ABORT_MAX_TIMEOUT;
  5063. else
  5064. ioc->nvme_abort_timeout = ioc->manu_pg11.NVMeAbortTO;
  5065. }
  5066. ioc->time_sync_interval =
  5067. ioc->manu_pg11.TimeSyncInterval & MPT3SAS_TIMESYNC_MASK;
  5068. if (ioc->time_sync_interval) {
  5069. if (ioc->manu_pg11.TimeSyncInterval & MPT3SAS_TIMESYNC_UNIT_MASK)
  5070. ioc->time_sync_interval =
  5071. ioc->time_sync_interval * SECONDS_PER_HOUR;
  5072. else
  5073. ioc->time_sync_interval =
  5074. ioc->time_sync_interval * SECONDS_PER_MIN;
  5075. dinitprintk(ioc, ioc_info(ioc,
  5076. "Driver-FW TimeSync interval is %d seconds. ManuPg11 TimeSync Unit is in %s\n",
  5077. ioc->time_sync_interval, (ioc->manu_pg11.TimeSyncInterval &
  5078. MPT3SAS_TIMESYNC_UNIT_MASK) ? "Hour" : "Minute"));
  5079. } else {
  5080. if (ioc->is_gen35_ioc)
  5081. ioc_warn(ioc,
  5082. "TimeSync Interval in Manuf page-11 is not enabled. Periodic Time-Sync will be disabled\n");
  5083. }
  5084. rc = _base_assign_fw_reported_qd(ioc);
  5085. if (rc)
  5086. return rc;
  5087. /*
  5088. * ATTO doesn't use bios page 2 and 3 for bios settings.
  5089. */
  5090. if (ioc->pdev->vendor == MPI2_MFGPAGE_VENDORID_ATTO)
  5091. ioc->bios_pg3.BiosVersion = 0;
  5092. else {
  5093. rc = mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  5094. if (rc)
  5095. return rc;
  5096. rc = mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  5097. if (rc)
  5098. return rc;
  5099. }
  5100. rc = mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  5101. if (rc)
  5102. return rc;
  5103. rc = mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  5104. if (rc)
  5105. return rc;
  5106. rc = mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  5107. if (rc)
  5108. return rc;
  5109. rc = mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
  5110. if (rc)
  5111. return rc;
  5112. _base_display_ioc_capabilities(ioc);
  5113. /*
  5114. * Enable task_set_full handling in iounit_pg1 when the
  5115. * facts capabilities indicate that its supported.
  5116. */
  5117. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  5118. if ((ioc->facts.IOCCapabilities &
  5119. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  5120. iounit_pg1_flags &=
  5121. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  5122. else
  5123. iounit_pg1_flags |=
  5124. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  5125. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  5126. rc = mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  5127. if (rc)
  5128. return rc;
  5129. if (ioc->iounit_pg8.NumSensors)
  5130. ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
  5131. if (ioc->is_aero_ioc) {
  5132. rc = _base_update_ioc_page1_inlinewith_perf_mode(ioc);
  5133. if (rc)
  5134. return rc;
  5135. }
  5136. if (ioc->is_gen35_ioc) {
  5137. if (ioc->is_driver_loading) {
  5138. rc = _base_get_diag_triggers(ioc);
  5139. if (rc)
  5140. return rc;
  5141. } else {
  5142. /*
  5143. * In case of online HBA FW update operation,
  5144. * check whether updated FW supports the driver trigger
  5145. * pages or not.
  5146. * - If previous FW has not supported driver trigger
  5147. * pages and newer FW supports them then update these
  5148. * pages with current diag trigger values.
  5149. * - If previous FW has supported driver trigger pages
  5150. * and new FW doesn't support them then disable
  5151. * support_trigger_pages flag.
  5152. */
  5153. _base_check_for_trigger_pages_support(ioc, &tg_flags);
  5154. if (!ioc->supports_trigger_pages && tg_flags != -EFAULT)
  5155. _base_update_diag_trigger_pages(ioc);
  5156. else if (ioc->supports_trigger_pages &&
  5157. tg_flags == -EFAULT)
  5158. ioc->supports_trigger_pages = 0;
  5159. }
  5160. }
  5161. return 0;
  5162. }
  5163. /**
  5164. * mpt3sas_free_enclosure_list - release memory
  5165. * @ioc: per adapter object
  5166. *
  5167. * Free memory allocated during enclosure add.
  5168. */
  5169. void
  5170. mpt3sas_free_enclosure_list(struct MPT3SAS_ADAPTER *ioc)
  5171. {
  5172. struct _enclosure_node *enclosure_dev, *enclosure_dev_next;
  5173. /* Free enclosure list */
  5174. list_for_each_entry_safe(enclosure_dev,
  5175. enclosure_dev_next, &ioc->enclosure_list, list) {
  5176. list_del(&enclosure_dev->list);
  5177. kfree(enclosure_dev);
  5178. }
  5179. }
  5180. /**
  5181. * _base_release_memory_pools - release memory
  5182. * @ioc: per adapter object
  5183. *
  5184. * Free memory allocated from _base_allocate_memory_pools.
  5185. */
  5186. static void
  5187. _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  5188. {
  5189. int i = 0;
  5190. int j = 0;
  5191. int dma_alloc_count = 0;
  5192. struct chain_tracker *ct;
  5193. int count = ioc->rdpq_array_enable ? ioc->reply_queue_count : 1;
  5194. dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  5195. if (ioc->request) {
  5196. dma_free_coherent(&ioc->pdev->dev, ioc->request_dma_sz,
  5197. ioc->request, ioc->request_dma);
  5198. dexitprintk(ioc,
  5199. ioc_info(ioc, "request_pool(0x%p): free\n",
  5200. ioc->request));
  5201. ioc->request = NULL;
  5202. }
  5203. if (ioc->sense) {
  5204. dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  5205. dma_pool_destroy(ioc->sense_dma_pool);
  5206. dexitprintk(ioc,
  5207. ioc_info(ioc, "sense_pool(0x%p): free\n",
  5208. ioc->sense));
  5209. ioc->sense = NULL;
  5210. }
  5211. if (ioc->reply) {
  5212. dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  5213. dma_pool_destroy(ioc->reply_dma_pool);
  5214. dexitprintk(ioc,
  5215. ioc_info(ioc, "reply_pool(0x%p): free\n",
  5216. ioc->reply));
  5217. ioc->reply = NULL;
  5218. }
  5219. if (ioc->reply_free) {
  5220. dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  5221. ioc->reply_free_dma);
  5222. dma_pool_destroy(ioc->reply_free_dma_pool);
  5223. dexitprintk(ioc,
  5224. ioc_info(ioc, "reply_free_pool(0x%p): free\n",
  5225. ioc->reply_free));
  5226. ioc->reply_free = NULL;
  5227. }
  5228. if (ioc->reply_post) {
  5229. dma_alloc_count = DIV_ROUND_UP(count,
  5230. RDPQ_MAX_INDEX_IN_ONE_CHUNK);
  5231. for (i = 0; i < count; i++) {
  5232. if (i % RDPQ_MAX_INDEX_IN_ONE_CHUNK == 0
  5233. && dma_alloc_count) {
  5234. if (ioc->reply_post[i].reply_post_free) {
  5235. dma_pool_free(
  5236. ioc->reply_post_free_dma_pool,
  5237. ioc->reply_post[i].reply_post_free,
  5238. ioc->reply_post[i].reply_post_free_dma);
  5239. dexitprintk(ioc, ioc_info(ioc,
  5240. "reply_post_free_pool(0x%p): free\n",
  5241. ioc->reply_post[i].reply_post_free));
  5242. ioc->reply_post[i].reply_post_free =
  5243. NULL;
  5244. }
  5245. --dma_alloc_count;
  5246. }
  5247. }
  5248. dma_pool_destroy(ioc->reply_post_free_dma_pool);
  5249. if (ioc->reply_post_free_array &&
  5250. ioc->rdpq_array_enable) {
  5251. dma_pool_free(ioc->reply_post_free_array_dma_pool,
  5252. ioc->reply_post_free_array,
  5253. ioc->reply_post_free_array_dma);
  5254. ioc->reply_post_free_array = NULL;
  5255. }
  5256. dma_pool_destroy(ioc->reply_post_free_array_dma_pool);
  5257. kfree(ioc->reply_post);
  5258. }
  5259. if (ioc->pcie_sgl_dma_pool) {
  5260. for (i = 0; i < ioc->scsiio_depth; i++) {
  5261. dma_pool_free(ioc->pcie_sgl_dma_pool,
  5262. ioc->pcie_sg_lookup[i].pcie_sgl,
  5263. ioc->pcie_sg_lookup[i].pcie_sgl_dma);
  5264. ioc->pcie_sg_lookup[i].pcie_sgl = NULL;
  5265. }
  5266. dma_pool_destroy(ioc->pcie_sgl_dma_pool);
  5267. }
  5268. kfree(ioc->pcie_sg_lookup);
  5269. ioc->pcie_sg_lookup = NULL;
  5270. if (ioc->config_page) {
  5271. dexitprintk(ioc,
  5272. ioc_info(ioc, "config_page(0x%p): free\n",
  5273. ioc->config_page));
  5274. dma_free_coherent(&ioc->pdev->dev, ioc->config_page_sz,
  5275. ioc->config_page, ioc->config_page_dma);
  5276. }
  5277. kfree(ioc->hpr_lookup);
  5278. ioc->hpr_lookup = NULL;
  5279. kfree(ioc->internal_lookup);
  5280. ioc->internal_lookup = NULL;
  5281. if (ioc->chain_lookup) {
  5282. for (i = 0; i < ioc->scsiio_depth; i++) {
  5283. for (j = ioc->chains_per_prp_buffer;
  5284. j < ioc->chains_needed_per_io; j++) {
  5285. ct = &ioc->chain_lookup[i].chains_per_smid[j];
  5286. if (ct && ct->chain_buffer)
  5287. dma_pool_free(ioc->chain_dma_pool,
  5288. ct->chain_buffer,
  5289. ct->chain_buffer_dma);
  5290. }
  5291. kfree(ioc->chain_lookup[i].chains_per_smid);
  5292. }
  5293. dma_pool_destroy(ioc->chain_dma_pool);
  5294. kfree(ioc->chain_lookup);
  5295. ioc->chain_lookup = NULL;
  5296. }
  5297. kfree(ioc->io_queue_num);
  5298. ioc->io_queue_num = NULL;
  5299. }
  5300. /**
  5301. * mpt3sas_check_same_4gb_region - checks whether all reply queues in a set are
  5302. * having same upper 32bits in their base memory address.
  5303. * @start_address: Base address of a reply queue set
  5304. * @pool_sz: Size of single Reply Descriptor Post Queues pool size
  5305. *
  5306. * Return: 1 if reply queues in a set have a same upper 32bits in their base
  5307. * memory address, else 0.
  5308. */
  5309. static int
  5310. mpt3sas_check_same_4gb_region(dma_addr_t start_address, u32 pool_sz)
  5311. {
  5312. dma_addr_t end_address;
  5313. end_address = start_address + pool_sz - 1;
  5314. if (upper_32_bits(start_address) == upper_32_bits(end_address))
  5315. return 1;
  5316. else
  5317. return 0;
  5318. }
  5319. /**
  5320. * _base_reduce_hba_queue_depth- Retry with reduced queue depth
  5321. * @ioc: Adapter object
  5322. *
  5323. * Return: 0 for success, non-zero for failure.
  5324. **/
  5325. static inline int
  5326. _base_reduce_hba_queue_depth(struct MPT3SAS_ADAPTER *ioc)
  5327. {
  5328. int reduce_sz = 64;
  5329. if ((ioc->hba_queue_depth - reduce_sz) >
  5330. (ioc->internal_depth + INTERNAL_SCSIIO_CMDS_COUNT)) {
  5331. ioc->hba_queue_depth -= reduce_sz;
  5332. return 0;
  5333. } else
  5334. return -ENOMEM;
  5335. }
  5336. /**
  5337. * _base_allocate_pcie_sgl_pool - Allocating DMA'able memory
  5338. * for pcie sgl pools.
  5339. * @ioc: Adapter object
  5340. * @sz: DMA Pool size
  5341. *
  5342. * Return: 0 for success, non-zero for failure.
  5343. */
  5344. static int
  5345. _base_allocate_pcie_sgl_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
  5346. {
  5347. int i = 0, j = 0;
  5348. struct chain_tracker *ct;
  5349. ioc->pcie_sgl_dma_pool =
  5350. dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz,
  5351. ioc->page_size, 0);
  5352. if (!ioc->pcie_sgl_dma_pool) {
  5353. ioc_err(ioc, "PCIe SGL pool: dma_pool_create failed\n");
  5354. return -ENOMEM;
  5355. }
  5356. ioc->chains_per_prp_buffer = sz/ioc->chain_segment_sz;
  5357. ioc->chains_per_prp_buffer =
  5358. min(ioc->chains_per_prp_buffer, ioc->chains_needed_per_io);
  5359. for (i = 0; i < ioc->scsiio_depth; i++) {
  5360. ioc->pcie_sg_lookup[i].pcie_sgl =
  5361. dma_pool_alloc(ioc->pcie_sgl_dma_pool, GFP_KERNEL,
  5362. &ioc->pcie_sg_lookup[i].pcie_sgl_dma);
  5363. if (!ioc->pcie_sg_lookup[i].pcie_sgl) {
  5364. ioc_err(ioc, "PCIe SGL pool: dma_pool_alloc failed\n");
  5365. return -EAGAIN;
  5366. }
  5367. if (!mpt3sas_check_same_4gb_region(
  5368. ioc->pcie_sg_lookup[i].pcie_sgl_dma, sz)) {
  5369. ioc_err(ioc, "PCIE SGLs are not in same 4G !! pcie sgl (0x%p) dma = (0x%llx)\n",
  5370. ioc->pcie_sg_lookup[i].pcie_sgl,
  5371. (unsigned long long)
  5372. ioc->pcie_sg_lookup[i].pcie_sgl_dma);
  5373. ioc->use_32bit_dma = true;
  5374. return -EAGAIN;
  5375. }
  5376. for (j = 0; j < ioc->chains_per_prp_buffer; j++) {
  5377. ct = &ioc->chain_lookup[i].chains_per_smid[j];
  5378. ct->chain_buffer =
  5379. ioc->pcie_sg_lookup[i].pcie_sgl +
  5380. (j * ioc->chain_segment_sz);
  5381. ct->chain_buffer_dma =
  5382. ioc->pcie_sg_lookup[i].pcie_sgl_dma +
  5383. (j * ioc->chain_segment_sz);
  5384. }
  5385. }
  5386. dinitprintk(ioc, ioc_info(ioc,
  5387. "PCIe sgl pool depth(%d), element_size(%d), pool_size(%d kB)\n",
  5388. ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
  5389. dinitprintk(ioc, ioc_info(ioc,
  5390. "Number of chains can fit in a PRP page(%d)\n",
  5391. ioc->chains_per_prp_buffer));
  5392. return 0;
  5393. }
  5394. /**
  5395. * _base_allocate_chain_dma_pool - Allocating DMA'able memory
  5396. * for chain dma pool.
  5397. * @ioc: Adapter object
  5398. * @sz: DMA Pool size
  5399. *
  5400. * Return: 0 for success, non-zero for failure.
  5401. */
  5402. static int
  5403. _base_allocate_chain_dma_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
  5404. {
  5405. int i = 0, j = 0;
  5406. struct chain_tracker *ctr;
  5407. ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
  5408. ioc->chain_segment_sz, 16, 0);
  5409. if (!ioc->chain_dma_pool)
  5410. return -ENOMEM;
  5411. for (i = 0; i < ioc->scsiio_depth; i++) {
  5412. for (j = ioc->chains_per_prp_buffer;
  5413. j < ioc->chains_needed_per_io; j++) {
  5414. ctr = &ioc->chain_lookup[i].chains_per_smid[j];
  5415. ctr->chain_buffer = dma_pool_alloc(ioc->chain_dma_pool,
  5416. GFP_KERNEL, &ctr->chain_buffer_dma);
  5417. if (!ctr->chain_buffer)
  5418. return -EAGAIN;
  5419. if (!mpt3sas_check_same_4gb_region(
  5420. ctr->chain_buffer_dma, ioc->chain_segment_sz)) {
  5421. ioc_err(ioc,
  5422. "Chain buffers are not in same 4G !!! Chain buff (0x%p) dma = (0x%llx)\n",
  5423. ctr->chain_buffer,
  5424. (unsigned long long)ctr->chain_buffer_dma);
  5425. ioc->use_32bit_dma = true;
  5426. return -EAGAIN;
  5427. }
  5428. }
  5429. }
  5430. dinitprintk(ioc, ioc_info(ioc,
  5431. "chain_lookup depth (%d), frame_size(%d), pool_size(%d kB)\n",
  5432. ioc->scsiio_depth, ioc->chain_segment_sz, ((ioc->scsiio_depth *
  5433. (ioc->chains_needed_per_io - ioc->chains_per_prp_buffer) *
  5434. ioc->chain_segment_sz))/1024));
  5435. return 0;
  5436. }
  5437. /**
  5438. * _base_allocate_sense_dma_pool - Allocating DMA'able memory
  5439. * for sense dma pool.
  5440. * @ioc: Adapter object
  5441. * @sz: DMA Pool size
  5442. * Return: 0 for success, non-zero for failure.
  5443. */
  5444. static int
  5445. _base_allocate_sense_dma_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
  5446. {
  5447. ioc->sense_dma_pool =
  5448. dma_pool_create("sense pool", &ioc->pdev->dev, sz, 4, 0);
  5449. if (!ioc->sense_dma_pool)
  5450. return -ENOMEM;
  5451. ioc->sense = dma_pool_alloc(ioc->sense_dma_pool,
  5452. GFP_KERNEL, &ioc->sense_dma);
  5453. if (!ioc->sense)
  5454. return -EAGAIN;
  5455. if (!mpt3sas_check_same_4gb_region(ioc->sense_dma, sz)) {
  5456. dinitprintk(ioc, pr_err(
  5457. "Bad Sense Pool! sense (0x%p) sense_dma = (0x%llx)\n",
  5458. ioc->sense, (unsigned long long) ioc->sense_dma));
  5459. ioc->use_32bit_dma = true;
  5460. return -EAGAIN;
  5461. }
  5462. ioc_info(ioc,
  5463. "sense pool(0x%p) - dma(0x%llx): depth(%d), element_size(%d), pool_size (%d kB)\n",
  5464. ioc->sense, (unsigned long long)ioc->sense_dma,
  5465. ioc->scsiio_depth, SCSI_SENSE_BUFFERSIZE, sz/1024);
  5466. return 0;
  5467. }
  5468. /**
  5469. * _base_allocate_reply_pool - Allocating DMA'able memory
  5470. * for reply pool.
  5471. * @ioc: Adapter object
  5472. * @sz: DMA Pool size
  5473. * Return: 0 for success, non-zero for failure.
  5474. */
  5475. static int
  5476. _base_allocate_reply_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
  5477. {
  5478. /* reply pool, 4 byte align */
  5479. ioc->reply_dma_pool = dma_pool_create("reply pool",
  5480. &ioc->pdev->dev, sz, 4, 0);
  5481. if (!ioc->reply_dma_pool)
  5482. return -ENOMEM;
  5483. ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
  5484. &ioc->reply_dma);
  5485. if (!ioc->reply)
  5486. return -EAGAIN;
  5487. if (!mpt3sas_check_same_4gb_region(ioc->reply_dma, sz)) {
  5488. dinitprintk(ioc, pr_err(
  5489. "Bad Reply Pool! Reply (0x%p) Reply dma = (0x%llx)\n",
  5490. ioc->reply, (unsigned long long) ioc->reply_dma));
  5491. ioc->use_32bit_dma = true;
  5492. return -EAGAIN;
  5493. }
  5494. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  5495. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  5496. ioc_info(ioc,
  5497. "reply pool(0x%p) - dma(0x%llx): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  5498. ioc->reply, (unsigned long long)ioc->reply_dma,
  5499. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024);
  5500. return 0;
  5501. }
  5502. /**
  5503. * _base_allocate_reply_free_dma_pool - Allocating DMA'able memory
  5504. * for reply free dma pool.
  5505. * @ioc: Adapter object
  5506. * @sz: DMA Pool size
  5507. * Return: 0 for success, non-zero for failure.
  5508. */
  5509. static int
  5510. _base_allocate_reply_free_dma_pool(struct MPT3SAS_ADAPTER *ioc, u32 sz)
  5511. {
  5512. /* reply free queue, 16 byte align */
  5513. ioc->reply_free_dma_pool = dma_pool_create(
  5514. "reply_free pool", &ioc->pdev->dev, sz, 16, 0);
  5515. if (!ioc->reply_free_dma_pool)
  5516. return -ENOMEM;
  5517. ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool,
  5518. GFP_KERNEL, &ioc->reply_free_dma);
  5519. if (!ioc->reply_free)
  5520. return -EAGAIN;
  5521. if (!mpt3sas_check_same_4gb_region(ioc->reply_free_dma, sz)) {
  5522. dinitprintk(ioc,
  5523. pr_err("Bad Reply Free Pool! Reply Free (0x%p) Reply Free dma = (0x%llx)\n",
  5524. ioc->reply_free, (unsigned long long) ioc->reply_free_dma));
  5525. ioc->use_32bit_dma = true;
  5526. return -EAGAIN;
  5527. }
  5528. memset(ioc->reply_free, 0, sz);
  5529. dinitprintk(ioc, ioc_info(ioc,
  5530. "reply_free pool(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  5531. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  5532. dinitprintk(ioc, ioc_info(ioc,
  5533. "reply_free_dma (0x%llx)\n",
  5534. (unsigned long long)ioc->reply_free_dma));
  5535. return 0;
  5536. }
  5537. /**
  5538. * _base_allocate_reply_post_free_array - Allocating DMA'able memory
  5539. * for reply post free array.
  5540. * @ioc: Adapter object
  5541. * @reply_post_free_array_sz: DMA Pool size
  5542. * Return: 0 for success, non-zero for failure.
  5543. */
  5544. static int
  5545. _base_allocate_reply_post_free_array(struct MPT3SAS_ADAPTER *ioc,
  5546. u32 reply_post_free_array_sz)
  5547. {
  5548. ioc->reply_post_free_array_dma_pool =
  5549. dma_pool_create("reply_post_free_array pool",
  5550. &ioc->pdev->dev, reply_post_free_array_sz, 16, 0);
  5551. if (!ioc->reply_post_free_array_dma_pool)
  5552. return -ENOMEM;
  5553. ioc->reply_post_free_array =
  5554. dma_pool_alloc(ioc->reply_post_free_array_dma_pool,
  5555. GFP_KERNEL, &ioc->reply_post_free_array_dma);
  5556. if (!ioc->reply_post_free_array)
  5557. return -EAGAIN;
  5558. if (!mpt3sas_check_same_4gb_region(ioc->reply_post_free_array_dma,
  5559. reply_post_free_array_sz)) {
  5560. dinitprintk(ioc, pr_err(
  5561. "Bad Reply Free Pool! Reply Free (0x%p) Reply Free dma = (0x%llx)\n",
  5562. ioc->reply_free,
  5563. (unsigned long long) ioc->reply_free_dma));
  5564. ioc->use_32bit_dma = true;
  5565. return -EAGAIN;
  5566. }
  5567. return 0;
  5568. }
  5569. /**
  5570. * base_alloc_rdpq_dma_pool - Allocating DMA'able memory
  5571. * for reply queues.
  5572. * @ioc: per adapter object
  5573. * @sz: DMA Pool size
  5574. * Return: 0 for success, non-zero for failure.
  5575. */
  5576. static int
  5577. base_alloc_rdpq_dma_pool(struct MPT3SAS_ADAPTER *ioc, int sz)
  5578. {
  5579. int i = 0;
  5580. u32 dma_alloc_count = 0;
  5581. int reply_post_free_sz = ioc->reply_post_queue_depth *
  5582. sizeof(Mpi2DefaultReplyDescriptor_t);
  5583. int count = ioc->rdpq_array_enable ? ioc->reply_queue_count : 1;
  5584. ioc->reply_post = kcalloc(count, sizeof(struct reply_post_struct),
  5585. GFP_KERNEL);
  5586. if (!ioc->reply_post)
  5587. return -ENOMEM;
  5588. /*
  5589. * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
  5590. * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should
  5591. * be within 4GB boundary i.e reply queues in a set must have same
  5592. * upper 32-bits in their memory address. so here driver is allocating
  5593. * the DMA'able memory for reply queues according.
  5594. * Driver uses limitation of
  5595. * VENTURA_SERIES to manage INVADER_SERIES as well.
  5596. */
  5597. dma_alloc_count = DIV_ROUND_UP(count,
  5598. RDPQ_MAX_INDEX_IN_ONE_CHUNK);
  5599. ioc->reply_post_free_dma_pool =
  5600. dma_pool_create("reply_post_free pool",
  5601. &ioc->pdev->dev, sz, 16, 0);
  5602. if (!ioc->reply_post_free_dma_pool)
  5603. return -ENOMEM;
  5604. for (i = 0; i < count; i++) {
  5605. if ((i % RDPQ_MAX_INDEX_IN_ONE_CHUNK == 0) && dma_alloc_count) {
  5606. ioc->reply_post[i].reply_post_free =
  5607. dma_pool_zalloc(ioc->reply_post_free_dma_pool,
  5608. GFP_KERNEL,
  5609. &ioc->reply_post[i].reply_post_free_dma);
  5610. if (!ioc->reply_post[i].reply_post_free)
  5611. return -ENOMEM;
  5612. /*
  5613. * Each set of RDPQ pool must satisfy 4gb boundary
  5614. * restriction.
  5615. * 1) Check if allocated resources for RDPQ pool are in
  5616. * the same 4GB range.
  5617. * 2) If #1 is true, continue with 64 bit DMA.
  5618. * 3) If #1 is false, return 1. which means free all the
  5619. * resources and set DMA mask to 32 and allocate.
  5620. */
  5621. if (!mpt3sas_check_same_4gb_region(
  5622. ioc->reply_post[i].reply_post_free_dma, sz)) {
  5623. dinitprintk(ioc,
  5624. ioc_err(ioc, "bad Replypost free pool(0x%p)"
  5625. "reply_post_free_dma = (0x%llx)\n",
  5626. ioc->reply_post[i].reply_post_free,
  5627. (unsigned long long)
  5628. ioc->reply_post[i].reply_post_free_dma));
  5629. return -EAGAIN;
  5630. }
  5631. dma_alloc_count--;
  5632. } else {
  5633. ioc->reply_post[i].reply_post_free =
  5634. (Mpi2ReplyDescriptorsUnion_t *)
  5635. ((long)ioc->reply_post[i-1].reply_post_free
  5636. + reply_post_free_sz);
  5637. ioc->reply_post[i].reply_post_free_dma =
  5638. (dma_addr_t)
  5639. (ioc->reply_post[i-1].reply_post_free_dma +
  5640. reply_post_free_sz);
  5641. }
  5642. }
  5643. return 0;
  5644. }
  5645. /**
  5646. * _base_allocate_memory_pools - allocate start of day memory pools
  5647. * @ioc: per adapter object
  5648. *
  5649. * Return: 0 success, anything else error.
  5650. */
  5651. static int
  5652. _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  5653. {
  5654. struct mpt3sas_facts *facts;
  5655. u16 max_sge_elements;
  5656. u16 chains_needed_per_io;
  5657. u32 sz, total_sz, reply_post_free_sz, reply_post_free_array_sz;
  5658. u32 retry_sz;
  5659. u32 rdpq_sz = 0, sense_sz = 0;
  5660. u16 max_request_credit, nvme_blocks_needed;
  5661. unsigned short sg_tablesize;
  5662. u16 sge_size;
  5663. int i;
  5664. int ret = 0, rc = 0;
  5665. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  5666. retry_sz = 0;
  5667. facts = &ioc->facts;
  5668. /* command line tunables for max sgl entries */
  5669. if (max_sgl_entries != -1)
  5670. sg_tablesize = max_sgl_entries;
  5671. else {
  5672. if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
  5673. sg_tablesize = MPT2SAS_SG_DEPTH;
  5674. else
  5675. sg_tablesize = MPT3SAS_SG_DEPTH;
  5676. }
  5677. /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
  5678. if (reset_devices)
  5679. sg_tablesize = min_t(unsigned short, sg_tablesize,
  5680. MPT_KDUMP_MIN_PHYS_SEGMENTS);
  5681. if (ioc->is_mcpu_endpoint)
  5682. ioc->shost->sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
  5683. else {
  5684. if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
  5685. sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
  5686. else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
  5687. sg_tablesize = min_t(unsigned short, sg_tablesize,
  5688. SG_MAX_SEGMENTS);
  5689. ioc_warn(ioc, "sg_tablesize(%u) is bigger than kernel defined SG_CHUNK_SIZE(%u)\n",
  5690. sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
  5691. }
  5692. ioc->shost->sg_tablesize = sg_tablesize;
  5693. }
  5694. ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
  5695. (facts->RequestCredit / 4));
  5696. if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
  5697. if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
  5698. INTERNAL_SCSIIO_CMDS_COUNT)) {
  5699. ioc_err(ioc, "IOC doesn't have enough Request Credits, it has just %d number of credits\n",
  5700. facts->RequestCredit);
  5701. return -ENOMEM;
  5702. }
  5703. ioc->internal_depth = 10;
  5704. }
  5705. ioc->hi_priority_depth = ioc->internal_depth - (5);
  5706. /* command line tunables for max controller queue depth */
  5707. if (max_queue_depth != -1 && max_queue_depth != 0) {
  5708. max_request_credit = min_t(u16, max_queue_depth +
  5709. ioc->internal_depth, facts->RequestCredit);
  5710. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  5711. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  5712. } else if (reset_devices)
  5713. max_request_credit = min_t(u16, facts->RequestCredit,
  5714. (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
  5715. else
  5716. max_request_credit = min_t(u16, facts->RequestCredit,
  5717. MAX_HBA_QUEUE_DEPTH);
  5718. /* Firmware maintains additional facts->HighPriorityCredit number of
  5719. * credits for HiPriprity Request messages, so hba queue depth will be
  5720. * sum of max_request_credit and high priority queue depth.
  5721. */
  5722. ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
  5723. /* request frame size */
  5724. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  5725. /* reply frame size */
  5726. ioc->reply_sz = facts->ReplyFrameSize * 4;
  5727. /* chain segment size */
  5728. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  5729. if (facts->IOCMaxChainSegmentSize)
  5730. ioc->chain_segment_sz =
  5731. facts->IOCMaxChainSegmentSize *
  5732. MAX_CHAIN_ELEMT_SZ;
  5733. else
  5734. /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
  5735. ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
  5736. MAX_CHAIN_ELEMT_SZ;
  5737. } else
  5738. ioc->chain_segment_sz = ioc->request_sz;
  5739. /* calculate the max scatter element size */
  5740. sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
  5741. retry_allocation:
  5742. total_sz = 0;
  5743. /* calculate number of sg elements left over in the 1st frame */
  5744. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  5745. sizeof(Mpi2SGEIOUnion_t)) + sge_size);
  5746. ioc->max_sges_in_main_message = max_sge_elements/sge_size;
  5747. /* now do the same for a chain buffer */
  5748. max_sge_elements = ioc->chain_segment_sz - sge_size;
  5749. ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
  5750. /*
  5751. * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  5752. */
  5753. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  5754. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  5755. + 1;
  5756. if (chains_needed_per_io > facts->MaxChainDepth) {
  5757. chains_needed_per_io = facts->MaxChainDepth;
  5758. ioc->shost->sg_tablesize = min_t(u16,
  5759. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  5760. * chains_needed_per_io), ioc->shost->sg_tablesize);
  5761. }
  5762. ioc->chains_needed_per_io = chains_needed_per_io;
  5763. /* reply free queue sizing - taking into account for 64 FW events */
  5764. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  5765. /* mCPU manage single counters for simplicity */
  5766. if (ioc->is_mcpu_endpoint)
  5767. ioc->reply_post_queue_depth = ioc->reply_free_queue_depth;
  5768. else {
  5769. /* calculate reply descriptor post queue depth */
  5770. ioc->reply_post_queue_depth = ioc->hba_queue_depth +
  5771. ioc->reply_free_queue_depth + 1;
  5772. /* align the reply post queue on the next 16 count boundary */
  5773. if (ioc->reply_post_queue_depth % 16)
  5774. ioc->reply_post_queue_depth += 16 -
  5775. (ioc->reply_post_queue_depth % 16);
  5776. }
  5777. if (ioc->reply_post_queue_depth >
  5778. facts->MaxReplyDescriptorPostQueueDepth) {
  5779. ioc->reply_post_queue_depth =
  5780. facts->MaxReplyDescriptorPostQueueDepth -
  5781. (facts->MaxReplyDescriptorPostQueueDepth % 16);
  5782. ioc->hba_queue_depth =
  5783. ((ioc->reply_post_queue_depth - 64) / 2) - 1;
  5784. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  5785. }
  5786. ioc_info(ioc,
  5787. "scatter gather: sge_in_main_msg(%d), sge_per_chain(%d), "
  5788. "sge_per_io(%d), chains_per_io(%d)\n",
  5789. ioc->max_sges_in_main_message,
  5790. ioc->max_sges_in_chain_message,
  5791. ioc->shost->sg_tablesize,
  5792. ioc->chains_needed_per_io);
  5793. /* reply post queue, 16 byte align */
  5794. reply_post_free_sz = ioc->reply_post_queue_depth *
  5795. sizeof(Mpi2DefaultReplyDescriptor_t);
  5796. rdpq_sz = reply_post_free_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
  5797. if ((_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
  5798. || (ioc->reply_queue_count < RDPQ_MAX_INDEX_IN_ONE_CHUNK))
  5799. rdpq_sz = reply_post_free_sz * ioc->reply_queue_count;
  5800. ret = base_alloc_rdpq_dma_pool(ioc, rdpq_sz);
  5801. if (ret == -EAGAIN) {
  5802. /*
  5803. * Free allocated bad RDPQ memory pools.
  5804. * Change dma coherent mask to 32 bit and reallocate RDPQ
  5805. */
  5806. _base_release_memory_pools(ioc);
  5807. ioc->use_32bit_dma = true;
  5808. if (_base_config_dma_addressing(ioc, ioc->pdev) != 0) {
  5809. ioc_err(ioc,
  5810. "32 DMA mask failed %s\n", pci_name(ioc->pdev));
  5811. return -ENODEV;
  5812. }
  5813. if (base_alloc_rdpq_dma_pool(ioc, rdpq_sz))
  5814. return -ENOMEM;
  5815. } else if (ret == -ENOMEM)
  5816. return -ENOMEM;
  5817. total_sz = rdpq_sz * (!ioc->rdpq_array_enable ? 1 :
  5818. DIV_ROUND_UP(ioc->reply_queue_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK));
  5819. ioc->scsiio_depth = ioc->hba_queue_depth -
  5820. ioc->hi_priority_depth - ioc->internal_depth;
  5821. /* set the scsi host can_queue depth
  5822. * with some internal commands that could be outstanding
  5823. */
  5824. ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
  5825. dinitprintk(ioc,
  5826. ioc_info(ioc, "scsi host: can_queue depth (%d)\n",
  5827. ioc->shost->can_queue));
  5828. /* contiguous pool for request and chains, 16 byte align, one extra "
  5829. * "frame for smid=0
  5830. */
  5831. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  5832. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  5833. /* hi-priority queue */
  5834. sz += (ioc->hi_priority_depth * ioc->request_sz);
  5835. /* internal queue */
  5836. sz += (ioc->internal_depth * ioc->request_sz);
  5837. ioc->request_dma_sz = sz;
  5838. ioc->request = dma_alloc_coherent(&ioc->pdev->dev, sz,
  5839. &ioc->request_dma, GFP_KERNEL);
  5840. if (!ioc->request) {
  5841. ioc_err(ioc, "request pool: dma_alloc_coherent failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), total(%d kB)\n",
  5842. ioc->hba_queue_depth, ioc->chains_needed_per_io,
  5843. ioc->request_sz, sz / 1024);
  5844. if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
  5845. goto out;
  5846. retry_sz = 64;
  5847. ioc->hba_queue_depth -= retry_sz;
  5848. _base_release_memory_pools(ioc);
  5849. goto retry_allocation;
  5850. }
  5851. if (retry_sz)
  5852. ioc_err(ioc, "request pool: dma_alloc_coherent succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), total(%d kb)\n",
  5853. ioc->hba_queue_depth, ioc->chains_needed_per_io,
  5854. ioc->request_sz, sz / 1024);
  5855. /* hi-priority queue */
  5856. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  5857. ioc->request_sz);
  5858. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  5859. ioc->request_sz);
  5860. /* internal queue */
  5861. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  5862. ioc->request_sz);
  5863. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  5864. ioc->request_sz);
  5865. ioc_info(ioc,
  5866. "request pool(0x%p) - dma(0x%llx): "
  5867. "depth(%d), frame_size(%d), pool_size(%d kB)\n",
  5868. ioc->request, (unsigned long long) ioc->request_dma,
  5869. ioc->hba_queue_depth, ioc->request_sz,
  5870. (ioc->hba_queue_depth * ioc->request_sz) / 1024);
  5871. total_sz += sz;
  5872. dinitprintk(ioc,
  5873. ioc_info(ioc, "scsiio(0x%p): depth(%d)\n",
  5874. ioc->request, ioc->scsiio_depth));
  5875. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  5876. sz = ioc->scsiio_depth * sizeof(struct chain_lookup);
  5877. ioc->chain_lookup = kzalloc(sz, GFP_KERNEL);
  5878. if (!ioc->chain_lookup) {
  5879. ioc_err(ioc, "chain_lookup: __get_free_pages failed\n");
  5880. goto out;
  5881. }
  5882. sz = ioc->chains_needed_per_io * sizeof(struct chain_tracker);
  5883. for (i = 0; i < ioc->scsiio_depth; i++) {
  5884. ioc->chain_lookup[i].chains_per_smid = kzalloc(sz, GFP_KERNEL);
  5885. if (!ioc->chain_lookup[i].chains_per_smid) {
  5886. ioc_err(ioc, "chain_lookup: kzalloc failed\n");
  5887. goto out;
  5888. }
  5889. }
  5890. /* initialize hi-priority queue smid's */
  5891. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  5892. sizeof(struct request_tracker), GFP_KERNEL);
  5893. if (!ioc->hpr_lookup) {
  5894. ioc_err(ioc, "hpr_lookup: kcalloc failed\n");
  5895. goto out;
  5896. }
  5897. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  5898. dinitprintk(ioc,
  5899. ioc_info(ioc, "hi_priority(0x%p): depth(%d), start smid(%d)\n",
  5900. ioc->hi_priority,
  5901. ioc->hi_priority_depth, ioc->hi_priority_smid));
  5902. /* initialize internal queue smid's */
  5903. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  5904. sizeof(struct request_tracker), GFP_KERNEL);
  5905. if (!ioc->internal_lookup) {
  5906. ioc_err(ioc, "internal_lookup: kcalloc failed\n");
  5907. goto out;
  5908. }
  5909. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  5910. dinitprintk(ioc,
  5911. ioc_info(ioc, "internal(0x%p): depth(%d), start smid(%d)\n",
  5912. ioc->internal,
  5913. ioc->internal_depth, ioc->internal_smid));
  5914. ioc->io_queue_num = kcalloc(ioc->scsiio_depth,
  5915. sizeof(u16), GFP_KERNEL);
  5916. if (!ioc->io_queue_num)
  5917. goto out;
  5918. /*
  5919. * The number of NVMe page sized blocks needed is:
  5920. * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
  5921. * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
  5922. * that is placed in the main message frame. 8 is the size of each PRP
  5923. * entry or PRP list pointer entry. 8 is subtracted from page_size
  5924. * because of the PRP list pointer entry at the end of a page, so this
  5925. * is not counted as a PRP entry. The 1 added page is a round up.
  5926. *
  5927. * To avoid allocation failures due to the amount of memory that could
  5928. * be required for NVMe PRP's, only each set of NVMe blocks will be
  5929. * contiguous, so a new set is allocated for each possible I/O.
  5930. */
  5931. ioc->chains_per_prp_buffer = 0;
  5932. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
  5933. nvme_blocks_needed =
  5934. (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
  5935. nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
  5936. nvme_blocks_needed++;
  5937. sz = sizeof(struct pcie_sg_list) * ioc->scsiio_depth;
  5938. ioc->pcie_sg_lookup = kzalloc(sz, GFP_KERNEL);
  5939. if (!ioc->pcie_sg_lookup) {
  5940. ioc_info(ioc, "PCIe SGL lookup: kzalloc failed\n");
  5941. goto out;
  5942. }
  5943. sz = nvme_blocks_needed * ioc->page_size;
  5944. rc = _base_allocate_pcie_sgl_pool(ioc, sz);
  5945. if (rc == -ENOMEM)
  5946. return -ENOMEM;
  5947. else if (rc == -EAGAIN)
  5948. goto try_32bit_dma;
  5949. total_sz += sz * ioc->scsiio_depth;
  5950. }
  5951. rc = _base_allocate_chain_dma_pool(ioc, ioc->chain_segment_sz);
  5952. if (rc == -ENOMEM)
  5953. return -ENOMEM;
  5954. else if (rc == -EAGAIN)
  5955. goto try_32bit_dma;
  5956. total_sz += ioc->chain_segment_sz * ((ioc->chains_needed_per_io -
  5957. ioc->chains_per_prp_buffer) * ioc->scsiio_depth);
  5958. dinitprintk(ioc,
  5959. ioc_info(ioc, "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
  5960. ioc->chain_depth, ioc->chain_segment_sz,
  5961. (ioc->chain_depth * ioc->chain_segment_sz) / 1024));
  5962. /* sense buffers, 4 byte align */
  5963. sense_sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  5964. rc = _base_allocate_sense_dma_pool(ioc, sense_sz);
  5965. if (rc == -ENOMEM)
  5966. return -ENOMEM;
  5967. else if (rc == -EAGAIN)
  5968. goto try_32bit_dma;
  5969. total_sz += sense_sz;
  5970. /* reply pool, 4 byte align */
  5971. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  5972. rc = _base_allocate_reply_pool(ioc, sz);
  5973. if (rc == -ENOMEM)
  5974. return -ENOMEM;
  5975. else if (rc == -EAGAIN)
  5976. goto try_32bit_dma;
  5977. total_sz += sz;
  5978. /* reply free queue, 16 byte align */
  5979. sz = ioc->reply_free_queue_depth * 4;
  5980. rc = _base_allocate_reply_free_dma_pool(ioc, sz);
  5981. if (rc == -ENOMEM)
  5982. return -ENOMEM;
  5983. else if (rc == -EAGAIN)
  5984. goto try_32bit_dma;
  5985. dinitprintk(ioc,
  5986. ioc_info(ioc, "reply_free_dma (0x%llx)\n",
  5987. (unsigned long long)ioc->reply_free_dma));
  5988. total_sz += sz;
  5989. if (ioc->rdpq_array_enable) {
  5990. reply_post_free_array_sz = ioc->reply_queue_count *
  5991. sizeof(Mpi2IOCInitRDPQArrayEntry);
  5992. rc = _base_allocate_reply_post_free_array(ioc,
  5993. reply_post_free_array_sz);
  5994. if (rc == -ENOMEM)
  5995. return -ENOMEM;
  5996. else if (rc == -EAGAIN)
  5997. goto try_32bit_dma;
  5998. }
  5999. ioc->config_page_sz = 512;
  6000. ioc->config_page = dma_alloc_coherent(&ioc->pdev->dev,
  6001. ioc->config_page_sz, &ioc->config_page_dma, GFP_KERNEL);
  6002. if (!ioc->config_page) {
  6003. ioc_err(ioc, "config page: dma_pool_alloc failed\n");
  6004. goto out;
  6005. }
  6006. ioc_info(ioc, "config page(0x%p) - dma(0x%llx): size(%d)\n",
  6007. ioc->config_page, (unsigned long long)ioc->config_page_dma,
  6008. ioc->config_page_sz);
  6009. total_sz += ioc->config_page_sz;
  6010. ioc_info(ioc, "Allocated physical memory: size(%d kB)\n",
  6011. total_sz / 1024);
  6012. ioc_info(ioc, "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
  6013. ioc->shost->can_queue, facts->RequestCredit);
  6014. ioc_info(ioc, "Scatter Gather Elements per IO(%d)\n",
  6015. ioc->shost->sg_tablesize);
  6016. return 0;
  6017. try_32bit_dma:
  6018. _base_release_memory_pools(ioc);
  6019. if (ioc->use_32bit_dma && (ioc->dma_mask > 32)) {
  6020. /* Change dma coherent mask to 32 bit and reallocate */
  6021. if (_base_config_dma_addressing(ioc, ioc->pdev) != 0) {
  6022. pr_err("Setting 32 bit coherent DMA mask Failed %s\n",
  6023. pci_name(ioc->pdev));
  6024. return -ENODEV;
  6025. }
  6026. } else if (_base_reduce_hba_queue_depth(ioc) != 0)
  6027. return -ENOMEM;
  6028. goto retry_allocation;
  6029. out:
  6030. return -ENOMEM;
  6031. }
  6032. /**
  6033. * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
  6034. * @ioc: Pointer to MPT_ADAPTER structure
  6035. * @cooked: Request raw or cooked IOC state
  6036. *
  6037. * Return: all IOC Doorbell register bits if cooked==0, else just the
  6038. * Doorbell bits in MPI_IOC_STATE_MASK.
  6039. */
  6040. u32
  6041. mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
  6042. {
  6043. u32 s, sc;
  6044. s = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
  6045. sc = s & MPI2_IOC_STATE_MASK;
  6046. return cooked ? sc : s;
  6047. }
  6048. /**
  6049. * _base_wait_on_iocstate - waiting on a particular ioc state
  6050. * @ioc: ?
  6051. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  6052. * @timeout: timeout in second
  6053. *
  6054. * Return: 0 for success, non-zero for failure.
  6055. */
  6056. static int
  6057. _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
  6058. {
  6059. u32 count, cntdn;
  6060. u32 current_state;
  6061. count = 0;
  6062. cntdn = 1000 * timeout;
  6063. do {
  6064. current_state = mpt3sas_base_get_iocstate(ioc, 1);
  6065. if (current_state == ioc_state)
  6066. return 0;
  6067. if (count && current_state == MPI2_IOC_STATE_FAULT)
  6068. break;
  6069. if (count && current_state == MPI2_IOC_STATE_COREDUMP)
  6070. break;
  6071. usleep_range(1000, 1500);
  6072. count++;
  6073. } while (--cntdn);
  6074. return current_state;
  6075. }
  6076. /**
  6077. * _base_dump_reg_set - This function will print hexdump of register set.
  6078. * @ioc: per adapter object
  6079. *
  6080. * Return: nothing.
  6081. */
  6082. static inline void
  6083. _base_dump_reg_set(struct MPT3SAS_ADAPTER *ioc)
  6084. {
  6085. unsigned int i, sz = 256;
  6086. u32 __iomem *reg = (u32 __iomem *)ioc->chip;
  6087. ioc_info(ioc, "System Register set:\n");
  6088. for (i = 0; i < (sz / sizeof(u32)); i++)
  6089. pr_info("%08x: %08x\n", (i * 4), readl(&reg[i]));
  6090. }
  6091. /**
  6092. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  6093. * a write to the doorbell)
  6094. * @ioc: per adapter object
  6095. * @timeout: timeout in seconds
  6096. *
  6097. * Return: 0 for success, non-zero for failure.
  6098. *
  6099. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  6100. */
  6101. static int
  6102. _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
  6103. {
  6104. u32 cntdn, count;
  6105. u32 int_status;
  6106. count = 0;
  6107. cntdn = 1000 * timeout;
  6108. do {
  6109. int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
  6110. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  6111. dhsprintk(ioc,
  6112. ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
  6113. __func__, count, timeout));
  6114. return 0;
  6115. }
  6116. usleep_range(1000, 1500);
  6117. count++;
  6118. } while (--cntdn);
  6119. ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
  6120. __func__, count, int_status);
  6121. return -EFAULT;
  6122. }
  6123. static int
  6124. _base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
  6125. {
  6126. u32 cntdn, count;
  6127. u32 int_status;
  6128. count = 0;
  6129. cntdn = 2000 * timeout;
  6130. do {
  6131. int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
  6132. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  6133. dhsprintk(ioc,
  6134. ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
  6135. __func__, count, timeout));
  6136. return 0;
  6137. }
  6138. udelay(500);
  6139. count++;
  6140. } while (--cntdn);
  6141. ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
  6142. __func__, count, int_status);
  6143. return -EFAULT;
  6144. }
  6145. /**
  6146. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  6147. * @ioc: per adapter object
  6148. * @timeout: timeout in second
  6149. *
  6150. * Return: 0 for success, non-zero for failure.
  6151. *
  6152. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  6153. * doorbell.
  6154. */
  6155. static int
  6156. _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
  6157. {
  6158. u32 cntdn, count;
  6159. u32 int_status;
  6160. u32 doorbell;
  6161. count = 0;
  6162. cntdn = 1000 * timeout;
  6163. do {
  6164. int_status = ioc->base_readl(&ioc->chip->HostInterruptStatus);
  6165. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  6166. dhsprintk(ioc,
  6167. ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
  6168. __func__, count, timeout));
  6169. return 0;
  6170. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  6171. doorbell = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
  6172. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  6173. MPI2_IOC_STATE_FAULT) {
  6174. mpt3sas_print_fault_code(ioc, doorbell);
  6175. return -EFAULT;
  6176. }
  6177. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  6178. MPI2_IOC_STATE_COREDUMP) {
  6179. mpt3sas_print_coredump_info(ioc, doorbell);
  6180. return -EFAULT;
  6181. }
  6182. } else if (int_status == 0xFFFFFFFF)
  6183. goto out;
  6184. usleep_range(1000, 1500);
  6185. count++;
  6186. } while (--cntdn);
  6187. out:
  6188. ioc_err(ioc, "%s: failed due to timeout count(%d), int_status(%x)!\n",
  6189. __func__, count, int_status);
  6190. return -EFAULT;
  6191. }
  6192. /**
  6193. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  6194. * @ioc: per adapter object
  6195. * @timeout: timeout in second
  6196. *
  6197. * Return: 0 for success, non-zero for failure.
  6198. */
  6199. static int
  6200. _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
  6201. {
  6202. u32 cntdn, count;
  6203. u32 doorbell_reg;
  6204. count = 0;
  6205. cntdn = 1000 * timeout;
  6206. do {
  6207. doorbell_reg = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
  6208. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  6209. dhsprintk(ioc,
  6210. ioc_info(ioc, "%s: successful count(%d), timeout(%d)\n",
  6211. __func__, count, timeout));
  6212. return 0;
  6213. }
  6214. usleep_range(1000, 1500);
  6215. count++;
  6216. } while (--cntdn);
  6217. ioc_err(ioc, "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
  6218. __func__, count, doorbell_reg);
  6219. return -EFAULT;
  6220. }
  6221. /**
  6222. * _base_send_ioc_reset - send doorbell reset
  6223. * @ioc: per adapter object
  6224. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  6225. * @timeout: timeout in second
  6226. *
  6227. * Return: 0 for success, non-zero for failure.
  6228. */
  6229. static int
  6230. _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
  6231. {
  6232. u32 ioc_state;
  6233. int r = 0;
  6234. unsigned long flags;
  6235. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  6236. ioc_err(ioc, "%s: unknown reset_type\n", __func__);
  6237. return -EFAULT;
  6238. }
  6239. if (!(ioc->facts.IOCCapabilities &
  6240. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  6241. return -EFAULT;
  6242. ioc_info(ioc, "sending message unit reset !!\n");
  6243. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  6244. &ioc->chip->Doorbell);
  6245. if ((_base_wait_for_doorbell_ack(ioc, 15))) {
  6246. r = -EFAULT;
  6247. goto out;
  6248. }
  6249. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
  6250. if (ioc_state) {
  6251. ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
  6252. __func__, ioc_state);
  6253. r = -EFAULT;
  6254. goto out;
  6255. }
  6256. out:
  6257. if (r != 0) {
  6258. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  6259. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  6260. /*
  6261. * Wait for IOC state CoreDump to clear only during
  6262. * HBA initialization & release time.
  6263. */
  6264. if ((ioc_state & MPI2_IOC_STATE_MASK) ==
  6265. MPI2_IOC_STATE_COREDUMP && (ioc->is_driver_loading == 1 ||
  6266. ioc->fault_reset_work_q == NULL)) {
  6267. spin_unlock_irqrestore(
  6268. &ioc->ioc_reset_in_progress_lock, flags);
  6269. mpt3sas_print_coredump_info(ioc, ioc_state);
  6270. mpt3sas_base_wait_for_coredump_completion(ioc,
  6271. __func__);
  6272. spin_lock_irqsave(
  6273. &ioc->ioc_reset_in_progress_lock, flags);
  6274. }
  6275. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  6276. }
  6277. ioc_info(ioc, "message unit reset: %s\n",
  6278. r == 0 ? "SUCCESS" : "FAILED");
  6279. return r;
  6280. }
  6281. /**
  6282. * mpt3sas_wait_for_ioc - IOC's operational state is checked here.
  6283. * @ioc: per adapter object
  6284. * @timeout: timeout in seconds
  6285. *
  6286. * Return: Waits up to timeout seconds for the IOC to
  6287. * become operational. Returns 0 if IOC is present
  6288. * and operational; otherwise returns %-EFAULT.
  6289. */
  6290. int
  6291. mpt3sas_wait_for_ioc(struct MPT3SAS_ADAPTER *ioc, int timeout)
  6292. {
  6293. int wait_state_count = 0;
  6294. u32 ioc_state;
  6295. do {
  6296. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  6297. if (ioc_state == MPI2_IOC_STATE_OPERATIONAL)
  6298. break;
  6299. /*
  6300. * Watchdog thread will be started after IOC Initialization, so
  6301. * no need to wait here for IOC state to become operational
  6302. * when IOC Initialization is on. Instead the driver will
  6303. * return ETIME status, so that calling function can issue
  6304. * diag reset operation and retry the command.
  6305. */
  6306. if (ioc->is_driver_loading)
  6307. return -ETIME;
  6308. ssleep(1);
  6309. ioc_info(ioc, "%s: waiting for operational state(count=%d)\n",
  6310. __func__, ++wait_state_count);
  6311. } while (--timeout);
  6312. if (!timeout) {
  6313. ioc_err(ioc, "%s: failed due to ioc not operational\n", __func__);
  6314. return -EFAULT;
  6315. }
  6316. if (wait_state_count)
  6317. ioc_info(ioc, "ioc is operational\n");
  6318. return 0;
  6319. }
  6320. /**
  6321. * _base_handshake_req_reply_wait - send request thru doorbell interface
  6322. * @ioc: per adapter object
  6323. * @request_bytes: request length
  6324. * @request: pointer having request payload
  6325. * @reply_bytes: reply length
  6326. * @reply: pointer to reply payload
  6327. * @timeout: timeout in second
  6328. *
  6329. * Return: 0 for success, non-zero for failure.
  6330. */
  6331. static int
  6332. _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
  6333. u32 *request, int reply_bytes, u16 *reply, int timeout)
  6334. {
  6335. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  6336. int i;
  6337. u8 failed;
  6338. __le32 *mfp;
  6339. /* make sure doorbell is not in use */
  6340. if ((ioc->base_readl_ext_retry(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  6341. ioc_err(ioc, "doorbell is in use (line=%d)\n", __LINE__);
  6342. return -EFAULT;
  6343. }
  6344. /* clear pending doorbell interrupts from previous state changes */
  6345. if (ioc->base_readl(&ioc->chip->HostInterruptStatus) &
  6346. MPI2_HIS_IOC2SYS_DB_STATUS)
  6347. writel(0, &ioc->chip->HostInterruptStatus);
  6348. /* send message to ioc */
  6349. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  6350. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  6351. &ioc->chip->Doorbell);
  6352. if ((_base_spin_on_doorbell_int(ioc, 5))) {
  6353. ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
  6354. __LINE__);
  6355. return -EFAULT;
  6356. }
  6357. writel(0, &ioc->chip->HostInterruptStatus);
  6358. if ((_base_wait_for_doorbell_ack(ioc, 5))) {
  6359. ioc_err(ioc, "doorbell handshake ack failed (line=%d)\n",
  6360. __LINE__);
  6361. return -EFAULT;
  6362. }
  6363. /* send message 32-bits at a time */
  6364. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  6365. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  6366. if ((_base_wait_for_doorbell_ack(ioc, 5)))
  6367. failed = 1;
  6368. }
  6369. if (failed) {
  6370. ioc_err(ioc, "doorbell handshake sending request failed (line=%d)\n",
  6371. __LINE__);
  6372. return -EFAULT;
  6373. }
  6374. /* now wait for the reply */
  6375. if ((_base_wait_for_doorbell_int(ioc, timeout))) {
  6376. ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
  6377. __LINE__);
  6378. return -EFAULT;
  6379. }
  6380. /* read the first two 16-bits, it gives the total length of the reply */
  6381. reply[0] = le16_to_cpu(ioc->base_readl_ext_retry(&ioc->chip->Doorbell)
  6382. & MPI2_DOORBELL_DATA_MASK);
  6383. writel(0, &ioc->chip->HostInterruptStatus);
  6384. if ((_base_wait_for_doorbell_int(ioc, 5))) {
  6385. ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
  6386. __LINE__);
  6387. return -EFAULT;
  6388. }
  6389. reply[1] = le16_to_cpu(ioc->base_readl_ext_retry(&ioc->chip->Doorbell)
  6390. & MPI2_DOORBELL_DATA_MASK);
  6391. writel(0, &ioc->chip->HostInterruptStatus);
  6392. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  6393. if ((_base_wait_for_doorbell_int(ioc, 5))) {
  6394. ioc_err(ioc, "doorbell handshake int failed (line=%d)\n",
  6395. __LINE__);
  6396. return -EFAULT;
  6397. }
  6398. if (i >= reply_bytes/2) /* overflow case */
  6399. ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
  6400. else
  6401. reply[i] = le16_to_cpu(
  6402. ioc->base_readl_ext_retry(&ioc->chip->Doorbell)
  6403. & MPI2_DOORBELL_DATA_MASK);
  6404. writel(0, &ioc->chip->HostInterruptStatus);
  6405. }
  6406. _base_wait_for_doorbell_int(ioc, 5);
  6407. if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
  6408. dhsprintk(ioc,
  6409. ioc_info(ioc, "doorbell is in use (line=%d)\n",
  6410. __LINE__));
  6411. }
  6412. writel(0, &ioc->chip->HostInterruptStatus);
  6413. if (ioc->logging_level & MPT_DEBUG_INIT) {
  6414. mfp = (__le32 *)reply;
  6415. pr_info("\toffset:data\n");
  6416. for (i = 0; i < reply_bytes/4; i++)
  6417. ioc_info(ioc, "\t[0x%02x]:%08x\n", i*4,
  6418. le32_to_cpu(mfp[i]));
  6419. }
  6420. return 0;
  6421. }
  6422. /**
  6423. * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
  6424. * @ioc: per adapter object
  6425. * @mpi_reply: the reply payload from FW
  6426. * @mpi_request: the request payload sent to FW
  6427. *
  6428. * The SAS IO Unit Control Request message allows the host to perform low-level
  6429. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  6430. * to obtain the IOC assigned device handles for a device if it has other
  6431. * identifying information about the device, in addition allows the host to
  6432. * remove IOC resources associated with the device.
  6433. *
  6434. * Return: 0 for success, non-zero for failure.
  6435. */
  6436. int
  6437. mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
  6438. Mpi2SasIoUnitControlReply_t *mpi_reply,
  6439. Mpi2SasIoUnitControlRequest_t *mpi_request)
  6440. {
  6441. u16 smid;
  6442. u8 issue_reset = 0;
  6443. int rc;
  6444. void *request;
  6445. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  6446. mutex_lock(&ioc->base_cmds.mutex);
  6447. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  6448. ioc_err(ioc, "%s: base_cmd in use\n", __func__);
  6449. rc = -EAGAIN;
  6450. goto out;
  6451. }
  6452. rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
  6453. if (rc)
  6454. goto out;
  6455. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  6456. if (!smid) {
  6457. ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
  6458. rc = -EAGAIN;
  6459. goto out;
  6460. }
  6461. rc = 0;
  6462. ioc->base_cmds.status = MPT3_CMD_PENDING;
  6463. request = mpt3sas_base_get_msg_frame(ioc, smid);
  6464. ioc->base_cmds.smid = smid;
  6465. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  6466. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  6467. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  6468. ioc->ioc_link_reset_in_progress = 1;
  6469. init_completion(&ioc->base_cmds.done);
  6470. ioc->put_smid_default(ioc, smid);
  6471. wait_for_completion_timeout(&ioc->base_cmds.done,
  6472. msecs_to_jiffies(10000));
  6473. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  6474. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  6475. ioc->ioc_link_reset_in_progress)
  6476. ioc->ioc_link_reset_in_progress = 0;
  6477. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  6478. mpt3sas_check_cmd_timeout(ioc, ioc->base_cmds.status,
  6479. mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t)/4,
  6480. issue_reset);
  6481. goto issue_host_reset;
  6482. }
  6483. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  6484. memcpy(mpi_reply, ioc->base_cmds.reply,
  6485. sizeof(Mpi2SasIoUnitControlReply_t));
  6486. else
  6487. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  6488. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  6489. goto out;
  6490. issue_host_reset:
  6491. if (issue_reset)
  6492. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  6493. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  6494. rc = -EFAULT;
  6495. out:
  6496. mutex_unlock(&ioc->base_cmds.mutex);
  6497. return rc;
  6498. }
  6499. /**
  6500. * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
  6501. * @ioc: per adapter object
  6502. * @mpi_reply: the reply payload from FW
  6503. * @mpi_request: the request payload sent to FW
  6504. *
  6505. * The SCSI Enclosure Processor request message causes the IOC to
  6506. * communicate with SES devices to control LED status signals.
  6507. *
  6508. * Return: 0 for success, non-zero for failure.
  6509. */
  6510. int
  6511. mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
  6512. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  6513. {
  6514. u16 smid;
  6515. u8 issue_reset = 0;
  6516. int rc;
  6517. void *request;
  6518. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  6519. mutex_lock(&ioc->base_cmds.mutex);
  6520. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  6521. ioc_err(ioc, "%s: base_cmd in use\n", __func__);
  6522. rc = -EAGAIN;
  6523. goto out;
  6524. }
  6525. rc = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
  6526. if (rc)
  6527. goto out;
  6528. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  6529. if (!smid) {
  6530. ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
  6531. rc = -EAGAIN;
  6532. goto out;
  6533. }
  6534. rc = 0;
  6535. ioc->base_cmds.status = MPT3_CMD_PENDING;
  6536. request = mpt3sas_base_get_msg_frame(ioc, smid);
  6537. ioc->base_cmds.smid = smid;
  6538. memset(request, 0, ioc->request_sz);
  6539. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  6540. init_completion(&ioc->base_cmds.done);
  6541. ioc->put_smid_default(ioc, smid);
  6542. wait_for_completion_timeout(&ioc->base_cmds.done,
  6543. msecs_to_jiffies(10000));
  6544. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  6545. mpt3sas_check_cmd_timeout(ioc,
  6546. ioc->base_cmds.status, mpi_request,
  6547. sizeof(Mpi2SepRequest_t)/4, issue_reset);
  6548. goto issue_host_reset;
  6549. }
  6550. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  6551. memcpy(mpi_reply, ioc->base_cmds.reply,
  6552. sizeof(Mpi2SepReply_t));
  6553. else
  6554. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  6555. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  6556. goto out;
  6557. issue_host_reset:
  6558. if (issue_reset)
  6559. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  6560. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  6561. rc = -EFAULT;
  6562. out:
  6563. mutex_unlock(&ioc->base_cmds.mutex);
  6564. return rc;
  6565. }
  6566. /**
  6567. * _base_get_port_facts - obtain port facts reply and save in ioc
  6568. * @ioc: per adapter object
  6569. * @port: ?
  6570. *
  6571. * Return: 0 for success, non-zero for failure.
  6572. */
  6573. static int
  6574. _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
  6575. {
  6576. Mpi2PortFactsRequest_t mpi_request;
  6577. Mpi2PortFactsReply_t mpi_reply;
  6578. struct mpt3sas_port_facts *pfacts;
  6579. int mpi_reply_sz, mpi_request_sz, r;
  6580. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  6581. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  6582. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  6583. memset(&mpi_request, 0, mpi_request_sz);
  6584. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  6585. mpi_request.PortNumber = port;
  6586. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  6587. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
  6588. if (r != 0) {
  6589. ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
  6590. return r;
  6591. }
  6592. pfacts = &ioc->pfacts[port];
  6593. memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
  6594. pfacts->PortNumber = mpi_reply.PortNumber;
  6595. pfacts->VP_ID = mpi_reply.VP_ID;
  6596. pfacts->VF_ID = mpi_reply.VF_ID;
  6597. pfacts->MaxPostedCmdBuffers =
  6598. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  6599. return 0;
  6600. }
  6601. /**
  6602. * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
  6603. * @ioc: per adapter object
  6604. * @timeout:
  6605. *
  6606. * Return: 0 for success, non-zero for failure.
  6607. */
  6608. static int
  6609. _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
  6610. {
  6611. u32 ioc_state;
  6612. int rc;
  6613. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  6614. if (ioc->pci_error_recovery) {
  6615. dfailprintk(ioc,
  6616. ioc_info(ioc, "%s: host in pci error recovery\n",
  6617. __func__));
  6618. return -EFAULT;
  6619. }
  6620. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  6621. dhsprintk(ioc,
  6622. ioc_info(ioc, "%s: ioc_state(0x%08x)\n",
  6623. __func__, ioc_state));
  6624. if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
  6625. (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  6626. return 0;
  6627. if (ioc_state & MPI2_DOORBELL_USED) {
  6628. dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
  6629. goto issue_diag_reset;
  6630. }
  6631. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  6632. mpt3sas_print_fault_code(ioc, ioc_state &
  6633. MPI2_DOORBELL_DATA_MASK);
  6634. goto issue_diag_reset;
  6635. } else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
  6636. MPI2_IOC_STATE_COREDUMP) {
  6637. ioc_info(ioc,
  6638. "%s: Skipping the diag reset here. (ioc_state=0x%x)\n",
  6639. __func__, ioc_state);
  6640. return -EFAULT;
  6641. }
  6642. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
  6643. if (ioc_state) {
  6644. dfailprintk(ioc,
  6645. ioc_info(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
  6646. __func__, ioc_state));
  6647. return -EFAULT;
  6648. }
  6649. issue_diag_reset:
  6650. rc = _base_diag_reset(ioc);
  6651. return rc;
  6652. }
  6653. /**
  6654. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  6655. * @ioc: per adapter object
  6656. *
  6657. * Return: 0 for success, non-zero for failure.
  6658. */
  6659. static int
  6660. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
  6661. {
  6662. Mpi2IOCFactsRequest_t mpi_request;
  6663. Mpi2IOCFactsReply_t mpi_reply;
  6664. struct mpt3sas_facts *facts;
  6665. int mpi_reply_sz, mpi_request_sz, r;
  6666. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  6667. r = _base_wait_for_iocstate(ioc, 10);
  6668. if (r) {
  6669. dfailprintk(ioc,
  6670. ioc_info(ioc, "%s: failed getting to correct state\n",
  6671. __func__));
  6672. return r;
  6673. }
  6674. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  6675. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  6676. memset(&mpi_request, 0, mpi_request_sz);
  6677. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  6678. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  6679. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
  6680. if (r != 0) {
  6681. ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
  6682. return r;
  6683. }
  6684. facts = &ioc->facts;
  6685. memset(facts, 0, sizeof(struct mpt3sas_facts));
  6686. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  6687. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  6688. facts->VP_ID = mpi_reply.VP_ID;
  6689. facts->VF_ID = mpi_reply.VF_ID;
  6690. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  6691. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  6692. facts->WhoInit = mpi_reply.WhoInit;
  6693. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  6694. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  6695. if (ioc->msix_enable && (facts->MaxMSIxVectors <=
  6696. MAX_COMBINED_MSIX_VECTORS(ioc->is_gen35_ioc)))
  6697. ioc->combined_reply_queue = 0;
  6698. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  6699. facts->MaxReplyDescriptorPostQueueDepth =
  6700. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  6701. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  6702. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  6703. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  6704. ioc->ir_firmware = 1;
  6705. if ((facts->IOCCapabilities &
  6706. MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
  6707. ioc->rdpq_array_capable = 1;
  6708. if ((facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
  6709. && ioc->is_aero_ioc)
  6710. ioc->atomic_desc_capable = 1;
  6711. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  6712. facts->IOCRequestFrameSize =
  6713. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  6714. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  6715. facts->IOCMaxChainSegmentSize =
  6716. le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
  6717. }
  6718. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  6719. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  6720. ioc->shost->max_id = -1;
  6721. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  6722. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  6723. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  6724. facts->HighPriorityCredit =
  6725. le16_to_cpu(mpi_reply.HighPriorityCredit);
  6726. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  6727. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  6728. facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
  6729. /*
  6730. * Get the Page Size from IOC Facts. If it's 0, default to 4k.
  6731. */
  6732. ioc->page_size = 1 << facts->CurrentHostPageSize;
  6733. if (ioc->page_size == 1) {
  6734. ioc_info(ioc, "CurrentHostPageSize is 0: Setting default host page size to 4k\n");
  6735. ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
  6736. }
  6737. dinitprintk(ioc,
  6738. ioc_info(ioc, "CurrentHostPageSize(%d)\n",
  6739. facts->CurrentHostPageSize));
  6740. dinitprintk(ioc,
  6741. ioc_info(ioc, "hba queue depth(%d), max chains per io(%d)\n",
  6742. facts->RequestCredit, facts->MaxChainDepth));
  6743. dinitprintk(ioc,
  6744. ioc_info(ioc, "request frame size(%d), reply frame size(%d)\n",
  6745. facts->IOCRequestFrameSize * 4,
  6746. facts->ReplyFrameSize * 4));
  6747. return 0;
  6748. }
  6749. /**
  6750. * _base_send_ioc_init - send ioc_init to firmware
  6751. * @ioc: per adapter object
  6752. *
  6753. * Return: 0 for success, non-zero for failure.
  6754. */
  6755. static int
  6756. _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
  6757. {
  6758. Mpi2IOCInitRequest_t mpi_request;
  6759. Mpi2IOCInitReply_t mpi_reply;
  6760. int i, r = 0;
  6761. ktime_t current_time;
  6762. u16 ioc_status;
  6763. u32 reply_post_free_array_sz = 0;
  6764. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  6765. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  6766. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  6767. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  6768. mpi_request.VF_ID = 0; /* TODO */
  6769. mpi_request.VP_ID = 0;
  6770. mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
  6771. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  6772. mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
  6773. if (_base_is_controller_msix_enabled(ioc))
  6774. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  6775. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  6776. mpi_request.ReplyDescriptorPostQueueDepth =
  6777. cpu_to_le16(ioc->reply_post_queue_depth);
  6778. mpi_request.ReplyFreeQueueDepth =
  6779. cpu_to_le16(ioc->reply_free_queue_depth);
  6780. mpi_request.SenseBufferAddressHigh =
  6781. cpu_to_le32((u64)ioc->sense_dma >> 32);
  6782. mpi_request.SystemReplyAddressHigh =
  6783. cpu_to_le32((u64)ioc->reply_dma >> 32);
  6784. mpi_request.SystemRequestFrameBaseAddress =
  6785. cpu_to_le64((u64)ioc->request_dma);
  6786. mpi_request.ReplyFreeQueueAddress =
  6787. cpu_to_le64((u64)ioc->reply_free_dma);
  6788. if (ioc->rdpq_array_enable) {
  6789. reply_post_free_array_sz = ioc->reply_queue_count *
  6790. sizeof(Mpi2IOCInitRDPQArrayEntry);
  6791. memset(ioc->reply_post_free_array, 0, reply_post_free_array_sz);
  6792. for (i = 0; i < ioc->reply_queue_count; i++)
  6793. ioc->reply_post_free_array[i].RDPQBaseAddress =
  6794. cpu_to_le64(
  6795. (u64)ioc->reply_post[i].reply_post_free_dma);
  6796. mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
  6797. mpi_request.ReplyDescriptorPostQueueAddress =
  6798. cpu_to_le64((u64)ioc->reply_post_free_array_dma);
  6799. } else {
  6800. mpi_request.ReplyDescriptorPostQueueAddress =
  6801. cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
  6802. }
  6803. /*
  6804. * Set the flag to enable CoreDump state feature in IOC firmware.
  6805. */
  6806. mpi_request.ConfigurationFlags |=
  6807. cpu_to_le16(MPI26_IOCINIT_CFGFLAGS_COREDUMP_ENABLE);
  6808. /* This time stamp specifies number of milliseconds
  6809. * since epoch ~ midnight January 1, 1970.
  6810. */
  6811. current_time = ktime_get_real();
  6812. mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
  6813. if (ioc->logging_level & MPT_DEBUG_INIT) {
  6814. __le32 *mfp;
  6815. int i;
  6816. mfp = (__le32 *)&mpi_request;
  6817. ioc_info(ioc, "\toffset:data\n");
  6818. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  6819. ioc_info(ioc, "\t[0x%02x]:%08x\n", i*4,
  6820. le32_to_cpu(mfp[i]));
  6821. }
  6822. r = _base_handshake_req_reply_wait(ioc,
  6823. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  6824. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 30);
  6825. if (r != 0) {
  6826. ioc_err(ioc, "%s: handshake failed (r=%d)\n", __func__, r);
  6827. return r;
  6828. }
  6829. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  6830. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  6831. mpi_reply.IOCLogInfo) {
  6832. ioc_err(ioc, "%s: failed\n", __func__);
  6833. r = -EIO;
  6834. }
  6835. /* Reset TimeSync Counter*/
  6836. ioc->timestamp_update_count = 0;
  6837. return r;
  6838. }
  6839. /**
  6840. * mpt3sas_port_enable_done - command completion routine for port enable
  6841. * @ioc: per adapter object
  6842. * @smid: system request message index
  6843. * @msix_index: MSIX table index supplied by the OS
  6844. * @reply: reply message frame(lower 32bit addr)
  6845. *
  6846. * Return: 1 meaning mf should be freed from _base_interrupt
  6847. * 0 means the mf is freed from this function.
  6848. */
  6849. u8
  6850. mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  6851. u32 reply)
  6852. {
  6853. MPI2DefaultReply_t *mpi_reply;
  6854. u16 ioc_status;
  6855. if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
  6856. return 1;
  6857. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  6858. if (!mpi_reply)
  6859. return 1;
  6860. if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
  6861. return 1;
  6862. ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
  6863. ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
  6864. ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
  6865. memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  6866. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  6867. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  6868. ioc->port_enable_failed = 1;
  6869. if (ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE_ASYNC) {
  6870. ioc->port_enable_cmds.status &= ~MPT3_CMD_COMPLETE_ASYNC;
  6871. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  6872. mpt3sas_port_enable_complete(ioc);
  6873. return 1;
  6874. } else {
  6875. ioc->start_scan_failed = ioc_status;
  6876. ioc->start_scan = 0;
  6877. return 1;
  6878. }
  6879. }
  6880. complete(&ioc->port_enable_cmds.done);
  6881. return 1;
  6882. }
  6883. /**
  6884. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  6885. * @ioc: per adapter object
  6886. *
  6887. * Return: 0 for success, non-zero for failure.
  6888. */
  6889. static int
  6890. _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
  6891. {
  6892. Mpi2PortEnableRequest_t *mpi_request;
  6893. Mpi2PortEnableReply_t *mpi_reply;
  6894. int r = 0;
  6895. u16 smid;
  6896. u16 ioc_status;
  6897. ioc_info(ioc, "sending port enable !!\n");
  6898. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  6899. ioc_err(ioc, "%s: internal command already in use\n", __func__);
  6900. return -EAGAIN;
  6901. }
  6902. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  6903. if (!smid) {
  6904. ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
  6905. return -EAGAIN;
  6906. }
  6907. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  6908. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  6909. ioc->port_enable_cmds.smid = smid;
  6910. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  6911. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  6912. init_completion(&ioc->port_enable_cmds.done);
  6913. ioc->put_smid_default(ioc, smid);
  6914. wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
  6915. if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
  6916. ioc_err(ioc, "%s: timeout\n", __func__);
  6917. _debug_dump_mf(mpi_request,
  6918. sizeof(Mpi2PortEnableRequest_t)/4);
  6919. if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
  6920. r = -EFAULT;
  6921. else
  6922. r = -ETIME;
  6923. goto out;
  6924. }
  6925. mpi_reply = ioc->port_enable_cmds.reply;
  6926. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  6927. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6928. ioc_err(ioc, "%s: failed with (ioc_status=0x%08x)\n",
  6929. __func__, ioc_status);
  6930. r = -EFAULT;
  6931. goto out;
  6932. }
  6933. out:
  6934. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  6935. ioc_info(ioc, "port enable: %s\n", r == 0 ? "SUCCESS" : "FAILED");
  6936. return r;
  6937. }
  6938. /**
  6939. * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
  6940. * @ioc: per adapter object
  6941. *
  6942. * Return: 0 for success, non-zero for failure.
  6943. */
  6944. int
  6945. mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
  6946. {
  6947. Mpi2PortEnableRequest_t *mpi_request;
  6948. u16 smid;
  6949. ioc_info(ioc, "sending port enable !!\n");
  6950. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  6951. ioc_err(ioc, "%s: internal command already in use\n", __func__);
  6952. return -EAGAIN;
  6953. }
  6954. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  6955. if (!smid) {
  6956. ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
  6957. return -EAGAIN;
  6958. }
  6959. ioc->drv_internal_flags |= MPT_DRV_INTERNAL_FIRST_PE_ISSUED;
  6960. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  6961. ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE_ASYNC;
  6962. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  6963. ioc->port_enable_cmds.smid = smid;
  6964. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  6965. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  6966. ioc->put_smid_default(ioc, smid);
  6967. return 0;
  6968. }
  6969. /**
  6970. * _base_determine_wait_on_discovery - desposition
  6971. * @ioc: per adapter object
  6972. *
  6973. * Decide whether to wait on discovery to complete. Used to either
  6974. * locate boot device, or report volumes ahead of physical devices.
  6975. *
  6976. * Return: 1 for wait, 0 for don't wait.
  6977. */
  6978. static int
  6979. _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
  6980. {
  6981. /* We wait for discovery to complete if IR firmware is loaded.
  6982. * The sas topology events arrive before PD events, so we need time to
  6983. * turn on the bit in ioc->pd_handles to indicate PD
  6984. * Also, it maybe required to report Volumes ahead of physical
  6985. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  6986. */
  6987. if (ioc->ir_firmware)
  6988. return 1;
  6989. /* if no Bios, then we don't need to wait */
  6990. if (!ioc->bios_pg3.BiosVersion)
  6991. return 0;
  6992. /* Bios is present, then we drop down here.
  6993. *
  6994. * If there any entries in the Bios Page 2, then we wait
  6995. * for discovery to complete.
  6996. */
  6997. /* Current Boot Device */
  6998. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  6999. MPI2_BIOSPAGE2_FORM_MASK) ==
  7000. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  7001. /* Request Boot Device */
  7002. (ioc->bios_pg2.ReqBootDeviceForm &
  7003. MPI2_BIOSPAGE2_FORM_MASK) ==
  7004. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  7005. /* Alternate Request Boot Device */
  7006. (ioc->bios_pg2.ReqAltBootDeviceForm &
  7007. MPI2_BIOSPAGE2_FORM_MASK) ==
  7008. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  7009. return 0;
  7010. return 1;
  7011. }
  7012. /**
  7013. * _base_unmask_events - turn on notification for this event
  7014. * @ioc: per adapter object
  7015. * @event: firmware event
  7016. *
  7017. * The mask is stored in ioc->event_masks.
  7018. */
  7019. static void
  7020. _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
  7021. {
  7022. u32 desired_event;
  7023. if (event >= 128)
  7024. return;
  7025. desired_event = (1 << (event % 32));
  7026. if (event < 32)
  7027. ioc->event_masks[0] &= ~desired_event;
  7028. else if (event < 64)
  7029. ioc->event_masks[1] &= ~desired_event;
  7030. else if (event < 96)
  7031. ioc->event_masks[2] &= ~desired_event;
  7032. else if (event < 128)
  7033. ioc->event_masks[3] &= ~desired_event;
  7034. }
  7035. /**
  7036. * _base_event_notification - send event notification
  7037. * @ioc: per adapter object
  7038. *
  7039. * Return: 0 for success, non-zero for failure.
  7040. */
  7041. static int
  7042. _base_event_notification(struct MPT3SAS_ADAPTER *ioc)
  7043. {
  7044. Mpi2EventNotificationRequest_t *mpi_request;
  7045. u16 smid;
  7046. int r = 0;
  7047. int i, issue_diag_reset = 0;
  7048. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  7049. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  7050. ioc_err(ioc, "%s: internal command already in use\n", __func__);
  7051. return -EAGAIN;
  7052. }
  7053. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  7054. if (!smid) {
  7055. ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
  7056. return -EAGAIN;
  7057. }
  7058. ioc->base_cmds.status = MPT3_CMD_PENDING;
  7059. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  7060. ioc->base_cmds.smid = smid;
  7061. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  7062. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  7063. mpi_request->VF_ID = 0; /* TODO */
  7064. mpi_request->VP_ID = 0;
  7065. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  7066. mpi_request->EventMasks[i] =
  7067. cpu_to_le32(ioc->event_masks[i]);
  7068. init_completion(&ioc->base_cmds.done);
  7069. ioc->put_smid_default(ioc, smid);
  7070. wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  7071. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  7072. ioc_err(ioc, "%s: timeout\n", __func__);
  7073. _debug_dump_mf(mpi_request,
  7074. sizeof(Mpi2EventNotificationRequest_t)/4);
  7075. if (ioc->base_cmds.status & MPT3_CMD_RESET)
  7076. r = -EFAULT;
  7077. else
  7078. issue_diag_reset = 1;
  7079. } else
  7080. dinitprintk(ioc, ioc_info(ioc, "%s: complete\n", __func__));
  7081. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  7082. if (issue_diag_reset) {
  7083. if (ioc->drv_internal_flags & MPT_DRV_INTERNAL_FIRST_PE_ISSUED)
  7084. return -EFAULT;
  7085. if (mpt3sas_base_check_for_fault_and_issue_reset(ioc))
  7086. return -EFAULT;
  7087. r = -EAGAIN;
  7088. }
  7089. return r;
  7090. }
  7091. /**
  7092. * mpt3sas_base_validate_event_type - validating event types
  7093. * @ioc: per adapter object
  7094. * @event_type: firmware event
  7095. *
  7096. * This will turn on firmware event notification when application
  7097. * ask for that event. We don't mask events that are already enabled.
  7098. */
  7099. void
  7100. mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
  7101. {
  7102. int i, j;
  7103. u32 event_mask, desired_event;
  7104. u8 send_update_to_fw;
  7105. for (i = 0, send_update_to_fw = 0; i <
  7106. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  7107. event_mask = ~event_type[i];
  7108. desired_event = 1;
  7109. for (j = 0; j < 32; j++) {
  7110. if (!(event_mask & desired_event) &&
  7111. (ioc->event_masks[i] & desired_event)) {
  7112. ioc->event_masks[i] &= ~desired_event;
  7113. send_update_to_fw = 1;
  7114. }
  7115. desired_event = (desired_event << 1);
  7116. }
  7117. }
  7118. if (!send_update_to_fw)
  7119. return;
  7120. mutex_lock(&ioc->base_cmds.mutex);
  7121. _base_event_notification(ioc);
  7122. mutex_unlock(&ioc->base_cmds.mutex);
  7123. }
  7124. /**
  7125. * _base_diag_reset - the "big hammer" start of day reset
  7126. * @ioc: per adapter object
  7127. *
  7128. * Return: 0 for success, non-zero for failure.
  7129. */
  7130. static int
  7131. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
  7132. {
  7133. u32 host_diagnostic;
  7134. u32 ioc_state;
  7135. u32 count;
  7136. u32 hcb_size;
  7137. ioc_info(ioc, "sending diag reset !!\n");
  7138. pci_cfg_access_lock(ioc->pdev);
  7139. drsprintk(ioc, ioc_info(ioc, "clear interrupts\n"));
  7140. count = 0;
  7141. do {
  7142. /* Write magic sequence to WriteSequence register
  7143. * Loop until in diagnostic mode
  7144. */
  7145. drsprintk(ioc, ioc_info(ioc, "write magic sequence\n"));
  7146. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  7147. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  7148. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  7149. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  7150. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  7151. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  7152. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  7153. /* wait 100 msec */
  7154. msleep(100);
  7155. if (count++ > 20) {
  7156. ioc_info(ioc,
  7157. "Stop writing magic sequence after 20 retries\n");
  7158. _base_dump_reg_set(ioc);
  7159. goto out;
  7160. }
  7161. host_diagnostic = ioc->base_readl_ext_retry(&ioc->chip->HostDiagnostic);
  7162. drsprintk(ioc,
  7163. ioc_info(ioc, "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
  7164. count, host_diagnostic));
  7165. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  7166. hcb_size = ioc->base_readl(&ioc->chip->HCBSize);
  7167. drsprintk(ioc, ioc_info(ioc, "diag reset: issued\n"));
  7168. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  7169. &ioc->chip->HostDiagnostic);
  7170. /*This delay allows the chip PCIe hardware time to finish reset tasks*/
  7171. msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
  7172. /* Approximately 300 second max wait */
  7173. for (count = 0; count < (300000000 /
  7174. MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
  7175. host_diagnostic = ioc->base_readl_ext_retry(&ioc->chip->HostDiagnostic);
  7176. if (host_diagnostic == 0xFFFFFFFF) {
  7177. ioc_info(ioc,
  7178. "Invalid host diagnostic register value\n");
  7179. _base_dump_reg_set(ioc);
  7180. goto out;
  7181. }
  7182. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  7183. break;
  7184. msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
  7185. }
  7186. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  7187. drsprintk(ioc,
  7188. ioc_info(ioc, "restart the adapter assuming the HCB Address points to good F/W\n"));
  7189. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  7190. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  7191. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  7192. drsprintk(ioc, ioc_info(ioc, "re-enable the HCDW\n"));
  7193. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  7194. &ioc->chip->HCBSize);
  7195. }
  7196. drsprintk(ioc, ioc_info(ioc, "restart the adapter\n"));
  7197. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  7198. &ioc->chip->HostDiagnostic);
  7199. drsprintk(ioc,
  7200. ioc_info(ioc, "disable writes to the diagnostic register\n"));
  7201. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  7202. drsprintk(ioc, ioc_info(ioc, "Wait for FW to go to the READY state\n"));
  7203. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
  7204. if (ioc_state) {
  7205. ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
  7206. __func__, ioc_state);
  7207. _base_dump_reg_set(ioc);
  7208. goto out;
  7209. }
  7210. pci_cfg_access_unlock(ioc->pdev);
  7211. ioc_info(ioc, "diag reset: SUCCESS\n");
  7212. return 0;
  7213. out:
  7214. pci_cfg_access_unlock(ioc->pdev);
  7215. ioc_err(ioc, "diag reset: FAILED\n");
  7216. return -EFAULT;
  7217. }
  7218. /**
  7219. * mpt3sas_base_make_ioc_ready - put controller in READY state
  7220. * @ioc: per adapter object
  7221. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  7222. *
  7223. * Return: 0 for success, non-zero for failure.
  7224. */
  7225. int
  7226. mpt3sas_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
  7227. {
  7228. u32 ioc_state;
  7229. int rc;
  7230. int count;
  7231. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  7232. if (ioc->pci_error_recovery)
  7233. return 0;
  7234. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  7235. dhsprintk(ioc,
  7236. ioc_info(ioc, "%s: ioc_state(0x%08x)\n",
  7237. __func__, ioc_state));
  7238. /* if in RESET state, it should move to READY state shortly */
  7239. count = 0;
  7240. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
  7241. while ((ioc_state & MPI2_IOC_STATE_MASK) !=
  7242. MPI2_IOC_STATE_READY) {
  7243. if (count++ == 10) {
  7244. ioc_err(ioc, "%s: failed going to ready state (ioc_state=0x%x)\n",
  7245. __func__, ioc_state);
  7246. return -EFAULT;
  7247. }
  7248. ssleep(1);
  7249. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  7250. }
  7251. }
  7252. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  7253. return 0;
  7254. if (ioc_state & MPI2_DOORBELL_USED) {
  7255. ioc_info(ioc, "unexpected doorbell active!\n");
  7256. goto issue_diag_reset;
  7257. }
  7258. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  7259. mpt3sas_print_fault_code(ioc, ioc_state &
  7260. MPI2_DOORBELL_DATA_MASK);
  7261. goto issue_diag_reset;
  7262. }
  7263. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_COREDUMP) {
  7264. /*
  7265. * if host reset is invoked while watch dog thread is waiting
  7266. * for IOC state to be changed to Fault state then driver has
  7267. * to wait here for CoreDump state to clear otherwise reset
  7268. * will be issued to the FW and FW move the IOC state to
  7269. * reset state without copying the FW logs to coredump region.
  7270. */
  7271. if (ioc->ioc_coredump_loop != MPT3SAS_COREDUMP_LOOP_DONE) {
  7272. mpt3sas_print_coredump_info(ioc, ioc_state &
  7273. MPI2_DOORBELL_DATA_MASK);
  7274. mpt3sas_base_wait_for_coredump_completion(ioc,
  7275. __func__);
  7276. }
  7277. goto issue_diag_reset;
  7278. }
  7279. if (type == FORCE_BIG_HAMMER)
  7280. goto issue_diag_reset;
  7281. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  7282. if (!(_base_send_ioc_reset(ioc,
  7283. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
  7284. return 0;
  7285. }
  7286. issue_diag_reset:
  7287. rc = _base_diag_reset(ioc);
  7288. return rc;
  7289. }
  7290. /**
  7291. * _base_make_ioc_operational - put controller in OPERATIONAL state
  7292. * @ioc: per adapter object
  7293. *
  7294. * Return: 0 for success, non-zero for failure.
  7295. */
  7296. static int
  7297. _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
  7298. {
  7299. int r, i, index, rc;
  7300. unsigned long flags;
  7301. u32 reply_address;
  7302. u16 smid;
  7303. struct _tr_list *delayed_tr, *delayed_tr_next;
  7304. struct _sc_list *delayed_sc, *delayed_sc_next;
  7305. struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
  7306. u8 hide_flag;
  7307. struct adapter_reply_queue *reply_q;
  7308. Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
  7309. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  7310. /* clean the delayed target reset list */
  7311. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  7312. &ioc->delayed_tr_list, list) {
  7313. list_del(&delayed_tr->list);
  7314. kfree(delayed_tr);
  7315. }
  7316. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  7317. &ioc->delayed_tr_volume_list, list) {
  7318. list_del(&delayed_tr->list);
  7319. kfree(delayed_tr);
  7320. }
  7321. list_for_each_entry_safe(delayed_sc, delayed_sc_next,
  7322. &ioc->delayed_sc_list, list) {
  7323. list_del(&delayed_sc->list);
  7324. kfree(delayed_sc);
  7325. }
  7326. list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
  7327. &ioc->delayed_event_ack_list, list) {
  7328. list_del(&delayed_event_ack->list);
  7329. kfree(delayed_event_ack);
  7330. }
  7331. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  7332. /* hi-priority queue */
  7333. INIT_LIST_HEAD(&ioc->hpr_free_list);
  7334. smid = ioc->hi_priority_smid;
  7335. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  7336. ioc->hpr_lookup[i].cb_idx = 0xFF;
  7337. ioc->hpr_lookup[i].smid = smid;
  7338. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  7339. &ioc->hpr_free_list);
  7340. }
  7341. /* internal queue */
  7342. INIT_LIST_HEAD(&ioc->internal_free_list);
  7343. smid = ioc->internal_smid;
  7344. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  7345. ioc->internal_lookup[i].cb_idx = 0xFF;
  7346. ioc->internal_lookup[i].smid = smid;
  7347. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  7348. &ioc->internal_free_list);
  7349. }
  7350. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  7351. /* initialize Reply Free Queue */
  7352. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  7353. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  7354. ioc->reply_sz) {
  7355. ioc->reply_free[i] = cpu_to_le32(reply_address);
  7356. if (ioc->is_mcpu_endpoint)
  7357. _base_clone_reply_to_sys_mem(ioc,
  7358. reply_address, i);
  7359. }
  7360. /* initialize reply queues */
  7361. if (ioc->is_driver_loading)
  7362. _base_assign_reply_queues(ioc);
  7363. /* initialize Reply Post Free Queue */
  7364. index = 0;
  7365. reply_post_free_contig = ioc->reply_post[0].reply_post_free;
  7366. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  7367. /*
  7368. * If RDPQ is enabled, switch to the next allocation.
  7369. * Otherwise advance within the contiguous region.
  7370. */
  7371. if (ioc->rdpq_array_enable) {
  7372. reply_q->reply_post_free =
  7373. ioc->reply_post[index++].reply_post_free;
  7374. } else {
  7375. reply_q->reply_post_free = reply_post_free_contig;
  7376. reply_post_free_contig += ioc->reply_post_queue_depth;
  7377. }
  7378. reply_q->reply_post_host_index = 0;
  7379. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  7380. reply_q->reply_post_free[i].Words =
  7381. cpu_to_le64(ULLONG_MAX);
  7382. if (!_base_is_controller_msix_enabled(ioc))
  7383. goto skip_init_reply_post_free_queue;
  7384. }
  7385. skip_init_reply_post_free_queue:
  7386. r = _base_send_ioc_init(ioc);
  7387. if (r) {
  7388. /*
  7389. * No need to check IOC state for fault state & issue
  7390. * diag reset during host reset. This check is need
  7391. * only during driver load time.
  7392. */
  7393. if (!ioc->is_driver_loading)
  7394. return r;
  7395. rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
  7396. if (rc || (_base_send_ioc_init(ioc)))
  7397. return r;
  7398. }
  7399. /* initialize reply free host index */
  7400. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  7401. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  7402. /* initialize reply post host index */
  7403. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  7404. if (ioc->combined_reply_queue)
  7405. writel((reply_q->msix_index & 7)<<
  7406. MPI2_RPHI_MSIX_INDEX_SHIFT,
  7407. ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
  7408. else
  7409. writel(reply_q->msix_index <<
  7410. MPI2_RPHI_MSIX_INDEX_SHIFT,
  7411. &ioc->chip->ReplyPostHostIndex);
  7412. if (!_base_is_controller_msix_enabled(ioc))
  7413. goto skip_init_reply_post_host_index;
  7414. }
  7415. skip_init_reply_post_host_index:
  7416. mpt3sas_base_unmask_interrupts(ioc);
  7417. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  7418. r = _base_display_fwpkg_version(ioc);
  7419. if (r)
  7420. return r;
  7421. }
  7422. r = _base_static_config_pages(ioc);
  7423. if (r)
  7424. return r;
  7425. r = _base_event_notification(ioc);
  7426. if (r)
  7427. return r;
  7428. if (!ioc->shost_recovery) {
  7429. if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
  7430. == 0x80) {
  7431. hide_flag = (u8) (
  7432. le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
  7433. MFG_PAGE10_HIDE_SSDS_MASK);
  7434. if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
  7435. ioc->mfg_pg10_hide_flag = hide_flag;
  7436. }
  7437. ioc->wait_for_discovery_to_complete =
  7438. _base_determine_wait_on_discovery(ioc);
  7439. return r; /* scan_start and scan_finished support */
  7440. }
  7441. r = _base_send_port_enable(ioc);
  7442. if (r)
  7443. return r;
  7444. return r;
  7445. }
  7446. /**
  7447. * mpt3sas_base_free_resources - free resources controller resources
  7448. * @ioc: per adapter object
  7449. */
  7450. void
  7451. mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
  7452. {
  7453. dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  7454. /* synchronizing freeing resource with pci_access_mutex lock */
  7455. mutex_lock(&ioc->pci_access_mutex);
  7456. if (ioc->chip_phys && ioc->chip) {
  7457. mpt3sas_base_mask_interrupts(ioc);
  7458. ioc->shost_recovery = 1;
  7459. mpt3sas_base_make_ioc_ready(ioc, SOFT_RESET);
  7460. ioc->shost_recovery = 0;
  7461. }
  7462. mpt3sas_base_unmap_resources(ioc);
  7463. mutex_unlock(&ioc->pci_access_mutex);
  7464. return;
  7465. }
  7466. /**
  7467. * mpt3sas_base_attach - attach controller instance
  7468. * @ioc: per adapter object
  7469. *
  7470. * Return: 0 for success, non-zero for failure.
  7471. */
  7472. int
  7473. mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
  7474. {
  7475. int r, i, rc;
  7476. int cpu_id, last_cpu_id = 0;
  7477. dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  7478. /* setup cpu_msix_table */
  7479. ioc->cpu_count = num_online_cpus();
  7480. for_each_online_cpu(cpu_id)
  7481. last_cpu_id = cpu_id;
  7482. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  7483. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  7484. ioc->reply_queue_count = 1;
  7485. if (!ioc->cpu_msix_table) {
  7486. ioc_info(ioc, "Allocation for cpu_msix_table failed!!!\n");
  7487. r = -ENOMEM;
  7488. goto out_free_resources;
  7489. }
  7490. if (ioc->is_warpdrive) {
  7491. ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
  7492. sizeof(resource_size_t *), GFP_KERNEL);
  7493. if (!ioc->reply_post_host_index) {
  7494. ioc_info(ioc, "Allocation for reply_post_host_index failed!!!\n");
  7495. r = -ENOMEM;
  7496. goto out_free_resources;
  7497. }
  7498. }
  7499. ioc->smp_affinity_enable = smp_affinity_enable;
  7500. ioc->rdpq_array_enable_assigned = 0;
  7501. ioc->use_32bit_dma = false;
  7502. ioc->dma_mask = 64;
  7503. if (ioc->is_aero_ioc) {
  7504. ioc->base_readl = &_base_readl_aero;
  7505. ioc->base_readl_ext_retry = &_base_readl_ext_retry;
  7506. } else {
  7507. ioc->base_readl = &_base_readl;
  7508. ioc->base_readl_ext_retry = &_base_readl;
  7509. }
  7510. r = mpt3sas_base_map_resources(ioc);
  7511. if (r)
  7512. goto out_free_resources;
  7513. pci_set_drvdata(ioc->pdev, ioc->shost);
  7514. r = _base_get_ioc_facts(ioc);
  7515. if (r) {
  7516. rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
  7517. if (rc || (_base_get_ioc_facts(ioc)))
  7518. goto out_free_resources;
  7519. }
  7520. switch (ioc->hba_mpi_version_belonged) {
  7521. case MPI2_VERSION:
  7522. ioc->build_sg_scmd = &_base_build_sg_scmd;
  7523. ioc->build_sg = &_base_build_sg;
  7524. ioc->build_zero_len_sge = &_base_build_zero_len_sge;
  7525. ioc->get_msix_index_for_smlio = &_base_get_msix_index;
  7526. break;
  7527. case MPI25_VERSION:
  7528. case MPI26_VERSION:
  7529. /*
  7530. * In SAS3.0,
  7531. * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
  7532. * Target Status - all require the IEEE formatted scatter gather
  7533. * elements.
  7534. */
  7535. ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
  7536. ioc->build_sg = &_base_build_sg_ieee;
  7537. ioc->build_nvme_prp = &_base_build_nvme_prp;
  7538. ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
  7539. ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
  7540. if (ioc->high_iops_queues)
  7541. ioc->get_msix_index_for_smlio =
  7542. &_base_get_high_iops_msix_index;
  7543. else
  7544. ioc->get_msix_index_for_smlio = &_base_get_msix_index;
  7545. break;
  7546. }
  7547. if (ioc->atomic_desc_capable) {
  7548. ioc->put_smid_default = &_base_put_smid_default_atomic;
  7549. ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
  7550. ioc->put_smid_fast_path =
  7551. &_base_put_smid_fast_path_atomic;
  7552. ioc->put_smid_hi_priority =
  7553. &_base_put_smid_hi_priority_atomic;
  7554. } else {
  7555. ioc->put_smid_default = &_base_put_smid_default;
  7556. ioc->put_smid_fast_path = &_base_put_smid_fast_path;
  7557. ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
  7558. if (ioc->is_mcpu_endpoint)
  7559. ioc->put_smid_scsi_io =
  7560. &_base_put_smid_mpi_ep_scsi_io;
  7561. else
  7562. ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
  7563. }
  7564. /*
  7565. * These function pointers for other requests that don't
  7566. * the require IEEE scatter gather elements.
  7567. *
  7568. * For example Configuration Pages and SAS IOUNIT Control don't.
  7569. */
  7570. ioc->build_sg_mpi = &_base_build_sg;
  7571. ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
  7572. r = mpt3sas_base_make_ioc_ready(ioc, SOFT_RESET);
  7573. if (r)
  7574. goto out_free_resources;
  7575. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  7576. sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
  7577. if (!ioc->pfacts) {
  7578. r = -ENOMEM;
  7579. goto out_free_resources;
  7580. }
  7581. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  7582. r = _base_get_port_facts(ioc, i);
  7583. if (r) {
  7584. rc = mpt3sas_base_check_for_fault_and_issue_reset(ioc);
  7585. if (rc || (_base_get_port_facts(ioc, i)))
  7586. goto out_free_resources;
  7587. }
  7588. }
  7589. r = _base_allocate_memory_pools(ioc);
  7590. if (r)
  7591. goto out_free_resources;
  7592. if (irqpoll_weight > 0)
  7593. ioc->thresh_hold = irqpoll_weight;
  7594. else
  7595. ioc->thresh_hold = ioc->hba_queue_depth/4;
  7596. _base_init_irqpolls(ioc);
  7597. init_waitqueue_head(&ioc->reset_wq);
  7598. /* allocate memory pd handle bitmask list */
  7599. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  7600. if (ioc->facts.MaxDevHandle % 8)
  7601. ioc->pd_handles_sz++;
  7602. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  7603. GFP_KERNEL);
  7604. if (!ioc->pd_handles) {
  7605. r = -ENOMEM;
  7606. goto out_free_resources;
  7607. }
  7608. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  7609. GFP_KERNEL);
  7610. if (!ioc->blocking_handles) {
  7611. r = -ENOMEM;
  7612. goto out_free_resources;
  7613. }
  7614. /* allocate memory for pending OS device add list */
  7615. ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
  7616. if (ioc->facts.MaxDevHandle % 8)
  7617. ioc->pend_os_device_add_sz++;
  7618. ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
  7619. GFP_KERNEL);
  7620. if (!ioc->pend_os_device_add) {
  7621. r = -ENOMEM;
  7622. goto out_free_resources;
  7623. }
  7624. ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
  7625. ioc->device_remove_in_progress =
  7626. kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
  7627. if (!ioc->device_remove_in_progress) {
  7628. r = -ENOMEM;
  7629. goto out_free_resources;
  7630. }
  7631. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  7632. /* base internal command bits */
  7633. mutex_init(&ioc->base_cmds.mutex);
  7634. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7635. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  7636. /* port_enable command bits */
  7637. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7638. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  7639. /* transport internal command bits */
  7640. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7641. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  7642. mutex_init(&ioc->transport_cmds.mutex);
  7643. /* scsih internal command bits */
  7644. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7645. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  7646. mutex_init(&ioc->scsih_cmds.mutex);
  7647. /* task management internal command bits */
  7648. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7649. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  7650. mutex_init(&ioc->tm_cmds.mutex);
  7651. /* config page internal command bits */
  7652. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7653. ioc->config_cmds.status = MPT3_CMD_NOT_USED;
  7654. mutex_init(&ioc->config_cmds.mutex);
  7655. /* ctl module internal command bits */
  7656. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  7657. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  7658. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  7659. mutex_init(&ioc->ctl_cmds.mutex);
  7660. if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
  7661. !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
  7662. !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
  7663. !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
  7664. r = -ENOMEM;
  7665. goto out_free_resources;
  7666. }
  7667. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  7668. ioc->event_masks[i] = -1;
  7669. /* here we enable the events we care about */
  7670. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  7671. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  7672. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  7673. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  7674. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  7675. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  7676. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  7677. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  7678. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  7679. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  7680. _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
  7681. _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
  7682. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
  7683. if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
  7684. if (ioc->is_gen35_ioc) {
  7685. _base_unmask_events(ioc,
  7686. MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
  7687. _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
  7688. _base_unmask_events(ioc,
  7689. MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
  7690. }
  7691. }
  7692. r = _base_make_ioc_operational(ioc);
  7693. if (r == -EAGAIN) {
  7694. r = _base_make_ioc_operational(ioc);
  7695. if (r)
  7696. goto out_free_resources;
  7697. }
  7698. /*
  7699. * Copy current copy of IOCFacts in prev_fw_facts
  7700. * and it will be used during online firmware upgrade.
  7701. */
  7702. memcpy(&ioc->prev_fw_facts, &ioc->facts,
  7703. sizeof(struct mpt3sas_facts));
  7704. ioc->non_operational_loop = 0;
  7705. ioc->ioc_coredump_loop = 0;
  7706. ioc->got_task_abort_from_ioctl = 0;
  7707. return 0;
  7708. out_free_resources:
  7709. ioc->remove_host = 1;
  7710. mpt3sas_base_free_resources(ioc);
  7711. _base_release_memory_pools(ioc);
  7712. pci_set_drvdata(ioc->pdev, NULL);
  7713. kfree(ioc->cpu_msix_table);
  7714. if (ioc->is_warpdrive)
  7715. kfree(ioc->reply_post_host_index);
  7716. kfree(ioc->pd_handles);
  7717. kfree(ioc->blocking_handles);
  7718. kfree(ioc->device_remove_in_progress);
  7719. kfree(ioc->pend_os_device_add);
  7720. kfree(ioc->tm_cmds.reply);
  7721. kfree(ioc->transport_cmds.reply);
  7722. kfree(ioc->scsih_cmds.reply);
  7723. kfree(ioc->config_cmds.reply);
  7724. kfree(ioc->base_cmds.reply);
  7725. kfree(ioc->port_enable_cmds.reply);
  7726. kfree(ioc->ctl_cmds.reply);
  7727. kfree(ioc->ctl_cmds.sense);
  7728. kfree(ioc->pfacts);
  7729. ioc->ctl_cmds.reply = NULL;
  7730. ioc->base_cmds.reply = NULL;
  7731. ioc->tm_cmds.reply = NULL;
  7732. ioc->scsih_cmds.reply = NULL;
  7733. ioc->transport_cmds.reply = NULL;
  7734. ioc->config_cmds.reply = NULL;
  7735. ioc->pfacts = NULL;
  7736. return r;
  7737. }
  7738. /**
  7739. * mpt3sas_base_detach - remove controller instance
  7740. * @ioc: per adapter object
  7741. */
  7742. void
  7743. mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
  7744. {
  7745. dexitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
  7746. mpt3sas_base_stop_watchdog(ioc);
  7747. mpt3sas_base_free_resources(ioc);
  7748. _base_release_memory_pools(ioc);
  7749. mpt3sas_free_enclosure_list(ioc);
  7750. pci_set_drvdata(ioc->pdev, NULL);
  7751. kfree(ioc->cpu_msix_table);
  7752. if (ioc->is_warpdrive)
  7753. kfree(ioc->reply_post_host_index);
  7754. kfree(ioc->pd_handles);
  7755. kfree(ioc->blocking_handles);
  7756. kfree(ioc->device_remove_in_progress);
  7757. kfree(ioc->pend_os_device_add);
  7758. kfree(ioc->pfacts);
  7759. kfree(ioc->ctl_cmds.reply);
  7760. kfree(ioc->ctl_cmds.sense);
  7761. kfree(ioc->base_cmds.reply);
  7762. kfree(ioc->port_enable_cmds.reply);
  7763. kfree(ioc->tm_cmds.reply);
  7764. kfree(ioc->transport_cmds.reply);
  7765. kfree(ioc->scsih_cmds.reply);
  7766. kfree(ioc->config_cmds.reply);
  7767. }
  7768. /**
  7769. * _base_pre_reset_handler - pre reset handler
  7770. * @ioc: per adapter object
  7771. */
  7772. static void _base_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
  7773. {
  7774. mpt3sas_scsih_pre_reset_handler(ioc);
  7775. mpt3sas_ctl_pre_reset_handler(ioc);
  7776. dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
  7777. }
  7778. /**
  7779. * _base_clear_outstanding_mpt_commands - clears outstanding mpt commands
  7780. * @ioc: per adapter object
  7781. */
  7782. static void
  7783. _base_clear_outstanding_mpt_commands(struct MPT3SAS_ADAPTER *ioc)
  7784. {
  7785. dtmprintk(ioc,
  7786. ioc_info(ioc, "%s: clear outstanding mpt cmds\n", __func__));
  7787. if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
  7788. ioc->transport_cmds.status |= MPT3_CMD_RESET;
  7789. mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  7790. complete(&ioc->transport_cmds.done);
  7791. }
  7792. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  7793. ioc->base_cmds.status |= MPT3_CMD_RESET;
  7794. mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
  7795. complete(&ioc->base_cmds.done);
  7796. }
  7797. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  7798. ioc->port_enable_failed = 1;
  7799. ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
  7800. mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  7801. if (ioc->is_driver_loading) {
  7802. ioc->start_scan_failed =
  7803. MPI2_IOCSTATUS_INTERNAL_ERROR;
  7804. ioc->start_scan = 0;
  7805. } else {
  7806. complete(&ioc->port_enable_cmds.done);
  7807. }
  7808. }
  7809. if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
  7810. ioc->config_cmds.status |= MPT3_CMD_RESET;
  7811. mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
  7812. ioc->config_cmds.smid = USHRT_MAX;
  7813. complete(&ioc->config_cmds.done);
  7814. }
  7815. }
  7816. /**
  7817. * _base_clear_outstanding_commands - clear all outstanding commands
  7818. * @ioc: per adapter object
  7819. */
  7820. static void _base_clear_outstanding_commands(struct MPT3SAS_ADAPTER *ioc)
  7821. {
  7822. mpt3sas_scsih_clear_outstanding_scsi_tm_commands(ioc);
  7823. mpt3sas_ctl_clear_outstanding_ioctls(ioc);
  7824. _base_clear_outstanding_mpt_commands(ioc);
  7825. }
  7826. /**
  7827. * _base_reset_done_handler - reset done handler
  7828. * @ioc: per adapter object
  7829. */
  7830. static void _base_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
  7831. {
  7832. mpt3sas_scsih_reset_done_handler(ioc);
  7833. mpt3sas_ctl_reset_done_handler(ioc);
  7834. dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
  7835. }
  7836. /**
  7837. * mpt3sas_wait_for_commands_to_complete - reset controller
  7838. * @ioc: Pointer to MPT_ADAPTER structure
  7839. *
  7840. * This function is waiting 10s for all pending commands to complete
  7841. * prior to putting controller in reset.
  7842. */
  7843. void
  7844. mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
  7845. {
  7846. u32 ioc_state;
  7847. ioc->pending_io_count = 0;
  7848. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  7849. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  7850. return;
  7851. /* pending command count */
  7852. ioc->pending_io_count = scsi_host_busy(ioc->shost);
  7853. if (!ioc->pending_io_count)
  7854. return;
  7855. /* wait for pending commands to complete */
  7856. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  7857. }
  7858. /**
  7859. * _base_check_ioc_facts_changes - Look for increase/decrease of IOCFacts
  7860. * attributes during online firmware upgrade and update the corresponding
  7861. * IOC variables accordingly.
  7862. *
  7863. * @ioc: Pointer to MPT_ADAPTER structure
  7864. */
  7865. static int
  7866. _base_check_ioc_facts_changes(struct MPT3SAS_ADAPTER *ioc)
  7867. {
  7868. u16 pd_handles_sz;
  7869. void *pd_handles = NULL, *blocking_handles = NULL;
  7870. void *pend_os_device_add = NULL, *device_remove_in_progress = NULL;
  7871. struct mpt3sas_facts *old_facts = &ioc->prev_fw_facts;
  7872. if (ioc->facts.MaxDevHandle > old_facts->MaxDevHandle) {
  7873. pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  7874. if (ioc->facts.MaxDevHandle % 8)
  7875. pd_handles_sz++;
  7876. pd_handles = krealloc(ioc->pd_handles, pd_handles_sz,
  7877. GFP_KERNEL);
  7878. if (!pd_handles) {
  7879. ioc_info(ioc,
  7880. "Unable to allocate the memory for pd_handles of sz: %d\n",
  7881. pd_handles_sz);
  7882. return -ENOMEM;
  7883. }
  7884. memset(pd_handles + ioc->pd_handles_sz, 0,
  7885. (pd_handles_sz - ioc->pd_handles_sz));
  7886. ioc->pd_handles = pd_handles;
  7887. blocking_handles = krealloc(ioc->blocking_handles,
  7888. pd_handles_sz, GFP_KERNEL);
  7889. if (!blocking_handles) {
  7890. ioc_info(ioc,
  7891. "Unable to allocate the memory for "
  7892. "blocking_handles of sz: %d\n",
  7893. pd_handles_sz);
  7894. return -ENOMEM;
  7895. }
  7896. memset(blocking_handles + ioc->pd_handles_sz, 0,
  7897. (pd_handles_sz - ioc->pd_handles_sz));
  7898. ioc->blocking_handles = blocking_handles;
  7899. ioc->pd_handles_sz = pd_handles_sz;
  7900. pend_os_device_add = krealloc(ioc->pend_os_device_add,
  7901. pd_handles_sz, GFP_KERNEL);
  7902. if (!pend_os_device_add) {
  7903. ioc_info(ioc,
  7904. "Unable to allocate the memory for pend_os_device_add of sz: %d\n",
  7905. pd_handles_sz);
  7906. return -ENOMEM;
  7907. }
  7908. memset(pend_os_device_add + ioc->pend_os_device_add_sz, 0,
  7909. (pd_handles_sz - ioc->pend_os_device_add_sz));
  7910. ioc->pend_os_device_add = pend_os_device_add;
  7911. ioc->pend_os_device_add_sz = pd_handles_sz;
  7912. device_remove_in_progress = krealloc(
  7913. ioc->device_remove_in_progress, pd_handles_sz, GFP_KERNEL);
  7914. if (!device_remove_in_progress) {
  7915. ioc_info(ioc,
  7916. "Unable to allocate the memory for "
  7917. "device_remove_in_progress of sz: %d\n "
  7918. , pd_handles_sz);
  7919. return -ENOMEM;
  7920. }
  7921. memset(device_remove_in_progress +
  7922. ioc->device_remove_in_progress_sz, 0,
  7923. (pd_handles_sz - ioc->device_remove_in_progress_sz));
  7924. ioc->device_remove_in_progress = device_remove_in_progress;
  7925. ioc->device_remove_in_progress_sz = pd_handles_sz;
  7926. }
  7927. memcpy(&ioc->prev_fw_facts, &ioc->facts, sizeof(struct mpt3sas_facts));
  7928. return 0;
  7929. }
  7930. /**
  7931. * mpt3sas_base_hard_reset_handler - reset controller
  7932. * @ioc: Pointer to MPT_ADAPTER structure
  7933. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  7934. *
  7935. * Return: 0 for success, non-zero for failure.
  7936. */
  7937. int
  7938. mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
  7939. enum reset_type type)
  7940. {
  7941. int r;
  7942. unsigned long flags;
  7943. u32 ioc_state;
  7944. u8 is_fault = 0, is_trigger = 0;
  7945. dtmprintk(ioc, ioc_info(ioc, "%s: enter\n", __func__));
  7946. if (ioc->pci_error_recovery) {
  7947. ioc_err(ioc, "%s: pci error recovery reset\n", __func__);
  7948. r = 0;
  7949. goto out_unlocked;
  7950. }
  7951. if (mpt3sas_fwfault_debug)
  7952. mpt3sas_halt_firmware(ioc);
  7953. /* wait for an active reset in progress to complete */
  7954. mutex_lock(&ioc->reset_in_progress_mutex);
  7955. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  7956. ioc->shost_recovery = 1;
  7957. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  7958. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  7959. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  7960. (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  7961. MPT3_DIAG_BUFFER_IS_RELEASED))) {
  7962. is_trigger = 1;
  7963. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  7964. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT ||
  7965. (ioc_state & MPI2_IOC_STATE_MASK) ==
  7966. MPI2_IOC_STATE_COREDUMP) {
  7967. is_fault = 1;
  7968. ioc->htb_rel.trigger_info_dwords[1] =
  7969. (ioc_state & MPI2_DOORBELL_DATA_MASK);
  7970. }
  7971. }
  7972. _base_pre_reset_handler(ioc);
  7973. mpt3sas_wait_for_commands_to_complete(ioc);
  7974. mpt3sas_base_mask_interrupts(ioc);
  7975. mpt3sas_base_pause_mq_polling(ioc);
  7976. r = mpt3sas_base_make_ioc_ready(ioc, type);
  7977. if (r)
  7978. goto out;
  7979. _base_clear_outstanding_commands(ioc);
  7980. /* If this hard reset is called while port enable is active, then
  7981. * there is no reason to call make_ioc_operational
  7982. */
  7983. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  7984. ioc->remove_host = 1;
  7985. r = -EFAULT;
  7986. goto out;
  7987. }
  7988. r = _base_get_ioc_facts(ioc);
  7989. if (r)
  7990. goto out;
  7991. r = _base_check_ioc_facts_changes(ioc);
  7992. if (r) {
  7993. ioc_info(ioc,
  7994. "Some of the parameters got changed in this new firmware"
  7995. " image and it requires system reboot\n");
  7996. goto out;
  7997. }
  7998. if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
  7999. panic("%s: Issue occurred with flashing controller firmware."
  8000. "Please reboot the system and ensure that the correct"
  8001. " firmware version is running\n", ioc->name);
  8002. r = _base_make_ioc_operational(ioc);
  8003. if (!r)
  8004. _base_reset_done_handler(ioc);
  8005. out:
  8006. ioc_info(ioc, "%s: %s\n", __func__, r == 0 ? "SUCCESS" : "FAILED");
  8007. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  8008. ioc->shost_recovery = 0;
  8009. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  8010. ioc->ioc_reset_count++;
  8011. mutex_unlock(&ioc->reset_in_progress_mutex);
  8012. mpt3sas_base_resume_mq_polling(ioc);
  8013. out_unlocked:
  8014. if ((r == 0) && is_trigger) {
  8015. if (is_fault)
  8016. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
  8017. else
  8018. mpt3sas_trigger_master(ioc,
  8019. MASTER_TRIGGER_ADAPTER_RESET);
  8020. }
  8021. dtmprintk(ioc, ioc_info(ioc, "%s: exit\n", __func__));
  8022. return r;
  8023. }