mpt3sas_base.c 198 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980
  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: MPT-FusionLinux.pdl@avagotech.com)
  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, 0);
  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, 0);
  72. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  73. static int msix_disable = -1;
  74. module_param(msix_disable, int, 0);
  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, S_IRUGO);
  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, 0);
  81. MODULE_PARM_DESC(max_msix_vectors,
  82. " max msix vectors");
  83. static int mpt3sas_fwfault_debug;
  84. MODULE_PARM_DESC(mpt3sas_fwfault_debug,
  85. " enable detection of firmware fault and halt firmware - (default=0)");
  86. static int
  87. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
  88. /**
  89. * mpt3sas_base_check_cmd_timeout - Function
  90. * to check timeout and command termination due
  91. * to Host reset.
  92. *
  93. * @ioc: per adapter object.
  94. * @status: Status of issued command.
  95. * @mpi_request:mf request pointer.
  96. * @sz: size of buffer.
  97. *
  98. * @Returns - 1/0 Reset to be done or Not
  99. */
  100. u8
  101. mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER *ioc,
  102. u8 status, void *mpi_request, int sz)
  103. {
  104. u8 issue_reset = 0;
  105. if (!(status & MPT3_CMD_RESET))
  106. issue_reset = 1;
  107. pr_err(MPT3SAS_FMT "Command %s\n", ioc->name,
  108. ((issue_reset == 0) ? "terminated due to Host Reset" : "Timeout"));
  109. _debug_dump_mf(mpi_request, sz);
  110. return issue_reset;
  111. }
  112. /**
  113. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  114. * @val: ?
  115. * @kp: ?
  116. *
  117. * Return: ?
  118. */
  119. static int
  120. _scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
  121. {
  122. int ret = param_set_int(val, kp);
  123. struct MPT3SAS_ADAPTER *ioc;
  124. if (ret)
  125. return ret;
  126. /* global ioc spinlock to protect controller list on list operations */
  127. pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
  128. spin_lock(&gioc_lock);
  129. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  130. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  131. spin_unlock(&gioc_lock);
  132. return 0;
  133. }
  134. module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
  135. param_get_int, &mpt3sas_fwfault_debug, 0644);
  136. /**
  137. * _base_clone_reply_to_sys_mem - copies reply to reply free iomem
  138. * in BAR0 space.
  139. *
  140. * @ioc: per adapter object
  141. * @reply: reply message frame(lower 32bit addr)
  142. * @index: System request message index.
  143. */
  144. static void
  145. _base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER *ioc, u32 reply,
  146. u32 index)
  147. {
  148. /*
  149. * 256 is offset within sys register.
  150. * 256 offset MPI frame starts. Max MPI frame supported is 32.
  151. * 32 * 128 = 4K. From here, Clone of reply free for mcpu starts
  152. */
  153. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  154. void __iomem *reply_free_iomem = (void __iomem *)ioc->chip +
  155. MPI_FRAME_START_OFFSET +
  156. (cmd_credit * ioc->request_sz) + (index * sizeof(u32));
  157. writel(reply, reply_free_iomem);
  158. }
  159. /**
  160. * _base_clone_mpi_to_sys_mem - Writes/copies MPI frames
  161. * to system/BAR0 region.
  162. *
  163. * @dst_iomem: Pointer to the destination location in BAR0 space.
  164. * @src: Pointer to the Source data.
  165. * @size: Size of data to be copied.
  166. */
  167. static void
  168. _base_clone_mpi_to_sys_mem(void *dst_iomem, void *src, u32 size)
  169. {
  170. int i;
  171. u32 *src_virt_mem = (u32 *)src;
  172. for (i = 0; i < size/4; i++)
  173. writel((u32)src_virt_mem[i],
  174. (void __iomem *)dst_iomem + (i * 4));
  175. }
  176. /**
  177. * _base_clone_to_sys_mem - Writes/copies data to system/BAR0 region
  178. *
  179. * @dst_iomem: Pointer to the destination location in BAR0 space.
  180. * @src: Pointer to the Source data.
  181. * @size: Size of data to be copied.
  182. */
  183. static void
  184. _base_clone_to_sys_mem(void __iomem *dst_iomem, void *src, u32 size)
  185. {
  186. int i;
  187. u32 *src_virt_mem = (u32 *)(src);
  188. for (i = 0; i < size/4; i++)
  189. writel((u32)src_virt_mem[i],
  190. (void __iomem *)dst_iomem + (i * 4));
  191. }
  192. /**
  193. * _base_get_chain - Calculates and Returns virtual chain address
  194. * for the provided smid in BAR0 space.
  195. *
  196. * @ioc: per adapter object
  197. * @smid: system request message index
  198. * @sge_chain_count: Scatter gather chain count.
  199. *
  200. * Return: the chain address.
  201. */
  202. static inline void __iomem*
  203. _base_get_chain(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  204. u8 sge_chain_count)
  205. {
  206. void __iomem *base_chain, *chain_virt;
  207. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  208. base_chain = (void __iomem *)ioc->chip + MPI_FRAME_START_OFFSET +
  209. (cmd_credit * ioc->request_sz) +
  210. REPLY_FREE_POOL_SIZE;
  211. chain_virt = base_chain + (smid * ioc->facts.MaxChainDepth *
  212. ioc->request_sz) + (sge_chain_count * ioc->request_sz);
  213. return chain_virt;
  214. }
  215. /**
  216. * _base_get_chain_phys - Calculates and Returns physical address
  217. * in BAR0 for scatter gather chains, for
  218. * the provided smid.
  219. *
  220. * @ioc: per adapter object
  221. * @smid: system request message index
  222. * @sge_chain_count: Scatter gather chain count.
  223. *
  224. * Return: Physical chain address.
  225. */
  226. static inline phys_addr_t
  227. _base_get_chain_phys(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  228. u8 sge_chain_count)
  229. {
  230. phys_addr_t base_chain_phys, chain_phys;
  231. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  232. base_chain_phys = ioc->chip_phys + MPI_FRAME_START_OFFSET +
  233. (cmd_credit * ioc->request_sz) +
  234. REPLY_FREE_POOL_SIZE;
  235. chain_phys = base_chain_phys + (smid * ioc->facts.MaxChainDepth *
  236. ioc->request_sz) + (sge_chain_count * ioc->request_sz);
  237. return chain_phys;
  238. }
  239. /**
  240. * _base_get_buffer_bar0 - Calculates and Returns BAR0 mapped Host
  241. * buffer address for the provided smid.
  242. * (Each smid can have 64K starts from 17024)
  243. *
  244. * @ioc: per adapter object
  245. * @smid: system request message index
  246. *
  247. * Return: Pointer to buffer location in BAR0.
  248. */
  249. static void __iomem *
  250. _base_get_buffer_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  251. {
  252. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  253. // Added extra 1 to reach end of chain.
  254. void __iomem *chain_end = _base_get_chain(ioc,
  255. cmd_credit + 1,
  256. ioc->facts.MaxChainDepth);
  257. return chain_end + (smid * 64 * 1024);
  258. }
  259. /**
  260. * _base_get_buffer_phys_bar0 - Calculates and Returns BAR0 mapped
  261. * Host buffer Physical address for the provided smid.
  262. * (Each smid can have 64K starts from 17024)
  263. *
  264. * @ioc: per adapter object
  265. * @smid: system request message index
  266. *
  267. * Return: Pointer to buffer location in BAR0.
  268. */
  269. static phys_addr_t
  270. _base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  271. {
  272. u16 cmd_credit = ioc->facts.RequestCredit + 1;
  273. phys_addr_t chain_end_phys = _base_get_chain_phys(ioc,
  274. cmd_credit + 1,
  275. ioc->facts.MaxChainDepth);
  276. return chain_end_phys + (smid * 64 * 1024);
  277. }
  278. /**
  279. * _base_get_chain_buffer_dma_to_chain_buffer - Iterates chain
  280. * lookup list and Provides chain_buffer
  281. * address for the matching dma address.
  282. * (Each smid can have 64K starts from 17024)
  283. *
  284. * @ioc: per adapter object
  285. * @chain_buffer_dma: Chain buffer dma address.
  286. *
  287. * Return: Pointer to chain buffer. Or Null on Failure.
  288. */
  289. static void *
  290. _base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER *ioc,
  291. dma_addr_t chain_buffer_dma)
  292. {
  293. u16 index, j;
  294. struct chain_tracker *ct;
  295. for (index = 0; index < ioc->scsiio_depth; index++) {
  296. for (j = 0; j < ioc->chains_needed_per_io; j++) {
  297. ct = &ioc->chain_lookup[index].chains_per_smid[j];
  298. if (ct && ct->chain_buffer_dma == chain_buffer_dma)
  299. return ct->chain_buffer;
  300. }
  301. }
  302. pr_info(MPT3SAS_FMT
  303. "Provided chain_buffer_dma address is not in the lookup list\n",
  304. ioc->name);
  305. return NULL;
  306. }
  307. /**
  308. * _clone_sg_entries - MPI EP's scsiio and config requests
  309. * are handled here. Base function for
  310. * double buffering, before submitting
  311. * the requests.
  312. *
  313. * @ioc: per adapter object.
  314. * @mpi_request: mf request pointer.
  315. * @smid: system request message index.
  316. */
  317. static void _clone_sg_entries(struct MPT3SAS_ADAPTER *ioc,
  318. void *mpi_request, u16 smid)
  319. {
  320. Mpi2SGESimple32_t *sgel, *sgel_next;
  321. u32 sgl_flags, sge_chain_count = 0;
  322. bool is_write = 0;
  323. u16 i = 0;
  324. void __iomem *buffer_iomem;
  325. phys_addr_t buffer_iomem_phys;
  326. void __iomem *buff_ptr;
  327. phys_addr_t buff_ptr_phys;
  328. void __iomem *dst_chain_addr[MCPU_MAX_CHAINS_PER_IO];
  329. void *src_chain_addr[MCPU_MAX_CHAINS_PER_IO];
  330. phys_addr_t dst_addr_phys;
  331. MPI2RequestHeader_t *request_hdr;
  332. struct scsi_cmnd *scmd;
  333. struct scatterlist *sg_scmd = NULL;
  334. int is_scsiio_req = 0;
  335. request_hdr = (MPI2RequestHeader_t *) mpi_request;
  336. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
  337. Mpi25SCSIIORequest_t *scsiio_request =
  338. (Mpi25SCSIIORequest_t *)mpi_request;
  339. sgel = (Mpi2SGESimple32_t *) &scsiio_request->SGL;
  340. is_scsiio_req = 1;
  341. } else if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
  342. Mpi2ConfigRequest_t *config_req =
  343. (Mpi2ConfigRequest_t *)mpi_request;
  344. sgel = (Mpi2SGESimple32_t *) &config_req->PageBufferSGE;
  345. } else
  346. return;
  347. /* From smid we can get scsi_cmd, once we have sg_scmd,
  348. * we just need to get sg_virt and sg_next to get virual
  349. * address associated with sgel->Address.
  350. */
  351. if (is_scsiio_req) {
  352. /* Get scsi_cmd using smid */
  353. scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  354. if (scmd == NULL) {
  355. pr_err(MPT3SAS_FMT "scmd is NULL\n", ioc->name);
  356. return;
  357. }
  358. /* Get sg_scmd from scmd provided */
  359. sg_scmd = scsi_sglist(scmd);
  360. }
  361. /*
  362. * 0 - 255 System register
  363. * 256 - 4352 MPI Frame. (This is based on maxCredit 32)
  364. * 4352 - 4864 Reply_free pool (512 byte is reserved
  365. * considering maxCredit 32. Reply need extra
  366. * room, for mCPU case kept four times of
  367. * maxCredit).
  368. * 4864 - 17152 SGE chain element. (32cmd * 3 chain of
  369. * 128 byte size = 12288)
  370. * 17152 - x Host buffer mapped with smid.
  371. * (Each smid can have 64K Max IO.)
  372. * BAR0+Last 1K MSIX Addr and Data
  373. * Total size in use 2113664 bytes of 4MB BAR0
  374. */
  375. buffer_iomem = _base_get_buffer_bar0(ioc, smid);
  376. buffer_iomem_phys = _base_get_buffer_phys_bar0(ioc, smid);
  377. buff_ptr = buffer_iomem;
  378. buff_ptr_phys = buffer_iomem_phys;
  379. WARN_ON(buff_ptr_phys > U32_MAX);
  380. if (le32_to_cpu(sgel->FlagsLength) &
  381. (MPI2_SGE_FLAGS_HOST_TO_IOC << MPI2_SGE_FLAGS_SHIFT))
  382. is_write = 1;
  383. for (i = 0; i < MPT_MIN_PHYS_SEGMENTS + ioc->facts.MaxChainDepth; i++) {
  384. sgl_flags =
  385. (le32_to_cpu(sgel->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT);
  386. switch (sgl_flags & MPI2_SGE_FLAGS_ELEMENT_MASK) {
  387. case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
  388. /*
  389. * Helper function which on passing
  390. * chain_buffer_dma returns chain_buffer. Get
  391. * the virtual address for sgel->Address
  392. */
  393. sgel_next =
  394. _base_get_chain_buffer_dma_to_chain_buffer(ioc,
  395. le32_to_cpu(sgel->Address));
  396. if (sgel_next == NULL)
  397. return;
  398. /*
  399. * This is coping 128 byte chain
  400. * frame (not a host buffer)
  401. */
  402. dst_chain_addr[sge_chain_count] =
  403. _base_get_chain(ioc,
  404. smid, sge_chain_count);
  405. src_chain_addr[sge_chain_count] =
  406. (void *) sgel_next;
  407. dst_addr_phys = _base_get_chain_phys(ioc,
  408. smid, sge_chain_count);
  409. WARN_ON(dst_addr_phys > U32_MAX);
  410. sgel->Address =
  411. cpu_to_le32(lower_32_bits(dst_addr_phys));
  412. sgel = sgel_next;
  413. sge_chain_count++;
  414. break;
  415. case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
  416. if (is_write) {
  417. if (is_scsiio_req) {
  418. _base_clone_to_sys_mem(buff_ptr,
  419. sg_virt(sg_scmd),
  420. (le32_to_cpu(sgel->FlagsLength) &
  421. 0x00ffffff));
  422. /*
  423. * FIXME: this relies on a a zero
  424. * PCI mem_offset.
  425. */
  426. sgel->Address =
  427. cpu_to_le32((u32)buff_ptr_phys);
  428. } else {
  429. _base_clone_to_sys_mem(buff_ptr,
  430. ioc->config_vaddr,
  431. (le32_to_cpu(sgel->FlagsLength) &
  432. 0x00ffffff));
  433. sgel->Address =
  434. cpu_to_le32((u32)buff_ptr_phys);
  435. }
  436. }
  437. buff_ptr += (le32_to_cpu(sgel->FlagsLength) &
  438. 0x00ffffff);
  439. buff_ptr_phys += (le32_to_cpu(sgel->FlagsLength) &
  440. 0x00ffffff);
  441. if ((le32_to_cpu(sgel->FlagsLength) &
  442. (MPI2_SGE_FLAGS_END_OF_BUFFER
  443. << MPI2_SGE_FLAGS_SHIFT)))
  444. goto eob_clone_chain;
  445. else {
  446. /*
  447. * Every single element in MPT will have
  448. * associated sg_next. Better to sanity that
  449. * sg_next is not NULL, but it will be a bug
  450. * if it is null.
  451. */
  452. if (is_scsiio_req) {
  453. sg_scmd = sg_next(sg_scmd);
  454. if (sg_scmd)
  455. sgel++;
  456. else
  457. goto eob_clone_chain;
  458. }
  459. }
  460. break;
  461. }
  462. }
  463. eob_clone_chain:
  464. for (i = 0; i < sge_chain_count; i++) {
  465. if (is_scsiio_req)
  466. _base_clone_to_sys_mem(dst_chain_addr[i],
  467. src_chain_addr[i], ioc->request_sz);
  468. }
  469. }
  470. /**
  471. * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
  472. * @arg: input argument, used to derive ioc
  473. *
  474. * Return:
  475. * 0 if controller is removed from pci subsystem.
  476. * -1 for other case.
  477. */
  478. static int mpt3sas_remove_dead_ioc_func(void *arg)
  479. {
  480. struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
  481. struct pci_dev *pdev;
  482. if ((ioc == NULL))
  483. return -1;
  484. pdev = ioc->pdev;
  485. if ((pdev == NULL))
  486. return -1;
  487. pci_stop_and_remove_bus_device_locked(pdev);
  488. return 0;
  489. }
  490. /**
  491. * _base_fault_reset_work - workq handling ioc fault conditions
  492. * @work: input argument, used to derive ioc
  493. *
  494. * Context: sleep.
  495. */
  496. static void
  497. _base_fault_reset_work(struct work_struct *work)
  498. {
  499. struct MPT3SAS_ADAPTER *ioc =
  500. container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
  501. unsigned long flags;
  502. u32 doorbell;
  503. int rc;
  504. struct task_struct *p;
  505. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  506. if (ioc->shost_recovery || ioc->pci_error_recovery)
  507. goto rearm_timer;
  508. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  509. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  510. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  511. pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
  512. ioc->name);
  513. /* It may be possible that EEH recovery can resolve some of
  514. * pci bus failure issues rather removing the dead ioc function
  515. * by considering controller is in a non-operational state. So
  516. * here priority is given to the EEH recovery. If it doesn't
  517. * not resolve this issue, mpt3sas driver will consider this
  518. * controller to non-operational state and remove the dead ioc
  519. * function.
  520. */
  521. if (ioc->non_operational_loop++ < 5) {
  522. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
  523. flags);
  524. goto rearm_timer;
  525. }
  526. /*
  527. * Call _scsih_flush_pending_cmds callback so that we flush all
  528. * pending commands back to OS. This call is required to aovid
  529. * deadlock at block layer. Dead IOC will fail to do diag reset,
  530. * and this call is safe since dead ioc will never return any
  531. * command back from HW.
  532. */
  533. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  534. /*
  535. * Set remove_host flag early since kernel thread will
  536. * take some time to execute.
  537. */
  538. ioc->remove_host = 1;
  539. /*Remove the Dead Host */
  540. p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
  541. "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
  542. if (IS_ERR(p))
  543. pr_err(MPT3SAS_FMT
  544. "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
  545. ioc->name, __func__);
  546. else
  547. pr_err(MPT3SAS_FMT
  548. "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
  549. ioc->name, __func__);
  550. return; /* don't rearm timer */
  551. }
  552. ioc->non_operational_loop = 0;
  553. if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
  554. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  555. pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
  556. __func__, (rc == 0) ? "success" : "failed");
  557. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  558. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  559. mpt3sas_base_fault_info(ioc, doorbell &
  560. MPI2_DOORBELL_DATA_MASK);
  561. if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
  562. MPI2_IOC_STATE_OPERATIONAL)
  563. return; /* don't rearm timer */
  564. }
  565. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  566. rearm_timer:
  567. if (ioc->fault_reset_work_q)
  568. queue_delayed_work(ioc->fault_reset_work_q,
  569. &ioc->fault_reset_work,
  570. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  571. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  572. }
  573. /**
  574. * mpt3sas_base_start_watchdog - start the fault_reset_work_q
  575. * @ioc: per adapter object
  576. *
  577. * Context: sleep.
  578. */
  579. void
  580. mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
  581. {
  582. unsigned long flags;
  583. if (ioc->fault_reset_work_q)
  584. return;
  585. /* initialize fault polling */
  586. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  587. snprintf(ioc->fault_reset_work_q_name,
  588. sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
  589. ioc->driver_name, ioc->id);
  590. ioc->fault_reset_work_q =
  591. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  592. if (!ioc->fault_reset_work_q) {
  593. pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
  594. ioc->name, __func__, __LINE__);
  595. return;
  596. }
  597. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  598. if (ioc->fault_reset_work_q)
  599. queue_delayed_work(ioc->fault_reset_work_q,
  600. &ioc->fault_reset_work,
  601. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  602. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  603. }
  604. /**
  605. * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
  606. * @ioc: per adapter object
  607. *
  608. * Context: sleep.
  609. */
  610. void
  611. mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
  612. {
  613. unsigned long flags;
  614. struct workqueue_struct *wq;
  615. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  616. wq = ioc->fault_reset_work_q;
  617. ioc->fault_reset_work_q = NULL;
  618. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  619. if (wq) {
  620. if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
  621. flush_workqueue(wq);
  622. destroy_workqueue(wq);
  623. }
  624. }
  625. /**
  626. * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
  627. * @ioc: per adapter object
  628. * @fault_code: fault code
  629. */
  630. void
  631. mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
  632. {
  633. pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
  634. ioc->name, fault_code);
  635. }
  636. /**
  637. * mpt3sas_halt_firmware - halt's mpt controller firmware
  638. * @ioc: per adapter object
  639. *
  640. * For debugging timeout related issues. Writing 0xCOFFEE00
  641. * to the doorbell register will halt controller firmware. With
  642. * the purpose to stop both driver and firmware, the enduser can
  643. * obtain a ring buffer from controller UART.
  644. */
  645. void
  646. mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
  647. {
  648. u32 doorbell;
  649. if (!ioc->fwfault_debug)
  650. return;
  651. dump_stack();
  652. doorbell = readl(&ioc->chip->Doorbell);
  653. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  654. mpt3sas_base_fault_info(ioc , doorbell);
  655. else {
  656. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  657. pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
  658. ioc->name);
  659. }
  660. if (ioc->fwfault_debug == 2)
  661. for (;;)
  662. ;
  663. else
  664. panic("panic in %s\n", __func__);
  665. }
  666. /**
  667. * _base_sas_ioc_info - verbose translation of the ioc status
  668. * @ioc: per adapter object
  669. * @mpi_reply: reply mf payload returned from firmware
  670. * @request_hdr: request mf
  671. */
  672. static void
  673. _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  674. MPI2RequestHeader_t *request_hdr)
  675. {
  676. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  677. MPI2_IOCSTATUS_MASK;
  678. char *desc = NULL;
  679. u16 frame_sz;
  680. char *func_str = NULL;
  681. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  682. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  683. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  684. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  685. return;
  686. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  687. return;
  688. switch (ioc_status) {
  689. /****************************************************************************
  690. * Common IOCStatus values for all replies
  691. ****************************************************************************/
  692. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  693. desc = "invalid function";
  694. break;
  695. case MPI2_IOCSTATUS_BUSY:
  696. desc = "busy";
  697. break;
  698. case MPI2_IOCSTATUS_INVALID_SGL:
  699. desc = "invalid sgl";
  700. break;
  701. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  702. desc = "internal error";
  703. break;
  704. case MPI2_IOCSTATUS_INVALID_VPID:
  705. desc = "invalid vpid";
  706. break;
  707. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  708. desc = "insufficient resources";
  709. break;
  710. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  711. desc = "insufficient power";
  712. break;
  713. case MPI2_IOCSTATUS_INVALID_FIELD:
  714. desc = "invalid field";
  715. break;
  716. case MPI2_IOCSTATUS_INVALID_STATE:
  717. desc = "invalid state";
  718. break;
  719. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  720. desc = "op state not supported";
  721. break;
  722. /****************************************************************************
  723. * Config IOCStatus values
  724. ****************************************************************************/
  725. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  726. desc = "config invalid action";
  727. break;
  728. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  729. desc = "config invalid type";
  730. break;
  731. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  732. desc = "config invalid page";
  733. break;
  734. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  735. desc = "config invalid data";
  736. break;
  737. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  738. desc = "config no defaults";
  739. break;
  740. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  741. desc = "config cant commit";
  742. break;
  743. /****************************************************************************
  744. * SCSI IO Reply
  745. ****************************************************************************/
  746. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  747. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  748. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  749. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  750. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  751. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  752. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  753. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  754. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  755. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  756. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  757. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  758. break;
  759. /****************************************************************************
  760. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  761. ****************************************************************************/
  762. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  763. desc = "eedp guard error";
  764. break;
  765. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  766. desc = "eedp ref tag error";
  767. break;
  768. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  769. desc = "eedp app tag error";
  770. break;
  771. /****************************************************************************
  772. * SCSI Target values
  773. ****************************************************************************/
  774. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  775. desc = "target invalid io index";
  776. break;
  777. case MPI2_IOCSTATUS_TARGET_ABORTED:
  778. desc = "target aborted";
  779. break;
  780. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  781. desc = "target no conn retryable";
  782. break;
  783. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  784. desc = "target no connection";
  785. break;
  786. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  787. desc = "target xfer count mismatch";
  788. break;
  789. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  790. desc = "target data offset error";
  791. break;
  792. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  793. desc = "target too much write data";
  794. break;
  795. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  796. desc = "target iu too short";
  797. break;
  798. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  799. desc = "target ack nak timeout";
  800. break;
  801. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  802. desc = "target nak received";
  803. break;
  804. /****************************************************************************
  805. * Serial Attached SCSI values
  806. ****************************************************************************/
  807. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  808. desc = "smp request failed";
  809. break;
  810. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  811. desc = "smp data overrun";
  812. break;
  813. /****************************************************************************
  814. * Diagnostic Buffer Post / Diagnostic Release values
  815. ****************************************************************************/
  816. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  817. desc = "diagnostic released";
  818. break;
  819. default:
  820. break;
  821. }
  822. if (!desc)
  823. return;
  824. switch (request_hdr->Function) {
  825. case MPI2_FUNCTION_CONFIG:
  826. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  827. func_str = "config_page";
  828. break;
  829. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  830. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  831. func_str = "task_mgmt";
  832. break;
  833. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  834. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  835. func_str = "sas_iounit_ctl";
  836. break;
  837. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  838. frame_sz = sizeof(Mpi2SepRequest_t);
  839. func_str = "enclosure";
  840. break;
  841. case MPI2_FUNCTION_IOC_INIT:
  842. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  843. func_str = "ioc_init";
  844. break;
  845. case MPI2_FUNCTION_PORT_ENABLE:
  846. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  847. func_str = "port_enable";
  848. break;
  849. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  850. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  851. func_str = "smp_passthru";
  852. break;
  853. case MPI2_FUNCTION_NVME_ENCAPSULATED:
  854. frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
  855. ioc->sge_size;
  856. func_str = "nvme_encapsulated";
  857. break;
  858. default:
  859. frame_sz = 32;
  860. func_str = "unknown";
  861. break;
  862. }
  863. pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
  864. ioc->name, desc, ioc_status, request_hdr, func_str);
  865. _debug_dump_mf(request_hdr, frame_sz/4);
  866. }
  867. /**
  868. * _base_display_event_data - verbose translation of firmware asyn events
  869. * @ioc: per adapter object
  870. * @mpi_reply: reply mf payload returned from firmware
  871. */
  872. static void
  873. _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
  874. Mpi2EventNotificationReply_t *mpi_reply)
  875. {
  876. char *desc = NULL;
  877. u16 event;
  878. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  879. return;
  880. event = le16_to_cpu(mpi_reply->Event);
  881. switch (event) {
  882. case MPI2_EVENT_LOG_DATA:
  883. desc = "Log Data";
  884. break;
  885. case MPI2_EVENT_STATE_CHANGE:
  886. desc = "Status Change";
  887. break;
  888. case MPI2_EVENT_HARD_RESET_RECEIVED:
  889. desc = "Hard Reset Received";
  890. break;
  891. case MPI2_EVENT_EVENT_CHANGE:
  892. desc = "Event Change";
  893. break;
  894. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  895. desc = "Device Status Change";
  896. break;
  897. case MPI2_EVENT_IR_OPERATION_STATUS:
  898. if (!ioc->hide_ir_msg)
  899. desc = "IR Operation Status";
  900. break;
  901. case MPI2_EVENT_SAS_DISCOVERY:
  902. {
  903. Mpi2EventDataSasDiscovery_t *event_data =
  904. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  905. pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
  906. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  907. "start" : "stop");
  908. if (event_data->DiscoveryStatus)
  909. pr_cont(" discovery_status(0x%08x)",
  910. le32_to_cpu(event_data->DiscoveryStatus));
  911. pr_cont("\n");
  912. return;
  913. }
  914. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  915. desc = "SAS Broadcast Primitive";
  916. break;
  917. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  918. desc = "SAS Init Device Status Change";
  919. break;
  920. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  921. desc = "SAS Init Table Overflow";
  922. break;
  923. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  924. desc = "SAS Topology Change List";
  925. break;
  926. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  927. desc = "SAS Enclosure Device Status Change";
  928. break;
  929. case MPI2_EVENT_IR_VOLUME:
  930. if (!ioc->hide_ir_msg)
  931. desc = "IR Volume";
  932. break;
  933. case MPI2_EVENT_IR_PHYSICAL_DISK:
  934. if (!ioc->hide_ir_msg)
  935. desc = "IR Physical Disk";
  936. break;
  937. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  938. if (!ioc->hide_ir_msg)
  939. desc = "IR Configuration Change List";
  940. break;
  941. case MPI2_EVENT_LOG_ENTRY_ADDED:
  942. if (!ioc->hide_ir_msg)
  943. desc = "Log Entry Added";
  944. break;
  945. case MPI2_EVENT_TEMP_THRESHOLD:
  946. desc = "Temperature Threshold";
  947. break;
  948. case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
  949. desc = "Cable Event";
  950. break;
  951. case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
  952. desc = "SAS Device Discovery Error";
  953. break;
  954. case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
  955. desc = "PCIE Device Status Change";
  956. break;
  957. case MPI2_EVENT_PCIE_ENUMERATION:
  958. {
  959. Mpi26EventDataPCIeEnumeration_t *event_data =
  960. (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
  961. pr_info(MPT3SAS_FMT "PCIE Enumeration: (%s)", ioc->name,
  962. (event_data->ReasonCode ==
  963. MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
  964. "start" : "stop");
  965. if (event_data->EnumerationStatus)
  966. pr_info("enumeration_status(0x%08x)",
  967. le32_to_cpu(event_data->EnumerationStatus));
  968. pr_info("\n");
  969. return;
  970. }
  971. case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
  972. desc = "PCIE Topology Change List";
  973. break;
  974. }
  975. if (!desc)
  976. return;
  977. pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
  978. }
  979. /**
  980. * _base_sas_log_info - verbose translation of firmware log info
  981. * @ioc: per adapter object
  982. * @log_info: log info
  983. */
  984. static void
  985. _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
  986. {
  987. union loginfo_type {
  988. u32 loginfo;
  989. struct {
  990. u32 subcode:16;
  991. u32 code:8;
  992. u32 originator:4;
  993. u32 bus_type:4;
  994. } dw;
  995. };
  996. union loginfo_type sas_loginfo;
  997. char *originator_str = NULL;
  998. sas_loginfo.loginfo = log_info;
  999. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  1000. return;
  1001. /* each nexus loss loginfo */
  1002. if (log_info == 0x31170000)
  1003. return;
  1004. /* eat the loginfos associated with task aborts */
  1005. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  1006. 0x31140000 || log_info == 0x31130000))
  1007. return;
  1008. switch (sas_loginfo.dw.originator) {
  1009. case 0:
  1010. originator_str = "IOP";
  1011. break;
  1012. case 1:
  1013. originator_str = "PL";
  1014. break;
  1015. case 2:
  1016. if (!ioc->hide_ir_msg)
  1017. originator_str = "IR";
  1018. else
  1019. originator_str = "WarpDrive";
  1020. break;
  1021. }
  1022. pr_warn(MPT3SAS_FMT
  1023. "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
  1024. ioc->name, log_info,
  1025. originator_str, sas_loginfo.dw.code,
  1026. sas_loginfo.dw.subcode);
  1027. }
  1028. /**
  1029. * _base_display_reply_info -
  1030. * @ioc: per adapter object
  1031. * @smid: system request message index
  1032. * @msix_index: MSIX table index supplied by the OS
  1033. * @reply: reply message frame(lower 32bit addr)
  1034. */
  1035. static void
  1036. _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  1037. u32 reply)
  1038. {
  1039. MPI2DefaultReply_t *mpi_reply;
  1040. u16 ioc_status;
  1041. u32 loginfo = 0;
  1042. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1043. if (unlikely(!mpi_reply)) {
  1044. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  1045. ioc->name, __FILE__, __LINE__, __func__);
  1046. return;
  1047. }
  1048. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  1049. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  1050. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  1051. _base_sas_ioc_info(ioc , mpi_reply,
  1052. mpt3sas_base_get_msg_frame(ioc, smid));
  1053. }
  1054. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  1055. loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
  1056. _base_sas_log_info(ioc, loginfo);
  1057. }
  1058. if (ioc_status || loginfo) {
  1059. ioc_status &= MPI2_IOCSTATUS_MASK;
  1060. mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
  1061. }
  1062. }
  1063. /**
  1064. * mpt3sas_base_done - base internal command completion routine
  1065. * @ioc: per adapter object
  1066. * @smid: system request message index
  1067. * @msix_index: MSIX table index supplied by the OS
  1068. * @reply: reply message frame(lower 32bit addr)
  1069. *
  1070. * Return:
  1071. * 1 meaning mf should be freed from _base_interrupt
  1072. * 0 means the mf is freed from this function.
  1073. */
  1074. u8
  1075. mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  1076. u32 reply)
  1077. {
  1078. MPI2DefaultReply_t *mpi_reply;
  1079. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1080. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  1081. return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
  1082. if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
  1083. return 1;
  1084. ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
  1085. if (mpi_reply) {
  1086. ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
  1087. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1088. }
  1089. ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
  1090. complete(&ioc->base_cmds.done);
  1091. return 1;
  1092. }
  1093. /**
  1094. * _base_async_event - main callback handler for firmware asyn events
  1095. * @ioc: per adapter object
  1096. * @msix_index: MSIX table index supplied by the OS
  1097. * @reply: reply message frame(lower 32bit addr)
  1098. *
  1099. * Return:
  1100. * 1 meaning mf should be freed from _base_interrupt
  1101. * 0 means the mf is freed from this function.
  1102. */
  1103. static u8
  1104. _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  1105. {
  1106. Mpi2EventNotificationReply_t *mpi_reply;
  1107. Mpi2EventAckRequest_t *ack_request;
  1108. u16 smid;
  1109. struct _event_ack_list *delayed_event_ack;
  1110. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1111. if (!mpi_reply)
  1112. return 1;
  1113. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  1114. return 1;
  1115. _base_display_event_data(ioc, mpi_reply);
  1116. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  1117. goto out;
  1118. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  1119. if (!smid) {
  1120. delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
  1121. GFP_ATOMIC);
  1122. if (!delayed_event_ack)
  1123. goto out;
  1124. INIT_LIST_HEAD(&delayed_event_ack->list);
  1125. delayed_event_ack->Event = mpi_reply->Event;
  1126. delayed_event_ack->EventContext = mpi_reply->EventContext;
  1127. list_add_tail(&delayed_event_ack->list,
  1128. &ioc->delayed_event_ack_list);
  1129. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1130. "DELAYED: EVENT ACK: event (0x%04x)\n",
  1131. ioc->name, le16_to_cpu(mpi_reply->Event)));
  1132. goto out;
  1133. }
  1134. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1135. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  1136. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  1137. ack_request->Event = mpi_reply->Event;
  1138. ack_request->EventContext = mpi_reply->EventContext;
  1139. ack_request->VF_ID = 0; /* TODO */
  1140. ack_request->VP_ID = 0;
  1141. mpt3sas_base_put_smid_default(ioc, smid);
  1142. out:
  1143. /* scsih callback handler */
  1144. mpt3sas_scsih_event_callback(ioc, msix_index, reply);
  1145. /* ctl callback handler */
  1146. mpt3sas_ctl_event_callback(ioc, msix_index, reply);
  1147. return 1;
  1148. }
  1149. static struct scsiio_tracker *
  1150. _get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1151. {
  1152. struct scsi_cmnd *cmd;
  1153. if (WARN_ON(!smid) ||
  1154. WARN_ON(smid >= ioc->hi_priority_smid))
  1155. return NULL;
  1156. cmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  1157. if (cmd)
  1158. return scsi_cmd_priv(cmd);
  1159. return NULL;
  1160. }
  1161. /**
  1162. * _base_get_cb_idx - obtain the callback index
  1163. * @ioc: per adapter object
  1164. * @smid: system request message index
  1165. *
  1166. * Return: callback index.
  1167. */
  1168. static u8
  1169. _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1170. {
  1171. int i;
  1172. u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
  1173. u8 cb_idx = 0xFF;
  1174. if (smid < ioc->hi_priority_smid) {
  1175. struct scsiio_tracker *st;
  1176. if (smid < ctl_smid) {
  1177. st = _get_st_from_smid(ioc, smid);
  1178. if (st)
  1179. cb_idx = st->cb_idx;
  1180. } else if (smid == ctl_smid)
  1181. cb_idx = ioc->ctl_cb_idx;
  1182. } else if (smid < ioc->internal_smid) {
  1183. i = smid - ioc->hi_priority_smid;
  1184. cb_idx = ioc->hpr_lookup[i].cb_idx;
  1185. } else if (smid <= ioc->hba_queue_depth) {
  1186. i = smid - ioc->internal_smid;
  1187. cb_idx = ioc->internal_lookup[i].cb_idx;
  1188. }
  1189. return cb_idx;
  1190. }
  1191. /**
  1192. * _base_mask_interrupts - disable interrupts
  1193. * @ioc: per adapter object
  1194. *
  1195. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  1196. */
  1197. static void
  1198. _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  1199. {
  1200. u32 him_register;
  1201. ioc->mask_interrupts = 1;
  1202. him_register = readl(&ioc->chip->HostInterruptMask);
  1203. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  1204. writel(him_register, &ioc->chip->HostInterruptMask);
  1205. readl(&ioc->chip->HostInterruptMask);
  1206. }
  1207. /**
  1208. * _base_unmask_interrupts - enable interrupts
  1209. * @ioc: per adapter object
  1210. *
  1211. * Enabling only Reply Interrupts
  1212. */
  1213. static void
  1214. _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  1215. {
  1216. u32 him_register;
  1217. him_register = readl(&ioc->chip->HostInterruptMask);
  1218. him_register &= ~MPI2_HIM_RIM;
  1219. writel(him_register, &ioc->chip->HostInterruptMask);
  1220. ioc->mask_interrupts = 0;
  1221. }
  1222. union reply_descriptor {
  1223. u64 word;
  1224. struct {
  1225. u32 low;
  1226. u32 high;
  1227. } u;
  1228. };
  1229. /**
  1230. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  1231. * @irq: irq number (not used)
  1232. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  1233. *
  1234. * Return: IRQ_HANDLED if processed, else IRQ_NONE.
  1235. */
  1236. static irqreturn_t
  1237. _base_interrupt(int irq, void *bus_id)
  1238. {
  1239. struct adapter_reply_queue *reply_q = bus_id;
  1240. union reply_descriptor rd;
  1241. u32 completed_cmds;
  1242. u8 request_desript_type;
  1243. u16 smid;
  1244. u8 cb_idx;
  1245. u32 reply;
  1246. u8 msix_index = reply_q->msix_index;
  1247. struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
  1248. Mpi2ReplyDescriptorsUnion_t *rpf;
  1249. u8 rc;
  1250. if (ioc->mask_interrupts)
  1251. return IRQ_NONE;
  1252. if (!atomic_add_unless(&reply_q->busy, 1, 1))
  1253. return IRQ_NONE;
  1254. rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
  1255. request_desript_type = rpf->Default.ReplyFlags
  1256. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1257. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  1258. atomic_dec(&reply_q->busy);
  1259. return IRQ_NONE;
  1260. }
  1261. completed_cmds = 0;
  1262. cb_idx = 0xFF;
  1263. do {
  1264. rd.word = le64_to_cpu(rpf->Words);
  1265. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  1266. goto out;
  1267. reply = 0;
  1268. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  1269. if (request_desript_type ==
  1270. MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
  1271. request_desript_type ==
  1272. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
  1273. request_desript_type ==
  1274. MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
  1275. cb_idx = _base_get_cb_idx(ioc, smid);
  1276. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  1277. (likely(mpt_callbacks[cb_idx] != NULL))) {
  1278. rc = mpt_callbacks[cb_idx](ioc, smid,
  1279. msix_index, 0);
  1280. if (rc)
  1281. mpt3sas_base_free_smid(ioc, smid);
  1282. }
  1283. } else if (request_desript_type ==
  1284. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  1285. reply = le32_to_cpu(
  1286. rpf->AddressReply.ReplyFrameAddress);
  1287. if (reply > ioc->reply_dma_max_address ||
  1288. reply < ioc->reply_dma_min_address)
  1289. reply = 0;
  1290. if (smid) {
  1291. cb_idx = _base_get_cb_idx(ioc, smid);
  1292. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  1293. (likely(mpt_callbacks[cb_idx] != NULL))) {
  1294. rc = mpt_callbacks[cb_idx](ioc, smid,
  1295. msix_index, reply);
  1296. if (reply)
  1297. _base_display_reply_info(ioc,
  1298. smid, msix_index, reply);
  1299. if (rc)
  1300. mpt3sas_base_free_smid(ioc,
  1301. smid);
  1302. }
  1303. } else {
  1304. _base_async_event(ioc, msix_index, reply);
  1305. }
  1306. /* reply free queue handling */
  1307. if (reply) {
  1308. ioc->reply_free_host_index =
  1309. (ioc->reply_free_host_index ==
  1310. (ioc->reply_free_queue_depth - 1)) ?
  1311. 0 : ioc->reply_free_host_index + 1;
  1312. ioc->reply_free[ioc->reply_free_host_index] =
  1313. cpu_to_le32(reply);
  1314. if (ioc->is_mcpu_endpoint)
  1315. _base_clone_reply_to_sys_mem(ioc,
  1316. reply,
  1317. ioc->reply_free_host_index);
  1318. writel(ioc->reply_free_host_index,
  1319. &ioc->chip->ReplyFreeHostIndex);
  1320. }
  1321. }
  1322. rpf->Words = cpu_to_le64(ULLONG_MAX);
  1323. reply_q->reply_post_host_index =
  1324. (reply_q->reply_post_host_index ==
  1325. (ioc->reply_post_queue_depth - 1)) ? 0 :
  1326. reply_q->reply_post_host_index + 1;
  1327. request_desript_type =
  1328. reply_q->reply_post_free[reply_q->reply_post_host_index].
  1329. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1330. completed_cmds++;
  1331. /* Update the reply post host index after continuously
  1332. * processing the threshold number of Reply Descriptors.
  1333. * So that FW can find enough entries to post the Reply
  1334. * Descriptors in the reply descriptor post queue.
  1335. */
  1336. if (completed_cmds > ioc->hba_queue_depth/3) {
  1337. if (ioc->combined_reply_queue) {
  1338. writel(reply_q->reply_post_host_index |
  1339. ((msix_index & 7) <<
  1340. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1341. ioc->replyPostRegisterIndex[msix_index/8]);
  1342. } else {
  1343. writel(reply_q->reply_post_host_index |
  1344. (msix_index <<
  1345. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1346. &ioc->chip->ReplyPostHostIndex);
  1347. }
  1348. completed_cmds = 1;
  1349. }
  1350. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1351. goto out;
  1352. if (!reply_q->reply_post_host_index)
  1353. rpf = reply_q->reply_post_free;
  1354. else
  1355. rpf++;
  1356. } while (1);
  1357. out:
  1358. if (!completed_cmds) {
  1359. atomic_dec(&reply_q->busy);
  1360. return IRQ_NONE;
  1361. }
  1362. if (ioc->is_warpdrive) {
  1363. writel(reply_q->reply_post_host_index,
  1364. ioc->reply_post_host_index[msix_index]);
  1365. atomic_dec(&reply_q->busy);
  1366. return IRQ_HANDLED;
  1367. }
  1368. /* Update Reply Post Host Index.
  1369. * For those HBA's which support combined reply queue feature
  1370. * 1. Get the correct Supplemental Reply Post Host Index Register.
  1371. * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
  1372. * Index Register address bank i.e replyPostRegisterIndex[],
  1373. * 2. Then update this register with new reply host index value
  1374. * in ReplyPostIndex field and the MSIxIndex field with
  1375. * msix_index value reduced to a value between 0 and 7,
  1376. * using a modulo 8 operation. Since each Supplemental Reply Post
  1377. * Host Index Register supports 8 MSI-X vectors.
  1378. *
  1379. * For other HBA's just update the Reply Post Host Index register with
  1380. * new reply host index value in ReplyPostIndex Field and msix_index
  1381. * value in MSIxIndex field.
  1382. */
  1383. if (ioc->combined_reply_queue)
  1384. writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
  1385. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1386. ioc->replyPostRegisterIndex[msix_index/8]);
  1387. else
  1388. writel(reply_q->reply_post_host_index | (msix_index <<
  1389. MPI2_RPHI_MSIX_INDEX_SHIFT),
  1390. &ioc->chip->ReplyPostHostIndex);
  1391. atomic_dec(&reply_q->busy);
  1392. return IRQ_HANDLED;
  1393. }
  1394. /**
  1395. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  1396. * @ioc: per adapter object
  1397. *
  1398. * Return: Whether or not MSI/X is enabled.
  1399. */
  1400. static inline int
  1401. _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
  1402. {
  1403. return (ioc->facts.IOCCapabilities &
  1404. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  1405. }
  1406. /**
  1407. * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
  1408. * @ioc: per adapter object
  1409. * Context: non ISR conext
  1410. *
  1411. * Called when a Task Management request has completed.
  1412. */
  1413. void
  1414. mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
  1415. {
  1416. struct adapter_reply_queue *reply_q;
  1417. /* If MSIX capability is turned off
  1418. * then multi-queues are not enabled
  1419. */
  1420. if (!_base_is_controller_msix_enabled(ioc))
  1421. return;
  1422. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  1423. if (ioc->shost_recovery || ioc->remove_host ||
  1424. ioc->pci_error_recovery)
  1425. return;
  1426. /* TMs are on msix_index == 0 */
  1427. if (reply_q->msix_index == 0)
  1428. continue;
  1429. synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
  1430. }
  1431. }
  1432. /**
  1433. * mpt3sas_base_release_callback_handler - clear interrupt callback handler
  1434. * @cb_idx: callback index
  1435. */
  1436. void
  1437. mpt3sas_base_release_callback_handler(u8 cb_idx)
  1438. {
  1439. mpt_callbacks[cb_idx] = NULL;
  1440. }
  1441. /**
  1442. * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
  1443. * @cb_func: callback function
  1444. *
  1445. * Return: Index of @cb_func.
  1446. */
  1447. u8
  1448. mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  1449. {
  1450. u8 cb_idx;
  1451. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  1452. if (mpt_callbacks[cb_idx] == NULL)
  1453. break;
  1454. mpt_callbacks[cb_idx] = cb_func;
  1455. return cb_idx;
  1456. }
  1457. /**
  1458. * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
  1459. */
  1460. void
  1461. mpt3sas_base_initialize_callback_handler(void)
  1462. {
  1463. u8 cb_idx;
  1464. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  1465. mpt3sas_base_release_callback_handler(cb_idx);
  1466. }
  1467. /**
  1468. * _base_build_zero_len_sge - build zero length sg entry
  1469. * @ioc: per adapter object
  1470. * @paddr: virtual address for SGE
  1471. *
  1472. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1473. * something to use if the target device goes brain dead and tries
  1474. * to send data even when none is asked for.
  1475. */
  1476. static void
  1477. _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1478. {
  1479. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  1480. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  1481. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  1482. MPI2_SGE_FLAGS_SHIFT);
  1483. ioc->base_add_sg_single(paddr, flags_length, -1);
  1484. }
  1485. /**
  1486. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1487. * @paddr: virtual address for SGE
  1488. * @flags_length: SGE flags and data transfer length
  1489. * @dma_addr: Physical address
  1490. */
  1491. static void
  1492. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1493. {
  1494. Mpi2SGESimple32_t *sgel = paddr;
  1495. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1496. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1497. sgel->FlagsLength = cpu_to_le32(flags_length);
  1498. sgel->Address = cpu_to_le32(dma_addr);
  1499. }
  1500. /**
  1501. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1502. * @paddr: virtual address for SGE
  1503. * @flags_length: SGE flags and data transfer length
  1504. * @dma_addr: Physical address
  1505. */
  1506. static void
  1507. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1508. {
  1509. Mpi2SGESimple64_t *sgel = paddr;
  1510. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1511. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1512. sgel->FlagsLength = cpu_to_le32(flags_length);
  1513. sgel->Address = cpu_to_le64(dma_addr);
  1514. }
  1515. /**
  1516. * _base_get_chain_buffer_tracker - obtain chain tracker
  1517. * @ioc: per adapter object
  1518. * @scmd: SCSI commands of the IO request
  1519. *
  1520. * Return: chain tracker from chain_lookup table using key as
  1521. * smid and smid's chain_offset.
  1522. */
  1523. static struct chain_tracker *
  1524. _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc,
  1525. struct scsi_cmnd *scmd)
  1526. {
  1527. struct chain_tracker *chain_req;
  1528. struct scsiio_tracker *st = scsi_cmd_priv(scmd);
  1529. u16 smid = st->smid;
  1530. u8 chain_offset =
  1531. atomic_read(&ioc->chain_lookup[smid - 1].chain_offset);
  1532. if (chain_offset == ioc->chains_needed_per_io)
  1533. return NULL;
  1534. chain_req = &ioc->chain_lookup[smid - 1].chains_per_smid[chain_offset];
  1535. atomic_inc(&ioc->chain_lookup[smid - 1].chain_offset);
  1536. return chain_req;
  1537. }
  1538. /**
  1539. * _base_build_sg - build generic sg
  1540. * @ioc: per adapter object
  1541. * @psge: virtual address for SGE
  1542. * @data_out_dma: physical address for WRITES
  1543. * @data_out_sz: data xfer size for WRITES
  1544. * @data_in_dma: physical address for READS
  1545. * @data_in_sz: data xfer size for READS
  1546. */
  1547. static void
  1548. _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1549. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1550. size_t data_in_sz)
  1551. {
  1552. u32 sgl_flags;
  1553. if (!data_out_sz && !data_in_sz) {
  1554. _base_build_zero_len_sge(ioc, psge);
  1555. return;
  1556. }
  1557. if (data_out_sz && data_in_sz) {
  1558. /* WRITE sgel first */
  1559. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1560. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1561. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1562. ioc->base_add_sg_single(psge, sgl_flags |
  1563. data_out_sz, data_out_dma);
  1564. /* incr sgel */
  1565. psge += ioc->sge_size;
  1566. /* READ sgel last */
  1567. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1568. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1569. MPI2_SGE_FLAGS_END_OF_LIST);
  1570. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1571. ioc->base_add_sg_single(psge, sgl_flags |
  1572. data_in_sz, data_in_dma);
  1573. } else if (data_out_sz) /* WRITE */ {
  1574. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1575. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1576. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1577. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1578. ioc->base_add_sg_single(psge, sgl_flags |
  1579. data_out_sz, data_out_dma);
  1580. } else if (data_in_sz) /* READ */ {
  1581. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1582. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1583. MPI2_SGE_FLAGS_END_OF_LIST);
  1584. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1585. ioc->base_add_sg_single(psge, sgl_flags |
  1586. data_in_sz, data_in_dma);
  1587. }
  1588. }
  1589. /* IEEE format sgls */
  1590. /**
  1591. * _base_build_nvme_prp - This function is called for NVMe end devices to build
  1592. * a native SGL (NVMe PRP). The native SGL is built starting in the first PRP
  1593. * entry of the NVMe message (PRP1). If the data buffer is small enough to be
  1594. * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
  1595. * used to describe a larger data buffer. If the data buffer is too large to
  1596. * describe using the two PRP entriess inside the NVMe message, then PRP1
  1597. * describes the first data memory segment, and PRP2 contains a pointer to a PRP
  1598. * list located elsewhere in memory to describe the remaining data memory
  1599. * segments. The PRP list will be contiguous.
  1600. *
  1601. * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
  1602. * consists of a list of PRP entries to describe a number of noncontigous
  1603. * physical memory segments as a single memory buffer, just as a SGL does. Note
  1604. * however, that this function is only used by the IOCTL call, so the memory
  1605. * given will be guaranteed to be contiguous. There is no need to translate
  1606. * non-contiguous SGL into a PRP in this case. All PRPs will describe
  1607. * contiguous space that is one page size each.
  1608. *
  1609. * Each NVMe message contains two PRP entries. The first (PRP1) either contains
  1610. * a PRP list pointer or a PRP element, depending upon the command. PRP2
  1611. * contains the second PRP element if the memory being described fits within 2
  1612. * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
  1613. *
  1614. * A PRP list pointer contains the address of a PRP list, structured as a linear
  1615. * array of PRP entries. Each PRP entry in this list describes a segment of
  1616. * physical memory.
  1617. *
  1618. * Each 64-bit PRP entry comprises an address and an offset field. The address
  1619. * always points at the beginning of a 4KB physical memory page, and the offset
  1620. * describes where within that 4KB page the memory segment begins. Only the
  1621. * first element in a PRP list may contain a non-zero offest, implying that all
  1622. * memory segments following the first begin at the start of a 4KB page.
  1623. *
  1624. * Each PRP element normally describes 4KB of physical memory, with exceptions
  1625. * for the first and last elements in the list. If the memory being described
  1626. * by the list begins at a non-zero offset within the first 4KB page, then the
  1627. * first PRP element will contain a non-zero offset indicating where the region
  1628. * begins within the 4KB page. The last memory segment may end before the end
  1629. * of the 4KB segment, depending upon the overall size of the memory being
  1630. * described by the PRP list.
  1631. *
  1632. * Since PRP entries lack any indication of size, the overall data buffer length
  1633. * is used to determine where the end of the data memory buffer is located, and
  1634. * how many PRP entries are required to describe it.
  1635. *
  1636. * @ioc: per adapter object
  1637. * @smid: system request message index for getting asscociated SGL
  1638. * @nvme_encap_request: the NVMe request msg frame pointer
  1639. * @data_out_dma: physical address for WRITES
  1640. * @data_out_sz: data xfer size for WRITES
  1641. * @data_in_dma: physical address for READS
  1642. * @data_in_sz: data xfer size for READS
  1643. */
  1644. static void
  1645. _base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  1646. Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
  1647. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1648. size_t data_in_sz)
  1649. {
  1650. int prp_size = NVME_PRP_SIZE;
  1651. __le64 *prp_entry, *prp1_entry, *prp2_entry;
  1652. __le64 *prp_page;
  1653. dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
  1654. u32 offset, entry_len;
  1655. u32 page_mask_result, page_mask;
  1656. size_t length;
  1657. struct mpt3sas_nvme_cmd *nvme_cmd =
  1658. (void *)nvme_encap_request->NVMe_Command;
  1659. /*
  1660. * Not all commands require a data transfer. If no data, just return
  1661. * without constructing any PRP.
  1662. */
  1663. if (!data_in_sz && !data_out_sz)
  1664. return;
  1665. prp1_entry = &nvme_cmd->prp1;
  1666. prp2_entry = &nvme_cmd->prp2;
  1667. prp_entry = prp1_entry;
  1668. /*
  1669. * For the PRP entries, use the specially allocated buffer of
  1670. * contiguous memory.
  1671. */
  1672. prp_page = (__le64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
  1673. prp_page_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
  1674. /*
  1675. * Check if we are within 1 entry of a page boundary we don't
  1676. * want our first entry to be a PRP List entry.
  1677. */
  1678. page_mask = ioc->page_size - 1;
  1679. page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
  1680. if (!page_mask_result) {
  1681. /* Bump up to next page boundary. */
  1682. prp_page = (__le64 *)((u8 *)prp_page + prp_size);
  1683. prp_page_dma = prp_page_dma + prp_size;
  1684. }
  1685. /*
  1686. * Set PRP physical pointer, which initially points to the current PRP
  1687. * DMA memory page.
  1688. */
  1689. prp_entry_dma = prp_page_dma;
  1690. /* Get physical address and length of the data buffer. */
  1691. if (data_in_sz) {
  1692. dma_addr = data_in_dma;
  1693. length = data_in_sz;
  1694. } else {
  1695. dma_addr = data_out_dma;
  1696. length = data_out_sz;
  1697. }
  1698. /* Loop while the length is not zero. */
  1699. while (length) {
  1700. /*
  1701. * Check if we need to put a list pointer here if we are at
  1702. * page boundary - prp_size (8 bytes).
  1703. */
  1704. page_mask_result = (prp_entry_dma + prp_size) & page_mask;
  1705. if (!page_mask_result) {
  1706. /*
  1707. * This is the last entry in a PRP List, so we need to
  1708. * put a PRP list pointer here. What this does is:
  1709. * - bump the current memory pointer to the next
  1710. * address, which will be the next full page.
  1711. * - set the PRP Entry to point to that page. This
  1712. * is now the PRP List pointer.
  1713. * - bump the PRP Entry pointer the start of the
  1714. * next page. Since all of this PRP memory is
  1715. * contiguous, no need to get a new page - it's
  1716. * just the next address.
  1717. */
  1718. prp_entry_dma++;
  1719. *prp_entry = cpu_to_le64(prp_entry_dma);
  1720. prp_entry++;
  1721. }
  1722. /* Need to handle if entry will be part of a page. */
  1723. offset = dma_addr & page_mask;
  1724. entry_len = ioc->page_size - offset;
  1725. if (prp_entry == prp1_entry) {
  1726. /*
  1727. * Must fill in the first PRP pointer (PRP1) before
  1728. * moving on.
  1729. */
  1730. *prp1_entry = cpu_to_le64(dma_addr);
  1731. /*
  1732. * Now point to the second PRP entry within the
  1733. * command (PRP2).
  1734. */
  1735. prp_entry = prp2_entry;
  1736. } else if (prp_entry == prp2_entry) {
  1737. /*
  1738. * Should the PRP2 entry be a PRP List pointer or just
  1739. * a regular PRP pointer? If there is more than one
  1740. * more page of data, must use a PRP List pointer.
  1741. */
  1742. if (length > ioc->page_size) {
  1743. /*
  1744. * PRP2 will contain a PRP List pointer because
  1745. * more PRP's are needed with this command. The
  1746. * list will start at the beginning of the
  1747. * contiguous buffer.
  1748. */
  1749. *prp2_entry = cpu_to_le64(prp_entry_dma);
  1750. /*
  1751. * The next PRP Entry will be the start of the
  1752. * first PRP List.
  1753. */
  1754. prp_entry = prp_page;
  1755. } else {
  1756. /*
  1757. * After this, the PRP Entries are complete.
  1758. * This command uses 2 PRP's and no PRP list.
  1759. */
  1760. *prp2_entry = cpu_to_le64(dma_addr);
  1761. }
  1762. } else {
  1763. /*
  1764. * Put entry in list and bump the addresses.
  1765. *
  1766. * After PRP1 and PRP2 are filled in, this will fill in
  1767. * all remaining PRP entries in a PRP List, one per
  1768. * each time through the loop.
  1769. */
  1770. *prp_entry = cpu_to_le64(dma_addr);
  1771. prp_entry++;
  1772. prp_entry_dma++;
  1773. }
  1774. /*
  1775. * Bump the phys address of the command's data buffer by the
  1776. * entry_len.
  1777. */
  1778. dma_addr += entry_len;
  1779. /* Decrement length accounting for last partial page. */
  1780. if (entry_len > length)
  1781. length = 0;
  1782. else
  1783. length -= entry_len;
  1784. }
  1785. }
  1786. /**
  1787. * base_make_prp_nvme -
  1788. * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
  1789. *
  1790. * @ioc: per adapter object
  1791. * @scmd: SCSI command from the mid-layer
  1792. * @mpi_request: mpi request
  1793. * @smid: msg Index
  1794. * @sge_count: scatter gather element count.
  1795. *
  1796. * Return: true: PRPs are built
  1797. * false: IEEE SGLs needs to be built
  1798. */
  1799. static void
  1800. base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
  1801. struct scsi_cmnd *scmd,
  1802. Mpi25SCSIIORequest_t *mpi_request,
  1803. u16 smid, int sge_count)
  1804. {
  1805. int sge_len, num_prp_in_chain = 0;
  1806. Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
  1807. __le64 *curr_buff;
  1808. dma_addr_t msg_dma, sge_addr, offset;
  1809. u32 page_mask, page_mask_result;
  1810. struct scatterlist *sg_scmd;
  1811. u32 first_prp_len;
  1812. int data_len = scsi_bufflen(scmd);
  1813. u32 nvme_pg_size;
  1814. nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
  1815. /*
  1816. * Nvme has a very convoluted prp format. One prp is required
  1817. * for each page or partial page. Driver need to split up OS sg_list
  1818. * entries if it is longer than one page or cross a page
  1819. * boundary. Driver also have to insert a PRP list pointer entry as
  1820. * the last entry in each physical page of the PRP list.
  1821. *
  1822. * NOTE: The first PRP "entry" is actually placed in the first
  1823. * SGL entry in the main message as IEEE 64 format. The 2nd
  1824. * entry in the main message is the chain element, and the rest
  1825. * of the PRP entries are built in the contiguous pcie buffer.
  1826. */
  1827. page_mask = nvme_pg_size - 1;
  1828. /*
  1829. * Native SGL is needed.
  1830. * Put a chain element in main message frame that points to the first
  1831. * chain buffer.
  1832. *
  1833. * NOTE: The ChainOffset field must be 0 when using a chain pointer to
  1834. * a native SGL.
  1835. */
  1836. /* Set main message chain element pointer */
  1837. main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
  1838. /*
  1839. * For NVMe the chain element needs to be the 2nd SG entry in the main
  1840. * message.
  1841. */
  1842. main_chain_element = (Mpi25IeeeSgeChain64_t *)
  1843. ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
  1844. /*
  1845. * For the PRP entries, use the specially allocated buffer of
  1846. * contiguous memory. Normal chain buffers can't be used
  1847. * because each chain buffer would need to be the size of an OS
  1848. * page (4k).
  1849. */
  1850. curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
  1851. msg_dma = mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
  1852. main_chain_element->Address = cpu_to_le64(msg_dma);
  1853. main_chain_element->NextChainOffset = 0;
  1854. main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1855. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  1856. MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
  1857. /* Build first prp, sge need not to be page aligned*/
  1858. ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
  1859. sg_scmd = scsi_sglist(scmd);
  1860. sge_addr = sg_dma_address(sg_scmd);
  1861. sge_len = sg_dma_len(sg_scmd);
  1862. offset = sge_addr & page_mask;
  1863. first_prp_len = nvme_pg_size - offset;
  1864. ptr_first_sgl->Address = cpu_to_le64(sge_addr);
  1865. ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
  1866. data_len -= first_prp_len;
  1867. if (sge_len > first_prp_len) {
  1868. sge_addr += first_prp_len;
  1869. sge_len -= first_prp_len;
  1870. } else if (data_len && (sge_len == first_prp_len)) {
  1871. sg_scmd = sg_next(sg_scmd);
  1872. sge_addr = sg_dma_address(sg_scmd);
  1873. sge_len = sg_dma_len(sg_scmd);
  1874. }
  1875. for (;;) {
  1876. offset = sge_addr & page_mask;
  1877. /* Put PRP pointer due to page boundary*/
  1878. page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
  1879. if (unlikely(!page_mask_result)) {
  1880. scmd_printk(KERN_NOTICE,
  1881. scmd, "page boundary curr_buff: 0x%p\n",
  1882. curr_buff);
  1883. msg_dma += 8;
  1884. *curr_buff = cpu_to_le64(msg_dma);
  1885. curr_buff++;
  1886. num_prp_in_chain++;
  1887. }
  1888. *curr_buff = cpu_to_le64(sge_addr);
  1889. curr_buff++;
  1890. msg_dma += 8;
  1891. num_prp_in_chain++;
  1892. sge_addr += nvme_pg_size;
  1893. sge_len -= nvme_pg_size;
  1894. data_len -= nvme_pg_size;
  1895. if (data_len <= 0)
  1896. break;
  1897. if (sge_len > 0)
  1898. continue;
  1899. sg_scmd = sg_next(sg_scmd);
  1900. sge_addr = sg_dma_address(sg_scmd);
  1901. sge_len = sg_dma_len(sg_scmd);
  1902. }
  1903. main_chain_element->Length =
  1904. cpu_to_le32(num_prp_in_chain * sizeof(u64));
  1905. return;
  1906. }
  1907. static bool
  1908. base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
  1909. struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
  1910. {
  1911. u32 data_length = 0;
  1912. bool build_prp = true;
  1913. data_length = scsi_bufflen(scmd);
  1914. /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
  1915. * we built IEEE SGL
  1916. */
  1917. if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
  1918. build_prp = false;
  1919. return build_prp;
  1920. }
  1921. /**
  1922. * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
  1923. * determine if the driver needs to build a native SGL. If so, that native
  1924. * SGL is built in the special contiguous buffers allocated especially for
  1925. * PCIe SGL creation. If the driver will not build a native SGL, return
  1926. * TRUE and a normal IEEE SGL will be built. Currently this routine
  1927. * supports NVMe.
  1928. * @ioc: per adapter object
  1929. * @mpi_request: mf request pointer
  1930. * @smid: system request message index
  1931. * @scmd: scsi command
  1932. * @pcie_device: points to the PCIe device's info
  1933. *
  1934. * Return: 0 if native SGL was built, 1 if no SGL was built
  1935. */
  1936. static int
  1937. _base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
  1938. Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
  1939. struct _pcie_device *pcie_device)
  1940. {
  1941. int sges_left;
  1942. /* Get the SG list pointer and info. */
  1943. sges_left = scsi_dma_map(scmd);
  1944. if (sges_left < 0) {
  1945. sdev_printk(KERN_ERR, scmd->device,
  1946. "scsi_dma_map failed: request for %d bytes!\n",
  1947. scsi_bufflen(scmd));
  1948. return 1;
  1949. }
  1950. /* Check if we need to build a native SG list. */
  1951. if (base_is_prp_possible(ioc, pcie_device,
  1952. scmd, sges_left) == 0) {
  1953. /* We built a native SG list, just return. */
  1954. goto out;
  1955. }
  1956. /*
  1957. * Build native NVMe PRP.
  1958. */
  1959. base_make_prp_nvme(ioc, scmd, mpi_request,
  1960. smid, sges_left);
  1961. return 0;
  1962. out:
  1963. scsi_dma_unmap(scmd);
  1964. return 1;
  1965. }
  1966. /**
  1967. * _base_add_sg_single_ieee - add sg element for IEEE format
  1968. * @paddr: virtual address for SGE
  1969. * @flags: SGE flags
  1970. * @chain_offset: number of 128 byte elements from start of segment
  1971. * @length: data transfer length
  1972. * @dma_addr: Physical address
  1973. */
  1974. static void
  1975. _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
  1976. dma_addr_t dma_addr)
  1977. {
  1978. Mpi25IeeeSgeChain64_t *sgel = paddr;
  1979. sgel->Flags = flags;
  1980. sgel->NextChainOffset = chain_offset;
  1981. sgel->Length = cpu_to_le32(length);
  1982. sgel->Address = cpu_to_le64(dma_addr);
  1983. }
  1984. /**
  1985. * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
  1986. * @ioc: per adapter object
  1987. * @paddr: virtual address for SGE
  1988. *
  1989. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1990. * something to use if the target device goes brain dead and tries
  1991. * to send data even when none is asked for.
  1992. */
  1993. static void
  1994. _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1995. {
  1996. u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1997. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  1998. MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
  1999. _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
  2000. }
  2001. /**
  2002. * _base_build_sg_scmd - main sg creation routine
  2003. * pcie_device is unused here!
  2004. * @ioc: per adapter object
  2005. * @scmd: scsi command
  2006. * @smid: system request message index
  2007. * @unused: unused pcie_device pointer
  2008. * Context: none.
  2009. *
  2010. * The main routine that builds scatter gather table from a given
  2011. * scsi request sent via the .queuecommand main handler.
  2012. *
  2013. * Return: 0 success, anything else error
  2014. */
  2015. static int
  2016. _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
  2017. struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
  2018. {
  2019. Mpi2SCSIIORequest_t *mpi_request;
  2020. dma_addr_t chain_dma;
  2021. struct scatterlist *sg_scmd;
  2022. void *sg_local, *chain;
  2023. u32 chain_offset;
  2024. u32 chain_length;
  2025. u32 chain_flags;
  2026. int sges_left;
  2027. u32 sges_in_segment;
  2028. u32 sgl_flags;
  2029. u32 sgl_flags_last_element;
  2030. u32 sgl_flags_end_buffer;
  2031. struct chain_tracker *chain_req;
  2032. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2033. /* init scatter gather flags */
  2034. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  2035. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2036. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  2037. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  2038. << MPI2_SGE_FLAGS_SHIFT;
  2039. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  2040. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  2041. << MPI2_SGE_FLAGS_SHIFT;
  2042. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  2043. sg_scmd = scsi_sglist(scmd);
  2044. sges_left = scsi_dma_map(scmd);
  2045. if (sges_left < 0) {
  2046. sdev_printk(KERN_ERR, scmd->device,
  2047. "pci_map_sg failed: request for %d bytes!\n",
  2048. scsi_bufflen(scmd));
  2049. return -ENOMEM;
  2050. }
  2051. sg_local = &mpi_request->SGL;
  2052. sges_in_segment = ioc->max_sges_in_main_message;
  2053. if (sges_left <= sges_in_segment)
  2054. goto fill_in_last_segment;
  2055. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  2056. (sges_in_segment * ioc->sge_size))/4;
  2057. /* fill in main message segment when there is a chain following */
  2058. while (sges_in_segment) {
  2059. if (sges_in_segment == 1)
  2060. ioc->base_add_sg_single(sg_local,
  2061. sgl_flags_last_element | sg_dma_len(sg_scmd),
  2062. sg_dma_address(sg_scmd));
  2063. else
  2064. ioc->base_add_sg_single(sg_local, sgl_flags |
  2065. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2066. sg_scmd = sg_next(sg_scmd);
  2067. sg_local += ioc->sge_size;
  2068. sges_left--;
  2069. sges_in_segment--;
  2070. }
  2071. /* initializing the chain flags and pointers */
  2072. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  2073. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2074. if (!chain_req)
  2075. return -1;
  2076. chain = chain_req->chain_buffer;
  2077. chain_dma = chain_req->chain_buffer_dma;
  2078. do {
  2079. sges_in_segment = (sges_left <=
  2080. ioc->max_sges_in_chain_message) ? sges_left :
  2081. ioc->max_sges_in_chain_message;
  2082. chain_offset = (sges_left == sges_in_segment) ?
  2083. 0 : (sges_in_segment * ioc->sge_size)/4;
  2084. chain_length = sges_in_segment * ioc->sge_size;
  2085. if (chain_offset) {
  2086. chain_offset = chain_offset <<
  2087. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  2088. chain_length += ioc->sge_size;
  2089. }
  2090. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  2091. chain_length, chain_dma);
  2092. sg_local = chain;
  2093. if (!chain_offset)
  2094. goto fill_in_last_segment;
  2095. /* fill in chain segments */
  2096. while (sges_in_segment) {
  2097. if (sges_in_segment == 1)
  2098. ioc->base_add_sg_single(sg_local,
  2099. sgl_flags_last_element |
  2100. sg_dma_len(sg_scmd),
  2101. sg_dma_address(sg_scmd));
  2102. else
  2103. ioc->base_add_sg_single(sg_local, sgl_flags |
  2104. sg_dma_len(sg_scmd),
  2105. sg_dma_address(sg_scmd));
  2106. sg_scmd = sg_next(sg_scmd);
  2107. sg_local += ioc->sge_size;
  2108. sges_left--;
  2109. sges_in_segment--;
  2110. }
  2111. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2112. if (!chain_req)
  2113. return -1;
  2114. chain = chain_req->chain_buffer;
  2115. chain_dma = chain_req->chain_buffer_dma;
  2116. } while (1);
  2117. fill_in_last_segment:
  2118. /* fill the last segment */
  2119. while (sges_left) {
  2120. if (sges_left == 1)
  2121. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  2122. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2123. else
  2124. ioc->base_add_sg_single(sg_local, sgl_flags |
  2125. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2126. sg_scmd = sg_next(sg_scmd);
  2127. sg_local += ioc->sge_size;
  2128. sges_left--;
  2129. }
  2130. return 0;
  2131. }
  2132. /**
  2133. * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
  2134. * @ioc: per adapter object
  2135. * @scmd: scsi command
  2136. * @smid: system request message index
  2137. * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
  2138. * constructed on need.
  2139. * Context: none.
  2140. *
  2141. * The main routine that builds scatter gather table from a given
  2142. * scsi request sent via the .queuecommand main handler.
  2143. *
  2144. * Return: 0 success, anything else error
  2145. */
  2146. static int
  2147. _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
  2148. struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
  2149. {
  2150. Mpi25SCSIIORequest_t *mpi_request;
  2151. dma_addr_t chain_dma;
  2152. struct scatterlist *sg_scmd;
  2153. void *sg_local, *chain;
  2154. u32 chain_offset;
  2155. u32 chain_length;
  2156. int sges_left;
  2157. u32 sges_in_segment;
  2158. u8 simple_sgl_flags;
  2159. u8 simple_sgl_flags_last;
  2160. u8 chain_sgl_flags;
  2161. struct chain_tracker *chain_req;
  2162. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2163. /* init scatter gather flags */
  2164. simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2165. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2166. simple_sgl_flags_last = simple_sgl_flags |
  2167. MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  2168. chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  2169. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2170. /* Check if we need to build a native SG list. */
  2171. if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
  2172. smid, scmd, pcie_device) == 0)) {
  2173. /* We built a native SG list, just return. */
  2174. return 0;
  2175. }
  2176. sg_scmd = scsi_sglist(scmd);
  2177. sges_left = scsi_dma_map(scmd);
  2178. if (sges_left < 0) {
  2179. sdev_printk(KERN_ERR, scmd->device,
  2180. "pci_map_sg failed: request for %d bytes!\n",
  2181. scsi_bufflen(scmd));
  2182. return -ENOMEM;
  2183. }
  2184. sg_local = &mpi_request->SGL;
  2185. sges_in_segment = (ioc->request_sz -
  2186. offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
  2187. if (sges_left <= sges_in_segment)
  2188. goto fill_in_last_segment;
  2189. mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
  2190. (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
  2191. /* fill in main message segment when there is a chain following */
  2192. while (sges_in_segment > 1) {
  2193. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  2194. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2195. sg_scmd = sg_next(sg_scmd);
  2196. sg_local += ioc->sge_size_ieee;
  2197. sges_left--;
  2198. sges_in_segment--;
  2199. }
  2200. /* initializing the pointers */
  2201. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2202. if (!chain_req)
  2203. return -1;
  2204. chain = chain_req->chain_buffer;
  2205. chain_dma = chain_req->chain_buffer_dma;
  2206. do {
  2207. sges_in_segment = (sges_left <=
  2208. ioc->max_sges_in_chain_message) ? sges_left :
  2209. ioc->max_sges_in_chain_message;
  2210. chain_offset = (sges_left == sges_in_segment) ?
  2211. 0 : sges_in_segment;
  2212. chain_length = sges_in_segment * ioc->sge_size_ieee;
  2213. if (chain_offset)
  2214. chain_length += ioc->sge_size_ieee;
  2215. _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
  2216. chain_offset, chain_length, chain_dma);
  2217. sg_local = chain;
  2218. if (!chain_offset)
  2219. goto fill_in_last_segment;
  2220. /* fill in chain segments */
  2221. while (sges_in_segment) {
  2222. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  2223. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2224. sg_scmd = sg_next(sg_scmd);
  2225. sg_local += ioc->sge_size_ieee;
  2226. sges_left--;
  2227. sges_in_segment--;
  2228. }
  2229. chain_req = _base_get_chain_buffer_tracker(ioc, scmd);
  2230. if (!chain_req)
  2231. return -1;
  2232. chain = chain_req->chain_buffer;
  2233. chain_dma = chain_req->chain_buffer_dma;
  2234. } while (1);
  2235. fill_in_last_segment:
  2236. /* fill the last segment */
  2237. while (sges_left > 0) {
  2238. if (sges_left == 1)
  2239. _base_add_sg_single_ieee(sg_local,
  2240. simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
  2241. sg_dma_address(sg_scmd));
  2242. else
  2243. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  2244. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  2245. sg_scmd = sg_next(sg_scmd);
  2246. sg_local += ioc->sge_size_ieee;
  2247. sges_left--;
  2248. }
  2249. return 0;
  2250. }
  2251. /**
  2252. * _base_build_sg_ieee - build generic sg for IEEE format
  2253. * @ioc: per adapter object
  2254. * @psge: virtual address for SGE
  2255. * @data_out_dma: physical address for WRITES
  2256. * @data_out_sz: data xfer size for WRITES
  2257. * @data_in_dma: physical address for READS
  2258. * @data_in_sz: data xfer size for READS
  2259. */
  2260. static void
  2261. _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
  2262. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  2263. size_t data_in_sz)
  2264. {
  2265. u8 sgl_flags;
  2266. if (!data_out_sz && !data_in_sz) {
  2267. _base_build_zero_len_sge_ieee(ioc, psge);
  2268. return;
  2269. }
  2270. if (data_out_sz && data_in_sz) {
  2271. /* WRITE sgel first */
  2272. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2273. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2274. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  2275. data_out_dma);
  2276. /* incr sgel */
  2277. psge += ioc->sge_size_ieee;
  2278. /* READ sgel last */
  2279. sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  2280. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  2281. data_in_dma);
  2282. } else if (data_out_sz) /* WRITE */ {
  2283. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2284. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  2285. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2286. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  2287. data_out_dma);
  2288. } else if (data_in_sz) /* READ */ {
  2289. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  2290. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  2291. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  2292. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  2293. data_in_dma);
  2294. }
  2295. }
  2296. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  2297. /**
  2298. * _base_config_dma_addressing - set dma addressing
  2299. * @ioc: per adapter object
  2300. * @pdev: PCI device struct
  2301. *
  2302. * Return: 0 for success, non-zero for failure.
  2303. */
  2304. static int
  2305. _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
  2306. {
  2307. struct sysinfo s;
  2308. u64 consistent_dma_mask;
  2309. /* Set 63 bit DMA mask for all SAS3 and SAS35 controllers */
  2310. int dma_mask = (ioc->hba_mpi_version_belonged > MPI2_VERSION) ? 63 : 64;
  2311. if (ioc->is_mcpu_endpoint)
  2312. goto try_32bit;
  2313. if (ioc->dma_mask)
  2314. consistent_dma_mask = DMA_BIT_MASK(dma_mask);
  2315. else
  2316. consistent_dma_mask = DMA_BIT_MASK(32);
  2317. if (sizeof(dma_addr_t) > 4) {
  2318. const uint64_t required_mask =
  2319. dma_get_required_mask(&pdev->dev);
  2320. if ((required_mask > DMA_BIT_MASK(32)) &&
  2321. !pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_mask)) &&
  2322. !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
  2323. ioc->base_add_sg_single = &_base_add_sg_single_64;
  2324. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  2325. ioc->dma_mask = dma_mask;
  2326. goto out;
  2327. }
  2328. }
  2329. try_32bit:
  2330. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  2331. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  2332. ioc->base_add_sg_single = &_base_add_sg_single_32;
  2333. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  2334. ioc->dma_mask = 32;
  2335. } else
  2336. return -ENODEV;
  2337. out:
  2338. si_meminfo(&s);
  2339. pr_info(MPT3SAS_FMT
  2340. "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
  2341. ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
  2342. return 0;
  2343. }
  2344. static int
  2345. _base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
  2346. struct pci_dev *pdev)
  2347. {
  2348. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(ioc->dma_mask))) {
  2349. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  2350. return -ENODEV;
  2351. }
  2352. return 0;
  2353. }
  2354. /**
  2355. * _base_check_enable_msix - checks MSIX capabable.
  2356. * @ioc: per adapter object
  2357. *
  2358. * Check to see if card is capable of MSIX, and set number
  2359. * of available msix vectors
  2360. */
  2361. static int
  2362. _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  2363. {
  2364. int base;
  2365. u16 message_control;
  2366. /* Check whether controller SAS2008 B0 controller,
  2367. * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
  2368. */
  2369. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
  2370. ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
  2371. return -EINVAL;
  2372. }
  2373. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  2374. if (!base) {
  2375. dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
  2376. ioc->name));
  2377. return -EINVAL;
  2378. }
  2379. /* get msix vector count */
  2380. /* NUMA_IO not supported for older controllers */
  2381. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
  2382. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
  2383. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
  2384. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
  2385. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
  2386. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
  2387. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
  2388. ioc->msix_vector_count = 1;
  2389. else {
  2390. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  2391. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  2392. }
  2393. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2394. "msix is supported, vector_count(%d)\n",
  2395. ioc->name, ioc->msix_vector_count));
  2396. return 0;
  2397. }
  2398. /**
  2399. * _base_free_irq - free irq
  2400. * @ioc: per adapter object
  2401. *
  2402. * Freeing respective reply_queue from the list.
  2403. */
  2404. static void
  2405. _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
  2406. {
  2407. struct adapter_reply_queue *reply_q, *next;
  2408. if (list_empty(&ioc->reply_queue_list))
  2409. return;
  2410. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  2411. list_del(&reply_q->list);
  2412. free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
  2413. reply_q);
  2414. kfree(reply_q);
  2415. }
  2416. }
  2417. /**
  2418. * _base_request_irq - request irq
  2419. * @ioc: per adapter object
  2420. * @index: msix index into vector table
  2421. *
  2422. * Inserting respective reply_queue into the list.
  2423. */
  2424. static int
  2425. _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
  2426. {
  2427. struct pci_dev *pdev = ioc->pdev;
  2428. struct adapter_reply_queue *reply_q;
  2429. int r;
  2430. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  2431. if (!reply_q) {
  2432. pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
  2433. ioc->name, (int)sizeof(struct adapter_reply_queue));
  2434. return -ENOMEM;
  2435. }
  2436. reply_q->ioc = ioc;
  2437. reply_q->msix_index = index;
  2438. atomic_set(&reply_q->busy, 0);
  2439. if (ioc->msix_enable)
  2440. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  2441. ioc->driver_name, ioc->id, index);
  2442. else
  2443. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  2444. ioc->driver_name, ioc->id);
  2445. r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
  2446. IRQF_SHARED, reply_q->name, reply_q);
  2447. if (r) {
  2448. pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
  2449. reply_q->name, pci_irq_vector(pdev, index));
  2450. kfree(reply_q);
  2451. return -EBUSY;
  2452. }
  2453. INIT_LIST_HEAD(&reply_q->list);
  2454. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  2455. return 0;
  2456. }
  2457. /**
  2458. * _base_assign_reply_queues - assigning msix index for each cpu
  2459. * @ioc: per adapter object
  2460. *
  2461. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  2462. *
  2463. * It would nice if we could call irq_set_affinity, however it is not
  2464. * an exported symbol
  2465. */
  2466. static void
  2467. _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  2468. {
  2469. unsigned int cpu, nr_cpus, nr_msix, index = 0;
  2470. struct adapter_reply_queue *reply_q;
  2471. if (!_base_is_controller_msix_enabled(ioc))
  2472. return;
  2473. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  2474. nr_cpus = num_online_cpus();
  2475. nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
  2476. ioc->facts.MaxMSIxVectors);
  2477. if (!nr_msix)
  2478. return;
  2479. if (smp_affinity_enable) {
  2480. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  2481. const cpumask_t *mask = pci_irq_get_affinity(ioc->pdev,
  2482. reply_q->msix_index);
  2483. if (!mask) {
  2484. pr_warn(MPT3SAS_FMT "no affinity for msi %x\n",
  2485. ioc->name, reply_q->msix_index);
  2486. continue;
  2487. }
  2488. for_each_cpu_and(cpu, mask, cpu_online_mask) {
  2489. if (cpu >= ioc->cpu_msix_table_sz)
  2490. break;
  2491. ioc->cpu_msix_table[cpu] = reply_q->msix_index;
  2492. }
  2493. }
  2494. return;
  2495. }
  2496. cpu = cpumask_first(cpu_online_mask);
  2497. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  2498. unsigned int i, group = nr_cpus / nr_msix;
  2499. if (cpu >= nr_cpus)
  2500. break;
  2501. if (index < nr_cpus % nr_msix)
  2502. group++;
  2503. for (i = 0 ; i < group ; i++) {
  2504. ioc->cpu_msix_table[cpu] = reply_q->msix_index;
  2505. cpu = cpumask_next(cpu, cpu_online_mask);
  2506. }
  2507. index++;
  2508. }
  2509. }
  2510. /**
  2511. * _base_disable_msix - disables msix
  2512. * @ioc: per adapter object
  2513. *
  2514. */
  2515. static void
  2516. _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
  2517. {
  2518. if (!ioc->msix_enable)
  2519. return;
  2520. pci_disable_msix(ioc->pdev);
  2521. ioc->msix_enable = 0;
  2522. }
  2523. /**
  2524. * _base_enable_msix - enables msix, failback to io_apic
  2525. * @ioc: per adapter object
  2526. *
  2527. */
  2528. static int
  2529. _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  2530. {
  2531. int r;
  2532. int i, local_max_msix_vectors;
  2533. u8 try_msix = 0;
  2534. unsigned int irq_flags = PCI_IRQ_MSIX;
  2535. if (msix_disable == -1 || msix_disable == 0)
  2536. try_msix = 1;
  2537. if (!try_msix)
  2538. goto try_ioapic;
  2539. if (_base_check_enable_msix(ioc) != 0)
  2540. goto try_ioapic;
  2541. ioc->reply_queue_count = min_t(int, ioc->cpu_count,
  2542. ioc->msix_vector_count);
  2543. printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
  2544. ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
  2545. ioc->cpu_count, max_msix_vectors);
  2546. if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
  2547. local_max_msix_vectors = (reset_devices) ? 1 : 8;
  2548. else
  2549. local_max_msix_vectors = max_msix_vectors;
  2550. if (local_max_msix_vectors > 0)
  2551. ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
  2552. ioc->reply_queue_count);
  2553. else if (local_max_msix_vectors == 0)
  2554. goto try_ioapic;
  2555. if (ioc->msix_vector_count < ioc->cpu_count)
  2556. smp_affinity_enable = 0;
  2557. if (smp_affinity_enable)
  2558. irq_flags |= PCI_IRQ_AFFINITY;
  2559. r = pci_alloc_irq_vectors(ioc->pdev, 1, ioc->reply_queue_count,
  2560. irq_flags);
  2561. if (r < 0) {
  2562. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  2563. "pci_alloc_irq_vectors failed (r=%d) !!!\n",
  2564. ioc->name, r));
  2565. goto try_ioapic;
  2566. }
  2567. ioc->msix_enable = 1;
  2568. ioc->reply_queue_count = r;
  2569. for (i = 0; i < ioc->reply_queue_count; i++) {
  2570. r = _base_request_irq(ioc, i);
  2571. if (r) {
  2572. _base_free_irq(ioc);
  2573. _base_disable_msix(ioc);
  2574. goto try_ioapic;
  2575. }
  2576. }
  2577. return 0;
  2578. /* failback to io_apic interrupt routing */
  2579. try_ioapic:
  2580. ioc->reply_queue_count = 1;
  2581. r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
  2582. if (r < 0) {
  2583. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  2584. "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
  2585. ioc->name, r));
  2586. } else
  2587. r = _base_request_irq(ioc, 0);
  2588. return r;
  2589. }
  2590. /**
  2591. * mpt3sas_base_unmap_resources - free controller resources
  2592. * @ioc: per adapter object
  2593. */
  2594. static void
  2595. mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
  2596. {
  2597. struct pci_dev *pdev = ioc->pdev;
  2598. dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
  2599. ioc->name, __func__));
  2600. _base_free_irq(ioc);
  2601. _base_disable_msix(ioc);
  2602. kfree(ioc->replyPostRegisterIndex);
  2603. ioc->replyPostRegisterIndex = NULL;
  2604. if (ioc->chip_phys) {
  2605. iounmap(ioc->chip);
  2606. ioc->chip_phys = 0;
  2607. }
  2608. if (pci_is_enabled(pdev)) {
  2609. pci_release_selected_regions(ioc->pdev, ioc->bars);
  2610. pci_disable_pcie_error_reporting(pdev);
  2611. pci_disable_device(pdev);
  2612. }
  2613. }
  2614. /**
  2615. * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
  2616. * @ioc: per adapter object
  2617. *
  2618. * Return: 0 for success, non-zero for failure.
  2619. */
  2620. int
  2621. mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
  2622. {
  2623. struct pci_dev *pdev = ioc->pdev;
  2624. u32 memap_sz;
  2625. u32 pio_sz;
  2626. int i, r = 0;
  2627. u64 pio_chip = 0;
  2628. phys_addr_t chip_phys = 0;
  2629. struct adapter_reply_queue *reply_q;
  2630. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
  2631. ioc->name, __func__));
  2632. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  2633. if (pci_enable_device_mem(pdev)) {
  2634. pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
  2635. ioc->name);
  2636. ioc->bars = 0;
  2637. return -ENODEV;
  2638. }
  2639. if (pci_request_selected_regions(pdev, ioc->bars,
  2640. ioc->driver_name)) {
  2641. pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
  2642. ioc->name);
  2643. ioc->bars = 0;
  2644. r = -ENODEV;
  2645. goto out_fail;
  2646. }
  2647. /* AER (Advanced Error Reporting) hooks */
  2648. pci_enable_pcie_error_reporting(pdev);
  2649. pci_set_master(pdev);
  2650. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  2651. pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
  2652. ioc->name, pci_name(pdev));
  2653. r = -ENODEV;
  2654. goto out_fail;
  2655. }
  2656. for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
  2657. (!memap_sz || !pio_sz); i++) {
  2658. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  2659. if (pio_sz)
  2660. continue;
  2661. pio_chip = (u64)pci_resource_start(pdev, i);
  2662. pio_sz = pci_resource_len(pdev, i);
  2663. } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  2664. if (memap_sz)
  2665. continue;
  2666. ioc->chip_phys = pci_resource_start(pdev, i);
  2667. chip_phys = ioc->chip_phys;
  2668. memap_sz = pci_resource_len(pdev, i);
  2669. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  2670. }
  2671. }
  2672. if (ioc->chip == NULL) {
  2673. pr_err(MPT3SAS_FMT "unable to map adapter memory! "
  2674. " or resource not found\n", ioc->name);
  2675. r = -EINVAL;
  2676. goto out_fail;
  2677. }
  2678. _base_mask_interrupts(ioc);
  2679. r = _base_get_ioc_facts(ioc);
  2680. if (r)
  2681. goto out_fail;
  2682. if (!ioc->rdpq_array_enable_assigned) {
  2683. ioc->rdpq_array_enable = ioc->rdpq_array_capable;
  2684. ioc->rdpq_array_enable_assigned = 1;
  2685. }
  2686. r = _base_enable_msix(ioc);
  2687. if (r)
  2688. goto out_fail;
  2689. /* Use the Combined reply queue feature only for SAS3 C0 & higher
  2690. * revision HBAs and also only when reply queue count is greater than 8
  2691. */
  2692. if (ioc->combined_reply_queue) {
  2693. /* Determine the Supplemental Reply Post Host Index Registers
  2694. * Addresse. Supplemental Reply Post Host Index Registers
  2695. * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
  2696. * each register is at offset bytes of
  2697. * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
  2698. */
  2699. ioc->replyPostRegisterIndex = kcalloc(
  2700. ioc->combined_reply_index_count,
  2701. sizeof(resource_size_t *), GFP_KERNEL);
  2702. if (!ioc->replyPostRegisterIndex) {
  2703. dfailprintk(ioc, printk(MPT3SAS_FMT
  2704. "allocation for reply Post Register Index failed!!!\n",
  2705. ioc->name));
  2706. r = -ENOMEM;
  2707. goto out_fail;
  2708. }
  2709. for (i = 0; i < ioc->combined_reply_index_count; i++) {
  2710. ioc->replyPostRegisterIndex[i] = (resource_size_t *)
  2711. ((u8 __force *)&ioc->chip->Doorbell +
  2712. MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
  2713. (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
  2714. }
  2715. }
  2716. if (ioc->is_warpdrive) {
  2717. ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
  2718. &ioc->chip->ReplyPostHostIndex;
  2719. for (i = 1; i < ioc->cpu_msix_table_sz; i++)
  2720. ioc->reply_post_host_index[i] =
  2721. (resource_size_t __iomem *)
  2722. ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
  2723. * 4)));
  2724. }
  2725. list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
  2726. pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
  2727. reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  2728. "IO-APIC enabled"),
  2729. pci_irq_vector(ioc->pdev, reply_q->msix_index));
  2730. pr_info(MPT3SAS_FMT "iomem(%pap), mapped(0x%p), size(%d)\n",
  2731. ioc->name, &chip_phys, ioc->chip, memap_sz);
  2732. pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
  2733. ioc->name, (unsigned long long)pio_chip, pio_sz);
  2734. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  2735. pci_save_state(pdev);
  2736. return 0;
  2737. out_fail:
  2738. mpt3sas_base_unmap_resources(ioc);
  2739. return r;
  2740. }
  2741. /**
  2742. * mpt3sas_base_get_msg_frame - obtain request mf pointer
  2743. * @ioc: per adapter object
  2744. * @smid: system request message index(smid zero is invalid)
  2745. *
  2746. * Return: virt pointer to message frame.
  2747. */
  2748. void *
  2749. mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2750. {
  2751. return (void *)(ioc->request + (smid * ioc->request_sz));
  2752. }
  2753. /**
  2754. * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
  2755. * @ioc: per adapter object
  2756. * @smid: system request message index
  2757. *
  2758. * Return: virt pointer to sense buffer.
  2759. */
  2760. void *
  2761. mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2762. {
  2763. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  2764. }
  2765. /**
  2766. * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
  2767. * @ioc: per adapter object
  2768. * @smid: system request message index
  2769. *
  2770. * Return: phys pointer to the low 32bit address of the sense buffer.
  2771. */
  2772. __le32
  2773. mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2774. {
  2775. return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
  2776. SCSI_SENSE_BUFFERSIZE));
  2777. }
  2778. /**
  2779. * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
  2780. * @ioc: per adapter object
  2781. * @smid: system request message index
  2782. *
  2783. * Return: virt pointer to a PCIe SGL.
  2784. */
  2785. void *
  2786. mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2787. {
  2788. return (void *)(ioc->pcie_sg_lookup[smid - 1].pcie_sgl);
  2789. }
  2790. /**
  2791. * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
  2792. * @ioc: per adapter object
  2793. * @smid: system request message index
  2794. *
  2795. * Return: phys pointer to the address of the PCIe buffer.
  2796. */
  2797. dma_addr_t
  2798. mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2799. {
  2800. return ioc->pcie_sg_lookup[smid - 1].pcie_sgl_dma;
  2801. }
  2802. /**
  2803. * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
  2804. * @ioc: per adapter object
  2805. * @phys_addr: lower 32 physical addr of the reply
  2806. *
  2807. * Converts 32bit lower physical addr into a virt address.
  2808. */
  2809. void *
  2810. mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
  2811. {
  2812. if (!phys_addr)
  2813. return NULL;
  2814. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  2815. }
  2816. static inline u8
  2817. _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
  2818. {
  2819. return ioc->cpu_msix_table[raw_smp_processor_id()];
  2820. }
  2821. /**
  2822. * mpt3sas_base_get_smid - obtain a free smid from internal queue
  2823. * @ioc: per adapter object
  2824. * @cb_idx: callback index
  2825. *
  2826. * Return: smid (zero is invalid)
  2827. */
  2828. u16
  2829. mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  2830. {
  2831. unsigned long flags;
  2832. struct request_tracker *request;
  2833. u16 smid;
  2834. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2835. if (list_empty(&ioc->internal_free_list)) {
  2836. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2837. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  2838. ioc->name, __func__);
  2839. return 0;
  2840. }
  2841. request = list_entry(ioc->internal_free_list.next,
  2842. struct request_tracker, tracker_list);
  2843. request->cb_idx = cb_idx;
  2844. smid = request->smid;
  2845. list_del(&request->tracker_list);
  2846. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2847. return smid;
  2848. }
  2849. /**
  2850. * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  2851. * @ioc: per adapter object
  2852. * @cb_idx: callback index
  2853. * @scmd: pointer to scsi command object
  2854. *
  2855. * Return: smid (zero is invalid)
  2856. */
  2857. u16
  2858. mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
  2859. struct scsi_cmnd *scmd)
  2860. {
  2861. struct scsiio_tracker *request = scsi_cmd_priv(scmd);
  2862. unsigned int tag = scmd->request->tag;
  2863. u16 smid;
  2864. smid = tag + 1;
  2865. request->cb_idx = cb_idx;
  2866. request->msix_io = _base_get_msix_index(ioc);
  2867. request->smid = smid;
  2868. INIT_LIST_HEAD(&request->chain_list);
  2869. return smid;
  2870. }
  2871. /**
  2872. * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  2873. * @ioc: per adapter object
  2874. * @cb_idx: callback index
  2875. *
  2876. * Return: smid (zero is invalid)
  2877. */
  2878. u16
  2879. mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  2880. {
  2881. unsigned long flags;
  2882. struct request_tracker *request;
  2883. u16 smid;
  2884. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2885. if (list_empty(&ioc->hpr_free_list)) {
  2886. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2887. return 0;
  2888. }
  2889. request = list_entry(ioc->hpr_free_list.next,
  2890. struct request_tracker, tracker_list);
  2891. request->cb_idx = cb_idx;
  2892. smid = request->smid;
  2893. list_del(&request->tracker_list);
  2894. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2895. return smid;
  2896. }
  2897. static void
  2898. _base_recovery_check(struct MPT3SAS_ADAPTER *ioc)
  2899. {
  2900. /*
  2901. * See _wait_for_commands_to_complete() call with regards to this code.
  2902. */
  2903. if (ioc->shost_recovery && ioc->pending_io_count) {
  2904. ioc->pending_io_count = scsi_host_busy(ioc->shost);
  2905. if (ioc->pending_io_count == 0)
  2906. wake_up(&ioc->reset_wq);
  2907. }
  2908. }
  2909. void mpt3sas_base_clear_st(struct MPT3SAS_ADAPTER *ioc,
  2910. struct scsiio_tracker *st)
  2911. {
  2912. if (WARN_ON(st->smid == 0))
  2913. return;
  2914. st->cb_idx = 0xFF;
  2915. st->direct_io = 0;
  2916. atomic_set(&ioc->chain_lookup[st->smid - 1].chain_offset, 0);
  2917. st->smid = 0;
  2918. }
  2919. /**
  2920. * mpt3sas_base_free_smid - put smid back on free_list
  2921. * @ioc: per adapter object
  2922. * @smid: system request message index
  2923. */
  2924. void
  2925. mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  2926. {
  2927. unsigned long flags;
  2928. int i;
  2929. if (smid < ioc->hi_priority_smid) {
  2930. struct scsiio_tracker *st;
  2931. void *request;
  2932. st = _get_st_from_smid(ioc, smid);
  2933. if (!st) {
  2934. _base_recovery_check(ioc);
  2935. return;
  2936. }
  2937. /* Clear MPI request frame */
  2938. request = mpt3sas_base_get_msg_frame(ioc, smid);
  2939. memset(request, 0, ioc->request_sz);
  2940. mpt3sas_base_clear_st(ioc, st);
  2941. _base_recovery_check(ioc);
  2942. return;
  2943. }
  2944. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2945. if (smid < ioc->internal_smid) {
  2946. /* hi-priority */
  2947. i = smid - ioc->hi_priority_smid;
  2948. ioc->hpr_lookup[i].cb_idx = 0xFF;
  2949. list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
  2950. } else if (smid <= ioc->hba_queue_depth) {
  2951. /* internal queue */
  2952. i = smid - ioc->internal_smid;
  2953. ioc->internal_lookup[i].cb_idx = 0xFF;
  2954. list_add(&ioc->internal_lookup[i].tracker_list,
  2955. &ioc->internal_free_list);
  2956. }
  2957. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2958. }
  2959. /**
  2960. * _base_mpi_ep_writeq - 32 bit write to MMIO
  2961. * @b: data payload
  2962. * @addr: address in MMIO space
  2963. * @writeq_lock: spin lock
  2964. *
  2965. * This special handling for MPI EP to take care of 32 bit
  2966. * environment where its not quarenteed to send the entire word
  2967. * in one transfer.
  2968. */
  2969. static inline void
  2970. _base_mpi_ep_writeq(__u64 b, volatile void __iomem *addr,
  2971. spinlock_t *writeq_lock)
  2972. {
  2973. unsigned long flags;
  2974. spin_lock_irqsave(writeq_lock, flags);
  2975. __raw_writel((u32)(b), addr);
  2976. __raw_writel((u32)(b >> 32), (addr + 4));
  2977. mmiowb();
  2978. spin_unlock_irqrestore(writeq_lock, flags);
  2979. }
  2980. /**
  2981. * _base_writeq - 64 bit write to MMIO
  2982. * @b: data payload
  2983. * @addr: address in MMIO space
  2984. * @writeq_lock: spin lock
  2985. *
  2986. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  2987. * care of 32 bit environment where its not quarenteed to send the entire word
  2988. * in one transfer.
  2989. */
  2990. #if defined(writeq) && defined(CONFIG_64BIT)
  2991. static inline void
  2992. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  2993. {
  2994. wmb();
  2995. __raw_writeq(b, addr);
  2996. barrier();
  2997. }
  2998. #else
  2999. static inline void
  3000. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  3001. {
  3002. _base_mpi_ep_writeq(b, addr, writeq_lock);
  3003. }
  3004. #endif
  3005. /**
  3006. * _base_put_smid_mpi_ep_scsi_io - send SCSI_IO request to firmware
  3007. * @ioc: per adapter object
  3008. * @smid: system request message index
  3009. * @handle: device handle
  3010. */
  3011. static void
  3012. _base_put_smid_mpi_ep_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
  3013. {
  3014. Mpi2RequestDescriptorUnion_t descriptor;
  3015. u64 *request = (u64 *)&descriptor;
  3016. void *mpi_req_iomem;
  3017. __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
  3018. _clone_sg_entries(ioc, (void *) mfp, smid);
  3019. mpi_req_iomem = (void __force *)ioc->chip +
  3020. MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
  3021. _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
  3022. ioc->request_sz);
  3023. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  3024. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  3025. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  3026. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  3027. descriptor.SCSIIO.LMID = 0;
  3028. _base_mpi_ep_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3029. &ioc->scsi_lookup_lock);
  3030. }
  3031. /**
  3032. * _base_put_smid_scsi_io - send SCSI_IO request to firmware
  3033. * @ioc: per adapter object
  3034. * @smid: system request message index
  3035. * @handle: device handle
  3036. */
  3037. static void
  3038. _base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
  3039. {
  3040. Mpi2RequestDescriptorUnion_t descriptor;
  3041. u64 *request = (u64 *)&descriptor;
  3042. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  3043. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  3044. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  3045. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  3046. descriptor.SCSIIO.LMID = 0;
  3047. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3048. &ioc->scsi_lookup_lock);
  3049. }
  3050. /**
  3051. * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
  3052. * @ioc: per adapter object
  3053. * @smid: system request message index
  3054. * @handle: device handle
  3055. */
  3056. void
  3057. mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3058. u16 handle)
  3059. {
  3060. Mpi2RequestDescriptorUnion_t descriptor;
  3061. u64 *request = (u64 *)&descriptor;
  3062. descriptor.SCSIIO.RequestFlags =
  3063. MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  3064. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  3065. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  3066. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  3067. descriptor.SCSIIO.LMID = 0;
  3068. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3069. &ioc->scsi_lookup_lock);
  3070. }
  3071. /**
  3072. * mpt3sas_base_put_smid_hi_priority - send Task Management request to firmware
  3073. * @ioc: per adapter object
  3074. * @smid: system request message index
  3075. * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
  3076. */
  3077. void
  3078. mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3079. u16 msix_task)
  3080. {
  3081. Mpi2RequestDescriptorUnion_t descriptor;
  3082. void *mpi_req_iomem;
  3083. u64 *request;
  3084. if (ioc->is_mcpu_endpoint) {
  3085. __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
  3086. /* TBD 256 is offset within sys register. */
  3087. mpi_req_iomem = (void __force *)ioc->chip
  3088. + MPI_FRAME_START_OFFSET
  3089. + (smid * ioc->request_sz);
  3090. _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
  3091. ioc->request_sz);
  3092. }
  3093. request = (u64 *)&descriptor;
  3094. descriptor.HighPriority.RequestFlags =
  3095. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  3096. descriptor.HighPriority.MSIxIndex = msix_task;
  3097. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  3098. descriptor.HighPriority.LMID = 0;
  3099. descriptor.HighPriority.Reserved1 = 0;
  3100. if (ioc->is_mcpu_endpoint)
  3101. _base_mpi_ep_writeq(*request,
  3102. &ioc->chip->RequestDescriptorPostLow,
  3103. &ioc->scsi_lookup_lock);
  3104. else
  3105. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3106. &ioc->scsi_lookup_lock);
  3107. }
  3108. /**
  3109. * mpt3sas_base_put_smid_nvme_encap - send NVMe encapsulated request to
  3110. * firmware
  3111. * @ioc: per adapter object
  3112. * @smid: system request message index
  3113. */
  3114. void
  3115. mpt3sas_base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3116. {
  3117. Mpi2RequestDescriptorUnion_t descriptor;
  3118. u64 *request = (u64 *)&descriptor;
  3119. descriptor.Default.RequestFlags =
  3120. MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
  3121. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  3122. descriptor.Default.SMID = cpu_to_le16(smid);
  3123. descriptor.Default.LMID = 0;
  3124. descriptor.Default.DescriptorTypeDependent = 0;
  3125. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3126. &ioc->scsi_lookup_lock);
  3127. }
  3128. /**
  3129. * mpt3sas_base_put_smid_default - Default, primarily used for config pages
  3130. * @ioc: per adapter object
  3131. * @smid: system request message index
  3132. */
  3133. void
  3134. mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3135. {
  3136. Mpi2RequestDescriptorUnion_t descriptor;
  3137. void *mpi_req_iomem;
  3138. u64 *request;
  3139. if (ioc->is_mcpu_endpoint) {
  3140. __le32 *mfp = (__le32 *)mpt3sas_base_get_msg_frame(ioc, smid);
  3141. _clone_sg_entries(ioc, (void *) mfp, smid);
  3142. /* TBD 256 is offset within sys register */
  3143. mpi_req_iomem = (void __force *)ioc->chip +
  3144. MPI_FRAME_START_OFFSET + (smid * ioc->request_sz);
  3145. _base_clone_mpi_to_sys_mem(mpi_req_iomem, (void *)mfp,
  3146. ioc->request_sz);
  3147. }
  3148. request = (u64 *)&descriptor;
  3149. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  3150. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  3151. descriptor.Default.SMID = cpu_to_le16(smid);
  3152. descriptor.Default.LMID = 0;
  3153. descriptor.Default.DescriptorTypeDependent = 0;
  3154. if (ioc->is_mcpu_endpoint)
  3155. _base_mpi_ep_writeq(*request,
  3156. &ioc->chip->RequestDescriptorPostLow,
  3157. &ioc->scsi_lookup_lock);
  3158. else
  3159. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  3160. &ioc->scsi_lookup_lock);
  3161. }
  3162. /**
  3163. * _base_display_OEMs_branding - Display branding string
  3164. * @ioc: per adapter object
  3165. */
  3166. static void
  3167. _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
  3168. {
  3169. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  3170. return;
  3171. switch (ioc->pdev->subsystem_vendor) {
  3172. case PCI_VENDOR_ID_INTEL:
  3173. switch (ioc->pdev->device) {
  3174. case MPI2_MFGPAGE_DEVID_SAS2008:
  3175. switch (ioc->pdev->subsystem_device) {
  3176. case MPT2SAS_INTEL_RMS2LL080_SSDID:
  3177. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3178. MPT2SAS_INTEL_RMS2LL080_BRANDING);
  3179. break;
  3180. case MPT2SAS_INTEL_RMS2LL040_SSDID:
  3181. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3182. MPT2SAS_INTEL_RMS2LL040_BRANDING);
  3183. break;
  3184. case MPT2SAS_INTEL_SSD910_SSDID:
  3185. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3186. MPT2SAS_INTEL_SSD910_BRANDING);
  3187. break;
  3188. default:
  3189. pr_info(MPT3SAS_FMT
  3190. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  3191. ioc->name, ioc->pdev->subsystem_device);
  3192. break;
  3193. }
  3194. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  3195. switch (ioc->pdev->subsystem_device) {
  3196. case MPT2SAS_INTEL_RS25GB008_SSDID:
  3197. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3198. MPT2SAS_INTEL_RS25GB008_BRANDING);
  3199. break;
  3200. case MPT2SAS_INTEL_RMS25JB080_SSDID:
  3201. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3202. MPT2SAS_INTEL_RMS25JB080_BRANDING);
  3203. break;
  3204. case MPT2SAS_INTEL_RMS25JB040_SSDID:
  3205. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3206. MPT2SAS_INTEL_RMS25JB040_BRANDING);
  3207. break;
  3208. case MPT2SAS_INTEL_RMS25KB080_SSDID:
  3209. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3210. MPT2SAS_INTEL_RMS25KB080_BRANDING);
  3211. break;
  3212. case MPT2SAS_INTEL_RMS25KB040_SSDID:
  3213. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3214. MPT2SAS_INTEL_RMS25KB040_BRANDING);
  3215. break;
  3216. case MPT2SAS_INTEL_RMS25LB040_SSDID:
  3217. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3218. MPT2SAS_INTEL_RMS25LB040_BRANDING);
  3219. break;
  3220. case MPT2SAS_INTEL_RMS25LB080_SSDID:
  3221. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3222. MPT2SAS_INTEL_RMS25LB080_BRANDING);
  3223. break;
  3224. default:
  3225. pr_info(MPT3SAS_FMT
  3226. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  3227. ioc->name, ioc->pdev->subsystem_device);
  3228. break;
  3229. }
  3230. case MPI25_MFGPAGE_DEVID_SAS3008:
  3231. switch (ioc->pdev->subsystem_device) {
  3232. case MPT3SAS_INTEL_RMS3JC080_SSDID:
  3233. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3234. MPT3SAS_INTEL_RMS3JC080_BRANDING);
  3235. break;
  3236. case MPT3SAS_INTEL_RS3GC008_SSDID:
  3237. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3238. MPT3SAS_INTEL_RS3GC008_BRANDING);
  3239. break;
  3240. case MPT3SAS_INTEL_RS3FC044_SSDID:
  3241. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3242. MPT3SAS_INTEL_RS3FC044_BRANDING);
  3243. break;
  3244. case MPT3SAS_INTEL_RS3UC080_SSDID:
  3245. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3246. MPT3SAS_INTEL_RS3UC080_BRANDING);
  3247. break;
  3248. default:
  3249. pr_info(MPT3SAS_FMT
  3250. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  3251. ioc->name, ioc->pdev->subsystem_device);
  3252. break;
  3253. }
  3254. break;
  3255. default:
  3256. pr_info(MPT3SAS_FMT
  3257. "Intel(R) Controller: Subsystem ID: 0x%X\n",
  3258. ioc->name, ioc->pdev->subsystem_device);
  3259. break;
  3260. }
  3261. break;
  3262. case PCI_VENDOR_ID_DELL:
  3263. switch (ioc->pdev->device) {
  3264. case MPI2_MFGPAGE_DEVID_SAS2008:
  3265. switch (ioc->pdev->subsystem_device) {
  3266. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  3267. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3268. MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
  3269. break;
  3270. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  3271. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3272. MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
  3273. break;
  3274. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  3275. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3276. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
  3277. break;
  3278. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  3279. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3280. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
  3281. break;
  3282. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  3283. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3284. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
  3285. break;
  3286. case MPT2SAS_DELL_PERC_H200_SSDID:
  3287. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3288. MPT2SAS_DELL_PERC_H200_BRANDING);
  3289. break;
  3290. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  3291. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3292. MPT2SAS_DELL_6GBPS_SAS_BRANDING);
  3293. break;
  3294. default:
  3295. pr_info(MPT3SAS_FMT
  3296. "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
  3297. ioc->name, ioc->pdev->subsystem_device);
  3298. break;
  3299. }
  3300. break;
  3301. case MPI25_MFGPAGE_DEVID_SAS3008:
  3302. switch (ioc->pdev->subsystem_device) {
  3303. case MPT3SAS_DELL_12G_HBA_SSDID:
  3304. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3305. MPT3SAS_DELL_12G_HBA_BRANDING);
  3306. break;
  3307. default:
  3308. pr_info(MPT3SAS_FMT
  3309. "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
  3310. ioc->name, ioc->pdev->subsystem_device);
  3311. break;
  3312. }
  3313. break;
  3314. default:
  3315. pr_info(MPT3SAS_FMT
  3316. "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
  3317. ioc->pdev->subsystem_device);
  3318. break;
  3319. }
  3320. break;
  3321. case PCI_VENDOR_ID_CISCO:
  3322. switch (ioc->pdev->device) {
  3323. case MPI25_MFGPAGE_DEVID_SAS3008:
  3324. switch (ioc->pdev->subsystem_device) {
  3325. case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
  3326. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3327. MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
  3328. break;
  3329. case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
  3330. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3331. MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
  3332. break;
  3333. case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
  3334. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3335. MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
  3336. break;
  3337. default:
  3338. pr_info(MPT3SAS_FMT
  3339. "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
  3340. ioc->name, ioc->pdev->subsystem_device);
  3341. break;
  3342. }
  3343. break;
  3344. case MPI25_MFGPAGE_DEVID_SAS3108_1:
  3345. switch (ioc->pdev->subsystem_device) {
  3346. case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
  3347. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3348. MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
  3349. break;
  3350. case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
  3351. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3352. MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
  3353. );
  3354. break;
  3355. default:
  3356. pr_info(MPT3SAS_FMT
  3357. "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
  3358. ioc->name, ioc->pdev->subsystem_device);
  3359. break;
  3360. }
  3361. break;
  3362. default:
  3363. pr_info(MPT3SAS_FMT
  3364. "Cisco SAS HBA: Subsystem ID: 0x%X\n",
  3365. ioc->name, ioc->pdev->subsystem_device);
  3366. break;
  3367. }
  3368. break;
  3369. case MPT2SAS_HP_3PAR_SSVID:
  3370. switch (ioc->pdev->device) {
  3371. case MPI2_MFGPAGE_DEVID_SAS2004:
  3372. switch (ioc->pdev->subsystem_device) {
  3373. case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
  3374. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3375. MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
  3376. break;
  3377. default:
  3378. pr_info(MPT3SAS_FMT
  3379. "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
  3380. ioc->name, ioc->pdev->subsystem_device);
  3381. break;
  3382. }
  3383. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  3384. switch (ioc->pdev->subsystem_device) {
  3385. case MPT2SAS_HP_2_4_INTERNAL_SSDID:
  3386. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3387. MPT2SAS_HP_2_4_INTERNAL_BRANDING);
  3388. break;
  3389. case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
  3390. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3391. MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
  3392. break;
  3393. case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
  3394. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3395. MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
  3396. break;
  3397. case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
  3398. pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3399. MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
  3400. break;
  3401. default:
  3402. pr_info(MPT3SAS_FMT
  3403. "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
  3404. ioc->name, ioc->pdev->subsystem_device);
  3405. break;
  3406. }
  3407. default:
  3408. pr_info(MPT3SAS_FMT
  3409. "HP SAS HBA: Subsystem ID: 0x%X\n",
  3410. ioc->name, ioc->pdev->subsystem_device);
  3411. break;
  3412. }
  3413. default:
  3414. break;
  3415. }
  3416. }
  3417. /**
  3418. * _base_display_fwpkg_version - sends FWUpload request to pull FWPkg
  3419. * version from FW Image Header.
  3420. * @ioc: per adapter object
  3421. *
  3422. * Return: 0 for success, non-zero for failure.
  3423. */
  3424. static int
  3425. _base_display_fwpkg_version(struct MPT3SAS_ADAPTER *ioc)
  3426. {
  3427. Mpi2FWImageHeader_t *FWImgHdr;
  3428. Mpi25FWUploadRequest_t *mpi_request;
  3429. Mpi2FWUploadReply_t mpi_reply;
  3430. int r = 0;
  3431. void *fwpkg_data = NULL;
  3432. dma_addr_t fwpkg_data_dma;
  3433. u16 smid, ioc_status;
  3434. size_t data_length;
  3435. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3436. __func__));
  3437. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  3438. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3439. ioc->name, __func__);
  3440. return -EAGAIN;
  3441. }
  3442. data_length = sizeof(Mpi2FWImageHeader_t);
  3443. fwpkg_data = pci_alloc_consistent(ioc->pdev, data_length,
  3444. &fwpkg_data_dma);
  3445. if (!fwpkg_data) {
  3446. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  3447. ioc->name, __FILE__, __LINE__, __func__);
  3448. return -ENOMEM;
  3449. }
  3450. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3451. if (!smid) {
  3452. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3453. ioc->name, __func__);
  3454. r = -EAGAIN;
  3455. goto out;
  3456. }
  3457. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3458. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3459. ioc->base_cmds.smid = smid;
  3460. memset(mpi_request, 0, sizeof(Mpi25FWUploadRequest_t));
  3461. mpi_request->Function = MPI2_FUNCTION_FW_UPLOAD;
  3462. mpi_request->ImageType = MPI2_FW_UPLOAD_ITYPE_FW_FLASH;
  3463. mpi_request->ImageSize = cpu_to_le32(data_length);
  3464. ioc->build_sg(ioc, &mpi_request->SGL, 0, 0, fwpkg_data_dma,
  3465. data_length);
  3466. init_completion(&ioc->base_cmds.done);
  3467. mpt3sas_base_put_smid_default(ioc, smid);
  3468. /* Wait for 15 seconds */
  3469. wait_for_completion_timeout(&ioc->base_cmds.done,
  3470. FW_IMG_HDR_READ_TIMEOUT*HZ);
  3471. pr_info(MPT3SAS_FMT "%s: complete\n",
  3472. ioc->name, __func__);
  3473. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3474. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3475. ioc->name, __func__);
  3476. _debug_dump_mf(mpi_request,
  3477. sizeof(Mpi25FWUploadRequest_t)/4);
  3478. r = -ETIME;
  3479. } else {
  3480. memset(&mpi_reply, 0, sizeof(Mpi2FWUploadReply_t));
  3481. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID) {
  3482. memcpy(&mpi_reply, ioc->base_cmds.reply,
  3483. sizeof(Mpi2FWUploadReply_t));
  3484. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3485. MPI2_IOCSTATUS_MASK;
  3486. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  3487. FWImgHdr = (Mpi2FWImageHeader_t *)fwpkg_data;
  3488. if (FWImgHdr->PackageVersion.Word) {
  3489. pr_info(MPT3SAS_FMT "FW Package Version"
  3490. "(%02d.%02d.%02d.%02d)\n",
  3491. ioc->name,
  3492. FWImgHdr->PackageVersion.Struct.Major,
  3493. FWImgHdr->PackageVersion.Struct.Minor,
  3494. FWImgHdr->PackageVersion.Struct.Unit,
  3495. FWImgHdr->PackageVersion.Struct.Dev);
  3496. }
  3497. } else {
  3498. _debug_dump_mf(&mpi_reply,
  3499. sizeof(Mpi2FWUploadReply_t)/4);
  3500. }
  3501. }
  3502. }
  3503. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3504. out:
  3505. if (fwpkg_data)
  3506. pci_free_consistent(ioc->pdev, data_length, fwpkg_data,
  3507. fwpkg_data_dma);
  3508. return r;
  3509. }
  3510. /**
  3511. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  3512. * @ioc: per adapter object
  3513. */
  3514. static void
  3515. _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
  3516. {
  3517. int i = 0;
  3518. char desc[16];
  3519. u32 iounit_pg1_flags;
  3520. u32 bios_version;
  3521. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  3522. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  3523. pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
  3524. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  3525. ioc->name, desc,
  3526. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  3527. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  3528. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  3529. ioc->facts.FWVersion.Word & 0x000000FF,
  3530. ioc->pdev->revision,
  3531. (bios_version & 0xFF000000) >> 24,
  3532. (bios_version & 0x00FF0000) >> 16,
  3533. (bios_version & 0x0000FF00) >> 8,
  3534. bios_version & 0x000000FF);
  3535. _base_display_OEMs_branding(ioc);
  3536. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
  3537. pr_info("%sNVMe", i ? "," : "");
  3538. i++;
  3539. }
  3540. pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
  3541. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  3542. pr_info("Initiator");
  3543. i++;
  3544. }
  3545. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  3546. pr_info("%sTarget", i ? "," : "");
  3547. i++;
  3548. }
  3549. i = 0;
  3550. pr_info("), ");
  3551. pr_info("Capabilities=(");
  3552. if (!ioc->hide_ir_msg) {
  3553. if (ioc->facts.IOCCapabilities &
  3554. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  3555. pr_info("Raid");
  3556. i++;
  3557. }
  3558. }
  3559. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  3560. pr_info("%sTLR", i ? "," : "");
  3561. i++;
  3562. }
  3563. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  3564. pr_info("%sMulticast", i ? "," : "");
  3565. i++;
  3566. }
  3567. if (ioc->facts.IOCCapabilities &
  3568. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  3569. pr_info("%sBIDI Target", i ? "," : "");
  3570. i++;
  3571. }
  3572. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  3573. pr_info("%sEEDP", i ? "," : "");
  3574. i++;
  3575. }
  3576. if (ioc->facts.IOCCapabilities &
  3577. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  3578. pr_info("%sSnapshot Buffer", i ? "," : "");
  3579. i++;
  3580. }
  3581. if (ioc->facts.IOCCapabilities &
  3582. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  3583. pr_info("%sDiag Trace Buffer", i ? "," : "");
  3584. i++;
  3585. }
  3586. if (ioc->facts.IOCCapabilities &
  3587. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  3588. pr_info("%sDiag Extended Buffer", i ? "," : "");
  3589. i++;
  3590. }
  3591. if (ioc->facts.IOCCapabilities &
  3592. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  3593. pr_info("%sTask Set Full", i ? "," : "");
  3594. i++;
  3595. }
  3596. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  3597. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  3598. pr_info("%sNCQ", i ? "," : "");
  3599. i++;
  3600. }
  3601. pr_info(")\n");
  3602. }
  3603. /**
  3604. * mpt3sas_base_update_missing_delay - change the missing delay timers
  3605. * @ioc: per adapter object
  3606. * @device_missing_delay: amount of time till device is reported missing
  3607. * @io_missing_delay: interval IO is returned when there is a missing device
  3608. *
  3609. * Passed on the command line, this function will modify the device missing
  3610. * delay, as well as the io missing delay. This should be called at driver
  3611. * load time.
  3612. */
  3613. void
  3614. mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
  3615. u16 device_missing_delay, u8 io_missing_delay)
  3616. {
  3617. u16 dmd, dmd_new, dmd_orignal;
  3618. u8 io_missing_delay_original;
  3619. u16 sz;
  3620. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  3621. Mpi2ConfigReply_t mpi_reply;
  3622. u8 num_phys = 0;
  3623. u16 ioc_status;
  3624. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  3625. if (!num_phys)
  3626. return;
  3627. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  3628. sizeof(Mpi2SasIOUnit1PhyData_t));
  3629. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  3630. if (!sas_iounit_pg1) {
  3631. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  3632. ioc->name, __FILE__, __LINE__, __func__);
  3633. goto out;
  3634. }
  3635. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  3636. sas_iounit_pg1, sz))) {
  3637. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  3638. ioc->name, __FILE__, __LINE__, __func__);
  3639. goto out;
  3640. }
  3641. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3642. MPI2_IOCSTATUS_MASK;
  3643. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3644. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  3645. ioc->name, __FILE__, __LINE__, __func__);
  3646. goto out;
  3647. }
  3648. /* device missing delay */
  3649. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  3650. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  3651. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  3652. else
  3653. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  3654. dmd_orignal = dmd;
  3655. if (device_missing_delay > 0x7F) {
  3656. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  3657. device_missing_delay;
  3658. dmd = dmd / 16;
  3659. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  3660. } else
  3661. dmd = device_missing_delay;
  3662. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  3663. /* io missing delay */
  3664. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  3665. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  3666. if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  3667. sz)) {
  3668. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  3669. dmd_new = (dmd &
  3670. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  3671. else
  3672. dmd_new =
  3673. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  3674. pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
  3675. ioc->name, dmd_orignal, dmd_new);
  3676. pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
  3677. ioc->name, io_missing_delay_original,
  3678. io_missing_delay);
  3679. ioc->device_missing_delay = dmd_new;
  3680. ioc->io_missing_delay = io_missing_delay;
  3681. }
  3682. out:
  3683. kfree(sas_iounit_pg1);
  3684. }
  3685. /**
  3686. * _base_static_config_pages - static start of day config pages
  3687. * @ioc: per adapter object
  3688. */
  3689. static void
  3690. _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
  3691. {
  3692. Mpi2ConfigReply_t mpi_reply;
  3693. u32 iounit_pg1_flags;
  3694. ioc->nvme_abort_timeout = 30;
  3695. mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  3696. if (ioc->ir_firmware)
  3697. mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  3698. &ioc->manu_pg10);
  3699. /*
  3700. * Ensure correct T10 PI operation if vendor left EEDPTagMode
  3701. * flag unset in NVDATA.
  3702. */
  3703. mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
  3704. if (!ioc->is_gen35_ioc && ioc->manu_pg11.EEDPTagMode == 0) {
  3705. pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
  3706. ioc->name);
  3707. ioc->manu_pg11.EEDPTagMode &= ~0x3;
  3708. ioc->manu_pg11.EEDPTagMode |= 0x1;
  3709. mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
  3710. &ioc->manu_pg11);
  3711. }
  3712. if (ioc->manu_pg11.AddlFlags2 & NVME_TASK_MNGT_CUSTOM_MASK)
  3713. ioc->tm_custom_handling = 1;
  3714. else {
  3715. ioc->tm_custom_handling = 0;
  3716. if (ioc->manu_pg11.NVMeAbortTO < NVME_TASK_ABORT_MIN_TIMEOUT)
  3717. ioc->nvme_abort_timeout = NVME_TASK_ABORT_MIN_TIMEOUT;
  3718. else if (ioc->manu_pg11.NVMeAbortTO >
  3719. NVME_TASK_ABORT_MAX_TIMEOUT)
  3720. ioc->nvme_abort_timeout = NVME_TASK_ABORT_MAX_TIMEOUT;
  3721. else
  3722. ioc->nvme_abort_timeout = ioc->manu_pg11.NVMeAbortTO;
  3723. }
  3724. mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  3725. mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  3726. mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  3727. mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  3728. mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  3729. mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
  3730. _base_display_ioc_capabilities(ioc);
  3731. /*
  3732. * Enable task_set_full handling in iounit_pg1 when the
  3733. * facts capabilities indicate that its supported.
  3734. */
  3735. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  3736. if ((ioc->facts.IOCCapabilities &
  3737. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  3738. iounit_pg1_flags &=
  3739. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  3740. else
  3741. iounit_pg1_flags |=
  3742. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  3743. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  3744. mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  3745. if (ioc->iounit_pg8.NumSensors)
  3746. ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
  3747. }
  3748. /**
  3749. * mpt3sas_free_enclosure_list - release memory
  3750. * @ioc: per adapter object
  3751. *
  3752. * Free memory allocated during encloure add.
  3753. */
  3754. void
  3755. mpt3sas_free_enclosure_list(struct MPT3SAS_ADAPTER *ioc)
  3756. {
  3757. struct _enclosure_node *enclosure_dev, *enclosure_dev_next;
  3758. /* Free enclosure list */
  3759. list_for_each_entry_safe(enclosure_dev,
  3760. enclosure_dev_next, &ioc->enclosure_list, list) {
  3761. list_del(&enclosure_dev->list);
  3762. kfree(enclosure_dev);
  3763. }
  3764. }
  3765. /**
  3766. * _base_release_memory_pools - release memory
  3767. * @ioc: per adapter object
  3768. *
  3769. * Free memory allocated from _base_allocate_memory_pools.
  3770. */
  3771. static void
  3772. _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  3773. {
  3774. int i = 0;
  3775. int j = 0;
  3776. struct chain_tracker *ct;
  3777. struct reply_post_struct *rps;
  3778. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3779. __func__));
  3780. if (ioc->request) {
  3781. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  3782. ioc->request, ioc->request_dma);
  3783. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  3784. "request_pool(0x%p): free\n",
  3785. ioc->name, ioc->request));
  3786. ioc->request = NULL;
  3787. }
  3788. if (ioc->sense) {
  3789. dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  3790. dma_pool_destroy(ioc->sense_dma_pool);
  3791. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  3792. "sense_pool(0x%p): free\n",
  3793. ioc->name, ioc->sense));
  3794. ioc->sense = NULL;
  3795. }
  3796. if (ioc->reply) {
  3797. dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  3798. dma_pool_destroy(ioc->reply_dma_pool);
  3799. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  3800. "reply_pool(0x%p): free\n",
  3801. ioc->name, ioc->reply));
  3802. ioc->reply = NULL;
  3803. }
  3804. if (ioc->reply_free) {
  3805. dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  3806. ioc->reply_free_dma);
  3807. dma_pool_destroy(ioc->reply_free_dma_pool);
  3808. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  3809. "reply_free_pool(0x%p): free\n",
  3810. ioc->name, ioc->reply_free));
  3811. ioc->reply_free = NULL;
  3812. }
  3813. if (ioc->reply_post) {
  3814. do {
  3815. rps = &ioc->reply_post[i];
  3816. if (rps->reply_post_free) {
  3817. dma_pool_free(
  3818. ioc->reply_post_free_dma_pool,
  3819. rps->reply_post_free,
  3820. rps->reply_post_free_dma);
  3821. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  3822. "reply_post_free_pool(0x%p): free\n",
  3823. ioc->name, rps->reply_post_free));
  3824. rps->reply_post_free = NULL;
  3825. }
  3826. } while (ioc->rdpq_array_enable &&
  3827. (++i < ioc->reply_queue_count));
  3828. if (ioc->reply_post_free_array &&
  3829. ioc->rdpq_array_enable) {
  3830. dma_pool_free(ioc->reply_post_free_array_dma_pool,
  3831. ioc->reply_post_free_array,
  3832. ioc->reply_post_free_array_dma);
  3833. ioc->reply_post_free_array = NULL;
  3834. }
  3835. dma_pool_destroy(ioc->reply_post_free_array_dma_pool);
  3836. dma_pool_destroy(ioc->reply_post_free_dma_pool);
  3837. kfree(ioc->reply_post);
  3838. }
  3839. if (ioc->pcie_sgl_dma_pool) {
  3840. for (i = 0; i < ioc->scsiio_depth; i++) {
  3841. dma_pool_free(ioc->pcie_sgl_dma_pool,
  3842. ioc->pcie_sg_lookup[i].pcie_sgl,
  3843. ioc->pcie_sg_lookup[i].pcie_sgl_dma);
  3844. }
  3845. if (ioc->pcie_sgl_dma_pool)
  3846. dma_pool_destroy(ioc->pcie_sgl_dma_pool);
  3847. }
  3848. if (ioc->config_page) {
  3849. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  3850. "config_page(0x%p): free\n", ioc->name,
  3851. ioc->config_page));
  3852. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  3853. ioc->config_page, ioc->config_page_dma);
  3854. }
  3855. kfree(ioc->hpr_lookup);
  3856. ioc->hpr_lookup = NULL;
  3857. kfree(ioc->internal_lookup);
  3858. ioc->internal_lookup = NULL;
  3859. if (ioc->chain_lookup) {
  3860. for (i = 0; i < ioc->scsiio_depth; i++) {
  3861. for (j = ioc->chains_per_prp_buffer;
  3862. j < ioc->chains_needed_per_io; j++) {
  3863. ct = &ioc->chain_lookup[i].chains_per_smid[j];
  3864. if (ct && ct->chain_buffer)
  3865. dma_pool_free(ioc->chain_dma_pool,
  3866. ct->chain_buffer,
  3867. ct->chain_buffer_dma);
  3868. }
  3869. kfree(ioc->chain_lookup[i].chains_per_smid);
  3870. }
  3871. dma_pool_destroy(ioc->chain_dma_pool);
  3872. kfree(ioc->chain_lookup);
  3873. ioc->chain_lookup = NULL;
  3874. }
  3875. }
  3876. /**
  3877. * is_MSB_are_same - checks whether all reply queues in a set are
  3878. * having same upper 32bits in their base memory address.
  3879. * @reply_pool_start_address: Base address of a reply queue set
  3880. * @pool_sz: Size of single Reply Descriptor Post Queues pool size
  3881. *
  3882. * Return: 1 if reply queues in a set have a same upper 32bits in their base
  3883. * memory address, else 0.
  3884. */
  3885. static int
  3886. is_MSB_are_same(long reply_pool_start_address, u32 pool_sz)
  3887. {
  3888. long reply_pool_end_address;
  3889. reply_pool_end_address = reply_pool_start_address + pool_sz;
  3890. if (upper_32_bits(reply_pool_start_address) ==
  3891. upper_32_bits(reply_pool_end_address))
  3892. return 1;
  3893. else
  3894. return 0;
  3895. }
  3896. /**
  3897. * _base_allocate_memory_pools - allocate start of day memory pools
  3898. * @ioc: per adapter object
  3899. *
  3900. * Return: 0 success, anything else error.
  3901. */
  3902. static int
  3903. _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  3904. {
  3905. struct mpt3sas_facts *facts;
  3906. u16 max_sge_elements;
  3907. u16 chains_needed_per_io;
  3908. u32 sz, total_sz, reply_post_free_sz, reply_post_free_array_sz;
  3909. u32 retry_sz;
  3910. u16 max_request_credit, nvme_blocks_needed;
  3911. unsigned short sg_tablesize;
  3912. u16 sge_size;
  3913. int i, j;
  3914. struct chain_tracker *ct;
  3915. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3916. __func__));
  3917. retry_sz = 0;
  3918. facts = &ioc->facts;
  3919. /* command line tunables for max sgl entries */
  3920. if (max_sgl_entries != -1)
  3921. sg_tablesize = max_sgl_entries;
  3922. else {
  3923. if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
  3924. sg_tablesize = MPT2SAS_SG_DEPTH;
  3925. else
  3926. sg_tablesize = MPT3SAS_SG_DEPTH;
  3927. }
  3928. /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
  3929. if (reset_devices)
  3930. sg_tablesize = min_t(unsigned short, sg_tablesize,
  3931. MPT_KDUMP_MIN_PHYS_SEGMENTS);
  3932. if (ioc->is_mcpu_endpoint)
  3933. ioc->shost->sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
  3934. else {
  3935. if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
  3936. sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
  3937. else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
  3938. sg_tablesize = min_t(unsigned short, sg_tablesize,
  3939. SG_MAX_SEGMENTS);
  3940. pr_warn(MPT3SAS_FMT
  3941. "sg_tablesize(%u) is bigger than kernel "
  3942. "defined SG_CHUNK_SIZE(%u)\n", ioc->name,
  3943. sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
  3944. }
  3945. ioc->shost->sg_tablesize = sg_tablesize;
  3946. }
  3947. ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
  3948. (facts->RequestCredit / 4));
  3949. if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
  3950. if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
  3951. INTERNAL_SCSIIO_CMDS_COUNT)) {
  3952. pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
  3953. Credits, it has just %d number of credits\n",
  3954. ioc->name, facts->RequestCredit);
  3955. return -ENOMEM;
  3956. }
  3957. ioc->internal_depth = 10;
  3958. }
  3959. ioc->hi_priority_depth = ioc->internal_depth - (5);
  3960. /* command line tunables for max controller queue depth */
  3961. if (max_queue_depth != -1 && max_queue_depth != 0) {
  3962. max_request_credit = min_t(u16, max_queue_depth +
  3963. ioc->internal_depth, facts->RequestCredit);
  3964. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  3965. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  3966. } else if (reset_devices)
  3967. max_request_credit = min_t(u16, facts->RequestCredit,
  3968. (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
  3969. else
  3970. max_request_credit = min_t(u16, facts->RequestCredit,
  3971. MAX_HBA_QUEUE_DEPTH);
  3972. /* Firmware maintains additional facts->HighPriorityCredit number of
  3973. * credits for HiPriprity Request messages, so hba queue depth will be
  3974. * sum of max_request_credit and high priority queue depth.
  3975. */
  3976. ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
  3977. /* request frame size */
  3978. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  3979. /* reply frame size */
  3980. ioc->reply_sz = facts->ReplyFrameSize * 4;
  3981. /* chain segment size */
  3982. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  3983. if (facts->IOCMaxChainSegmentSize)
  3984. ioc->chain_segment_sz =
  3985. facts->IOCMaxChainSegmentSize *
  3986. MAX_CHAIN_ELEMT_SZ;
  3987. else
  3988. /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
  3989. ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
  3990. MAX_CHAIN_ELEMT_SZ;
  3991. } else
  3992. ioc->chain_segment_sz = ioc->request_sz;
  3993. /* calculate the max scatter element size */
  3994. sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
  3995. retry_allocation:
  3996. total_sz = 0;
  3997. /* calculate number of sg elements left over in the 1st frame */
  3998. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  3999. sizeof(Mpi2SGEIOUnion_t)) + sge_size);
  4000. ioc->max_sges_in_main_message = max_sge_elements/sge_size;
  4001. /* now do the same for a chain buffer */
  4002. max_sge_elements = ioc->chain_segment_sz - sge_size;
  4003. ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
  4004. /*
  4005. * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  4006. */
  4007. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  4008. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  4009. + 1;
  4010. if (chains_needed_per_io > facts->MaxChainDepth) {
  4011. chains_needed_per_io = facts->MaxChainDepth;
  4012. ioc->shost->sg_tablesize = min_t(u16,
  4013. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  4014. * chains_needed_per_io), ioc->shost->sg_tablesize);
  4015. }
  4016. ioc->chains_needed_per_io = chains_needed_per_io;
  4017. /* reply free queue sizing - taking into account for 64 FW events */
  4018. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  4019. /* mCPU manage single counters for simplicity */
  4020. if (ioc->is_mcpu_endpoint)
  4021. ioc->reply_post_queue_depth = ioc->reply_free_queue_depth;
  4022. else {
  4023. /* calculate reply descriptor post queue depth */
  4024. ioc->reply_post_queue_depth = ioc->hba_queue_depth +
  4025. ioc->reply_free_queue_depth + 1;
  4026. /* align the reply post queue on the next 16 count boundary */
  4027. if (ioc->reply_post_queue_depth % 16)
  4028. ioc->reply_post_queue_depth += 16 -
  4029. (ioc->reply_post_queue_depth % 16);
  4030. }
  4031. if (ioc->reply_post_queue_depth >
  4032. facts->MaxReplyDescriptorPostQueueDepth) {
  4033. ioc->reply_post_queue_depth =
  4034. facts->MaxReplyDescriptorPostQueueDepth -
  4035. (facts->MaxReplyDescriptorPostQueueDepth % 16);
  4036. ioc->hba_queue_depth =
  4037. ((ioc->reply_post_queue_depth - 64) / 2) - 1;
  4038. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  4039. }
  4040. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
  4041. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  4042. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  4043. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  4044. ioc->chains_needed_per_io));
  4045. /* reply post queue, 16 byte align */
  4046. reply_post_free_sz = ioc->reply_post_queue_depth *
  4047. sizeof(Mpi2DefaultReplyDescriptor_t);
  4048. sz = reply_post_free_sz;
  4049. if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
  4050. sz *= ioc->reply_queue_count;
  4051. ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
  4052. (ioc->reply_queue_count):1,
  4053. sizeof(struct reply_post_struct), GFP_KERNEL);
  4054. if (!ioc->reply_post) {
  4055. pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
  4056. ioc->name);
  4057. goto out;
  4058. }
  4059. ioc->reply_post_free_dma_pool = dma_pool_create("reply_post_free pool",
  4060. &ioc->pdev->dev, sz, 16, 0);
  4061. if (!ioc->reply_post_free_dma_pool) {
  4062. pr_err(MPT3SAS_FMT
  4063. "reply_post_free pool: dma_pool_create failed\n",
  4064. ioc->name);
  4065. goto out;
  4066. }
  4067. i = 0;
  4068. do {
  4069. ioc->reply_post[i].reply_post_free =
  4070. dma_pool_alloc(ioc->reply_post_free_dma_pool,
  4071. GFP_KERNEL,
  4072. &ioc->reply_post[i].reply_post_free_dma);
  4073. if (!ioc->reply_post[i].reply_post_free) {
  4074. pr_err(MPT3SAS_FMT
  4075. "reply_post_free pool: dma_pool_alloc failed\n",
  4076. ioc->name);
  4077. goto out;
  4078. }
  4079. memset(ioc->reply_post[i].reply_post_free, 0, sz);
  4080. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4081. "reply post free pool (0x%p): depth(%d),"
  4082. "element_size(%d), pool_size(%d kB)\n", ioc->name,
  4083. ioc->reply_post[i].reply_post_free,
  4084. ioc->reply_post_queue_depth, 8, sz/1024));
  4085. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4086. "reply_post_free_dma = (0x%llx)\n", ioc->name,
  4087. (unsigned long long)
  4088. ioc->reply_post[i].reply_post_free_dma));
  4089. total_sz += sz;
  4090. } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
  4091. if (ioc->dma_mask > 32) {
  4092. if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
  4093. pr_warn(MPT3SAS_FMT
  4094. "no suitable consistent DMA mask for %s\n",
  4095. ioc->name, pci_name(ioc->pdev));
  4096. goto out;
  4097. }
  4098. }
  4099. ioc->scsiio_depth = ioc->hba_queue_depth -
  4100. ioc->hi_priority_depth - ioc->internal_depth;
  4101. /* set the scsi host can_queue depth
  4102. * with some internal commands that could be outstanding
  4103. */
  4104. ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
  4105. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4106. "scsi host: can_queue depth (%d)\n",
  4107. ioc->name, ioc->shost->can_queue));
  4108. /* contiguous pool for request and chains, 16 byte align, one extra "
  4109. * "frame for smid=0
  4110. */
  4111. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  4112. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  4113. /* hi-priority queue */
  4114. sz += (ioc->hi_priority_depth * ioc->request_sz);
  4115. /* internal queue */
  4116. sz += (ioc->internal_depth * ioc->request_sz);
  4117. ioc->request_dma_sz = sz;
  4118. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  4119. if (!ioc->request) {
  4120. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  4121. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  4122. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  4123. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  4124. if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
  4125. goto out;
  4126. retry_sz = 64;
  4127. ioc->hba_queue_depth -= retry_sz;
  4128. _base_release_memory_pools(ioc);
  4129. goto retry_allocation;
  4130. }
  4131. if (retry_sz)
  4132. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  4133. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  4134. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  4135. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  4136. /* hi-priority queue */
  4137. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  4138. ioc->request_sz);
  4139. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  4140. ioc->request_sz);
  4141. /* internal queue */
  4142. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  4143. ioc->request_sz);
  4144. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  4145. ioc->request_sz);
  4146. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4147. "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  4148. ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  4149. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  4150. dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
  4151. ioc->name, (unsigned long long) ioc->request_dma));
  4152. total_sz += sz;
  4153. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
  4154. ioc->name, ioc->request, ioc->scsiio_depth));
  4155. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  4156. sz = ioc->scsiio_depth * sizeof(struct chain_lookup);
  4157. ioc->chain_lookup = kzalloc(sz, GFP_KERNEL);
  4158. if (!ioc->chain_lookup) {
  4159. pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages "
  4160. "failed\n", ioc->name);
  4161. goto out;
  4162. }
  4163. sz = ioc->chains_needed_per_io * sizeof(struct chain_tracker);
  4164. for (i = 0; i < ioc->scsiio_depth; i++) {
  4165. ioc->chain_lookup[i].chains_per_smid = kzalloc(sz, GFP_KERNEL);
  4166. if (!ioc->chain_lookup[i].chains_per_smid) {
  4167. pr_err(MPT3SAS_FMT "chain_lookup: "
  4168. " kzalloc failed\n", ioc->name);
  4169. goto out;
  4170. }
  4171. }
  4172. /* initialize hi-priority queue smid's */
  4173. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  4174. sizeof(struct request_tracker), GFP_KERNEL);
  4175. if (!ioc->hpr_lookup) {
  4176. pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
  4177. ioc->name);
  4178. goto out;
  4179. }
  4180. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  4181. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4182. "hi_priority(0x%p): depth(%d), start smid(%d)\n",
  4183. ioc->name, ioc->hi_priority,
  4184. ioc->hi_priority_depth, ioc->hi_priority_smid));
  4185. /* initialize internal queue smid's */
  4186. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  4187. sizeof(struct request_tracker), GFP_KERNEL);
  4188. if (!ioc->internal_lookup) {
  4189. pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
  4190. ioc->name);
  4191. goto out;
  4192. }
  4193. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  4194. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4195. "internal(0x%p): depth(%d), start smid(%d)\n",
  4196. ioc->name, ioc->internal,
  4197. ioc->internal_depth, ioc->internal_smid));
  4198. /*
  4199. * The number of NVMe page sized blocks needed is:
  4200. * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
  4201. * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
  4202. * that is placed in the main message frame. 8 is the size of each PRP
  4203. * entry or PRP list pointer entry. 8 is subtracted from page_size
  4204. * because of the PRP list pointer entry at the end of a page, so this
  4205. * is not counted as a PRP entry. The 1 added page is a round up.
  4206. *
  4207. * To avoid allocation failures due to the amount of memory that could
  4208. * be required for NVMe PRP's, only each set of NVMe blocks will be
  4209. * contiguous, so a new set is allocated for each possible I/O.
  4210. */
  4211. ioc->chains_per_prp_buffer = 0;
  4212. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
  4213. nvme_blocks_needed =
  4214. (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
  4215. nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
  4216. nvme_blocks_needed++;
  4217. sz = sizeof(struct pcie_sg_list) * ioc->scsiio_depth;
  4218. ioc->pcie_sg_lookup = kzalloc(sz, GFP_KERNEL);
  4219. if (!ioc->pcie_sg_lookup) {
  4220. pr_info(MPT3SAS_FMT
  4221. "PCIe SGL lookup: kzalloc failed\n", ioc->name);
  4222. goto out;
  4223. }
  4224. sz = nvme_blocks_needed * ioc->page_size;
  4225. ioc->pcie_sgl_dma_pool =
  4226. dma_pool_create("PCIe SGL pool", &ioc->pdev->dev, sz, 16, 0);
  4227. if (!ioc->pcie_sgl_dma_pool) {
  4228. pr_info(MPT3SAS_FMT
  4229. "PCIe SGL pool: dma_pool_create failed\n",
  4230. ioc->name);
  4231. goto out;
  4232. }
  4233. ioc->chains_per_prp_buffer = sz/ioc->chain_segment_sz;
  4234. ioc->chains_per_prp_buffer = min(ioc->chains_per_prp_buffer,
  4235. ioc->chains_needed_per_io);
  4236. for (i = 0; i < ioc->scsiio_depth; i++) {
  4237. ioc->pcie_sg_lookup[i].pcie_sgl = dma_pool_alloc(
  4238. ioc->pcie_sgl_dma_pool, GFP_KERNEL,
  4239. &ioc->pcie_sg_lookup[i].pcie_sgl_dma);
  4240. if (!ioc->pcie_sg_lookup[i].pcie_sgl) {
  4241. pr_info(MPT3SAS_FMT
  4242. "PCIe SGL pool: dma_pool_alloc failed\n",
  4243. ioc->name);
  4244. goto out;
  4245. }
  4246. for (j = 0; j < ioc->chains_per_prp_buffer; j++) {
  4247. ct = &ioc->chain_lookup[i].chains_per_smid[j];
  4248. ct->chain_buffer =
  4249. ioc->pcie_sg_lookup[i].pcie_sgl +
  4250. (j * ioc->chain_segment_sz);
  4251. ct->chain_buffer_dma =
  4252. ioc->pcie_sg_lookup[i].pcie_sgl_dma +
  4253. (j * ioc->chain_segment_sz);
  4254. }
  4255. }
  4256. dinitprintk(ioc, pr_info(MPT3SAS_FMT "PCIe sgl pool depth(%d), "
  4257. "element_size(%d), pool_size(%d kB)\n", ioc->name,
  4258. ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
  4259. dinitprintk(ioc, pr_info(MPT3SAS_FMT "Number of chains can "
  4260. "fit in a PRP page(%d)\n", ioc->name,
  4261. ioc->chains_per_prp_buffer));
  4262. total_sz += sz * ioc->scsiio_depth;
  4263. }
  4264. ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
  4265. ioc->chain_segment_sz, 16, 0);
  4266. if (!ioc->chain_dma_pool) {
  4267. pr_err(MPT3SAS_FMT "chain_dma_pool: dma_pool_create failed\n",
  4268. ioc->name);
  4269. goto out;
  4270. }
  4271. for (i = 0; i < ioc->scsiio_depth; i++) {
  4272. for (j = ioc->chains_per_prp_buffer;
  4273. j < ioc->chains_needed_per_io; j++) {
  4274. ct = &ioc->chain_lookup[i].chains_per_smid[j];
  4275. ct->chain_buffer = dma_pool_alloc(
  4276. ioc->chain_dma_pool, GFP_KERNEL,
  4277. &ct->chain_buffer_dma);
  4278. if (!ct->chain_buffer) {
  4279. pr_err(MPT3SAS_FMT "chain_lookup: "
  4280. " pci_pool_alloc failed\n", ioc->name);
  4281. _base_release_memory_pools(ioc);
  4282. goto out;
  4283. }
  4284. }
  4285. total_sz += ioc->chain_segment_sz;
  4286. }
  4287. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4288. "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
  4289. ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
  4290. ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
  4291. /* sense buffers, 4 byte align */
  4292. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  4293. ioc->sense_dma_pool = dma_pool_create("sense pool", &ioc->pdev->dev, sz,
  4294. 4, 0);
  4295. if (!ioc->sense_dma_pool) {
  4296. pr_err(MPT3SAS_FMT "sense pool: dma_pool_create failed\n",
  4297. ioc->name);
  4298. goto out;
  4299. }
  4300. ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
  4301. &ioc->sense_dma);
  4302. if (!ioc->sense) {
  4303. pr_err(MPT3SAS_FMT "sense pool: dma_pool_alloc failed\n",
  4304. ioc->name);
  4305. goto out;
  4306. }
  4307. /* sense buffer requires to be in same 4 gb region.
  4308. * Below function will check the same.
  4309. * In case of failure, new pci pool will be created with updated
  4310. * alignment. Older allocation and pool will be destroyed.
  4311. * Alignment will be used such a way that next allocation if
  4312. * success, will always meet same 4gb region requirement.
  4313. * Actual requirement is not alignment, but we need start and end of
  4314. * DMA address must have same upper 32 bit address.
  4315. */
  4316. if (!is_MSB_are_same((long)ioc->sense, sz)) {
  4317. //Release Sense pool & Reallocate
  4318. dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  4319. dma_pool_destroy(ioc->sense_dma_pool);
  4320. ioc->sense = NULL;
  4321. ioc->sense_dma_pool =
  4322. dma_pool_create("sense pool", &ioc->pdev->dev, sz,
  4323. roundup_pow_of_two(sz), 0);
  4324. if (!ioc->sense_dma_pool) {
  4325. pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
  4326. ioc->name);
  4327. goto out;
  4328. }
  4329. ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
  4330. &ioc->sense_dma);
  4331. if (!ioc->sense) {
  4332. pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
  4333. ioc->name);
  4334. goto out;
  4335. }
  4336. }
  4337. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4338. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  4339. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  4340. SCSI_SENSE_BUFFERSIZE, sz/1024));
  4341. dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
  4342. ioc->name, (unsigned long long)ioc->sense_dma));
  4343. total_sz += sz;
  4344. /* reply pool, 4 byte align */
  4345. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  4346. ioc->reply_dma_pool = dma_pool_create("reply pool", &ioc->pdev->dev, sz,
  4347. 4, 0);
  4348. if (!ioc->reply_dma_pool) {
  4349. pr_err(MPT3SAS_FMT "reply pool: dma_pool_create failed\n",
  4350. ioc->name);
  4351. goto out;
  4352. }
  4353. ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
  4354. &ioc->reply_dma);
  4355. if (!ioc->reply) {
  4356. pr_err(MPT3SAS_FMT "reply pool: dma_pool_alloc failed\n",
  4357. ioc->name);
  4358. goto out;
  4359. }
  4360. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  4361. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  4362. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4363. "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  4364. ioc->name, ioc->reply,
  4365. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  4366. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
  4367. ioc->name, (unsigned long long)ioc->reply_dma));
  4368. total_sz += sz;
  4369. /* reply free queue, 16 byte align */
  4370. sz = ioc->reply_free_queue_depth * 4;
  4371. ioc->reply_free_dma_pool = dma_pool_create("reply_free pool",
  4372. &ioc->pdev->dev, sz, 16, 0);
  4373. if (!ioc->reply_free_dma_pool) {
  4374. pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_create failed\n",
  4375. ioc->name);
  4376. goto out;
  4377. }
  4378. ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool, GFP_KERNEL,
  4379. &ioc->reply_free_dma);
  4380. if (!ioc->reply_free) {
  4381. pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_alloc failed\n",
  4382. ioc->name);
  4383. goto out;
  4384. }
  4385. memset(ioc->reply_free, 0, sz);
  4386. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
  4387. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  4388. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  4389. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4390. "reply_free_dma (0x%llx)\n",
  4391. ioc->name, (unsigned long long)ioc->reply_free_dma));
  4392. total_sz += sz;
  4393. if (ioc->rdpq_array_enable) {
  4394. reply_post_free_array_sz = ioc->reply_queue_count *
  4395. sizeof(Mpi2IOCInitRDPQArrayEntry);
  4396. ioc->reply_post_free_array_dma_pool =
  4397. dma_pool_create("reply_post_free_array pool",
  4398. &ioc->pdev->dev, reply_post_free_array_sz, 16, 0);
  4399. if (!ioc->reply_post_free_array_dma_pool) {
  4400. dinitprintk(ioc,
  4401. pr_info(MPT3SAS_FMT "reply_post_free_array pool: "
  4402. "dma_pool_create failed\n", ioc->name));
  4403. goto out;
  4404. }
  4405. ioc->reply_post_free_array =
  4406. dma_pool_alloc(ioc->reply_post_free_array_dma_pool,
  4407. GFP_KERNEL, &ioc->reply_post_free_array_dma);
  4408. if (!ioc->reply_post_free_array) {
  4409. dinitprintk(ioc,
  4410. pr_info(MPT3SAS_FMT "reply_post_free_array pool: "
  4411. "dma_pool_alloc failed\n", ioc->name));
  4412. goto out;
  4413. }
  4414. }
  4415. ioc->config_page_sz = 512;
  4416. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  4417. ioc->config_page_sz, &ioc->config_page_dma);
  4418. if (!ioc->config_page) {
  4419. pr_err(MPT3SAS_FMT
  4420. "config page: dma_pool_alloc failed\n",
  4421. ioc->name);
  4422. goto out;
  4423. }
  4424. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  4425. "config page(0x%p): size(%d)\n",
  4426. ioc->name, ioc->config_page, ioc->config_page_sz));
  4427. dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
  4428. ioc->name, (unsigned long long)ioc->config_page_dma));
  4429. total_sz += ioc->config_page_sz;
  4430. pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
  4431. ioc->name, total_sz/1024);
  4432. pr_info(MPT3SAS_FMT
  4433. "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
  4434. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  4435. pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
  4436. ioc->name, ioc->shost->sg_tablesize);
  4437. return 0;
  4438. out:
  4439. return -ENOMEM;
  4440. }
  4441. /**
  4442. * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
  4443. * @ioc: Pointer to MPT_ADAPTER structure
  4444. * @cooked: Request raw or cooked IOC state
  4445. *
  4446. * Return: all IOC Doorbell register bits if cooked==0, else just the
  4447. * Doorbell bits in MPI_IOC_STATE_MASK.
  4448. */
  4449. u32
  4450. mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
  4451. {
  4452. u32 s, sc;
  4453. s = readl(&ioc->chip->Doorbell);
  4454. sc = s & MPI2_IOC_STATE_MASK;
  4455. return cooked ? sc : s;
  4456. }
  4457. /**
  4458. * _base_wait_on_iocstate - waiting on a particular ioc state
  4459. * @ioc: ?
  4460. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  4461. * @timeout: timeout in second
  4462. *
  4463. * Return: 0 for success, non-zero for failure.
  4464. */
  4465. static int
  4466. _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
  4467. {
  4468. u32 count, cntdn;
  4469. u32 current_state;
  4470. count = 0;
  4471. cntdn = 1000 * timeout;
  4472. do {
  4473. current_state = mpt3sas_base_get_iocstate(ioc, 1);
  4474. if (current_state == ioc_state)
  4475. return 0;
  4476. if (count && current_state == MPI2_IOC_STATE_FAULT)
  4477. break;
  4478. usleep_range(1000, 1500);
  4479. count++;
  4480. } while (--cntdn);
  4481. return current_state;
  4482. }
  4483. /**
  4484. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  4485. * a write to the doorbell)
  4486. * @ioc: per adapter object
  4487. *
  4488. * Return: 0 for success, non-zero for failure.
  4489. *
  4490. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  4491. */
  4492. static int
  4493. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
  4494. static int
  4495. _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
  4496. {
  4497. u32 cntdn, count;
  4498. u32 int_status;
  4499. count = 0;
  4500. cntdn = 1000 * timeout;
  4501. do {
  4502. int_status = readl(&ioc->chip->HostInterruptStatus);
  4503. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  4504. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  4505. "%s: successful count(%d), timeout(%d)\n",
  4506. ioc->name, __func__, count, timeout));
  4507. return 0;
  4508. }
  4509. usleep_range(1000, 1500);
  4510. count++;
  4511. } while (--cntdn);
  4512. pr_err(MPT3SAS_FMT
  4513. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  4514. ioc->name, __func__, count, int_status);
  4515. return -EFAULT;
  4516. }
  4517. static int
  4518. _base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
  4519. {
  4520. u32 cntdn, count;
  4521. u32 int_status;
  4522. count = 0;
  4523. cntdn = 2000 * timeout;
  4524. do {
  4525. int_status = readl(&ioc->chip->HostInterruptStatus);
  4526. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  4527. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  4528. "%s: successful count(%d), timeout(%d)\n",
  4529. ioc->name, __func__, count, timeout));
  4530. return 0;
  4531. }
  4532. udelay(500);
  4533. count++;
  4534. } while (--cntdn);
  4535. pr_err(MPT3SAS_FMT
  4536. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  4537. ioc->name, __func__, count, int_status);
  4538. return -EFAULT;
  4539. }
  4540. /**
  4541. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  4542. * @ioc: per adapter object
  4543. * @timeout: timeout in second
  4544. *
  4545. * Return: 0 for success, non-zero for failure.
  4546. *
  4547. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  4548. * doorbell.
  4549. */
  4550. static int
  4551. _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
  4552. {
  4553. u32 cntdn, count;
  4554. u32 int_status;
  4555. u32 doorbell;
  4556. count = 0;
  4557. cntdn = 1000 * timeout;
  4558. do {
  4559. int_status = readl(&ioc->chip->HostInterruptStatus);
  4560. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  4561. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  4562. "%s: successful count(%d), timeout(%d)\n",
  4563. ioc->name, __func__, count, timeout));
  4564. return 0;
  4565. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  4566. doorbell = readl(&ioc->chip->Doorbell);
  4567. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  4568. MPI2_IOC_STATE_FAULT) {
  4569. mpt3sas_base_fault_info(ioc , doorbell);
  4570. return -EFAULT;
  4571. }
  4572. } else if (int_status == 0xFFFFFFFF)
  4573. goto out;
  4574. usleep_range(1000, 1500);
  4575. count++;
  4576. } while (--cntdn);
  4577. out:
  4578. pr_err(MPT3SAS_FMT
  4579. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  4580. ioc->name, __func__, count, int_status);
  4581. return -EFAULT;
  4582. }
  4583. /**
  4584. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  4585. * @ioc: per adapter object
  4586. * @timeout: timeout in second
  4587. *
  4588. * Return: 0 for success, non-zero for failure.
  4589. */
  4590. static int
  4591. _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
  4592. {
  4593. u32 cntdn, count;
  4594. u32 doorbell_reg;
  4595. count = 0;
  4596. cntdn = 1000 * timeout;
  4597. do {
  4598. doorbell_reg = readl(&ioc->chip->Doorbell);
  4599. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  4600. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  4601. "%s: successful count(%d), timeout(%d)\n",
  4602. ioc->name, __func__, count, timeout));
  4603. return 0;
  4604. }
  4605. usleep_range(1000, 1500);
  4606. count++;
  4607. } while (--cntdn);
  4608. pr_err(MPT3SAS_FMT
  4609. "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
  4610. ioc->name, __func__, count, doorbell_reg);
  4611. return -EFAULT;
  4612. }
  4613. /**
  4614. * _base_send_ioc_reset - send doorbell reset
  4615. * @ioc: per adapter object
  4616. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  4617. * @timeout: timeout in second
  4618. *
  4619. * Return: 0 for success, non-zero for failure.
  4620. */
  4621. static int
  4622. _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
  4623. {
  4624. u32 ioc_state;
  4625. int r = 0;
  4626. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  4627. pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
  4628. ioc->name, __func__);
  4629. return -EFAULT;
  4630. }
  4631. if (!(ioc->facts.IOCCapabilities &
  4632. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  4633. return -EFAULT;
  4634. pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
  4635. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  4636. &ioc->chip->Doorbell);
  4637. if ((_base_wait_for_doorbell_ack(ioc, 15))) {
  4638. r = -EFAULT;
  4639. goto out;
  4640. }
  4641. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
  4642. if (ioc_state) {
  4643. pr_err(MPT3SAS_FMT
  4644. "%s: failed going to ready state (ioc_state=0x%x)\n",
  4645. ioc->name, __func__, ioc_state);
  4646. r = -EFAULT;
  4647. goto out;
  4648. }
  4649. out:
  4650. pr_info(MPT3SAS_FMT "message unit reset: %s\n",
  4651. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  4652. return r;
  4653. }
  4654. /**
  4655. * _base_handshake_req_reply_wait - send request thru doorbell interface
  4656. * @ioc: per adapter object
  4657. * @request_bytes: request length
  4658. * @request: pointer having request payload
  4659. * @reply_bytes: reply length
  4660. * @reply: pointer to reply payload
  4661. * @timeout: timeout in second
  4662. *
  4663. * Return: 0 for success, non-zero for failure.
  4664. */
  4665. static int
  4666. _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
  4667. u32 *request, int reply_bytes, u16 *reply, int timeout)
  4668. {
  4669. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  4670. int i;
  4671. u8 failed;
  4672. __le32 *mfp;
  4673. /* make sure doorbell is not in use */
  4674. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  4675. pr_err(MPT3SAS_FMT
  4676. "doorbell is in use (line=%d)\n",
  4677. ioc->name, __LINE__);
  4678. return -EFAULT;
  4679. }
  4680. /* clear pending doorbell interrupts from previous state changes */
  4681. if (readl(&ioc->chip->HostInterruptStatus) &
  4682. MPI2_HIS_IOC2SYS_DB_STATUS)
  4683. writel(0, &ioc->chip->HostInterruptStatus);
  4684. /* send message to ioc */
  4685. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  4686. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  4687. &ioc->chip->Doorbell);
  4688. if ((_base_spin_on_doorbell_int(ioc, 5))) {
  4689. pr_err(MPT3SAS_FMT
  4690. "doorbell handshake int failed (line=%d)\n",
  4691. ioc->name, __LINE__);
  4692. return -EFAULT;
  4693. }
  4694. writel(0, &ioc->chip->HostInterruptStatus);
  4695. if ((_base_wait_for_doorbell_ack(ioc, 5))) {
  4696. pr_err(MPT3SAS_FMT
  4697. "doorbell handshake ack failed (line=%d)\n",
  4698. ioc->name, __LINE__);
  4699. return -EFAULT;
  4700. }
  4701. /* send message 32-bits at a time */
  4702. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  4703. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  4704. if ((_base_wait_for_doorbell_ack(ioc, 5)))
  4705. failed = 1;
  4706. }
  4707. if (failed) {
  4708. pr_err(MPT3SAS_FMT
  4709. "doorbell handshake sending request failed (line=%d)\n",
  4710. ioc->name, __LINE__);
  4711. return -EFAULT;
  4712. }
  4713. /* now wait for the reply */
  4714. if ((_base_wait_for_doorbell_int(ioc, timeout))) {
  4715. pr_err(MPT3SAS_FMT
  4716. "doorbell handshake int failed (line=%d)\n",
  4717. ioc->name, __LINE__);
  4718. return -EFAULT;
  4719. }
  4720. /* read the first two 16-bits, it gives the total length of the reply */
  4721. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  4722. & MPI2_DOORBELL_DATA_MASK);
  4723. writel(0, &ioc->chip->HostInterruptStatus);
  4724. if ((_base_wait_for_doorbell_int(ioc, 5))) {
  4725. pr_err(MPT3SAS_FMT
  4726. "doorbell handshake int failed (line=%d)\n",
  4727. ioc->name, __LINE__);
  4728. return -EFAULT;
  4729. }
  4730. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  4731. & MPI2_DOORBELL_DATA_MASK);
  4732. writel(0, &ioc->chip->HostInterruptStatus);
  4733. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  4734. if ((_base_wait_for_doorbell_int(ioc, 5))) {
  4735. pr_err(MPT3SAS_FMT
  4736. "doorbell handshake int failed (line=%d)\n",
  4737. ioc->name, __LINE__);
  4738. return -EFAULT;
  4739. }
  4740. if (i >= reply_bytes/2) /* overflow case */
  4741. readl(&ioc->chip->Doorbell);
  4742. else
  4743. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  4744. & MPI2_DOORBELL_DATA_MASK);
  4745. writel(0, &ioc->chip->HostInterruptStatus);
  4746. }
  4747. _base_wait_for_doorbell_int(ioc, 5);
  4748. if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
  4749. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  4750. "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
  4751. }
  4752. writel(0, &ioc->chip->HostInterruptStatus);
  4753. if (ioc->logging_level & MPT_DEBUG_INIT) {
  4754. mfp = (__le32 *)reply;
  4755. pr_info("\toffset:data\n");
  4756. for (i = 0; i < reply_bytes/4; i++)
  4757. pr_info("\t[0x%02x]:%08x\n", i*4,
  4758. le32_to_cpu(mfp[i]));
  4759. }
  4760. return 0;
  4761. }
  4762. /**
  4763. * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
  4764. * @ioc: per adapter object
  4765. * @mpi_reply: the reply payload from FW
  4766. * @mpi_request: the request payload sent to FW
  4767. *
  4768. * The SAS IO Unit Control Request message allows the host to perform low-level
  4769. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  4770. * to obtain the IOC assigned device handles for a device if it has other
  4771. * identifying information about the device, in addition allows the host to
  4772. * remove IOC resources associated with the device.
  4773. *
  4774. * Return: 0 for success, non-zero for failure.
  4775. */
  4776. int
  4777. mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
  4778. Mpi2SasIoUnitControlReply_t *mpi_reply,
  4779. Mpi2SasIoUnitControlRequest_t *mpi_request)
  4780. {
  4781. u16 smid;
  4782. u32 ioc_state;
  4783. u8 issue_reset = 0;
  4784. int rc;
  4785. void *request;
  4786. u16 wait_state_count;
  4787. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4788. __func__));
  4789. mutex_lock(&ioc->base_cmds.mutex);
  4790. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  4791. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  4792. ioc->name, __func__);
  4793. rc = -EAGAIN;
  4794. goto out;
  4795. }
  4796. wait_state_count = 0;
  4797. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  4798. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  4799. if (wait_state_count++ == 10) {
  4800. pr_err(MPT3SAS_FMT
  4801. "%s: failed due to ioc not operational\n",
  4802. ioc->name, __func__);
  4803. rc = -EFAULT;
  4804. goto out;
  4805. }
  4806. ssleep(1);
  4807. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  4808. pr_info(MPT3SAS_FMT
  4809. "%s: waiting for operational state(count=%d)\n",
  4810. ioc->name, __func__, wait_state_count);
  4811. }
  4812. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  4813. if (!smid) {
  4814. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  4815. ioc->name, __func__);
  4816. rc = -EAGAIN;
  4817. goto out;
  4818. }
  4819. rc = 0;
  4820. ioc->base_cmds.status = MPT3_CMD_PENDING;
  4821. request = mpt3sas_base_get_msg_frame(ioc, smid);
  4822. ioc->base_cmds.smid = smid;
  4823. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  4824. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  4825. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  4826. ioc->ioc_link_reset_in_progress = 1;
  4827. init_completion(&ioc->base_cmds.done);
  4828. mpt3sas_base_put_smid_default(ioc, smid);
  4829. wait_for_completion_timeout(&ioc->base_cmds.done,
  4830. msecs_to_jiffies(10000));
  4831. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  4832. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  4833. ioc->ioc_link_reset_in_progress)
  4834. ioc->ioc_link_reset_in_progress = 0;
  4835. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  4836. issue_reset =
  4837. mpt3sas_base_check_cmd_timeout(ioc,
  4838. ioc->base_cmds.status, mpi_request,
  4839. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  4840. goto issue_host_reset;
  4841. }
  4842. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  4843. memcpy(mpi_reply, ioc->base_cmds.reply,
  4844. sizeof(Mpi2SasIoUnitControlReply_t));
  4845. else
  4846. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  4847. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4848. goto out;
  4849. issue_host_reset:
  4850. if (issue_reset)
  4851. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  4852. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4853. rc = -EFAULT;
  4854. out:
  4855. mutex_unlock(&ioc->base_cmds.mutex);
  4856. return rc;
  4857. }
  4858. /**
  4859. * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
  4860. * @ioc: per adapter object
  4861. * @mpi_reply: the reply payload from FW
  4862. * @mpi_request: the request payload sent to FW
  4863. *
  4864. * The SCSI Enclosure Processor request message causes the IOC to
  4865. * communicate with SES devices to control LED status signals.
  4866. *
  4867. * Return: 0 for success, non-zero for failure.
  4868. */
  4869. int
  4870. mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
  4871. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  4872. {
  4873. u16 smid;
  4874. u32 ioc_state;
  4875. u8 issue_reset = 0;
  4876. int rc;
  4877. void *request;
  4878. u16 wait_state_count;
  4879. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4880. __func__));
  4881. mutex_lock(&ioc->base_cmds.mutex);
  4882. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  4883. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  4884. ioc->name, __func__);
  4885. rc = -EAGAIN;
  4886. goto out;
  4887. }
  4888. wait_state_count = 0;
  4889. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  4890. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  4891. if (wait_state_count++ == 10) {
  4892. pr_err(MPT3SAS_FMT
  4893. "%s: failed due to ioc not operational\n",
  4894. ioc->name, __func__);
  4895. rc = -EFAULT;
  4896. goto out;
  4897. }
  4898. ssleep(1);
  4899. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  4900. pr_info(MPT3SAS_FMT
  4901. "%s: waiting for operational state(count=%d)\n",
  4902. ioc->name,
  4903. __func__, wait_state_count);
  4904. }
  4905. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  4906. if (!smid) {
  4907. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  4908. ioc->name, __func__);
  4909. rc = -EAGAIN;
  4910. goto out;
  4911. }
  4912. rc = 0;
  4913. ioc->base_cmds.status = MPT3_CMD_PENDING;
  4914. request = mpt3sas_base_get_msg_frame(ioc, smid);
  4915. ioc->base_cmds.smid = smid;
  4916. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  4917. init_completion(&ioc->base_cmds.done);
  4918. mpt3sas_base_put_smid_default(ioc, smid);
  4919. wait_for_completion_timeout(&ioc->base_cmds.done,
  4920. msecs_to_jiffies(10000));
  4921. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  4922. issue_reset =
  4923. mpt3sas_base_check_cmd_timeout(ioc,
  4924. ioc->base_cmds.status, mpi_request,
  4925. sizeof(Mpi2SepRequest_t)/4);
  4926. goto issue_host_reset;
  4927. }
  4928. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  4929. memcpy(mpi_reply, ioc->base_cmds.reply,
  4930. sizeof(Mpi2SepReply_t));
  4931. else
  4932. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  4933. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4934. goto out;
  4935. issue_host_reset:
  4936. if (issue_reset)
  4937. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  4938. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4939. rc = -EFAULT;
  4940. out:
  4941. mutex_unlock(&ioc->base_cmds.mutex);
  4942. return rc;
  4943. }
  4944. /**
  4945. * _base_get_port_facts - obtain port facts reply and save in ioc
  4946. * @ioc: per adapter object
  4947. * @port: ?
  4948. *
  4949. * Return: 0 for success, non-zero for failure.
  4950. */
  4951. static int
  4952. _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
  4953. {
  4954. Mpi2PortFactsRequest_t mpi_request;
  4955. Mpi2PortFactsReply_t mpi_reply;
  4956. struct mpt3sas_port_facts *pfacts;
  4957. int mpi_reply_sz, mpi_request_sz, r;
  4958. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4959. __func__));
  4960. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  4961. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  4962. memset(&mpi_request, 0, mpi_request_sz);
  4963. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  4964. mpi_request.PortNumber = port;
  4965. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  4966. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
  4967. if (r != 0) {
  4968. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  4969. ioc->name, __func__, r);
  4970. return r;
  4971. }
  4972. pfacts = &ioc->pfacts[port];
  4973. memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
  4974. pfacts->PortNumber = mpi_reply.PortNumber;
  4975. pfacts->VP_ID = mpi_reply.VP_ID;
  4976. pfacts->VF_ID = mpi_reply.VF_ID;
  4977. pfacts->MaxPostedCmdBuffers =
  4978. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  4979. return 0;
  4980. }
  4981. /**
  4982. * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
  4983. * @ioc: per adapter object
  4984. * @timeout:
  4985. *
  4986. * Return: 0 for success, non-zero for failure.
  4987. */
  4988. static int
  4989. _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
  4990. {
  4991. u32 ioc_state;
  4992. int rc;
  4993. dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
  4994. __func__));
  4995. if (ioc->pci_error_recovery) {
  4996. dfailprintk(ioc, printk(MPT3SAS_FMT
  4997. "%s: host in pci error recovery\n", ioc->name, __func__));
  4998. return -EFAULT;
  4999. }
  5000. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  5001. dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
  5002. ioc->name, __func__, ioc_state));
  5003. if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
  5004. (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  5005. return 0;
  5006. if (ioc_state & MPI2_DOORBELL_USED) {
  5007. dhsprintk(ioc, printk(MPT3SAS_FMT
  5008. "unexpected doorbell active!\n", ioc->name));
  5009. goto issue_diag_reset;
  5010. }
  5011. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  5012. mpt3sas_base_fault_info(ioc, ioc_state &
  5013. MPI2_DOORBELL_DATA_MASK);
  5014. goto issue_diag_reset;
  5015. }
  5016. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
  5017. if (ioc_state) {
  5018. dfailprintk(ioc, printk(MPT3SAS_FMT
  5019. "%s: failed going to ready state (ioc_state=0x%x)\n",
  5020. ioc->name, __func__, ioc_state));
  5021. return -EFAULT;
  5022. }
  5023. issue_diag_reset:
  5024. rc = _base_diag_reset(ioc);
  5025. return rc;
  5026. }
  5027. /**
  5028. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  5029. * @ioc: per adapter object
  5030. *
  5031. * Return: 0 for success, non-zero for failure.
  5032. */
  5033. static int
  5034. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
  5035. {
  5036. Mpi2IOCFactsRequest_t mpi_request;
  5037. Mpi2IOCFactsReply_t mpi_reply;
  5038. struct mpt3sas_facts *facts;
  5039. int mpi_reply_sz, mpi_request_sz, r;
  5040. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5041. __func__));
  5042. r = _base_wait_for_iocstate(ioc, 10);
  5043. if (r) {
  5044. dfailprintk(ioc, printk(MPT3SAS_FMT
  5045. "%s: failed getting to correct state\n",
  5046. ioc->name, __func__));
  5047. return r;
  5048. }
  5049. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  5050. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  5051. memset(&mpi_request, 0, mpi_request_sz);
  5052. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  5053. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  5054. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
  5055. if (r != 0) {
  5056. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  5057. ioc->name, __func__, r);
  5058. return r;
  5059. }
  5060. facts = &ioc->facts;
  5061. memset(facts, 0, sizeof(struct mpt3sas_facts));
  5062. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  5063. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  5064. facts->VP_ID = mpi_reply.VP_ID;
  5065. facts->VF_ID = mpi_reply.VF_ID;
  5066. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  5067. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  5068. facts->WhoInit = mpi_reply.WhoInit;
  5069. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  5070. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  5071. if (ioc->msix_enable && (facts->MaxMSIxVectors <=
  5072. MAX_COMBINED_MSIX_VECTORS(ioc->is_gen35_ioc)))
  5073. ioc->combined_reply_queue = 0;
  5074. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  5075. facts->MaxReplyDescriptorPostQueueDepth =
  5076. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  5077. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  5078. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  5079. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  5080. ioc->ir_firmware = 1;
  5081. if ((facts->IOCCapabilities &
  5082. MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
  5083. ioc->rdpq_array_capable = 1;
  5084. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  5085. facts->IOCRequestFrameSize =
  5086. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  5087. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  5088. facts->IOCMaxChainSegmentSize =
  5089. le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
  5090. }
  5091. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  5092. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  5093. ioc->shost->max_id = -1;
  5094. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  5095. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  5096. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  5097. facts->HighPriorityCredit =
  5098. le16_to_cpu(mpi_reply.HighPriorityCredit);
  5099. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  5100. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  5101. facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
  5102. /*
  5103. * Get the Page Size from IOC Facts. If it's 0, default to 4k.
  5104. */
  5105. ioc->page_size = 1 << facts->CurrentHostPageSize;
  5106. if (ioc->page_size == 1) {
  5107. pr_info(MPT3SAS_FMT "CurrentHostPageSize is 0: Setting "
  5108. "default host page size to 4k\n", ioc->name);
  5109. ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
  5110. }
  5111. dinitprintk(ioc, pr_info(MPT3SAS_FMT "CurrentHostPageSize(%d)\n",
  5112. ioc->name, facts->CurrentHostPageSize));
  5113. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  5114. "hba queue depth(%d), max chains per io(%d)\n",
  5115. ioc->name, facts->RequestCredit,
  5116. facts->MaxChainDepth));
  5117. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  5118. "request frame size(%d), reply frame size(%d)\n", ioc->name,
  5119. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  5120. return 0;
  5121. }
  5122. /**
  5123. * _base_send_ioc_init - send ioc_init to firmware
  5124. * @ioc: per adapter object
  5125. *
  5126. * Return: 0 for success, non-zero for failure.
  5127. */
  5128. static int
  5129. _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
  5130. {
  5131. Mpi2IOCInitRequest_t mpi_request;
  5132. Mpi2IOCInitReply_t mpi_reply;
  5133. int i, r = 0;
  5134. ktime_t current_time;
  5135. u16 ioc_status;
  5136. u32 reply_post_free_array_sz = 0;
  5137. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5138. __func__));
  5139. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  5140. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  5141. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  5142. mpi_request.VF_ID = 0; /* TODO */
  5143. mpi_request.VP_ID = 0;
  5144. mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
  5145. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  5146. mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
  5147. if (_base_is_controller_msix_enabled(ioc))
  5148. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  5149. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  5150. mpi_request.ReplyDescriptorPostQueueDepth =
  5151. cpu_to_le16(ioc->reply_post_queue_depth);
  5152. mpi_request.ReplyFreeQueueDepth =
  5153. cpu_to_le16(ioc->reply_free_queue_depth);
  5154. mpi_request.SenseBufferAddressHigh =
  5155. cpu_to_le32((u64)ioc->sense_dma >> 32);
  5156. mpi_request.SystemReplyAddressHigh =
  5157. cpu_to_le32((u64)ioc->reply_dma >> 32);
  5158. mpi_request.SystemRequestFrameBaseAddress =
  5159. cpu_to_le64((u64)ioc->request_dma);
  5160. mpi_request.ReplyFreeQueueAddress =
  5161. cpu_to_le64((u64)ioc->reply_free_dma);
  5162. if (ioc->rdpq_array_enable) {
  5163. reply_post_free_array_sz = ioc->reply_queue_count *
  5164. sizeof(Mpi2IOCInitRDPQArrayEntry);
  5165. memset(ioc->reply_post_free_array, 0, reply_post_free_array_sz);
  5166. for (i = 0; i < ioc->reply_queue_count; i++)
  5167. ioc->reply_post_free_array[i].RDPQBaseAddress =
  5168. cpu_to_le64(
  5169. (u64)ioc->reply_post[i].reply_post_free_dma);
  5170. mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
  5171. mpi_request.ReplyDescriptorPostQueueAddress =
  5172. cpu_to_le64((u64)ioc->reply_post_free_array_dma);
  5173. } else {
  5174. mpi_request.ReplyDescriptorPostQueueAddress =
  5175. cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
  5176. }
  5177. /* This time stamp specifies number of milliseconds
  5178. * since epoch ~ midnight January 1, 1970.
  5179. */
  5180. current_time = ktime_get_real();
  5181. mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
  5182. if (ioc->logging_level & MPT_DEBUG_INIT) {
  5183. __le32 *mfp;
  5184. int i;
  5185. mfp = (__le32 *)&mpi_request;
  5186. pr_info("\toffset:data\n");
  5187. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  5188. pr_info("\t[0x%02x]:%08x\n", i*4,
  5189. le32_to_cpu(mfp[i]));
  5190. }
  5191. r = _base_handshake_req_reply_wait(ioc,
  5192. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  5193. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 30);
  5194. if (r != 0) {
  5195. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  5196. ioc->name, __func__, r);
  5197. return r;
  5198. }
  5199. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  5200. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  5201. mpi_reply.IOCLogInfo) {
  5202. pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
  5203. r = -EIO;
  5204. }
  5205. return r;
  5206. }
  5207. /**
  5208. * mpt3sas_port_enable_done - command completion routine for port enable
  5209. * @ioc: per adapter object
  5210. * @smid: system request message index
  5211. * @msix_index: MSIX table index supplied by the OS
  5212. * @reply: reply message frame(lower 32bit addr)
  5213. *
  5214. * Return: 1 meaning mf should be freed from _base_interrupt
  5215. * 0 means the mf is freed from this function.
  5216. */
  5217. u8
  5218. mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  5219. u32 reply)
  5220. {
  5221. MPI2DefaultReply_t *mpi_reply;
  5222. u16 ioc_status;
  5223. if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
  5224. return 1;
  5225. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  5226. if (!mpi_reply)
  5227. return 1;
  5228. if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
  5229. return 1;
  5230. ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
  5231. ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
  5232. ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
  5233. memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  5234. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  5235. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  5236. ioc->port_enable_failed = 1;
  5237. if (ioc->is_driver_loading) {
  5238. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  5239. mpt3sas_port_enable_complete(ioc);
  5240. return 1;
  5241. } else {
  5242. ioc->start_scan_failed = ioc_status;
  5243. ioc->start_scan = 0;
  5244. return 1;
  5245. }
  5246. }
  5247. complete(&ioc->port_enable_cmds.done);
  5248. return 1;
  5249. }
  5250. /**
  5251. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  5252. * @ioc: per adapter object
  5253. *
  5254. * Return: 0 for success, non-zero for failure.
  5255. */
  5256. static int
  5257. _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
  5258. {
  5259. Mpi2PortEnableRequest_t *mpi_request;
  5260. Mpi2PortEnableReply_t *mpi_reply;
  5261. int r = 0;
  5262. u16 smid;
  5263. u16 ioc_status;
  5264. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  5265. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  5266. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  5267. ioc->name, __func__);
  5268. return -EAGAIN;
  5269. }
  5270. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  5271. if (!smid) {
  5272. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  5273. ioc->name, __func__);
  5274. return -EAGAIN;
  5275. }
  5276. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  5277. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  5278. ioc->port_enable_cmds.smid = smid;
  5279. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  5280. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  5281. init_completion(&ioc->port_enable_cmds.done);
  5282. mpt3sas_base_put_smid_default(ioc, smid);
  5283. wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
  5284. if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
  5285. pr_err(MPT3SAS_FMT "%s: timeout\n",
  5286. ioc->name, __func__);
  5287. _debug_dump_mf(mpi_request,
  5288. sizeof(Mpi2PortEnableRequest_t)/4);
  5289. if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
  5290. r = -EFAULT;
  5291. else
  5292. r = -ETIME;
  5293. goto out;
  5294. }
  5295. mpi_reply = ioc->port_enable_cmds.reply;
  5296. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  5297. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5298. pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
  5299. ioc->name, __func__, ioc_status);
  5300. r = -EFAULT;
  5301. goto out;
  5302. }
  5303. out:
  5304. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  5305. pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
  5306. "SUCCESS" : "FAILED"));
  5307. return r;
  5308. }
  5309. /**
  5310. * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
  5311. * @ioc: per adapter object
  5312. *
  5313. * Return: 0 for success, non-zero for failure.
  5314. */
  5315. int
  5316. mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
  5317. {
  5318. Mpi2PortEnableRequest_t *mpi_request;
  5319. u16 smid;
  5320. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  5321. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  5322. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  5323. ioc->name, __func__);
  5324. return -EAGAIN;
  5325. }
  5326. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  5327. if (!smid) {
  5328. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  5329. ioc->name, __func__);
  5330. return -EAGAIN;
  5331. }
  5332. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  5333. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  5334. ioc->port_enable_cmds.smid = smid;
  5335. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  5336. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  5337. mpt3sas_base_put_smid_default(ioc, smid);
  5338. return 0;
  5339. }
  5340. /**
  5341. * _base_determine_wait_on_discovery - desposition
  5342. * @ioc: per adapter object
  5343. *
  5344. * Decide whether to wait on discovery to complete. Used to either
  5345. * locate boot device, or report volumes ahead of physical devices.
  5346. *
  5347. * Return: 1 for wait, 0 for don't wait.
  5348. */
  5349. static int
  5350. _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
  5351. {
  5352. /* We wait for discovery to complete if IR firmware is loaded.
  5353. * The sas topology events arrive before PD events, so we need time to
  5354. * turn on the bit in ioc->pd_handles to indicate PD
  5355. * Also, it maybe required to report Volumes ahead of physical
  5356. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  5357. */
  5358. if (ioc->ir_firmware)
  5359. return 1;
  5360. /* if no Bios, then we don't need to wait */
  5361. if (!ioc->bios_pg3.BiosVersion)
  5362. return 0;
  5363. /* Bios is present, then we drop down here.
  5364. *
  5365. * If there any entries in the Bios Page 2, then we wait
  5366. * for discovery to complete.
  5367. */
  5368. /* Current Boot Device */
  5369. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  5370. MPI2_BIOSPAGE2_FORM_MASK) ==
  5371. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  5372. /* Request Boot Device */
  5373. (ioc->bios_pg2.ReqBootDeviceForm &
  5374. MPI2_BIOSPAGE2_FORM_MASK) ==
  5375. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  5376. /* Alternate Request Boot Device */
  5377. (ioc->bios_pg2.ReqAltBootDeviceForm &
  5378. MPI2_BIOSPAGE2_FORM_MASK) ==
  5379. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  5380. return 0;
  5381. return 1;
  5382. }
  5383. /**
  5384. * _base_unmask_events - turn on notification for this event
  5385. * @ioc: per adapter object
  5386. * @event: firmware event
  5387. *
  5388. * The mask is stored in ioc->event_masks.
  5389. */
  5390. static void
  5391. _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
  5392. {
  5393. u32 desired_event;
  5394. if (event >= 128)
  5395. return;
  5396. desired_event = (1 << (event % 32));
  5397. if (event < 32)
  5398. ioc->event_masks[0] &= ~desired_event;
  5399. else if (event < 64)
  5400. ioc->event_masks[1] &= ~desired_event;
  5401. else if (event < 96)
  5402. ioc->event_masks[2] &= ~desired_event;
  5403. else if (event < 128)
  5404. ioc->event_masks[3] &= ~desired_event;
  5405. }
  5406. /**
  5407. * _base_event_notification - send event notification
  5408. * @ioc: per adapter object
  5409. *
  5410. * Return: 0 for success, non-zero for failure.
  5411. */
  5412. static int
  5413. _base_event_notification(struct MPT3SAS_ADAPTER *ioc)
  5414. {
  5415. Mpi2EventNotificationRequest_t *mpi_request;
  5416. u16 smid;
  5417. int r = 0;
  5418. int i;
  5419. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5420. __func__));
  5421. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  5422. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  5423. ioc->name, __func__);
  5424. return -EAGAIN;
  5425. }
  5426. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  5427. if (!smid) {
  5428. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  5429. ioc->name, __func__);
  5430. return -EAGAIN;
  5431. }
  5432. ioc->base_cmds.status = MPT3_CMD_PENDING;
  5433. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  5434. ioc->base_cmds.smid = smid;
  5435. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  5436. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  5437. mpi_request->VF_ID = 0; /* TODO */
  5438. mpi_request->VP_ID = 0;
  5439. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  5440. mpi_request->EventMasks[i] =
  5441. cpu_to_le32(ioc->event_masks[i]);
  5442. init_completion(&ioc->base_cmds.done);
  5443. mpt3sas_base_put_smid_default(ioc, smid);
  5444. wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  5445. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  5446. pr_err(MPT3SAS_FMT "%s: timeout\n",
  5447. ioc->name, __func__);
  5448. _debug_dump_mf(mpi_request,
  5449. sizeof(Mpi2EventNotificationRequest_t)/4);
  5450. if (ioc->base_cmds.status & MPT3_CMD_RESET)
  5451. r = -EFAULT;
  5452. else
  5453. r = -ETIME;
  5454. } else
  5455. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
  5456. ioc->name, __func__));
  5457. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  5458. return r;
  5459. }
  5460. /**
  5461. * mpt3sas_base_validate_event_type - validating event types
  5462. * @ioc: per adapter object
  5463. * @event_type: firmware event
  5464. *
  5465. * This will turn on firmware event notification when application
  5466. * ask for that event. We don't mask events that are already enabled.
  5467. */
  5468. void
  5469. mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
  5470. {
  5471. int i, j;
  5472. u32 event_mask, desired_event;
  5473. u8 send_update_to_fw;
  5474. for (i = 0, send_update_to_fw = 0; i <
  5475. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  5476. event_mask = ~event_type[i];
  5477. desired_event = 1;
  5478. for (j = 0; j < 32; j++) {
  5479. if (!(event_mask & desired_event) &&
  5480. (ioc->event_masks[i] & desired_event)) {
  5481. ioc->event_masks[i] &= ~desired_event;
  5482. send_update_to_fw = 1;
  5483. }
  5484. desired_event = (desired_event << 1);
  5485. }
  5486. }
  5487. if (!send_update_to_fw)
  5488. return;
  5489. mutex_lock(&ioc->base_cmds.mutex);
  5490. _base_event_notification(ioc);
  5491. mutex_unlock(&ioc->base_cmds.mutex);
  5492. }
  5493. /**
  5494. * _base_diag_reset - the "big hammer" start of day reset
  5495. * @ioc: per adapter object
  5496. *
  5497. * Return: 0 for success, non-zero for failure.
  5498. */
  5499. static int
  5500. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
  5501. {
  5502. u32 host_diagnostic;
  5503. u32 ioc_state;
  5504. u32 count;
  5505. u32 hcb_size;
  5506. pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
  5507. drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
  5508. ioc->name));
  5509. count = 0;
  5510. do {
  5511. /* Write magic sequence to WriteSequence register
  5512. * Loop until in diagnostic mode
  5513. */
  5514. drsprintk(ioc, pr_info(MPT3SAS_FMT
  5515. "write magic sequence\n", ioc->name));
  5516. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  5517. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  5518. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  5519. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  5520. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  5521. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  5522. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  5523. /* wait 100 msec */
  5524. msleep(100);
  5525. if (count++ > 20)
  5526. goto out;
  5527. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  5528. drsprintk(ioc, pr_info(MPT3SAS_FMT
  5529. "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
  5530. ioc->name, count, host_diagnostic));
  5531. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  5532. hcb_size = readl(&ioc->chip->HCBSize);
  5533. drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
  5534. ioc->name));
  5535. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  5536. &ioc->chip->HostDiagnostic);
  5537. /*This delay allows the chip PCIe hardware time to finish reset tasks*/
  5538. msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
  5539. /* Approximately 300 second max wait */
  5540. for (count = 0; count < (300000000 /
  5541. MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
  5542. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  5543. if (host_diagnostic == 0xFFFFFFFF)
  5544. goto out;
  5545. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  5546. break;
  5547. msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
  5548. }
  5549. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  5550. drsprintk(ioc, pr_info(MPT3SAS_FMT
  5551. "restart the adapter assuming the HCB Address points to good F/W\n",
  5552. ioc->name));
  5553. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  5554. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  5555. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  5556. drsprintk(ioc, pr_info(MPT3SAS_FMT
  5557. "re-enable the HCDW\n", ioc->name));
  5558. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  5559. &ioc->chip->HCBSize);
  5560. }
  5561. drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
  5562. ioc->name));
  5563. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  5564. &ioc->chip->HostDiagnostic);
  5565. drsprintk(ioc, pr_info(MPT3SAS_FMT
  5566. "disable writes to the diagnostic register\n", ioc->name));
  5567. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  5568. drsprintk(ioc, pr_info(MPT3SAS_FMT
  5569. "Wait for FW to go to the READY state\n", ioc->name));
  5570. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
  5571. if (ioc_state) {
  5572. pr_err(MPT3SAS_FMT
  5573. "%s: failed going to ready state (ioc_state=0x%x)\n",
  5574. ioc->name, __func__, ioc_state);
  5575. goto out;
  5576. }
  5577. pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
  5578. return 0;
  5579. out:
  5580. pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
  5581. return -EFAULT;
  5582. }
  5583. /**
  5584. * _base_make_ioc_ready - put controller in READY state
  5585. * @ioc: per adapter object
  5586. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  5587. *
  5588. * Return: 0 for success, non-zero for failure.
  5589. */
  5590. static int
  5591. _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
  5592. {
  5593. u32 ioc_state;
  5594. int rc;
  5595. int count;
  5596. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5597. __func__));
  5598. if (ioc->pci_error_recovery)
  5599. return 0;
  5600. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  5601. dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
  5602. ioc->name, __func__, ioc_state));
  5603. /* if in RESET state, it should move to READY state shortly */
  5604. count = 0;
  5605. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
  5606. while ((ioc_state & MPI2_IOC_STATE_MASK) !=
  5607. MPI2_IOC_STATE_READY) {
  5608. if (count++ == 10) {
  5609. pr_err(MPT3SAS_FMT
  5610. "%s: failed going to ready state (ioc_state=0x%x)\n",
  5611. ioc->name, __func__, ioc_state);
  5612. return -EFAULT;
  5613. }
  5614. ssleep(1);
  5615. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  5616. }
  5617. }
  5618. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  5619. return 0;
  5620. if (ioc_state & MPI2_DOORBELL_USED) {
  5621. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  5622. "unexpected doorbell active!\n",
  5623. ioc->name));
  5624. goto issue_diag_reset;
  5625. }
  5626. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  5627. mpt3sas_base_fault_info(ioc, ioc_state &
  5628. MPI2_DOORBELL_DATA_MASK);
  5629. goto issue_diag_reset;
  5630. }
  5631. if (type == FORCE_BIG_HAMMER)
  5632. goto issue_diag_reset;
  5633. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  5634. if (!(_base_send_ioc_reset(ioc,
  5635. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
  5636. return 0;
  5637. }
  5638. issue_diag_reset:
  5639. rc = _base_diag_reset(ioc);
  5640. return rc;
  5641. }
  5642. /**
  5643. * _base_make_ioc_operational - put controller in OPERATIONAL state
  5644. * @ioc: per adapter object
  5645. *
  5646. * Return: 0 for success, non-zero for failure.
  5647. */
  5648. static int
  5649. _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
  5650. {
  5651. int r, i, index;
  5652. unsigned long flags;
  5653. u32 reply_address;
  5654. u16 smid;
  5655. struct _tr_list *delayed_tr, *delayed_tr_next;
  5656. struct _sc_list *delayed_sc, *delayed_sc_next;
  5657. struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
  5658. u8 hide_flag;
  5659. struct adapter_reply_queue *reply_q;
  5660. Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
  5661. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5662. __func__));
  5663. /* clean the delayed target reset list */
  5664. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  5665. &ioc->delayed_tr_list, list) {
  5666. list_del(&delayed_tr->list);
  5667. kfree(delayed_tr);
  5668. }
  5669. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  5670. &ioc->delayed_tr_volume_list, list) {
  5671. list_del(&delayed_tr->list);
  5672. kfree(delayed_tr);
  5673. }
  5674. list_for_each_entry_safe(delayed_sc, delayed_sc_next,
  5675. &ioc->delayed_sc_list, list) {
  5676. list_del(&delayed_sc->list);
  5677. kfree(delayed_sc);
  5678. }
  5679. list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
  5680. &ioc->delayed_event_ack_list, list) {
  5681. list_del(&delayed_event_ack->list);
  5682. kfree(delayed_event_ack);
  5683. }
  5684. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5685. /* hi-priority queue */
  5686. INIT_LIST_HEAD(&ioc->hpr_free_list);
  5687. smid = ioc->hi_priority_smid;
  5688. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  5689. ioc->hpr_lookup[i].cb_idx = 0xFF;
  5690. ioc->hpr_lookup[i].smid = smid;
  5691. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  5692. &ioc->hpr_free_list);
  5693. }
  5694. /* internal queue */
  5695. INIT_LIST_HEAD(&ioc->internal_free_list);
  5696. smid = ioc->internal_smid;
  5697. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  5698. ioc->internal_lookup[i].cb_idx = 0xFF;
  5699. ioc->internal_lookup[i].smid = smid;
  5700. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  5701. &ioc->internal_free_list);
  5702. }
  5703. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  5704. /* initialize Reply Free Queue */
  5705. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  5706. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  5707. ioc->reply_sz) {
  5708. ioc->reply_free[i] = cpu_to_le32(reply_address);
  5709. if (ioc->is_mcpu_endpoint)
  5710. _base_clone_reply_to_sys_mem(ioc,
  5711. reply_address, i);
  5712. }
  5713. /* initialize reply queues */
  5714. if (ioc->is_driver_loading)
  5715. _base_assign_reply_queues(ioc);
  5716. /* initialize Reply Post Free Queue */
  5717. index = 0;
  5718. reply_post_free_contig = ioc->reply_post[0].reply_post_free;
  5719. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  5720. /*
  5721. * If RDPQ is enabled, switch to the next allocation.
  5722. * Otherwise advance within the contiguous region.
  5723. */
  5724. if (ioc->rdpq_array_enable) {
  5725. reply_q->reply_post_free =
  5726. ioc->reply_post[index++].reply_post_free;
  5727. } else {
  5728. reply_q->reply_post_free = reply_post_free_contig;
  5729. reply_post_free_contig += ioc->reply_post_queue_depth;
  5730. }
  5731. reply_q->reply_post_host_index = 0;
  5732. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  5733. reply_q->reply_post_free[i].Words =
  5734. cpu_to_le64(ULLONG_MAX);
  5735. if (!_base_is_controller_msix_enabled(ioc))
  5736. goto skip_init_reply_post_free_queue;
  5737. }
  5738. skip_init_reply_post_free_queue:
  5739. r = _base_send_ioc_init(ioc);
  5740. if (r)
  5741. return r;
  5742. /* initialize reply free host index */
  5743. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  5744. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  5745. /* initialize reply post host index */
  5746. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  5747. if (ioc->combined_reply_queue)
  5748. writel((reply_q->msix_index & 7)<<
  5749. MPI2_RPHI_MSIX_INDEX_SHIFT,
  5750. ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
  5751. else
  5752. writel(reply_q->msix_index <<
  5753. MPI2_RPHI_MSIX_INDEX_SHIFT,
  5754. &ioc->chip->ReplyPostHostIndex);
  5755. if (!_base_is_controller_msix_enabled(ioc))
  5756. goto skip_init_reply_post_host_index;
  5757. }
  5758. skip_init_reply_post_host_index:
  5759. _base_unmask_interrupts(ioc);
  5760. if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  5761. r = _base_display_fwpkg_version(ioc);
  5762. if (r)
  5763. return r;
  5764. }
  5765. _base_static_config_pages(ioc);
  5766. r = _base_event_notification(ioc);
  5767. if (r)
  5768. return r;
  5769. if (ioc->is_driver_loading) {
  5770. if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
  5771. == 0x80) {
  5772. hide_flag = (u8) (
  5773. le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
  5774. MFG_PAGE10_HIDE_SSDS_MASK);
  5775. if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
  5776. ioc->mfg_pg10_hide_flag = hide_flag;
  5777. }
  5778. ioc->wait_for_discovery_to_complete =
  5779. _base_determine_wait_on_discovery(ioc);
  5780. return r; /* scan_start and scan_finished support */
  5781. }
  5782. r = _base_send_port_enable(ioc);
  5783. if (r)
  5784. return r;
  5785. return r;
  5786. }
  5787. /**
  5788. * mpt3sas_base_free_resources - free resources controller resources
  5789. * @ioc: per adapter object
  5790. */
  5791. void
  5792. mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
  5793. {
  5794. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5795. __func__));
  5796. /* synchronizing freeing resource with pci_access_mutex lock */
  5797. mutex_lock(&ioc->pci_access_mutex);
  5798. if (ioc->chip_phys && ioc->chip) {
  5799. _base_mask_interrupts(ioc);
  5800. ioc->shost_recovery = 1;
  5801. _base_make_ioc_ready(ioc, SOFT_RESET);
  5802. ioc->shost_recovery = 0;
  5803. }
  5804. mpt3sas_base_unmap_resources(ioc);
  5805. mutex_unlock(&ioc->pci_access_mutex);
  5806. return;
  5807. }
  5808. /**
  5809. * mpt3sas_base_attach - attach controller instance
  5810. * @ioc: per adapter object
  5811. *
  5812. * Return: 0 for success, non-zero for failure.
  5813. */
  5814. int
  5815. mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
  5816. {
  5817. int r, i;
  5818. int cpu_id, last_cpu_id = 0;
  5819. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  5820. __func__));
  5821. /* setup cpu_msix_table */
  5822. ioc->cpu_count = num_online_cpus();
  5823. for_each_online_cpu(cpu_id)
  5824. last_cpu_id = cpu_id;
  5825. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  5826. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  5827. ioc->reply_queue_count = 1;
  5828. if (!ioc->cpu_msix_table) {
  5829. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  5830. "allocation for cpu_msix_table failed!!!\n",
  5831. ioc->name));
  5832. r = -ENOMEM;
  5833. goto out_free_resources;
  5834. }
  5835. if (ioc->is_warpdrive) {
  5836. ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
  5837. sizeof(resource_size_t *), GFP_KERNEL);
  5838. if (!ioc->reply_post_host_index) {
  5839. dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
  5840. "for reply_post_host_index failed!!!\n",
  5841. ioc->name));
  5842. r = -ENOMEM;
  5843. goto out_free_resources;
  5844. }
  5845. }
  5846. ioc->rdpq_array_enable_assigned = 0;
  5847. ioc->dma_mask = 0;
  5848. r = mpt3sas_base_map_resources(ioc);
  5849. if (r)
  5850. goto out_free_resources;
  5851. pci_set_drvdata(ioc->pdev, ioc->shost);
  5852. r = _base_get_ioc_facts(ioc);
  5853. if (r)
  5854. goto out_free_resources;
  5855. switch (ioc->hba_mpi_version_belonged) {
  5856. case MPI2_VERSION:
  5857. ioc->build_sg_scmd = &_base_build_sg_scmd;
  5858. ioc->build_sg = &_base_build_sg;
  5859. ioc->build_zero_len_sge = &_base_build_zero_len_sge;
  5860. break;
  5861. case MPI25_VERSION:
  5862. case MPI26_VERSION:
  5863. /*
  5864. * In SAS3.0,
  5865. * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
  5866. * Target Status - all require the IEEE formated scatter gather
  5867. * elements.
  5868. */
  5869. ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
  5870. ioc->build_sg = &_base_build_sg_ieee;
  5871. ioc->build_nvme_prp = &_base_build_nvme_prp;
  5872. ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
  5873. ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
  5874. break;
  5875. }
  5876. if (ioc->is_mcpu_endpoint)
  5877. ioc->put_smid_scsi_io = &_base_put_smid_mpi_ep_scsi_io;
  5878. else
  5879. ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
  5880. /*
  5881. * These function pointers for other requests that don't
  5882. * the require IEEE scatter gather elements.
  5883. *
  5884. * For example Configuration Pages and SAS IOUNIT Control don't.
  5885. */
  5886. ioc->build_sg_mpi = &_base_build_sg;
  5887. ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
  5888. r = _base_make_ioc_ready(ioc, SOFT_RESET);
  5889. if (r)
  5890. goto out_free_resources;
  5891. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  5892. sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
  5893. if (!ioc->pfacts) {
  5894. r = -ENOMEM;
  5895. goto out_free_resources;
  5896. }
  5897. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  5898. r = _base_get_port_facts(ioc, i);
  5899. if (r)
  5900. goto out_free_resources;
  5901. }
  5902. r = _base_allocate_memory_pools(ioc);
  5903. if (r)
  5904. goto out_free_resources;
  5905. init_waitqueue_head(&ioc->reset_wq);
  5906. /* allocate memory pd handle bitmask list */
  5907. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  5908. if (ioc->facts.MaxDevHandle % 8)
  5909. ioc->pd_handles_sz++;
  5910. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  5911. GFP_KERNEL);
  5912. if (!ioc->pd_handles) {
  5913. r = -ENOMEM;
  5914. goto out_free_resources;
  5915. }
  5916. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  5917. GFP_KERNEL);
  5918. if (!ioc->blocking_handles) {
  5919. r = -ENOMEM;
  5920. goto out_free_resources;
  5921. }
  5922. /* allocate memory for pending OS device add list */
  5923. ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
  5924. if (ioc->facts.MaxDevHandle % 8)
  5925. ioc->pend_os_device_add_sz++;
  5926. ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
  5927. GFP_KERNEL);
  5928. if (!ioc->pend_os_device_add) {
  5929. r = -ENOMEM;
  5930. goto out_free_resources;
  5931. }
  5932. ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
  5933. ioc->device_remove_in_progress =
  5934. kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
  5935. if (!ioc->device_remove_in_progress) {
  5936. r = -ENOMEM;
  5937. goto out_free_resources;
  5938. }
  5939. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  5940. /* base internal command bits */
  5941. mutex_init(&ioc->base_cmds.mutex);
  5942. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5943. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  5944. /* port_enable command bits */
  5945. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5946. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  5947. /* transport internal command bits */
  5948. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5949. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  5950. mutex_init(&ioc->transport_cmds.mutex);
  5951. /* scsih internal command bits */
  5952. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5953. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  5954. mutex_init(&ioc->scsih_cmds.mutex);
  5955. /* task management internal command bits */
  5956. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5957. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  5958. mutex_init(&ioc->tm_cmds.mutex);
  5959. /* config page internal command bits */
  5960. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5961. ioc->config_cmds.status = MPT3_CMD_NOT_USED;
  5962. mutex_init(&ioc->config_cmds.mutex);
  5963. /* ctl module internal command bits */
  5964. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  5965. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  5966. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  5967. mutex_init(&ioc->ctl_cmds.mutex);
  5968. if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
  5969. !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
  5970. !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
  5971. !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
  5972. r = -ENOMEM;
  5973. goto out_free_resources;
  5974. }
  5975. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  5976. ioc->event_masks[i] = -1;
  5977. /* here we enable the events we care about */
  5978. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  5979. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  5980. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  5981. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  5982. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  5983. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  5984. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  5985. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  5986. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  5987. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  5988. _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
  5989. _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
  5990. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
  5991. if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
  5992. if (ioc->is_gen35_ioc) {
  5993. _base_unmask_events(ioc,
  5994. MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
  5995. _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
  5996. _base_unmask_events(ioc,
  5997. MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
  5998. }
  5999. }
  6000. r = _base_make_ioc_operational(ioc);
  6001. if (r)
  6002. goto out_free_resources;
  6003. ioc->non_operational_loop = 0;
  6004. ioc->got_task_abort_from_ioctl = 0;
  6005. return 0;
  6006. out_free_resources:
  6007. ioc->remove_host = 1;
  6008. mpt3sas_base_free_resources(ioc);
  6009. _base_release_memory_pools(ioc);
  6010. pci_set_drvdata(ioc->pdev, NULL);
  6011. kfree(ioc->cpu_msix_table);
  6012. if (ioc->is_warpdrive)
  6013. kfree(ioc->reply_post_host_index);
  6014. kfree(ioc->pd_handles);
  6015. kfree(ioc->blocking_handles);
  6016. kfree(ioc->device_remove_in_progress);
  6017. kfree(ioc->pend_os_device_add);
  6018. kfree(ioc->tm_cmds.reply);
  6019. kfree(ioc->transport_cmds.reply);
  6020. kfree(ioc->scsih_cmds.reply);
  6021. kfree(ioc->config_cmds.reply);
  6022. kfree(ioc->base_cmds.reply);
  6023. kfree(ioc->port_enable_cmds.reply);
  6024. kfree(ioc->ctl_cmds.reply);
  6025. kfree(ioc->ctl_cmds.sense);
  6026. kfree(ioc->pfacts);
  6027. ioc->ctl_cmds.reply = NULL;
  6028. ioc->base_cmds.reply = NULL;
  6029. ioc->tm_cmds.reply = NULL;
  6030. ioc->scsih_cmds.reply = NULL;
  6031. ioc->transport_cmds.reply = NULL;
  6032. ioc->config_cmds.reply = NULL;
  6033. ioc->pfacts = NULL;
  6034. return r;
  6035. }
  6036. /**
  6037. * mpt3sas_base_detach - remove controller instance
  6038. * @ioc: per adapter object
  6039. */
  6040. void
  6041. mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
  6042. {
  6043. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  6044. __func__));
  6045. mpt3sas_base_stop_watchdog(ioc);
  6046. mpt3sas_base_free_resources(ioc);
  6047. _base_release_memory_pools(ioc);
  6048. mpt3sas_free_enclosure_list(ioc);
  6049. pci_set_drvdata(ioc->pdev, NULL);
  6050. kfree(ioc->cpu_msix_table);
  6051. if (ioc->is_warpdrive)
  6052. kfree(ioc->reply_post_host_index);
  6053. kfree(ioc->pd_handles);
  6054. kfree(ioc->blocking_handles);
  6055. kfree(ioc->device_remove_in_progress);
  6056. kfree(ioc->pend_os_device_add);
  6057. kfree(ioc->pfacts);
  6058. kfree(ioc->ctl_cmds.reply);
  6059. kfree(ioc->ctl_cmds.sense);
  6060. kfree(ioc->base_cmds.reply);
  6061. kfree(ioc->port_enable_cmds.reply);
  6062. kfree(ioc->tm_cmds.reply);
  6063. kfree(ioc->transport_cmds.reply);
  6064. kfree(ioc->scsih_cmds.reply);
  6065. kfree(ioc->config_cmds.reply);
  6066. }
  6067. /**
  6068. * _base_pre_reset_handler - pre reset handler
  6069. * @ioc: per adapter object
  6070. */
  6071. static void _base_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
  6072. {
  6073. mpt3sas_scsih_pre_reset_handler(ioc);
  6074. mpt3sas_ctl_pre_reset_handler(ioc);
  6075. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  6076. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  6077. }
  6078. /**
  6079. * _base_after_reset_handler - after reset handler
  6080. * @ioc: per adapter object
  6081. */
  6082. static void _base_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
  6083. {
  6084. mpt3sas_scsih_after_reset_handler(ioc);
  6085. mpt3sas_ctl_after_reset_handler(ioc);
  6086. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  6087. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  6088. if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
  6089. ioc->transport_cmds.status |= MPT3_CMD_RESET;
  6090. mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  6091. complete(&ioc->transport_cmds.done);
  6092. }
  6093. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  6094. ioc->base_cmds.status |= MPT3_CMD_RESET;
  6095. mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
  6096. complete(&ioc->base_cmds.done);
  6097. }
  6098. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  6099. ioc->port_enable_failed = 1;
  6100. ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
  6101. mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  6102. if (ioc->is_driver_loading) {
  6103. ioc->start_scan_failed =
  6104. MPI2_IOCSTATUS_INTERNAL_ERROR;
  6105. ioc->start_scan = 0;
  6106. ioc->port_enable_cmds.status =
  6107. MPT3_CMD_NOT_USED;
  6108. } else {
  6109. complete(&ioc->port_enable_cmds.done);
  6110. }
  6111. }
  6112. if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
  6113. ioc->config_cmds.status |= MPT3_CMD_RESET;
  6114. mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
  6115. ioc->config_cmds.smid = USHRT_MAX;
  6116. complete(&ioc->config_cmds.done);
  6117. }
  6118. }
  6119. /**
  6120. * _base_reset_done_handler - reset done handler
  6121. * @ioc: per adapter object
  6122. */
  6123. static void _base_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
  6124. {
  6125. mpt3sas_scsih_reset_done_handler(ioc);
  6126. mpt3sas_ctl_reset_done_handler(ioc);
  6127. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  6128. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  6129. }
  6130. /**
  6131. * mpt3sas_wait_for_commands_to_complete - reset controller
  6132. * @ioc: Pointer to MPT_ADAPTER structure
  6133. *
  6134. * This function is waiting 10s for all pending commands to complete
  6135. * prior to putting controller in reset.
  6136. */
  6137. void
  6138. mpt3sas_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
  6139. {
  6140. u32 ioc_state;
  6141. ioc->pending_io_count = 0;
  6142. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  6143. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  6144. return;
  6145. /* pending command count */
  6146. ioc->pending_io_count = scsi_host_busy(ioc->shost);
  6147. if (!ioc->pending_io_count)
  6148. return;
  6149. /* wait for pending commands to complete */
  6150. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  6151. }
  6152. /**
  6153. * mpt3sas_base_hard_reset_handler - reset controller
  6154. * @ioc: Pointer to MPT_ADAPTER structure
  6155. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  6156. *
  6157. * Return: 0 for success, non-zero for failure.
  6158. */
  6159. int
  6160. mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
  6161. enum reset_type type)
  6162. {
  6163. int r;
  6164. unsigned long flags;
  6165. u32 ioc_state;
  6166. u8 is_fault = 0, is_trigger = 0;
  6167. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  6168. __func__));
  6169. if (ioc->pci_error_recovery) {
  6170. pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
  6171. ioc->name, __func__);
  6172. r = 0;
  6173. goto out_unlocked;
  6174. }
  6175. if (mpt3sas_fwfault_debug)
  6176. mpt3sas_halt_firmware(ioc);
  6177. /* wait for an active reset in progress to complete */
  6178. mutex_lock(&ioc->reset_in_progress_mutex);
  6179. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  6180. ioc->shost_recovery = 1;
  6181. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  6182. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  6183. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  6184. (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  6185. MPT3_DIAG_BUFFER_IS_RELEASED))) {
  6186. is_trigger = 1;
  6187. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  6188. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  6189. is_fault = 1;
  6190. }
  6191. _base_pre_reset_handler(ioc);
  6192. mpt3sas_wait_for_commands_to_complete(ioc);
  6193. _base_mask_interrupts(ioc);
  6194. r = _base_make_ioc_ready(ioc, type);
  6195. if (r)
  6196. goto out;
  6197. _base_after_reset_handler(ioc);
  6198. /* If this hard reset is called while port enable is active, then
  6199. * there is no reason to call make_ioc_operational
  6200. */
  6201. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  6202. ioc->remove_host = 1;
  6203. r = -EFAULT;
  6204. goto out;
  6205. }
  6206. r = _base_get_ioc_facts(ioc);
  6207. if (r)
  6208. goto out;
  6209. if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
  6210. panic("%s: Issue occurred with flashing controller firmware."
  6211. "Please reboot the system and ensure that the correct"
  6212. " firmware version is running\n", ioc->name);
  6213. r = _base_make_ioc_operational(ioc);
  6214. if (!r)
  6215. _base_reset_done_handler(ioc);
  6216. out:
  6217. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
  6218. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  6219. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  6220. ioc->shost_recovery = 0;
  6221. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  6222. ioc->ioc_reset_count++;
  6223. mutex_unlock(&ioc->reset_in_progress_mutex);
  6224. out_unlocked:
  6225. if ((r == 0) && is_trigger) {
  6226. if (is_fault)
  6227. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
  6228. else
  6229. mpt3sas_trigger_master(ioc,
  6230. MASTER_TRIGGER_ADAPTER_RESET);
  6231. }
  6232. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  6233. __func__));
  6234. return r;
  6235. }