megaraid_sas_base.c 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2003-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Authors: Avago Technologies
  21. * Sreenivas Bagalkote
  22. * Sumant Patro
  23. * Bo Yang
  24. * Adam Radford
  25. * Kashyap Desai <kashyap.desai@avagotech.com>
  26. * Sumit Saxena <sumit.saxena@avagotech.com>
  27. *
  28. * Send feedback to: megaraidlinux.pdl@avagotech.com
  29. *
  30. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  31. * San Jose, California 95131
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/list.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uio.h>
  43. #include <linux/slab.h>
  44. #include <linux/uaccess.h>
  45. #include <asm/unaligned.h>
  46. #include <linux/fs.h>
  47. #include <linux/compat.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/mutex.h>
  50. #include <linux/poll.h>
  51. #include <linux/vmalloc.h>
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsi_device.h>
  55. #include <scsi/scsi_host.h>
  56. #include <scsi/scsi_tcq.h>
  57. #include "megaraid_sas_fusion.h"
  58. #include "megaraid_sas.h"
  59. /*
  60. * Number of sectors per IO command
  61. * Will be set in megasas_init_mfi if user does not provide
  62. */
  63. static unsigned int max_sectors;
  64. module_param_named(max_sectors, max_sectors, int, 0);
  65. MODULE_PARM_DESC(max_sectors,
  66. "Maximum number of sectors per IO command");
  67. static int msix_disable;
  68. module_param(msix_disable, int, S_IRUGO);
  69. MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  70. static unsigned int msix_vectors;
  71. module_param(msix_vectors, int, S_IRUGO);
  72. MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  73. static int allow_vf_ioctls;
  74. module_param(allow_vf_ioctls, int, S_IRUGO);
  75. MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
  76. static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  77. module_param(throttlequeuedepth, int, S_IRUGO);
  78. MODULE_PARM_DESC(throttlequeuedepth,
  79. "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  80. unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  81. module_param(resetwaittime, int, S_IRUGO);
  82. MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
  83. "before resetting adapter. Default: 180");
  84. int smp_affinity_enable = 1;
  85. module_param(smp_affinity_enable, int, S_IRUGO);
  86. MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
  87. int rdpq_enable = 1;
  88. module_param(rdpq_enable, int, S_IRUGO);
  89. MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
  90. unsigned int dual_qdepth_disable;
  91. module_param(dual_qdepth_disable, int, S_IRUGO);
  92. MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
  93. unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  94. module_param(scmd_timeout, int, S_IRUGO);
  95. MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
  96. MODULE_LICENSE("GPL");
  97. MODULE_VERSION(MEGASAS_VERSION);
  98. MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
  99. MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
  100. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  101. static int megasas_get_pd_list(struct megasas_instance *instance);
  102. static int megasas_ld_list_query(struct megasas_instance *instance,
  103. u8 query_type);
  104. static int megasas_issue_init_mfi(struct megasas_instance *instance);
  105. static int megasas_register_aen(struct megasas_instance *instance,
  106. u32 seq_num, u32 class_locale_word);
  107. static void megasas_get_pd_info(struct megasas_instance *instance,
  108. struct scsi_device *sdev);
  109. /*
  110. * PCI ID table for all supported controllers
  111. */
  112. static struct pci_device_id megasas_pci_table[] = {
  113. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  114. /* xscale IOP */
  115. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  116. /* ppc IOP */
  117. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  118. /* ppc IOP */
  119. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  120. /* gen2*/
  121. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  122. /* gen2*/
  123. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  124. /* skinny*/
  125. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  126. /* skinny*/
  127. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  128. /* xscale IOP, vega */
  129. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  130. /* xscale IOP */
  131. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
  132. /* Fusion */
  133. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
  134. /* Plasma */
  135. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
  136. /* Invader */
  137. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
  138. /* Fury */
  139. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
  140. /* Intruder */
  141. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
  142. /* Intruder 24 port*/
  143. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
  144. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
  145. /* VENTURA */
  146. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
  147. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
  148. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
  149. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
  150. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
  151. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
  152. {}
  153. };
  154. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  155. static int megasas_mgmt_majorno;
  156. struct megasas_mgmt_info megasas_mgmt_info;
  157. static struct fasync_struct *megasas_async_queue;
  158. static DEFINE_MUTEX(megasas_async_queue_mutex);
  159. static int megasas_poll_wait_aen;
  160. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  161. static u32 support_poll_for_event;
  162. u32 megasas_dbg_lvl;
  163. static u32 support_device_change;
  164. static bool support_nvme_encapsulation;
  165. /* define lock for aen poll */
  166. spinlock_t poll_aen_lock;
  167. void
  168. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  169. u8 alt_status);
  170. static u32
  171. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
  172. static int
  173. megasas_adp_reset_gen2(struct megasas_instance *instance,
  174. struct megasas_register_set __iomem *reg_set);
  175. static irqreturn_t megasas_isr(int irq, void *devp);
  176. static u32
  177. megasas_init_adapter_mfi(struct megasas_instance *instance);
  178. u32
  179. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  180. struct scsi_cmnd *scmd);
  181. static void megasas_complete_cmd_dpc(unsigned long instance_addr);
  182. int
  183. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  184. int seconds);
  185. void megasas_fusion_ocr_wq(struct work_struct *work);
  186. static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
  187. int initial);
  188. static int
  189. megasas_set_dma_mask(struct megasas_instance *instance);
  190. static int
  191. megasas_alloc_ctrl_mem(struct megasas_instance *instance);
  192. static inline void
  193. megasas_free_ctrl_mem(struct megasas_instance *instance);
  194. static inline int
  195. megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
  196. static inline void
  197. megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
  198. static inline void
  199. megasas_init_ctrl_params(struct megasas_instance *instance);
  200. /**
  201. * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
  202. * @instance: Adapter soft state
  203. * @dcmd: DCMD frame inside MFI command
  204. * @dma_addr: DMA address of buffer to be passed to FW
  205. * @dma_len: Length of DMA buffer to be passed to FW
  206. * @return: void
  207. */
  208. void megasas_set_dma_settings(struct megasas_instance *instance,
  209. struct megasas_dcmd_frame *dcmd,
  210. dma_addr_t dma_addr, u32 dma_len)
  211. {
  212. if (instance->consistent_mask_64bit) {
  213. dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
  214. dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
  215. dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
  216. } else {
  217. dcmd->sgl.sge32[0].phys_addr =
  218. cpu_to_le32(lower_32_bits(dma_addr));
  219. dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
  220. dcmd->flags = cpu_to_le16(dcmd->flags);
  221. }
  222. }
  223. void
  224. megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  225. {
  226. instance->instancet->fire_cmd(instance,
  227. cmd->frame_phys_addr, 0, instance->reg_set);
  228. return;
  229. }
  230. /**
  231. * megasas_get_cmd - Get a command from the free pool
  232. * @instance: Adapter soft state
  233. *
  234. * Returns a free command from the pool
  235. */
  236. struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  237. *instance)
  238. {
  239. unsigned long flags;
  240. struct megasas_cmd *cmd = NULL;
  241. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  242. if (!list_empty(&instance->cmd_pool)) {
  243. cmd = list_entry((&instance->cmd_pool)->next,
  244. struct megasas_cmd, list);
  245. list_del_init(&cmd->list);
  246. } else {
  247. dev_err(&instance->pdev->dev, "Command pool empty!\n");
  248. }
  249. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  250. return cmd;
  251. }
  252. /**
  253. * megasas_return_cmd - Return a cmd to free command pool
  254. * @instance: Adapter soft state
  255. * @cmd: Command packet to be returned to free command pool
  256. */
  257. void
  258. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  259. {
  260. unsigned long flags;
  261. u32 blk_tags;
  262. struct megasas_cmd_fusion *cmd_fusion;
  263. struct fusion_context *fusion = instance->ctrl_context;
  264. /* This flag is used only for fusion adapter.
  265. * Wait for Interrupt for Polled mode DCMD
  266. */
  267. if (cmd->flags & DRV_DCMD_POLLED_MODE)
  268. return;
  269. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  270. if (fusion) {
  271. blk_tags = instance->max_scsi_cmds + cmd->index;
  272. cmd_fusion = fusion->cmd_list[blk_tags];
  273. megasas_return_cmd_fusion(instance, cmd_fusion);
  274. }
  275. cmd->scmd = NULL;
  276. cmd->frame_count = 0;
  277. cmd->flags = 0;
  278. memset(cmd->frame, 0, instance->mfi_frame_size);
  279. cmd->frame->io.context = cpu_to_le32(cmd->index);
  280. if (!fusion && reset_devices)
  281. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  282. list_add(&cmd->list, (&instance->cmd_pool)->next);
  283. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  284. }
  285. static const char *
  286. format_timestamp(uint32_t timestamp)
  287. {
  288. static char buffer[32];
  289. if ((timestamp & 0xff000000) == 0xff000000)
  290. snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
  291. 0x00ffffff);
  292. else
  293. snprintf(buffer, sizeof(buffer), "%us", timestamp);
  294. return buffer;
  295. }
  296. static const char *
  297. format_class(int8_t class)
  298. {
  299. static char buffer[6];
  300. switch (class) {
  301. case MFI_EVT_CLASS_DEBUG:
  302. return "debug";
  303. case MFI_EVT_CLASS_PROGRESS:
  304. return "progress";
  305. case MFI_EVT_CLASS_INFO:
  306. return "info";
  307. case MFI_EVT_CLASS_WARNING:
  308. return "WARN";
  309. case MFI_EVT_CLASS_CRITICAL:
  310. return "CRIT";
  311. case MFI_EVT_CLASS_FATAL:
  312. return "FATAL";
  313. case MFI_EVT_CLASS_DEAD:
  314. return "DEAD";
  315. default:
  316. snprintf(buffer, sizeof(buffer), "%d", class);
  317. return buffer;
  318. }
  319. }
  320. /**
  321. * megasas_decode_evt: Decode FW AEN event and print critical event
  322. * for information.
  323. * @instance: Adapter soft state
  324. */
  325. static void
  326. megasas_decode_evt(struct megasas_instance *instance)
  327. {
  328. struct megasas_evt_detail *evt_detail = instance->evt_detail;
  329. union megasas_evt_class_locale class_locale;
  330. class_locale.word = le32_to_cpu(evt_detail->cl.word);
  331. if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
  332. dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
  333. le32_to_cpu(evt_detail->seq_num),
  334. format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
  335. (class_locale.members.locale),
  336. format_class(class_locale.members.class),
  337. evt_detail->description);
  338. }
  339. /**
  340. * The following functions are defined for xscale
  341. * (deviceid : 1064R, PERC5) controllers
  342. */
  343. /**
  344. * megasas_enable_intr_xscale - Enables interrupts
  345. * @regs: MFI register set
  346. */
  347. static inline void
  348. megasas_enable_intr_xscale(struct megasas_instance *instance)
  349. {
  350. struct megasas_register_set __iomem *regs;
  351. regs = instance->reg_set;
  352. writel(0, &(regs)->outbound_intr_mask);
  353. /* Dummy readl to force pci flush */
  354. readl(&regs->outbound_intr_mask);
  355. }
  356. /**
  357. * megasas_disable_intr_xscale -Disables interrupt
  358. * @regs: MFI register set
  359. */
  360. static inline void
  361. megasas_disable_intr_xscale(struct megasas_instance *instance)
  362. {
  363. struct megasas_register_set __iomem *regs;
  364. u32 mask = 0x1f;
  365. regs = instance->reg_set;
  366. writel(mask, &regs->outbound_intr_mask);
  367. /* Dummy readl to force pci flush */
  368. readl(&regs->outbound_intr_mask);
  369. }
  370. /**
  371. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  372. * @regs: MFI register set
  373. */
  374. static u32
  375. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  376. {
  377. return readl(&(regs)->outbound_msg_0);
  378. }
  379. /**
  380. * megasas_clear_interrupt_xscale - Check & clear interrupt
  381. * @regs: MFI register set
  382. */
  383. static int
  384. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  385. {
  386. u32 status;
  387. u32 mfiStatus = 0;
  388. /*
  389. * Check if it is our interrupt
  390. */
  391. status = readl(&regs->outbound_intr_status);
  392. if (status & MFI_OB_INTR_STATUS_MASK)
  393. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  394. if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
  395. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  396. /*
  397. * Clear the interrupt by writing back the same value
  398. */
  399. if (mfiStatus)
  400. writel(status, &regs->outbound_intr_status);
  401. /* Dummy readl to force pci flush */
  402. readl(&regs->outbound_intr_status);
  403. return mfiStatus;
  404. }
  405. /**
  406. * megasas_fire_cmd_xscale - Sends command to the FW
  407. * @frame_phys_addr : Physical address of cmd
  408. * @frame_count : Number of frames for the command
  409. * @regs : MFI register set
  410. */
  411. static inline void
  412. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  413. dma_addr_t frame_phys_addr,
  414. u32 frame_count,
  415. struct megasas_register_set __iomem *regs)
  416. {
  417. unsigned long flags;
  418. spin_lock_irqsave(&instance->hba_lock, flags);
  419. writel((frame_phys_addr >> 3)|(frame_count),
  420. &(regs)->inbound_queue_port);
  421. spin_unlock_irqrestore(&instance->hba_lock, flags);
  422. }
  423. /**
  424. * megasas_adp_reset_xscale - For controller reset
  425. * @regs: MFI register set
  426. */
  427. static int
  428. megasas_adp_reset_xscale(struct megasas_instance *instance,
  429. struct megasas_register_set __iomem *regs)
  430. {
  431. u32 i;
  432. u32 pcidata;
  433. writel(MFI_ADP_RESET, &regs->inbound_doorbell);
  434. for (i = 0; i < 3; i++)
  435. msleep(1000); /* sleep for 3 secs */
  436. pcidata = 0;
  437. pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
  438. dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
  439. if (pcidata & 0x2) {
  440. dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
  441. pcidata &= ~0x2;
  442. pci_write_config_dword(instance->pdev,
  443. MFI_1068_PCSR_OFFSET, pcidata);
  444. for (i = 0; i < 2; i++)
  445. msleep(1000); /* need to wait 2 secs again */
  446. pcidata = 0;
  447. pci_read_config_dword(instance->pdev,
  448. MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
  449. dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
  450. if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
  451. dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
  452. pcidata = 0;
  453. pci_write_config_dword(instance->pdev,
  454. MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
  455. }
  456. }
  457. return 0;
  458. }
  459. /**
  460. * megasas_check_reset_xscale - For controller reset check
  461. * @regs: MFI register set
  462. */
  463. static int
  464. megasas_check_reset_xscale(struct megasas_instance *instance,
  465. struct megasas_register_set __iomem *regs)
  466. {
  467. if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
  468. (le32_to_cpu(*instance->consumer) ==
  469. MEGASAS_ADPRESET_INPROG_SIGN))
  470. return 1;
  471. return 0;
  472. }
  473. static struct megasas_instance_template megasas_instance_template_xscale = {
  474. .fire_cmd = megasas_fire_cmd_xscale,
  475. .enable_intr = megasas_enable_intr_xscale,
  476. .disable_intr = megasas_disable_intr_xscale,
  477. .clear_intr = megasas_clear_intr_xscale,
  478. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  479. .adp_reset = megasas_adp_reset_xscale,
  480. .check_reset = megasas_check_reset_xscale,
  481. .service_isr = megasas_isr,
  482. .tasklet = megasas_complete_cmd_dpc,
  483. .init_adapter = megasas_init_adapter_mfi,
  484. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  485. .issue_dcmd = megasas_issue_dcmd,
  486. };
  487. /**
  488. * This is the end of set of functions & definitions specific
  489. * to xscale (deviceid : 1064R, PERC5) controllers
  490. */
  491. /**
  492. * The following functions are defined for ppc (deviceid : 0x60)
  493. * controllers
  494. */
  495. /**
  496. * megasas_enable_intr_ppc - Enables interrupts
  497. * @regs: MFI register set
  498. */
  499. static inline void
  500. megasas_enable_intr_ppc(struct megasas_instance *instance)
  501. {
  502. struct megasas_register_set __iomem *regs;
  503. regs = instance->reg_set;
  504. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  505. writel(~0x80000000, &(regs)->outbound_intr_mask);
  506. /* Dummy readl to force pci flush */
  507. readl(&regs->outbound_intr_mask);
  508. }
  509. /**
  510. * megasas_disable_intr_ppc - Disable interrupt
  511. * @regs: MFI register set
  512. */
  513. static inline void
  514. megasas_disable_intr_ppc(struct megasas_instance *instance)
  515. {
  516. struct megasas_register_set __iomem *regs;
  517. u32 mask = 0xFFFFFFFF;
  518. regs = instance->reg_set;
  519. writel(mask, &regs->outbound_intr_mask);
  520. /* Dummy readl to force pci flush */
  521. readl(&regs->outbound_intr_mask);
  522. }
  523. /**
  524. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  525. * @regs: MFI register set
  526. */
  527. static u32
  528. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  529. {
  530. return readl(&(regs)->outbound_scratch_pad);
  531. }
  532. /**
  533. * megasas_clear_interrupt_ppc - Check & clear interrupt
  534. * @regs: MFI register set
  535. */
  536. static int
  537. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  538. {
  539. u32 status, mfiStatus = 0;
  540. /*
  541. * Check if it is our interrupt
  542. */
  543. status = readl(&regs->outbound_intr_status);
  544. if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
  545. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  546. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
  547. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  548. /*
  549. * Clear the interrupt by writing back the same value
  550. */
  551. writel(status, &regs->outbound_doorbell_clear);
  552. /* Dummy readl to force pci flush */
  553. readl(&regs->outbound_doorbell_clear);
  554. return mfiStatus;
  555. }
  556. /**
  557. * megasas_fire_cmd_ppc - Sends command to the FW
  558. * @frame_phys_addr : Physical address of cmd
  559. * @frame_count : Number of frames for the command
  560. * @regs : MFI register set
  561. */
  562. static inline void
  563. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  564. dma_addr_t frame_phys_addr,
  565. u32 frame_count,
  566. struct megasas_register_set __iomem *regs)
  567. {
  568. unsigned long flags;
  569. spin_lock_irqsave(&instance->hba_lock, flags);
  570. writel((frame_phys_addr | (frame_count<<1))|1,
  571. &(regs)->inbound_queue_port);
  572. spin_unlock_irqrestore(&instance->hba_lock, flags);
  573. }
  574. /**
  575. * megasas_check_reset_ppc - For controller reset check
  576. * @regs: MFI register set
  577. */
  578. static int
  579. megasas_check_reset_ppc(struct megasas_instance *instance,
  580. struct megasas_register_set __iomem *regs)
  581. {
  582. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  583. return 1;
  584. return 0;
  585. }
  586. static struct megasas_instance_template megasas_instance_template_ppc = {
  587. .fire_cmd = megasas_fire_cmd_ppc,
  588. .enable_intr = megasas_enable_intr_ppc,
  589. .disable_intr = megasas_disable_intr_ppc,
  590. .clear_intr = megasas_clear_intr_ppc,
  591. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  592. .adp_reset = megasas_adp_reset_xscale,
  593. .check_reset = megasas_check_reset_ppc,
  594. .service_isr = megasas_isr,
  595. .tasklet = megasas_complete_cmd_dpc,
  596. .init_adapter = megasas_init_adapter_mfi,
  597. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  598. .issue_dcmd = megasas_issue_dcmd,
  599. };
  600. /**
  601. * megasas_enable_intr_skinny - Enables interrupts
  602. * @regs: MFI register set
  603. */
  604. static inline void
  605. megasas_enable_intr_skinny(struct megasas_instance *instance)
  606. {
  607. struct megasas_register_set __iomem *regs;
  608. regs = instance->reg_set;
  609. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  610. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  611. /* Dummy readl to force pci flush */
  612. readl(&regs->outbound_intr_mask);
  613. }
  614. /**
  615. * megasas_disable_intr_skinny - Disables interrupt
  616. * @regs: MFI register set
  617. */
  618. static inline void
  619. megasas_disable_intr_skinny(struct megasas_instance *instance)
  620. {
  621. struct megasas_register_set __iomem *regs;
  622. u32 mask = 0xFFFFFFFF;
  623. regs = instance->reg_set;
  624. writel(mask, &regs->outbound_intr_mask);
  625. /* Dummy readl to force pci flush */
  626. readl(&regs->outbound_intr_mask);
  627. }
  628. /**
  629. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  630. * @regs: MFI register set
  631. */
  632. static u32
  633. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  634. {
  635. return readl(&(regs)->outbound_scratch_pad);
  636. }
  637. /**
  638. * megasas_clear_interrupt_skinny - Check & clear interrupt
  639. * @regs: MFI register set
  640. */
  641. static int
  642. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  643. {
  644. u32 status;
  645. u32 mfiStatus = 0;
  646. /*
  647. * Check if it is our interrupt
  648. */
  649. status = readl(&regs->outbound_intr_status);
  650. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  651. return 0;
  652. }
  653. /*
  654. * Check if it is our interrupt
  655. */
  656. if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
  657. MFI_STATE_FAULT) {
  658. mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  659. } else
  660. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  661. /*
  662. * Clear the interrupt by writing back the same value
  663. */
  664. writel(status, &regs->outbound_intr_status);
  665. /*
  666. * dummy read to flush PCI
  667. */
  668. readl(&regs->outbound_intr_status);
  669. return mfiStatus;
  670. }
  671. /**
  672. * megasas_fire_cmd_skinny - Sends command to the FW
  673. * @frame_phys_addr : Physical address of cmd
  674. * @frame_count : Number of frames for the command
  675. * @regs : MFI register set
  676. */
  677. static inline void
  678. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  679. dma_addr_t frame_phys_addr,
  680. u32 frame_count,
  681. struct megasas_register_set __iomem *regs)
  682. {
  683. unsigned long flags;
  684. spin_lock_irqsave(&instance->hba_lock, flags);
  685. writel(upper_32_bits(frame_phys_addr),
  686. &(regs)->inbound_high_queue_port);
  687. writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
  688. &(regs)->inbound_low_queue_port);
  689. mmiowb();
  690. spin_unlock_irqrestore(&instance->hba_lock, flags);
  691. }
  692. /**
  693. * megasas_check_reset_skinny - For controller reset check
  694. * @regs: MFI register set
  695. */
  696. static int
  697. megasas_check_reset_skinny(struct megasas_instance *instance,
  698. struct megasas_register_set __iomem *regs)
  699. {
  700. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  701. return 1;
  702. return 0;
  703. }
  704. static struct megasas_instance_template megasas_instance_template_skinny = {
  705. .fire_cmd = megasas_fire_cmd_skinny,
  706. .enable_intr = megasas_enable_intr_skinny,
  707. .disable_intr = megasas_disable_intr_skinny,
  708. .clear_intr = megasas_clear_intr_skinny,
  709. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  710. .adp_reset = megasas_adp_reset_gen2,
  711. .check_reset = megasas_check_reset_skinny,
  712. .service_isr = megasas_isr,
  713. .tasklet = megasas_complete_cmd_dpc,
  714. .init_adapter = megasas_init_adapter_mfi,
  715. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  716. .issue_dcmd = megasas_issue_dcmd,
  717. };
  718. /**
  719. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  720. * controllers
  721. */
  722. /**
  723. * megasas_enable_intr_gen2 - Enables interrupts
  724. * @regs: MFI register set
  725. */
  726. static inline void
  727. megasas_enable_intr_gen2(struct megasas_instance *instance)
  728. {
  729. struct megasas_register_set __iomem *regs;
  730. regs = instance->reg_set;
  731. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  732. /* write ~0x00000005 (4 & 1) to the intr mask*/
  733. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  734. /* Dummy readl to force pci flush */
  735. readl(&regs->outbound_intr_mask);
  736. }
  737. /**
  738. * megasas_disable_intr_gen2 - Disables interrupt
  739. * @regs: MFI register set
  740. */
  741. static inline void
  742. megasas_disable_intr_gen2(struct megasas_instance *instance)
  743. {
  744. struct megasas_register_set __iomem *regs;
  745. u32 mask = 0xFFFFFFFF;
  746. regs = instance->reg_set;
  747. writel(mask, &regs->outbound_intr_mask);
  748. /* Dummy readl to force pci flush */
  749. readl(&regs->outbound_intr_mask);
  750. }
  751. /**
  752. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  753. * @regs: MFI register set
  754. */
  755. static u32
  756. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  757. {
  758. return readl(&(regs)->outbound_scratch_pad);
  759. }
  760. /**
  761. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  762. * @regs: MFI register set
  763. */
  764. static int
  765. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  766. {
  767. u32 status;
  768. u32 mfiStatus = 0;
  769. /*
  770. * Check if it is our interrupt
  771. */
  772. status = readl(&regs->outbound_intr_status);
  773. if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
  774. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  775. }
  776. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
  777. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  778. }
  779. /*
  780. * Clear the interrupt by writing back the same value
  781. */
  782. if (mfiStatus)
  783. writel(status, &regs->outbound_doorbell_clear);
  784. /* Dummy readl to force pci flush */
  785. readl(&regs->outbound_intr_status);
  786. return mfiStatus;
  787. }
  788. /**
  789. * megasas_fire_cmd_gen2 - Sends command to the FW
  790. * @frame_phys_addr : Physical address of cmd
  791. * @frame_count : Number of frames for the command
  792. * @regs : MFI register set
  793. */
  794. static inline void
  795. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  796. dma_addr_t frame_phys_addr,
  797. u32 frame_count,
  798. struct megasas_register_set __iomem *regs)
  799. {
  800. unsigned long flags;
  801. spin_lock_irqsave(&instance->hba_lock, flags);
  802. writel((frame_phys_addr | (frame_count<<1))|1,
  803. &(regs)->inbound_queue_port);
  804. spin_unlock_irqrestore(&instance->hba_lock, flags);
  805. }
  806. /**
  807. * megasas_adp_reset_gen2 - For controller reset
  808. * @regs: MFI register set
  809. */
  810. static int
  811. megasas_adp_reset_gen2(struct megasas_instance *instance,
  812. struct megasas_register_set __iomem *reg_set)
  813. {
  814. u32 retry = 0 ;
  815. u32 HostDiag;
  816. u32 __iomem *seq_offset = &reg_set->seq_offset;
  817. u32 __iomem *hostdiag_offset = &reg_set->host_diag;
  818. if (instance->instancet == &megasas_instance_template_skinny) {
  819. seq_offset = &reg_set->fusion_seq_offset;
  820. hostdiag_offset = &reg_set->fusion_host_diag;
  821. }
  822. writel(0, seq_offset);
  823. writel(4, seq_offset);
  824. writel(0xb, seq_offset);
  825. writel(2, seq_offset);
  826. writel(7, seq_offset);
  827. writel(0xd, seq_offset);
  828. msleep(1000);
  829. HostDiag = (u32)readl(hostdiag_offset);
  830. while (!(HostDiag & DIAG_WRITE_ENABLE)) {
  831. msleep(100);
  832. HostDiag = (u32)readl(hostdiag_offset);
  833. dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
  834. retry, HostDiag);
  835. if (retry++ >= 100)
  836. return 1;
  837. }
  838. dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
  839. writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
  840. ssleep(10);
  841. HostDiag = (u32)readl(hostdiag_offset);
  842. while (HostDiag & DIAG_RESET_ADAPTER) {
  843. msleep(100);
  844. HostDiag = (u32)readl(hostdiag_offset);
  845. dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
  846. retry, HostDiag);
  847. if (retry++ >= 1000)
  848. return 1;
  849. }
  850. return 0;
  851. }
  852. /**
  853. * megasas_check_reset_gen2 - For controller reset check
  854. * @regs: MFI register set
  855. */
  856. static int
  857. megasas_check_reset_gen2(struct megasas_instance *instance,
  858. struct megasas_register_set __iomem *regs)
  859. {
  860. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  861. return 1;
  862. return 0;
  863. }
  864. static struct megasas_instance_template megasas_instance_template_gen2 = {
  865. .fire_cmd = megasas_fire_cmd_gen2,
  866. .enable_intr = megasas_enable_intr_gen2,
  867. .disable_intr = megasas_disable_intr_gen2,
  868. .clear_intr = megasas_clear_intr_gen2,
  869. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  870. .adp_reset = megasas_adp_reset_gen2,
  871. .check_reset = megasas_check_reset_gen2,
  872. .service_isr = megasas_isr,
  873. .tasklet = megasas_complete_cmd_dpc,
  874. .init_adapter = megasas_init_adapter_mfi,
  875. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  876. .issue_dcmd = megasas_issue_dcmd,
  877. };
  878. /**
  879. * This is the end of set of functions & definitions
  880. * specific to gen2 (deviceid : 0x78, 0x79) controllers
  881. */
  882. /*
  883. * Template added for TB (Fusion)
  884. */
  885. extern struct megasas_instance_template megasas_instance_template_fusion;
  886. /**
  887. * megasas_issue_polled - Issues a polling command
  888. * @instance: Adapter soft state
  889. * @cmd: Command packet to be issued
  890. *
  891. * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
  892. */
  893. int
  894. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  895. {
  896. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  897. frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
  898. frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  899. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  900. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  901. __func__, __LINE__);
  902. return DCMD_NOT_FIRED;
  903. }
  904. instance->instancet->issue_dcmd(instance, cmd);
  905. return wait_and_poll(instance, cmd, instance->requestorId ?
  906. MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
  907. }
  908. /**
  909. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  910. * @instance: Adapter soft state
  911. * @cmd: Command to be issued
  912. * @timeout: Timeout in seconds
  913. *
  914. * This function waits on an event for the command to be returned from ISR.
  915. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  916. * Used to issue ioctl commands.
  917. */
  918. int
  919. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  920. struct megasas_cmd *cmd, int timeout)
  921. {
  922. int ret = 0;
  923. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  924. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  925. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  926. __func__, __LINE__);
  927. return DCMD_NOT_FIRED;
  928. }
  929. instance->instancet->issue_dcmd(instance, cmd);
  930. if (timeout) {
  931. ret = wait_event_timeout(instance->int_cmd_wait_q,
  932. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
  933. if (!ret) {
  934. dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
  935. __func__, __LINE__);
  936. return DCMD_TIMEOUT;
  937. }
  938. } else
  939. wait_event(instance->int_cmd_wait_q,
  940. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
  941. return (cmd->cmd_status_drv == MFI_STAT_OK) ?
  942. DCMD_SUCCESS : DCMD_FAILED;
  943. }
  944. /**
  945. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  946. * @instance: Adapter soft state
  947. * @cmd_to_abort: Previously issued cmd to be aborted
  948. * @timeout: Timeout in seconds
  949. *
  950. * MFI firmware can abort previously issued AEN comamnd (automatic event
  951. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  952. * cmd and waits for return status.
  953. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  954. */
  955. static int
  956. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  957. struct megasas_cmd *cmd_to_abort, int timeout)
  958. {
  959. struct megasas_cmd *cmd;
  960. struct megasas_abort_frame *abort_fr;
  961. int ret = 0;
  962. cmd = megasas_get_cmd(instance);
  963. if (!cmd)
  964. return -1;
  965. abort_fr = &cmd->frame->abort;
  966. /*
  967. * Prepare and issue the abort frame
  968. */
  969. abort_fr->cmd = MFI_CMD_ABORT;
  970. abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
  971. abort_fr->flags = cpu_to_le16(0);
  972. abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
  973. abort_fr->abort_mfi_phys_addr_lo =
  974. cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
  975. abort_fr->abort_mfi_phys_addr_hi =
  976. cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
  977. cmd->sync_cmd = 1;
  978. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  979. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  980. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  981. __func__, __LINE__);
  982. return DCMD_NOT_FIRED;
  983. }
  984. instance->instancet->issue_dcmd(instance, cmd);
  985. if (timeout) {
  986. ret = wait_event_timeout(instance->abort_cmd_wait_q,
  987. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
  988. if (!ret) {
  989. dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
  990. __func__, __LINE__);
  991. return DCMD_TIMEOUT;
  992. }
  993. } else
  994. wait_event(instance->abort_cmd_wait_q,
  995. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
  996. cmd->sync_cmd = 0;
  997. megasas_return_cmd(instance, cmd);
  998. return (cmd->cmd_status_drv == MFI_STAT_OK) ?
  999. DCMD_SUCCESS : DCMD_FAILED;
  1000. }
  1001. /**
  1002. * megasas_make_sgl32 - Prepares 32-bit SGL
  1003. * @instance: Adapter soft state
  1004. * @scp: SCSI command from the mid-layer
  1005. * @mfi_sgl: SGL to be filled in
  1006. *
  1007. * If successful, this function returns the number of SG elements. Otherwise,
  1008. * it returnes -1.
  1009. */
  1010. static int
  1011. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1012. union megasas_sgl *mfi_sgl)
  1013. {
  1014. int i;
  1015. int sge_count;
  1016. struct scatterlist *os_sgl;
  1017. sge_count = scsi_dma_map(scp);
  1018. BUG_ON(sge_count < 0);
  1019. if (sge_count) {
  1020. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1021. mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  1022. mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
  1023. }
  1024. }
  1025. return sge_count;
  1026. }
  1027. /**
  1028. * megasas_make_sgl64 - Prepares 64-bit SGL
  1029. * @instance: Adapter soft state
  1030. * @scp: SCSI command from the mid-layer
  1031. * @mfi_sgl: SGL to be filled in
  1032. *
  1033. * If successful, this function returns the number of SG elements. Otherwise,
  1034. * it returnes -1.
  1035. */
  1036. static int
  1037. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1038. union megasas_sgl *mfi_sgl)
  1039. {
  1040. int i;
  1041. int sge_count;
  1042. struct scatterlist *os_sgl;
  1043. sge_count = scsi_dma_map(scp);
  1044. BUG_ON(sge_count < 0);
  1045. if (sge_count) {
  1046. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1047. mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  1048. mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
  1049. }
  1050. }
  1051. return sge_count;
  1052. }
  1053. /**
  1054. * megasas_make_sgl_skinny - Prepares IEEE SGL
  1055. * @instance: Adapter soft state
  1056. * @scp: SCSI command from the mid-layer
  1057. * @mfi_sgl: SGL to be filled in
  1058. *
  1059. * If successful, this function returns the number of SG elements. Otherwise,
  1060. * it returnes -1.
  1061. */
  1062. static int
  1063. megasas_make_sgl_skinny(struct megasas_instance *instance,
  1064. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  1065. {
  1066. int i;
  1067. int sge_count;
  1068. struct scatterlist *os_sgl;
  1069. sge_count = scsi_dma_map(scp);
  1070. if (sge_count) {
  1071. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1072. mfi_sgl->sge_skinny[i].length =
  1073. cpu_to_le32(sg_dma_len(os_sgl));
  1074. mfi_sgl->sge_skinny[i].phys_addr =
  1075. cpu_to_le64(sg_dma_address(os_sgl));
  1076. mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
  1077. }
  1078. }
  1079. return sge_count;
  1080. }
  1081. /**
  1082. * megasas_get_frame_count - Computes the number of frames
  1083. * @frame_type : type of frame- io or pthru frame
  1084. * @sge_count : number of sg elements
  1085. *
  1086. * Returns the number of frames required for numnber of sge's (sge_count)
  1087. */
  1088. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  1089. u8 sge_count, u8 frame_type)
  1090. {
  1091. int num_cnt;
  1092. int sge_bytes;
  1093. u32 sge_sz;
  1094. u32 frame_count = 0;
  1095. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1096. sizeof(struct megasas_sge32);
  1097. if (instance->flag_ieee) {
  1098. sge_sz = sizeof(struct megasas_sge_skinny);
  1099. }
  1100. /*
  1101. * Main frame can contain 2 SGEs for 64-bit SGLs and
  1102. * 3 SGEs for 32-bit SGLs for ldio &
  1103. * 1 SGEs for 64-bit SGLs and
  1104. * 2 SGEs for 32-bit SGLs for pthru frame
  1105. */
  1106. if (unlikely(frame_type == PTHRU_FRAME)) {
  1107. if (instance->flag_ieee == 1) {
  1108. num_cnt = sge_count - 1;
  1109. } else if (IS_DMA64)
  1110. num_cnt = sge_count - 1;
  1111. else
  1112. num_cnt = sge_count - 2;
  1113. } else {
  1114. if (instance->flag_ieee == 1) {
  1115. num_cnt = sge_count - 1;
  1116. } else if (IS_DMA64)
  1117. num_cnt = sge_count - 2;
  1118. else
  1119. num_cnt = sge_count - 3;
  1120. }
  1121. if (num_cnt > 0) {
  1122. sge_bytes = sge_sz * num_cnt;
  1123. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  1124. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  1125. }
  1126. /* Main frame */
  1127. frame_count += 1;
  1128. if (frame_count > 7)
  1129. frame_count = 8;
  1130. return frame_count;
  1131. }
  1132. /**
  1133. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  1134. * @instance: Adapter soft state
  1135. * @scp: SCSI command
  1136. * @cmd: Command to be prepared in
  1137. *
  1138. * This function prepares CDB commands. These are typcially pass-through
  1139. * commands to the devices.
  1140. */
  1141. static int
  1142. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1143. struct megasas_cmd *cmd)
  1144. {
  1145. u32 is_logical;
  1146. u32 device_id;
  1147. u16 flags = 0;
  1148. struct megasas_pthru_frame *pthru;
  1149. is_logical = MEGASAS_IS_LOGICAL(scp->device);
  1150. device_id = MEGASAS_DEV_INDEX(scp);
  1151. pthru = (struct megasas_pthru_frame *)cmd->frame;
  1152. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1153. flags = MFI_FRAME_DIR_WRITE;
  1154. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1155. flags = MFI_FRAME_DIR_READ;
  1156. else if (scp->sc_data_direction == PCI_DMA_NONE)
  1157. flags = MFI_FRAME_DIR_NONE;
  1158. if (instance->flag_ieee == 1) {
  1159. flags |= MFI_FRAME_IEEE;
  1160. }
  1161. /*
  1162. * Prepare the DCDB frame
  1163. */
  1164. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  1165. pthru->cmd_status = 0x0;
  1166. pthru->scsi_status = 0x0;
  1167. pthru->target_id = device_id;
  1168. pthru->lun = scp->device->lun;
  1169. pthru->cdb_len = scp->cmd_len;
  1170. pthru->timeout = 0;
  1171. pthru->pad_0 = 0;
  1172. pthru->flags = cpu_to_le16(flags);
  1173. pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
  1174. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1175. /*
  1176. * If the command is for the tape device, set the
  1177. * pthru timeout to the os layer timeout value.
  1178. */
  1179. if (scp->device->type == TYPE_TAPE) {
  1180. if ((scp->request->timeout / HZ) > 0xFFFF)
  1181. pthru->timeout = cpu_to_le16(0xFFFF);
  1182. else
  1183. pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
  1184. }
  1185. /*
  1186. * Construct SGL
  1187. */
  1188. if (instance->flag_ieee == 1) {
  1189. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1190. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  1191. &pthru->sgl);
  1192. } else if (IS_DMA64) {
  1193. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1194. pthru->sge_count = megasas_make_sgl64(instance, scp,
  1195. &pthru->sgl);
  1196. } else
  1197. pthru->sge_count = megasas_make_sgl32(instance, scp,
  1198. &pthru->sgl);
  1199. if (pthru->sge_count > instance->max_num_sge) {
  1200. dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
  1201. pthru->sge_count);
  1202. return 0;
  1203. }
  1204. /*
  1205. * Sense info specific
  1206. */
  1207. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  1208. pthru->sense_buf_phys_addr_hi =
  1209. cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
  1210. pthru->sense_buf_phys_addr_lo =
  1211. cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
  1212. /*
  1213. * Compute the total number of frames this command consumes. FW uses
  1214. * this number to pull sufficient number of frames from host memory.
  1215. */
  1216. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  1217. PTHRU_FRAME);
  1218. return cmd->frame_count;
  1219. }
  1220. /**
  1221. * megasas_build_ldio - Prepares IOs to logical devices
  1222. * @instance: Adapter soft state
  1223. * @scp: SCSI command
  1224. * @cmd: Command to be prepared
  1225. *
  1226. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  1227. */
  1228. static int
  1229. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1230. struct megasas_cmd *cmd)
  1231. {
  1232. u32 device_id;
  1233. u8 sc = scp->cmnd[0];
  1234. u16 flags = 0;
  1235. struct megasas_io_frame *ldio;
  1236. device_id = MEGASAS_DEV_INDEX(scp);
  1237. ldio = (struct megasas_io_frame *)cmd->frame;
  1238. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1239. flags = MFI_FRAME_DIR_WRITE;
  1240. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1241. flags = MFI_FRAME_DIR_READ;
  1242. if (instance->flag_ieee == 1) {
  1243. flags |= MFI_FRAME_IEEE;
  1244. }
  1245. /*
  1246. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  1247. */
  1248. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  1249. ldio->cmd_status = 0x0;
  1250. ldio->scsi_status = 0x0;
  1251. ldio->target_id = device_id;
  1252. ldio->timeout = 0;
  1253. ldio->reserved_0 = 0;
  1254. ldio->pad_0 = 0;
  1255. ldio->flags = cpu_to_le16(flags);
  1256. ldio->start_lba_hi = 0;
  1257. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  1258. /*
  1259. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1260. */
  1261. if (scp->cmd_len == 6) {
  1262. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
  1263. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
  1264. ((u32) scp->cmnd[2] << 8) |
  1265. (u32) scp->cmnd[3]);
  1266. ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
  1267. }
  1268. /*
  1269. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1270. */
  1271. else if (scp->cmd_len == 10) {
  1272. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
  1273. ((u32) scp->cmnd[7] << 8));
  1274. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1275. ((u32) scp->cmnd[3] << 16) |
  1276. ((u32) scp->cmnd[4] << 8) |
  1277. (u32) scp->cmnd[5]);
  1278. }
  1279. /*
  1280. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1281. */
  1282. else if (scp->cmd_len == 12) {
  1283. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1284. ((u32) scp->cmnd[7] << 16) |
  1285. ((u32) scp->cmnd[8] << 8) |
  1286. (u32) scp->cmnd[9]);
  1287. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1288. ((u32) scp->cmnd[3] << 16) |
  1289. ((u32) scp->cmnd[4] << 8) |
  1290. (u32) scp->cmnd[5]);
  1291. }
  1292. /*
  1293. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1294. */
  1295. else if (scp->cmd_len == 16) {
  1296. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
  1297. ((u32) scp->cmnd[11] << 16) |
  1298. ((u32) scp->cmnd[12] << 8) |
  1299. (u32) scp->cmnd[13]);
  1300. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1301. ((u32) scp->cmnd[7] << 16) |
  1302. ((u32) scp->cmnd[8] << 8) |
  1303. (u32) scp->cmnd[9]);
  1304. ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1305. ((u32) scp->cmnd[3] << 16) |
  1306. ((u32) scp->cmnd[4] << 8) |
  1307. (u32) scp->cmnd[5]);
  1308. }
  1309. /*
  1310. * Construct SGL
  1311. */
  1312. if (instance->flag_ieee) {
  1313. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1314. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  1315. &ldio->sgl);
  1316. } else if (IS_DMA64) {
  1317. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1318. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  1319. } else
  1320. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  1321. if (ldio->sge_count > instance->max_num_sge) {
  1322. dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
  1323. ldio->sge_count);
  1324. return 0;
  1325. }
  1326. /*
  1327. * Sense info specific
  1328. */
  1329. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  1330. ldio->sense_buf_phys_addr_hi = 0;
  1331. ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
  1332. /*
  1333. * Compute the total number of frames this command consumes. FW uses
  1334. * this number to pull sufficient number of frames from host memory.
  1335. */
  1336. cmd->frame_count = megasas_get_frame_count(instance,
  1337. ldio->sge_count, IO_FRAME);
  1338. return cmd->frame_count;
  1339. }
  1340. /**
  1341. * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
  1342. * and whether it's RW or non RW
  1343. * @scmd: SCSI command
  1344. *
  1345. */
  1346. inline int megasas_cmd_type(struct scsi_cmnd *cmd)
  1347. {
  1348. int ret;
  1349. switch (cmd->cmnd[0]) {
  1350. case READ_10:
  1351. case WRITE_10:
  1352. case READ_12:
  1353. case WRITE_12:
  1354. case READ_6:
  1355. case WRITE_6:
  1356. case READ_16:
  1357. case WRITE_16:
  1358. ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
  1359. READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
  1360. break;
  1361. default:
  1362. ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
  1363. NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
  1364. }
  1365. return ret;
  1366. }
  1367. /**
  1368. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  1369. * in FW
  1370. * @instance: Adapter soft state
  1371. */
  1372. static inline void
  1373. megasas_dump_pending_frames(struct megasas_instance *instance)
  1374. {
  1375. struct megasas_cmd *cmd;
  1376. int i,n;
  1377. union megasas_sgl *mfi_sgl;
  1378. struct megasas_io_frame *ldio;
  1379. struct megasas_pthru_frame *pthru;
  1380. u32 sgcount;
  1381. u16 max_cmd = instance->max_fw_cmds;
  1382. dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  1383. dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  1384. if (IS_DMA64)
  1385. dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  1386. else
  1387. dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  1388. dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  1389. for (i = 0; i < max_cmd; i++) {
  1390. cmd = instance->cmd_list[i];
  1391. if (!cmd->scmd)
  1392. continue;
  1393. dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  1394. if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
  1395. ldio = (struct megasas_io_frame *)cmd->frame;
  1396. mfi_sgl = &ldio->sgl;
  1397. sgcount = ldio->sge_count;
  1398. dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
  1399. " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1400. instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
  1401. le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
  1402. le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
  1403. } else {
  1404. pthru = (struct megasas_pthru_frame *) cmd->frame;
  1405. mfi_sgl = &pthru->sgl;
  1406. sgcount = pthru->sge_count;
  1407. dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
  1408. "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1409. instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
  1410. pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
  1411. le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
  1412. }
  1413. if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
  1414. for (n = 0; n < sgcount; n++) {
  1415. if (IS_DMA64)
  1416. dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
  1417. le32_to_cpu(mfi_sgl->sge64[n].length),
  1418. le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
  1419. else
  1420. dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
  1421. le32_to_cpu(mfi_sgl->sge32[n].length),
  1422. le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
  1423. }
  1424. }
  1425. } /*for max_cmd*/
  1426. dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  1427. for (i = 0; i < max_cmd; i++) {
  1428. cmd = instance->cmd_list[i];
  1429. if (cmd->sync_cmd == 1)
  1430. dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  1431. }
  1432. dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
  1433. }
  1434. u32
  1435. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  1436. struct scsi_cmnd *scmd)
  1437. {
  1438. struct megasas_cmd *cmd;
  1439. u32 frame_count;
  1440. cmd = megasas_get_cmd(instance);
  1441. if (!cmd)
  1442. return SCSI_MLQUEUE_HOST_BUSY;
  1443. /*
  1444. * Logical drive command
  1445. */
  1446. if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
  1447. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1448. else
  1449. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1450. if (!frame_count)
  1451. goto out_return_cmd;
  1452. cmd->scmd = scmd;
  1453. scmd->SCp.ptr = (char *)cmd;
  1454. /*
  1455. * Issue the command to the FW
  1456. */
  1457. atomic_inc(&instance->fw_outstanding);
  1458. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1459. cmd->frame_count-1, instance->reg_set);
  1460. return 0;
  1461. out_return_cmd:
  1462. megasas_return_cmd(instance, cmd);
  1463. return SCSI_MLQUEUE_HOST_BUSY;
  1464. }
  1465. /**
  1466. * megasas_queue_command - Queue entry point
  1467. * @scmd: SCSI command to be queued
  1468. * @done: Callback entry point
  1469. */
  1470. static int
  1471. megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
  1472. {
  1473. struct megasas_instance *instance;
  1474. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1475. instance = (struct megasas_instance *)
  1476. scmd->device->host->hostdata;
  1477. if (instance->unload == 1) {
  1478. scmd->result = DID_NO_CONNECT << 16;
  1479. scmd->scsi_done(scmd);
  1480. return 0;
  1481. }
  1482. if (instance->issuepend_done == 0)
  1483. return SCSI_MLQUEUE_HOST_BUSY;
  1484. /* Check for an mpio path and adjust behavior */
  1485. if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
  1486. if (megasas_check_mpio_paths(instance, scmd) ==
  1487. (DID_REQUEUE << 16)) {
  1488. return SCSI_MLQUEUE_HOST_BUSY;
  1489. } else {
  1490. scmd->result = DID_NO_CONNECT << 16;
  1491. scmd->scsi_done(scmd);
  1492. return 0;
  1493. }
  1494. }
  1495. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  1496. scmd->result = DID_NO_CONNECT << 16;
  1497. scmd->scsi_done(scmd);
  1498. return 0;
  1499. }
  1500. mr_device_priv_data = scmd->device->hostdata;
  1501. if (!mr_device_priv_data) {
  1502. scmd->result = DID_NO_CONNECT << 16;
  1503. scmd->scsi_done(scmd);
  1504. return 0;
  1505. }
  1506. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  1507. return SCSI_MLQUEUE_HOST_BUSY;
  1508. if (mr_device_priv_data->tm_busy)
  1509. return SCSI_MLQUEUE_DEVICE_BUSY;
  1510. scmd->result = 0;
  1511. if (MEGASAS_IS_LOGICAL(scmd->device) &&
  1512. (scmd->device->id >= instance->fw_supported_vd_count ||
  1513. scmd->device->lun)) {
  1514. scmd->result = DID_BAD_TARGET << 16;
  1515. goto out_done;
  1516. }
  1517. if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
  1518. MEGASAS_IS_LOGICAL(scmd->device) &&
  1519. (!instance->fw_sync_cache_support)) {
  1520. scmd->result = DID_OK << 16;
  1521. goto out_done;
  1522. }
  1523. return instance->instancet->build_and_issue_cmd(instance, scmd);
  1524. out_done:
  1525. scmd->scsi_done(scmd);
  1526. return 0;
  1527. }
  1528. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1529. {
  1530. int i;
  1531. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1532. if ((megasas_mgmt_info.instance[i]) &&
  1533. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1534. return megasas_mgmt_info.instance[i];
  1535. }
  1536. return NULL;
  1537. }
  1538. /*
  1539. * megasas_set_dynamic_target_properties -
  1540. * Device property set by driver may not be static and it is required to be
  1541. * updated after OCR
  1542. *
  1543. * set tm_capable.
  1544. * set dma alignment (only for eedp protection enable vd).
  1545. *
  1546. * @sdev: OS provided scsi device
  1547. *
  1548. * Returns void
  1549. */
  1550. void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
  1551. bool is_target_prop)
  1552. {
  1553. u16 pd_index = 0, ld;
  1554. u32 device_id;
  1555. struct megasas_instance *instance;
  1556. struct fusion_context *fusion;
  1557. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1558. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  1559. struct MR_LD_RAID *raid;
  1560. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1561. instance = megasas_lookup_instance(sdev->host->host_no);
  1562. fusion = instance->ctrl_context;
  1563. mr_device_priv_data = sdev->hostdata;
  1564. if (!fusion || !mr_device_priv_data)
  1565. return;
  1566. if (MEGASAS_IS_LOGICAL(sdev)) {
  1567. device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
  1568. + sdev->id;
  1569. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1570. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  1571. if (ld >= instance->fw_supported_vd_count)
  1572. return;
  1573. raid = MR_LdRaidGet(ld, local_map_ptr);
  1574. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
  1575. blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
  1576. mr_device_priv_data->is_tm_capable =
  1577. raid->capability.tmCapable;
  1578. } else if (instance->use_seqnum_jbod_fp) {
  1579. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1580. sdev->id;
  1581. pd_sync = (void *)fusion->pd_seq_sync
  1582. [(instance->pd_seq_map_id - 1) & 1];
  1583. mr_device_priv_data->is_tm_capable =
  1584. pd_sync->seq[pd_index].capability.tmCapable;
  1585. }
  1586. if (is_target_prop && instance->tgt_prop->reset_tmo) {
  1587. /*
  1588. * If FW provides a target reset timeout value, driver will use
  1589. * it. If not set, fallback to default values.
  1590. */
  1591. mr_device_priv_data->target_reset_tmo =
  1592. min_t(u8, instance->max_reset_tmo,
  1593. instance->tgt_prop->reset_tmo);
  1594. mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
  1595. } else {
  1596. mr_device_priv_data->target_reset_tmo =
  1597. MEGASAS_DEFAULT_TM_TIMEOUT;
  1598. mr_device_priv_data->task_abort_tmo =
  1599. MEGASAS_DEFAULT_TM_TIMEOUT;
  1600. }
  1601. }
  1602. /*
  1603. * megasas_set_nvme_device_properties -
  1604. * set nomerges=2
  1605. * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
  1606. * set maximum io transfer = MDTS of NVME device provided by MR firmware.
  1607. *
  1608. * MR firmware provides value in KB. Caller of this function converts
  1609. * kb into bytes.
  1610. *
  1611. * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
  1612. * MR firmware provides value 128 as (32 * 4K) = 128K.
  1613. *
  1614. * @sdev: scsi device
  1615. * @max_io_size: maximum io transfer size
  1616. *
  1617. */
  1618. static inline void
  1619. megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
  1620. {
  1621. struct megasas_instance *instance;
  1622. u32 mr_nvme_pg_size;
  1623. instance = (struct megasas_instance *)sdev->host->hostdata;
  1624. mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
  1625. MR_DEFAULT_NVME_PAGE_SIZE);
  1626. blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
  1627. blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
  1628. blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
  1629. }
  1630. /*
  1631. * megasas_set_static_target_properties -
  1632. * Device property set by driver are static and it is not required to be
  1633. * updated after OCR.
  1634. *
  1635. * set io timeout
  1636. * set device queue depth
  1637. * set nvme device properties. see - megasas_set_nvme_device_properties
  1638. *
  1639. * @sdev: scsi device
  1640. * @is_target_prop true, if fw provided target properties.
  1641. */
  1642. static void megasas_set_static_target_properties(struct scsi_device *sdev,
  1643. bool is_target_prop)
  1644. {
  1645. u16 target_index = 0;
  1646. u8 interface_type;
  1647. u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
  1648. u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
  1649. u32 tgt_device_qd;
  1650. struct megasas_instance *instance;
  1651. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1652. instance = megasas_lookup_instance(sdev->host->host_no);
  1653. mr_device_priv_data = sdev->hostdata;
  1654. interface_type = mr_device_priv_data->interface_type;
  1655. /*
  1656. * The RAID firmware may require extended timeouts.
  1657. */
  1658. blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
  1659. target_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
  1660. switch (interface_type) {
  1661. case SAS_PD:
  1662. device_qd = MEGASAS_SAS_QD;
  1663. break;
  1664. case SATA_PD:
  1665. device_qd = MEGASAS_SATA_QD;
  1666. break;
  1667. case NVME_PD:
  1668. device_qd = MEGASAS_NVME_QD;
  1669. break;
  1670. }
  1671. if (is_target_prop) {
  1672. tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
  1673. if (tgt_device_qd &&
  1674. (tgt_device_qd <= instance->host->can_queue))
  1675. device_qd = tgt_device_qd;
  1676. /* max_io_size_kb will be set to non zero for
  1677. * nvme based vd and syspd.
  1678. */
  1679. max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
  1680. }
  1681. if (instance->nvme_page_size && max_io_size_kb)
  1682. megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
  1683. scsi_change_queue_depth(sdev, device_qd);
  1684. }
  1685. static int megasas_slave_configure(struct scsi_device *sdev)
  1686. {
  1687. u16 pd_index = 0;
  1688. struct megasas_instance *instance;
  1689. int ret_target_prop = DCMD_FAILED;
  1690. bool is_target_prop = false;
  1691. instance = megasas_lookup_instance(sdev->host->host_no);
  1692. if (instance->pd_list_not_supported) {
  1693. if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
  1694. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1695. sdev->id;
  1696. if (instance->pd_list[pd_index].driveState !=
  1697. MR_PD_STATE_SYSTEM)
  1698. return -ENXIO;
  1699. }
  1700. }
  1701. mutex_lock(&instance->reset_mutex);
  1702. /* Send DCMD to Firmware and cache the information */
  1703. if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
  1704. megasas_get_pd_info(instance, sdev);
  1705. /* Some ventura firmware may not have instance->nvme_page_size set.
  1706. * Do not send MR_DCMD_DRV_GET_TARGET_PROP
  1707. */
  1708. if ((instance->tgt_prop) && (instance->nvme_page_size))
  1709. ret_target_prop = megasas_get_target_prop(instance, sdev);
  1710. is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
  1711. megasas_set_static_target_properties(sdev, is_target_prop);
  1712. /* This sdev property may change post OCR */
  1713. megasas_set_dynamic_target_properties(sdev, is_target_prop);
  1714. mutex_unlock(&instance->reset_mutex);
  1715. return 0;
  1716. }
  1717. static int megasas_slave_alloc(struct scsi_device *sdev)
  1718. {
  1719. u16 pd_index = 0;
  1720. struct megasas_instance *instance ;
  1721. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1722. instance = megasas_lookup_instance(sdev->host->host_no);
  1723. if (!MEGASAS_IS_LOGICAL(sdev)) {
  1724. /*
  1725. * Open the OS scan to the SYSTEM PD
  1726. */
  1727. pd_index =
  1728. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1729. sdev->id;
  1730. if ((instance->pd_list_not_supported ||
  1731. instance->pd_list[pd_index].driveState ==
  1732. MR_PD_STATE_SYSTEM)) {
  1733. goto scan_target;
  1734. }
  1735. return -ENXIO;
  1736. }
  1737. scan_target:
  1738. mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
  1739. GFP_KERNEL);
  1740. if (!mr_device_priv_data)
  1741. return -ENOMEM;
  1742. sdev->hostdata = mr_device_priv_data;
  1743. atomic_set(&mr_device_priv_data->r1_ldio_hint,
  1744. instance->r1_ldio_hint_default);
  1745. return 0;
  1746. }
  1747. static void megasas_slave_destroy(struct scsi_device *sdev)
  1748. {
  1749. kfree(sdev->hostdata);
  1750. sdev->hostdata = NULL;
  1751. }
  1752. /*
  1753. * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
  1754. * kill adapter
  1755. * @instance: Adapter soft state
  1756. *
  1757. */
  1758. static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
  1759. {
  1760. int i;
  1761. struct megasas_cmd *cmd_mfi;
  1762. struct megasas_cmd_fusion *cmd_fusion;
  1763. struct fusion_context *fusion = instance->ctrl_context;
  1764. /* Find all outstanding ioctls */
  1765. if (fusion) {
  1766. for (i = 0; i < instance->max_fw_cmds; i++) {
  1767. cmd_fusion = fusion->cmd_list[i];
  1768. if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
  1769. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  1770. if (cmd_mfi->sync_cmd &&
  1771. (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
  1772. cmd_mfi->frame->hdr.cmd_status =
  1773. MFI_STAT_WRONG_STATE;
  1774. megasas_complete_cmd(instance,
  1775. cmd_mfi, DID_OK);
  1776. }
  1777. }
  1778. }
  1779. } else {
  1780. for (i = 0; i < instance->max_fw_cmds; i++) {
  1781. cmd_mfi = instance->cmd_list[i];
  1782. if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
  1783. MFI_CMD_ABORT)
  1784. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  1785. }
  1786. }
  1787. }
  1788. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1789. {
  1790. /* Set critical error to block I/O & ioctls in case caller didn't */
  1791. atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
  1792. /* Wait 1 second to ensure IO or ioctls in build have posted */
  1793. msleep(1000);
  1794. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1795. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1796. (instance->adapter_type != MFI_SERIES)) {
  1797. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1798. /* Flush */
  1799. readl(&instance->reg_set->doorbell);
  1800. if (instance->requestorId && instance->peerIsPresent)
  1801. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  1802. } else {
  1803. writel(MFI_STOP_ADP,
  1804. &instance->reg_set->inbound_doorbell);
  1805. }
  1806. /* Complete outstanding ioctls when adapter is killed */
  1807. megasas_complete_outstanding_ioctls(instance);
  1808. }
  1809. /**
  1810. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1811. * restored to max value
  1812. * @instance: Adapter soft state
  1813. *
  1814. */
  1815. void
  1816. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1817. {
  1818. unsigned long flags;
  1819. if (instance->flag & MEGASAS_FW_BUSY
  1820. && time_after(jiffies, instance->last_time + 5 * HZ)
  1821. && atomic_read(&instance->fw_outstanding) <
  1822. instance->throttlequeuedepth + 1) {
  1823. spin_lock_irqsave(instance->host->host_lock, flags);
  1824. instance->flag &= ~MEGASAS_FW_BUSY;
  1825. instance->host->can_queue = instance->cur_can_queue;
  1826. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1827. }
  1828. }
  1829. /**
  1830. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1831. * @instance_addr: Address of adapter soft state
  1832. *
  1833. * Tasklet to complete cmds
  1834. */
  1835. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1836. {
  1837. u32 producer;
  1838. u32 consumer;
  1839. u32 context;
  1840. struct megasas_cmd *cmd;
  1841. struct megasas_instance *instance =
  1842. (struct megasas_instance *)instance_addr;
  1843. unsigned long flags;
  1844. /* If we have already declared adapter dead, donot complete cmds */
  1845. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  1846. return;
  1847. spin_lock_irqsave(&instance->completion_lock, flags);
  1848. producer = le32_to_cpu(*instance->producer);
  1849. consumer = le32_to_cpu(*instance->consumer);
  1850. while (consumer != producer) {
  1851. context = le32_to_cpu(instance->reply_queue[consumer]);
  1852. if (context >= instance->max_fw_cmds) {
  1853. dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
  1854. context);
  1855. BUG();
  1856. }
  1857. cmd = instance->cmd_list[context];
  1858. megasas_complete_cmd(instance, cmd, DID_OK);
  1859. consumer++;
  1860. if (consumer == (instance->max_fw_cmds + 1)) {
  1861. consumer = 0;
  1862. }
  1863. }
  1864. *instance->consumer = cpu_to_le32(producer);
  1865. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1866. /*
  1867. * Check if we can restore can_queue
  1868. */
  1869. megasas_check_and_restore_queue_depth(instance);
  1870. }
  1871. static void megasas_sriov_heartbeat_handler(struct timer_list *t);
  1872. /**
  1873. * megasas_start_timer - Initializes sriov heartbeat timer object
  1874. * @instance: Adapter soft state
  1875. *
  1876. */
  1877. void megasas_start_timer(struct megasas_instance *instance)
  1878. {
  1879. struct timer_list *timer = &instance->sriov_heartbeat_timer;
  1880. timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
  1881. timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
  1882. add_timer(timer);
  1883. }
  1884. static void
  1885. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1886. static void
  1887. process_fw_state_change_wq(struct work_struct *work);
  1888. void megasas_do_ocr(struct megasas_instance *instance)
  1889. {
  1890. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1891. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1892. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1893. *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  1894. }
  1895. instance->instancet->disable_intr(instance);
  1896. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  1897. instance->issuepend_done = 0;
  1898. atomic_set(&instance->fw_outstanding, 0);
  1899. megasas_internal_reset_defer_cmds(instance);
  1900. process_fw_state_change_wq(&instance->work_init);
  1901. }
  1902. static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
  1903. int initial)
  1904. {
  1905. struct megasas_cmd *cmd;
  1906. struct megasas_dcmd_frame *dcmd;
  1907. struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
  1908. dma_addr_t new_affiliation_111_h;
  1909. int ld, retval = 0;
  1910. u8 thisVf;
  1911. cmd = megasas_get_cmd(instance);
  1912. if (!cmd) {
  1913. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
  1914. "Failed to get cmd for scsi%d\n",
  1915. instance->host->host_no);
  1916. return -ENOMEM;
  1917. }
  1918. dcmd = &cmd->frame->dcmd;
  1919. if (!instance->vf_affiliation_111) {
  1920. dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
  1921. "affiliation for scsi%d\n", instance->host->host_no);
  1922. megasas_return_cmd(instance, cmd);
  1923. return -ENOMEM;
  1924. }
  1925. if (initial)
  1926. memset(instance->vf_affiliation_111, 0,
  1927. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1928. else {
  1929. new_affiliation_111 =
  1930. pci_zalloc_consistent(instance->pdev,
  1931. sizeof(struct MR_LD_VF_AFFILIATION_111),
  1932. &new_affiliation_111_h);
  1933. if (!new_affiliation_111) {
  1934. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
  1935. "memory for new affiliation for scsi%d\n",
  1936. instance->host->host_no);
  1937. megasas_return_cmd(instance, cmd);
  1938. return -ENOMEM;
  1939. }
  1940. }
  1941. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1942. dcmd->cmd = MFI_CMD_DCMD;
  1943. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  1944. dcmd->sge_count = 1;
  1945. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  1946. dcmd->timeout = 0;
  1947. dcmd->pad_0 = 0;
  1948. dcmd->data_xfer_len =
  1949. cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
  1950. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
  1951. if (initial)
  1952. dcmd->sgl.sge32[0].phys_addr =
  1953. cpu_to_le32(instance->vf_affiliation_111_h);
  1954. else
  1955. dcmd->sgl.sge32[0].phys_addr =
  1956. cpu_to_le32(new_affiliation_111_h);
  1957. dcmd->sgl.sge32[0].length = cpu_to_le32(
  1958. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1959. dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
  1960. "scsi%d\n", instance->host->host_no);
  1961. if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
  1962. dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
  1963. " failed with status 0x%x for scsi%d\n",
  1964. dcmd->cmd_status, instance->host->host_no);
  1965. retval = 1; /* Do a scan if we couldn't get affiliation */
  1966. goto out;
  1967. }
  1968. if (!initial) {
  1969. thisVf = new_affiliation_111->thisVf;
  1970. for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
  1971. if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
  1972. new_affiliation_111->map[ld].policy[thisVf]) {
  1973. dev_warn(&instance->pdev->dev, "SR-IOV: "
  1974. "Got new LD/VF affiliation for scsi%d\n",
  1975. instance->host->host_no);
  1976. memcpy(instance->vf_affiliation_111,
  1977. new_affiliation_111,
  1978. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1979. retval = 1;
  1980. goto out;
  1981. }
  1982. }
  1983. out:
  1984. if (new_affiliation_111) {
  1985. pci_free_consistent(instance->pdev,
  1986. sizeof(struct MR_LD_VF_AFFILIATION_111),
  1987. new_affiliation_111,
  1988. new_affiliation_111_h);
  1989. }
  1990. megasas_return_cmd(instance, cmd);
  1991. return retval;
  1992. }
  1993. static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
  1994. int initial)
  1995. {
  1996. struct megasas_cmd *cmd;
  1997. struct megasas_dcmd_frame *dcmd;
  1998. struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
  1999. struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
  2000. dma_addr_t new_affiliation_h;
  2001. int i, j, retval = 0, found = 0, doscan = 0;
  2002. u8 thisVf;
  2003. cmd = megasas_get_cmd(instance);
  2004. if (!cmd) {
  2005. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
  2006. "Failed to get cmd for scsi%d\n",
  2007. instance->host->host_no);
  2008. return -ENOMEM;
  2009. }
  2010. dcmd = &cmd->frame->dcmd;
  2011. if (!instance->vf_affiliation) {
  2012. dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
  2013. "affiliation for scsi%d\n", instance->host->host_no);
  2014. megasas_return_cmd(instance, cmd);
  2015. return -ENOMEM;
  2016. }
  2017. if (initial)
  2018. memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
  2019. sizeof(struct MR_LD_VF_AFFILIATION));
  2020. else {
  2021. new_affiliation =
  2022. pci_zalloc_consistent(instance->pdev,
  2023. (MAX_LOGICAL_DRIVES + 1) *
  2024. sizeof(struct MR_LD_VF_AFFILIATION),
  2025. &new_affiliation_h);
  2026. if (!new_affiliation) {
  2027. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
  2028. "memory for new affiliation for scsi%d\n",
  2029. instance->host->host_no);
  2030. megasas_return_cmd(instance, cmd);
  2031. return -ENOMEM;
  2032. }
  2033. }
  2034. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2035. dcmd->cmd = MFI_CMD_DCMD;
  2036. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  2037. dcmd->sge_count = 1;
  2038. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  2039. dcmd->timeout = 0;
  2040. dcmd->pad_0 = 0;
  2041. dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
  2042. sizeof(struct MR_LD_VF_AFFILIATION));
  2043. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
  2044. if (initial)
  2045. dcmd->sgl.sge32[0].phys_addr =
  2046. cpu_to_le32(instance->vf_affiliation_h);
  2047. else
  2048. dcmd->sgl.sge32[0].phys_addr =
  2049. cpu_to_le32(new_affiliation_h);
  2050. dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
  2051. sizeof(struct MR_LD_VF_AFFILIATION));
  2052. dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
  2053. "scsi%d\n", instance->host->host_no);
  2054. if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
  2055. dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
  2056. " failed with status 0x%x for scsi%d\n",
  2057. dcmd->cmd_status, instance->host->host_no);
  2058. retval = 1; /* Do a scan if we couldn't get affiliation */
  2059. goto out;
  2060. }
  2061. if (!initial) {
  2062. if (!new_affiliation->ldCount) {
  2063. dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
  2064. "affiliation for passive path for scsi%d\n",
  2065. instance->host->host_no);
  2066. retval = 1;
  2067. goto out;
  2068. }
  2069. newmap = new_affiliation->map;
  2070. savedmap = instance->vf_affiliation->map;
  2071. thisVf = new_affiliation->thisVf;
  2072. for (i = 0 ; i < new_affiliation->ldCount; i++) {
  2073. found = 0;
  2074. for (j = 0; j < instance->vf_affiliation->ldCount;
  2075. j++) {
  2076. if (newmap->ref.targetId ==
  2077. savedmap->ref.targetId) {
  2078. found = 1;
  2079. if (newmap->policy[thisVf] !=
  2080. savedmap->policy[thisVf]) {
  2081. doscan = 1;
  2082. goto out;
  2083. }
  2084. }
  2085. savedmap = (struct MR_LD_VF_MAP *)
  2086. ((unsigned char *)savedmap +
  2087. savedmap->size);
  2088. }
  2089. if (!found && newmap->policy[thisVf] !=
  2090. MR_LD_ACCESS_HIDDEN) {
  2091. doscan = 1;
  2092. goto out;
  2093. }
  2094. newmap = (struct MR_LD_VF_MAP *)
  2095. ((unsigned char *)newmap + newmap->size);
  2096. }
  2097. newmap = new_affiliation->map;
  2098. savedmap = instance->vf_affiliation->map;
  2099. for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
  2100. found = 0;
  2101. for (j = 0 ; j < new_affiliation->ldCount; j++) {
  2102. if (savedmap->ref.targetId ==
  2103. newmap->ref.targetId) {
  2104. found = 1;
  2105. if (savedmap->policy[thisVf] !=
  2106. newmap->policy[thisVf]) {
  2107. doscan = 1;
  2108. goto out;
  2109. }
  2110. }
  2111. newmap = (struct MR_LD_VF_MAP *)
  2112. ((unsigned char *)newmap +
  2113. newmap->size);
  2114. }
  2115. if (!found && savedmap->policy[thisVf] !=
  2116. MR_LD_ACCESS_HIDDEN) {
  2117. doscan = 1;
  2118. goto out;
  2119. }
  2120. savedmap = (struct MR_LD_VF_MAP *)
  2121. ((unsigned char *)savedmap +
  2122. savedmap->size);
  2123. }
  2124. }
  2125. out:
  2126. if (doscan) {
  2127. dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
  2128. "affiliation for scsi%d\n", instance->host->host_no);
  2129. memcpy(instance->vf_affiliation, new_affiliation,
  2130. new_affiliation->size);
  2131. retval = 1;
  2132. }
  2133. if (new_affiliation)
  2134. pci_free_consistent(instance->pdev,
  2135. (MAX_LOGICAL_DRIVES + 1) *
  2136. sizeof(struct MR_LD_VF_AFFILIATION),
  2137. new_affiliation, new_affiliation_h);
  2138. megasas_return_cmd(instance, cmd);
  2139. return retval;
  2140. }
  2141. /* This function will get the current SR-IOV LD/VF affiliation */
  2142. static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
  2143. int initial)
  2144. {
  2145. int retval;
  2146. if (instance->PlasmaFW111)
  2147. retval = megasas_get_ld_vf_affiliation_111(instance, initial);
  2148. else
  2149. retval = megasas_get_ld_vf_affiliation_12(instance, initial);
  2150. return retval;
  2151. }
  2152. /* This function will tell FW to start the SR-IOV heartbeat */
  2153. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  2154. int initial)
  2155. {
  2156. struct megasas_cmd *cmd;
  2157. struct megasas_dcmd_frame *dcmd;
  2158. int retval = 0;
  2159. cmd = megasas_get_cmd(instance);
  2160. if (!cmd) {
  2161. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
  2162. "Failed to get cmd for scsi%d\n",
  2163. instance->host->host_no);
  2164. return -ENOMEM;
  2165. }
  2166. dcmd = &cmd->frame->dcmd;
  2167. if (initial) {
  2168. instance->hb_host_mem =
  2169. pci_zalloc_consistent(instance->pdev,
  2170. sizeof(struct MR_CTRL_HB_HOST_MEM),
  2171. &instance->hb_host_mem_h);
  2172. if (!instance->hb_host_mem) {
  2173. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
  2174. " memory for heartbeat host memory for scsi%d\n",
  2175. instance->host->host_no);
  2176. retval = -ENOMEM;
  2177. goto out;
  2178. }
  2179. }
  2180. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2181. dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2182. dcmd->cmd = MFI_CMD_DCMD;
  2183. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  2184. dcmd->sge_count = 1;
  2185. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  2186. dcmd->timeout = 0;
  2187. dcmd->pad_0 = 0;
  2188. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2189. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
  2190. megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
  2191. sizeof(struct MR_CTRL_HB_HOST_MEM));
  2192. dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
  2193. instance->host->host_no);
  2194. if ((instance->adapter_type != MFI_SERIES) &&
  2195. !instance->mask_interrupts)
  2196. retval = megasas_issue_blocked_cmd(instance, cmd,
  2197. MEGASAS_ROUTINE_WAIT_TIME_VF);
  2198. else
  2199. retval = megasas_issue_polled(instance, cmd);
  2200. if (retval) {
  2201. dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
  2202. "_MEM_ALLOC DCMD %s for scsi%d\n",
  2203. (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
  2204. "timed out" : "failed", instance->host->host_no);
  2205. retval = 1;
  2206. }
  2207. out:
  2208. megasas_return_cmd(instance, cmd);
  2209. return retval;
  2210. }
  2211. /* Handler for SR-IOV heartbeat */
  2212. static void megasas_sriov_heartbeat_handler(struct timer_list *t)
  2213. {
  2214. struct megasas_instance *instance =
  2215. from_timer(instance, t, sriov_heartbeat_timer);
  2216. if (instance->hb_host_mem->HB.fwCounter !=
  2217. instance->hb_host_mem->HB.driverCounter) {
  2218. instance->hb_host_mem->HB.driverCounter =
  2219. instance->hb_host_mem->HB.fwCounter;
  2220. mod_timer(&instance->sriov_heartbeat_timer,
  2221. jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  2222. } else {
  2223. dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
  2224. "completed for scsi%d\n", instance->host->host_no);
  2225. schedule_work(&instance->work_init);
  2226. }
  2227. }
  2228. /**
  2229. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  2230. * @instance: Adapter soft state
  2231. *
  2232. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  2233. * complete all its outstanding commands. Returns error if one or more IOs
  2234. * are pending after this time period. It also marks the controller dead.
  2235. */
  2236. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  2237. {
  2238. int i, sl, outstanding;
  2239. u32 reset_index;
  2240. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  2241. unsigned long flags;
  2242. struct list_head clist_local;
  2243. struct megasas_cmd *reset_cmd;
  2244. u32 fw_state;
  2245. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2246. dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
  2247. __func__, __LINE__);
  2248. return FAILED;
  2249. }
  2250. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2251. INIT_LIST_HEAD(&clist_local);
  2252. spin_lock_irqsave(&instance->hba_lock, flags);
  2253. list_splice_init(&instance->internal_reset_pending_q,
  2254. &clist_local);
  2255. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2256. dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
  2257. for (i = 0; i < wait_time; i++) {
  2258. msleep(1000);
  2259. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
  2260. break;
  2261. }
  2262. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2263. dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
  2264. atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
  2265. return FAILED;
  2266. }
  2267. reset_index = 0;
  2268. while (!list_empty(&clist_local)) {
  2269. reset_cmd = list_entry((&clist_local)->next,
  2270. struct megasas_cmd, list);
  2271. list_del_init(&reset_cmd->list);
  2272. if (reset_cmd->scmd) {
  2273. reset_cmd->scmd->result = DID_REQUEUE << 16;
  2274. dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
  2275. reset_index, reset_cmd,
  2276. reset_cmd->scmd->cmnd[0]);
  2277. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  2278. megasas_return_cmd(instance, reset_cmd);
  2279. } else if (reset_cmd->sync_cmd) {
  2280. dev_notice(&instance->pdev->dev, "%p synch cmds"
  2281. "reset queue\n",
  2282. reset_cmd);
  2283. reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  2284. instance->instancet->fire_cmd(instance,
  2285. reset_cmd->frame_phys_addr,
  2286. 0, instance->reg_set);
  2287. } else {
  2288. dev_notice(&instance->pdev->dev, "%p unexpected"
  2289. "cmds lst\n",
  2290. reset_cmd);
  2291. }
  2292. reset_index++;
  2293. }
  2294. return SUCCESS;
  2295. }
  2296. for (i = 0; i < resetwaittime; i++) {
  2297. outstanding = atomic_read(&instance->fw_outstanding);
  2298. if (!outstanding)
  2299. break;
  2300. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  2301. dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
  2302. "commands to complete\n",i,outstanding);
  2303. /*
  2304. * Call cmd completion routine. Cmd to be
  2305. * be completed directly without depending on isr.
  2306. */
  2307. megasas_complete_cmd_dpc((unsigned long)instance);
  2308. }
  2309. msleep(1000);
  2310. }
  2311. i = 0;
  2312. outstanding = atomic_read(&instance->fw_outstanding);
  2313. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2314. if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
  2315. goto no_outstanding;
  2316. if (instance->disableOnlineCtrlReset)
  2317. goto kill_hba_and_failed;
  2318. do {
  2319. if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
  2320. dev_info(&instance->pdev->dev,
  2321. "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
  2322. __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
  2323. if (i == 3)
  2324. goto kill_hba_and_failed;
  2325. megasas_do_ocr(instance);
  2326. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2327. dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
  2328. __func__, __LINE__);
  2329. return FAILED;
  2330. }
  2331. dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
  2332. __func__, __LINE__);
  2333. for (sl = 0; sl < 10; sl++)
  2334. msleep(500);
  2335. outstanding = atomic_read(&instance->fw_outstanding);
  2336. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2337. if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
  2338. goto no_outstanding;
  2339. }
  2340. i++;
  2341. } while (i <= 3);
  2342. no_outstanding:
  2343. dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
  2344. __func__, __LINE__);
  2345. return SUCCESS;
  2346. kill_hba_and_failed:
  2347. /* Reset not supported, kill adapter */
  2348. dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
  2349. " disableOnlineCtrlReset %d fw_outstanding %d \n",
  2350. __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
  2351. atomic_read(&instance->fw_outstanding));
  2352. megasas_dump_pending_frames(instance);
  2353. megaraid_sas_kill_hba(instance);
  2354. return FAILED;
  2355. }
  2356. /**
  2357. * megasas_generic_reset - Generic reset routine
  2358. * @scmd: Mid-layer SCSI command
  2359. *
  2360. * This routine implements a generic reset handler for device, bus and host
  2361. * reset requests. Device, bus and host specific reset handlers can use this
  2362. * function after they do their specific tasks.
  2363. */
  2364. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  2365. {
  2366. int ret_val;
  2367. struct megasas_instance *instance;
  2368. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2369. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  2370. scmd->cmnd[0], scmd->retries);
  2371. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2372. dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
  2373. return FAILED;
  2374. }
  2375. ret_val = megasas_wait_for_outstanding(instance);
  2376. if (ret_val == SUCCESS)
  2377. dev_notice(&instance->pdev->dev, "reset successful\n");
  2378. else
  2379. dev_err(&instance->pdev->dev, "failed to do reset\n");
  2380. return ret_val;
  2381. }
  2382. /**
  2383. * megasas_reset_timer - quiesce the adapter if required
  2384. * @scmd: scsi cmnd
  2385. *
  2386. * Sets the FW busy flag and reduces the host->can_queue if the
  2387. * cmd has not been completed within the timeout period.
  2388. */
  2389. static enum
  2390. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  2391. {
  2392. struct megasas_instance *instance;
  2393. unsigned long flags;
  2394. if (time_after(jiffies, scmd->jiffies_at_alloc +
  2395. (scmd_timeout * 2) * HZ)) {
  2396. return BLK_EH_DONE;
  2397. }
  2398. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2399. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  2400. /* FW is busy, throttle IO */
  2401. spin_lock_irqsave(instance->host->host_lock, flags);
  2402. instance->host->can_queue = instance->throttlequeuedepth;
  2403. instance->last_time = jiffies;
  2404. instance->flag |= MEGASAS_FW_BUSY;
  2405. spin_unlock_irqrestore(instance->host->host_lock, flags);
  2406. }
  2407. return BLK_EH_RESET_TIMER;
  2408. }
  2409. /**
  2410. * megasas_dump_frame - This function will dump MPT/MFI frame
  2411. */
  2412. static inline void
  2413. megasas_dump_frame(void *mpi_request, int sz)
  2414. {
  2415. int i;
  2416. __le32 *mfp = (__le32 *)mpi_request;
  2417. printk(KERN_INFO "IO request frame:\n\t");
  2418. for (i = 0; i < sz / sizeof(__le32); i++) {
  2419. if (i && ((i % 8) == 0))
  2420. printk("\n\t");
  2421. printk("%08x ", le32_to_cpu(mfp[i]));
  2422. }
  2423. printk("\n");
  2424. }
  2425. /**
  2426. * megasas_reset_bus_host - Bus & host reset handler entry point
  2427. */
  2428. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  2429. {
  2430. int ret;
  2431. struct megasas_instance *instance;
  2432. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2433. scmd_printk(KERN_INFO, scmd,
  2434. "Controller reset is requested due to IO timeout\n"
  2435. "SCSI command pointer: (%p)\t SCSI host state: %d\t"
  2436. " SCSI host busy: %d\t FW outstanding: %d\n",
  2437. scmd, scmd->device->host->shost_state,
  2438. scsi_host_busy(scmd->device->host),
  2439. atomic_read(&instance->fw_outstanding));
  2440. /*
  2441. * First wait for all commands to complete
  2442. */
  2443. if (instance->adapter_type == MFI_SERIES) {
  2444. ret = megasas_generic_reset(scmd);
  2445. } else {
  2446. struct megasas_cmd_fusion *cmd;
  2447. cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
  2448. if (cmd)
  2449. megasas_dump_frame(cmd->io_request,
  2450. MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
  2451. ret = megasas_reset_fusion(scmd->device->host,
  2452. SCSIIO_TIMEOUT_OCR);
  2453. }
  2454. return ret;
  2455. }
  2456. /**
  2457. * megasas_task_abort - Issues task abort request to firmware
  2458. * (supported only for fusion adapters)
  2459. * @scmd: SCSI command pointer
  2460. */
  2461. static int megasas_task_abort(struct scsi_cmnd *scmd)
  2462. {
  2463. int ret;
  2464. struct megasas_instance *instance;
  2465. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2466. if (instance->adapter_type != MFI_SERIES)
  2467. ret = megasas_task_abort_fusion(scmd);
  2468. else {
  2469. sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
  2470. ret = FAILED;
  2471. }
  2472. return ret;
  2473. }
  2474. /**
  2475. * megasas_reset_target: Issues target reset request to firmware
  2476. * (supported only for fusion adapters)
  2477. * @scmd: SCSI command pointer
  2478. */
  2479. static int megasas_reset_target(struct scsi_cmnd *scmd)
  2480. {
  2481. int ret;
  2482. struct megasas_instance *instance;
  2483. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2484. if (instance->adapter_type != MFI_SERIES)
  2485. ret = megasas_reset_target_fusion(scmd);
  2486. else {
  2487. sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
  2488. ret = FAILED;
  2489. }
  2490. return ret;
  2491. }
  2492. /**
  2493. * megasas_bios_param - Returns disk geometry for a disk
  2494. * @sdev: device handle
  2495. * @bdev: block device
  2496. * @capacity: drive capacity
  2497. * @geom: geometry parameters
  2498. */
  2499. static int
  2500. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  2501. sector_t capacity, int geom[])
  2502. {
  2503. int heads;
  2504. int sectors;
  2505. sector_t cylinders;
  2506. unsigned long tmp;
  2507. /* Default heads (64) & sectors (32) */
  2508. heads = 64;
  2509. sectors = 32;
  2510. tmp = heads * sectors;
  2511. cylinders = capacity;
  2512. sector_div(cylinders, tmp);
  2513. /*
  2514. * Handle extended translation size for logical drives > 1Gb
  2515. */
  2516. if (capacity >= 0x200000) {
  2517. heads = 255;
  2518. sectors = 63;
  2519. tmp = heads*sectors;
  2520. cylinders = capacity;
  2521. sector_div(cylinders, tmp);
  2522. }
  2523. geom[0] = heads;
  2524. geom[1] = sectors;
  2525. geom[2] = cylinders;
  2526. return 0;
  2527. }
  2528. static void megasas_aen_polling(struct work_struct *work);
  2529. /**
  2530. * megasas_service_aen - Processes an event notification
  2531. * @instance: Adapter soft state
  2532. * @cmd: AEN command completed by the ISR
  2533. *
  2534. * For AEN, driver sends a command down to FW that is held by the FW till an
  2535. * event occurs. When an event of interest occurs, FW completes the command
  2536. * that it was previously holding.
  2537. *
  2538. * This routines sends SIGIO signal to processes that have registered with the
  2539. * driver for AEN.
  2540. */
  2541. static void
  2542. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  2543. {
  2544. unsigned long flags;
  2545. /*
  2546. * Don't signal app if it is just an aborted previously registered aen
  2547. */
  2548. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  2549. spin_lock_irqsave(&poll_aen_lock, flags);
  2550. megasas_poll_wait_aen = 1;
  2551. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2552. wake_up(&megasas_poll_wait);
  2553. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  2554. }
  2555. else
  2556. cmd->abort_aen = 0;
  2557. instance->aen_cmd = NULL;
  2558. megasas_return_cmd(instance, cmd);
  2559. if ((instance->unload == 0) &&
  2560. ((instance->issuepend_done == 1))) {
  2561. struct megasas_aen_event *ev;
  2562. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2563. if (!ev) {
  2564. dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
  2565. } else {
  2566. ev->instance = instance;
  2567. instance->ev = ev;
  2568. INIT_DELAYED_WORK(&ev->hotplug_work,
  2569. megasas_aen_polling);
  2570. schedule_delayed_work(&ev->hotplug_work, 0);
  2571. }
  2572. }
  2573. }
  2574. static ssize_t
  2575. megasas_fw_crash_buffer_store(struct device *cdev,
  2576. struct device_attribute *attr, const char *buf, size_t count)
  2577. {
  2578. struct Scsi_Host *shost = class_to_shost(cdev);
  2579. struct megasas_instance *instance =
  2580. (struct megasas_instance *) shost->hostdata;
  2581. int val = 0;
  2582. unsigned long flags;
  2583. if (kstrtoint(buf, 0, &val) != 0)
  2584. return -EINVAL;
  2585. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2586. instance->fw_crash_buffer_offset = val;
  2587. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2588. return strlen(buf);
  2589. }
  2590. static ssize_t
  2591. megasas_fw_crash_buffer_show(struct device *cdev,
  2592. struct device_attribute *attr, char *buf)
  2593. {
  2594. struct Scsi_Host *shost = class_to_shost(cdev);
  2595. struct megasas_instance *instance =
  2596. (struct megasas_instance *) shost->hostdata;
  2597. u32 size;
  2598. unsigned long buff_addr;
  2599. unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
  2600. unsigned long chunk_left_bytes;
  2601. unsigned long src_addr;
  2602. unsigned long flags;
  2603. u32 buff_offset;
  2604. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2605. buff_offset = instance->fw_crash_buffer_offset;
  2606. if (!instance->crash_dump_buf &&
  2607. !((instance->fw_crash_state == AVAILABLE) ||
  2608. (instance->fw_crash_state == COPYING))) {
  2609. dev_err(&instance->pdev->dev,
  2610. "Firmware crash dump is not available\n");
  2611. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2612. return -EINVAL;
  2613. }
  2614. buff_addr = (unsigned long) buf;
  2615. if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
  2616. dev_err(&instance->pdev->dev,
  2617. "Firmware crash dump offset is out of range\n");
  2618. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2619. return 0;
  2620. }
  2621. size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
  2622. chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
  2623. size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
  2624. size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
  2625. src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
  2626. (buff_offset % dmachunk);
  2627. memcpy(buf, (void *)src_addr, size);
  2628. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2629. return size;
  2630. }
  2631. static ssize_t
  2632. megasas_fw_crash_buffer_size_show(struct device *cdev,
  2633. struct device_attribute *attr, char *buf)
  2634. {
  2635. struct Scsi_Host *shost = class_to_shost(cdev);
  2636. struct megasas_instance *instance =
  2637. (struct megasas_instance *) shost->hostdata;
  2638. return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
  2639. ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
  2640. }
  2641. static ssize_t
  2642. megasas_fw_crash_state_store(struct device *cdev,
  2643. struct device_attribute *attr, const char *buf, size_t count)
  2644. {
  2645. struct Scsi_Host *shost = class_to_shost(cdev);
  2646. struct megasas_instance *instance =
  2647. (struct megasas_instance *) shost->hostdata;
  2648. int val = 0;
  2649. unsigned long flags;
  2650. if (kstrtoint(buf, 0, &val) != 0)
  2651. return -EINVAL;
  2652. if ((val <= AVAILABLE || val > COPY_ERROR)) {
  2653. dev_err(&instance->pdev->dev, "application updates invalid "
  2654. "firmware crash state\n");
  2655. return -EINVAL;
  2656. }
  2657. instance->fw_crash_state = val;
  2658. if ((val == COPIED) || (val == COPY_ERROR)) {
  2659. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2660. megasas_free_host_crash_buffer(instance);
  2661. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2662. if (val == COPY_ERROR)
  2663. dev_info(&instance->pdev->dev, "application failed to "
  2664. "copy Firmware crash dump\n");
  2665. else
  2666. dev_info(&instance->pdev->dev, "Firmware crash dump "
  2667. "copied successfully\n");
  2668. }
  2669. return strlen(buf);
  2670. }
  2671. static ssize_t
  2672. megasas_fw_crash_state_show(struct device *cdev,
  2673. struct device_attribute *attr, char *buf)
  2674. {
  2675. struct Scsi_Host *shost = class_to_shost(cdev);
  2676. struct megasas_instance *instance =
  2677. (struct megasas_instance *) shost->hostdata;
  2678. return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
  2679. }
  2680. static ssize_t
  2681. megasas_page_size_show(struct device *cdev,
  2682. struct device_attribute *attr, char *buf)
  2683. {
  2684. return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
  2685. }
  2686. static ssize_t
  2687. megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
  2688. char *buf)
  2689. {
  2690. struct Scsi_Host *shost = class_to_shost(cdev);
  2691. struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
  2692. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
  2693. }
  2694. static ssize_t
  2695. megasas_fw_cmds_outstanding_show(struct device *cdev,
  2696. struct device_attribute *attr, char *buf)
  2697. {
  2698. struct Scsi_Host *shost = class_to_shost(cdev);
  2699. struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
  2700. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
  2701. }
  2702. static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
  2703. megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
  2704. static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
  2705. megasas_fw_crash_buffer_size_show, NULL);
  2706. static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
  2707. megasas_fw_crash_state_show, megasas_fw_crash_state_store);
  2708. static DEVICE_ATTR(page_size, S_IRUGO,
  2709. megasas_page_size_show, NULL);
  2710. static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
  2711. megasas_ldio_outstanding_show, NULL);
  2712. static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
  2713. megasas_fw_cmds_outstanding_show, NULL);
  2714. struct device_attribute *megaraid_host_attrs[] = {
  2715. &dev_attr_fw_crash_buffer_size,
  2716. &dev_attr_fw_crash_buffer,
  2717. &dev_attr_fw_crash_state,
  2718. &dev_attr_page_size,
  2719. &dev_attr_ldio_outstanding,
  2720. &dev_attr_fw_cmds_outstanding,
  2721. NULL,
  2722. };
  2723. /*
  2724. * Scsi host template for megaraid_sas driver
  2725. */
  2726. static struct scsi_host_template megasas_template = {
  2727. .module = THIS_MODULE,
  2728. .name = "Avago SAS based MegaRAID driver",
  2729. .proc_name = "megaraid_sas",
  2730. .slave_configure = megasas_slave_configure,
  2731. .slave_alloc = megasas_slave_alloc,
  2732. .slave_destroy = megasas_slave_destroy,
  2733. .queuecommand = megasas_queue_command,
  2734. .eh_target_reset_handler = megasas_reset_target,
  2735. .eh_abort_handler = megasas_task_abort,
  2736. .eh_host_reset_handler = megasas_reset_bus_host,
  2737. .eh_timed_out = megasas_reset_timer,
  2738. .shost_attrs = megaraid_host_attrs,
  2739. .bios_param = megasas_bios_param,
  2740. .use_clustering = ENABLE_CLUSTERING,
  2741. .change_queue_depth = scsi_change_queue_depth,
  2742. .no_write_same = 1,
  2743. };
  2744. /**
  2745. * megasas_complete_int_cmd - Completes an internal command
  2746. * @instance: Adapter soft state
  2747. * @cmd: Command to be completed
  2748. *
  2749. * The megasas_issue_blocked_cmd() function waits for a command to complete
  2750. * after it issues a command. This function wakes up that waiting routine by
  2751. * calling wake_up() on the wait queue.
  2752. */
  2753. static void
  2754. megasas_complete_int_cmd(struct megasas_instance *instance,
  2755. struct megasas_cmd *cmd)
  2756. {
  2757. cmd->cmd_status_drv = cmd->frame->io.cmd_status;
  2758. wake_up(&instance->int_cmd_wait_q);
  2759. }
  2760. /**
  2761. * megasas_complete_abort - Completes aborting a command
  2762. * @instance: Adapter soft state
  2763. * @cmd: Cmd that was issued to abort another cmd
  2764. *
  2765. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  2766. * after it issues an abort on a previously issued command. This function
  2767. * wakes up all functions waiting on the same wait queue.
  2768. */
  2769. static void
  2770. megasas_complete_abort(struct megasas_instance *instance,
  2771. struct megasas_cmd *cmd)
  2772. {
  2773. if (cmd->sync_cmd) {
  2774. cmd->sync_cmd = 0;
  2775. cmd->cmd_status_drv = 0;
  2776. wake_up(&instance->abort_cmd_wait_q);
  2777. }
  2778. }
  2779. /**
  2780. * megasas_complete_cmd - Completes a command
  2781. * @instance: Adapter soft state
  2782. * @cmd: Command to be completed
  2783. * @alt_status: If non-zero, use this value as status to
  2784. * SCSI mid-layer instead of the value returned
  2785. * by the FW. This should be used if caller wants
  2786. * an alternate status (as in the case of aborted
  2787. * commands)
  2788. */
  2789. void
  2790. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  2791. u8 alt_status)
  2792. {
  2793. int exception = 0;
  2794. struct megasas_header *hdr = &cmd->frame->hdr;
  2795. unsigned long flags;
  2796. struct fusion_context *fusion = instance->ctrl_context;
  2797. u32 opcode, status;
  2798. /* flag for the retry reset */
  2799. cmd->retry_for_fw_reset = 0;
  2800. if (cmd->scmd)
  2801. cmd->scmd->SCp.ptr = NULL;
  2802. switch (hdr->cmd) {
  2803. case MFI_CMD_INVALID:
  2804. /* Some older 1068 controller FW may keep a pended
  2805. MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
  2806. when booting the kdump kernel. Ignore this command to
  2807. prevent a kernel panic on shutdown of the kdump kernel. */
  2808. dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
  2809. "completed\n");
  2810. dev_warn(&instance->pdev->dev, "If you have a controller "
  2811. "other than PERC5, please upgrade your firmware\n");
  2812. break;
  2813. case MFI_CMD_PD_SCSI_IO:
  2814. case MFI_CMD_LD_SCSI_IO:
  2815. /*
  2816. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  2817. * issued either through an IO path or an IOCTL path. If it
  2818. * was via IOCTL, we will send it to internal completion.
  2819. */
  2820. if (cmd->sync_cmd) {
  2821. cmd->sync_cmd = 0;
  2822. megasas_complete_int_cmd(instance, cmd);
  2823. break;
  2824. }
  2825. case MFI_CMD_LD_READ:
  2826. case MFI_CMD_LD_WRITE:
  2827. if (alt_status) {
  2828. cmd->scmd->result = alt_status << 16;
  2829. exception = 1;
  2830. }
  2831. if (exception) {
  2832. atomic_dec(&instance->fw_outstanding);
  2833. scsi_dma_unmap(cmd->scmd);
  2834. cmd->scmd->scsi_done(cmd->scmd);
  2835. megasas_return_cmd(instance, cmd);
  2836. break;
  2837. }
  2838. switch (hdr->cmd_status) {
  2839. case MFI_STAT_OK:
  2840. cmd->scmd->result = DID_OK << 16;
  2841. break;
  2842. case MFI_STAT_SCSI_IO_FAILED:
  2843. case MFI_STAT_LD_INIT_IN_PROGRESS:
  2844. cmd->scmd->result =
  2845. (DID_ERROR << 16) | hdr->scsi_status;
  2846. break;
  2847. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  2848. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  2849. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  2850. memset(cmd->scmd->sense_buffer, 0,
  2851. SCSI_SENSE_BUFFERSIZE);
  2852. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  2853. hdr->sense_len);
  2854. cmd->scmd->result |= DRIVER_SENSE << 24;
  2855. }
  2856. break;
  2857. case MFI_STAT_LD_OFFLINE:
  2858. case MFI_STAT_DEVICE_NOT_FOUND:
  2859. cmd->scmd->result = DID_BAD_TARGET << 16;
  2860. break;
  2861. default:
  2862. dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
  2863. hdr->cmd_status);
  2864. cmd->scmd->result = DID_ERROR << 16;
  2865. break;
  2866. }
  2867. atomic_dec(&instance->fw_outstanding);
  2868. scsi_dma_unmap(cmd->scmd);
  2869. cmd->scmd->scsi_done(cmd->scmd);
  2870. megasas_return_cmd(instance, cmd);
  2871. break;
  2872. case MFI_CMD_SMP:
  2873. case MFI_CMD_STP:
  2874. case MFI_CMD_NVME:
  2875. megasas_complete_int_cmd(instance, cmd);
  2876. break;
  2877. case MFI_CMD_DCMD:
  2878. opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
  2879. /* Check for LD map update */
  2880. if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
  2881. && (cmd->frame->dcmd.mbox.b[1] == 1)) {
  2882. fusion->fast_path_io = 0;
  2883. spin_lock_irqsave(instance->host->host_lock, flags);
  2884. status = cmd->frame->hdr.cmd_status;
  2885. instance->map_update_cmd = NULL;
  2886. if (status != MFI_STAT_OK) {
  2887. if (status != MFI_STAT_NOT_FOUND)
  2888. dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
  2889. cmd->frame->hdr.cmd_status);
  2890. else {
  2891. megasas_return_cmd(instance, cmd);
  2892. spin_unlock_irqrestore(
  2893. instance->host->host_lock,
  2894. flags);
  2895. break;
  2896. }
  2897. }
  2898. megasas_return_cmd(instance, cmd);
  2899. /*
  2900. * Set fast path IO to ZERO.
  2901. * Validate Map will set proper value.
  2902. * Meanwhile all IOs will go as LD IO.
  2903. */
  2904. if (status == MFI_STAT_OK &&
  2905. (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
  2906. instance->map_id++;
  2907. fusion->fast_path_io = 1;
  2908. } else {
  2909. fusion->fast_path_io = 0;
  2910. }
  2911. megasas_sync_map_info(instance);
  2912. spin_unlock_irqrestore(instance->host->host_lock,
  2913. flags);
  2914. break;
  2915. }
  2916. if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  2917. opcode == MR_DCMD_CTRL_EVENT_GET) {
  2918. spin_lock_irqsave(&poll_aen_lock, flags);
  2919. megasas_poll_wait_aen = 0;
  2920. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2921. }
  2922. /* FW has an updated PD sequence */
  2923. if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
  2924. (cmd->frame->dcmd.mbox.b[0] == 1)) {
  2925. spin_lock_irqsave(instance->host->host_lock, flags);
  2926. status = cmd->frame->hdr.cmd_status;
  2927. instance->jbod_seq_cmd = NULL;
  2928. megasas_return_cmd(instance, cmd);
  2929. if (status == MFI_STAT_OK) {
  2930. instance->pd_seq_map_id++;
  2931. /* Re-register a pd sync seq num cmd */
  2932. if (megasas_sync_pd_seq_num(instance, true))
  2933. instance->use_seqnum_jbod_fp = false;
  2934. } else
  2935. instance->use_seqnum_jbod_fp = false;
  2936. spin_unlock_irqrestore(instance->host->host_lock, flags);
  2937. break;
  2938. }
  2939. /*
  2940. * See if got an event notification
  2941. */
  2942. if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
  2943. megasas_service_aen(instance, cmd);
  2944. else
  2945. megasas_complete_int_cmd(instance, cmd);
  2946. break;
  2947. case MFI_CMD_ABORT:
  2948. /*
  2949. * Cmd issued to abort another cmd returned
  2950. */
  2951. megasas_complete_abort(instance, cmd);
  2952. break;
  2953. default:
  2954. dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
  2955. hdr->cmd);
  2956. megasas_complete_int_cmd(instance, cmd);
  2957. break;
  2958. }
  2959. }
  2960. /**
  2961. * megasas_issue_pending_cmds_again - issue all pending cmds
  2962. * in FW again because of the fw reset
  2963. * @instance: Adapter soft state
  2964. */
  2965. static inline void
  2966. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  2967. {
  2968. struct megasas_cmd *cmd;
  2969. struct list_head clist_local;
  2970. union megasas_evt_class_locale class_locale;
  2971. unsigned long flags;
  2972. u32 seq_num;
  2973. INIT_LIST_HEAD(&clist_local);
  2974. spin_lock_irqsave(&instance->hba_lock, flags);
  2975. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  2976. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2977. while (!list_empty(&clist_local)) {
  2978. cmd = list_entry((&clist_local)->next,
  2979. struct megasas_cmd, list);
  2980. list_del_init(&cmd->list);
  2981. if (cmd->sync_cmd || cmd->scmd) {
  2982. dev_notice(&instance->pdev->dev, "command %p, %p:%d"
  2983. "detected to be pending while HBA reset\n",
  2984. cmd, cmd->scmd, cmd->sync_cmd);
  2985. cmd->retry_for_fw_reset++;
  2986. if (cmd->retry_for_fw_reset == 3) {
  2987. dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
  2988. "was tried multiple times during reset."
  2989. "Shutting down the HBA\n",
  2990. cmd, cmd->scmd, cmd->sync_cmd);
  2991. instance->instancet->disable_intr(instance);
  2992. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2993. megaraid_sas_kill_hba(instance);
  2994. return;
  2995. }
  2996. }
  2997. if (cmd->sync_cmd == 1) {
  2998. if (cmd->scmd) {
  2999. dev_notice(&instance->pdev->dev, "unexpected"
  3000. "cmd attached to internal command!\n");
  3001. }
  3002. dev_notice(&instance->pdev->dev, "%p synchronous cmd"
  3003. "on the internal reset queue,"
  3004. "issue it again.\n", cmd);
  3005. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  3006. instance->instancet->fire_cmd(instance,
  3007. cmd->frame_phys_addr,
  3008. 0, instance->reg_set);
  3009. } else if (cmd->scmd) {
  3010. dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
  3011. "detected on the internal queue, issue again.\n",
  3012. cmd, cmd->scmd->cmnd[0]);
  3013. atomic_inc(&instance->fw_outstanding);
  3014. instance->instancet->fire_cmd(instance,
  3015. cmd->frame_phys_addr,
  3016. cmd->frame_count-1, instance->reg_set);
  3017. } else {
  3018. dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
  3019. "internal reset defer list while re-issue!!\n",
  3020. cmd);
  3021. }
  3022. }
  3023. if (instance->aen_cmd) {
  3024. dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
  3025. megasas_return_cmd(instance, instance->aen_cmd);
  3026. instance->aen_cmd = NULL;
  3027. }
  3028. /*
  3029. * Initiate AEN (Asynchronous Event Notification)
  3030. */
  3031. seq_num = instance->last_seq_num;
  3032. class_locale.members.reserved = 0;
  3033. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3034. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3035. megasas_register_aen(instance, seq_num, class_locale.word);
  3036. }
  3037. /**
  3038. * Move the internal reset pending commands to a deferred queue.
  3039. *
  3040. * We move the commands pending at internal reset time to a
  3041. * pending queue. This queue would be flushed after successful
  3042. * completion of the internal reset sequence. if the internal reset
  3043. * did not complete in time, the kernel reset handler would flush
  3044. * these commands.
  3045. **/
  3046. static void
  3047. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  3048. {
  3049. struct megasas_cmd *cmd;
  3050. int i;
  3051. u16 max_cmd = instance->max_fw_cmds;
  3052. u32 defer_index;
  3053. unsigned long flags;
  3054. defer_index = 0;
  3055. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  3056. for (i = 0; i < max_cmd; i++) {
  3057. cmd = instance->cmd_list[i];
  3058. if (cmd->sync_cmd == 1 || cmd->scmd) {
  3059. dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
  3060. "on the defer queue as internal\n",
  3061. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  3062. if (!list_empty(&cmd->list)) {
  3063. dev_notice(&instance->pdev->dev, "ERROR while"
  3064. " moving this cmd:%p, %d %p, it was"
  3065. "discovered on some list?\n",
  3066. cmd, cmd->sync_cmd, cmd->scmd);
  3067. list_del_init(&cmd->list);
  3068. }
  3069. defer_index++;
  3070. list_add_tail(&cmd->list,
  3071. &instance->internal_reset_pending_q);
  3072. }
  3073. }
  3074. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  3075. }
  3076. static void
  3077. process_fw_state_change_wq(struct work_struct *work)
  3078. {
  3079. struct megasas_instance *instance =
  3080. container_of(work, struct megasas_instance, work_init);
  3081. u32 wait;
  3082. unsigned long flags;
  3083. if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
  3084. dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
  3085. atomic_read(&instance->adprecovery));
  3086. return ;
  3087. }
  3088. if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
  3089. dev_notice(&instance->pdev->dev, "FW detected to be in fault"
  3090. "state, restarting it...\n");
  3091. instance->instancet->disable_intr(instance);
  3092. atomic_set(&instance->fw_outstanding, 0);
  3093. atomic_set(&instance->fw_reset_no_pci_access, 1);
  3094. instance->instancet->adp_reset(instance, instance->reg_set);
  3095. atomic_set(&instance->fw_reset_no_pci_access, 0);
  3096. dev_notice(&instance->pdev->dev, "FW restarted successfully,"
  3097. "initiating next stage...\n");
  3098. dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
  3099. "state 2 starting...\n");
  3100. /* waiting for about 20 second before start the second init */
  3101. for (wait = 0; wait < 30; wait++) {
  3102. msleep(1000);
  3103. }
  3104. if (megasas_transition_to_ready(instance, 1)) {
  3105. dev_notice(&instance->pdev->dev, "adapter not ready\n");
  3106. atomic_set(&instance->fw_reset_no_pci_access, 1);
  3107. megaraid_sas_kill_hba(instance);
  3108. return ;
  3109. }
  3110. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  3111. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  3112. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  3113. ) {
  3114. *instance->consumer = *instance->producer;
  3115. } else {
  3116. *instance->consumer = 0;
  3117. *instance->producer = 0;
  3118. }
  3119. megasas_issue_init_mfi(instance);
  3120. spin_lock_irqsave(&instance->hba_lock, flags);
  3121. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  3122. spin_unlock_irqrestore(&instance->hba_lock, flags);
  3123. instance->instancet->enable_intr(instance);
  3124. megasas_issue_pending_cmds_again(instance);
  3125. instance->issuepend_done = 1;
  3126. }
  3127. }
  3128. /**
  3129. * megasas_deplete_reply_queue - Processes all completed commands
  3130. * @instance: Adapter soft state
  3131. * @alt_status: Alternate status to be returned to
  3132. * SCSI mid-layer instead of the status
  3133. * returned by the FW
  3134. * Note: this must be called with hba lock held
  3135. */
  3136. static int
  3137. megasas_deplete_reply_queue(struct megasas_instance *instance,
  3138. u8 alt_status)
  3139. {
  3140. u32 mfiStatus;
  3141. u32 fw_state;
  3142. if ((mfiStatus = instance->instancet->check_reset(instance,
  3143. instance->reg_set)) == 1) {
  3144. return IRQ_HANDLED;
  3145. }
  3146. if ((mfiStatus = instance->instancet->clear_intr(
  3147. instance->reg_set)
  3148. ) == 0) {
  3149. /* Hardware may not set outbound_intr_status in MSI-X mode */
  3150. if (!instance->msix_vectors)
  3151. return IRQ_NONE;
  3152. }
  3153. instance->mfiStatus = mfiStatus;
  3154. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  3155. fw_state = instance->instancet->read_fw_status_reg(
  3156. instance->reg_set) & MFI_STATE_MASK;
  3157. if (fw_state != MFI_STATE_FAULT) {
  3158. dev_notice(&instance->pdev->dev, "fw state:%x\n",
  3159. fw_state);
  3160. }
  3161. if ((fw_state == MFI_STATE_FAULT) &&
  3162. (instance->disableOnlineCtrlReset == 0)) {
  3163. dev_notice(&instance->pdev->dev, "wait adp restart\n");
  3164. if ((instance->pdev->device ==
  3165. PCI_DEVICE_ID_LSI_SAS1064R) ||
  3166. (instance->pdev->device ==
  3167. PCI_DEVICE_ID_DELL_PERC5) ||
  3168. (instance->pdev->device ==
  3169. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  3170. *instance->consumer =
  3171. cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  3172. }
  3173. instance->instancet->disable_intr(instance);
  3174. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  3175. instance->issuepend_done = 0;
  3176. atomic_set(&instance->fw_outstanding, 0);
  3177. megasas_internal_reset_defer_cmds(instance);
  3178. dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
  3179. fw_state, atomic_read(&instance->adprecovery));
  3180. schedule_work(&instance->work_init);
  3181. return IRQ_HANDLED;
  3182. } else {
  3183. dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
  3184. fw_state, instance->disableOnlineCtrlReset);
  3185. }
  3186. }
  3187. tasklet_schedule(&instance->isr_tasklet);
  3188. return IRQ_HANDLED;
  3189. }
  3190. /**
  3191. * megasas_isr - isr entry point
  3192. */
  3193. static irqreturn_t megasas_isr(int irq, void *devp)
  3194. {
  3195. struct megasas_irq_context *irq_context = devp;
  3196. struct megasas_instance *instance = irq_context->instance;
  3197. unsigned long flags;
  3198. irqreturn_t rc;
  3199. if (atomic_read(&instance->fw_reset_no_pci_access))
  3200. return IRQ_HANDLED;
  3201. spin_lock_irqsave(&instance->hba_lock, flags);
  3202. rc = megasas_deplete_reply_queue(instance, DID_OK);
  3203. spin_unlock_irqrestore(&instance->hba_lock, flags);
  3204. return rc;
  3205. }
  3206. /**
  3207. * megasas_transition_to_ready - Move the FW to READY state
  3208. * @instance: Adapter soft state
  3209. *
  3210. * During the initialization, FW passes can potentially be in any one of
  3211. * several possible states. If the FW in operational, waiting-for-handshake
  3212. * states, driver must take steps to bring it to ready state. Otherwise, it
  3213. * has to wait for the ready state.
  3214. */
  3215. int
  3216. megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
  3217. {
  3218. int i;
  3219. u8 max_wait;
  3220. u32 fw_state;
  3221. u32 cur_state;
  3222. u32 abs_state, curr_abs_state;
  3223. abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
  3224. fw_state = abs_state & MFI_STATE_MASK;
  3225. if (fw_state != MFI_STATE_READY)
  3226. dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
  3227. " state\n");
  3228. while (fw_state != MFI_STATE_READY) {
  3229. switch (fw_state) {
  3230. case MFI_STATE_FAULT:
  3231. dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
  3232. if (ocr) {
  3233. max_wait = MEGASAS_RESET_WAIT_TIME;
  3234. cur_state = MFI_STATE_FAULT;
  3235. break;
  3236. } else
  3237. return -ENODEV;
  3238. case MFI_STATE_WAIT_HANDSHAKE:
  3239. /*
  3240. * Set the CLR bit in inbound doorbell
  3241. */
  3242. if ((instance->pdev->device ==
  3243. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3244. (instance->pdev->device ==
  3245. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3246. (instance->adapter_type != MFI_SERIES))
  3247. writel(
  3248. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  3249. &instance->reg_set->doorbell);
  3250. else
  3251. writel(
  3252. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  3253. &instance->reg_set->inbound_doorbell);
  3254. max_wait = MEGASAS_RESET_WAIT_TIME;
  3255. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  3256. break;
  3257. case MFI_STATE_BOOT_MESSAGE_PENDING:
  3258. if ((instance->pdev->device ==
  3259. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3260. (instance->pdev->device ==
  3261. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3262. (instance->adapter_type != MFI_SERIES))
  3263. writel(MFI_INIT_HOTPLUG,
  3264. &instance->reg_set->doorbell);
  3265. else
  3266. writel(MFI_INIT_HOTPLUG,
  3267. &instance->reg_set->inbound_doorbell);
  3268. max_wait = MEGASAS_RESET_WAIT_TIME;
  3269. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  3270. break;
  3271. case MFI_STATE_OPERATIONAL:
  3272. /*
  3273. * Bring it to READY state; assuming max wait 10 secs
  3274. */
  3275. instance->instancet->disable_intr(instance);
  3276. if ((instance->pdev->device ==
  3277. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3278. (instance->pdev->device ==
  3279. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3280. (instance->adapter_type != MFI_SERIES)) {
  3281. writel(MFI_RESET_FLAGS,
  3282. &instance->reg_set->doorbell);
  3283. if (instance->adapter_type != MFI_SERIES) {
  3284. for (i = 0; i < (10 * 1000); i += 20) {
  3285. if (readl(
  3286. &instance->
  3287. reg_set->
  3288. doorbell) & 1)
  3289. msleep(20);
  3290. else
  3291. break;
  3292. }
  3293. }
  3294. } else
  3295. writel(MFI_RESET_FLAGS,
  3296. &instance->reg_set->inbound_doorbell);
  3297. max_wait = MEGASAS_RESET_WAIT_TIME;
  3298. cur_state = MFI_STATE_OPERATIONAL;
  3299. break;
  3300. case MFI_STATE_UNDEFINED:
  3301. /*
  3302. * This state should not last for more than 2 seconds
  3303. */
  3304. max_wait = MEGASAS_RESET_WAIT_TIME;
  3305. cur_state = MFI_STATE_UNDEFINED;
  3306. break;
  3307. case MFI_STATE_BB_INIT:
  3308. max_wait = MEGASAS_RESET_WAIT_TIME;
  3309. cur_state = MFI_STATE_BB_INIT;
  3310. break;
  3311. case MFI_STATE_FW_INIT:
  3312. max_wait = MEGASAS_RESET_WAIT_TIME;
  3313. cur_state = MFI_STATE_FW_INIT;
  3314. break;
  3315. case MFI_STATE_FW_INIT_2:
  3316. max_wait = MEGASAS_RESET_WAIT_TIME;
  3317. cur_state = MFI_STATE_FW_INIT_2;
  3318. break;
  3319. case MFI_STATE_DEVICE_SCAN:
  3320. max_wait = MEGASAS_RESET_WAIT_TIME;
  3321. cur_state = MFI_STATE_DEVICE_SCAN;
  3322. break;
  3323. case MFI_STATE_FLUSH_CACHE:
  3324. max_wait = MEGASAS_RESET_WAIT_TIME;
  3325. cur_state = MFI_STATE_FLUSH_CACHE;
  3326. break;
  3327. default:
  3328. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
  3329. fw_state);
  3330. return -ENODEV;
  3331. }
  3332. /*
  3333. * The cur_state should not last for more than max_wait secs
  3334. */
  3335. for (i = 0; i < max_wait * 50; i++) {
  3336. curr_abs_state = instance->instancet->
  3337. read_fw_status_reg(instance->reg_set);
  3338. if (abs_state == curr_abs_state) {
  3339. msleep(20);
  3340. } else
  3341. break;
  3342. }
  3343. /*
  3344. * Return error if fw_state hasn't changed after max_wait
  3345. */
  3346. if (curr_abs_state == abs_state) {
  3347. dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
  3348. "in %d secs\n", fw_state, max_wait);
  3349. return -ENODEV;
  3350. }
  3351. abs_state = curr_abs_state;
  3352. fw_state = curr_abs_state & MFI_STATE_MASK;
  3353. }
  3354. dev_info(&instance->pdev->dev, "FW now in Ready state\n");
  3355. return 0;
  3356. }
  3357. /**
  3358. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  3359. * @instance: Adapter soft state
  3360. */
  3361. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  3362. {
  3363. int i;
  3364. u16 max_cmd = instance->max_mfi_cmds;
  3365. struct megasas_cmd *cmd;
  3366. if (!instance->frame_dma_pool)
  3367. return;
  3368. /*
  3369. * Return all frames to pool
  3370. */
  3371. for (i = 0; i < max_cmd; i++) {
  3372. cmd = instance->cmd_list[i];
  3373. if (cmd->frame)
  3374. dma_pool_free(instance->frame_dma_pool, cmd->frame,
  3375. cmd->frame_phys_addr);
  3376. if (cmd->sense)
  3377. dma_pool_free(instance->sense_dma_pool, cmd->sense,
  3378. cmd->sense_phys_addr);
  3379. }
  3380. /*
  3381. * Now destroy the pool itself
  3382. */
  3383. dma_pool_destroy(instance->frame_dma_pool);
  3384. dma_pool_destroy(instance->sense_dma_pool);
  3385. instance->frame_dma_pool = NULL;
  3386. instance->sense_dma_pool = NULL;
  3387. }
  3388. /**
  3389. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  3390. * @instance: Adapter soft state
  3391. *
  3392. * Each command packet has an embedded DMA memory buffer that is used for
  3393. * filling MFI frame and the SG list that immediately follows the frame. This
  3394. * function creates those DMA memory buffers for each command packet by using
  3395. * PCI pool facility.
  3396. */
  3397. static int megasas_create_frame_pool(struct megasas_instance *instance)
  3398. {
  3399. int i;
  3400. u16 max_cmd;
  3401. u32 sge_sz;
  3402. u32 frame_count;
  3403. struct megasas_cmd *cmd;
  3404. max_cmd = instance->max_mfi_cmds;
  3405. /*
  3406. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  3407. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  3408. */
  3409. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  3410. sizeof(struct megasas_sge32);
  3411. if (instance->flag_ieee)
  3412. sge_sz = sizeof(struct megasas_sge_skinny);
  3413. /*
  3414. * For MFI controllers.
  3415. * max_num_sge = 60
  3416. * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
  3417. * Total 960 byte (15 MFI frame of 64 byte)
  3418. *
  3419. * Fusion adapter require only 3 extra frame.
  3420. * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
  3421. * max_sge_sz = 12 byte (sizeof megasas_sge64)
  3422. * Total 192 byte (3 MFI frame of 64 byte)
  3423. */
  3424. frame_count = (instance->adapter_type == MFI_SERIES) ?
  3425. (15 + 1) : (3 + 1);
  3426. instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
  3427. /*
  3428. * Use DMA pool facility provided by PCI layer
  3429. */
  3430. instance->frame_dma_pool = dma_pool_create("megasas frame pool",
  3431. &instance->pdev->dev,
  3432. instance->mfi_frame_size, 256, 0);
  3433. if (!instance->frame_dma_pool) {
  3434. dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
  3435. return -ENOMEM;
  3436. }
  3437. instance->sense_dma_pool = dma_pool_create("megasas sense pool",
  3438. &instance->pdev->dev, 128,
  3439. 4, 0);
  3440. if (!instance->sense_dma_pool) {
  3441. dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
  3442. dma_pool_destroy(instance->frame_dma_pool);
  3443. instance->frame_dma_pool = NULL;
  3444. return -ENOMEM;
  3445. }
  3446. /*
  3447. * Allocate and attach a frame to each of the commands in cmd_list.
  3448. * By making cmd->index as the context instead of the &cmd, we can
  3449. * always use 32bit context regardless of the architecture
  3450. */
  3451. for (i = 0; i < max_cmd; i++) {
  3452. cmd = instance->cmd_list[i];
  3453. cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
  3454. GFP_KERNEL, &cmd->frame_phys_addr);
  3455. cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
  3456. GFP_KERNEL, &cmd->sense_phys_addr);
  3457. /*
  3458. * megasas_teardown_frame_pool() takes care of freeing
  3459. * whatever has been allocated
  3460. */
  3461. if (!cmd->frame || !cmd->sense) {
  3462. dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
  3463. megasas_teardown_frame_pool(instance);
  3464. return -ENOMEM;
  3465. }
  3466. cmd->frame->io.context = cpu_to_le32(cmd->index);
  3467. cmd->frame->io.pad_0 = 0;
  3468. if ((instance->adapter_type == MFI_SERIES) && reset_devices)
  3469. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  3470. }
  3471. return 0;
  3472. }
  3473. /**
  3474. * megasas_free_cmds - Free all the cmds in the free cmd pool
  3475. * @instance: Adapter soft state
  3476. */
  3477. void megasas_free_cmds(struct megasas_instance *instance)
  3478. {
  3479. int i;
  3480. /* First free the MFI frame pool */
  3481. megasas_teardown_frame_pool(instance);
  3482. /* Free all the commands in the cmd_list */
  3483. for (i = 0; i < instance->max_mfi_cmds; i++)
  3484. kfree(instance->cmd_list[i]);
  3485. /* Free the cmd_list buffer itself */
  3486. kfree(instance->cmd_list);
  3487. instance->cmd_list = NULL;
  3488. INIT_LIST_HEAD(&instance->cmd_pool);
  3489. }
  3490. /**
  3491. * megasas_alloc_cmds - Allocates the command packets
  3492. * @instance: Adapter soft state
  3493. *
  3494. * Each command that is issued to the FW, whether IO commands from the OS or
  3495. * internal commands like IOCTLs, are wrapped in local data structure called
  3496. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  3497. * the FW.
  3498. *
  3499. * Each frame has a 32-bit field called context (tag). This context is used
  3500. * to get back the megasas_cmd from the frame when a frame gets completed in
  3501. * the ISR. Typically the address of the megasas_cmd itself would be used as
  3502. * the context. But we wanted to keep the differences between 32 and 64 bit
  3503. * systems to the mininum. We always use 32 bit integers for the context. In
  3504. * this driver, the 32 bit values are the indices into an array cmd_list.
  3505. * This array is used only to look up the megasas_cmd given the context. The
  3506. * free commands themselves are maintained in a linked list called cmd_pool.
  3507. */
  3508. int megasas_alloc_cmds(struct megasas_instance *instance)
  3509. {
  3510. int i;
  3511. int j;
  3512. u16 max_cmd;
  3513. struct megasas_cmd *cmd;
  3514. max_cmd = instance->max_mfi_cmds;
  3515. /*
  3516. * instance->cmd_list is an array of struct megasas_cmd pointers.
  3517. * Allocate the dynamic array first and then allocate individual
  3518. * commands.
  3519. */
  3520. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  3521. if (!instance->cmd_list) {
  3522. dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
  3523. return -ENOMEM;
  3524. }
  3525. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  3526. for (i = 0; i < max_cmd; i++) {
  3527. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  3528. GFP_KERNEL);
  3529. if (!instance->cmd_list[i]) {
  3530. for (j = 0; j < i; j++)
  3531. kfree(instance->cmd_list[j]);
  3532. kfree(instance->cmd_list);
  3533. instance->cmd_list = NULL;
  3534. return -ENOMEM;
  3535. }
  3536. }
  3537. for (i = 0; i < max_cmd; i++) {
  3538. cmd = instance->cmd_list[i];
  3539. memset(cmd, 0, sizeof(struct megasas_cmd));
  3540. cmd->index = i;
  3541. cmd->scmd = NULL;
  3542. cmd->instance = instance;
  3543. list_add_tail(&cmd->list, &instance->cmd_pool);
  3544. }
  3545. /*
  3546. * Create a frame pool and assign one frame to each cmd
  3547. */
  3548. if (megasas_create_frame_pool(instance)) {
  3549. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
  3550. megasas_free_cmds(instance);
  3551. return -ENOMEM;
  3552. }
  3553. return 0;
  3554. }
  3555. /*
  3556. * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
  3557. * @instance: Adapter soft state
  3558. *
  3559. * Return 0 for only Fusion adapter, if driver load/unload is not in progress
  3560. * or FW is not under OCR.
  3561. */
  3562. inline int
  3563. dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
  3564. if (instance->adapter_type == MFI_SERIES)
  3565. return KILL_ADAPTER;
  3566. else if (instance->unload ||
  3567. test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
  3568. &instance->reset_flags))
  3569. return IGNORE_TIMEOUT;
  3570. else
  3571. return INITIATE_OCR;
  3572. }
  3573. static void
  3574. megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
  3575. {
  3576. int ret;
  3577. struct megasas_cmd *cmd;
  3578. struct megasas_dcmd_frame *dcmd;
  3579. struct MR_PRIV_DEVICE *mr_device_priv_data;
  3580. u16 device_id = 0;
  3581. device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
  3582. cmd = megasas_get_cmd(instance);
  3583. if (!cmd) {
  3584. dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
  3585. return;
  3586. }
  3587. dcmd = &cmd->frame->dcmd;
  3588. memset(instance->pd_info, 0, sizeof(*instance->pd_info));
  3589. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3590. dcmd->mbox.s[0] = cpu_to_le16(device_id);
  3591. dcmd->cmd = MFI_CMD_DCMD;
  3592. dcmd->cmd_status = 0xFF;
  3593. dcmd->sge_count = 1;
  3594. dcmd->flags = MFI_FRAME_DIR_READ;
  3595. dcmd->timeout = 0;
  3596. dcmd->pad_0 = 0;
  3597. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
  3598. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
  3599. megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
  3600. sizeof(struct MR_PD_INFO));
  3601. if ((instance->adapter_type != MFI_SERIES) &&
  3602. !instance->mask_interrupts)
  3603. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3604. else
  3605. ret = megasas_issue_polled(instance, cmd);
  3606. switch (ret) {
  3607. case DCMD_SUCCESS:
  3608. mr_device_priv_data = sdev->hostdata;
  3609. le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
  3610. mr_device_priv_data->interface_type =
  3611. instance->pd_info->state.ddf.pdType.intf;
  3612. break;
  3613. case DCMD_TIMEOUT:
  3614. switch (dcmd_timeout_ocr_possible(instance)) {
  3615. case INITIATE_OCR:
  3616. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3617. megasas_reset_fusion(instance->host,
  3618. MFI_IO_TIMEOUT_OCR);
  3619. break;
  3620. case KILL_ADAPTER:
  3621. megaraid_sas_kill_hba(instance);
  3622. break;
  3623. case IGNORE_TIMEOUT:
  3624. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3625. __func__, __LINE__);
  3626. break;
  3627. }
  3628. break;
  3629. }
  3630. if (ret != DCMD_TIMEOUT)
  3631. megasas_return_cmd(instance, cmd);
  3632. return;
  3633. }
  3634. /*
  3635. * megasas_get_pd_list_info - Returns FW's pd_list structure
  3636. * @instance: Adapter soft state
  3637. * @pd_list: pd_list structure
  3638. *
  3639. * Issues an internal command (DCMD) to get the FW's controller PD
  3640. * list structure. This information is mainly used to find out SYSTEM
  3641. * supported by the FW.
  3642. */
  3643. static int
  3644. megasas_get_pd_list(struct megasas_instance *instance)
  3645. {
  3646. int ret = 0, pd_index = 0;
  3647. struct megasas_cmd *cmd;
  3648. struct megasas_dcmd_frame *dcmd;
  3649. struct MR_PD_LIST *ci;
  3650. struct MR_PD_ADDRESS *pd_addr;
  3651. dma_addr_t ci_h = 0;
  3652. if (instance->pd_list_not_supported) {
  3653. dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
  3654. "not supported by firmware\n");
  3655. return ret;
  3656. }
  3657. ci = instance->pd_list_buf;
  3658. ci_h = instance->pd_list_buf_h;
  3659. cmd = megasas_get_cmd(instance);
  3660. if (!cmd) {
  3661. dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
  3662. return -ENOMEM;
  3663. }
  3664. dcmd = &cmd->frame->dcmd;
  3665. memset(ci, 0, sizeof(*ci));
  3666. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3667. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  3668. dcmd->mbox.b[1] = 0;
  3669. dcmd->cmd = MFI_CMD_DCMD;
  3670. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3671. dcmd->sge_count = 1;
  3672. dcmd->flags = MFI_FRAME_DIR_READ;
  3673. dcmd->timeout = 0;
  3674. dcmd->pad_0 = 0;
  3675. dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  3676. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
  3677. megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
  3678. (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
  3679. if ((instance->adapter_type != MFI_SERIES) &&
  3680. !instance->mask_interrupts)
  3681. ret = megasas_issue_blocked_cmd(instance, cmd,
  3682. MFI_IO_TIMEOUT_SECS);
  3683. else
  3684. ret = megasas_issue_polled(instance, cmd);
  3685. switch (ret) {
  3686. case DCMD_FAILED:
  3687. dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
  3688. "failed/not supported by firmware\n");
  3689. if (instance->adapter_type != MFI_SERIES)
  3690. megaraid_sas_kill_hba(instance);
  3691. else
  3692. instance->pd_list_not_supported = 1;
  3693. break;
  3694. case DCMD_TIMEOUT:
  3695. switch (dcmd_timeout_ocr_possible(instance)) {
  3696. case INITIATE_OCR:
  3697. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3698. /*
  3699. * DCMD failed from AEN path.
  3700. * AEN path already hold reset_mutex to avoid PCI access
  3701. * while OCR is in progress.
  3702. */
  3703. mutex_unlock(&instance->reset_mutex);
  3704. megasas_reset_fusion(instance->host,
  3705. MFI_IO_TIMEOUT_OCR);
  3706. mutex_lock(&instance->reset_mutex);
  3707. break;
  3708. case KILL_ADAPTER:
  3709. megaraid_sas_kill_hba(instance);
  3710. break;
  3711. case IGNORE_TIMEOUT:
  3712. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
  3713. __func__, __LINE__);
  3714. break;
  3715. }
  3716. break;
  3717. case DCMD_SUCCESS:
  3718. pd_addr = ci->addr;
  3719. if ((le32_to_cpu(ci->count) >
  3720. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
  3721. break;
  3722. memset(instance->local_pd_list, 0,
  3723. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  3724. for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
  3725. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
  3726. le16_to_cpu(pd_addr->deviceId);
  3727. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
  3728. pd_addr->scsiDevType;
  3729. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
  3730. MR_PD_STATE_SYSTEM;
  3731. pd_addr++;
  3732. }
  3733. memcpy(instance->pd_list, instance->local_pd_list,
  3734. sizeof(instance->pd_list));
  3735. break;
  3736. }
  3737. if (ret != DCMD_TIMEOUT)
  3738. megasas_return_cmd(instance, cmd);
  3739. return ret;
  3740. }
  3741. /*
  3742. * megasas_get_ld_list_info - Returns FW's ld_list structure
  3743. * @instance: Adapter soft state
  3744. * @ld_list: ld_list structure
  3745. *
  3746. * Issues an internal command (DCMD) to get the FW's controller PD
  3747. * list structure. This information is mainly used to find out SYSTEM
  3748. * supported by the FW.
  3749. */
  3750. static int
  3751. megasas_get_ld_list(struct megasas_instance *instance)
  3752. {
  3753. int ret = 0, ld_index = 0, ids = 0;
  3754. struct megasas_cmd *cmd;
  3755. struct megasas_dcmd_frame *dcmd;
  3756. struct MR_LD_LIST *ci;
  3757. dma_addr_t ci_h = 0;
  3758. u32 ld_count;
  3759. ci = instance->ld_list_buf;
  3760. ci_h = instance->ld_list_buf_h;
  3761. cmd = megasas_get_cmd(instance);
  3762. if (!cmd) {
  3763. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
  3764. return -ENOMEM;
  3765. }
  3766. dcmd = &cmd->frame->dcmd;
  3767. memset(ci, 0, sizeof(*ci));
  3768. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3769. if (instance->supportmax256vd)
  3770. dcmd->mbox.b[0] = 1;
  3771. dcmd->cmd = MFI_CMD_DCMD;
  3772. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3773. dcmd->sge_count = 1;
  3774. dcmd->flags = MFI_FRAME_DIR_READ;
  3775. dcmd->timeout = 0;
  3776. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
  3777. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
  3778. dcmd->pad_0 = 0;
  3779. megasas_set_dma_settings(instance, dcmd, ci_h,
  3780. sizeof(struct MR_LD_LIST));
  3781. if ((instance->adapter_type != MFI_SERIES) &&
  3782. !instance->mask_interrupts)
  3783. ret = megasas_issue_blocked_cmd(instance, cmd,
  3784. MFI_IO_TIMEOUT_SECS);
  3785. else
  3786. ret = megasas_issue_polled(instance, cmd);
  3787. ld_count = le32_to_cpu(ci->ldCount);
  3788. switch (ret) {
  3789. case DCMD_FAILED:
  3790. megaraid_sas_kill_hba(instance);
  3791. break;
  3792. case DCMD_TIMEOUT:
  3793. switch (dcmd_timeout_ocr_possible(instance)) {
  3794. case INITIATE_OCR:
  3795. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3796. /*
  3797. * DCMD failed from AEN path.
  3798. * AEN path already hold reset_mutex to avoid PCI access
  3799. * while OCR is in progress.
  3800. */
  3801. mutex_unlock(&instance->reset_mutex);
  3802. megasas_reset_fusion(instance->host,
  3803. MFI_IO_TIMEOUT_OCR);
  3804. mutex_lock(&instance->reset_mutex);
  3805. break;
  3806. case KILL_ADAPTER:
  3807. megaraid_sas_kill_hba(instance);
  3808. break;
  3809. case IGNORE_TIMEOUT:
  3810. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3811. __func__, __LINE__);
  3812. break;
  3813. }
  3814. break;
  3815. case DCMD_SUCCESS:
  3816. if (ld_count > instance->fw_supported_vd_count)
  3817. break;
  3818. memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
  3819. for (ld_index = 0; ld_index < ld_count; ld_index++) {
  3820. if (ci->ldList[ld_index].state != 0) {
  3821. ids = ci->ldList[ld_index].ref.targetId;
  3822. instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
  3823. }
  3824. }
  3825. break;
  3826. }
  3827. if (ret != DCMD_TIMEOUT)
  3828. megasas_return_cmd(instance, cmd);
  3829. return ret;
  3830. }
  3831. /**
  3832. * megasas_ld_list_query - Returns FW's ld_list structure
  3833. * @instance: Adapter soft state
  3834. * @ld_list: ld_list structure
  3835. *
  3836. * Issues an internal command (DCMD) to get the FW's controller PD
  3837. * list structure. This information is mainly used to find out SYSTEM
  3838. * supported by the FW.
  3839. */
  3840. static int
  3841. megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
  3842. {
  3843. int ret = 0, ld_index = 0, ids = 0;
  3844. struct megasas_cmd *cmd;
  3845. struct megasas_dcmd_frame *dcmd;
  3846. struct MR_LD_TARGETID_LIST *ci;
  3847. dma_addr_t ci_h = 0;
  3848. u32 tgtid_count;
  3849. ci = instance->ld_targetid_list_buf;
  3850. ci_h = instance->ld_targetid_list_buf_h;
  3851. cmd = megasas_get_cmd(instance);
  3852. if (!cmd) {
  3853. dev_warn(&instance->pdev->dev,
  3854. "megasas_ld_list_query: Failed to get cmd\n");
  3855. return -ENOMEM;
  3856. }
  3857. dcmd = &cmd->frame->dcmd;
  3858. memset(ci, 0, sizeof(*ci));
  3859. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3860. dcmd->mbox.b[0] = query_type;
  3861. if (instance->supportmax256vd)
  3862. dcmd->mbox.b[2] = 1;
  3863. dcmd->cmd = MFI_CMD_DCMD;
  3864. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3865. dcmd->sge_count = 1;
  3866. dcmd->flags = MFI_FRAME_DIR_READ;
  3867. dcmd->timeout = 0;
  3868. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  3869. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
  3870. dcmd->pad_0 = 0;
  3871. megasas_set_dma_settings(instance, dcmd, ci_h,
  3872. sizeof(struct MR_LD_TARGETID_LIST));
  3873. if ((instance->adapter_type != MFI_SERIES) &&
  3874. !instance->mask_interrupts)
  3875. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3876. else
  3877. ret = megasas_issue_polled(instance, cmd);
  3878. switch (ret) {
  3879. case DCMD_FAILED:
  3880. dev_info(&instance->pdev->dev,
  3881. "DCMD not supported by firmware - %s %d\n",
  3882. __func__, __LINE__);
  3883. ret = megasas_get_ld_list(instance);
  3884. break;
  3885. case DCMD_TIMEOUT:
  3886. switch (dcmd_timeout_ocr_possible(instance)) {
  3887. case INITIATE_OCR:
  3888. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3889. /*
  3890. * DCMD failed from AEN path.
  3891. * AEN path already hold reset_mutex to avoid PCI access
  3892. * while OCR is in progress.
  3893. */
  3894. mutex_unlock(&instance->reset_mutex);
  3895. megasas_reset_fusion(instance->host,
  3896. MFI_IO_TIMEOUT_OCR);
  3897. mutex_lock(&instance->reset_mutex);
  3898. break;
  3899. case KILL_ADAPTER:
  3900. megaraid_sas_kill_hba(instance);
  3901. break;
  3902. case IGNORE_TIMEOUT:
  3903. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3904. __func__, __LINE__);
  3905. break;
  3906. }
  3907. break;
  3908. case DCMD_SUCCESS:
  3909. tgtid_count = le32_to_cpu(ci->count);
  3910. if ((tgtid_count > (instance->fw_supported_vd_count)))
  3911. break;
  3912. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3913. for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
  3914. ids = ci->targetId[ld_index];
  3915. instance->ld_ids[ids] = ci->targetId[ld_index];
  3916. }
  3917. break;
  3918. }
  3919. if (ret != DCMD_TIMEOUT)
  3920. megasas_return_cmd(instance, cmd);
  3921. return ret;
  3922. }
  3923. /*
  3924. * megasas_update_ext_vd_details : Update details w.r.t Extended VD
  3925. * instance : Controller's instance
  3926. */
  3927. static void megasas_update_ext_vd_details(struct megasas_instance *instance)
  3928. {
  3929. struct fusion_context *fusion;
  3930. u32 ventura_map_sz = 0;
  3931. fusion = instance->ctrl_context;
  3932. /* For MFI based controllers return dummy success */
  3933. if (!fusion)
  3934. return;
  3935. instance->supportmax256vd =
  3936. instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
  3937. /* Below is additional check to address future FW enhancement */
  3938. if (instance->ctrl_info_buf->max_lds > 64)
  3939. instance->supportmax256vd = 1;
  3940. instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
  3941. * MEGASAS_MAX_DEV_PER_CHANNEL;
  3942. instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
  3943. * MEGASAS_MAX_DEV_PER_CHANNEL;
  3944. if (instance->supportmax256vd) {
  3945. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
  3946. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  3947. } else {
  3948. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  3949. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  3950. }
  3951. dev_info(&instance->pdev->dev,
  3952. "firmware type\t: %s\n",
  3953. instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
  3954. "Legacy(64 VD) firmware");
  3955. if (instance->max_raid_mapsize) {
  3956. ventura_map_sz = instance->max_raid_mapsize *
  3957. MR_MIN_MAP_SIZE; /* 64k */
  3958. fusion->current_map_sz = ventura_map_sz;
  3959. fusion->max_map_sz = ventura_map_sz;
  3960. } else {
  3961. fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
  3962. (sizeof(struct MR_LD_SPAN_MAP) *
  3963. (instance->fw_supported_vd_count - 1));
  3964. fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
  3965. fusion->max_map_sz =
  3966. max(fusion->old_map_sz, fusion->new_map_sz);
  3967. if (instance->supportmax256vd)
  3968. fusion->current_map_sz = fusion->new_map_sz;
  3969. else
  3970. fusion->current_map_sz = fusion->old_map_sz;
  3971. }
  3972. /* irrespective of FW raid maps, driver raid map is constant */
  3973. fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
  3974. }
  3975. /**
  3976. * megasas_get_controller_info - Returns FW's controller structure
  3977. * @instance: Adapter soft state
  3978. *
  3979. * Issues an internal command (DCMD) to get the FW's controller structure.
  3980. * This information is mainly used to find out the maximum IO transfer per
  3981. * command supported by the FW.
  3982. */
  3983. int
  3984. megasas_get_ctrl_info(struct megasas_instance *instance)
  3985. {
  3986. int ret = 0;
  3987. struct megasas_cmd *cmd;
  3988. struct megasas_dcmd_frame *dcmd;
  3989. struct megasas_ctrl_info *ci;
  3990. dma_addr_t ci_h = 0;
  3991. ci = instance->ctrl_info_buf;
  3992. ci_h = instance->ctrl_info_buf_h;
  3993. cmd = megasas_get_cmd(instance);
  3994. if (!cmd) {
  3995. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
  3996. return -ENOMEM;
  3997. }
  3998. dcmd = &cmd->frame->dcmd;
  3999. memset(ci, 0, sizeof(*ci));
  4000. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4001. dcmd->cmd = MFI_CMD_DCMD;
  4002. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  4003. dcmd->sge_count = 1;
  4004. dcmd->flags = MFI_FRAME_DIR_READ;
  4005. dcmd->timeout = 0;
  4006. dcmd->pad_0 = 0;
  4007. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  4008. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
  4009. dcmd->mbox.b[0] = 1;
  4010. megasas_set_dma_settings(instance, dcmd, ci_h,
  4011. sizeof(struct megasas_ctrl_info));
  4012. if ((instance->adapter_type != MFI_SERIES) &&
  4013. !instance->mask_interrupts) {
  4014. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  4015. } else {
  4016. ret = megasas_issue_polled(instance, cmd);
  4017. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  4018. }
  4019. switch (ret) {
  4020. case DCMD_SUCCESS:
  4021. /* Save required controller information in
  4022. * CPU endianness format.
  4023. */
  4024. le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
  4025. le32_to_cpus((u32 *)&ci->adapterOperations2);
  4026. le32_to_cpus((u32 *)&ci->adapterOperations3);
  4027. le16_to_cpus((u16 *)&ci->adapter_operations4);
  4028. /* Update the latest Ext VD info.
  4029. * From Init path, store current firmware details.
  4030. * From OCR path, detect any firmware properties changes.
  4031. * in case of Firmware upgrade without system reboot.
  4032. */
  4033. megasas_update_ext_vd_details(instance);
  4034. instance->use_seqnum_jbod_fp =
  4035. ci->adapterOperations3.useSeqNumJbodFP;
  4036. instance->support_morethan256jbod =
  4037. ci->adapter_operations4.support_pd_map_target_id;
  4038. instance->support_nvme_passthru =
  4039. ci->adapter_operations4.support_nvme_passthru;
  4040. instance->task_abort_tmo = ci->TaskAbortTO;
  4041. instance->max_reset_tmo = ci->MaxResetTO;
  4042. /*Check whether controller is iMR or MR */
  4043. instance->is_imr = (ci->memory_size ? 0 : 1);
  4044. dev_info(&instance->pdev->dev,
  4045. "controller type\t: %s(%dMB)\n",
  4046. instance->is_imr ? "iMR" : "MR",
  4047. le16_to_cpu(ci->memory_size));
  4048. instance->disableOnlineCtrlReset =
  4049. ci->properties.OnOffProperties.disableOnlineCtrlReset;
  4050. instance->secure_jbod_support =
  4051. ci->adapterOperations3.supportSecurityonJBOD;
  4052. dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
  4053. instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
  4054. dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
  4055. instance->secure_jbod_support ? "Yes" : "No");
  4056. dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
  4057. instance->support_nvme_passthru ? "Yes" : "No");
  4058. dev_info(&instance->pdev->dev,
  4059. "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
  4060. instance->task_abort_tmo, instance->max_reset_tmo);
  4061. break;
  4062. case DCMD_TIMEOUT:
  4063. switch (dcmd_timeout_ocr_possible(instance)) {
  4064. case INITIATE_OCR:
  4065. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  4066. megasas_reset_fusion(instance->host,
  4067. MFI_IO_TIMEOUT_OCR);
  4068. break;
  4069. case KILL_ADAPTER:
  4070. megaraid_sas_kill_hba(instance);
  4071. break;
  4072. case IGNORE_TIMEOUT:
  4073. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  4074. __func__, __LINE__);
  4075. break;
  4076. }
  4077. break;
  4078. case DCMD_FAILED:
  4079. megaraid_sas_kill_hba(instance);
  4080. break;
  4081. }
  4082. if (ret != DCMD_TIMEOUT)
  4083. megasas_return_cmd(instance, cmd);
  4084. return ret;
  4085. }
  4086. /*
  4087. * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
  4088. * to firmware
  4089. *
  4090. * @instance: Adapter soft state
  4091. * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
  4092. MR_CRASH_BUF_TURN_OFF = 0
  4093. MR_CRASH_BUF_TURN_ON = 1
  4094. * @return 0 on success non-zero on failure.
  4095. * Issues an internal command (DCMD) to set parameters for crash dump feature.
  4096. * Driver will send address of crash dump DMA buffer and set mbox to tell FW
  4097. * that driver supports crash dump feature. This DCMD will be sent only if
  4098. * crash dump feature is supported by the FW.
  4099. *
  4100. */
  4101. int megasas_set_crash_dump_params(struct megasas_instance *instance,
  4102. u8 crash_buf_state)
  4103. {
  4104. int ret = 0;
  4105. struct megasas_cmd *cmd;
  4106. struct megasas_dcmd_frame *dcmd;
  4107. cmd = megasas_get_cmd(instance);
  4108. if (!cmd) {
  4109. dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
  4110. return -ENOMEM;
  4111. }
  4112. dcmd = &cmd->frame->dcmd;
  4113. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4114. dcmd->mbox.b[0] = crash_buf_state;
  4115. dcmd->cmd = MFI_CMD_DCMD;
  4116. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  4117. dcmd->sge_count = 1;
  4118. dcmd->flags = MFI_FRAME_DIR_NONE;
  4119. dcmd->timeout = 0;
  4120. dcmd->pad_0 = 0;
  4121. dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
  4122. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
  4123. megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
  4124. CRASH_DMA_BUF_SIZE);
  4125. if ((instance->adapter_type != MFI_SERIES) &&
  4126. !instance->mask_interrupts)
  4127. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  4128. else
  4129. ret = megasas_issue_polled(instance, cmd);
  4130. if (ret == DCMD_TIMEOUT) {
  4131. switch (dcmd_timeout_ocr_possible(instance)) {
  4132. case INITIATE_OCR:
  4133. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  4134. megasas_reset_fusion(instance->host,
  4135. MFI_IO_TIMEOUT_OCR);
  4136. break;
  4137. case KILL_ADAPTER:
  4138. megaraid_sas_kill_hba(instance);
  4139. break;
  4140. case IGNORE_TIMEOUT:
  4141. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  4142. __func__, __LINE__);
  4143. break;
  4144. }
  4145. } else
  4146. megasas_return_cmd(instance, cmd);
  4147. return ret;
  4148. }
  4149. /**
  4150. * megasas_issue_init_mfi - Initializes the FW
  4151. * @instance: Adapter soft state
  4152. *
  4153. * Issues the INIT MFI cmd
  4154. */
  4155. static int
  4156. megasas_issue_init_mfi(struct megasas_instance *instance)
  4157. {
  4158. __le32 context;
  4159. struct megasas_cmd *cmd;
  4160. struct megasas_init_frame *init_frame;
  4161. struct megasas_init_queue_info *initq_info;
  4162. dma_addr_t init_frame_h;
  4163. dma_addr_t initq_info_h;
  4164. /*
  4165. * Prepare a init frame. Note the init frame points to queue info
  4166. * structure. Each frame has SGL allocated after first 64 bytes. For
  4167. * this frame - since we don't need any SGL - we use SGL's space as
  4168. * queue info structure
  4169. *
  4170. * We will not get a NULL command below. We just created the pool.
  4171. */
  4172. cmd = megasas_get_cmd(instance);
  4173. init_frame = (struct megasas_init_frame *)cmd->frame;
  4174. initq_info = (struct megasas_init_queue_info *)
  4175. ((unsigned long)init_frame + 64);
  4176. init_frame_h = cmd->frame_phys_addr;
  4177. initq_info_h = init_frame_h + 64;
  4178. context = init_frame->context;
  4179. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  4180. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  4181. init_frame->context = context;
  4182. initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
  4183. initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
  4184. initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
  4185. initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
  4186. init_frame->cmd = MFI_CMD_INIT;
  4187. init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
  4188. init_frame->queue_info_new_phys_addr_lo =
  4189. cpu_to_le32(lower_32_bits(initq_info_h));
  4190. init_frame->queue_info_new_phys_addr_hi =
  4191. cpu_to_le32(upper_32_bits(initq_info_h));
  4192. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
  4193. /*
  4194. * disable the intr before firing the init frame to FW
  4195. */
  4196. instance->instancet->disable_intr(instance);
  4197. /*
  4198. * Issue the init frame in polled mode
  4199. */
  4200. if (megasas_issue_polled(instance, cmd)) {
  4201. dev_err(&instance->pdev->dev, "Failed to init firmware\n");
  4202. megasas_return_cmd(instance, cmd);
  4203. goto fail_fw_init;
  4204. }
  4205. megasas_return_cmd(instance, cmd);
  4206. return 0;
  4207. fail_fw_init:
  4208. return -EINVAL;
  4209. }
  4210. static u32
  4211. megasas_init_adapter_mfi(struct megasas_instance *instance)
  4212. {
  4213. struct megasas_register_set __iomem *reg_set;
  4214. u32 context_sz;
  4215. u32 reply_q_sz;
  4216. reg_set = instance->reg_set;
  4217. /*
  4218. * Get various operational parameters from status register
  4219. */
  4220. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  4221. /*
  4222. * Reduce the max supported cmds by 1. This is to ensure that the
  4223. * reply_q_sz (1 more than the max cmd that driver may send)
  4224. * does not exceed max cmds that the FW can support
  4225. */
  4226. instance->max_fw_cmds = instance->max_fw_cmds-1;
  4227. instance->max_mfi_cmds = instance->max_fw_cmds;
  4228. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  4229. 0x10;
  4230. /*
  4231. * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
  4232. * are reserved for IOCTL + driver's internal DCMDs.
  4233. */
  4234. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  4235. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  4236. instance->max_scsi_cmds = (instance->max_fw_cmds -
  4237. MEGASAS_SKINNY_INT_CMDS);
  4238. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  4239. } else {
  4240. instance->max_scsi_cmds = (instance->max_fw_cmds -
  4241. MEGASAS_INT_CMDS);
  4242. sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
  4243. }
  4244. instance->cur_can_queue = instance->max_scsi_cmds;
  4245. /*
  4246. * Create a pool of commands
  4247. */
  4248. if (megasas_alloc_cmds(instance))
  4249. goto fail_alloc_cmds;
  4250. /*
  4251. * Allocate memory for reply queue. Length of reply queue should
  4252. * be _one_ more than the maximum commands handled by the firmware.
  4253. *
  4254. * Note: When FW completes commands, it places corresponding contex
  4255. * values in this circular reply queue. This circular queue is a fairly
  4256. * typical producer-consumer queue. FW is the producer (of completed
  4257. * commands) and the driver is the consumer.
  4258. */
  4259. context_sz = sizeof(u32);
  4260. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  4261. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  4262. reply_q_sz,
  4263. &instance->reply_queue_h);
  4264. if (!instance->reply_queue) {
  4265. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
  4266. goto fail_reply_queue;
  4267. }
  4268. if (megasas_issue_init_mfi(instance))
  4269. goto fail_fw_init;
  4270. if (megasas_get_ctrl_info(instance)) {
  4271. dev_err(&instance->pdev->dev, "(%d): Could get controller info "
  4272. "Fail from %s %d\n", instance->unique_id,
  4273. __func__, __LINE__);
  4274. goto fail_fw_init;
  4275. }
  4276. instance->fw_support_ieee = 0;
  4277. instance->fw_support_ieee =
  4278. (instance->instancet->read_fw_status_reg(reg_set) &
  4279. 0x04000000);
  4280. dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
  4281. instance->fw_support_ieee);
  4282. if (instance->fw_support_ieee)
  4283. instance->flag_ieee = 1;
  4284. return 0;
  4285. fail_fw_init:
  4286. pci_free_consistent(instance->pdev, reply_q_sz,
  4287. instance->reply_queue, instance->reply_queue_h);
  4288. fail_reply_queue:
  4289. megasas_free_cmds(instance);
  4290. fail_alloc_cmds:
  4291. return 1;
  4292. }
  4293. /*
  4294. * megasas_setup_irqs_ioapic - register legacy interrupts.
  4295. * @instance: Adapter soft state
  4296. *
  4297. * Do not enable interrupt, only setup ISRs.
  4298. *
  4299. * Return 0 on success.
  4300. */
  4301. static int
  4302. megasas_setup_irqs_ioapic(struct megasas_instance *instance)
  4303. {
  4304. struct pci_dev *pdev;
  4305. pdev = instance->pdev;
  4306. instance->irq_context[0].instance = instance;
  4307. instance->irq_context[0].MSIxIndex = 0;
  4308. if (request_irq(pci_irq_vector(pdev, 0),
  4309. instance->instancet->service_isr, IRQF_SHARED,
  4310. "megasas", &instance->irq_context[0])) {
  4311. dev_err(&instance->pdev->dev,
  4312. "Failed to register IRQ from %s %d\n",
  4313. __func__, __LINE__);
  4314. return -1;
  4315. }
  4316. return 0;
  4317. }
  4318. /**
  4319. * megasas_setup_irqs_msix - register MSI-x interrupts.
  4320. * @instance: Adapter soft state
  4321. * @is_probe: Driver probe check
  4322. *
  4323. * Do not enable interrupt, only setup ISRs.
  4324. *
  4325. * Return 0 on success.
  4326. */
  4327. static int
  4328. megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
  4329. {
  4330. int i, j;
  4331. struct pci_dev *pdev;
  4332. pdev = instance->pdev;
  4333. /* Try MSI-x */
  4334. for (i = 0; i < instance->msix_vectors; i++) {
  4335. instance->irq_context[i].instance = instance;
  4336. instance->irq_context[i].MSIxIndex = i;
  4337. if (request_irq(pci_irq_vector(pdev, i),
  4338. instance->instancet->service_isr, 0, "megasas",
  4339. &instance->irq_context[i])) {
  4340. dev_err(&instance->pdev->dev,
  4341. "Failed to register IRQ for vector %d.\n", i);
  4342. for (j = 0; j < i; j++)
  4343. free_irq(pci_irq_vector(pdev, j),
  4344. &instance->irq_context[j]);
  4345. /* Retry irq register for IO_APIC*/
  4346. instance->msix_vectors = 0;
  4347. if (is_probe) {
  4348. pci_free_irq_vectors(instance->pdev);
  4349. return megasas_setup_irqs_ioapic(instance);
  4350. } else {
  4351. return -1;
  4352. }
  4353. }
  4354. }
  4355. return 0;
  4356. }
  4357. /*
  4358. * megasas_destroy_irqs- unregister interrupts.
  4359. * @instance: Adapter soft state
  4360. * return: void
  4361. */
  4362. static void
  4363. megasas_destroy_irqs(struct megasas_instance *instance) {
  4364. int i;
  4365. if (instance->msix_vectors)
  4366. for (i = 0; i < instance->msix_vectors; i++) {
  4367. free_irq(pci_irq_vector(instance->pdev, i),
  4368. &instance->irq_context[i]);
  4369. }
  4370. else
  4371. free_irq(pci_irq_vector(instance->pdev, 0),
  4372. &instance->irq_context[0]);
  4373. }
  4374. /**
  4375. * megasas_setup_jbod_map - setup jbod map for FP seq_number.
  4376. * @instance: Adapter soft state
  4377. * @is_probe: Driver probe check
  4378. *
  4379. * Return 0 on success.
  4380. */
  4381. void
  4382. megasas_setup_jbod_map(struct megasas_instance *instance)
  4383. {
  4384. int i;
  4385. struct fusion_context *fusion = instance->ctrl_context;
  4386. u32 pd_seq_map_sz;
  4387. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  4388. (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
  4389. if (reset_devices || !fusion ||
  4390. !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
  4391. dev_info(&instance->pdev->dev,
  4392. "Jbod map is not supported %s %d\n",
  4393. __func__, __LINE__);
  4394. instance->use_seqnum_jbod_fp = false;
  4395. return;
  4396. }
  4397. if (fusion->pd_seq_sync[0])
  4398. goto skip_alloc;
  4399. for (i = 0; i < JBOD_MAPS_COUNT; i++) {
  4400. fusion->pd_seq_sync[i] = dma_alloc_coherent
  4401. (&instance->pdev->dev, pd_seq_map_sz,
  4402. &fusion->pd_seq_phys[i], GFP_KERNEL);
  4403. if (!fusion->pd_seq_sync[i]) {
  4404. dev_err(&instance->pdev->dev,
  4405. "Failed to allocate memory from %s %d\n",
  4406. __func__, __LINE__);
  4407. if (i == 1) {
  4408. dma_free_coherent(&instance->pdev->dev,
  4409. pd_seq_map_sz, fusion->pd_seq_sync[0],
  4410. fusion->pd_seq_phys[0]);
  4411. fusion->pd_seq_sync[0] = NULL;
  4412. }
  4413. instance->use_seqnum_jbod_fp = false;
  4414. return;
  4415. }
  4416. }
  4417. skip_alloc:
  4418. if (!megasas_sync_pd_seq_num(instance, false) &&
  4419. !megasas_sync_pd_seq_num(instance, true))
  4420. instance->use_seqnum_jbod_fp = true;
  4421. else
  4422. instance->use_seqnum_jbod_fp = false;
  4423. }
  4424. static void megasas_setup_reply_map(struct megasas_instance *instance)
  4425. {
  4426. const struct cpumask *mask;
  4427. unsigned int queue, cpu;
  4428. for (queue = 0; queue < instance->msix_vectors; queue++) {
  4429. mask = pci_irq_get_affinity(instance->pdev, queue);
  4430. if (!mask)
  4431. goto fallback;
  4432. for_each_cpu(cpu, mask)
  4433. instance->reply_map[cpu] = queue;
  4434. }
  4435. return;
  4436. fallback:
  4437. for_each_possible_cpu(cpu)
  4438. instance->reply_map[cpu] = cpu % instance->msix_vectors;
  4439. }
  4440. /**
  4441. * megasas_init_fw - Initializes the FW
  4442. * @instance: Adapter soft state
  4443. *
  4444. * This is the main function for initializing firmware
  4445. */
  4446. static int megasas_init_fw(struct megasas_instance *instance)
  4447. {
  4448. u32 max_sectors_1;
  4449. u32 max_sectors_2, tmp_sectors, msix_enable;
  4450. u32 scratch_pad_2, scratch_pad_3, scratch_pad_4, status_reg;
  4451. resource_size_t base_addr;
  4452. struct megasas_register_set __iomem *reg_set;
  4453. struct megasas_ctrl_info *ctrl_info = NULL;
  4454. unsigned long bar_list;
  4455. int i, j, loop, fw_msix_count = 0;
  4456. struct IOV_111 *iovPtr;
  4457. struct fusion_context *fusion;
  4458. bool do_adp_reset = true;
  4459. fusion = instance->ctrl_context;
  4460. /* Find first memory bar */
  4461. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  4462. instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
  4463. if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
  4464. "megasas: LSI")) {
  4465. dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
  4466. return -EBUSY;
  4467. }
  4468. base_addr = pci_resource_start(instance->pdev, instance->bar);
  4469. instance->reg_set = ioremap_nocache(base_addr, 8192);
  4470. if (!instance->reg_set) {
  4471. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
  4472. goto fail_ioremap;
  4473. }
  4474. reg_set = instance->reg_set;
  4475. if (instance->adapter_type != MFI_SERIES)
  4476. instance->instancet = &megasas_instance_template_fusion;
  4477. else {
  4478. switch (instance->pdev->device) {
  4479. case PCI_DEVICE_ID_LSI_SAS1078R:
  4480. case PCI_DEVICE_ID_LSI_SAS1078DE:
  4481. instance->instancet = &megasas_instance_template_ppc;
  4482. break;
  4483. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  4484. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  4485. instance->instancet = &megasas_instance_template_gen2;
  4486. break;
  4487. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  4488. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  4489. instance->instancet = &megasas_instance_template_skinny;
  4490. break;
  4491. case PCI_DEVICE_ID_LSI_SAS1064R:
  4492. case PCI_DEVICE_ID_DELL_PERC5:
  4493. default:
  4494. instance->instancet = &megasas_instance_template_xscale;
  4495. instance->pd_list_not_supported = 1;
  4496. break;
  4497. }
  4498. }
  4499. if (megasas_transition_to_ready(instance, 0)) {
  4500. if (instance->adapter_type >= INVADER_SERIES) {
  4501. status_reg = instance->instancet->read_fw_status_reg(
  4502. instance->reg_set);
  4503. do_adp_reset = status_reg & MFI_RESET_ADAPTER;
  4504. }
  4505. if (do_adp_reset) {
  4506. atomic_set(&instance->fw_reset_no_pci_access, 1);
  4507. instance->instancet->adp_reset
  4508. (instance, instance->reg_set);
  4509. atomic_set(&instance->fw_reset_no_pci_access, 0);
  4510. dev_info(&instance->pdev->dev,
  4511. "FW restarted successfully from %s!\n",
  4512. __func__);
  4513. /*waiting for about 30 second before retry*/
  4514. ssleep(30);
  4515. if (megasas_transition_to_ready(instance, 0))
  4516. goto fail_ready_state;
  4517. } else {
  4518. goto fail_ready_state;
  4519. }
  4520. }
  4521. megasas_init_ctrl_params(instance);
  4522. if (megasas_set_dma_mask(instance))
  4523. goto fail_ready_state;
  4524. if (megasas_alloc_ctrl_mem(instance))
  4525. goto fail_alloc_dma_buf;
  4526. if (megasas_alloc_ctrl_dma_buffers(instance))
  4527. goto fail_alloc_dma_buf;
  4528. fusion = instance->ctrl_context;
  4529. if (instance->adapter_type == VENTURA_SERIES) {
  4530. scratch_pad_3 =
  4531. readl(&instance->reg_set->outbound_scratch_pad_3);
  4532. instance->max_raid_mapsize = ((scratch_pad_3 >>
  4533. MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
  4534. MR_MAX_RAID_MAP_SIZE_MASK);
  4535. }
  4536. /* Check if MSI-X is supported while in ready state */
  4537. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  4538. 0x4000000) >> 0x1a;
  4539. if (msix_enable && !msix_disable) {
  4540. int irq_flags = PCI_IRQ_MSIX;
  4541. scratch_pad_2 = readl
  4542. (&instance->reg_set->outbound_scratch_pad_2);
  4543. /* Check max MSI-X vectors */
  4544. if (fusion) {
  4545. if (instance->adapter_type == THUNDERBOLT_SERIES) {
  4546. /* Thunderbolt Series*/
  4547. instance->msix_vectors = (scratch_pad_2
  4548. & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
  4549. fw_msix_count = instance->msix_vectors;
  4550. } else {
  4551. instance->msix_vectors = ((scratch_pad_2
  4552. & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
  4553. >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
  4554. /*
  4555. * For Invader series, > 8 MSI-x vectors
  4556. * supported by FW/HW implies combined
  4557. * reply queue mode is enabled.
  4558. * For Ventura series, > 16 MSI-x vectors
  4559. * supported by FW/HW implies combined
  4560. * reply queue mode is enabled.
  4561. */
  4562. switch (instance->adapter_type) {
  4563. case INVADER_SERIES:
  4564. if (instance->msix_vectors > 8)
  4565. instance->msix_combined = true;
  4566. break;
  4567. case VENTURA_SERIES:
  4568. if (instance->msix_vectors > 16)
  4569. instance->msix_combined = true;
  4570. break;
  4571. }
  4572. if (rdpq_enable)
  4573. instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
  4574. 1 : 0;
  4575. fw_msix_count = instance->msix_vectors;
  4576. /* Save 1-15 reply post index address to local memory
  4577. * Index 0 is already saved from reg offset
  4578. * MPI2_REPLY_POST_HOST_INDEX_OFFSET
  4579. */
  4580. for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
  4581. instance->reply_post_host_index_addr[loop] =
  4582. (u32 __iomem *)
  4583. ((u8 __iomem *)instance->reg_set +
  4584. MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
  4585. + (loop * 0x10));
  4586. }
  4587. }
  4588. if (msix_vectors)
  4589. instance->msix_vectors = min(msix_vectors,
  4590. instance->msix_vectors);
  4591. } else /* MFI adapters */
  4592. instance->msix_vectors = 1;
  4593. /* Don't bother allocating more MSI-X vectors than cpus */
  4594. instance->msix_vectors = min(instance->msix_vectors,
  4595. (unsigned int)num_online_cpus());
  4596. if (smp_affinity_enable)
  4597. irq_flags |= PCI_IRQ_AFFINITY;
  4598. i = pci_alloc_irq_vectors(instance->pdev, 1,
  4599. instance->msix_vectors, irq_flags);
  4600. if (i > 0)
  4601. instance->msix_vectors = i;
  4602. else
  4603. instance->msix_vectors = 0;
  4604. }
  4605. /*
  4606. * MSI-X host index 0 is common for all adapter.
  4607. * It is used for all MPT based Adapters.
  4608. */
  4609. if (instance->msix_combined) {
  4610. instance->reply_post_host_index_addr[0] =
  4611. (u32 *)((u8 *)instance->reg_set +
  4612. MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
  4613. } else {
  4614. instance->reply_post_host_index_addr[0] =
  4615. (u32 *)((u8 *)instance->reg_set +
  4616. MPI2_REPLY_POST_HOST_INDEX_OFFSET);
  4617. }
  4618. if (!instance->msix_vectors) {
  4619. i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
  4620. if (i < 0)
  4621. goto fail_init_adapter;
  4622. }
  4623. megasas_setup_reply_map(instance);
  4624. dev_info(&instance->pdev->dev,
  4625. "firmware supports msix\t: (%d)", fw_msix_count);
  4626. dev_info(&instance->pdev->dev,
  4627. "current msix/online cpus\t: (%d/%d)\n",
  4628. instance->msix_vectors, (unsigned int)num_online_cpus());
  4629. dev_info(&instance->pdev->dev,
  4630. "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
  4631. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  4632. (unsigned long)instance);
  4633. /*
  4634. * Below are default value for legacy Firmware.
  4635. * non-fusion based controllers
  4636. */
  4637. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  4638. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  4639. /* Get operational params, sge flags, send init cmd to controller */
  4640. if (instance->instancet->init_adapter(instance))
  4641. goto fail_init_adapter;
  4642. if (instance->adapter_type == VENTURA_SERIES) {
  4643. scratch_pad_4 =
  4644. readl(&instance->reg_set->outbound_scratch_pad_4);
  4645. if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
  4646. MR_DEFAULT_NVME_PAGE_SHIFT)
  4647. instance->nvme_page_size =
  4648. (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
  4649. dev_info(&instance->pdev->dev,
  4650. "NVME page size\t: (%d)\n", instance->nvme_page_size);
  4651. }
  4652. if (instance->msix_vectors ?
  4653. megasas_setup_irqs_msix(instance, 1) :
  4654. megasas_setup_irqs_ioapic(instance))
  4655. goto fail_init_adapter;
  4656. instance->instancet->enable_intr(instance);
  4657. dev_info(&instance->pdev->dev, "INIT adapter done\n");
  4658. megasas_setup_jbod_map(instance);
  4659. /** for passthrough
  4660. * the following function will get the PD LIST.
  4661. */
  4662. memset(instance->pd_list, 0,
  4663. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  4664. if (megasas_get_pd_list(instance) < 0) {
  4665. dev_err(&instance->pdev->dev, "failed to get PD list\n");
  4666. goto fail_get_ld_pd_list;
  4667. }
  4668. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  4669. /* stream detection initialization */
  4670. if (instance->adapter_type == VENTURA_SERIES) {
  4671. fusion->stream_detect_by_ld =
  4672. kcalloc(MAX_LOGICAL_DRIVES_EXT,
  4673. sizeof(struct LD_STREAM_DETECT *),
  4674. GFP_KERNEL);
  4675. if (!fusion->stream_detect_by_ld) {
  4676. dev_err(&instance->pdev->dev,
  4677. "unable to allocate stream detection for pool of LDs\n");
  4678. goto fail_get_ld_pd_list;
  4679. }
  4680. for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
  4681. fusion->stream_detect_by_ld[i] =
  4682. kzalloc(sizeof(struct LD_STREAM_DETECT),
  4683. GFP_KERNEL);
  4684. if (!fusion->stream_detect_by_ld[i]) {
  4685. dev_err(&instance->pdev->dev,
  4686. "unable to allocate stream detect by LD\n ");
  4687. for (j = 0; j < i; ++j)
  4688. kfree(fusion->stream_detect_by_ld[j]);
  4689. kfree(fusion->stream_detect_by_ld);
  4690. fusion->stream_detect_by_ld = NULL;
  4691. goto fail_get_ld_pd_list;
  4692. }
  4693. fusion->stream_detect_by_ld[i]->mru_bit_map
  4694. = MR_STREAM_BITMAP;
  4695. }
  4696. }
  4697. if (megasas_ld_list_query(instance,
  4698. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  4699. goto fail_get_ld_pd_list;
  4700. /*
  4701. * Compute the max allowed sectors per IO: The controller info has two
  4702. * limits on max sectors. Driver should use the minimum of these two.
  4703. *
  4704. * 1 << stripe_sz_ops.min = max sectors per strip
  4705. *
  4706. * Note that older firmwares ( < FW ver 30) didn't report information
  4707. * to calculate max_sectors_1. So the number ended up as zero always.
  4708. */
  4709. tmp_sectors = 0;
  4710. ctrl_info = instance->ctrl_info_buf;
  4711. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  4712. le16_to_cpu(ctrl_info->max_strips_per_io);
  4713. max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
  4714. tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
  4715. instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
  4716. instance->passive = ctrl_info->cluster.passive;
  4717. memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
  4718. instance->UnevenSpanSupport =
  4719. ctrl_info->adapterOperations2.supportUnevenSpans;
  4720. if (instance->UnevenSpanSupport) {
  4721. struct fusion_context *fusion = instance->ctrl_context;
  4722. if (MR_ValidateMapInfo(instance, instance->map_id))
  4723. fusion->fast_path_io = 1;
  4724. else
  4725. fusion->fast_path_io = 0;
  4726. }
  4727. if (ctrl_info->host_interface.SRIOV) {
  4728. instance->requestorId = ctrl_info->iov.requestorId;
  4729. if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
  4730. if (!ctrl_info->adapterOperations2.activePassive)
  4731. instance->PlasmaFW111 = 1;
  4732. dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
  4733. instance->PlasmaFW111 ? "1.11" : "new");
  4734. if (instance->PlasmaFW111) {
  4735. iovPtr = (struct IOV_111 *)
  4736. ((unsigned char *)ctrl_info + IOV_111_OFFSET);
  4737. instance->requestorId = iovPtr->requestorId;
  4738. }
  4739. }
  4740. dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
  4741. instance->requestorId);
  4742. }
  4743. instance->crash_dump_fw_support =
  4744. ctrl_info->adapterOperations3.supportCrashDump;
  4745. instance->crash_dump_drv_support =
  4746. (instance->crash_dump_fw_support &&
  4747. instance->crash_dump_buf);
  4748. if (instance->crash_dump_drv_support)
  4749. megasas_set_crash_dump_params(instance,
  4750. MR_CRASH_BUF_TURN_OFF);
  4751. else {
  4752. if (instance->crash_dump_buf)
  4753. pci_free_consistent(instance->pdev,
  4754. CRASH_DMA_BUF_SIZE,
  4755. instance->crash_dump_buf,
  4756. instance->crash_dump_h);
  4757. instance->crash_dump_buf = NULL;
  4758. }
  4759. dev_info(&instance->pdev->dev,
  4760. "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
  4761. le16_to_cpu(ctrl_info->pci.vendor_id),
  4762. le16_to_cpu(ctrl_info->pci.device_id),
  4763. le16_to_cpu(ctrl_info->pci.sub_vendor_id),
  4764. le16_to_cpu(ctrl_info->pci.sub_device_id));
  4765. dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
  4766. instance->UnevenSpanSupport ? "yes" : "no");
  4767. dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
  4768. instance->crash_dump_drv_support ? "yes" : "no");
  4769. dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
  4770. instance->use_seqnum_jbod_fp ? "yes" : "no");
  4771. instance->max_sectors_per_req = instance->max_num_sge *
  4772. SGE_BUFFER_SIZE / 512;
  4773. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  4774. instance->max_sectors_per_req = tmp_sectors;
  4775. /* Check for valid throttlequeuedepth module parameter */
  4776. if (throttlequeuedepth &&
  4777. throttlequeuedepth <= instance->max_scsi_cmds)
  4778. instance->throttlequeuedepth = throttlequeuedepth;
  4779. else
  4780. instance->throttlequeuedepth =
  4781. MEGASAS_THROTTLE_QUEUE_DEPTH;
  4782. if ((resetwaittime < 1) ||
  4783. (resetwaittime > MEGASAS_RESET_WAIT_TIME))
  4784. resetwaittime = MEGASAS_RESET_WAIT_TIME;
  4785. if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
  4786. scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  4787. /* Launch SR-IOV heartbeat timer */
  4788. if (instance->requestorId) {
  4789. if (!megasas_sriov_start_heartbeat(instance, 1))
  4790. megasas_start_timer(instance);
  4791. else
  4792. instance->skip_heartbeat_timer_del = 1;
  4793. }
  4794. return 0;
  4795. fail_get_ld_pd_list:
  4796. instance->instancet->disable_intr(instance);
  4797. megasas_destroy_irqs(instance);
  4798. fail_init_adapter:
  4799. if (instance->msix_vectors)
  4800. pci_free_irq_vectors(instance->pdev);
  4801. instance->msix_vectors = 0;
  4802. fail_alloc_dma_buf:
  4803. megasas_free_ctrl_dma_buffers(instance);
  4804. megasas_free_ctrl_mem(instance);
  4805. fail_ready_state:
  4806. iounmap(instance->reg_set);
  4807. fail_ioremap:
  4808. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  4809. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  4810. __func__, __LINE__);
  4811. return -EINVAL;
  4812. }
  4813. /**
  4814. * megasas_release_mfi - Reverses the FW initialization
  4815. * @instance: Adapter soft state
  4816. */
  4817. static void megasas_release_mfi(struct megasas_instance *instance)
  4818. {
  4819. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  4820. if (instance->reply_queue)
  4821. pci_free_consistent(instance->pdev, reply_q_sz,
  4822. instance->reply_queue, instance->reply_queue_h);
  4823. megasas_free_cmds(instance);
  4824. iounmap(instance->reg_set);
  4825. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  4826. }
  4827. /**
  4828. * megasas_get_seq_num - Gets latest event sequence numbers
  4829. * @instance: Adapter soft state
  4830. * @eli: FW event log sequence numbers information
  4831. *
  4832. * FW maintains a log of all events in a non-volatile area. Upper layers would
  4833. * usually find out the latest sequence number of the events, the seq number at
  4834. * the boot etc. They would "read" all the events below the latest seq number
  4835. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  4836. * number), they would subsribe to AEN (asynchronous event notification) and
  4837. * wait for the events to happen.
  4838. */
  4839. static int
  4840. megasas_get_seq_num(struct megasas_instance *instance,
  4841. struct megasas_evt_log_info *eli)
  4842. {
  4843. struct megasas_cmd *cmd;
  4844. struct megasas_dcmd_frame *dcmd;
  4845. struct megasas_evt_log_info *el_info;
  4846. dma_addr_t el_info_h = 0;
  4847. int ret;
  4848. cmd = megasas_get_cmd(instance);
  4849. if (!cmd) {
  4850. return -ENOMEM;
  4851. }
  4852. dcmd = &cmd->frame->dcmd;
  4853. el_info = pci_zalloc_consistent(instance->pdev,
  4854. sizeof(struct megasas_evt_log_info),
  4855. &el_info_h);
  4856. if (!el_info) {
  4857. megasas_return_cmd(instance, cmd);
  4858. return -ENOMEM;
  4859. }
  4860. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4861. dcmd->cmd = MFI_CMD_DCMD;
  4862. dcmd->cmd_status = 0x0;
  4863. dcmd->sge_count = 1;
  4864. dcmd->flags = MFI_FRAME_DIR_READ;
  4865. dcmd->timeout = 0;
  4866. dcmd->pad_0 = 0;
  4867. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  4868. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
  4869. megasas_set_dma_settings(instance, dcmd, el_info_h,
  4870. sizeof(struct megasas_evt_log_info));
  4871. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  4872. if (ret != DCMD_SUCCESS) {
  4873. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  4874. __func__, __LINE__);
  4875. goto dcmd_failed;
  4876. }
  4877. /*
  4878. * Copy the data back into callers buffer
  4879. */
  4880. eli->newest_seq_num = el_info->newest_seq_num;
  4881. eli->oldest_seq_num = el_info->oldest_seq_num;
  4882. eli->clear_seq_num = el_info->clear_seq_num;
  4883. eli->shutdown_seq_num = el_info->shutdown_seq_num;
  4884. eli->boot_seq_num = el_info->boot_seq_num;
  4885. dcmd_failed:
  4886. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  4887. el_info, el_info_h);
  4888. megasas_return_cmd(instance, cmd);
  4889. return ret;
  4890. }
  4891. /**
  4892. * megasas_register_aen - Registers for asynchronous event notification
  4893. * @instance: Adapter soft state
  4894. * @seq_num: The starting sequence number
  4895. * @class_locale: Class of the event
  4896. *
  4897. * This function subscribes for AEN for events beyond the @seq_num. It requests
  4898. * to be notified if and only if the event is of type @class_locale
  4899. */
  4900. static int
  4901. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  4902. u32 class_locale_word)
  4903. {
  4904. int ret_val;
  4905. struct megasas_cmd *cmd;
  4906. struct megasas_dcmd_frame *dcmd;
  4907. union megasas_evt_class_locale curr_aen;
  4908. union megasas_evt_class_locale prev_aen;
  4909. /*
  4910. * If there an AEN pending already (aen_cmd), check if the
  4911. * class_locale of that pending AEN is inclusive of the new
  4912. * AEN request we currently have. If it is, then we don't have
  4913. * to do anything. In other words, whichever events the current
  4914. * AEN request is subscribing to, have already been subscribed
  4915. * to.
  4916. *
  4917. * If the old_cmd is _not_ inclusive, then we have to abort
  4918. * that command, form a class_locale that is superset of both
  4919. * old and current and re-issue to the FW
  4920. */
  4921. curr_aen.word = class_locale_word;
  4922. if (instance->aen_cmd) {
  4923. prev_aen.word =
  4924. le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
  4925. if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
  4926. (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
  4927. dev_info(&instance->pdev->dev,
  4928. "%s %d out of range class %d send by application\n",
  4929. __func__, __LINE__, curr_aen.members.class);
  4930. return 0;
  4931. }
  4932. /*
  4933. * A class whose enum value is smaller is inclusive of all
  4934. * higher values. If a PROGRESS (= -1) was previously
  4935. * registered, then a new registration requests for higher
  4936. * classes need not be sent to FW. They are automatically
  4937. * included.
  4938. *
  4939. * Locale numbers don't have such hierarchy. They are bitmap
  4940. * values
  4941. */
  4942. if ((prev_aen.members.class <= curr_aen.members.class) &&
  4943. !((prev_aen.members.locale & curr_aen.members.locale) ^
  4944. curr_aen.members.locale)) {
  4945. /*
  4946. * Previously issued event registration includes
  4947. * current request. Nothing to do.
  4948. */
  4949. return 0;
  4950. } else {
  4951. curr_aen.members.locale |= prev_aen.members.locale;
  4952. if (prev_aen.members.class < curr_aen.members.class)
  4953. curr_aen.members.class = prev_aen.members.class;
  4954. instance->aen_cmd->abort_aen = 1;
  4955. ret_val = megasas_issue_blocked_abort_cmd(instance,
  4956. instance->
  4957. aen_cmd, 30);
  4958. if (ret_val) {
  4959. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
  4960. "previous AEN command\n");
  4961. return ret_val;
  4962. }
  4963. }
  4964. }
  4965. cmd = megasas_get_cmd(instance);
  4966. if (!cmd)
  4967. return -ENOMEM;
  4968. dcmd = &cmd->frame->dcmd;
  4969. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  4970. /*
  4971. * Prepare DCMD for aen registration
  4972. */
  4973. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4974. dcmd->cmd = MFI_CMD_DCMD;
  4975. dcmd->cmd_status = 0x0;
  4976. dcmd->sge_count = 1;
  4977. dcmd->flags = MFI_FRAME_DIR_READ;
  4978. dcmd->timeout = 0;
  4979. dcmd->pad_0 = 0;
  4980. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
  4981. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
  4982. dcmd->mbox.w[0] = cpu_to_le32(seq_num);
  4983. instance->last_seq_num = seq_num;
  4984. dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
  4985. megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
  4986. sizeof(struct megasas_evt_detail));
  4987. if (instance->aen_cmd != NULL) {
  4988. megasas_return_cmd(instance, cmd);
  4989. return 0;
  4990. }
  4991. /*
  4992. * Store reference to the cmd used to register for AEN. When an
  4993. * application wants us to register for AEN, we have to abort this
  4994. * cmd and re-register with a new EVENT LOCALE supplied by that app
  4995. */
  4996. instance->aen_cmd = cmd;
  4997. /*
  4998. * Issue the aen registration frame
  4999. */
  5000. instance->instancet->issue_dcmd(instance, cmd);
  5001. return 0;
  5002. }
  5003. /* megasas_get_target_prop - Send DCMD with below details to firmware.
  5004. *
  5005. * This DCMD will fetch few properties of LD/system PD defined
  5006. * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
  5007. *
  5008. * DCMD send by drivers whenever new target is added to the OS.
  5009. *
  5010. * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
  5011. * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
  5012. * 0 = system PD, 1 = LD.
  5013. * dcmd.mbox.s[1] - TargetID for LD/system PD.
  5014. * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
  5015. *
  5016. * @instance: Adapter soft state
  5017. * @sdev: OS provided scsi device
  5018. *
  5019. * Returns 0 on success non-zero on failure.
  5020. */
  5021. int
  5022. megasas_get_target_prop(struct megasas_instance *instance,
  5023. struct scsi_device *sdev)
  5024. {
  5025. int ret;
  5026. struct megasas_cmd *cmd;
  5027. struct megasas_dcmd_frame *dcmd;
  5028. u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
  5029. sdev->id;
  5030. cmd = megasas_get_cmd(instance);
  5031. if (!cmd) {
  5032. dev_err(&instance->pdev->dev,
  5033. "Failed to get cmd %s\n", __func__);
  5034. return -ENOMEM;
  5035. }
  5036. dcmd = &cmd->frame->dcmd;
  5037. memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
  5038. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5039. dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
  5040. dcmd->mbox.s[1] = cpu_to_le16(targetId);
  5041. dcmd->cmd = MFI_CMD_DCMD;
  5042. dcmd->cmd_status = 0xFF;
  5043. dcmd->sge_count = 1;
  5044. dcmd->flags = MFI_FRAME_DIR_READ;
  5045. dcmd->timeout = 0;
  5046. dcmd->pad_0 = 0;
  5047. dcmd->data_xfer_len =
  5048. cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
  5049. dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
  5050. megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
  5051. sizeof(struct MR_TARGET_PROPERTIES));
  5052. if ((instance->adapter_type != MFI_SERIES) &&
  5053. !instance->mask_interrupts)
  5054. ret = megasas_issue_blocked_cmd(instance,
  5055. cmd, MFI_IO_TIMEOUT_SECS);
  5056. else
  5057. ret = megasas_issue_polled(instance, cmd);
  5058. switch (ret) {
  5059. case DCMD_TIMEOUT:
  5060. switch (dcmd_timeout_ocr_possible(instance)) {
  5061. case INITIATE_OCR:
  5062. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  5063. megasas_reset_fusion(instance->host,
  5064. MFI_IO_TIMEOUT_OCR);
  5065. break;
  5066. case KILL_ADAPTER:
  5067. megaraid_sas_kill_hba(instance);
  5068. break;
  5069. case IGNORE_TIMEOUT:
  5070. dev_info(&instance->pdev->dev,
  5071. "Ignore DCMD timeout: %s %d\n",
  5072. __func__, __LINE__);
  5073. break;
  5074. }
  5075. break;
  5076. default:
  5077. megasas_return_cmd(instance, cmd);
  5078. }
  5079. if (ret != DCMD_SUCCESS)
  5080. dev_err(&instance->pdev->dev,
  5081. "return from %s %d return value %d\n",
  5082. __func__, __LINE__, ret);
  5083. return ret;
  5084. }
  5085. /**
  5086. * megasas_start_aen - Subscribes to AEN during driver load time
  5087. * @instance: Adapter soft state
  5088. */
  5089. static int megasas_start_aen(struct megasas_instance *instance)
  5090. {
  5091. struct megasas_evt_log_info eli;
  5092. union megasas_evt_class_locale class_locale;
  5093. /*
  5094. * Get the latest sequence number from FW
  5095. */
  5096. memset(&eli, 0, sizeof(eli));
  5097. if (megasas_get_seq_num(instance, &eli))
  5098. return -1;
  5099. /*
  5100. * Register AEN with FW for latest sequence number plus 1
  5101. */
  5102. class_locale.members.reserved = 0;
  5103. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  5104. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  5105. return megasas_register_aen(instance,
  5106. le32_to_cpu(eli.newest_seq_num) + 1,
  5107. class_locale.word);
  5108. }
  5109. /**
  5110. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  5111. * @instance: Adapter soft state
  5112. */
  5113. static int megasas_io_attach(struct megasas_instance *instance)
  5114. {
  5115. struct Scsi_Host *host = instance->host;
  5116. /*
  5117. * Export parameters required by SCSI mid-layer
  5118. */
  5119. host->unique_id = instance->unique_id;
  5120. host->can_queue = instance->max_scsi_cmds;
  5121. host->this_id = instance->init_id;
  5122. host->sg_tablesize = instance->max_num_sge;
  5123. if (instance->fw_support_ieee)
  5124. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  5125. /*
  5126. * Check if the module parameter value for max_sectors can be used
  5127. */
  5128. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  5129. instance->max_sectors_per_req = max_sectors;
  5130. else {
  5131. if (max_sectors) {
  5132. if (((instance->pdev->device ==
  5133. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  5134. (instance->pdev->device ==
  5135. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  5136. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  5137. instance->max_sectors_per_req = max_sectors;
  5138. } else {
  5139. dev_info(&instance->pdev->dev, "max_sectors should be > 0"
  5140. "and <= %d (or < 1MB for GEN2 controller)\n",
  5141. instance->max_sectors_per_req);
  5142. }
  5143. }
  5144. }
  5145. host->max_sectors = instance->max_sectors_per_req;
  5146. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  5147. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  5148. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  5149. host->max_lun = MEGASAS_MAX_LUN;
  5150. host->max_cmd_len = 16;
  5151. /*
  5152. * Notify the mid-layer about the new controller
  5153. */
  5154. if (scsi_add_host(host, &instance->pdev->dev)) {
  5155. dev_err(&instance->pdev->dev,
  5156. "Failed to add host from %s %d\n",
  5157. __func__, __LINE__);
  5158. return -ENODEV;
  5159. }
  5160. return 0;
  5161. }
  5162. /**
  5163. * megasas_set_dma_mask - Set DMA mask for supported controllers
  5164. *
  5165. * @instance: Adapter soft state
  5166. * Description:
  5167. *
  5168. * For Ventura, driver/FW will operate in 63bit DMA addresses.
  5169. *
  5170. * For invader-
  5171. * By default, driver/FW will operate in 32bit DMA addresses
  5172. * for consistent DMA mapping but if 32 bit consistent
  5173. * DMA mask fails, driver will try with 63 bit consistent
  5174. * mask provided FW is true 63bit DMA capable
  5175. *
  5176. * For older controllers(Thunderbolt and MFI based adapters)-
  5177. * driver/FW will operate in 32 bit consistent DMA addresses.
  5178. */
  5179. static int
  5180. megasas_set_dma_mask(struct megasas_instance *instance)
  5181. {
  5182. u64 consistent_mask;
  5183. struct pci_dev *pdev;
  5184. u32 scratch_pad_2;
  5185. pdev = instance->pdev;
  5186. consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
  5187. DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
  5188. if (IS_DMA64) {
  5189. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
  5190. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
  5191. goto fail_set_dma_mask;
  5192. if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
  5193. (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
  5194. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
  5195. /*
  5196. * If 32 bit DMA mask fails, then try for 64 bit mask
  5197. * for FW capable of handling 64 bit DMA.
  5198. */
  5199. scratch_pad_2 = readl
  5200. (&instance->reg_set->outbound_scratch_pad_2);
  5201. if (!(scratch_pad_2 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
  5202. goto fail_set_dma_mask;
  5203. else if (dma_set_mask_and_coherent(&pdev->dev,
  5204. DMA_BIT_MASK(63)))
  5205. goto fail_set_dma_mask;
  5206. }
  5207. } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
  5208. goto fail_set_dma_mask;
  5209. if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
  5210. instance->consistent_mask_64bit = false;
  5211. else
  5212. instance->consistent_mask_64bit = true;
  5213. dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
  5214. ((*pdev->dev.dma_mask == DMA_BIT_MASK(64)) ? "63" : "32"),
  5215. (instance->consistent_mask_64bit ? "63" : "32"));
  5216. return 0;
  5217. fail_set_dma_mask:
  5218. dev_err(&pdev->dev, "Failed to set DMA mask\n");
  5219. return -1;
  5220. }
  5221. /*
  5222. * megasas_set_adapter_type - Set adapter type.
  5223. * Supported controllers can be divided in
  5224. * 4 categories- enum MR_ADAPTER_TYPE {
  5225. * MFI_SERIES = 1,
  5226. * THUNDERBOLT_SERIES = 2,
  5227. * INVADER_SERIES = 3,
  5228. * VENTURA_SERIES = 4,
  5229. * };
  5230. * @instance: Adapter soft state
  5231. * return: void
  5232. */
  5233. static inline void megasas_set_adapter_type(struct megasas_instance *instance)
  5234. {
  5235. if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
  5236. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
  5237. instance->adapter_type = MFI_SERIES;
  5238. } else {
  5239. switch (instance->pdev->device) {
  5240. case PCI_DEVICE_ID_LSI_VENTURA:
  5241. case PCI_DEVICE_ID_LSI_CRUSADER:
  5242. case PCI_DEVICE_ID_LSI_HARPOON:
  5243. case PCI_DEVICE_ID_LSI_TOMCAT:
  5244. case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
  5245. case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
  5246. instance->adapter_type = VENTURA_SERIES;
  5247. break;
  5248. case PCI_DEVICE_ID_LSI_FUSION:
  5249. case PCI_DEVICE_ID_LSI_PLASMA:
  5250. instance->adapter_type = THUNDERBOLT_SERIES;
  5251. break;
  5252. case PCI_DEVICE_ID_LSI_INVADER:
  5253. case PCI_DEVICE_ID_LSI_INTRUDER:
  5254. case PCI_DEVICE_ID_LSI_INTRUDER_24:
  5255. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  5256. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  5257. case PCI_DEVICE_ID_LSI_FURY:
  5258. instance->adapter_type = INVADER_SERIES;
  5259. break;
  5260. default: /* For all other supported controllers */
  5261. instance->adapter_type = MFI_SERIES;
  5262. break;
  5263. }
  5264. }
  5265. }
  5266. static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
  5267. {
  5268. instance->producer = pci_alloc_consistent(instance->pdev, sizeof(u32),
  5269. &instance->producer_h);
  5270. instance->consumer = pci_alloc_consistent(instance->pdev, sizeof(u32),
  5271. &instance->consumer_h);
  5272. if (!instance->producer || !instance->consumer) {
  5273. dev_err(&instance->pdev->dev,
  5274. "Failed to allocate memory for producer, consumer\n");
  5275. return -1;
  5276. }
  5277. *instance->producer = 0;
  5278. *instance->consumer = 0;
  5279. return 0;
  5280. }
  5281. /**
  5282. * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
  5283. * structures which are not common across MFI
  5284. * adapters and fusion adapters.
  5285. * For MFI based adapters, allocate producer and
  5286. * consumer buffers. For fusion adapters, allocate
  5287. * memory for fusion context.
  5288. * @instance: Adapter soft state
  5289. * return: 0 for SUCCESS
  5290. */
  5291. static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
  5292. {
  5293. instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
  5294. GFP_KERNEL);
  5295. if (!instance->reply_map)
  5296. return -ENOMEM;
  5297. switch (instance->adapter_type) {
  5298. case MFI_SERIES:
  5299. if (megasas_alloc_mfi_ctrl_mem(instance))
  5300. goto fail;
  5301. break;
  5302. case VENTURA_SERIES:
  5303. case THUNDERBOLT_SERIES:
  5304. case INVADER_SERIES:
  5305. if (megasas_alloc_fusion_context(instance))
  5306. goto fail;
  5307. break;
  5308. }
  5309. return 0;
  5310. fail:
  5311. kfree(instance->reply_map);
  5312. instance->reply_map = NULL;
  5313. return -ENOMEM;
  5314. }
  5315. /*
  5316. * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
  5317. * producer, consumer buffers for MFI adapters
  5318. *
  5319. * @instance - Adapter soft instance
  5320. *
  5321. */
  5322. static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
  5323. {
  5324. kfree(instance->reply_map);
  5325. if (instance->adapter_type == MFI_SERIES) {
  5326. if (instance->producer)
  5327. pci_free_consistent(instance->pdev, sizeof(u32),
  5328. instance->producer,
  5329. instance->producer_h);
  5330. if (instance->consumer)
  5331. pci_free_consistent(instance->pdev, sizeof(u32),
  5332. instance->consumer,
  5333. instance->consumer_h);
  5334. } else {
  5335. megasas_free_fusion_context(instance);
  5336. }
  5337. }
  5338. /**
  5339. * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
  5340. * driver load time
  5341. *
  5342. * @instance- Adapter soft instance
  5343. * @return- O for SUCCESS
  5344. */
  5345. static inline
  5346. int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
  5347. {
  5348. struct pci_dev *pdev = instance->pdev;
  5349. struct fusion_context *fusion = instance->ctrl_context;
  5350. instance->evt_detail =
  5351. pci_alloc_consistent(pdev,
  5352. sizeof(struct megasas_evt_detail),
  5353. &instance->evt_detail_h);
  5354. if (!instance->evt_detail) {
  5355. dev_err(&instance->pdev->dev,
  5356. "Failed to allocate event detail buffer\n");
  5357. return -ENOMEM;
  5358. }
  5359. if (fusion) {
  5360. fusion->ioc_init_request =
  5361. dma_alloc_coherent(&pdev->dev,
  5362. sizeof(struct MPI2_IOC_INIT_REQUEST),
  5363. &fusion->ioc_init_request_phys,
  5364. GFP_KERNEL);
  5365. if (!fusion->ioc_init_request) {
  5366. dev_err(&pdev->dev,
  5367. "Failed to allocate PD list buffer\n");
  5368. return -ENOMEM;
  5369. }
  5370. }
  5371. instance->pd_list_buf =
  5372. pci_alloc_consistent(pdev,
  5373. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  5374. &instance->pd_list_buf_h);
  5375. if (!instance->pd_list_buf) {
  5376. dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
  5377. return -ENOMEM;
  5378. }
  5379. instance->ctrl_info_buf =
  5380. pci_alloc_consistent(pdev,
  5381. sizeof(struct megasas_ctrl_info),
  5382. &instance->ctrl_info_buf_h);
  5383. if (!instance->ctrl_info_buf) {
  5384. dev_err(&pdev->dev,
  5385. "Failed to allocate controller info buffer\n");
  5386. return -ENOMEM;
  5387. }
  5388. instance->ld_list_buf =
  5389. pci_alloc_consistent(pdev,
  5390. sizeof(struct MR_LD_LIST),
  5391. &instance->ld_list_buf_h);
  5392. if (!instance->ld_list_buf) {
  5393. dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
  5394. return -ENOMEM;
  5395. }
  5396. instance->ld_targetid_list_buf =
  5397. pci_alloc_consistent(pdev,
  5398. sizeof(struct MR_LD_TARGETID_LIST),
  5399. &instance->ld_targetid_list_buf_h);
  5400. if (!instance->ld_targetid_list_buf) {
  5401. dev_err(&pdev->dev,
  5402. "Failed to allocate LD targetid list buffer\n");
  5403. return -ENOMEM;
  5404. }
  5405. if (!reset_devices) {
  5406. instance->system_info_buf =
  5407. pci_alloc_consistent(pdev,
  5408. sizeof(struct MR_DRV_SYSTEM_INFO),
  5409. &instance->system_info_h);
  5410. instance->pd_info =
  5411. pci_alloc_consistent(pdev,
  5412. sizeof(struct MR_PD_INFO),
  5413. &instance->pd_info_h);
  5414. instance->tgt_prop =
  5415. pci_alloc_consistent(pdev,
  5416. sizeof(struct MR_TARGET_PROPERTIES),
  5417. &instance->tgt_prop_h);
  5418. instance->crash_dump_buf =
  5419. pci_alloc_consistent(pdev,
  5420. CRASH_DMA_BUF_SIZE,
  5421. &instance->crash_dump_h);
  5422. if (!instance->system_info_buf)
  5423. dev_err(&instance->pdev->dev,
  5424. "Failed to allocate system info buffer\n");
  5425. if (!instance->pd_info)
  5426. dev_err(&instance->pdev->dev,
  5427. "Failed to allocate pd_info buffer\n");
  5428. if (!instance->tgt_prop)
  5429. dev_err(&instance->pdev->dev,
  5430. "Failed to allocate tgt_prop buffer\n");
  5431. if (!instance->crash_dump_buf)
  5432. dev_err(&instance->pdev->dev,
  5433. "Failed to allocate crash dump buffer\n");
  5434. }
  5435. return 0;
  5436. }
  5437. /*
  5438. * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
  5439. * during driver load time
  5440. *
  5441. * @instance- Adapter soft instance
  5442. *
  5443. */
  5444. static inline
  5445. void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
  5446. {
  5447. struct pci_dev *pdev = instance->pdev;
  5448. struct fusion_context *fusion = instance->ctrl_context;
  5449. if (instance->evt_detail)
  5450. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5451. instance->evt_detail,
  5452. instance->evt_detail_h);
  5453. if (fusion && fusion->ioc_init_request)
  5454. dma_free_coherent(&pdev->dev,
  5455. sizeof(struct MPI2_IOC_INIT_REQUEST),
  5456. fusion->ioc_init_request,
  5457. fusion->ioc_init_request_phys);
  5458. if (instance->pd_list_buf)
  5459. pci_free_consistent(pdev,
  5460. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  5461. instance->pd_list_buf,
  5462. instance->pd_list_buf_h);
  5463. if (instance->ld_list_buf)
  5464. pci_free_consistent(pdev, sizeof(struct MR_LD_LIST),
  5465. instance->ld_list_buf,
  5466. instance->ld_list_buf_h);
  5467. if (instance->ld_targetid_list_buf)
  5468. pci_free_consistent(pdev, sizeof(struct MR_LD_TARGETID_LIST),
  5469. instance->ld_targetid_list_buf,
  5470. instance->ld_targetid_list_buf_h);
  5471. if (instance->ctrl_info_buf)
  5472. pci_free_consistent(pdev, sizeof(struct megasas_ctrl_info),
  5473. instance->ctrl_info_buf,
  5474. instance->ctrl_info_buf_h);
  5475. if (instance->system_info_buf)
  5476. pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
  5477. instance->system_info_buf,
  5478. instance->system_info_h);
  5479. if (instance->pd_info)
  5480. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5481. instance->pd_info, instance->pd_info_h);
  5482. if (instance->tgt_prop)
  5483. pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
  5484. instance->tgt_prop, instance->tgt_prop_h);
  5485. if (instance->crash_dump_buf)
  5486. pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
  5487. instance->crash_dump_buf,
  5488. instance->crash_dump_h);
  5489. }
  5490. /*
  5491. * megasas_init_ctrl_params - Initialize controller's instance
  5492. * parameters before FW init
  5493. * @instance - Adapter soft instance
  5494. * @return - void
  5495. */
  5496. static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
  5497. {
  5498. instance->fw_crash_state = UNAVAILABLE;
  5499. megasas_poll_wait_aen = 0;
  5500. instance->issuepend_done = 1;
  5501. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  5502. /*
  5503. * Initialize locks and queues
  5504. */
  5505. INIT_LIST_HEAD(&instance->cmd_pool);
  5506. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  5507. atomic_set(&instance->fw_outstanding, 0);
  5508. init_waitqueue_head(&instance->int_cmd_wait_q);
  5509. init_waitqueue_head(&instance->abort_cmd_wait_q);
  5510. spin_lock_init(&instance->crashdump_lock);
  5511. spin_lock_init(&instance->mfi_pool_lock);
  5512. spin_lock_init(&instance->hba_lock);
  5513. spin_lock_init(&instance->stream_lock);
  5514. spin_lock_init(&instance->completion_lock);
  5515. mutex_init(&instance->reset_mutex);
  5516. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  5517. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
  5518. instance->flag_ieee = 1;
  5519. megasas_dbg_lvl = 0;
  5520. instance->flag = 0;
  5521. instance->unload = 1;
  5522. instance->last_time = 0;
  5523. instance->disableOnlineCtrlReset = 1;
  5524. instance->UnevenSpanSupport = 0;
  5525. if (instance->adapter_type != MFI_SERIES) {
  5526. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  5527. INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
  5528. } else {
  5529. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  5530. }
  5531. }
  5532. /**
  5533. * megasas_probe_one - PCI hotplug entry point
  5534. * @pdev: PCI device structure
  5535. * @id: PCI ids of supported hotplugged adapter
  5536. */
  5537. static int megasas_probe_one(struct pci_dev *pdev,
  5538. const struct pci_device_id *id)
  5539. {
  5540. int rval, pos;
  5541. struct Scsi_Host *host;
  5542. struct megasas_instance *instance;
  5543. u16 control = 0;
  5544. /* Reset MSI-X in the kdump kernel */
  5545. if (reset_devices) {
  5546. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  5547. if (pos) {
  5548. pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
  5549. &control);
  5550. if (control & PCI_MSIX_FLAGS_ENABLE) {
  5551. dev_info(&pdev->dev, "resetting MSI-X\n");
  5552. pci_write_config_word(pdev,
  5553. pos + PCI_MSIX_FLAGS,
  5554. control &
  5555. ~PCI_MSIX_FLAGS_ENABLE);
  5556. }
  5557. }
  5558. }
  5559. /*
  5560. * PCI prepping: enable device set bus mastering and dma mask
  5561. */
  5562. rval = pci_enable_device_mem(pdev);
  5563. if (rval) {
  5564. return rval;
  5565. }
  5566. pci_set_master(pdev);
  5567. host = scsi_host_alloc(&megasas_template,
  5568. sizeof(struct megasas_instance));
  5569. if (!host) {
  5570. dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
  5571. goto fail_alloc_instance;
  5572. }
  5573. instance = (struct megasas_instance *)host->hostdata;
  5574. memset(instance, 0, sizeof(*instance));
  5575. atomic_set(&instance->fw_reset_no_pci_access, 0);
  5576. /*
  5577. * Initialize PCI related and misc parameters
  5578. */
  5579. instance->pdev = pdev;
  5580. instance->host = host;
  5581. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  5582. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  5583. megasas_set_adapter_type(instance);
  5584. /*
  5585. * Initialize MFI Firmware
  5586. */
  5587. if (megasas_init_fw(instance))
  5588. goto fail_init_mfi;
  5589. if (instance->requestorId) {
  5590. if (instance->PlasmaFW111) {
  5591. instance->vf_affiliation_111 =
  5592. pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
  5593. &instance->vf_affiliation_111_h);
  5594. if (!instance->vf_affiliation_111)
  5595. dev_warn(&pdev->dev, "Can't allocate "
  5596. "memory for VF affiliation buffer\n");
  5597. } else {
  5598. instance->vf_affiliation =
  5599. pci_alloc_consistent(pdev,
  5600. (MAX_LOGICAL_DRIVES + 1) *
  5601. sizeof(struct MR_LD_VF_AFFILIATION),
  5602. &instance->vf_affiliation_h);
  5603. if (!instance->vf_affiliation)
  5604. dev_warn(&pdev->dev, "Can't allocate "
  5605. "memory for VF affiliation buffer\n");
  5606. }
  5607. }
  5608. /*
  5609. * Store instance in PCI softstate
  5610. */
  5611. pci_set_drvdata(pdev, instance);
  5612. /*
  5613. * Add this controller to megasas_mgmt_info structure so that it
  5614. * can be exported to management applications
  5615. */
  5616. megasas_mgmt_info.count++;
  5617. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  5618. megasas_mgmt_info.max_index++;
  5619. /*
  5620. * Register with SCSI mid-layer
  5621. */
  5622. if (megasas_io_attach(instance))
  5623. goto fail_io_attach;
  5624. instance->unload = 0;
  5625. /*
  5626. * Trigger SCSI to scan our drives
  5627. */
  5628. scsi_scan_host(host);
  5629. /*
  5630. * Initiate AEN (Asynchronous Event Notification)
  5631. */
  5632. if (megasas_start_aen(instance)) {
  5633. dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
  5634. goto fail_start_aen;
  5635. }
  5636. /* Get current SR-IOV LD/VF affiliation */
  5637. if (instance->requestorId)
  5638. megasas_get_ld_vf_affiliation(instance, 1);
  5639. return 0;
  5640. fail_start_aen:
  5641. fail_io_attach:
  5642. megasas_mgmt_info.count--;
  5643. megasas_mgmt_info.max_index--;
  5644. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  5645. instance->instancet->disable_intr(instance);
  5646. megasas_destroy_irqs(instance);
  5647. if (instance->adapter_type != MFI_SERIES)
  5648. megasas_release_fusion(instance);
  5649. else
  5650. megasas_release_mfi(instance);
  5651. if (instance->msix_vectors)
  5652. pci_free_irq_vectors(instance->pdev);
  5653. fail_init_mfi:
  5654. scsi_host_put(host);
  5655. fail_alloc_instance:
  5656. pci_disable_device(pdev);
  5657. return -ENODEV;
  5658. }
  5659. /**
  5660. * megasas_flush_cache - Requests FW to flush all its caches
  5661. * @instance: Adapter soft state
  5662. */
  5663. static void megasas_flush_cache(struct megasas_instance *instance)
  5664. {
  5665. struct megasas_cmd *cmd;
  5666. struct megasas_dcmd_frame *dcmd;
  5667. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  5668. return;
  5669. cmd = megasas_get_cmd(instance);
  5670. if (!cmd)
  5671. return;
  5672. dcmd = &cmd->frame->dcmd;
  5673. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5674. dcmd->cmd = MFI_CMD_DCMD;
  5675. dcmd->cmd_status = 0x0;
  5676. dcmd->sge_count = 0;
  5677. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  5678. dcmd->timeout = 0;
  5679. dcmd->pad_0 = 0;
  5680. dcmd->data_xfer_len = 0;
  5681. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
  5682. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  5683. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
  5684. != DCMD_SUCCESS) {
  5685. dev_err(&instance->pdev->dev,
  5686. "return from %s %d\n", __func__, __LINE__);
  5687. return;
  5688. }
  5689. megasas_return_cmd(instance, cmd);
  5690. }
  5691. /**
  5692. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  5693. * @instance: Adapter soft state
  5694. * @opcode: Shutdown/Hibernate
  5695. */
  5696. static void megasas_shutdown_controller(struct megasas_instance *instance,
  5697. u32 opcode)
  5698. {
  5699. struct megasas_cmd *cmd;
  5700. struct megasas_dcmd_frame *dcmd;
  5701. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  5702. return;
  5703. cmd = megasas_get_cmd(instance);
  5704. if (!cmd)
  5705. return;
  5706. if (instance->aen_cmd)
  5707. megasas_issue_blocked_abort_cmd(instance,
  5708. instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
  5709. if (instance->map_update_cmd)
  5710. megasas_issue_blocked_abort_cmd(instance,
  5711. instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
  5712. if (instance->jbod_seq_cmd)
  5713. megasas_issue_blocked_abort_cmd(instance,
  5714. instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
  5715. dcmd = &cmd->frame->dcmd;
  5716. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5717. dcmd->cmd = MFI_CMD_DCMD;
  5718. dcmd->cmd_status = 0x0;
  5719. dcmd->sge_count = 0;
  5720. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  5721. dcmd->timeout = 0;
  5722. dcmd->pad_0 = 0;
  5723. dcmd->data_xfer_len = 0;
  5724. dcmd->opcode = cpu_to_le32(opcode);
  5725. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
  5726. != DCMD_SUCCESS) {
  5727. dev_err(&instance->pdev->dev,
  5728. "return from %s %d\n", __func__, __LINE__);
  5729. return;
  5730. }
  5731. megasas_return_cmd(instance, cmd);
  5732. }
  5733. #ifdef CONFIG_PM
  5734. /**
  5735. * megasas_suspend - driver suspend entry point
  5736. * @pdev: PCI device structure
  5737. * @state: PCI power state to suspend routine
  5738. */
  5739. static int
  5740. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  5741. {
  5742. struct Scsi_Host *host;
  5743. struct megasas_instance *instance;
  5744. instance = pci_get_drvdata(pdev);
  5745. host = instance->host;
  5746. instance->unload = 1;
  5747. /* Shutdown SR-IOV heartbeat timer */
  5748. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  5749. del_timer_sync(&instance->sriov_heartbeat_timer);
  5750. megasas_flush_cache(instance);
  5751. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  5752. /* cancel the delayed work if this work still in queue */
  5753. if (instance->ev != NULL) {
  5754. struct megasas_aen_event *ev = instance->ev;
  5755. cancel_delayed_work_sync(&ev->hotplug_work);
  5756. instance->ev = NULL;
  5757. }
  5758. tasklet_kill(&instance->isr_tasklet);
  5759. pci_set_drvdata(instance->pdev, instance);
  5760. instance->instancet->disable_intr(instance);
  5761. megasas_destroy_irqs(instance);
  5762. if (instance->msix_vectors)
  5763. pci_free_irq_vectors(instance->pdev);
  5764. pci_save_state(pdev);
  5765. pci_disable_device(pdev);
  5766. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  5767. return 0;
  5768. }
  5769. /**
  5770. * megasas_resume- driver resume entry point
  5771. * @pdev: PCI device structure
  5772. */
  5773. static int
  5774. megasas_resume(struct pci_dev *pdev)
  5775. {
  5776. int rval;
  5777. struct Scsi_Host *host;
  5778. struct megasas_instance *instance;
  5779. int irq_flags = PCI_IRQ_LEGACY;
  5780. instance = pci_get_drvdata(pdev);
  5781. host = instance->host;
  5782. pci_set_power_state(pdev, PCI_D0);
  5783. pci_enable_wake(pdev, PCI_D0, 0);
  5784. pci_restore_state(pdev);
  5785. /*
  5786. * PCI prepping: enable device set bus mastering and dma mask
  5787. */
  5788. rval = pci_enable_device_mem(pdev);
  5789. if (rval) {
  5790. dev_err(&pdev->dev, "Enable device failed\n");
  5791. return rval;
  5792. }
  5793. pci_set_master(pdev);
  5794. /*
  5795. * We expect the FW state to be READY
  5796. */
  5797. if (megasas_transition_to_ready(instance, 0))
  5798. goto fail_ready_state;
  5799. if (megasas_set_dma_mask(instance))
  5800. goto fail_set_dma_mask;
  5801. /*
  5802. * Initialize MFI Firmware
  5803. */
  5804. atomic_set(&instance->fw_outstanding, 0);
  5805. atomic_set(&instance->ldio_outstanding, 0);
  5806. /* Now re-enable MSI-X */
  5807. if (instance->msix_vectors) {
  5808. irq_flags = PCI_IRQ_MSIX;
  5809. if (smp_affinity_enable)
  5810. irq_flags |= PCI_IRQ_AFFINITY;
  5811. }
  5812. rval = pci_alloc_irq_vectors(instance->pdev, 1,
  5813. instance->msix_vectors ?
  5814. instance->msix_vectors : 1, irq_flags);
  5815. if (rval < 0)
  5816. goto fail_reenable_msix;
  5817. megasas_setup_reply_map(instance);
  5818. if (instance->adapter_type != MFI_SERIES) {
  5819. megasas_reset_reply_desc(instance);
  5820. if (megasas_ioc_init_fusion(instance)) {
  5821. megasas_free_cmds(instance);
  5822. megasas_free_cmds_fusion(instance);
  5823. goto fail_init_mfi;
  5824. }
  5825. if (!megasas_get_map_info(instance))
  5826. megasas_sync_map_info(instance);
  5827. } else {
  5828. *instance->producer = 0;
  5829. *instance->consumer = 0;
  5830. if (megasas_issue_init_mfi(instance))
  5831. goto fail_init_mfi;
  5832. }
  5833. if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
  5834. goto fail_init_mfi;
  5835. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  5836. (unsigned long)instance);
  5837. if (instance->msix_vectors ?
  5838. megasas_setup_irqs_msix(instance, 0) :
  5839. megasas_setup_irqs_ioapic(instance))
  5840. goto fail_init_mfi;
  5841. /* Re-launch SR-IOV heartbeat timer */
  5842. if (instance->requestorId) {
  5843. if (!megasas_sriov_start_heartbeat(instance, 0))
  5844. megasas_start_timer(instance);
  5845. else {
  5846. instance->skip_heartbeat_timer_del = 1;
  5847. goto fail_init_mfi;
  5848. }
  5849. }
  5850. instance->instancet->enable_intr(instance);
  5851. megasas_setup_jbod_map(instance);
  5852. instance->unload = 0;
  5853. /*
  5854. * Initiate AEN (Asynchronous Event Notification)
  5855. */
  5856. if (megasas_start_aen(instance))
  5857. dev_err(&instance->pdev->dev, "Start AEN failed\n");
  5858. return 0;
  5859. fail_init_mfi:
  5860. megasas_free_ctrl_dma_buffers(instance);
  5861. megasas_free_ctrl_mem(instance);
  5862. scsi_host_put(host);
  5863. fail_reenable_msix:
  5864. fail_set_dma_mask:
  5865. fail_ready_state:
  5866. pci_disable_device(pdev);
  5867. return -ENODEV;
  5868. }
  5869. #else
  5870. #define megasas_suspend NULL
  5871. #define megasas_resume NULL
  5872. #endif
  5873. static inline int
  5874. megasas_wait_for_adapter_operational(struct megasas_instance *instance)
  5875. {
  5876. int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
  5877. int i;
  5878. u8 adp_state;
  5879. for (i = 0; i < wait_time; i++) {
  5880. adp_state = atomic_read(&instance->adprecovery);
  5881. if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
  5882. (adp_state == MEGASAS_HW_CRITICAL_ERROR))
  5883. break;
  5884. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
  5885. dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
  5886. msleep(1000);
  5887. }
  5888. if (adp_state != MEGASAS_HBA_OPERATIONAL) {
  5889. dev_info(&instance->pdev->dev,
  5890. "%s HBA failed to become operational, adp_state %d\n",
  5891. __func__, adp_state);
  5892. return 1;
  5893. }
  5894. return 0;
  5895. }
  5896. /**
  5897. * megasas_detach_one - PCI hot"un"plug entry point
  5898. * @pdev: PCI device structure
  5899. */
  5900. static void megasas_detach_one(struct pci_dev *pdev)
  5901. {
  5902. int i;
  5903. struct Scsi_Host *host;
  5904. struct megasas_instance *instance;
  5905. struct fusion_context *fusion;
  5906. u32 pd_seq_map_sz;
  5907. instance = pci_get_drvdata(pdev);
  5908. host = instance->host;
  5909. fusion = instance->ctrl_context;
  5910. /* Shutdown SR-IOV heartbeat timer */
  5911. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  5912. del_timer_sync(&instance->sriov_heartbeat_timer);
  5913. if (instance->fw_crash_state != UNAVAILABLE)
  5914. megasas_free_host_crash_buffer(instance);
  5915. scsi_remove_host(instance->host);
  5916. instance->unload = 1;
  5917. if (megasas_wait_for_adapter_operational(instance))
  5918. goto skip_firing_dcmds;
  5919. megasas_flush_cache(instance);
  5920. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  5921. skip_firing_dcmds:
  5922. /* cancel the delayed work if this work still in queue*/
  5923. if (instance->ev != NULL) {
  5924. struct megasas_aen_event *ev = instance->ev;
  5925. cancel_delayed_work_sync(&ev->hotplug_work);
  5926. instance->ev = NULL;
  5927. }
  5928. /* cancel all wait events */
  5929. wake_up_all(&instance->int_cmd_wait_q);
  5930. tasklet_kill(&instance->isr_tasklet);
  5931. /*
  5932. * Take the instance off the instance array. Note that we will not
  5933. * decrement the max_index. We let this array be sparse array
  5934. */
  5935. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  5936. if (megasas_mgmt_info.instance[i] == instance) {
  5937. megasas_mgmt_info.count--;
  5938. megasas_mgmt_info.instance[i] = NULL;
  5939. break;
  5940. }
  5941. }
  5942. instance->instancet->disable_intr(instance);
  5943. megasas_destroy_irqs(instance);
  5944. if (instance->msix_vectors)
  5945. pci_free_irq_vectors(instance->pdev);
  5946. if (instance->adapter_type == VENTURA_SERIES) {
  5947. for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
  5948. kfree(fusion->stream_detect_by_ld[i]);
  5949. kfree(fusion->stream_detect_by_ld);
  5950. fusion->stream_detect_by_ld = NULL;
  5951. }
  5952. if (instance->adapter_type != MFI_SERIES) {
  5953. megasas_release_fusion(instance);
  5954. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  5955. (sizeof(struct MR_PD_CFG_SEQ) *
  5956. (MAX_PHYSICAL_DEVICES - 1));
  5957. for (i = 0; i < 2 ; i++) {
  5958. if (fusion->ld_map[i])
  5959. dma_free_coherent(&instance->pdev->dev,
  5960. fusion->max_map_sz,
  5961. fusion->ld_map[i],
  5962. fusion->ld_map_phys[i]);
  5963. if (fusion->ld_drv_map[i]) {
  5964. if (is_vmalloc_addr(fusion->ld_drv_map[i]))
  5965. vfree(fusion->ld_drv_map[i]);
  5966. else
  5967. free_pages((ulong)fusion->ld_drv_map[i],
  5968. fusion->drv_map_pages);
  5969. }
  5970. if (fusion->pd_seq_sync[i])
  5971. dma_free_coherent(&instance->pdev->dev,
  5972. pd_seq_map_sz,
  5973. fusion->pd_seq_sync[i],
  5974. fusion->pd_seq_phys[i]);
  5975. }
  5976. } else {
  5977. megasas_release_mfi(instance);
  5978. }
  5979. if (instance->vf_affiliation)
  5980. pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
  5981. sizeof(struct MR_LD_VF_AFFILIATION),
  5982. instance->vf_affiliation,
  5983. instance->vf_affiliation_h);
  5984. if (instance->vf_affiliation_111)
  5985. pci_free_consistent(pdev,
  5986. sizeof(struct MR_LD_VF_AFFILIATION_111),
  5987. instance->vf_affiliation_111,
  5988. instance->vf_affiliation_111_h);
  5989. if (instance->hb_host_mem)
  5990. pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
  5991. instance->hb_host_mem,
  5992. instance->hb_host_mem_h);
  5993. megasas_free_ctrl_dma_buffers(instance);
  5994. megasas_free_ctrl_mem(instance);
  5995. scsi_host_put(host);
  5996. pci_disable_device(pdev);
  5997. }
  5998. /**
  5999. * megasas_shutdown - Shutdown entry point
  6000. * @device: Generic device structure
  6001. */
  6002. static void megasas_shutdown(struct pci_dev *pdev)
  6003. {
  6004. struct megasas_instance *instance = pci_get_drvdata(pdev);
  6005. instance->unload = 1;
  6006. if (megasas_wait_for_adapter_operational(instance))
  6007. goto skip_firing_dcmds;
  6008. megasas_flush_cache(instance);
  6009. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  6010. skip_firing_dcmds:
  6011. instance->instancet->disable_intr(instance);
  6012. megasas_destroy_irqs(instance);
  6013. if (instance->msix_vectors)
  6014. pci_free_irq_vectors(instance->pdev);
  6015. }
  6016. /**
  6017. * megasas_mgmt_open - char node "open" entry point
  6018. */
  6019. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  6020. {
  6021. /*
  6022. * Allow only those users with admin rights
  6023. */
  6024. if (!capable(CAP_SYS_ADMIN))
  6025. return -EACCES;
  6026. return 0;
  6027. }
  6028. /**
  6029. * megasas_mgmt_fasync - Async notifier registration from applications
  6030. *
  6031. * This function adds the calling process to a driver global queue. When an
  6032. * event occurs, SIGIO will be sent to all processes in this queue.
  6033. */
  6034. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  6035. {
  6036. int rc;
  6037. mutex_lock(&megasas_async_queue_mutex);
  6038. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  6039. mutex_unlock(&megasas_async_queue_mutex);
  6040. if (rc >= 0) {
  6041. /* For sanity check when we get ioctl */
  6042. filep->private_data = filep;
  6043. return 0;
  6044. }
  6045. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  6046. return rc;
  6047. }
  6048. /**
  6049. * megasas_mgmt_poll - char node "poll" entry point
  6050. * */
  6051. static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
  6052. {
  6053. __poll_t mask;
  6054. unsigned long flags;
  6055. poll_wait(file, &megasas_poll_wait, wait);
  6056. spin_lock_irqsave(&poll_aen_lock, flags);
  6057. if (megasas_poll_wait_aen)
  6058. mask = (EPOLLIN | EPOLLRDNORM);
  6059. else
  6060. mask = 0;
  6061. megasas_poll_wait_aen = 0;
  6062. spin_unlock_irqrestore(&poll_aen_lock, flags);
  6063. return mask;
  6064. }
  6065. /*
  6066. * megasas_set_crash_dump_params_ioctl:
  6067. * Send CRASH_DUMP_MODE DCMD to all controllers
  6068. * @cmd: MFI command frame
  6069. */
  6070. static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
  6071. {
  6072. struct megasas_instance *local_instance;
  6073. int i, error = 0;
  6074. int crash_support;
  6075. crash_support = cmd->frame->dcmd.mbox.w[0];
  6076. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  6077. local_instance = megasas_mgmt_info.instance[i];
  6078. if (local_instance && local_instance->crash_dump_drv_support) {
  6079. if ((atomic_read(&local_instance->adprecovery) ==
  6080. MEGASAS_HBA_OPERATIONAL) &&
  6081. !megasas_set_crash_dump_params(local_instance,
  6082. crash_support)) {
  6083. local_instance->crash_dump_app_support =
  6084. crash_support;
  6085. dev_info(&local_instance->pdev->dev,
  6086. "Application firmware crash "
  6087. "dump mode set success\n");
  6088. error = 0;
  6089. } else {
  6090. dev_info(&local_instance->pdev->dev,
  6091. "Application firmware crash "
  6092. "dump mode set failed\n");
  6093. error = -1;
  6094. }
  6095. }
  6096. }
  6097. return error;
  6098. }
  6099. /**
  6100. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  6101. * @instance: Adapter soft state
  6102. * @argp: User's ioctl packet
  6103. */
  6104. static int
  6105. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  6106. struct megasas_iocpacket __user * user_ioc,
  6107. struct megasas_iocpacket *ioc)
  6108. {
  6109. struct megasas_sge64 *kern_sge64 = NULL;
  6110. struct megasas_sge32 *kern_sge32 = NULL;
  6111. struct megasas_cmd *cmd;
  6112. void *kbuff_arr[MAX_IOCTL_SGE];
  6113. dma_addr_t buf_handle = 0;
  6114. int error = 0, i;
  6115. void *sense = NULL;
  6116. dma_addr_t sense_handle;
  6117. void *sense_ptr;
  6118. u32 opcode = 0;
  6119. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  6120. if (ioc->sge_count > MAX_IOCTL_SGE) {
  6121. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
  6122. ioc->sge_count, MAX_IOCTL_SGE);
  6123. return -EINVAL;
  6124. }
  6125. if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
  6126. ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
  6127. !instance->support_nvme_passthru)) {
  6128. dev_err(&instance->pdev->dev,
  6129. "Received invalid ioctl command 0x%x\n",
  6130. ioc->frame.hdr.cmd);
  6131. return -ENOTSUPP;
  6132. }
  6133. cmd = megasas_get_cmd(instance);
  6134. if (!cmd) {
  6135. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
  6136. return -ENOMEM;
  6137. }
  6138. /*
  6139. * User's IOCTL packet has 2 frames (maximum). Copy those two
  6140. * frames into our cmd's frames. cmd->frame's context will get
  6141. * overwritten when we copy from user's frames. So set that value
  6142. * alone separately
  6143. */
  6144. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  6145. cmd->frame->hdr.context = cpu_to_le32(cmd->index);
  6146. cmd->frame->hdr.pad_0 = 0;
  6147. cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
  6148. if (instance->consistent_mask_64bit)
  6149. cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
  6150. MFI_FRAME_SENSE64));
  6151. else
  6152. cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
  6153. MFI_FRAME_SENSE64));
  6154. if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
  6155. opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
  6156. if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
  6157. if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
  6158. megasas_return_cmd(instance, cmd);
  6159. return -1;
  6160. }
  6161. }
  6162. if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
  6163. error = megasas_set_crash_dump_params_ioctl(cmd);
  6164. megasas_return_cmd(instance, cmd);
  6165. return error;
  6166. }
  6167. /*
  6168. * The management interface between applications and the fw uses
  6169. * MFI frames. E.g, RAID configuration changes, LD property changes
  6170. * etc are accomplishes through different kinds of MFI frames. The
  6171. * driver needs to care only about substituting user buffers with
  6172. * kernel buffers in SGLs. The location of SGL is embedded in the
  6173. * struct iocpacket itself.
  6174. */
  6175. if (instance->consistent_mask_64bit)
  6176. kern_sge64 = (struct megasas_sge64 *)
  6177. ((unsigned long)cmd->frame + ioc->sgl_off);
  6178. else
  6179. kern_sge32 = (struct megasas_sge32 *)
  6180. ((unsigned long)cmd->frame + ioc->sgl_off);
  6181. /*
  6182. * For each user buffer, create a mirror buffer and copy in
  6183. */
  6184. for (i = 0; i < ioc->sge_count; i++) {
  6185. if (!ioc->sgl[i].iov_len)
  6186. continue;
  6187. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  6188. ioc->sgl[i].iov_len,
  6189. &buf_handle, GFP_KERNEL);
  6190. if (!kbuff_arr[i]) {
  6191. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
  6192. "kernel SGL buffer for IOCTL\n");
  6193. error = -ENOMEM;
  6194. goto out;
  6195. }
  6196. /*
  6197. * We don't change the dma_coherent_mask, so
  6198. * pci_alloc_consistent only returns 32bit addresses
  6199. */
  6200. if (instance->consistent_mask_64bit) {
  6201. kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
  6202. kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
  6203. } else {
  6204. kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
  6205. kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
  6206. }
  6207. /*
  6208. * We created a kernel buffer corresponding to the
  6209. * user buffer. Now copy in from the user buffer
  6210. */
  6211. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  6212. (u32) (ioc->sgl[i].iov_len))) {
  6213. error = -EFAULT;
  6214. goto out;
  6215. }
  6216. }
  6217. if (ioc->sense_len) {
  6218. /* make sure the pointer is part of the frame */
  6219. if (ioc->sense_off >
  6220. (sizeof(union megasas_frame) - sizeof(__le64))) {
  6221. error = -EINVAL;
  6222. goto out;
  6223. }
  6224. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  6225. &sense_handle, GFP_KERNEL);
  6226. if (!sense) {
  6227. error = -ENOMEM;
  6228. goto out;
  6229. }
  6230. /* always store 64 bits regardless of addressing */
  6231. sense_ptr = (void *)cmd->frame + ioc->sense_off;
  6232. put_unaligned_le64(sense_handle, sense_ptr);
  6233. }
  6234. /*
  6235. * Set the sync_cmd flag so that the ISR knows not to complete this
  6236. * cmd to the SCSI mid-layer
  6237. */
  6238. cmd->sync_cmd = 1;
  6239. if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
  6240. cmd->sync_cmd = 0;
  6241. dev_err(&instance->pdev->dev,
  6242. "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
  6243. __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
  6244. cmd->cmd_status_drv);
  6245. return -EBUSY;
  6246. }
  6247. cmd->sync_cmd = 0;
  6248. if (instance->unload == 1) {
  6249. dev_info(&instance->pdev->dev, "Driver unload is in progress "
  6250. "don't submit data to application\n");
  6251. goto out;
  6252. }
  6253. /*
  6254. * copy out the kernel buffers to user buffers
  6255. */
  6256. for (i = 0; i < ioc->sge_count; i++) {
  6257. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  6258. ioc->sgl[i].iov_len)) {
  6259. error = -EFAULT;
  6260. goto out;
  6261. }
  6262. }
  6263. /*
  6264. * copy out the sense
  6265. */
  6266. if (ioc->sense_len) {
  6267. /*
  6268. * sense_ptr points to the location that has the user
  6269. * sense buffer address
  6270. */
  6271. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  6272. ioc->sense_off);
  6273. if (copy_to_user((void __user *)((unsigned long)
  6274. get_unaligned((unsigned long *)sense_ptr)),
  6275. sense, ioc->sense_len)) {
  6276. dev_err(&instance->pdev->dev, "Failed to copy out to user "
  6277. "sense data\n");
  6278. error = -EFAULT;
  6279. goto out;
  6280. }
  6281. }
  6282. /*
  6283. * copy the status codes returned by the fw
  6284. */
  6285. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  6286. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  6287. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
  6288. error = -EFAULT;
  6289. }
  6290. out:
  6291. if (sense) {
  6292. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  6293. sense, sense_handle);
  6294. }
  6295. for (i = 0; i < ioc->sge_count; i++) {
  6296. if (kbuff_arr[i]) {
  6297. if (instance->consistent_mask_64bit)
  6298. dma_free_coherent(&instance->pdev->dev,
  6299. le32_to_cpu(kern_sge64[i].length),
  6300. kbuff_arr[i],
  6301. le64_to_cpu(kern_sge64[i].phys_addr));
  6302. else
  6303. dma_free_coherent(&instance->pdev->dev,
  6304. le32_to_cpu(kern_sge32[i].length),
  6305. kbuff_arr[i],
  6306. le32_to_cpu(kern_sge32[i].phys_addr));
  6307. kbuff_arr[i] = NULL;
  6308. }
  6309. }
  6310. megasas_return_cmd(instance, cmd);
  6311. return error;
  6312. }
  6313. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  6314. {
  6315. struct megasas_iocpacket __user *user_ioc =
  6316. (struct megasas_iocpacket __user *)arg;
  6317. struct megasas_iocpacket *ioc;
  6318. struct megasas_instance *instance;
  6319. int error;
  6320. ioc = memdup_user(user_ioc, sizeof(*ioc));
  6321. if (IS_ERR(ioc))
  6322. return PTR_ERR(ioc);
  6323. instance = megasas_lookup_instance(ioc->host_no);
  6324. if (!instance) {
  6325. error = -ENODEV;
  6326. goto out_kfree_ioc;
  6327. }
  6328. /* Block ioctls in VF mode */
  6329. if (instance->requestorId && !allow_vf_ioctls) {
  6330. error = -ENODEV;
  6331. goto out_kfree_ioc;
  6332. }
  6333. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  6334. dev_err(&instance->pdev->dev, "Controller in crit error\n");
  6335. error = -ENODEV;
  6336. goto out_kfree_ioc;
  6337. }
  6338. if (instance->unload == 1) {
  6339. error = -ENODEV;
  6340. goto out_kfree_ioc;
  6341. }
  6342. if (down_interruptible(&instance->ioctl_sem)) {
  6343. error = -ERESTARTSYS;
  6344. goto out_kfree_ioc;
  6345. }
  6346. if (megasas_wait_for_adapter_operational(instance)) {
  6347. error = -ENODEV;
  6348. goto out_up;
  6349. }
  6350. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  6351. out_up:
  6352. up(&instance->ioctl_sem);
  6353. out_kfree_ioc:
  6354. kfree(ioc);
  6355. return error;
  6356. }
  6357. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  6358. {
  6359. struct megasas_instance *instance;
  6360. struct megasas_aen aen;
  6361. int error;
  6362. if (file->private_data != file) {
  6363. printk(KERN_DEBUG "megasas: fasync_helper was not "
  6364. "called first\n");
  6365. return -EINVAL;
  6366. }
  6367. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  6368. return -EFAULT;
  6369. instance = megasas_lookup_instance(aen.host_no);
  6370. if (!instance)
  6371. return -ENODEV;
  6372. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  6373. return -ENODEV;
  6374. }
  6375. if (instance->unload == 1) {
  6376. return -ENODEV;
  6377. }
  6378. if (megasas_wait_for_adapter_operational(instance))
  6379. return -ENODEV;
  6380. mutex_lock(&instance->reset_mutex);
  6381. error = megasas_register_aen(instance, aen.seq_num,
  6382. aen.class_locale_word);
  6383. mutex_unlock(&instance->reset_mutex);
  6384. return error;
  6385. }
  6386. /**
  6387. * megasas_mgmt_ioctl - char node ioctl entry point
  6388. */
  6389. static long
  6390. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  6391. {
  6392. switch (cmd) {
  6393. case MEGASAS_IOC_FIRMWARE:
  6394. return megasas_mgmt_ioctl_fw(file, arg);
  6395. case MEGASAS_IOC_GET_AEN:
  6396. return megasas_mgmt_ioctl_aen(file, arg);
  6397. }
  6398. return -ENOTTY;
  6399. }
  6400. #ifdef CONFIG_COMPAT
  6401. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  6402. {
  6403. struct compat_megasas_iocpacket __user *cioc =
  6404. (struct compat_megasas_iocpacket __user *)arg;
  6405. struct megasas_iocpacket __user *ioc =
  6406. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  6407. int i;
  6408. int error = 0;
  6409. compat_uptr_t ptr;
  6410. u32 local_sense_off;
  6411. u32 local_sense_len;
  6412. u32 user_sense_off;
  6413. if (clear_user(ioc, sizeof(*ioc)))
  6414. return -EFAULT;
  6415. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  6416. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  6417. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  6418. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  6419. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  6420. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  6421. return -EFAULT;
  6422. /*
  6423. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  6424. * sense_len is not null, so prepare the 64bit value under
  6425. * the same condition.
  6426. */
  6427. if (get_user(local_sense_off, &ioc->sense_off) ||
  6428. get_user(local_sense_len, &ioc->sense_len) ||
  6429. get_user(user_sense_off, &cioc->sense_off))
  6430. return -EFAULT;
  6431. if (local_sense_off != user_sense_off)
  6432. return -EINVAL;
  6433. if (local_sense_len) {
  6434. void __user **sense_ioc_ptr =
  6435. (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
  6436. compat_uptr_t *sense_cioc_ptr =
  6437. (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
  6438. if (get_user(ptr, sense_cioc_ptr) ||
  6439. put_user(compat_ptr(ptr), sense_ioc_ptr))
  6440. return -EFAULT;
  6441. }
  6442. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  6443. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  6444. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  6445. copy_in_user(&ioc->sgl[i].iov_len,
  6446. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  6447. return -EFAULT;
  6448. }
  6449. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  6450. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  6451. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  6452. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  6453. return -EFAULT;
  6454. }
  6455. return error;
  6456. }
  6457. static long
  6458. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  6459. unsigned long arg)
  6460. {
  6461. switch (cmd) {
  6462. case MEGASAS_IOC_FIRMWARE32:
  6463. return megasas_mgmt_compat_ioctl_fw(file, arg);
  6464. case MEGASAS_IOC_GET_AEN:
  6465. return megasas_mgmt_ioctl_aen(file, arg);
  6466. }
  6467. return -ENOTTY;
  6468. }
  6469. #endif
  6470. /*
  6471. * File operations structure for management interface
  6472. */
  6473. static const struct file_operations megasas_mgmt_fops = {
  6474. .owner = THIS_MODULE,
  6475. .open = megasas_mgmt_open,
  6476. .fasync = megasas_mgmt_fasync,
  6477. .unlocked_ioctl = megasas_mgmt_ioctl,
  6478. .poll = megasas_mgmt_poll,
  6479. #ifdef CONFIG_COMPAT
  6480. .compat_ioctl = megasas_mgmt_compat_ioctl,
  6481. #endif
  6482. .llseek = noop_llseek,
  6483. };
  6484. /*
  6485. * PCI hotplug support registration structure
  6486. */
  6487. static struct pci_driver megasas_pci_driver = {
  6488. .name = "megaraid_sas",
  6489. .id_table = megasas_pci_table,
  6490. .probe = megasas_probe_one,
  6491. .remove = megasas_detach_one,
  6492. .suspend = megasas_suspend,
  6493. .resume = megasas_resume,
  6494. .shutdown = megasas_shutdown,
  6495. };
  6496. /*
  6497. * Sysfs driver attributes
  6498. */
  6499. static ssize_t version_show(struct device_driver *dd, char *buf)
  6500. {
  6501. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  6502. MEGASAS_VERSION);
  6503. }
  6504. static DRIVER_ATTR_RO(version);
  6505. static ssize_t release_date_show(struct device_driver *dd, char *buf)
  6506. {
  6507. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  6508. MEGASAS_RELDATE);
  6509. }
  6510. static DRIVER_ATTR_RO(release_date);
  6511. static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
  6512. {
  6513. return sprintf(buf, "%u\n", support_poll_for_event);
  6514. }
  6515. static DRIVER_ATTR_RO(support_poll_for_event);
  6516. static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
  6517. {
  6518. return sprintf(buf, "%u\n", support_device_change);
  6519. }
  6520. static DRIVER_ATTR_RO(support_device_change);
  6521. static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
  6522. {
  6523. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  6524. }
  6525. static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
  6526. size_t count)
  6527. {
  6528. int retval = count;
  6529. if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
  6530. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  6531. retval = -EINVAL;
  6532. }
  6533. return retval;
  6534. }
  6535. static DRIVER_ATTR_RW(dbg_lvl);
  6536. static ssize_t
  6537. support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
  6538. {
  6539. return sprintf(buf, "%u\n", support_nvme_encapsulation);
  6540. }
  6541. static DRIVER_ATTR_RO(support_nvme_encapsulation);
  6542. static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
  6543. {
  6544. sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
  6545. scsi_remove_device(sdev);
  6546. scsi_device_put(sdev);
  6547. }
  6548. static void
  6549. megasas_aen_polling(struct work_struct *work)
  6550. {
  6551. struct megasas_aen_event *ev =
  6552. container_of(work, struct megasas_aen_event, hotplug_work.work);
  6553. struct megasas_instance *instance = ev->instance;
  6554. union megasas_evt_class_locale class_locale;
  6555. struct Scsi_Host *host;
  6556. struct scsi_device *sdev1;
  6557. u16 pd_index = 0;
  6558. u16 ld_index = 0;
  6559. int i, j, doscan = 0;
  6560. u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
  6561. int error;
  6562. u8 dcmd_ret = DCMD_SUCCESS;
  6563. if (!instance) {
  6564. printk(KERN_ERR "invalid instance!\n");
  6565. kfree(ev);
  6566. return;
  6567. }
  6568. /* Adjust event workqueue thread wait time for VF mode */
  6569. if (instance->requestorId)
  6570. wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
  6571. /* Don't run the event workqueue thread if OCR is running */
  6572. mutex_lock(&instance->reset_mutex);
  6573. instance->ev = NULL;
  6574. host = instance->host;
  6575. if (instance->evt_detail) {
  6576. megasas_decode_evt(instance);
  6577. switch (le32_to_cpu(instance->evt_detail->code)) {
  6578. case MR_EVT_PD_INSERTED:
  6579. case MR_EVT_PD_REMOVED:
  6580. dcmd_ret = megasas_get_pd_list(instance);
  6581. if (dcmd_ret == DCMD_SUCCESS)
  6582. doscan = SCAN_PD_CHANNEL;
  6583. break;
  6584. case MR_EVT_LD_OFFLINE:
  6585. case MR_EVT_CFG_CLEARED:
  6586. case MR_EVT_LD_DELETED:
  6587. case MR_EVT_LD_CREATED:
  6588. if (!instance->requestorId ||
  6589. (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
  6590. dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
  6591. if (dcmd_ret == DCMD_SUCCESS)
  6592. doscan = SCAN_VD_CHANNEL;
  6593. break;
  6594. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  6595. case MR_EVT_FOREIGN_CFG_IMPORTED:
  6596. case MR_EVT_LD_STATE_CHANGE:
  6597. dcmd_ret = megasas_get_pd_list(instance);
  6598. if (dcmd_ret != DCMD_SUCCESS)
  6599. break;
  6600. if (!instance->requestorId ||
  6601. (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
  6602. dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
  6603. if (dcmd_ret != DCMD_SUCCESS)
  6604. break;
  6605. doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
  6606. dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
  6607. instance->host->host_no);
  6608. break;
  6609. case MR_EVT_CTRL_PROP_CHANGED:
  6610. dcmd_ret = megasas_get_ctrl_info(instance);
  6611. break;
  6612. default:
  6613. doscan = 0;
  6614. break;
  6615. }
  6616. } else {
  6617. dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
  6618. mutex_unlock(&instance->reset_mutex);
  6619. kfree(ev);
  6620. return;
  6621. }
  6622. mutex_unlock(&instance->reset_mutex);
  6623. if (doscan & SCAN_PD_CHANNEL) {
  6624. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  6625. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  6626. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  6627. sdev1 = scsi_device_lookup(host, i, j, 0);
  6628. if (instance->pd_list[pd_index].driveState ==
  6629. MR_PD_STATE_SYSTEM) {
  6630. if (!sdev1)
  6631. scsi_add_device(host, i, j, 0);
  6632. else
  6633. scsi_device_put(sdev1);
  6634. } else {
  6635. if (sdev1)
  6636. megasas_remove_scsi_device(sdev1);
  6637. }
  6638. }
  6639. }
  6640. }
  6641. if (doscan & SCAN_VD_CHANNEL) {
  6642. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  6643. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  6644. ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  6645. sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  6646. if (instance->ld_ids[ld_index] != 0xff) {
  6647. if (!sdev1)
  6648. scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  6649. else
  6650. scsi_device_put(sdev1);
  6651. } else {
  6652. if (sdev1)
  6653. megasas_remove_scsi_device(sdev1);
  6654. }
  6655. }
  6656. }
  6657. }
  6658. if (dcmd_ret == DCMD_SUCCESS)
  6659. seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
  6660. else
  6661. seq_num = instance->last_seq_num;
  6662. /* Register AEN with FW for latest sequence number plus 1 */
  6663. class_locale.members.reserved = 0;
  6664. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  6665. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  6666. if (instance->aen_cmd != NULL) {
  6667. kfree(ev);
  6668. return;
  6669. }
  6670. mutex_lock(&instance->reset_mutex);
  6671. error = megasas_register_aen(instance, seq_num,
  6672. class_locale.word);
  6673. if (error)
  6674. dev_err(&instance->pdev->dev,
  6675. "register aen failed error %x\n", error);
  6676. mutex_unlock(&instance->reset_mutex);
  6677. kfree(ev);
  6678. }
  6679. /**
  6680. * megasas_init - Driver load entry point
  6681. */
  6682. static int __init megasas_init(void)
  6683. {
  6684. int rval;
  6685. /*
  6686. * Booted in kdump kernel, minimize memory footprints by
  6687. * disabling few features
  6688. */
  6689. if (reset_devices) {
  6690. msix_vectors = 1;
  6691. rdpq_enable = 0;
  6692. dual_qdepth_disable = 1;
  6693. }
  6694. /*
  6695. * Announce driver version and other information
  6696. */
  6697. pr_info("megasas: %s\n", MEGASAS_VERSION);
  6698. spin_lock_init(&poll_aen_lock);
  6699. support_poll_for_event = 2;
  6700. support_device_change = 1;
  6701. support_nvme_encapsulation = true;
  6702. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  6703. /*
  6704. * Register character device node
  6705. */
  6706. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  6707. if (rval < 0) {
  6708. printk(KERN_DEBUG "megasas: failed to open device node\n");
  6709. return rval;
  6710. }
  6711. megasas_mgmt_majorno = rval;
  6712. /*
  6713. * Register ourselves as PCI hotplug module
  6714. */
  6715. rval = pci_register_driver(&megasas_pci_driver);
  6716. if (rval) {
  6717. printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
  6718. goto err_pcidrv;
  6719. }
  6720. rval = driver_create_file(&megasas_pci_driver.driver,
  6721. &driver_attr_version);
  6722. if (rval)
  6723. goto err_dcf_attr_ver;
  6724. rval = driver_create_file(&megasas_pci_driver.driver,
  6725. &driver_attr_release_date);
  6726. if (rval)
  6727. goto err_dcf_rel_date;
  6728. rval = driver_create_file(&megasas_pci_driver.driver,
  6729. &driver_attr_support_poll_for_event);
  6730. if (rval)
  6731. goto err_dcf_support_poll_for_event;
  6732. rval = driver_create_file(&megasas_pci_driver.driver,
  6733. &driver_attr_dbg_lvl);
  6734. if (rval)
  6735. goto err_dcf_dbg_lvl;
  6736. rval = driver_create_file(&megasas_pci_driver.driver,
  6737. &driver_attr_support_device_change);
  6738. if (rval)
  6739. goto err_dcf_support_device_change;
  6740. rval = driver_create_file(&megasas_pci_driver.driver,
  6741. &driver_attr_support_nvme_encapsulation);
  6742. if (rval)
  6743. goto err_dcf_support_nvme_encapsulation;
  6744. return rval;
  6745. err_dcf_support_nvme_encapsulation:
  6746. driver_remove_file(&megasas_pci_driver.driver,
  6747. &driver_attr_support_device_change);
  6748. err_dcf_support_device_change:
  6749. driver_remove_file(&megasas_pci_driver.driver,
  6750. &driver_attr_dbg_lvl);
  6751. err_dcf_dbg_lvl:
  6752. driver_remove_file(&megasas_pci_driver.driver,
  6753. &driver_attr_support_poll_for_event);
  6754. err_dcf_support_poll_for_event:
  6755. driver_remove_file(&megasas_pci_driver.driver,
  6756. &driver_attr_release_date);
  6757. err_dcf_rel_date:
  6758. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  6759. err_dcf_attr_ver:
  6760. pci_unregister_driver(&megasas_pci_driver);
  6761. err_pcidrv:
  6762. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  6763. return rval;
  6764. }
  6765. /**
  6766. * megasas_exit - Driver unload entry point
  6767. */
  6768. static void __exit megasas_exit(void)
  6769. {
  6770. driver_remove_file(&megasas_pci_driver.driver,
  6771. &driver_attr_dbg_lvl);
  6772. driver_remove_file(&megasas_pci_driver.driver,
  6773. &driver_attr_support_poll_for_event);
  6774. driver_remove_file(&megasas_pci_driver.driver,
  6775. &driver_attr_support_device_change);
  6776. driver_remove_file(&megasas_pci_driver.driver,
  6777. &driver_attr_release_date);
  6778. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  6779. driver_remove_file(&megasas_pci_driver.driver,
  6780. &driver_attr_support_nvme_encapsulation);
  6781. pci_unregister_driver(&megasas_pci_driver);
  6782. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  6783. }
  6784. module_init(megasas_init);
  6785. module_exit(megasas_exit);