megaraid_sas_base.c 197 KB

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