megaraid_sas_base.c 210 KB

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