megaraid_sas_base.c 182 KB

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