megaraid_sas_base.c 213 KB

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