megaraid_sas_base.c 185 KB

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