pmcraid.c 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102
  1. /*
  2. * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
  3. *
  4. * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
  5. * PMC-Sierra Inc
  6. *
  7. * Copyright (C) 2008, 2009 PMC Sierra Inc
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
  22. * USA
  23. *
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/ioport.h>
  31. #include <linux/delay.h>
  32. #include <linux/pci.h>
  33. #include <linux/wait.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/sched.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/firmware.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/hdreg.h>
  42. #include <linux/io.h>
  43. #include <linux/slab.h>
  44. #include <asm/irq.h>
  45. #include <asm/processor.h>
  46. #include <linux/libata.h>
  47. #include <linux/mutex.h>
  48. #include <scsi/scsi.h>
  49. #include <scsi/scsi_host.h>
  50. #include <scsi/scsi_device.h>
  51. #include <scsi/scsi_tcq.h>
  52. #include <scsi/scsi_eh.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsicam.h>
  55. #include "pmcraid.h"
  56. /*
  57. * Module configuration parameters
  58. */
  59. static unsigned int pmcraid_debug_log;
  60. static unsigned int pmcraid_disable_aen;
  61. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  62. static unsigned int pmcraid_enable_msix;
  63. /*
  64. * Data structures to support multiple adapters by the LLD.
  65. * pmcraid_adapter_count - count of configured adapters
  66. */
  67. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  68. /*
  69. * Supporting user-level control interface through IOCTL commands.
  70. * pmcraid_major - major number to use
  71. * pmcraid_minor - minor number(s) to use
  72. */
  73. static unsigned int pmcraid_major;
  74. static struct class *pmcraid_class;
  75. DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  76. /*
  77. * Module parameters
  78. */
  79. MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
  80. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  81. MODULE_LICENSE("GPL");
  82. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  83. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  84. MODULE_PARM_DESC(log_level,
  85. "Enables firmware error code logging, default :1 high-severity"
  86. " errors, 2: all errors including high-severity errors,"
  87. " 0: disables logging");
  88. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  89. MODULE_PARM_DESC(debug,
  90. "Enable driver verbose message logging. Set 1 to enable."
  91. "(default: 0)");
  92. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  93. MODULE_PARM_DESC(disable_aen,
  94. "Disable driver aen notifications to apps. Set 1 to disable."
  95. "(default: 0)");
  96. /* chip specific constants for PMC MaxRAID controllers (same for
  97. * 0x5220 and 0x8010
  98. */
  99. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  100. {
  101. .ioastatus = 0x0,
  102. .ioarrin = 0x00040,
  103. .mailbox = 0x7FC30,
  104. .global_intr_mask = 0x00034,
  105. .ioa_host_intr = 0x0009C,
  106. .ioa_host_intr_clr = 0x000A0,
  107. .ioa_host_msix_intr = 0x7FC40,
  108. .ioa_host_mask = 0x7FC28,
  109. .ioa_host_mask_clr = 0x7FC28,
  110. .host_ioa_intr = 0x00020,
  111. .host_ioa_intr_clr = 0x00020,
  112. .transop_timeout = 300
  113. }
  114. };
  115. /*
  116. * PCI device ids supported by pmcraid driver
  117. */
  118. static struct pci_device_id pmcraid_pci_table[] = {
  119. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  120. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  121. },
  122. {}
  123. };
  124. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  125. /**
  126. * pmcraid_slave_alloc - Prepare for commands to a device
  127. * @scsi_dev: scsi device struct
  128. *
  129. * This function is called by mid-layer prior to sending any command to the new
  130. * device. Stores resource entry details of the device in scsi_device struct.
  131. * Queuecommand uses the resource handle and other details to fill up IOARCB
  132. * while sending commands to the device.
  133. *
  134. * Return value:
  135. * 0 on success / -ENXIO if device does not exist
  136. */
  137. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  138. {
  139. struct pmcraid_resource_entry *temp, *res = NULL;
  140. struct pmcraid_instance *pinstance;
  141. u8 target, bus, lun;
  142. unsigned long lock_flags;
  143. int rc = -ENXIO;
  144. u16 fw_version;
  145. pinstance = shost_priv(scsi_dev->host);
  146. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  147. /* Driver exposes VSET and GSCSI resources only; all other device types
  148. * are not exposed. Resource list is synchronized using resource lock
  149. * so any traversal or modifications to the list should be done inside
  150. * this lock
  151. */
  152. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  153. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  154. /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
  155. if (RES_IS_VSET(temp->cfg_entry)) {
  156. if (fw_version <= PMCRAID_FW_VERSION_1)
  157. target = temp->cfg_entry.unique_flags1;
  158. else
  159. target = temp->cfg_entry.array_id & 0xFF;
  160. if (target > PMCRAID_MAX_VSET_TARGETS)
  161. continue;
  162. bus = PMCRAID_VSET_BUS_ID;
  163. lun = 0;
  164. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  165. target = RES_TARGET(temp->cfg_entry.resource_address);
  166. bus = PMCRAID_PHYS_BUS_ID;
  167. lun = RES_LUN(temp->cfg_entry.resource_address);
  168. } else {
  169. continue;
  170. }
  171. if (bus == scsi_dev->channel &&
  172. target == scsi_dev->id &&
  173. lun == scsi_dev->lun) {
  174. res = temp;
  175. break;
  176. }
  177. }
  178. if (res) {
  179. res->scsi_dev = scsi_dev;
  180. scsi_dev->hostdata = res;
  181. res->change_detected = 0;
  182. atomic_set(&res->read_failures, 0);
  183. atomic_set(&res->write_failures, 0);
  184. rc = 0;
  185. }
  186. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  187. return rc;
  188. }
  189. /**
  190. * pmcraid_slave_configure - Configures a SCSI device
  191. * @scsi_dev: scsi device struct
  192. *
  193. * This function is executed by SCSI mid layer just after a device is first
  194. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  195. * timeout value (default 30s) will be over-written to a higher value (60s)
  196. * and max_sectors value will be over-written to 512. It also sets queue depth
  197. * to host->cmd_per_lun value
  198. *
  199. * Return value:
  200. * 0 on success
  201. */
  202. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  203. {
  204. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  205. if (!res)
  206. return 0;
  207. /* LLD exposes VSETs and Enclosure devices only */
  208. if (RES_IS_GSCSI(res->cfg_entry) &&
  209. scsi_dev->type != TYPE_ENCLOSURE)
  210. return -ENXIO;
  211. pmcraid_info("configuring %x:%x:%x:%x\n",
  212. scsi_dev->host->unique_id,
  213. scsi_dev->channel,
  214. scsi_dev->id,
  215. (u8)scsi_dev->lun);
  216. if (RES_IS_GSCSI(res->cfg_entry)) {
  217. scsi_dev->allow_restart = 1;
  218. } else if (RES_IS_VSET(res->cfg_entry)) {
  219. scsi_dev->allow_restart = 1;
  220. blk_queue_rq_timeout(scsi_dev->request_queue,
  221. PMCRAID_VSET_IO_TIMEOUT);
  222. blk_queue_max_hw_sectors(scsi_dev->request_queue,
  223. PMCRAID_VSET_MAX_SECTORS);
  224. }
  225. if (scsi_dev->tagged_supported &&
  226. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  227. scsi_adjust_queue_depth(scsi_dev, MSG_SIMPLE_TAG,
  228. scsi_dev->host->cmd_per_lun);
  229. } else {
  230. scsi_adjust_queue_depth(scsi_dev, 0,
  231. scsi_dev->host->cmd_per_lun);
  232. }
  233. return 0;
  234. }
  235. /**
  236. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  237. *
  238. * @scsi_dev: scsi device struct
  239. *
  240. * This is called by mid-layer before removing a device. Pointer assignments
  241. * done in pmcraid_slave_alloc will be reset to NULL here.
  242. *
  243. * Return value
  244. * none
  245. */
  246. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  247. {
  248. struct pmcraid_resource_entry *res;
  249. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  250. if (res)
  251. res->scsi_dev = NULL;
  252. scsi_dev->hostdata = NULL;
  253. }
  254. /**
  255. * pmcraid_change_queue_depth - Change the device's queue depth
  256. * @scsi_dev: scsi device struct
  257. * @depth: depth to set
  258. * @reason: calling context
  259. *
  260. * Return value
  261. * actual depth set
  262. */
  263. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth,
  264. int reason)
  265. {
  266. if (reason != SCSI_QDEPTH_DEFAULT)
  267. return -EOPNOTSUPP;
  268. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  269. depth = PMCRAID_MAX_CMD_PER_LUN;
  270. scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
  271. return scsi_dev->queue_depth;
  272. }
  273. /**
  274. * pmcraid_change_queue_type - Change the device's queue type
  275. * @scsi_dev: scsi device struct
  276. * @tag: type of tags to use
  277. *
  278. * Return value:
  279. * actual queue type set
  280. */
  281. static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
  282. {
  283. struct pmcraid_resource_entry *res;
  284. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  285. if (res && scsi_dev->tagged_supported &&
  286. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)))
  287. tag = scsi_change_queue_type(scsi_dev, tag);
  288. else
  289. tag = 0;
  290. return tag;
  291. }
  292. /**
  293. * pmcraid_init_cmdblk - initializes a command block
  294. *
  295. * @cmd: pointer to struct pmcraid_cmd to be initialized
  296. * @index: if >=0 first time initialization; otherwise reinitialization
  297. *
  298. * Return Value
  299. * None
  300. */
  301. void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  302. {
  303. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  304. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  305. if (index >= 0) {
  306. /* first time initialization (called from probe) */
  307. u32 ioasa_offset =
  308. offsetof(struct pmcraid_control_block, ioasa);
  309. cmd->index = index;
  310. ioarcb->response_handle = cpu_to_le32(index << 2);
  311. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  312. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  313. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  314. } else {
  315. /* re-initialization of various lengths, called once command is
  316. * processed by IOA
  317. */
  318. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  319. ioarcb->hrrq_id = 0;
  320. ioarcb->request_flags0 = 0;
  321. ioarcb->request_flags1 = 0;
  322. ioarcb->cmd_timeout = 0;
  323. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  324. ioarcb->ioadl_bus_addr = 0;
  325. ioarcb->ioadl_length = 0;
  326. ioarcb->data_transfer_length = 0;
  327. ioarcb->add_cmd_param_length = 0;
  328. ioarcb->add_cmd_param_offset = 0;
  329. cmd->ioa_cb->ioasa.ioasc = 0;
  330. cmd->ioa_cb->ioasa.residual_data_length = 0;
  331. cmd->time_left = 0;
  332. }
  333. cmd->cmd_done = NULL;
  334. cmd->scsi_cmd = NULL;
  335. cmd->release = 0;
  336. cmd->completion_req = 0;
  337. cmd->sense_buffer = 0;
  338. cmd->sense_buffer_dma = 0;
  339. cmd->dma_handle = 0;
  340. init_timer(&cmd->timer);
  341. }
  342. /**
  343. * pmcraid_reinit_cmdblk - reinitialize a command block
  344. *
  345. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  346. *
  347. * Return Value
  348. * None
  349. */
  350. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  351. {
  352. pmcraid_init_cmdblk(cmd, -1);
  353. }
  354. /**
  355. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  356. * @pinstance: adapter instance structure
  357. *
  358. * Return Value:
  359. * returns pointer to cmd block or NULL if no blocks are available
  360. */
  361. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  362. struct pmcraid_instance *pinstance
  363. )
  364. {
  365. struct pmcraid_cmd *cmd = NULL;
  366. unsigned long lock_flags;
  367. /* free cmd block list is protected by free_pool_lock */
  368. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  369. if (!list_empty(&pinstance->free_cmd_pool)) {
  370. cmd = list_entry(pinstance->free_cmd_pool.next,
  371. struct pmcraid_cmd, free_list);
  372. list_del(&cmd->free_list);
  373. }
  374. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  375. /* Initialize the command block before giving it the caller */
  376. if (cmd != NULL)
  377. pmcraid_reinit_cmdblk(cmd);
  378. return cmd;
  379. }
  380. /**
  381. * pmcraid_return_cmd - return a completed command block back into free pool
  382. * @cmd: pointer to the command block
  383. *
  384. * Return Value:
  385. * nothing
  386. */
  387. void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  388. {
  389. struct pmcraid_instance *pinstance = cmd->drv_inst;
  390. unsigned long lock_flags;
  391. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  392. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  393. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  394. }
  395. /**
  396. * pmcraid_read_interrupts - reads IOA interrupts
  397. *
  398. * @pinstance: pointer to adapter instance structure
  399. *
  400. * Return value
  401. * interrupts read from IOA
  402. */
  403. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  404. {
  405. return (pinstance->interrupt_mode) ?
  406. ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
  407. ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  408. }
  409. /**
  410. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  411. *
  412. * @pinstance: pointer to per adapter instance structure
  413. * @intrs: interrupts to disable
  414. *
  415. * Return Value
  416. * None
  417. */
  418. static void pmcraid_disable_interrupts(
  419. struct pmcraid_instance *pinstance,
  420. u32 intrs
  421. )
  422. {
  423. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  424. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  425. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  426. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  427. ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  428. if (!pinstance->interrupt_mode) {
  429. iowrite32(intrs,
  430. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  431. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  432. }
  433. }
  434. /**
  435. * pmcraid_enable_interrupts - Enables specified interrupts
  436. *
  437. * @pinstance: pointer to per adapter instance structure
  438. * @intr: interrupts to enable
  439. *
  440. * Return Value
  441. * None
  442. */
  443. static void pmcraid_enable_interrupts(
  444. struct pmcraid_instance *pinstance,
  445. u32 intrs
  446. )
  447. {
  448. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  449. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  450. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  451. if (!pinstance->interrupt_mode) {
  452. iowrite32(~intrs,
  453. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  454. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  455. }
  456. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  457. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  458. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  459. }
  460. /**
  461. * pmcraid_clr_trans_op - clear trans to op interrupt
  462. *
  463. * @pinstance: pointer to per adapter instance structure
  464. *
  465. * Return Value
  466. * None
  467. */
  468. static void pmcraid_clr_trans_op(
  469. struct pmcraid_instance *pinstance
  470. )
  471. {
  472. unsigned long lock_flags;
  473. if (!pinstance->interrupt_mode) {
  474. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  475. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  476. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  477. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  478. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  479. ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
  480. }
  481. if (pinstance->reset_cmd != NULL) {
  482. del_timer(&pinstance->reset_cmd->timer);
  483. spin_lock_irqsave(
  484. pinstance->host->host_lock, lock_flags);
  485. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  486. spin_unlock_irqrestore(
  487. pinstance->host->host_lock, lock_flags);
  488. }
  489. }
  490. /**
  491. * pmcraid_reset_type - Determine the required reset type
  492. * @pinstance: pointer to adapter instance structure
  493. *
  494. * IOA requires hard reset if any of the following conditions is true.
  495. * 1. If HRRQ valid interrupt is not masked
  496. * 2. IOA reset alert doorbell is set
  497. * 3. If there are any error interrupts
  498. */
  499. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  500. {
  501. u32 mask;
  502. u32 intrs;
  503. u32 alerts;
  504. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  505. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  506. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  507. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  508. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  509. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  510. pmcraid_info("IOA requires hard reset\n");
  511. pinstance->ioa_hard_reset = 1;
  512. }
  513. /* If unit check is active, trigger the dump */
  514. if (intrs & INTRS_IOA_UNIT_CHECK)
  515. pinstance->ioa_unit_check = 1;
  516. }
  517. /**
  518. * pmcraid_bist_done - completion function for PCI BIST
  519. * @cmd: pointer to reset command
  520. * Return Value
  521. * none
  522. */
  523. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  524. static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
  525. {
  526. struct pmcraid_instance *pinstance = cmd->drv_inst;
  527. unsigned long lock_flags;
  528. int rc;
  529. u16 pci_reg;
  530. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  531. /* If PCI config space can't be accessed wait for another two secs */
  532. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  533. cmd->time_left > 0) {
  534. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  535. cmd->timer.expires = jiffies + cmd->time_left;
  536. cmd->time_left = 0;
  537. cmd->timer.data = (unsigned long)cmd;
  538. cmd->timer.function =
  539. (void (*)(unsigned long))pmcraid_bist_done;
  540. add_timer(&cmd->timer);
  541. } else {
  542. cmd->time_left = 0;
  543. pmcraid_info("BIST is complete, proceeding with reset\n");
  544. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  545. pmcraid_ioa_reset(cmd);
  546. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  547. }
  548. }
  549. /**
  550. * pmcraid_start_bist - starts BIST
  551. * @cmd: pointer to reset cmd
  552. * Return Value
  553. * none
  554. */
  555. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  556. {
  557. struct pmcraid_instance *pinstance = cmd->drv_inst;
  558. u32 doorbells, intrs;
  559. /* proceed with bist and wait for 2 seconds */
  560. iowrite32(DOORBELL_IOA_START_BIST,
  561. pinstance->int_regs.host_ioa_interrupt_reg);
  562. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  563. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  564. pmcraid_info("doorbells after start bist: %x intrs: %x\n",
  565. doorbells, intrs);
  566. cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  567. cmd->timer.data = (unsigned long)cmd;
  568. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  569. cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
  570. add_timer(&cmd->timer);
  571. }
  572. /**
  573. * pmcraid_reset_alert_done - completion routine for reset_alert
  574. * @cmd: pointer to command block used in reset sequence
  575. * Return value
  576. * None
  577. */
  578. static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
  579. {
  580. struct pmcraid_instance *pinstance = cmd->drv_inst;
  581. u32 status = ioread32(pinstance->ioa_status);
  582. unsigned long lock_flags;
  583. /* if the critical operation in progress bit is set or the wait times
  584. * out, invoke reset engine to proceed with hard reset. If there is
  585. * some more time to wait, restart the timer
  586. */
  587. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  588. cmd->time_left <= 0) {
  589. pmcraid_info("critical op is reset proceeding with reset\n");
  590. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  591. pmcraid_ioa_reset(cmd);
  592. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  593. } else {
  594. pmcraid_info("critical op is not yet reset waiting again\n");
  595. /* restart timer if some more time is available to wait */
  596. cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  597. cmd->timer.data = (unsigned long)cmd;
  598. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  599. cmd->timer.function =
  600. (void (*)(unsigned long))pmcraid_reset_alert_done;
  601. add_timer(&cmd->timer);
  602. }
  603. }
  604. /**
  605. * pmcraid_reset_alert - alerts IOA for a possible reset
  606. * @cmd : command block to be used for reset sequence.
  607. *
  608. * Return Value
  609. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  610. * successfully written to IOA. Returns non-zero in case pci_config_space
  611. * is not accessible
  612. */
  613. static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
  614. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  615. {
  616. struct pmcraid_instance *pinstance = cmd->drv_inst;
  617. u32 doorbells;
  618. int rc;
  619. u16 pci_reg;
  620. /* If we are able to access IOA PCI config space, alert IOA that we are
  621. * going to reset it soon. This enables IOA to preserv persistent error
  622. * data if any. In case memory space is not accessible, proceed with
  623. * BIST or slot_reset
  624. */
  625. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  626. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  627. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  628. * reset IOA doesn't generate any interrupts when CRITICAL
  629. * OPERATION bit is reset. A timer is started to wait for this
  630. * bit to be reset.
  631. */
  632. cmd->time_left = PMCRAID_RESET_TIMEOUT;
  633. cmd->timer.data = (unsigned long)cmd;
  634. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  635. cmd->timer.function =
  636. (void (*)(unsigned long))pmcraid_reset_alert_done;
  637. add_timer(&cmd->timer);
  638. iowrite32(DOORBELL_IOA_RESET_ALERT,
  639. pinstance->int_regs.host_ioa_interrupt_reg);
  640. doorbells =
  641. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  642. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  643. } else {
  644. pmcraid_info("PCI config is not accessible starting BIST\n");
  645. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  646. pmcraid_start_bist(cmd);
  647. }
  648. }
  649. /**
  650. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  651. *
  652. * @cmd : pointer to command structure, that got timedout
  653. *
  654. * This function blocks host requests and initiates an adapter reset.
  655. *
  656. * Return value:
  657. * None
  658. */
  659. static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
  660. {
  661. struct pmcraid_instance *pinstance = cmd->drv_inst;
  662. unsigned long lock_flags;
  663. dev_info(&pinstance->pdev->dev,
  664. "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
  665. cmd->ioa_cb->ioarcb.cdb[0]);
  666. /* Command timeouts result in hard reset sequence. The command that got
  667. * timed out may be the one used as part of reset sequence. In this
  668. * case restart reset sequence using the same command block even if
  669. * reset is in progress. Otherwise fail this command and get a free
  670. * command block to restart the reset sequence.
  671. */
  672. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  673. if (!pinstance->ioa_reset_in_progress) {
  674. pinstance->ioa_reset_attempts = 0;
  675. cmd = pmcraid_get_free_cmd(pinstance);
  676. /* If we are out of command blocks, just return here itself.
  677. * Some other command's timeout handler can do the reset job
  678. */
  679. if (cmd == NULL) {
  680. spin_unlock_irqrestore(pinstance->host->host_lock,
  681. lock_flags);
  682. pmcraid_err("no free cmnd block for timeout handler\n");
  683. return;
  684. }
  685. pinstance->reset_cmd = cmd;
  686. pinstance->ioa_reset_in_progress = 1;
  687. } else {
  688. pmcraid_info("reset is already in progress\n");
  689. if (pinstance->reset_cmd != cmd) {
  690. /* This command should have been given to IOA, this
  691. * command will be completed by fail_outstanding_cmds
  692. * anyway
  693. */
  694. pmcraid_err("cmd is pending but reset in progress\n");
  695. }
  696. /* If this command was being used as part of the reset
  697. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  698. * causes fail_outstanding_commands not to return the command
  699. * block back to free pool
  700. */
  701. if (cmd == pinstance->reset_cmd)
  702. cmd->cmd_done = pmcraid_ioa_reset;
  703. }
  704. /* Notify apps of important IOA bringup/bringdown sequences */
  705. if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
  706. pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
  707. pmcraid_notify_ioastate(pinstance,
  708. PMC_DEVICE_EVENT_RESET_START);
  709. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  710. scsi_block_requests(pinstance->host);
  711. pmcraid_reset_alert(cmd);
  712. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  713. }
  714. /**
  715. * pmcraid_internal_done - completion routine for internally generated cmds
  716. *
  717. * @cmd: command that got response from IOA
  718. *
  719. * Return Value:
  720. * none
  721. */
  722. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  723. {
  724. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  725. cmd->ioa_cb->ioarcb.cdb[0],
  726. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  727. /* Some of the internal commands are sent with callers blocking for the
  728. * response. Same will be indicated as part of cmd->completion_req
  729. * field. Response path needs to wake up any waiters waiting for cmd
  730. * completion if this flag is set.
  731. */
  732. if (cmd->completion_req) {
  733. cmd->completion_req = 0;
  734. complete(&cmd->wait_for_completion);
  735. }
  736. /* most of the internal commands are completed by caller itself, so
  737. * no need to return the command block back to free pool until we are
  738. * required to do so (e.g once done with initialization).
  739. */
  740. if (cmd->release) {
  741. cmd->release = 0;
  742. pmcraid_return_cmd(cmd);
  743. }
  744. }
  745. /**
  746. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  747. *
  748. * @cmd: command that got response from IOA
  749. *
  750. * This routine is called after driver re-reads configuration table due to a
  751. * lost CCN. It returns the command block back to free pool and schedules
  752. * worker thread to add/delete devices into the system.
  753. *
  754. * Return Value:
  755. * none
  756. */
  757. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  758. {
  759. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  760. cmd->ioa_cb->ioarcb.cdb[0],
  761. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  762. if (cmd->release) {
  763. cmd->release = 0;
  764. pmcraid_return_cmd(cmd);
  765. }
  766. pmcraid_info("scheduling worker for config table reinitialization\n");
  767. schedule_work(&cmd->drv_inst->worker_q);
  768. }
  769. /**
  770. * pmcraid_erp_done - Process completion of SCSI error response from device
  771. * @cmd: pmcraid_command
  772. *
  773. * This function copies the sense buffer into the scsi_cmd struct and completes
  774. * scsi_cmd by calling scsi_done function.
  775. *
  776. * Return value:
  777. * none
  778. */
  779. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  780. {
  781. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  782. struct pmcraid_instance *pinstance = cmd->drv_inst;
  783. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  784. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  785. scsi_cmd->result |= (DID_ERROR << 16);
  786. scmd_printk(KERN_INFO, scsi_cmd,
  787. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  788. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  789. }
  790. /* if we had allocated sense buffers for request sense, copy the sense
  791. * release the buffers
  792. */
  793. if (cmd->sense_buffer != NULL) {
  794. memcpy(scsi_cmd->sense_buffer,
  795. cmd->sense_buffer,
  796. SCSI_SENSE_BUFFERSIZE);
  797. pci_free_consistent(pinstance->pdev,
  798. SCSI_SENSE_BUFFERSIZE,
  799. cmd->sense_buffer, cmd->sense_buffer_dma);
  800. cmd->sense_buffer = NULL;
  801. cmd->sense_buffer_dma = 0;
  802. }
  803. scsi_dma_unmap(scsi_cmd);
  804. pmcraid_return_cmd(cmd);
  805. scsi_cmd->scsi_done(scsi_cmd);
  806. }
  807. /**
  808. * pmcraid_fire_command - sends an IOA command to adapter
  809. *
  810. * This function adds the given block into pending command list
  811. * and returns without waiting
  812. *
  813. * @cmd : command to be sent to the device
  814. *
  815. * Return Value
  816. * None
  817. */
  818. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  819. {
  820. struct pmcraid_instance *pinstance = cmd->drv_inst;
  821. unsigned long lock_flags;
  822. /* Add this command block to pending cmd pool. We do this prior to
  823. * writting IOARCB to ioarrin because IOA might complete the command
  824. * by the time we are about to add it to the list. Response handler
  825. * (isr/tasklet) looks for cmd block in the pending pending list.
  826. */
  827. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  828. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  829. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  830. atomic_inc(&pinstance->outstanding_cmds);
  831. /* driver writes lower 32-bit value of IOARCB address only */
  832. mb();
  833. iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
  834. pinstance->ioarrin);
  835. }
  836. /**
  837. * pmcraid_send_cmd - fires a command to IOA
  838. *
  839. * This function also sets up timeout function, and command completion
  840. * function
  841. *
  842. * @cmd: pointer to the command block to be fired to IOA
  843. * @cmd_done: command completion function, called once IOA responds
  844. * @timeout: timeout to wait for this command completion
  845. * @timeout_func: timeout handler
  846. *
  847. * Return value
  848. * none
  849. */
  850. static void pmcraid_send_cmd(
  851. struct pmcraid_cmd *cmd,
  852. void (*cmd_done) (struct pmcraid_cmd *),
  853. unsigned long timeout,
  854. void (*timeout_func) (struct pmcraid_cmd *)
  855. )
  856. {
  857. /* initialize done function */
  858. cmd->cmd_done = cmd_done;
  859. if (timeout_func) {
  860. /* setup timeout handler */
  861. cmd->timer.data = (unsigned long)cmd;
  862. cmd->timer.expires = jiffies + timeout;
  863. cmd->timer.function = (void (*)(unsigned long))timeout_func;
  864. add_timer(&cmd->timer);
  865. }
  866. /* fire the command to IOA */
  867. _pmcraid_fire_command(cmd);
  868. }
  869. /**
  870. * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
  871. * @cmd: pointer to the command block used for sending IOA shutdown command
  872. *
  873. * Return value
  874. * None
  875. */
  876. static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
  877. {
  878. struct pmcraid_instance *pinstance = cmd->drv_inst;
  879. unsigned long lock_flags;
  880. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  881. pmcraid_ioa_reset(cmd);
  882. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  883. }
  884. /**
  885. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  886. *
  887. * @cmd: pointer to the command block used as part of reset sequence
  888. *
  889. * Return Value
  890. * None
  891. */
  892. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  893. {
  894. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  895. cmd->ioa_cb->ioarcb.cdb[0],
  896. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  897. /* Note that commands sent during reset require next command to be sent
  898. * to IOA. Hence reinit the done function as well as timeout function
  899. */
  900. pmcraid_reinit_cmdblk(cmd);
  901. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  902. cmd->ioa_cb->ioarcb.resource_handle =
  903. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  904. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  905. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  906. /* fire shutdown command to hardware. */
  907. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  908. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  909. pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
  910. pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
  911. PMCRAID_SHUTDOWN_TIMEOUT,
  912. pmcraid_timeout_handler);
  913. }
  914. /**
  915. * pmcraid_get_fwversion_done - completion function for get_fwversion
  916. *
  917. * @cmd: pointer to command block used to send INQUIRY command
  918. *
  919. * Return Value
  920. * none
  921. */
  922. static void pmcraid_querycfg(struct pmcraid_cmd *);
  923. static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
  924. {
  925. struct pmcraid_instance *pinstance = cmd->drv_inst;
  926. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  927. unsigned long lock_flags;
  928. /* configuration table entry size depends on firmware version. If fw
  929. * version is not known, it is not possible to interpret IOA config
  930. * table
  931. */
  932. if (ioasc) {
  933. pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
  934. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  935. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  936. pmcraid_reset_alert(cmd);
  937. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  938. } else {
  939. pmcraid_querycfg(cmd);
  940. }
  941. }
  942. /**
  943. * pmcraid_get_fwversion - reads firmware version information
  944. *
  945. * @cmd: pointer to command block used to send INQUIRY command
  946. *
  947. * Return Value
  948. * none
  949. */
  950. static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
  951. {
  952. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  953. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  954. struct pmcraid_instance *pinstance = cmd->drv_inst;
  955. u16 data_size = sizeof(struct pmcraid_inquiry_data);
  956. pmcraid_reinit_cmdblk(cmd);
  957. ioarcb->request_type = REQ_TYPE_SCSI;
  958. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  959. ioarcb->cdb[0] = INQUIRY;
  960. ioarcb->cdb[1] = 1;
  961. ioarcb->cdb[2] = 0xD0;
  962. ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
  963. ioarcb->cdb[4] = data_size & 0xFF;
  964. /* Since entire inquiry data it can be part of IOARCB itself
  965. */
  966. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  967. offsetof(struct pmcraid_ioarcb,
  968. add_data.u.ioadl[0]));
  969. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  970. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  971. ioarcb->request_flags0 |= NO_LINK_DESCS;
  972. ioarcb->data_transfer_length = cpu_to_le32(data_size);
  973. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  974. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  975. ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
  976. ioadl->data_len = cpu_to_le32(data_size);
  977. pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
  978. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  979. }
  980. /**
  981. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  982. * @cmd: pointer to command block to be used for identify hrrq
  983. *
  984. * Return Value
  985. * none
  986. */
  987. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  988. {
  989. struct pmcraid_instance *pinstance = cmd->drv_inst;
  990. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  991. int index = cmd->hrrq_index;
  992. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  993. u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  994. void (*done_function)(struct pmcraid_cmd *);
  995. pmcraid_reinit_cmdblk(cmd);
  996. cmd->hrrq_index = index + 1;
  997. if (cmd->hrrq_index < pinstance->num_hrrq) {
  998. done_function = pmcraid_identify_hrrq;
  999. } else {
  1000. cmd->hrrq_index = 0;
  1001. done_function = pmcraid_get_fwversion;
  1002. }
  1003. /* Initialize ioarcb */
  1004. ioarcb->request_type = REQ_TYPE_IOACMD;
  1005. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1006. /* initialize the hrrq number where IOA will respond to this command */
  1007. ioarcb->hrrq_id = index;
  1008. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  1009. ioarcb->cdb[1] = index;
  1010. /* IOA expects 64-bit pci address to be written in B.E format
  1011. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  1012. */
  1013. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
  1014. hrrq_addr, ioarcb->ioarcb_bus_addr, index);
  1015. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  1016. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  1017. /* Subsequent commands require HRRQ identification to be successful.
  1018. * Note that this gets called even during reset from SCSI mid-layer
  1019. * or tasklet
  1020. */
  1021. pmcraid_send_cmd(cmd, done_function,
  1022. PMCRAID_INTERNAL_TIMEOUT,
  1023. pmcraid_timeout_handler);
  1024. }
  1025. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  1026. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  1027. /**
  1028. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  1029. *
  1030. * @cmd: initialized command block pointer
  1031. *
  1032. * Return Value
  1033. * none
  1034. */
  1035. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  1036. {
  1037. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  1038. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  1039. else
  1040. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  1041. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  1042. }
  1043. /**
  1044. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  1045. *
  1046. * @pinstance: pointer to adapter instance structure
  1047. * @type: HCAM type
  1048. *
  1049. * Return Value
  1050. * pointer to initialized pmcraid_cmd structure or NULL
  1051. */
  1052. static struct pmcraid_cmd *pmcraid_init_hcam
  1053. (
  1054. struct pmcraid_instance *pinstance,
  1055. u8 type
  1056. )
  1057. {
  1058. struct pmcraid_cmd *cmd;
  1059. struct pmcraid_ioarcb *ioarcb;
  1060. struct pmcraid_ioadl_desc *ioadl;
  1061. struct pmcraid_hostrcb *hcam;
  1062. void (*cmd_done) (struct pmcraid_cmd *);
  1063. dma_addr_t dma;
  1064. int rcb_size;
  1065. cmd = pmcraid_get_free_cmd(pinstance);
  1066. if (!cmd) {
  1067. pmcraid_err("no free command blocks for hcam\n");
  1068. return cmd;
  1069. }
  1070. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  1071. rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
  1072. cmd_done = pmcraid_process_ccn;
  1073. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  1074. hcam = &pinstance->ccn;
  1075. } else {
  1076. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  1077. cmd_done = pmcraid_process_ldn;
  1078. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  1079. hcam = &pinstance->ldn;
  1080. }
  1081. /* initialize command pointer used for HCAM registration */
  1082. hcam->cmd = cmd;
  1083. ioarcb = &cmd->ioa_cb->ioarcb;
  1084. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1085. offsetof(struct pmcraid_ioarcb,
  1086. add_data.u.ioadl[0]));
  1087. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1088. ioadl = ioarcb->add_data.u.ioadl;
  1089. /* Initialize ioarcb */
  1090. ioarcb->request_type = REQ_TYPE_HCAM;
  1091. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1092. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  1093. ioarcb->cdb[1] = type;
  1094. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  1095. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  1096. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  1097. ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
  1098. ioadl[0].data_len = cpu_to_le32(rcb_size);
  1099. ioadl[0].address = cpu_to_le32(dma);
  1100. cmd->cmd_done = cmd_done;
  1101. return cmd;
  1102. }
  1103. /**
  1104. * pmcraid_send_hcam - Send an HCAM to IOA
  1105. * @pinstance: ioa config struct
  1106. * @type: HCAM type
  1107. *
  1108. * This function will send a Host Controlled Async command to IOA.
  1109. *
  1110. * Return value:
  1111. * none
  1112. */
  1113. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  1114. {
  1115. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  1116. pmcraid_send_hcam_cmd(cmd);
  1117. }
  1118. /**
  1119. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  1120. *
  1121. * @cmd: pointer to cmd that is used as cancelling command
  1122. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  1123. */
  1124. static void pmcraid_prepare_cancel_cmd(
  1125. struct pmcraid_cmd *cmd,
  1126. struct pmcraid_cmd *cmd_to_cancel
  1127. )
  1128. {
  1129. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1130. __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
  1131. /* Get the resource handle to where the command to be aborted has been
  1132. * sent.
  1133. */
  1134. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  1135. ioarcb->request_type = REQ_TYPE_IOACMD;
  1136. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1137. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  1138. /* IOARCB address of the command to be cancelled is given in
  1139. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  1140. * IOARCB address are not masked.
  1141. */
  1142. ioarcb_addr = cpu_to_be64(ioarcb_addr);
  1143. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  1144. }
  1145. /**
  1146. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1147. *
  1148. * @cmd: command to be used as cancelling command
  1149. * @type: HCAM type
  1150. * @cmd_done: op done function for the cancelling command
  1151. */
  1152. static void pmcraid_cancel_hcam(
  1153. struct pmcraid_cmd *cmd,
  1154. u8 type,
  1155. void (*cmd_done) (struct pmcraid_cmd *)
  1156. )
  1157. {
  1158. struct pmcraid_instance *pinstance;
  1159. struct pmcraid_hostrcb *hcam;
  1160. pinstance = cmd->drv_inst;
  1161. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1162. &pinstance->ldn : &pinstance->ccn;
  1163. /* prepare for cancelling previous hcam command. If the HCAM is
  1164. * currently not pending with IOA, we would have hcam->cmd as non-null
  1165. */
  1166. if (hcam->cmd == NULL)
  1167. return;
  1168. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1169. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1170. * schedule queuecommand while we are doing this
  1171. */
  1172. pmcraid_send_cmd(cmd, cmd_done,
  1173. PMCRAID_INTERNAL_TIMEOUT,
  1174. pmcraid_timeout_handler);
  1175. }
  1176. /**
  1177. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1178. *
  1179. * @cmd: command block to be used for cancelling the HCAM
  1180. */
  1181. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1182. {
  1183. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1184. cmd->ioa_cb->ioarcb.cdb[0],
  1185. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1186. pmcraid_reinit_cmdblk(cmd);
  1187. pmcraid_cancel_hcam(cmd,
  1188. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1189. pmcraid_ioa_shutdown);
  1190. }
  1191. /**
  1192. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1193. *
  1194. * @cmd: command block to be used for cancelling the HCAM
  1195. */
  1196. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1197. {
  1198. pmcraid_cancel_hcam(cmd,
  1199. PMCRAID_HCAM_CODE_LOG_DATA,
  1200. pmcraid_cancel_ccn);
  1201. }
  1202. /**
  1203. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1204. *
  1205. * @fw_version: firmware version code
  1206. * @cfgte: pointer to configuration table entry of the resource
  1207. *
  1208. * Return value:
  1209. * true if resource can be added to midlayer, false(0) otherwise
  1210. */
  1211. static int pmcraid_expose_resource(u16 fw_version,
  1212. struct pmcraid_config_table_entry *cfgte)
  1213. {
  1214. int retval = 0;
  1215. if (cfgte->resource_type == RES_TYPE_VSET) {
  1216. if (fw_version <= PMCRAID_FW_VERSION_1)
  1217. retval = ((cfgte->unique_flags1 & 0x80) == 0);
  1218. else
  1219. retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
  1220. (cfgte->unique_flags1 & 0x80) == 0);
  1221. } else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1222. retval = (RES_BUS(cfgte->resource_address) !=
  1223. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1224. return retval;
  1225. }
  1226. /* attributes supported by pmcraid_event_family */
  1227. enum {
  1228. PMCRAID_AEN_ATTR_UNSPEC,
  1229. PMCRAID_AEN_ATTR_EVENT,
  1230. __PMCRAID_AEN_ATTR_MAX,
  1231. };
  1232. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1233. /* commands supported by pmcraid_event_family */
  1234. enum {
  1235. PMCRAID_AEN_CMD_UNSPEC,
  1236. PMCRAID_AEN_CMD_EVENT,
  1237. __PMCRAID_AEN_CMD_MAX,
  1238. };
  1239. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1240. static struct genl_multicast_group pmcraid_mcgrps[] = {
  1241. { .name = "events", /* not really used - see ID discussion below */ },
  1242. };
  1243. static struct genl_family pmcraid_event_family = {
  1244. /*
  1245. * Due to prior multicast group abuse (the code having assumed that
  1246. * the family ID can be used as a multicast group ID) we need to
  1247. * statically allocate a family (and thus group) ID.
  1248. */
  1249. .id = GENL_ID_PMCRAID,
  1250. .name = "pmcraid",
  1251. .version = 1,
  1252. .maxattr = PMCRAID_AEN_ATTR_MAX,
  1253. .mcgrps = pmcraid_mcgrps,
  1254. .n_mcgrps = ARRAY_SIZE(pmcraid_mcgrps),
  1255. };
  1256. /**
  1257. * pmcraid_netlink_init - registers pmcraid_event_family
  1258. *
  1259. * Return value:
  1260. * 0 if the pmcraid_event_family is successfully registered
  1261. * with netlink generic, non-zero otherwise
  1262. */
  1263. static int pmcraid_netlink_init(void)
  1264. {
  1265. int result;
  1266. result = genl_register_family(&pmcraid_event_family);
  1267. if (result)
  1268. return result;
  1269. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1270. pmcraid_event_family.id);
  1271. return result;
  1272. }
  1273. /**
  1274. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1275. *
  1276. * Return value:
  1277. * none
  1278. */
  1279. static void pmcraid_netlink_release(void)
  1280. {
  1281. genl_unregister_family(&pmcraid_event_family);
  1282. }
  1283. /**
  1284. * pmcraid_notify_aen - sends event msg to user space application
  1285. * @pinstance: pointer to adapter instance structure
  1286. * @type: HCAM type
  1287. *
  1288. * Return value:
  1289. * 0 if success, error value in case of any failure.
  1290. */
  1291. static int pmcraid_notify_aen(
  1292. struct pmcraid_instance *pinstance,
  1293. struct pmcraid_aen_msg *aen_msg,
  1294. u32 data_size
  1295. )
  1296. {
  1297. struct sk_buff *skb;
  1298. void *msg_header;
  1299. u32 total_size, nla_genl_hdr_total_size;
  1300. int result;
  1301. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1302. MINOR(pinstance->cdev.dev));
  1303. aen_msg->length = data_size;
  1304. data_size += sizeof(*aen_msg);
  1305. total_size = nla_total_size(data_size);
  1306. /* Add GENL_HDR to total_size */
  1307. nla_genl_hdr_total_size =
  1308. (total_size + (GENL_HDRLEN +
  1309. ((struct genl_family *)&pmcraid_event_family)->hdrsize)
  1310. + NLMSG_HDRLEN);
  1311. skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
  1312. if (!skb) {
  1313. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1314. total_size);
  1315. return -ENOMEM;
  1316. }
  1317. /* add the genetlink message header */
  1318. msg_header = genlmsg_put(skb, 0, 0,
  1319. &pmcraid_event_family, 0,
  1320. PMCRAID_AEN_CMD_EVENT);
  1321. if (!msg_header) {
  1322. pmcraid_err("failed to copy command details\n");
  1323. nlmsg_free(skb);
  1324. return -ENOMEM;
  1325. }
  1326. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1327. if (result) {
  1328. pmcraid_err("failed to copy AEN attribute data\n");
  1329. nlmsg_free(skb);
  1330. return -EINVAL;
  1331. }
  1332. /* send genetlink multicast message to notify appplications */
  1333. result = genlmsg_end(skb, msg_header);
  1334. if (result < 0) {
  1335. pmcraid_err("genlmsg_end failed\n");
  1336. nlmsg_free(skb);
  1337. return result;
  1338. }
  1339. result = genlmsg_multicast(&pmcraid_event_family, skb,
  1340. 0, 0, GFP_ATOMIC);
  1341. /* If there are no listeners, genlmsg_multicast may return non-zero
  1342. * value.
  1343. */
  1344. if (result)
  1345. pmcraid_info("error (%x) sending aen event message\n", result);
  1346. return result;
  1347. }
  1348. /**
  1349. * pmcraid_notify_ccn - notifies about CCN event msg to user space
  1350. * @pinstance: pointer adapter instance structure
  1351. *
  1352. * Return value:
  1353. * 0 if success, error value in case of any failure
  1354. */
  1355. static int pmcraid_notify_ccn(struct pmcraid_instance *pinstance)
  1356. {
  1357. return pmcraid_notify_aen(pinstance,
  1358. pinstance->ccn.msg,
  1359. pinstance->ccn.hcam->data_len +
  1360. sizeof(struct pmcraid_hcam_hdr));
  1361. }
  1362. /**
  1363. * pmcraid_notify_ldn - notifies about CCN event msg to user space
  1364. * @pinstance: pointer adapter instance structure
  1365. *
  1366. * Return value:
  1367. * 0 if success, error value in case of any failure
  1368. */
  1369. static int pmcraid_notify_ldn(struct pmcraid_instance *pinstance)
  1370. {
  1371. return pmcraid_notify_aen(pinstance,
  1372. pinstance->ldn.msg,
  1373. pinstance->ldn.hcam->data_len +
  1374. sizeof(struct pmcraid_hcam_hdr));
  1375. }
  1376. /**
  1377. * pmcraid_notify_ioastate - sends IOA state event msg to user space
  1378. * @pinstance: pointer adapter instance structure
  1379. * @evt: controller state event to be sent
  1380. *
  1381. * Return value:
  1382. * 0 if success, error value in case of any failure
  1383. */
  1384. static void pmcraid_notify_ioastate(struct pmcraid_instance *pinstance, u32 evt)
  1385. {
  1386. pinstance->scn.ioa_state = evt;
  1387. pmcraid_notify_aen(pinstance,
  1388. &pinstance->scn.msg,
  1389. sizeof(u32));
  1390. }
  1391. /**
  1392. * pmcraid_handle_config_change - Handle a config change from the adapter
  1393. * @pinstance: pointer to per adapter instance structure
  1394. *
  1395. * Return value:
  1396. * none
  1397. */
  1398. static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
  1399. {
  1400. struct pmcraid_config_table_entry *cfg_entry;
  1401. struct pmcraid_hcam_ccn *ccn_hcam;
  1402. struct pmcraid_cmd *cmd;
  1403. struct pmcraid_cmd *cfgcmd;
  1404. struct pmcraid_resource_entry *res = NULL;
  1405. unsigned long lock_flags;
  1406. unsigned long host_lock_flags;
  1407. u32 new_entry = 1;
  1408. u32 hidden_entry = 0;
  1409. u16 fw_version;
  1410. int rc;
  1411. ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
  1412. cfg_entry = &ccn_hcam->cfg_entry;
  1413. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  1414. pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
  1415. res: %x:%x:%x:%x\n",
  1416. pinstance->ccn.hcam->ilid,
  1417. pinstance->ccn.hcam->op_code,
  1418. ((pinstance->ccn.hcam->timestamp1) |
  1419. ((pinstance->ccn.hcam->timestamp2 & 0xffffffffLL) << 32)),
  1420. pinstance->ccn.hcam->notification_type,
  1421. pinstance->ccn.hcam->notification_lost,
  1422. pinstance->ccn.hcam->flags,
  1423. pinstance->host->unique_id,
  1424. RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
  1425. (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
  1426. RES_BUS(cfg_entry->resource_address)),
  1427. RES_IS_VSET(*cfg_entry) ?
  1428. (fw_version <= PMCRAID_FW_VERSION_1 ?
  1429. cfg_entry->unique_flags1 :
  1430. cfg_entry->array_id & 0xFF) :
  1431. RES_TARGET(cfg_entry->resource_address),
  1432. RES_LUN(cfg_entry->resource_address));
  1433. /* If this HCAM indicates a lost notification, read the config table */
  1434. if (pinstance->ccn.hcam->notification_lost) {
  1435. cfgcmd = pmcraid_get_free_cmd(pinstance);
  1436. if (cfgcmd) {
  1437. pmcraid_info("lost CCN, reading config table\b");
  1438. pinstance->reinit_cfg_table = 1;
  1439. pmcraid_querycfg(cfgcmd);
  1440. } else {
  1441. pmcraid_err("lost CCN, no free cmd for querycfg\n");
  1442. }
  1443. goto out_notify_apps;
  1444. }
  1445. /* If this resource is not going to be added to mid-layer, just notify
  1446. * applications and return. If this notification is about hiding a VSET
  1447. * resource, check if it was exposed already.
  1448. */
  1449. if (pinstance->ccn.hcam->notification_type ==
  1450. NOTIFICATION_TYPE_ENTRY_CHANGED &&
  1451. cfg_entry->resource_type == RES_TYPE_VSET) {
  1452. if (fw_version <= PMCRAID_FW_VERSION_1)
  1453. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1454. else
  1455. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1456. } else if (!pmcraid_expose_resource(fw_version, cfg_entry)) {
  1457. goto out_notify_apps;
  1458. }
  1459. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  1460. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  1461. rc = memcmp(&res->cfg_entry.resource_address,
  1462. &cfg_entry->resource_address,
  1463. sizeof(cfg_entry->resource_address));
  1464. if (!rc) {
  1465. new_entry = 0;
  1466. break;
  1467. }
  1468. }
  1469. if (new_entry) {
  1470. if (hidden_entry) {
  1471. spin_unlock_irqrestore(&pinstance->resource_lock,
  1472. lock_flags);
  1473. goto out_notify_apps;
  1474. }
  1475. /* If there are more number of resources than what driver can
  1476. * manage, do not notify the applications about the CCN. Just
  1477. * ignore this notifications and re-register the same HCAM
  1478. */
  1479. if (list_empty(&pinstance->free_res_q)) {
  1480. spin_unlock_irqrestore(&pinstance->resource_lock,
  1481. lock_flags);
  1482. pmcraid_err("too many resources attached\n");
  1483. spin_lock_irqsave(pinstance->host->host_lock,
  1484. host_lock_flags);
  1485. pmcraid_send_hcam(pinstance,
  1486. PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1487. spin_unlock_irqrestore(pinstance->host->host_lock,
  1488. host_lock_flags);
  1489. return;
  1490. }
  1491. res = list_entry(pinstance->free_res_q.next,
  1492. struct pmcraid_resource_entry, queue);
  1493. list_del(&res->queue);
  1494. res->scsi_dev = NULL;
  1495. res->reset_progress = 0;
  1496. list_add_tail(&res->queue, &pinstance->used_res_q);
  1497. }
  1498. memcpy(&res->cfg_entry, cfg_entry, pinstance->config_table_entry_size);
  1499. if (pinstance->ccn.hcam->notification_type ==
  1500. NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
  1501. if (res->scsi_dev) {
  1502. if (fw_version <= PMCRAID_FW_VERSION_1)
  1503. res->cfg_entry.unique_flags1 &= 0x7F;
  1504. else
  1505. res->cfg_entry.array_id &= 0xFF;
  1506. res->change_detected = RES_CHANGE_DEL;
  1507. res->cfg_entry.resource_handle =
  1508. PMCRAID_INVALID_RES_HANDLE;
  1509. schedule_work(&pinstance->worker_q);
  1510. } else {
  1511. /* This may be one of the non-exposed resources */
  1512. list_move_tail(&res->queue, &pinstance->free_res_q);
  1513. }
  1514. } else if (!res->scsi_dev) {
  1515. res->change_detected = RES_CHANGE_ADD;
  1516. schedule_work(&pinstance->worker_q);
  1517. }
  1518. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  1519. out_notify_apps:
  1520. /* Notify configuration changes to registered applications.*/
  1521. if (!pmcraid_disable_aen)
  1522. pmcraid_notify_ccn(pinstance);
  1523. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1524. if (cmd)
  1525. pmcraid_send_hcam_cmd(cmd);
  1526. }
  1527. /**
  1528. * pmcraid_get_error_info - return error string for an ioasc
  1529. * @ioasc: ioasc code
  1530. * Return Value
  1531. * none
  1532. */
  1533. static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
  1534. {
  1535. int i;
  1536. for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
  1537. if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
  1538. return &pmcraid_ioasc_error_table[i];
  1539. }
  1540. return NULL;
  1541. }
  1542. /**
  1543. * pmcraid_ioasc_logger - log IOASC information based user-settings
  1544. * @ioasc: ioasc code
  1545. * @cmd: pointer to command that resulted in 'ioasc'
  1546. */
  1547. void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
  1548. {
  1549. struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
  1550. if (error_info == NULL ||
  1551. cmd->drv_inst->current_log_level < error_info->log_level)
  1552. return;
  1553. /* log the error string */
  1554. pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
  1555. cmd->ioa_cb->ioarcb.cdb[0],
  1556. cmd->ioa_cb->ioarcb.resource_handle,
  1557. le32_to_cpu(ioasc), error_info->error_string);
  1558. }
  1559. /**
  1560. * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
  1561. *
  1562. * @pinstance: pointer to per adapter instance structure
  1563. *
  1564. * Return value:
  1565. * none
  1566. */
  1567. static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
  1568. {
  1569. struct pmcraid_hcam_ldn *hcam_ldn;
  1570. u32 ioasc;
  1571. hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1572. pmcraid_info
  1573. ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
  1574. pinstance->ldn.hcam->ilid,
  1575. pinstance->ldn.hcam->op_code,
  1576. pinstance->ldn.hcam->notification_type,
  1577. pinstance->ldn.hcam->notification_lost,
  1578. pinstance->ldn.hcam->flags,
  1579. pinstance->ldn.hcam->overlay_id);
  1580. /* log only the errors, no need to log informational log entries */
  1581. if (pinstance->ldn.hcam->notification_type !=
  1582. NOTIFICATION_TYPE_ERROR_LOG)
  1583. return;
  1584. if (pinstance->ldn.hcam->notification_lost ==
  1585. HOSTRCB_NOTIFICATIONS_LOST)
  1586. dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
  1587. ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
  1588. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  1589. ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
  1590. dev_info(&pinstance->pdev->dev,
  1591. "UnitAttention due to IOA Bus Reset\n");
  1592. scsi_report_bus_reset(
  1593. pinstance->host,
  1594. RES_BUS(hcam_ldn->error_log.fd_ra));
  1595. }
  1596. return;
  1597. }
  1598. /**
  1599. * pmcraid_process_ccn - Op done function for a CCN.
  1600. * @cmd: pointer to command struct
  1601. *
  1602. * This function is the op done function for a configuration
  1603. * change notification
  1604. *
  1605. * Return value:
  1606. * none
  1607. */
  1608. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
  1609. {
  1610. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1611. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1612. unsigned long lock_flags;
  1613. pinstance->ccn.cmd = NULL;
  1614. pmcraid_return_cmd(cmd);
  1615. /* If driver initiated IOA reset happened while this hcam was pending
  1616. * with IOA, or IOA bringdown sequence is in progress, no need to
  1617. * re-register the hcam
  1618. */
  1619. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1620. atomic_read(&pinstance->ccn.ignore) == 1) {
  1621. return;
  1622. } else if (ioasc) {
  1623. dev_info(&pinstance->pdev->dev,
  1624. "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
  1625. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1626. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1627. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1628. } else {
  1629. pmcraid_handle_config_change(pinstance);
  1630. }
  1631. }
  1632. /**
  1633. * pmcraid_process_ldn - op done function for an LDN
  1634. * @cmd: pointer to command block
  1635. *
  1636. * Return value
  1637. * none
  1638. */
  1639. static void pmcraid_initiate_reset(struct pmcraid_instance *);
  1640. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
  1641. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
  1642. {
  1643. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1644. struct pmcraid_hcam_ldn *ldn_hcam =
  1645. (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1646. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1647. u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
  1648. unsigned long lock_flags;
  1649. /* return the command block back to freepool */
  1650. pinstance->ldn.cmd = NULL;
  1651. pmcraid_return_cmd(cmd);
  1652. /* If driver initiated IOA reset happened while this hcam was pending
  1653. * with IOA, no need to re-register the hcam as reset engine will do it
  1654. * once reset sequence is complete
  1655. */
  1656. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1657. atomic_read(&pinstance->ccn.ignore) == 1) {
  1658. return;
  1659. } else if (!ioasc) {
  1660. pmcraid_handle_error_log(pinstance);
  1661. if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
  1662. spin_lock_irqsave(pinstance->host->host_lock,
  1663. lock_flags);
  1664. pmcraid_initiate_reset(pinstance);
  1665. spin_unlock_irqrestore(pinstance->host->host_lock,
  1666. lock_flags);
  1667. return;
  1668. }
  1669. if (fd_ioasc == PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC) {
  1670. pinstance->timestamp_error = 1;
  1671. pmcraid_set_timestamp(cmd);
  1672. }
  1673. } else {
  1674. dev_info(&pinstance->pdev->dev,
  1675. "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
  1676. }
  1677. /* send netlink message for HCAM notification if enabled */
  1678. if (!pmcraid_disable_aen)
  1679. pmcraid_notify_ldn(pinstance);
  1680. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1681. if (cmd)
  1682. pmcraid_send_hcam_cmd(cmd);
  1683. }
  1684. /**
  1685. * pmcraid_register_hcams - register HCAMs for CCN and LDN
  1686. *
  1687. * @pinstance: pointer per adapter instance structure
  1688. *
  1689. * Return Value
  1690. * none
  1691. */
  1692. static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
  1693. {
  1694. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1695. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1696. }
  1697. /**
  1698. * pmcraid_unregister_hcams - cancel HCAMs registered already
  1699. * @cmd: pointer to command used as part of reset sequence
  1700. */
  1701. static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
  1702. {
  1703. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1704. /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
  1705. * handling hcam response though it is not necessary. In order to
  1706. * prevent this, set 'ignore', so that bring-down sequence doesn't
  1707. * re-send any more hcams
  1708. */
  1709. atomic_set(&pinstance->ccn.ignore, 1);
  1710. atomic_set(&pinstance->ldn.ignore, 1);
  1711. /* If adapter reset was forced as part of runtime reset sequence,
  1712. * start the reset sequence. Reset will be triggered even in case
  1713. * IOA unit_check.
  1714. */
  1715. if ((pinstance->force_ioa_reset && !pinstance->ioa_bringdown) ||
  1716. pinstance->ioa_unit_check) {
  1717. pinstance->force_ioa_reset = 0;
  1718. pinstance->ioa_unit_check = 0;
  1719. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1720. pmcraid_reset_alert(cmd);
  1721. return;
  1722. }
  1723. /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
  1724. * one after the other. So CCN cancellation will be triggered by
  1725. * pmcraid_cancel_ldn itself.
  1726. */
  1727. pmcraid_cancel_ldn(cmd);
  1728. }
  1729. /**
  1730. * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
  1731. * @pinstance: pointer to adapter instance structure
  1732. * Return Value
  1733. * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
  1734. */
  1735. static void pmcraid_reinit_buffers(struct pmcraid_instance *);
  1736. static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
  1737. {
  1738. u32 intrs;
  1739. pmcraid_reinit_buffers(pinstance);
  1740. intrs = pmcraid_read_interrupts(pinstance);
  1741. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  1742. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  1743. if (!pinstance->interrupt_mode) {
  1744. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1745. pinstance->int_regs.
  1746. ioa_host_interrupt_mask_reg);
  1747. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1748. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  1749. }
  1750. return 1;
  1751. } else {
  1752. return 0;
  1753. }
  1754. }
  1755. /**
  1756. * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
  1757. * @cmd : pointer to reset command block
  1758. *
  1759. * Return Value
  1760. * none
  1761. */
  1762. static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
  1763. {
  1764. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1765. u32 int_reg;
  1766. u32 doorbell;
  1767. /* There will be an interrupt when Transition to Operational bit is
  1768. * set so tasklet would execute next reset task. The timeout handler
  1769. * would re-initiate a reset
  1770. */
  1771. cmd->cmd_done = pmcraid_ioa_reset;
  1772. cmd->timer.data = (unsigned long)cmd;
  1773. cmd->timer.expires = jiffies +
  1774. msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
  1775. cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
  1776. if (!timer_pending(&cmd->timer))
  1777. add_timer(&cmd->timer);
  1778. /* Enable destructive diagnostics on IOA if it is not yet in
  1779. * operational state
  1780. */
  1781. doorbell = DOORBELL_RUNTIME_RESET |
  1782. DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
  1783. /* Since we do RESET_ALERT and Start BIST we have to again write
  1784. * MSIX Doorbell to indicate the interrupt mode
  1785. */
  1786. if (pinstance->interrupt_mode) {
  1787. iowrite32(DOORBELL_INTR_MODE_MSIX,
  1788. pinstance->int_regs.host_ioa_interrupt_reg);
  1789. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  1790. }
  1791. iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
  1792. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1793. int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  1794. pmcraid_info("Waiting for IOA to become operational %x:%x\n",
  1795. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1796. int_reg);
  1797. }
  1798. /**
  1799. * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
  1800. *
  1801. * @pinstance: pointer to adapter instance structure
  1802. *
  1803. * Return Value
  1804. * none
  1805. */
  1806. static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
  1807. {
  1808. pmcraid_info("%s is not yet implemented\n", __func__);
  1809. }
  1810. /**
  1811. * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
  1812. * @pinstance: pointer to adapter instance structure
  1813. *
  1814. * This function fails all outstanding ops. If they are submitted to IOA
  1815. * already, it sends cancel all messages if IOA is still accepting IOARCBs,
  1816. * otherwise just completes the commands and returns the cmd blocks to free
  1817. * pool.
  1818. *
  1819. * Return value:
  1820. * none
  1821. */
  1822. static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
  1823. {
  1824. struct pmcraid_cmd *cmd, *temp;
  1825. unsigned long lock_flags;
  1826. /* pending command list is protected by pending_pool_lock. Its
  1827. * traversal must be done as within this lock
  1828. */
  1829. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1830. list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
  1831. free_list) {
  1832. list_del(&cmd->free_list);
  1833. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  1834. lock_flags);
  1835. cmd->ioa_cb->ioasa.ioasc =
  1836. cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
  1837. cmd->ioa_cb->ioasa.ilid =
  1838. cpu_to_be32(PMCRAID_DRIVER_ILID);
  1839. /* In case the command timer is still running */
  1840. del_timer(&cmd->timer);
  1841. /* If this is an IO command, complete it by invoking scsi_done
  1842. * function. If this is one of the internal commands other
  1843. * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
  1844. * complete it
  1845. */
  1846. if (cmd->scsi_cmd) {
  1847. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1848. __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
  1849. scsi_cmd->result |= DID_ERROR << 16;
  1850. scsi_dma_unmap(scsi_cmd);
  1851. pmcraid_return_cmd(cmd);
  1852. pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
  1853. le32_to_cpu(resp) >> 2,
  1854. cmd->ioa_cb->ioarcb.cdb[0],
  1855. scsi_cmd->result);
  1856. scsi_cmd->scsi_done(scsi_cmd);
  1857. } else if (cmd->cmd_done == pmcraid_internal_done ||
  1858. cmd->cmd_done == pmcraid_erp_done) {
  1859. cmd->cmd_done(cmd);
  1860. } else if (cmd->cmd_done != pmcraid_ioa_reset &&
  1861. cmd->cmd_done != pmcraid_ioa_shutdown_done) {
  1862. pmcraid_return_cmd(cmd);
  1863. }
  1864. atomic_dec(&pinstance->outstanding_cmds);
  1865. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1866. }
  1867. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  1868. }
  1869. /**
  1870. * pmcraid_ioa_reset - Implementation of IOA reset logic
  1871. *
  1872. * @cmd: pointer to the cmd block to be used for entire reset process
  1873. *
  1874. * This function executes most of the steps required for IOA reset. This gets
  1875. * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
  1876. * 'eh_' thread. Access to variables used for controlling the reset sequence is
  1877. * synchronized using host lock. Various functions called during reset process
  1878. * would make use of a single command block, pointer to which is also stored in
  1879. * adapter instance structure.
  1880. *
  1881. * Return Value
  1882. * None
  1883. */
  1884. static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
  1885. {
  1886. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1887. u8 reset_complete = 0;
  1888. pinstance->ioa_reset_in_progress = 1;
  1889. if (pinstance->reset_cmd != cmd) {
  1890. pmcraid_err("reset is called with different command block\n");
  1891. pinstance->reset_cmd = cmd;
  1892. }
  1893. pmcraid_info("reset_engine: state = %d, command = %p\n",
  1894. pinstance->ioa_state, cmd);
  1895. switch (pinstance->ioa_state) {
  1896. case IOA_STATE_DEAD:
  1897. /* If IOA is offline, whatever may be the reset reason, just
  1898. * return. callers might be waiting on the reset wait_q, wake
  1899. * up them
  1900. */
  1901. pmcraid_err("IOA is offline no reset is possible\n");
  1902. reset_complete = 1;
  1903. break;
  1904. case IOA_STATE_IN_BRINGDOWN:
  1905. /* we enter here, once ioa shutdown command is processed by IOA
  1906. * Alert IOA for a possible reset. If reset alert fails, IOA
  1907. * goes through hard-reset
  1908. */
  1909. pmcraid_disable_interrupts(pinstance, ~0);
  1910. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1911. pmcraid_reset_alert(cmd);
  1912. break;
  1913. case IOA_STATE_UNKNOWN:
  1914. /* We may be called during probe or resume. Some pre-processing
  1915. * is required for prior to reset
  1916. */
  1917. scsi_block_requests(pinstance->host);
  1918. /* If asked to reset while IOA was processing responses or
  1919. * there are any error responses then IOA may require
  1920. * hard-reset.
  1921. */
  1922. if (pinstance->ioa_hard_reset == 0) {
  1923. if (ioread32(pinstance->ioa_status) &
  1924. INTRS_TRANSITION_TO_OPERATIONAL) {
  1925. pmcraid_info("sticky bit set, bring-up\n");
  1926. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1927. pmcraid_reinit_cmdblk(cmd);
  1928. pmcraid_identify_hrrq(cmd);
  1929. } else {
  1930. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1931. pmcraid_soft_reset(cmd);
  1932. }
  1933. } else {
  1934. /* Alert IOA of a possible reset and wait for critical
  1935. * operation in progress bit to reset
  1936. */
  1937. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1938. pmcraid_reset_alert(cmd);
  1939. }
  1940. break;
  1941. case IOA_STATE_IN_RESET_ALERT:
  1942. /* If critical operation in progress bit is reset or wait gets
  1943. * timed out, reset proceeds with starting BIST on the IOA.
  1944. * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
  1945. * they are 3 or more, reset engine marks IOA dead and returns
  1946. */
  1947. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  1948. pmcraid_start_bist(cmd);
  1949. break;
  1950. case IOA_STATE_IN_HARD_RESET:
  1951. pinstance->ioa_reset_attempts++;
  1952. /* retry reset if we haven't reached maximum allowed limit */
  1953. if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
  1954. pinstance->ioa_reset_attempts = 0;
  1955. pmcraid_err("IOA didn't respond marking it as dead\n");
  1956. pinstance->ioa_state = IOA_STATE_DEAD;
  1957. if (pinstance->ioa_bringdown)
  1958. pmcraid_notify_ioastate(pinstance,
  1959. PMC_DEVICE_EVENT_SHUTDOWN_FAILED);
  1960. else
  1961. pmcraid_notify_ioastate(pinstance,
  1962. PMC_DEVICE_EVENT_RESET_FAILED);
  1963. reset_complete = 1;
  1964. break;
  1965. }
  1966. /* Once either bist or pci reset is done, restore PCI config
  1967. * space. If this fails, proceed with hard reset again
  1968. */
  1969. pci_restore_state(pinstance->pdev);
  1970. /* fail all pending commands */
  1971. pmcraid_fail_outstanding_cmds(pinstance);
  1972. /* check if unit check is active, if so extract dump */
  1973. if (pinstance->ioa_unit_check) {
  1974. pmcraid_info("unit check is active\n");
  1975. pinstance->ioa_unit_check = 0;
  1976. pmcraid_get_dump(pinstance);
  1977. pinstance->ioa_reset_attempts--;
  1978. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1979. pmcraid_reset_alert(cmd);
  1980. break;
  1981. }
  1982. /* if the reset reason is to bring-down the ioa, we might be
  1983. * done with the reset restore pci_config_space and complete
  1984. * the reset
  1985. */
  1986. if (pinstance->ioa_bringdown) {
  1987. pmcraid_info("bringing down the adapter\n");
  1988. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1989. pinstance->ioa_bringdown = 0;
  1990. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  1991. pmcraid_notify_ioastate(pinstance,
  1992. PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS);
  1993. reset_complete = 1;
  1994. } else {
  1995. /* bring-up IOA, so proceed with soft reset
  1996. * Reinitialize hrrq_buffers and their indices also
  1997. * enable interrupts after a pci_restore_state
  1998. */
  1999. if (pmcraid_reset_enable_ioa(pinstance)) {
  2000. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  2001. pmcraid_info("bringing up the adapter\n");
  2002. pmcraid_reinit_cmdblk(cmd);
  2003. pmcraid_identify_hrrq(cmd);
  2004. } else {
  2005. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  2006. pmcraid_soft_reset(cmd);
  2007. }
  2008. }
  2009. break;
  2010. case IOA_STATE_IN_SOFT_RESET:
  2011. /* TRANSITION TO OPERATIONAL is on so start initialization
  2012. * sequence
  2013. */
  2014. pmcraid_info("In softreset proceeding with bring-up\n");
  2015. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  2016. /* Initialization commands start with HRRQ identification. From
  2017. * now on tasklet completes most of the commands as IOA is up
  2018. * and intrs are enabled
  2019. */
  2020. pmcraid_identify_hrrq(cmd);
  2021. break;
  2022. case IOA_STATE_IN_BRINGUP:
  2023. /* we are done with bringing up of IOA, change the ioa_state to
  2024. * operational and wake up any waiters
  2025. */
  2026. pinstance->ioa_state = IOA_STATE_OPERATIONAL;
  2027. reset_complete = 1;
  2028. break;
  2029. case IOA_STATE_OPERATIONAL:
  2030. default:
  2031. /* When IOA is operational and a reset is requested, check for
  2032. * the reset reason. If reset is to bring down IOA, unregister
  2033. * HCAMs and initiate shutdown; if adapter reset is forced then
  2034. * restart reset sequence again
  2035. */
  2036. if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
  2037. pinstance->force_ioa_reset == 0) {
  2038. pmcraid_notify_ioastate(pinstance,
  2039. PMC_DEVICE_EVENT_RESET_SUCCESS);
  2040. reset_complete = 1;
  2041. } else {
  2042. if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
  2043. pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
  2044. pmcraid_reinit_cmdblk(cmd);
  2045. pmcraid_unregister_hcams(cmd);
  2046. }
  2047. break;
  2048. }
  2049. /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
  2050. * OPERATIONAL. Reset all control variables used during reset, wake up
  2051. * any waiting threads and let the SCSI mid-layer send commands. Note
  2052. * that host_lock must be held before invoking scsi_report_bus_reset.
  2053. */
  2054. if (reset_complete) {
  2055. pinstance->ioa_reset_in_progress = 0;
  2056. pinstance->ioa_reset_attempts = 0;
  2057. pinstance->reset_cmd = NULL;
  2058. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2059. pinstance->ioa_bringdown = 0;
  2060. pmcraid_return_cmd(cmd);
  2061. /* If target state is to bring up the adapter, proceed with
  2062. * hcam registration and resource exposure to mid-layer.
  2063. */
  2064. if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
  2065. pmcraid_register_hcams(pinstance);
  2066. wake_up_all(&pinstance->reset_wait_q);
  2067. }
  2068. return;
  2069. }
  2070. /**
  2071. * pmcraid_initiate_reset - initiates reset sequence. This is called from
  2072. * ISR/tasklet during error interrupts including IOA unit check. If reset
  2073. * is already in progress, it just returns, otherwise initiates IOA reset
  2074. * to bring IOA up to operational state.
  2075. *
  2076. * @pinstance: pointer to adapter instance structure
  2077. *
  2078. * Return value
  2079. * none
  2080. */
  2081. static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
  2082. {
  2083. struct pmcraid_cmd *cmd;
  2084. /* If the reset is already in progress, just return, otherwise start
  2085. * reset sequence and return
  2086. */
  2087. if (!pinstance->ioa_reset_in_progress) {
  2088. scsi_block_requests(pinstance->host);
  2089. cmd = pmcraid_get_free_cmd(pinstance);
  2090. if (cmd == NULL) {
  2091. pmcraid_err("no cmnd blocks for initiate_reset\n");
  2092. return;
  2093. }
  2094. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2095. pinstance->reset_cmd = cmd;
  2096. pinstance->force_ioa_reset = 1;
  2097. pmcraid_notify_ioastate(pinstance,
  2098. PMC_DEVICE_EVENT_RESET_START);
  2099. pmcraid_ioa_reset(cmd);
  2100. }
  2101. }
  2102. /**
  2103. * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
  2104. * or bringdown IOA
  2105. * @pinstance: pointer adapter instance structure
  2106. * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
  2107. * @target_state: expected target state after reset
  2108. *
  2109. * Note: This command initiates reset and waits for its completion. Hence this
  2110. * should not be called from isr/timer/tasklet functions (timeout handlers,
  2111. * error response handlers and interrupt handlers).
  2112. *
  2113. * Return Value
  2114. * 1 in case ioa_state is not target_state, 0 otherwise.
  2115. */
  2116. static int pmcraid_reset_reload(
  2117. struct pmcraid_instance *pinstance,
  2118. u8 shutdown_type,
  2119. u8 target_state
  2120. )
  2121. {
  2122. struct pmcraid_cmd *reset_cmd = NULL;
  2123. unsigned long lock_flags;
  2124. int reset = 1;
  2125. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2126. if (pinstance->ioa_reset_in_progress) {
  2127. pmcraid_info("reset_reload: reset is already in progress\n");
  2128. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2129. wait_event(pinstance->reset_wait_q,
  2130. !pinstance->ioa_reset_in_progress);
  2131. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2132. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2133. spin_unlock_irqrestore(pinstance->host->host_lock,
  2134. lock_flags);
  2135. pmcraid_info("reset_reload: IOA is dead\n");
  2136. return reset;
  2137. } else if (pinstance->ioa_state == target_state) {
  2138. reset = 0;
  2139. }
  2140. }
  2141. if (reset) {
  2142. pmcraid_info("reset_reload: proceeding with reset\n");
  2143. scsi_block_requests(pinstance->host);
  2144. reset_cmd = pmcraid_get_free_cmd(pinstance);
  2145. if (reset_cmd == NULL) {
  2146. pmcraid_err("no free cmnd for reset_reload\n");
  2147. spin_unlock_irqrestore(pinstance->host->host_lock,
  2148. lock_flags);
  2149. return reset;
  2150. }
  2151. if (shutdown_type == SHUTDOWN_NORMAL)
  2152. pinstance->ioa_bringdown = 1;
  2153. pinstance->ioa_shutdown_type = shutdown_type;
  2154. pinstance->reset_cmd = reset_cmd;
  2155. pinstance->force_ioa_reset = reset;
  2156. pmcraid_info("reset_reload: initiating reset\n");
  2157. pmcraid_ioa_reset(reset_cmd);
  2158. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2159. pmcraid_info("reset_reload: waiting for reset to complete\n");
  2160. wait_event(pinstance->reset_wait_q,
  2161. !pinstance->ioa_reset_in_progress);
  2162. pmcraid_info("reset_reload: reset is complete !!\n");
  2163. scsi_unblock_requests(pinstance->host);
  2164. if (pinstance->ioa_state == target_state)
  2165. reset = 0;
  2166. }
  2167. return reset;
  2168. }
  2169. /**
  2170. * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
  2171. *
  2172. * @pinstance: pointer to adapter instance structure
  2173. *
  2174. * Return Value
  2175. * whatever is returned from pmcraid_reset_reload
  2176. */
  2177. static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
  2178. {
  2179. return pmcraid_reset_reload(pinstance,
  2180. SHUTDOWN_NORMAL,
  2181. IOA_STATE_UNKNOWN);
  2182. }
  2183. /**
  2184. * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
  2185. *
  2186. * @pinstance: pointer to adapter instance structure
  2187. *
  2188. * Return Value
  2189. * whatever is returned from pmcraid_reset_reload
  2190. */
  2191. static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
  2192. {
  2193. pmcraid_notify_ioastate(pinstance, PMC_DEVICE_EVENT_RESET_START);
  2194. return pmcraid_reset_reload(pinstance,
  2195. SHUTDOWN_NONE,
  2196. IOA_STATE_OPERATIONAL);
  2197. }
  2198. /**
  2199. * pmcraid_request_sense - Send request sense to a device
  2200. * @cmd: pmcraid command struct
  2201. *
  2202. * This function sends a request sense to a device as a result of a check
  2203. * condition. This method re-uses the same command block that failed earlier.
  2204. */
  2205. static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
  2206. {
  2207. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2208. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2209. /* allocate DMAable memory for sense buffers */
  2210. cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
  2211. SCSI_SENSE_BUFFERSIZE,
  2212. &cmd->sense_buffer_dma);
  2213. if (cmd->sense_buffer == NULL) {
  2214. pmcraid_err
  2215. ("couldn't allocate sense buffer for request sense\n");
  2216. pmcraid_erp_done(cmd);
  2217. return;
  2218. }
  2219. /* re-use the command block */
  2220. memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
  2221. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2222. ioarcb->request_flags0 = (SYNC_COMPLETE |
  2223. NO_LINK_DESCS |
  2224. INHIBIT_UL_CHECK);
  2225. ioarcb->request_type = REQ_TYPE_SCSI;
  2226. ioarcb->cdb[0] = REQUEST_SENSE;
  2227. ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2228. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2229. offsetof(struct pmcraid_ioarcb,
  2230. add_data.u.ioadl[0]));
  2231. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  2232. ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2233. ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
  2234. ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2235. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  2236. /* request sense might be called as part of error response processing
  2237. * which runs in tasklets context. It is possible that mid-layer might
  2238. * schedule queuecommand during this time, hence, writting to IOARRIN
  2239. * must be protect by host_lock
  2240. */
  2241. pmcraid_send_cmd(cmd, pmcraid_erp_done,
  2242. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2243. pmcraid_timeout_handler);
  2244. }
  2245. /**
  2246. * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
  2247. * @cmd: command that failed
  2248. * @sense: true if request_sense is required after cancel all
  2249. *
  2250. * This function sends a cancel all to a device to clear the queue.
  2251. */
  2252. static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
  2253. {
  2254. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2255. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2256. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2257. void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
  2258. : pmcraid_request_sense;
  2259. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2260. ioarcb->request_flags0 = SYNC_OVERRIDE;
  2261. ioarcb->request_type = REQ_TYPE_IOACMD;
  2262. ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
  2263. if (RES_IS_GSCSI(res->cfg_entry))
  2264. ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
  2265. ioarcb->ioadl_bus_addr = 0;
  2266. ioarcb->ioadl_length = 0;
  2267. ioarcb->data_transfer_length = 0;
  2268. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  2269. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  2270. * schedule queuecommand while we are doing this
  2271. */
  2272. pmcraid_send_cmd(cmd, cmd_done,
  2273. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2274. pmcraid_timeout_handler);
  2275. }
  2276. /**
  2277. * pmcraid_frame_auto_sense: frame fixed format sense information
  2278. *
  2279. * @cmd: pointer to failing command block
  2280. *
  2281. * Return value
  2282. * none
  2283. */
  2284. static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
  2285. {
  2286. u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
  2287. struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
  2288. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2289. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2290. u32 failing_lba = 0;
  2291. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  2292. cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  2293. if (RES_IS_VSET(res->cfg_entry) &&
  2294. ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
  2295. ioasa->u.vset.failing_lba_hi != 0) {
  2296. sense_buf[0] = 0x72;
  2297. sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2298. sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2299. sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2300. sense_buf[7] = 12;
  2301. sense_buf[8] = 0;
  2302. sense_buf[9] = 0x0A;
  2303. sense_buf[10] = 0x80;
  2304. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
  2305. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  2306. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  2307. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  2308. sense_buf[15] = failing_lba & 0x000000ff;
  2309. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
  2310. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  2311. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  2312. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  2313. sense_buf[19] = failing_lba & 0x000000ff;
  2314. } else {
  2315. sense_buf[0] = 0x70;
  2316. sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2317. sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2318. sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2319. if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
  2320. if (RES_IS_VSET(res->cfg_entry))
  2321. failing_lba =
  2322. le32_to_cpu(ioasa->u.
  2323. vset.failing_lba_lo);
  2324. sense_buf[0] |= 0x80;
  2325. sense_buf[3] = (failing_lba >> 24) & 0xff;
  2326. sense_buf[4] = (failing_lba >> 16) & 0xff;
  2327. sense_buf[5] = (failing_lba >> 8) & 0xff;
  2328. sense_buf[6] = failing_lba & 0xff;
  2329. }
  2330. sense_buf[7] = 6; /* additional length */
  2331. }
  2332. }
  2333. /**
  2334. * pmcraid_error_handler - Error response handlers for a SCSI op
  2335. * @cmd: pointer to pmcraid_cmd that has failed
  2336. *
  2337. * This function determines whether or not to initiate ERP on the affected
  2338. * device. This is called from a tasklet, which doesn't hold any locks.
  2339. *
  2340. * Return value:
  2341. * 0 it caller can complete the request, otherwise 1 where in error
  2342. * handler itself completes the request and returns the command block
  2343. * back to free-pool
  2344. */
  2345. static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
  2346. {
  2347. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2348. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2349. struct pmcraid_instance *pinstance = cmd->drv_inst;
  2350. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2351. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2352. u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
  2353. u32 sense_copied = 0;
  2354. if (!res) {
  2355. pmcraid_info("resource pointer is NULL\n");
  2356. return 0;
  2357. }
  2358. /* If this was a SCSI read/write command keep count of errors */
  2359. if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
  2360. atomic_inc(&res->read_failures);
  2361. else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
  2362. atomic_inc(&res->write_failures);
  2363. if (!RES_IS_GSCSI(res->cfg_entry) &&
  2364. masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
  2365. pmcraid_frame_auto_sense(cmd);
  2366. }
  2367. /* Log IOASC/IOASA information based on user settings */
  2368. pmcraid_ioasc_logger(ioasc, cmd);
  2369. switch (masked_ioasc) {
  2370. case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
  2371. scsi_cmd->result |= (DID_ABORT << 16);
  2372. break;
  2373. case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
  2374. case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
  2375. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  2376. break;
  2377. case PMCRAID_IOASC_NR_SYNC_REQUIRED:
  2378. res->sync_reqd = 1;
  2379. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  2380. break;
  2381. case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
  2382. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  2383. break;
  2384. case PMCRAID_IOASC_UA_BUS_WAS_RESET:
  2385. case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
  2386. if (!res->reset_progress)
  2387. scsi_report_bus_reset(pinstance->host,
  2388. scsi_cmd->device->channel);
  2389. scsi_cmd->result |= (DID_ERROR << 16);
  2390. break;
  2391. case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
  2392. scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
  2393. res->sync_reqd = 1;
  2394. /* if check_condition is not active return with error otherwise
  2395. * get/frame the sense buffer
  2396. */
  2397. if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
  2398. SAM_STAT_CHECK_CONDITION &&
  2399. PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
  2400. return 0;
  2401. /* If we have auto sense data as part of IOASA pass it to
  2402. * mid-layer
  2403. */
  2404. if (ioasa->auto_sense_length != 0) {
  2405. short sense_len = ioasa->auto_sense_length;
  2406. int data_size = min_t(u16, le16_to_cpu(sense_len),
  2407. SCSI_SENSE_BUFFERSIZE);
  2408. memcpy(scsi_cmd->sense_buffer,
  2409. ioasa->sense_data,
  2410. data_size);
  2411. sense_copied = 1;
  2412. }
  2413. if (RES_IS_GSCSI(res->cfg_entry))
  2414. pmcraid_cancel_all(cmd, sense_copied);
  2415. else if (sense_copied)
  2416. pmcraid_erp_done(cmd);
  2417. else
  2418. pmcraid_request_sense(cmd);
  2419. return 1;
  2420. case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
  2421. break;
  2422. default:
  2423. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  2424. scsi_cmd->result |= (DID_ERROR << 16);
  2425. break;
  2426. }
  2427. return 0;
  2428. }
  2429. /**
  2430. * pmcraid_reset_device - device reset handler functions
  2431. *
  2432. * @scsi_cmd: scsi command struct
  2433. * @modifier: reset modifier indicating the reset sequence to be performed
  2434. *
  2435. * This function issues a device reset to the affected device.
  2436. * A LUN reset will be sent to the device first. If that does
  2437. * not work, a target reset will be sent.
  2438. *
  2439. * Return value:
  2440. * SUCCESS / FAILED
  2441. */
  2442. static int pmcraid_reset_device(
  2443. struct scsi_cmnd *scsi_cmd,
  2444. unsigned long timeout,
  2445. u8 modifier
  2446. )
  2447. {
  2448. struct pmcraid_cmd *cmd;
  2449. struct pmcraid_instance *pinstance;
  2450. struct pmcraid_resource_entry *res;
  2451. struct pmcraid_ioarcb *ioarcb;
  2452. unsigned long lock_flags;
  2453. u32 ioasc;
  2454. pinstance =
  2455. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2456. res = scsi_cmd->device->hostdata;
  2457. if (!res) {
  2458. sdev_printk(KERN_ERR, scsi_cmd->device,
  2459. "reset_device: NULL resource pointer\n");
  2460. return FAILED;
  2461. }
  2462. /* If adapter is currently going through reset/reload, return failed.
  2463. * This will force the mid-layer to call _eh_bus/host reset, which
  2464. * will then go to sleep and wait for the reset to complete
  2465. */
  2466. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2467. if (pinstance->ioa_reset_in_progress ||
  2468. pinstance->ioa_state == IOA_STATE_DEAD) {
  2469. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2470. return FAILED;
  2471. }
  2472. res->reset_progress = 1;
  2473. pmcraid_info("Resetting %s resource with addr %x\n",
  2474. ((modifier & RESET_DEVICE_LUN) ? "LUN" :
  2475. ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
  2476. le32_to_cpu(res->cfg_entry.resource_address));
  2477. /* get a free cmd block */
  2478. cmd = pmcraid_get_free_cmd(pinstance);
  2479. if (cmd == NULL) {
  2480. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2481. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2482. return FAILED;
  2483. }
  2484. ioarcb = &cmd->ioa_cb->ioarcb;
  2485. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2486. ioarcb->request_type = REQ_TYPE_IOACMD;
  2487. ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
  2488. /* Initialize reset modifier bits */
  2489. if (modifier)
  2490. modifier = ENABLE_RESET_MODIFIER | modifier;
  2491. ioarcb->cdb[1] = modifier;
  2492. init_completion(&cmd->wait_for_completion);
  2493. cmd->completion_req = 1;
  2494. pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
  2495. cmd->ioa_cb->ioarcb.cdb[0],
  2496. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  2497. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2498. pmcraid_send_cmd(cmd,
  2499. pmcraid_internal_done,
  2500. timeout,
  2501. pmcraid_timeout_handler);
  2502. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2503. /* RESET_DEVICE command completes after all pending IOARCBs are
  2504. * completed. Once this command is completed, pmcraind_internal_done
  2505. * will wake up the 'completion' queue.
  2506. */
  2507. wait_for_completion(&cmd->wait_for_completion);
  2508. /* complete the command here itself and return the command block
  2509. * to free list
  2510. */
  2511. pmcraid_return_cmd(cmd);
  2512. res->reset_progress = 0;
  2513. ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2514. /* set the return value based on the returned ioasc */
  2515. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2516. }
  2517. /**
  2518. * _pmcraid_io_done - helper for pmcraid_io_done function
  2519. *
  2520. * @cmd: pointer to pmcraid command struct
  2521. * @reslen: residual data length to be set in the ioasa
  2522. * @ioasc: ioasc either returned by IOA or set by driver itself.
  2523. *
  2524. * This function is invoked by pmcraid_io_done to complete mid-layer
  2525. * scsi ops.
  2526. *
  2527. * Return value:
  2528. * 0 if caller is required to return it to free_pool. Returns 1 if
  2529. * caller need not worry about freeing command block as error handler
  2530. * will take care of that.
  2531. */
  2532. static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
  2533. {
  2534. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2535. int rc = 0;
  2536. scsi_set_resid(scsi_cmd, reslen);
  2537. pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
  2538. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2539. cmd->ioa_cb->ioarcb.cdb[0],
  2540. ioasc, scsi_cmd->result);
  2541. if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
  2542. rc = pmcraid_error_handler(cmd);
  2543. if (rc == 0) {
  2544. scsi_dma_unmap(scsi_cmd);
  2545. scsi_cmd->scsi_done(scsi_cmd);
  2546. }
  2547. return rc;
  2548. }
  2549. /**
  2550. * pmcraid_io_done - SCSI completion function
  2551. *
  2552. * @cmd: pointer to pmcraid command struct
  2553. *
  2554. * This function is invoked by tasklet/mid-layer error handler to completing
  2555. * the SCSI ops sent from mid-layer.
  2556. *
  2557. * Return value
  2558. * none
  2559. */
  2560. static void pmcraid_io_done(struct pmcraid_cmd *cmd)
  2561. {
  2562. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2563. u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
  2564. if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
  2565. pmcraid_return_cmd(cmd);
  2566. }
  2567. /**
  2568. * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
  2569. *
  2570. * @cmd: command block of the command to be aborted
  2571. *
  2572. * Return Value:
  2573. * returns pointer to command structure used as cancelling cmd
  2574. */
  2575. static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
  2576. {
  2577. struct pmcraid_cmd *cancel_cmd;
  2578. struct pmcraid_instance *pinstance;
  2579. struct pmcraid_resource_entry *res;
  2580. pinstance = (struct pmcraid_instance *)cmd->drv_inst;
  2581. res = cmd->scsi_cmd->device->hostdata;
  2582. cancel_cmd = pmcraid_get_free_cmd(pinstance);
  2583. if (cancel_cmd == NULL) {
  2584. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2585. return NULL;
  2586. }
  2587. pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
  2588. pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
  2589. cmd->ioa_cb->ioarcb.cdb[0],
  2590. cmd->ioa_cb->ioarcb.response_handle >> 2);
  2591. init_completion(&cancel_cmd->wait_for_completion);
  2592. cancel_cmd->completion_req = 1;
  2593. pmcraid_info("command (%d) CDB[0] = %x for %x\n",
  2594. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2595. cancel_cmd->ioa_cb->ioarcb.cdb[0],
  2596. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
  2597. pmcraid_send_cmd(cancel_cmd,
  2598. pmcraid_internal_done,
  2599. PMCRAID_INTERNAL_TIMEOUT,
  2600. pmcraid_timeout_handler);
  2601. return cancel_cmd;
  2602. }
  2603. /**
  2604. * pmcraid_abort_complete - Waits for ABORT TASK completion
  2605. *
  2606. * @cancel_cmd: command block use as cancelling command
  2607. *
  2608. * Return Value:
  2609. * returns SUCCESS if ABORT TASK has good completion
  2610. * otherwise FAILED
  2611. */
  2612. static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
  2613. {
  2614. struct pmcraid_resource_entry *res;
  2615. u32 ioasc;
  2616. wait_for_completion(&cancel_cmd->wait_for_completion);
  2617. res = cancel_cmd->res;
  2618. cancel_cmd->res = NULL;
  2619. ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
  2620. /* If the abort task is not timed out we will get a Good completion
  2621. * as sense_key, otherwise we may get one the following responses
  2622. * due to subsequent bus reset or device reset. In case IOASC is
  2623. * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
  2624. */
  2625. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  2626. ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
  2627. if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
  2628. res->sync_reqd = 1;
  2629. ioasc = 0;
  2630. }
  2631. /* complete the command here itself */
  2632. pmcraid_return_cmd(cancel_cmd);
  2633. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2634. }
  2635. /**
  2636. * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
  2637. *
  2638. * @scsi_cmd: scsi command struct given by mid-layer. When this is called
  2639. * mid-layer ensures that no other commands are queued. This
  2640. * never gets called under interrupt, but a separate eh thread.
  2641. *
  2642. * Return value:
  2643. * SUCCESS / FAILED
  2644. */
  2645. static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
  2646. {
  2647. struct pmcraid_instance *pinstance;
  2648. struct pmcraid_cmd *cmd;
  2649. struct pmcraid_resource_entry *res;
  2650. unsigned long host_lock_flags;
  2651. unsigned long pending_lock_flags;
  2652. struct pmcraid_cmd *cancel_cmd = NULL;
  2653. int cmd_found = 0;
  2654. int rc = FAILED;
  2655. pinstance =
  2656. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2657. scmd_printk(KERN_INFO, scsi_cmd,
  2658. "I/O command timed out, aborting it.\n");
  2659. res = scsi_cmd->device->hostdata;
  2660. if (res == NULL)
  2661. return rc;
  2662. /* If we are currently going through reset/reload, return failed.
  2663. * This will force the mid-layer to eventually call
  2664. * pmcraid_eh_host_reset which will then go to sleep and wait for the
  2665. * reset to complete
  2666. */
  2667. spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
  2668. if (pinstance->ioa_reset_in_progress ||
  2669. pinstance->ioa_state == IOA_STATE_DEAD) {
  2670. spin_unlock_irqrestore(pinstance->host->host_lock,
  2671. host_lock_flags);
  2672. return rc;
  2673. }
  2674. /* loop over pending cmd list to find cmd corresponding to this
  2675. * scsi_cmd. Note that this command might not have been completed
  2676. * already. locking: all pending commands are protected with
  2677. * pending_pool_lock.
  2678. */
  2679. spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
  2680. list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
  2681. if (cmd->scsi_cmd == scsi_cmd) {
  2682. cmd_found = 1;
  2683. break;
  2684. }
  2685. }
  2686. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  2687. pending_lock_flags);
  2688. /* If the command to be aborted was given to IOA and still pending with
  2689. * it, send ABORT_TASK to abort this and wait for its completion
  2690. */
  2691. if (cmd_found)
  2692. cancel_cmd = pmcraid_abort_cmd(cmd);
  2693. spin_unlock_irqrestore(pinstance->host->host_lock,
  2694. host_lock_flags);
  2695. if (cancel_cmd) {
  2696. cancel_cmd->res = cmd->scsi_cmd->device->hostdata;
  2697. rc = pmcraid_abort_complete(cancel_cmd);
  2698. }
  2699. return cmd_found ? rc : SUCCESS;
  2700. }
  2701. /**
  2702. * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
  2703. *
  2704. * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
  2705. *
  2706. * All these routines invokve pmcraid_reset_device with appropriate parameters.
  2707. * Since these are called from mid-layer EH thread, no other IO will be queued
  2708. * to the resource being reset. However, control path (IOCTL) may be active so
  2709. * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
  2710. * takes care by locking/unlocking host_lock.
  2711. *
  2712. * Return value
  2713. * SUCCESS or FAILED
  2714. */
  2715. static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
  2716. {
  2717. scmd_printk(KERN_INFO, scmd,
  2718. "resetting device due to an I/O command timeout.\n");
  2719. return pmcraid_reset_device(scmd,
  2720. PMCRAID_INTERNAL_TIMEOUT,
  2721. RESET_DEVICE_LUN);
  2722. }
  2723. static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
  2724. {
  2725. scmd_printk(KERN_INFO, scmd,
  2726. "Doing bus reset due to an I/O command timeout.\n");
  2727. return pmcraid_reset_device(scmd,
  2728. PMCRAID_RESET_BUS_TIMEOUT,
  2729. RESET_DEVICE_BUS);
  2730. }
  2731. static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
  2732. {
  2733. scmd_printk(KERN_INFO, scmd,
  2734. "Doing target reset due to an I/O command timeout.\n");
  2735. return pmcraid_reset_device(scmd,
  2736. PMCRAID_INTERNAL_TIMEOUT,
  2737. RESET_DEVICE_TARGET);
  2738. }
  2739. /**
  2740. * pmcraid_eh_host_reset_handler - adapter reset handler callback
  2741. *
  2742. * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
  2743. *
  2744. * Initiates adapter reset to bring it up to operational state
  2745. *
  2746. * Return value
  2747. * SUCCESS or FAILED
  2748. */
  2749. static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
  2750. {
  2751. unsigned long interval = 10000; /* 10 seconds interval */
  2752. int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
  2753. struct pmcraid_instance *pinstance =
  2754. (struct pmcraid_instance *)(scmd->device->host->hostdata);
  2755. /* wait for an additional 150 seconds just in case firmware could come
  2756. * up and if it could complete all the pending commands excluding the
  2757. * two HCAM (CCN and LDN).
  2758. */
  2759. while (waits--) {
  2760. if (atomic_read(&pinstance->outstanding_cmds) <=
  2761. PMCRAID_MAX_HCAM_CMD)
  2762. return SUCCESS;
  2763. msleep(interval);
  2764. }
  2765. dev_err(&pinstance->pdev->dev,
  2766. "Adapter being reset due to an I/O command timeout.\n");
  2767. return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
  2768. }
  2769. /**
  2770. * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
  2771. * @cmd: pmcraid command struct
  2772. * @sgcount: count of scatter-gather elements
  2773. *
  2774. * Return value
  2775. * returns pointer pmcraid_ioadl_desc, initialized to point to internal
  2776. * or external IOADLs
  2777. */
  2778. struct pmcraid_ioadl_desc *
  2779. pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
  2780. {
  2781. struct pmcraid_ioadl_desc *ioadl;
  2782. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2783. int ioadl_count = 0;
  2784. if (ioarcb->add_cmd_param_length)
  2785. ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
  2786. ioarcb->ioadl_length =
  2787. sizeof(struct pmcraid_ioadl_desc) * sgcount;
  2788. if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
  2789. /* external ioadls start at offset 0x80 from control_block
  2790. * structure, re-using 24 out of 27 ioadls part of IOARCB.
  2791. * It is necessary to indicate to firmware that driver is
  2792. * using ioadls to be treated as external to IOARCB.
  2793. */
  2794. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  2795. ioarcb->ioadl_bus_addr =
  2796. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2797. offsetof(struct pmcraid_ioarcb,
  2798. add_data.u.ioadl[3]));
  2799. ioadl = &ioarcb->add_data.u.ioadl[3];
  2800. } else {
  2801. ioarcb->ioadl_bus_addr =
  2802. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2803. offsetof(struct pmcraid_ioarcb,
  2804. add_data.u.ioadl[ioadl_count]));
  2805. ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
  2806. ioarcb->ioarcb_bus_addr |=
  2807. DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
  2808. }
  2809. return ioadl;
  2810. }
  2811. /**
  2812. * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
  2813. * @pinstance: pointer to adapter instance structure
  2814. * @cmd: pmcraid command struct
  2815. *
  2816. * This function is invoked by queuecommand entry point while sending a command
  2817. * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
  2818. *
  2819. * Return value:
  2820. * 0 on success or -1 on failure
  2821. */
  2822. static int pmcraid_build_ioadl(
  2823. struct pmcraid_instance *pinstance,
  2824. struct pmcraid_cmd *cmd
  2825. )
  2826. {
  2827. int i, nseg;
  2828. struct scatterlist *sglist;
  2829. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2830. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  2831. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2832. u32 length = scsi_bufflen(scsi_cmd);
  2833. if (!length)
  2834. return 0;
  2835. nseg = scsi_dma_map(scsi_cmd);
  2836. if (nseg < 0) {
  2837. scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
  2838. return -1;
  2839. } else if (nseg > PMCRAID_MAX_IOADLS) {
  2840. scsi_dma_unmap(scsi_cmd);
  2841. scmd_printk(KERN_ERR, scsi_cmd,
  2842. "sg count is (%d) more than allowed!\n", nseg);
  2843. return -1;
  2844. }
  2845. /* Initialize IOARCB data transfer length fields */
  2846. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
  2847. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  2848. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2849. ioarcb->data_transfer_length = cpu_to_le32(length);
  2850. ioadl = pmcraid_init_ioadls(cmd, nseg);
  2851. /* Initialize IOADL descriptor addresses */
  2852. scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
  2853. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
  2854. ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
  2855. ioadl[i].flags = 0;
  2856. }
  2857. /* setup last descriptor */
  2858. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  2859. return 0;
  2860. }
  2861. /**
  2862. * pmcraid_free_sglist - Frees an allocated SG buffer list
  2863. * @sglist: scatter/gather list pointer
  2864. *
  2865. * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
  2866. *
  2867. * Return value:
  2868. * none
  2869. */
  2870. static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
  2871. {
  2872. int i;
  2873. for (i = 0; i < sglist->num_sg; i++)
  2874. __free_pages(sg_page(&(sglist->scatterlist[i])),
  2875. sglist->order);
  2876. kfree(sglist);
  2877. }
  2878. /**
  2879. * pmcraid_alloc_sglist - Allocates memory for a SG list
  2880. * @buflen: buffer length
  2881. *
  2882. * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
  2883. * list.
  2884. *
  2885. * Return value
  2886. * pointer to sglist / NULL on failure
  2887. */
  2888. static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
  2889. {
  2890. struct pmcraid_sglist *sglist;
  2891. struct scatterlist *scatterlist;
  2892. struct page *page;
  2893. int num_elem, i, j;
  2894. int sg_size;
  2895. int order;
  2896. int bsize_elem;
  2897. sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
  2898. order = (sg_size > 0) ? get_order(sg_size) : 0;
  2899. bsize_elem = PAGE_SIZE * (1 << order);
  2900. /* Determine the actual number of sg entries needed */
  2901. if (buflen % bsize_elem)
  2902. num_elem = (buflen / bsize_elem) + 1;
  2903. else
  2904. num_elem = buflen / bsize_elem;
  2905. /* Allocate a scatter/gather list for the DMA */
  2906. sglist = kzalloc(sizeof(struct pmcraid_sglist) +
  2907. (sizeof(struct scatterlist) * (num_elem - 1)),
  2908. GFP_KERNEL);
  2909. if (sglist == NULL)
  2910. return NULL;
  2911. scatterlist = sglist->scatterlist;
  2912. sg_init_table(scatterlist, num_elem);
  2913. sglist->order = order;
  2914. sglist->num_sg = num_elem;
  2915. sg_size = buflen;
  2916. for (i = 0; i < num_elem; i++) {
  2917. page = alloc_pages(GFP_KERNEL|GFP_DMA|__GFP_ZERO, order);
  2918. if (!page) {
  2919. for (j = i - 1; j >= 0; j--)
  2920. __free_pages(sg_page(&scatterlist[j]), order);
  2921. kfree(sglist);
  2922. return NULL;
  2923. }
  2924. sg_set_page(&scatterlist[i], page,
  2925. sg_size < bsize_elem ? sg_size : bsize_elem, 0);
  2926. sg_size -= bsize_elem;
  2927. }
  2928. return sglist;
  2929. }
  2930. /**
  2931. * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
  2932. * @sglist: scatter/gather list pointer
  2933. * @buffer: buffer pointer
  2934. * @len: buffer length
  2935. * @direction: data transfer direction
  2936. *
  2937. * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
  2938. *
  2939. * Return value:
  2940. * 0 on success / other on failure
  2941. */
  2942. static int pmcraid_copy_sglist(
  2943. struct pmcraid_sglist *sglist,
  2944. unsigned long buffer,
  2945. u32 len,
  2946. int direction
  2947. )
  2948. {
  2949. struct scatterlist *scatterlist;
  2950. void *kaddr;
  2951. int bsize_elem;
  2952. int i;
  2953. int rc = 0;
  2954. /* Determine the actual number of bytes per element */
  2955. bsize_elem = PAGE_SIZE * (1 << sglist->order);
  2956. scatterlist = sglist->scatterlist;
  2957. for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
  2958. struct page *page = sg_page(&scatterlist[i]);
  2959. kaddr = kmap(page);
  2960. if (direction == DMA_TO_DEVICE)
  2961. rc = __copy_from_user(kaddr,
  2962. (void *)buffer,
  2963. bsize_elem);
  2964. else
  2965. rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
  2966. kunmap(page);
  2967. if (rc) {
  2968. pmcraid_err("failed to copy user data into sg list\n");
  2969. return -EFAULT;
  2970. }
  2971. scatterlist[i].length = bsize_elem;
  2972. }
  2973. if (len % bsize_elem) {
  2974. struct page *page = sg_page(&scatterlist[i]);
  2975. kaddr = kmap(page);
  2976. if (direction == DMA_TO_DEVICE)
  2977. rc = __copy_from_user(kaddr,
  2978. (void *)buffer,
  2979. len % bsize_elem);
  2980. else
  2981. rc = __copy_to_user((void *)buffer,
  2982. kaddr,
  2983. len % bsize_elem);
  2984. kunmap(page);
  2985. scatterlist[i].length = len % bsize_elem;
  2986. }
  2987. if (rc) {
  2988. pmcraid_err("failed to copy user data into sg list\n");
  2989. rc = -EFAULT;
  2990. }
  2991. return rc;
  2992. }
  2993. /**
  2994. * pmcraid_queuecommand - Queue a mid-layer request
  2995. * @scsi_cmd: scsi command struct
  2996. * @done: done function
  2997. *
  2998. * This function queues a request generated by the mid-layer. Midlayer calls
  2999. * this routine within host->lock. Some of the functions called by queuecommand
  3000. * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
  3001. *
  3002. * Return value:
  3003. * 0 on success
  3004. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  3005. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  3006. */
  3007. static int pmcraid_queuecommand_lck(
  3008. struct scsi_cmnd *scsi_cmd,
  3009. void (*done) (struct scsi_cmnd *)
  3010. )
  3011. {
  3012. struct pmcraid_instance *pinstance;
  3013. struct pmcraid_resource_entry *res;
  3014. struct pmcraid_ioarcb *ioarcb;
  3015. struct pmcraid_cmd *cmd;
  3016. u32 fw_version;
  3017. int rc = 0;
  3018. pinstance =
  3019. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  3020. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3021. scsi_cmd->scsi_done = done;
  3022. res = scsi_cmd->device->hostdata;
  3023. scsi_cmd->result = (DID_OK << 16);
  3024. /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
  3025. * the command
  3026. */
  3027. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  3028. pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
  3029. scsi_cmd->result = (DID_NO_CONNECT << 16);
  3030. scsi_cmd->scsi_done(scsi_cmd);
  3031. return 0;
  3032. }
  3033. /* If IOA reset is in progress, can't queue the commands */
  3034. if (pinstance->ioa_reset_in_progress)
  3035. return SCSI_MLQUEUE_HOST_BUSY;
  3036. /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
  3037. * the command here itself with success return
  3038. */
  3039. if (scsi_cmd->cmnd[0] == SYNCHRONIZE_CACHE) {
  3040. pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
  3041. scsi_cmd->scsi_done(scsi_cmd);
  3042. return 0;
  3043. }
  3044. /* initialize the command and IOARCB to be sent to IOA */
  3045. cmd = pmcraid_get_free_cmd(pinstance);
  3046. if (cmd == NULL) {
  3047. pmcraid_err("free command block is not available\n");
  3048. return SCSI_MLQUEUE_HOST_BUSY;
  3049. }
  3050. cmd->scsi_cmd = scsi_cmd;
  3051. ioarcb = &(cmd->ioa_cb->ioarcb);
  3052. memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  3053. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  3054. ioarcb->request_type = REQ_TYPE_SCSI;
  3055. /* set hrrq number where the IOA should respond to. Note that all cmds
  3056. * generated internally uses hrrq_id 0, exception to this is the cmd
  3057. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3058. * hrrq_id assigned here in queuecommand
  3059. */
  3060. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3061. pinstance->num_hrrq;
  3062. cmd->cmd_done = pmcraid_io_done;
  3063. if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
  3064. if (scsi_cmd->underflow == 0)
  3065. ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
  3066. if (res->sync_reqd) {
  3067. ioarcb->request_flags0 |= SYNC_COMPLETE;
  3068. res->sync_reqd = 0;
  3069. }
  3070. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3071. if (scsi_cmd->flags & SCMD_TAGGED)
  3072. ioarcb->request_flags1 |= TASK_TAG_SIMPLE;
  3073. if (RES_IS_GSCSI(res->cfg_entry))
  3074. ioarcb->request_flags1 |= DELAY_AFTER_RESET;
  3075. }
  3076. rc = pmcraid_build_ioadl(pinstance, cmd);
  3077. pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
  3078. le32_to_cpu(ioarcb->response_handle) >> 2,
  3079. scsi_cmd->cmnd[0], pinstance->host->unique_id,
  3080. RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
  3081. PMCRAID_PHYS_BUS_ID,
  3082. RES_IS_VSET(res->cfg_entry) ?
  3083. (fw_version <= PMCRAID_FW_VERSION_1 ?
  3084. res->cfg_entry.unique_flags1 :
  3085. res->cfg_entry.array_id & 0xFF) :
  3086. RES_TARGET(res->cfg_entry.resource_address),
  3087. RES_LUN(res->cfg_entry.resource_address));
  3088. if (likely(rc == 0)) {
  3089. _pmcraid_fire_command(cmd);
  3090. } else {
  3091. pmcraid_err("queuecommand could not build ioadl\n");
  3092. pmcraid_return_cmd(cmd);
  3093. rc = SCSI_MLQUEUE_HOST_BUSY;
  3094. }
  3095. return rc;
  3096. }
  3097. static DEF_SCSI_QCMD(pmcraid_queuecommand)
  3098. /**
  3099. * pmcraid_open -char node "open" entry, allowed only users with admin access
  3100. */
  3101. static int pmcraid_chr_open(struct inode *inode, struct file *filep)
  3102. {
  3103. struct pmcraid_instance *pinstance;
  3104. if (!capable(CAP_SYS_ADMIN))
  3105. return -EACCES;
  3106. /* Populate adapter instance * pointer for use by ioctl */
  3107. pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
  3108. filep->private_data = pinstance;
  3109. return 0;
  3110. }
  3111. /**
  3112. * pmcraid_fasync - Async notifier registration from applications
  3113. *
  3114. * This function adds the calling process to a driver global queue. When an
  3115. * event occurs, SIGIO will be sent to all processes in this queue.
  3116. */
  3117. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  3118. {
  3119. struct pmcraid_instance *pinstance;
  3120. int rc;
  3121. pinstance = filep->private_data;
  3122. mutex_lock(&pinstance->aen_queue_lock);
  3123. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  3124. mutex_unlock(&pinstance->aen_queue_lock);
  3125. return rc;
  3126. }
  3127. /**
  3128. * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
  3129. * commands sent over IOCTL interface
  3130. *
  3131. * @cmd : pointer to struct pmcraid_cmd
  3132. * @buflen : length of the request buffer
  3133. * @direction : data transfer direction
  3134. *
  3135. * Return value
  3136. * 0 on success, non-zero error code on failure
  3137. */
  3138. static int pmcraid_build_passthrough_ioadls(
  3139. struct pmcraid_cmd *cmd,
  3140. int buflen,
  3141. int direction
  3142. )
  3143. {
  3144. struct pmcraid_sglist *sglist = NULL;
  3145. struct scatterlist *sg = NULL;
  3146. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  3147. struct pmcraid_ioadl_desc *ioadl;
  3148. int i;
  3149. sglist = pmcraid_alloc_sglist(buflen);
  3150. if (!sglist) {
  3151. pmcraid_err("can't allocate memory for passthrough SGls\n");
  3152. return -ENOMEM;
  3153. }
  3154. sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
  3155. sglist->scatterlist,
  3156. sglist->num_sg, direction);
  3157. if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
  3158. dev_err(&cmd->drv_inst->pdev->dev,
  3159. "Failed to map passthrough buffer!\n");
  3160. pmcraid_free_sglist(sglist);
  3161. return -EIO;
  3162. }
  3163. cmd->sglist = sglist;
  3164. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3165. ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
  3166. /* Initialize IOADL descriptor addresses */
  3167. for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
  3168. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
  3169. ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
  3170. ioadl[i].flags = 0;
  3171. }
  3172. /* setup the last descriptor */
  3173. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  3174. return 0;
  3175. }
  3176. /**
  3177. * pmcraid_release_passthrough_ioadls - release passthrough ioadls
  3178. *
  3179. * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
  3180. * @buflen: size of the request buffer
  3181. * @direction: data transfer direction
  3182. *
  3183. * Return value
  3184. * 0 on success, non-zero error code on failure
  3185. */
  3186. static void pmcraid_release_passthrough_ioadls(
  3187. struct pmcraid_cmd *cmd,
  3188. int buflen,
  3189. int direction
  3190. )
  3191. {
  3192. struct pmcraid_sglist *sglist = cmd->sglist;
  3193. if (buflen > 0) {
  3194. pci_unmap_sg(cmd->drv_inst->pdev,
  3195. sglist->scatterlist,
  3196. sglist->num_sg,
  3197. direction);
  3198. pmcraid_free_sglist(sglist);
  3199. cmd->sglist = NULL;
  3200. }
  3201. }
  3202. /**
  3203. * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
  3204. *
  3205. * @pinstance: pointer to adapter instance structure
  3206. * @cmd: ioctl code
  3207. * @arg: pointer to pmcraid_passthrough_buffer user buffer
  3208. *
  3209. * Return value
  3210. * 0 on success, non-zero error code on failure
  3211. */
  3212. static long pmcraid_ioctl_passthrough(
  3213. struct pmcraid_instance *pinstance,
  3214. unsigned int ioctl_cmd,
  3215. unsigned int buflen,
  3216. unsigned long arg
  3217. )
  3218. {
  3219. struct pmcraid_passthrough_ioctl_buffer *buffer;
  3220. struct pmcraid_ioarcb *ioarcb;
  3221. struct pmcraid_cmd *cmd;
  3222. struct pmcraid_cmd *cancel_cmd;
  3223. unsigned long request_buffer;
  3224. unsigned long request_offset;
  3225. unsigned long lock_flags;
  3226. void *ioasa;
  3227. u32 ioasc;
  3228. int request_size;
  3229. int buffer_size;
  3230. u8 access, direction;
  3231. int rc = 0;
  3232. /* If IOA reset is in progress, wait 10 secs for reset to complete */
  3233. if (pinstance->ioa_reset_in_progress) {
  3234. rc = wait_event_interruptible_timeout(
  3235. pinstance->reset_wait_q,
  3236. !pinstance->ioa_reset_in_progress,
  3237. msecs_to_jiffies(10000));
  3238. if (!rc)
  3239. return -ETIMEDOUT;
  3240. else if (rc < 0)
  3241. return -ERESTARTSYS;
  3242. }
  3243. /* If adapter is not in operational state, return error */
  3244. if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
  3245. pmcraid_err("IOA is not operational\n");
  3246. return -ENOTTY;
  3247. }
  3248. buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
  3249. buffer = kmalloc(buffer_size, GFP_KERNEL);
  3250. if (!buffer) {
  3251. pmcraid_err("no memory for passthrough buffer\n");
  3252. return -ENOMEM;
  3253. }
  3254. request_offset =
  3255. offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
  3256. request_buffer = arg + request_offset;
  3257. rc = __copy_from_user(buffer,
  3258. (struct pmcraid_passthrough_ioctl_buffer *) arg,
  3259. sizeof(struct pmcraid_passthrough_ioctl_buffer));
  3260. ioasa =
  3261. (void *)(arg +
  3262. offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
  3263. if (rc) {
  3264. pmcraid_err("ioctl: can't copy passthrough buffer\n");
  3265. rc = -EFAULT;
  3266. goto out_free_buffer;
  3267. }
  3268. request_size = buffer->ioarcb.data_transfer_length;
  3269. if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
  3270. access = VERIFY_READ;
  3271. direction = DMA_TO_DEVICE;
  3272. } else {
  3273. access = VERIFY_WRITE;
  3274. direction = DMA_FROM_DEVICE;
  3275. }
  3276. if (request_size > 0) {
  3277. rc = access_ok(access, arg, request_offset + request_size);
  3278. if (!rc) {
  3279. rc = -EFAULT;
  3280. goto out_free_buffer;
  3281. }
  3282. } else if (request_size < 0) {
  3283. rc = -EINVAL;
  3284. goto out_free_buffer;
  3285. }
  3286. /* check if we have any additional command parameters */
  3287. if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
  3288. rc = -EINVAL;
  3289. goto out_free_buffer;
  3290. }
  3291. cmd = pmcraid_get_free_cmd(pinstance);
  3292. if (!cmd) {
  3293. pmcraid_err("free command block is not available\n");
  3294. rc = -ENOMEM;
  3295. goto out_free_buffer;
  3296. }
  3297. cmd->scsi_cmd = NULL;
  3298. ioarcb = &(cmd->ioa_cb->ioarcb);
  3299. /* Copy the user-provided IOARCB stuff field by field */
  3300. ioarcb->resource_handle = buffer->ioarcb.resource_handle;
  3301. ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
  3302. ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
  3303. ioarcb->request_type = buffer->ioarcb.request_type;
  3304. ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
  3305. ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
  3306. memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
  3307. if (buffer->ioarcb.add_cmd_param_length) {
  3308. ioarcb->add_cmd_param_length =
  3309. buffer->ioarcb.add_cmd_param_length;
  3310. ioarcb->add_cmd_param_offset =
  3311. buffer->ioarcb.add_cmd_param_offset;
  3312. memcpy(ioarcb->add_data.u.add_cmd_params,
  3313. buffer->ioarcb.add_data.u.add_cmd_params,
  3314. buffer->ioarcb.add_cmd_param_length);
  3315. }
  3316. /* set hrrq number where the IOA should respond to. Note that all cmds
  3317. * generated internally uses hrrq_id 0, exception to this is the cmd
  3318. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3319. * hrrq_id assigned here in queuecommand
  3320. */
  3321. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3322. pinstance->num_hrrq;
  3323. if (request_size) {
  3324. rc = pmcraid_build_passthrough_ioadls(cmd,
  3325. request_size,
  3326. direction);
  3327. if (rc) {
  3328. pmcraid_err("couldn't build passthrough ioadls\n");
  3329. goto out_free_buffer;
  3330. }
  3331. } else if (request_size < 0) {
  3332. rc = -EINVAL;
  3333. goto out_free_buffer;
  3334. }
  3335. /* If data is being written into the device, copy the data from user
  3336. * buffers
  3337. */
  3338. if (direction == DMA_TO_DEVICE && request_size > 0) {
  3339. rc = pmcraid_copy_sglist(cmd->sglist,
  3340. request_buffer,
  3341. request_size,
  3342. direction);
  3343. if (rc) {
  3344. pmcraid_err("failed to copy user buffer\n");
  3345. goto out_free_sglist;
  3346. }
  3347. }
  3348. /* passthrough ioctl is a blocking command so, put the user to sleep
  3349. * until timeout. Note that a timeout value of 0 means, do timeout.
  3350. */
  3351. cmd->cmd_done = pmcraid_internal_done;
  3352. init_completion(&cmd->wait_for_completion);
  3353. cmd->completion_req = 1;
  3354. pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
  3355. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  3356. cmd->ioa_cb->ioarcb.cdb[0],
  3357. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
  3358. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3359. _pmcraid_fire_command(cmd);
  3360. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3361. /* NOTE ! Remove the below line once abort_task is implemented
  3362. * in firmware. This line disables ioctl command timeout handling logic
  3363. * similar to IO command timeout handling, making ioctl commands to wait
  3364. * until the command completion regardless of timeout value specified in
  3365. * ioarcb
  3366. */
  3367. buffer->ioarcb.cmd_timeout = 0;
  3368. /* If command timeout is specified put caller to wait till that time,
  3369. * otherwise it would be blocking wait. If command gets timed out, it
  3370. * will be aborted.
  3371. */
  3372. if (buffer->ioarcb.cmd_timeout == 0) {
  3373. wait_for_completion(&cmd->wait_for_completion);
  3374. } else if (!wait_for_completion_timeout(
  3375. &cmd->wait_for_completion,
  3376. msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
  3377. pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
  3378. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
  3379. cmd->ioa_cb->ioarcb.cdb[0]);
  3380. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3381. cancel_cmd = pmcraid_abort_cmd(cmd);
  3382. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3383. if (cancel_cmd) {
  3384. wait_for_completion(&cancel_cmd->wait_for_completion);
  3385. ioasc = cancel_cmd->ioa_cb->ioasa.ioasc;
  3386. pmcraid_return_cmd(cancel_cmd);
  3387. /* if abort task couldn't find the command i.e it got
  3388. * completed prior to aborting, return good completion.
  3389. * if command got aborted successfully or there was IOA
  3390. * reset due to abort task itself getting timedout then
  3391. * return -ETIMEDOUT
  3392. */
  3393. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  3394. PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
  3395. if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
  3396. rc = -ETIMEDOUT;
  3397. goto out_handle_response;
  3398. }
  3399. }
  3400. /* no command block for abort task or abort task failed to abort
  3401. * the IOARCB, then wait for 150 more seconds and initiate reset
  3402. * sequence after timeout
  3403. */
  3404. if (!wait_for_completion_timeout(
  3405. &cmd->wait_for_completion,
  3406. msecs_to_jiffies(150 * 1000))) {
  3407. pmcraid_reset_bringup(cmd->drv_inst);
  3408. rc = -ETIMEDOUT;
  3409. }
  3410. }
  3411. out_handle_response:
  3412. /* copy entire IOASA buffer and return IOCTL success.
  3413. * If copying IOASA to user-buffer fails, return
  3414. * EFAULT
  3415. */
  3416. if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
  3417. sizeof(struct pmcraid_ioasa))) {
  3418. pmcraid_err("failed to copy ioasa buffer to user\n");
  3419. rc = -EFAULT;
  3420. }
  3421. /* If the data transfer was from device, copy the data onto user
  3422. * buffers
  3423. */
  3424. else if (direction == DMA_FROM_DEVICE && request_size > 0) {
  3425. rc = pmcraid_copy_sglist(cmd->sglist,
  3426. request_buffer,
  3427. request_size,
  3428. direction);
  3429. if (rc) {
  3430. pmcraid_err("failed to copy user buffer\n");
  3431. rc = -EFAULT;
  3432. }
  3433. }
  3434. out_free_sglist:
  3435. pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
  3436. pmcraid_return_cmd(cmd);
  3437. out_free_buffer:
  3438. kfree(buffer);
  3439. return rc;
  3440. }
  3441. /**
  3442. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  3443. *
  3444. * @pinstance: pointer to adapter instance structure
  3445. * @cmd: ioctl command passed in
  3446. * @buflen: length of user_buffer
  3447. * @user_buffer: user buffer pointer
  3448. *
  3449. * Return Value
  3450. * 0 in case of success, otherwise appropriate error code
  3451. */
  3452. static long pmcraid_ioctl_driver(
  3453. struct pmcraid_instance *pinstance,
  3454. unsigned int cmd,
  3455. unsigned int buflen,
  3456. void __user *user_buffer
  3457. )
  3458. {
  3459. int rc = -ENOSYS;
  3460. if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
  3461. pmcraid_err("ioctl_driver: access fault in request buffer\n");
  3462. return -EFAULT;
  3463. }
  3464. switch (cmd) {
  3465. case PMCRAID_IOCTL_RESET_ADAPTER:
  3466. pmcraid_reset_bringup(pinstance);
  3467. rc = 0;
  3468. break;
  3469. default:
  3470. break;
  3471. }
  3472. return rc;
  3473. }
  3474. /**
  3475. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  3476. *
  3477. * @cmd: ioctl command
  3478. * @arg: user buffer
  3479. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  3480. *
  3481. * Return Value
  3482. * negetive error code if there are access issues, otherwise zero.
  3483. * Upon success, returns ioctl header copied out of user buffer.
  3484. */
  3485. static int pmcraid_check_ioctl_buffer(
  3486. int cmd,
  3487. void __user *arg,
  3488. struct pmcraid_ioctl_header *hdr
  3489. )
  3490. {
  3491. int rc = 0;
  3492. int access = VERIFY_READ;
  3493. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  3494. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  3495. return -EFAULT;
  3496. }
  3497. /* check for valid driver signature */
  3498. rc = memcmp(hdr->signature,
  3499. PMCRAID_IOCTL_SIGNATURE,
  3500. sizeof(hdr->signature));
  3501. if (rc) {
  3502. pmcraid_err("signature verification failed\n");
  3503. return -EINVAL;
  3504. }
  3505. /* check for appropriate buffer access */
  3506. if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
  3507. access = VERIFY_WRITE;
  3508. rc = access_ok(access,
  3509. (arg + sizeof(struct pmcraid_ioctl_header)),
  3510. hdr->buffer_length);
  3511. if (!rc) {
  3512. pmcraid_err("access failed for user buffer of size %d\n",
  3513. hdr->buffer_length);
  3514. return -EFAULT;
  3515. }
  3516. return 0;
  3517. }
  3518. /**
  3519. * pmcraid_ioctl - char node ioctl entry point
  3520. */
  3521. static long pmcraid_chr_ioctl(
  3522. struct file *filep,
  3523. unsigned int cmd,
  3524. unsigned long arg
  3525. )
  3526. {
  3527. struct pmcraid_instance *pinstance = NULL;
  3528. struct pmcraid_ioctl_header *hdr = NULL;
  3529. int retval = -ENOTTY;
  3530. hdr = kmalloc(sizeof(struct pmcraid_ioctl_header), GFP_KERNEL);
  3531. if (!hdr) {
  3532. pmcraid_err("failed to allocate memory for ioctl header\n");
  3533. return -ENOMEM;
  3534. }
  3535. retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
  3536. if (retval) {
  3537. pmcraid_info("chr_ioctl: header check failed\n");
  3538. kfree(hdr);
  3539. return retval;
  3540. }
  3541. pinstance = filep->private_data;
  3542. if (!pinstance) {
  3543. pmcraid_info("adapter instance is not found\n");
  3544. kfree(hdr);
  3545. return -ENOTTY;
  3546. }
  3547. switch (_IOC_TYPE(cmd)) {
  3548. case PMCRAID_PASSTHROUGH_IOCTL:
  3549. /* If ioctl code is to download microcode, we need to block
  3550. * mid-layer requests.
  3551. */
  3552. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3553. scsi_block_requests(pinstance->host);
  3554. retval = pmcraid_ioctl_passthrough(pinstance,
  3555. cmd,
  3556. hdr->buffer_length,
  3557. arg);
  3558. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3559. scsi_unblock_requests(pinstance->host);
  3560. break;
  3561. case PMCRAID_DRIVER_IOCTL:
  3562. arg += sizeof(struct pmcraid_ioctl_header);
  3563. retval = pmcraid_ioctl_driver(pinstance,
  3564. cmd,
  3565. hdr->buffer_length,
  3566. (void __user *)arg);
  3567. break;
  3568. default:
  3569. retval = -ENOTTY;
  3570. break;
  3571. }
  3572. kfree(hdr);
  3573. return retval;
  3574. }
  3575. /**
  3576. * File operations structure for management interface
  3577. */
  3578. static const struct file_operations pmcraid_fops = {
  3579. .owner = THIS_MODULE,
  3580. .open = pmcraid_chr_open,
  3581. .fasync = pmcraid_chr_fasync,
  3582. .unlocked_ioctl = pmcraid_chr_ioctl,
  3583. #ifdef CONFIG_COMPAT
  3584. .compat_ioctl = pmcraid_chr_ioctl,
  3585. #endif
  3586. .llseek = noop_llseek,
  3587. };
  3588. /**
  3589. * pmcraid_show_log_level - Display adapter's error logging level
  3590. * @dev: class device struct
  3591. * @buf: buffer
  3592. *
  3593. * Return value:
  3594. * number of bytes printed to buffer
  3595. */
  3596. static ssize_t pmcraid_show_log_level(
  3597. struct device *dev,
  3598. struct device_attribute *attr,
  3599. char *buf)
  3600. {
  3601. struct Scsi_Host *shost = class_to_shost(dev);
  3602. struct pmcraid_instance *pinstance =
  3603. (struct pmcraid_instance *)shost->hostdata;
  3604. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3605. }
  3606. /**
  3607. * pmcraid_store_log_level - Change the adapter's error logging level
  3608. * @dev: class device struct
  3609. * @buf: buffer
  3610. * @count: not used
  3611. *
  3612. * Return value:
  3613. * number of bytes printed to buffer
  3614. */
  3615. static ssize_t pmcraid_store_log_level(
  3616. struct device *dev,
  3617. struct device_attribute *attr,
  3618. const char *buf,
  3619. size_t count
  3620. )
  3621. {
  3622. struct Scsi_Host *shost;
  3623. struct pmcraid_instance *pinstance;
  3624. u8 val;
  3625. if (kstrtou8(buf, 10, &val))
  3626. return -EINVAL;
  3627. /* log-level should be from 0 to 2 */
  3628. if (val > 2)
  3629. return -EINVAL;
  3630. shost = class_to_shost(dev);
  3631. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3632. pinstance->current_log_level = val;
  3633. return strlen(buf);
  3634. }
  3635. static struct device_attribute pmcraid_log_level_attr = {
  3636. .attr = {
  3637. .name = "log_level",
  3638. .mode = S_IRUGO | S_IWUSR,
  3639. },
  3640. .show = pmcraid_show_log_level,
  3641. .store = pmcraid_store_log_level,
  3642. };
  3643. /**
  3644. * pmcraid_show_drv_version - Display driver version
  3645. * @dev: class device struct
  3646. * @buf: buffer
  3647. *
  3648. * Return value:
  3649. * number of bytes printed to buffer
  3650. */
  3651. static ssize_t pmcraid_show_drv_version(
  3652. struct device *dev,
  3653. struct device_attribute *attr,
  3654. char *buf
  3655. )
  3656. {
  3657. return snprintf(buf, PAGE_SIZE, "version: %s\n",
  3658. PMCRAID_DRIVER_VERSION);
  3659. }
  3660. static struct device_attribute pmcraid_driver_version_attr = {
  3661. .attr = {
  3662. .name = "drv_version",
  3663. .mode = S_IRUGO,
  3664. },
  3665. .show = pmcraid_show_drv_version,
  3666. };
  3667. /**
  3668. * pmcraid_show_io_adapter_id - Display driver assigned adapter id
  3669. * @dev: class device struct
  3670. * @buf: buffer
  3671. *
  3672. * Return value:
  3673. * number of bytes printed to buffer
  3674. */
  3675. static ssize_t pmcraid_show_adapter_id(
  3676. struct device *dev,
  3677. struct device_attribute *attr,
  3678. char *buf
  3679. )
  3680. {
  3681. struct Scsi_Host *shost = class_to_shost(dev);
  3682. struct pmcraid_instance *pinstance =
  3683. (struct pmcraid_instance *)shost->hostdata;
  3684. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3685. pinstance->pdev->devfn;
  3686. u32 aen_group = pmcraid_event_family.id;
  3687. return snprintf(buf, PAGE_SIZE,
  3688. "adapter id: %d\nminor: %d\naen group: %d\n",
  3689. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3690. }
  3691. static struct device_attribute pmcraid_adapter_id_attr = {
  3692. .attr = {
  3693. .name = "adapter_id",
  3694. .mode = S_IRUGO | S_IWUSR,
  3695. },
  3696. .show = pmcraid_show_adapter_id,
  3697. };
  3698. static struct device_attribute *pmcraid_host_attrs[] = {
  3699. &pmcraid_log_level_attr,
  3700. &pmcraid_driver_version_attr,
  3701. &pmcraid_adapter_id_attr,
  3702. NULL,
  3703. };
  3704. /* host template structure for pmcraid driver */
  3705. static struct scsi_host_template pmcraid_host_template = {
  3706. .module = THIS_MODULE,
  3707. .name = PMCRAID_DRIVER_NAME,
  3708. .queuecommand = pmcraid_queuecommand,
  3709. .eh_abort_handler = pmcraid_eh_abort_handler,
  3710. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3711. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3712. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3713. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3714. .slave_alloc = pmcraid_slave_alloc,
  3715. .slave_configure = pmcraid_slave_configure,
  3716. .slave_destroy = pmcraid_slave_destroy,
  3717. .change_queue_depth = pmcraid_change_queue_depth,
  3718. .change_queue_type = pmcraid_change_queue_type,
  3719. .can_queue = PMCRAID_MAX_IO_CMD,
  3720. .this_id = -1,
  3721. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3722. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3723. .no_write_same = 1,
  3724. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3725. .use_clustering = ENABLE_CLUSTERING,
  3726. .shost_attrs = pmcraid_host_attrs,
  3727. .proc_name = PMCRAID_DRIVER_NAME,
  3728. .use_blk_tags = 1,
  3729. };
  3730. /*
  3731. * pmcraid_isr_msix - implements MSI-X interrupt handling routine
  3732. * @irq: interrupt vector number
  3733. * @dev_id: pointer hrrq_vector
  3734. *
  3735. * Return Value
  3736. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3737. */
  3738. static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
  3739. {
  3740. struct pmcraid_isr_param *hrrq_vector;
  3741. struct pmcraid_instance *pinstance;
  3742. unsigned long lock_flags;
  3743. u32 intrs_val;
  3744. int hrrq_id;
  3745. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3746. hrrq_id = hrrq_vector->hrrq_id;
  3747. pinstance = hrrq_vector->drv_inst;
  3748. if (!hrrq_id) {
  3749. /* Read the interrupt */
  3750. intrs_val = pmcraid_read_interrupts(pinstance);
  3751. if (intrs_val &&
  3752. ((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
  3753. & DOORBELL_INTR_MSIX_CLR) == 0)) {
  3754. /* Any error interrupts including unit_check,
  3755. * initiate IOA reset.In case of unit check indicate
  3756. * to reset_sequence that IOA unit checked and prepare
  3757. * for a dump during reset sequence
  3758. */
  3759. if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
  3760. if (intrs_val & INTRS_IOA_UNIT_CHECK)
  3761. pinstance->ioa_unit_check = 1;
  3762. pmcraid_err("ISR: error interrupts: %x \
  3763. initiating reset\n", intrs_val);
  3764. spin_lock_irqsave(pinstance->host->host_lock,
  3765. lock_flags);
  3766. pmcraid_initiate_reset(pinstance);
  3767. spin_unlock_irqrestore(
  3768. pinstance->host->host_lock,
  3769. lock_flags);
  3770. }
  3771. /* If interrupt was as part of the ioa initialization,
  3772. * clear it. Delete the timer and wakeup the
  3773. * reset engine to proceed with reset sequence
  3774. */
  3775. if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
  3776. pmcraid_clr_trans_op(pinstance);
  3777. /* Clear the interrupt register by writing
  3778. * to host to ioa doorbell. Once done
  3779. * FW will clear the interrupt.
  3780. */
  3781. iowrite32(DOORBELL_INTR_MSIX_CLR,
  3782. pinstance->int_regs.host_ioa_interrupt_reg);
  3783. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3784. }
  3785. }
  3786. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3787. return IRQ_HANDLED;
  3788. }
  3789. /**
  3790. * pmcraid_isr - implements legacy interrupt handling routine
  3791. *
  3792. * @irq: interrupt vector number
  3793. * @dev_id: pointer hrrq_vector
  3794. *
  3795. * Return Value
  3796. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3797. */
  3798. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3799. {
  3800. struct pmcraid_isr_param *hrrq_vector;
  3801. struct pmcraid_instance *pinstance;
  3802. u32 intrs;
  3803. unsigned long lock_flags;
  3804. int hrrq_id = 0;
  3805. /* In case of legacy interrupt mode where interrupts are shared across
  3806. * isrs, it may be possible that the current interrupt is not from IOA
  3807. */
  3808. if (!dev_id) {
  3809. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3810. return IRQ_NONE;
  3811. }
  3812. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3813. pinstance = hrrq_vector->drv_inst;
  3814. intrs = pmcraid_read_interrupts(pinstance);
  3815. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
  3816. return IRQ_NONE;
  3817. /* Any error interrupts including unit_check, initiate IOA reset.
  3818. * In case of unit check indicate to reset_sequence that IOA unit
  3819. * checked and prepare for a dump during reset sequence
  3820. */
  3821. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3822. if (intrs & INTRS_IOA_UNIT_CHECK)
  3823. pinstance->ioa_unit_check = 1;
  3824. iowrite32(intrs,
  3825. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3826. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3827. intrs);
  3828. intrs = ioread32(
  3829. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3830. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3831. pmcraid_initiate_reset(pinstance);
  3832. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3833. } else {
  3834. /* If interrupt was as part of the ioa initialization,
  3835. * clear. Delete the timer and wakeup the
  3836. * reset engine to proceed with reset sequence
  3837. */
  3838. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3839. pmcraid_clr_trans_op(pinstance);
  3840. } else {
  3841. iowrite32(intrs,
  3842. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3843. ioread32(
  3844. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3845. tasklet_schedule(
  3846. &(pinstance->isr_tasklet[hrrq_id]));
  3847. }
  3848. }
  3849. return IRQ_HANDLED;
  3850. }
  3851. /**
  3852. * pmcraid_worker_function - worker thread function
  3853. *
  3854. * @workp: pointer to struct work queue
  3855. *
  3856. * Return Value
  3857. * None
  3858. */
  3859. static void pmcraid_worker_function(struct work_struct *workp)
  3860. {
  3861. struct pmcraid_instance *pinstance;
  3862. struct pmcraid_resource_entry *res;
  3863. struct pmcraid_resource_entry *temp;
  3864. struct scsi_device *sdev;
  3865. unsigned long lock_flags;
  3866. unsigned long host_lock_flags;
  3867. u16 fw_version;
  3868. u8 bus, target, lun;
  3869. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3870. /* add resources only after host is added into system */
  3871. if (!atomic_read(&pinstance->expose_resources))
  3872. return;
  3873. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3874. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3875. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3876. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3877. sdev = res->scsi_dev;
  3878. /* host_lock must be held before calling
  3879. * scsi_device_get
  3880. */
  3881. spin_lock_irqsave(pinstance->host->host_lock,
  3882. host_lock_flags);
  3883. if (!scsi_device_get(sdev)) {
  3884. spin_unlock_irqrestore(
  3885. pinstance->host->host_lock,
  3886. host_lock_flags);
  3887. pmcraid_info("deleting %x from midlayer\n",
  3888. res->cfg_entry.resource_address);
  3889. list_move_tail(&res->queue,
  3890. &pinstance->free_res_q);
  3891. spin_unlock_irqrestore(
  3892. &pinstance->resource_lock,
  3893. lock_flags);
  3894. scsi_remove_device(sdev);
  3895. scsi_device_put(sdev);
  3896. spin_lock_irqsave(&pinstance->resource_lock,
  3897. lock_flags);
  3898. res->change_detected = 0;
  3899. } else {
  3900. spin_unlock_irqrestore(
  3901. pinstance->host->host_lock,
  3902. host_lock_flags);
  3903. }
  3904. }
  3905. }
  3906. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3907. if (res->change_detected == RES_CHANGE_ADD) {
  3908. if (!pmcraid_expose_resource(fw_version,
  3909. &res->cfg_entry))
  3910. continue;
  3911. if (RES_IS_VSET(res->cfg_entry)) {
  3912. bus = PMCRAID_VSET_BUS_ID;
  3913. if (fw_version <= PMCRAID_FW_VERSION_1)
  3914. target = res->cfg_entry.unique_flags1;
  3915. else
  3916. target = res->cfg_entry.array_id & 0xFF;
  3917. lun = PMCRAID_VSET_LUN_ID;
  3918. } else {
  3919. bus = PMCRAID_PHYS_BUS_ID;
  3920. target =
  3921. RES_TARGET(
  3922. res->cfg_entry.resource_address);
  3923. lun = RES_LUN(res->cfg_entry.resource_address);
  3924. }
  3925. res->change_detected = 0;
  3926. spin_unlock_irqrestore(&pinstance->resource_lock,
  3927. lock_flags);
  3928. scsi_add_device(pinstance->host, bus, target, lun);
  3929. spin_lock_irqsave(&pinstance->resource_lock,
  3930. lock_flags);
  3931. }
  3932. }
  3933. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3934. }
  3935. /**
  3936. * pmcraid_tasklet_function - Tasklet function
  3937. *
  3938. * @instance: pointer to msix param structure
  3939. *
  3940. * Return Value
  3941. * None
  3942. */
  3943. static void pmcraid_tasklet_function(unsigned long instance)
  3944. {
  3945. struct pmcraid_isr_param *hrrq_vector;
  3946. struct pmcraid_instance *pinstance;
  3947. unsigned long hrrq_lock_flags;
  3948. unsigned long pending_lock_flags;
  3949. unsigned long host_lock_flags;
  3950. spinlock_t *lockp; /* hrrq buffer lock */
  3951. int id;
  3952. __le32 resp;
  3953. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3954. pinstance = hrrq_vector->drv_inst;
  3955. id = hrrq_vector->hrrq_id;
  3956. lockp = &(pinstance->hrrq_lock[id]);
  3957. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3958. * protected by its own lock. Traversals must be done within this lock
  3959. * as there may be multiple tasklets running on multiple CPUs. Note
  3960. * that the lock is held just for picking up the response handle and
  3961. * manipulating hrrq_curr/toggle_bit values.
  3962. */
  3963. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3964. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3965. while ((resp & HRRQ_TOGGLE_BIT) ==
  3966. pinstance->host_toggle_bit[id]) {
  3967. int cmd_index = resp >> 2;
  3968. struct pmcraid_cmd *cmd = NULL;
  3969. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  3970. pinstance->hrrq_curr[id]++;
  3971. } else {
  3972. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  3973. pinstance->host_toggle_bit[id] ^= 1u;
  3974. }
  3975. if (cmd_index >= PMCRAID_MAX_CMD) {
  3976. /* In case of invalid response handle, log message */
  3977. pmcraid_err("Invalid response handle %d\n", cmd_index);
  3978. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3979. continue;
  3980. }
  3981. cmd = pinstance->cmd_list[cmd_index];
  3982. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3983. spin_lock_irqsave(&pinstance->pending_pool_lock,
  3984. pending_lock_flags);
  3985. list_del(&cmd->free_list);
  3986. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  3987. pending_lock_flags);
  3988. del_timer(&cmd->timer);
  3989. atomic_dec(&pinstance->outstanding_cmds);
  3990. if (cmd->cmd_done == pmcraid_ioa_reset) {
  3991. spin_lock_irqsave(pinstance->host->host_lock,
  3992. host_lock_flags);
  3993. cmd->cmd_done(cmd);
  3994. spin_unlock_irqrestore(pinstance->host->host_lock,
  3995. host_lock_flags);
  3996. } else if (cmd->cmd_done != NULL) {
  3997. cmd->cmd_done(cmd);
  3998. }
  3999. /* loop over until we are done with all responses */
  4000. spin_lock_irqsave(lockp, hrrq_lock_flags);
  4001. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  4002. }
  4003. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  4004. }
  4005. /**
  4006. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  4007. * @pinstance: pointer to adapter instance structure
  4008. *
  4009. * This routine un-registers registered interrupt handler and
  4010. * also frees irqs/vectors.
  4011. *
  4012. * Retun Value
  4013. * None
  4014. */
  4015. static
  4016. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  4017. {
  4018. int i;
  4019. for (i = 0; i < pinstance->num_hrrq; i++)
  4020. free_irq(pinstance->hrrq_vector[i].vector,
  4021. &(pinstance->hrrq_vector[i]));
  4022. if (pinstance->interrupt_mode) {
  4023. pci_disable_msix(pinstance->pdev);
  4024. pinstance->interrupt_mode = 0;
  4025. }
  4026. }
  4027. /**
  4028. * pmcraid_register_interrupt_handler - registers interrupt handler
  4029. * @pinstance: pointer to per-adapter instance structure
  4030. *
  4031. * Return Value
  4032. * 0 on success, non-zero error code otherwise.
  4033. */
  4034. static int
  4035. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  4036. {
  4037. int rc;
  4038. struct pci_dev *pdev = pinstance->pdev;
  4039. if ((pmcraid_enable_msix) &&
  4040. (pci_find_capability(pdev, PCI_CAP_ID_MSIX))) {
  4041. int num_hrrq = PMCRAID_NUM_MSIX_VECTORS;
  4042. struct msix_entry entries[PMCRAID_NUM_MSIX_VECTORS];
  4043. int i;
  4044. for (i = 0; i < PMCRAID_NUM_MSIX_VECTORS; i++)
  4045. entries[i].entry = i;
  4046. num_hrrq = pci_enable_msix_range(pdev, entries, 1, num_hrrq);
  4047. if (num_hrrq < 0)
  4048. goto pmcraid_isr_legacy;
  4049. for (i = 0; i < num_hrrq; i++) {
  4050. pinstance->hrrq_vector[i].hrrq_id = i;
  4051. pinstance->hrrq_vector[i].drv_inst = pinstance;
  4052. pinstance->hrrq_vector[i].vector = entries[i].vector;
  4053. rc = request_irq(pinstance->hrrq_vector[i].vector,
  4054. pmcraid_isr_msix, 0,
  4055. PMCRAID_DRIVER_NAME,
  4056. &(pinstance->hrrq_vector[i]));
  4057. if (rc) {
  4058. int j;
  4059. for (j = 0; j < i; j++)
  4060. free_irq(entries[j].vector,
  4061. &(pinstance->hrrq_vector[j]));
  4062. pci_disable_msix(pdev);
  4063. goto pmcraid_isr_legacy;
  4064. }
  4065. }
  4066. pinstance->num_hrrq = num_hrrq;
  4067. pinstance->interrupt_mode = 1;
  4068. iowrite32(DOORBELL_INTR_MODE_MSIX,
  4069. pinstance->int_regs.host_ioa_interrupt_reg);
  4070. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  4071. goto pmcraid_isr_out;
  4072. }
  4073. pmcraid_isr_legacy:
  4074. /* If MSI-X registration failed fallback to legacy mode, where
  4075. * only one hrrq entry will be used
  4076. */
  4077. pinstance->hrrq_vector[0].hrrq_id = 0;
  4078. pinstance->hrrq_vector[0].drv_inst = pinstance;
  4079. pinstance->hrrq_vector[0].vector = pdev->irq;
  4080. pinstance->num_hrrq = 1;
  4081. rc = request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
  4082. PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
  4083. pmcraid_isr_out:
  4084. return rc;
  4085. }
  4086. /**
  4087. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  4088. * @pinstance: per adapter instance structure pointer
  4089. * @max_index: number of buffer blocks to release
  4090. *
  4091. * Return Value
  4092. * None
  4093. */
  4094. static void
  4095. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  4096. {
  4097. int i;
  4098. for (i = 0; i < max_index; i++) {
  4099. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  4100. pinstance->cmd_list[i] = NULL;
  4101. }
  4102. kmem_cache_destroy(pinstance->cmd_cachep);
  4103. pinstance->cmd_cachep = NULL;
  4104. }
  4105. /**
  4106. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  4107. * @pinstance: pointer to per adapter instance structure
  4108. * @max_index: number of buffers (from 0 onwards) to release
  4109. *
  4110. * This function assumes that the command blocks for which control blocks are
  4111. * linked are not released.
  4112. *
  4113. * Return Value
  4114. * None
  4115. */
  4116. static void
  4117. pmcraid_release_control_blocks(
  4118. struct pmcraid_instance *pinstance,
  4119. int max_index
  4120. )
  4121. {
  4122. int i;
  4123. if (pinstance->control_pool == NULL)
  4124. return;
  4125. for (i = 0; i < max_index; i++) {
  4126. pci_pool_free(pinstance->control_pool,
  4127. pinstance->cmd_list[i]->ioa_cb,
  4128. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  4129. pinstance->cmd_list[i]->ioa_cb = NULL;
  4130. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  4131. }
  4132. pci_pool_destroy(pinstance->control_pool);
  4133. pinstance->control_pool = NULL;
  4134. }
  4135. /**
  4136. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  4137. * @pinstance - pointer to per adapter instance structure
  4138. *
  4139. * Allocates memory for command blocks using kernel slab allocator.
  4140. *
  4141. * Return Value
  4142. * 0 in case of success; -ENOMEM in case of failure
  4143. */
  4144. static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  4145. {
  4146. int i;
  4147. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  4148. pinstance->host->unique_id);
  4149. pinstance->cmd_cachep = kmem_cache_create(
  4150. pinstance->cmd_pool_name,
  4151. sizeof(struct pmcraid_cmd), 0,
  4152. SLAB_HWCACHE_ALIGN, NULL);
  4153. if (!pinstance->cmd_cachep)
  4154. return -ENOMEM;
  4155. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4156. pinstance->cmd_list[i] =
  4157. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  4158. if (!pinstance->cmd_list[i]) {
  4159. pmcraid_release_cmd_blocks(pinstance, i);
  4160. return -ENOMEM;
  4161. }
  4162. }
  4163. return 0;
  4164. }
  4165. /**
  4166. * pmcraid_allocate_control_blocks - allocates memory control blocks
  4167. * @pinstance : pointer to per adapter instance structure
  4168. *
  4169. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  4170. * and IOASAs. This is called after command blocks are already allocated.
  4171. *
  4172. * Return Value
  4173. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  4174. */
  4175. static int pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  4176. {
  4177. int i;
  4178. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  4179. pinstance->host->unique_id);
  4180. pinstance->control_pool =
  4181. pci_pool_create(pinstance->ctl_pool_name,
  4182. pinstance->pdev,
  4183. sizeof(struct pmcraid_control_block),
  4184. PMCRAID_IOARCB_ALIGNMENT, 0);
  4185. if (!pinstance->control_pool)
  4186. return -ENOMEM;
  4187. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4188. pinstance->cmd_list[i]->ioa_cb =
  4189. pci_pool_alloc(
  4190. pinstance->control_pool,
  4191. GFP_KERNEL,
  4192. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  4193. if (!pinstance->cmd_list[i]->ioa_cb) {
  4194. pmcraid_release_control_blocks(pinstance, i);
  4195. return -ENOMEM;
  4196. }
  4197. memset(pinstance->cmd_list[i]->ioa_cb, 0,
  4198. sizeof(struct pmcraid_control_block));
  4199. }
  4200. return 0;
  4201. }
  4202. /**
  4203. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  4204. * @pinstance: pointer to per adapter instance structure
  4205. * @maxindex: size of hrrq buffer pointer array
  4206. *
  4207. * Return Value
  4208. * None
  4209. */
  4210. static void
  4211. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  4212. {
  4213. int i;
  4214. for (i = 0; i < maxindex; i++) {
  4215. pci_free_consistent(pinstance->pdev,
  4216. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  4217. pinstance->hrrq_start[i],
  4218. pinstance->hrrq_start_bus_addr[i]);
  4219. /* reset pointers and toggle bit to zeros */
  4220. pinstance->hrrq_start[i] = NULL;
  4221. pinstance->hrrq_start_bus_addr[i] = 0;
  4222. pinstance->host_toggle_bit[i] = 0;
  4223. }
  4224. }
  4225. /**
  4226. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  4227. * @pinstance: pointer to per adapter instance structure
  4228. *
  4229. * Return value
  4230. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  4231. */
  4232. static int pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  4233. {
  4234. int i, buffer_size;
  4235. buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4236. for (i = 0; i < pinstance->num_hrrq; i++) {
  4237. pinstance->hrrq_start[i] =
  4238. pci_alloc_consistent(
  4239. pinstance->pdev,
  4240. buffer_size,
  4241. &(pinstance->hrrq_start_bus_addr[i]));
  4242. if (pinstance->hrrq_start[i] == 0) {
  4243. pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
  4244. i);
  4245. pmcraid_release_host_rrqs(pinstance, i);
  4246. return -ENOMEM;
  4247. }
  4248. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4249. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4250. pinstance->hrrq_end[i] =
  4251. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4252. pinstance->host_toggle_bit[i] = 1;
  4253. spin_lock_init(&pinstance->hrrq_lock[i]);
  4254. }
  4255. return 0;
  4256. }
  4257. /**
  4258. * pmcraid_release_hcams - release HCAM buffers
  4259. *
  4260. * @pinstance: pointer to per adapter instance structure
  4261. *
  4262. * Return value
  4263. * none
  4264. */
  4265. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  4266. {
  4267. if (pinstance->ccn.msg != NULL) {
  4268. pci_free_consistent(pinstance->pdev,
  4269. PMCRAID_AEN_HDR_SIZE +
  4270. sizeof(struct pmcraid_hcam_ccn_ext),
  4271. pinstance->ccn.msg,
  4272. pinstance->ccn.baddr);
  4273. pinstance->ccn.msg = NULL;
  4274. pinstance->ccn.hcam = NULL;
  4275. pinstance->ccn.baddr = 0;
  4276. }
  4277. if (pinstance->ldn.msg != NULL) {
  4278. pci_free_consistent(pinstance->pdev,
  4279. PMCRAID_AEN_HDR_SIZE +
  4280. sizeof(struct pmcraid_hcam_ldn),
  4281. pinstance->ldn.msg,
  4282. pinstance->ldn.baddr);
  4283. pinstance->ldn.msg = NULL;
  4284. pinstance->ldn.hcam = NULL;
  4285. pinstance->ldn.baddr = 0;
  4286. }
  4287. }
  4288. /**
  4289. * pmcraid_allocate_hcams - allocates HCAM buffers
  4290. * @pinstance : pointer to per adapter instance structure
  4291. *
  4292. * Return Value:
  4293. * 0 in case of successful allocation, non-zero otherwise
  4294. */
  4295. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  4296. {
  4297. pinstance->ccn.msg = pci_alloc_consistent(
  4298. pinstance->pdev,
  4299. PMCRAID_AEN_HDR_SIZE +
  4300. sizeof(struct pmcraid_hcam_ccn_ext),
  4301. &(pinstance->ccn.baddr));
  4302. pinstance->ldn.msg = pci_alloc_consistent(
  4303. pinstance->pdev,
  4304. PMCRAID_AEN_HDR_SIZE +
  4305. sizeof(struct pmcraid_hcam_ldn),
  4306. &(pinstance->ldn.baddr));
  4307. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  4308. pmcraid_release_hcams(pinstance);
  4309. } else {
  4310. pinstance->ccn.hcam =
  4311. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  4312. pinstance->ldn.hcam =
  4313. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  4314. atomic_set(&pinstance->ccn.ignore, 0);
  4315. atomic_set(&pinstance->ldn.ignore, 0);
  4316. }
  4317. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  4318. }
  4319. /**
  4320. * pmcraid_release_config_buffers - release config.table buffers
  4321. * @pinstance: pointer to per adapter instance structure
  4322. *
  4323. * Return Value
  4324. * none
  4325. */
  4326. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  4327. {
  4328. if (pinstance->cfg_table != NULL &&
  4329. pinstance->cfg_table_bus_addr != 0) {
  4330. pci_free_consistent(pinstance->pdev,
  4331. sizeof(struct pmcraid_config_table),
  4332. pinstance->cfg_table,
  4333. pinstance->cfg_table_bus_addr);
  4334. pinstance->cfg_table = NULL;
  4335. pinstance->cfg_table_bus_addr = 0;
  4336. }
  4337. if (pinstance->res_entries != NULL) {
  4338. int i;
  4339. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4340. list_del(&pinstance->res_entries[i].queue);
  4341. kfree(pinstance->res_entries);
  4342. pinstance->res_entries = NULL;
  4343. }
  4344. pmcraid_release_hcams(pinstance);
  4345. }
  4346. /**
  4347. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  4348. * @pinstance : pointer to per adapter instance structure
  4349. *
  4350. * Return Value
  4351. * 0 for successful allocation, -ENOMEM for any failure
  4352. */
  4353. static int pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  4354. {
  4355. int i;
  4356. pinstance->res_entries =
  4357. kzalloc(sizeof(struct pmcraid_resource_entry) *
  4358. PMCRAID_MAX_RESOURCES, GFP_KERNEL);
  4359. if (NULL == pinstance->res_entries) {
  4360. pmcraid_err("failed to allocate memory for resource table\n");
  4361. return -ENOMEM;
  4362. }
  4363. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4364. list_add_tail(&pinstance->res_entries[i].queue,
  4365. &pinstance->free_res_q);
  4366. pinstance->cfg_table =
  4367. pci_alloc_consistent(pinstance->pdev,
  4368. sizeof(struct pmcraid_config_table),
  4369. &pinstance->cfg_table_bus_addr);
  4370. if (NULL == pinstance->cfg_table) {
  4371. pmcraid_err("couldn't alloc DMA memory for config table\n");
  4372. pmcraid_release_config_buffers(pinstance);
  4373. return -ENOMEM;
  4374. }
  4375. if (pmcraid_allocate_hcams(pinstance)) {
  4376. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  4377. pmcraid_release_config_buffers(pinstance);
  4378. return -ENOMEM;
  4379. }
  4380. return 0;
  4381. }
  4382. /**
  4383. * pmcraid_init_tasklets - registers tasklets for response handling
  4384. *
  4385. * @pinstance: pointer adapter instance structure
  4386. *
  4387. * Return value
  4388. * none
  4389. */
  4390. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  4391. {
  4392. int i;
  4393. for (i = 0; i < pinstance->num_hrrq; i++)
  4394. tasklet_init(&pinstance->isr_tasklet[i],
  4395. pmcraid_tasklet_function,
  4396. (unsigned long)&pinstance->hrrq_vector[i]);
  4397. }
  4398. /**
  4399. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  4400. *
  4401. * @pinstance: pointer to adapter instance structure
  4402. *
  4403. * Return value
  4404. * none
  4405. */
  4406. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  4407. {
  4408. int i;
  4409. for (i = 0; i < pinstance->num_hrrq; i++)
  4410. tasklet_kill(&pinstance->isr_tasklet[i]);
  4411. }
  4412. /**
  4413. * pmcraid_release_buffers - release per-adapter buffers allocated
  4414. *
  4415. * @pinstance: pointer to adapter soft state
  4416. *
  4417. * Return Value
  4418. * none
  4419. */
  4420. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  4421. {
  4422. pmcraid_release_config_buffers(pinstance);
  4423. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  4424. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4425. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4426. if (pinstance->inq_data != NULL) {
  4427. pci_free_consistent(pinstance->pdev,
  4428. sizeof(struct pmcraid_inquiry_data),
  4429. pinstance->inq_data,
  4430. pinstance->inq_data_baddr);
  4431. pinstance->inq_data = NULL;
  4432. pinstance->inq_data_baddr = 0;
  4433. }
  4434. if (pinstance->timestamp_data != NULL) {
  4435. pci_free_consistent(pinstance->pdev,
  4436. sizeof(struct pmcraid_timestamp_data),
  4437. pinstance->timestamp_data,
  4438. pinstance->timestamp_data_baddr);
  4439. pinstance->timestamp_data = NULL;
  4440. pinstance->timestamp_data_baddr = 0;
  4441. }
  4442. }
  4443. /**
  4444. * pmcraid_init_buffers - allocates memory and initializes various structures
  4445. * @pinstance: pointer to per adapter instance structure
  4446. *
  4447. * This routine pre-allocates memory based on the type of block as below:
  4448. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  4449. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  4450. * config-table entries : DMAable memory using pci_alloc_consistent
  4451. * HostRRQs : DMAable memory, using pci_alloc_consistent
  4452. *
  4453. * Return Value
  4454. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  4455. */
  4456. static int pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  4457. {
  4458. int i;
  4459. if (pmcraid_allocate_host_rrqs(pinstance)) {
  4460. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  4461. pinstance->num_hrrq);
  4462. return -ENOMEM;
  4463. }
  4464. if (pmcraid_allocate_config_buffers(pinstance)) {
  4465. pmcraid_err("couldn't allocate memory for config buffers\n");
  4466. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4467. return -ENOMEM;
  4468. }
  4469. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  4470. pmcraid_err("couldn't allocate memory for cmd blocks\n");
  4471. pmcraid_release_config_buffers(pinstance);
  4472. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4473. return -ENOMEM;
  4474. }
  4475. if (pmcraid_allocate_control_blocks(pinstance)) {
  4476. pmcraid_err("couldn't allocate memory control blocks\n");
  4477. pmcraid_release_config_buffers(pinstance);
  4478. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4479. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4480. return -ENOMEM;
  4481. }
  4482. /* allocate DMAable memory for page D0 INQUIRY buffer */
  4483. pinstance->inq_data = pci_alloc_consistent(
  4484. pinstance->pdev,
  4485. sizeof(struct pmcraid_inquiry_data),
  4486. &pinstance->inq_data_baddr);
  4487. if (pinstance->inq_data == NULL) {
  4488. pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
  4489. pmcraid_release_buffers(pinstance);
  4490. return -ENOMEM;
  4491. }
  4492. /* allocate DMAable memory for set timestamp data buffer */
  4493. pinstance->timestamp_data = pci_alloc_consistent(
  4494. pinstance->pdev,
  4495. sizeof(struct pmcraid_timestamp_data),
  4496. &pinstance->timestamp_data_baddr);
  4497. if (pinstance->timestamp_data == NULL) {
  4498. pmcraid_err("couldn't allocate DMA memory for \
  4499. set time_stamp \n");
  4500. pmcraid_release_buffers(pinstance);
  4501. return -ENOMEM;
  4502. }
  4503. /* Initialize all the command blocks and add them to free pool. No
  4504. * need to lock (free_pool_lock) as this is done in initialization
  4505. * itself
  4506. */
  4507. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4508. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  4509. pmcraid_init_cmdblk(cmdp, i);
  4510. cmdp->drv_inst = pinstance;
  4511. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  4512. }
  4513. return 0;
  4514. }
  4515. /**
  4516. * pmcraid_reinit_buffers - resets various buffer pointers
  4517. * @pinstance: pointer to adapter instance
  4518. * Return value
  4519. * none
  4520. */
  4521. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  4522. {
  4523. int i;
  4524. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4525. for (i = 0; i < pinstance->num_hrrq; i++) {
  4526. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4527. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4528. pinstance->hrrq_end[i] =
  4529. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4530. pinstance->host_toggle_bit[i] = 1;
  4531. }
  4532. }
  4533. /**
  4534. * pmcraid_init_instance - initialize per instance data structure
  4535. * @pdev: pointer to pci device structure
  4536. * @host: pointer to Scsi_Host structure
  4537. * @mapped_pci_addr: memory mapped IOA configuration registers
  4538. *
  4539. * Return Value
  4540. * 0 on success, non-zero in case of any failure
  4541. */
  4542. static int pmcraid_init_instance(struct pci_dev *pdev, struct Scsi_Host *host,
  4543. void __iomem *mapped_pci_addr)
  4544. {
  4545. struct pmcraid_instance *pinstance =
  4546. (struct pmcraid_instance *)host->hostdata;
  4547. pinstance->host = host;
  4548. pinstance->pdev = pdev;
  4549. /* Initialize register addresses */
  4550. pinstance->mapped_dma_addr = mapped_pci_addr;
  4551. /* Initialize chip-specific details */
  4552. {
  4553. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  4554. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  4555. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  4556. pint_regs->ioa_host_interrupt_reg =
  4557. mapped_pci_addr + chip_cfg->ioa_host_intr;
  4558. pint_regs->ioa_host_interrupt_clr_reg =
  4559. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  4560. pint_regs->ioa_host_msix_interrupt_reg =
  4561. mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
  4562. pint_regs->host_ioa_interrupt_reg =
  4563. mapped_pci_addr + chip_cfg->host_ioa_intr;
  4564. pint_regs->host_ioa_interrupt_clr_reg =
  4565. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  4566. /* Current version of firmware exposes interrupt mask set
  4567. * and mask clr registers through memory mapped bar0.
  4568. */
  4569. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  4570. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  4571. pint_regs->ioa_host_interrupt_mask_reg =
  4572. mapped_pci_addr + chip_cfg->ioa_host_mask;
  4573. pint_regs->ioa_host_interrupt_mask_clr_reg =
  4574. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  4575. pint_regs->global_interrupt_mask_reg =
  4576. mapped_pci_addr + chip_cfg->global_intr_mask;
  4577. };
  4578. pinstance->ioa_reset_attempts = 0;
  4579. init_waitqueue_head(&pinstance->reset_wait_q);
  4580. atomic_set(&pinstance->outstanding_cmds, 0);
  4581. atomic_set(&pinstance->last_message_id, 0);
  4582. atomic_set(&pinstance->expose_resources, 0);
  4583. INIT_LIST_HEAD(&pinstance->free_res_q);
  4584. INIT_LIST_HEAD(&pinstance->used_res_q);
  4585. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4586. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4587. spin_lock_init(&pinstance->free_pool_lock);
  4588. spin_lock_init(&pinstance->pending_pool_lock);
  4589. spin_lock_init(&pinstance->resource_lock);
  4590. mutex_init(&pinstance->aen_queue_lock);
  4591. /* Work-queue (Shared) for deferred processing error handling */
  4592. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4593. /* Initialize the default log_level */
  4594. pinstance->current_log_level = pmcraid_log_level;
  4595. /* Setup variables required for reset engine */
  4596. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4597. pinstance->reset_cmd = NULL;
  4598. return 0;
  4599. }
  4600. /**
  4601. * pmcraid_shutdown - shutdown adapter controller.
  4602. * @pdev: pci device struct
  4603. *
  4604. * Issues an adapter shutdown to the card waits for its completion
  4605. *
  4606. * Return value
  4607. * none
  4608. */
  4609. static void pmcraid_shutdown(struct pci_dev *pdev)
  4610. {
  4611. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4612. pmcraid_reset_bringdown(pinstance);
  4613. }
  4614. /**
  4615. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4616. */
  4617. static unsigned short pmcraid_get_minor(void)
  4618. {
  4619. int minor;
  4620. minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
  4621. __set_bit(minor, pmcraid_minor);
  4622. return minor;
  4623. }
  4624. /**
  4625. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4626. */
  4627. static void pmcraid_release_minor(unsigned short minor)
  4628. {
  4629. __clear_bit(minor, pmcraid_minor);
  4630. }
  4631. /**
  4632. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4633. *
  4634. * @pinstance: pointer to adapter instance for which to register device
  4635. *
  4636. * Return value
  4637. * 0 in case of success, otherwise non-zero
  4638. */
  4639. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4640. {
  4641. int minor;
  4642. int error;
  4643. minor = pmcraid_get_minor();
  4644. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4645. pinstance->cdev.owner = THIS_MODULE;
  4646. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4647. if (error)
  4648. pmcraid_release_minor(minor);
  4649. else
  4650. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4651. NULL, "%s%u", PMCRAID_DEVFILE, minor);
  4652. return error;
  4653. }
  4654. /**
  4655. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4656. *
  4657. * @pinstance: pointer to adapter instance structure
  4658. *
  4659. * Return value
  4660. * none
  4661. */
  4662. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4663. {
  4664. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4665. device_destroy(pmcraid_class,
  4666. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4667. cdev_del(&pinstance->cdev);
  4668. }
  4669. /**
  4670. * pmcraid_remove - IOA hot plug remove entry point
  4671. * @pdev: pci device struct
  4672. *
  4673. * Return value
  4674. * none
  4675. */
  4676. static void pmcraid_remove(struct pci_dev *pdev)
  4677. {
  4678. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4679. /* remove the management interface (/dev file) for this device */
  4680. pmcraid_release_chrdev(pinstance);
  4681. /* remove host template from scsi midlayer */
  4682. scsi_remove_host(pinstance->host);
  4683. /* block requests from mid-layer */
  4684. scsi_block_requests(pinstance->host);
  4685. /* initiate shutdown adapter */
  4686. pmcraid_shutdown(pdev);
  4687. pmcraid_disable_interrupts(pinstance, ~0);
  4688. flush_work(&pinstance->worker_q);
  4689. pmcraid_kill_tasklets(pinstance);
  4690. pmcraid_unregister_interrupt_handler(pinstance);
  4691. pmcraid_release_buffers(pinstance);
  4692. iounmap(pinstance->mapped_dma_addr);
  4693. pci_release_regions(pdev);
  4694. scsi_host_put(pinstance->host);
  4695. pci_disable_device(pdev);
  4696. return;
  4697. }
  4698. #ifdef CONFIG_PM
  4699. /**
  4700. * pmcraid_suspend - driver suspend entry point for power management
  4701. * @pdev: PCI device structure
  4702. * @state: PCI power state to suspend routine
  4703. *
  4704. * Return Value - 0 always
  4705. */
  4706. static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
  4707. {
  4708. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4709. pmcraid_shutdown(pdev);
  4710. pmcraid_disable_interrupts(pinstance, ~0);
  4711. pmcraid_kill_tasklets(pinstance);
  4712. pci_set_drvdata(pinstance->pdev, pinstance);
  4713. pmcraid_unregister_interrupt_handler(pinstance);
  4714. pci_save_state(pdev);
  4715. pci_disable_device(pdev);
  4716. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4717. return 0;
  4718. }
  4719. /**
  4720. * pmcraid_resume - driver resume entry point PCI power management
  4721. * @pdev: PCI device structure
  4722. *
  4723. * Return Value - 0 in case of success. Error code in case of any failure
  4724. */
  4725. static int pmcraid_resume(struct pci_dev *pdev)
  4726. {
  4727. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4728. struct Scsi_Host *host = pinstance->host;
  4729. int rc;
  4730. pci_set_power_state(pdev, PCI_D0);
  4731. pci_enable_wake(pdev, PCI_D0, 0);
  4732. pci_restore_state(pdev);
  4733. rc = pci_enable_device(pdev);
  4734. if (rc) {
  4735. dev_err(&pdev->dev, "resume: Enable device failed\n");
  4736. return rc;
  4737. }
  4738. pci_set_master(pdev);
  4739. if ((sizeof(dma_addr_t) == 4) ||
  4740. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4741. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4742. if (rc == 0)
  4743. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4744. if (rc != 0) {
  4745. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4746. goto disable_device;
  4747. }
  4748. pmcraid_disable_interrupts(pinstance, ~0);
  4749. atomic_set(&pinstance->outstanding_cmds, 0);
  4750. rc = pmcraid_register_interrupt_handler(pinstance);
  4751. if (rc) {
  4752. dev_err(&pdev->dev,
  4753. "resume: couldn't register interrupt handlers\n");
  4754. rc = -ENODEV;
  4755. goto release_host;
  4756. }
  4757. pmcraid_init_tasklets(pinstance);
  4758. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4759. /* Start with hard reset sequence which brings up IOA to operational
  4760. * state as well as completes the reset sequence.
  4761. */
  4762. pinstance->ioa_hard_reset = 1;
  4763. /* Start IOA firmware initialization and bring card to Operational
  4764. * state.
  4765. */
  4766. if (pmcraid_reset_bringup(pinstance)) {
  4767. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4768. rc = -ENODEV;
  4769. goto release_tasklets;
  4770. }
  4771. return 0;
  4772. release_tasklets:
  4773. pmcraid_disable_interrupts(pinstance, ~0);
  4774. pmcraid_kill_tasklets(pinstance);
  4775. pmcraid_unregister_interrupt_handler(pinstance);
  4776. release_host:
  4777. scsi_host_put(host);
  4778. disable_device:
  4779. pci_disable_device(pdev);
  4780. return rc;
  4781. }
  4782. #else
  4783. #define pmcraid_suspend NULL
  4784. #define pmcraid_resume NULL
  4785. #endif /* CONFIG_PM */
  4786. /**
  4787. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4788. * completion of the ioa reset
  4789. * @cmd: pointer to reset command block
  4790. */
  4791. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4792. {
  4793. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4794. unsigned long flags;
  4795. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4796. pmcraid_ioa_reset(cmd);
  4797. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4798. scsi_unblock_requests(pinstance->host);
  4799. schedule_work(&pinstance->worker_q);
  4800. }
  4801. /**
  4802. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4803. *
  4804. * @cmd: pointer to pmcraid_cmd structure
  4805. *
  4806. * Return Value
  4807. * 0 for success or non-zero for failure cases
  4808. */
  4809. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4810. {
  4811. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4812. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4813. pmcraid_reinit_cmdblk(cmd);
  4814. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4815. ioarcb->request_type = REQ_TYPE_IOACMD;
  4816. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4817. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4818. /* If this was called as part of resource table reinitialization due to
  4819. * lost CCN, it is enough to return the command block back to free pool
  4820. * as part of set_supported_devs completion function.
  4821. */
  4822. if (cmd->drv_inst->reinit_cfg_table) {
  4823. cmd->drv_inst->reinit_cfg_table = 0;
  4824. cmd->release = 1;
  4825. cmd_done = pmcraid_reinit_cfgtable_done;
  4826. }
  4827. /* we will be done with the reset sequence after set supported devices,
  4828. * setup the done function to return the command block back to free
  4829. * pool
  4830. */
  4831. pmcraid_send_cmd(cmd,
  4832. cmd_done,
  4833. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4834. pmcraid_timeout_handler);
  4835. return;
  4836. }
  4837. /**
  4838. * pmcraid_set_timestamp - set the timestamp to IOAFP
  4839. *
  4840. * @cmd: pointer to pmcraid_cmd structure
  4841. *
  4842. * Return Value
  4843. * 0 for success or non-zero for failure cases
  4844. */
  4845. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
  4846. {
  4847. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4848. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4849. __be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
  4850. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4851. struct timeval tv;
  4852. __le64 timestamp;
  4853. do_gettimeofday(&tv);
  4854. timestamp = tv.tv_sec * 1000;
  4855. pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
  4856. pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
  4857. pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
  4858. pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
  4859. pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
  4860. pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp) >> 40);
  4861. pmcraid_reinit_cmdblk(cmd);
  4862. ioarcb->request_type = REQ_TYPE_SCSI;
  4863. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4864. ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
  4865. ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
  4866. memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
  4867. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4868. offsetof(struct pmcraid_ioarcb,
  4869. add_data.u.ioadl[0]));
  4870. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4871. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4872. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4873. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  4874. ioarcb->data_transfer_length =
  4875. cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4876. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4877. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4878. ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
  4879. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4880. if (!pinstance->timestamp_error) {
  4881. pinstance->timestamp_error = 0;
  4882. pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
  4883. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4884. } else {
  4885. pmcraid_send_cmd(cmd, pmcraid_return_cmd,
  4886. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4887. return;
  4888. }
  4889. }
  4890. /**
  4891. * pmcraid_init_res_table - Initialize the resource table
  4892. * @cmd: pointer to pmcraid command struct
  4893. *
  4894. * This function looks through the existing resource table, comparing
  4895. * it with the config table. This function will take care of old/new
  4896. * devices and schedule adding/removing them from the mid-layer
  4897. * as appropriate.
  4898. *
  4899. * Return value
  4900. * None
  4901. */
  4902. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4903. {
  4904. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4905. struct pmcraid_resource_entry *res, *temp;
  4906. struct pmcraid_config_table_entry *cfgte;
  4907. unsigned long lock_flags;
  4908. int found, rc, i;
  4909. u16 fw_version;
  4910. LIST_HEAD(old_res);
  4911. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4912. pmcraid_err("IOA requires microcode download\n");
  4913. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  4914. /* resource list is protected by pinstance->resource_lock.
  4915. * init_res_table can be called from probe (user-thread) or runtime
  4916. * reset (timer/tasklet)
  4917. */
  4918. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4919. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4920. list_move_tail(&res->queue, &old_res);
  4921. for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
  4922. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4923. PMCRAID_FW_VERSION_1)
  4924. cfgte = &pinstance->cfg_table->entries[i];
  4925. else
  4926. cfgte = (struct pmcraid_config_table_entry *)
  4927. &pinstance->cfg_table->entries_ext[i];
  4928. if (!pmcraid_expose_resource(fw_version, cfgte))
  4929. continue;
  4930. found = 0;
  4931. /* If this entry was already detected and initialized */
  4932. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4933. rc = memcmp(&res->cfg_entry.resource_address,
  4934. &cfgte->resource_address,
  4935. sizeof(cfgte->resource_address));
  4936. if (!rc) {
  4937. list_move_tail(&res->queue,
  4938. &pinstance->used_res_q);
  4939. found = 1;
  4940. break;
  4941. }
  4942. }
  4943. /* If this is new entry, initialize it and add it the queue */
  4944. if (!found) {
  4945. if (list_empty(&pinstance->free_res_q)) {
  4946. pmcraid_err("Too many devices attached\n");
  4947. break;
  4948. }
  4949. found = 1;
  4950. res = list_entry(pinstance->free_res_q.next,
  4951. struct pmcraid_resource_entry, queue);
  4952. res->scsi_dev = NULL;
  4953. res->change_detected = RES_CHANGE_ADD;
  4954. res->reset_progress = 0;
  4955. list_move_tail(&res->queue, &pinstance->used_res_q);
  4956. }
  4957. /* copy new configuration table entry details into driver
  4958. * maintained resource entry
  4959. */
  4960. if (found) {
  4961. memcpy(&res->cfg_entry, cfgte,
  4962. pinstance->config_table_entry_size);
  4963. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  4964. res->cfg_entry.resource_type,
  4965. (fw_version <= PMCRAID_FW_VERSION_1 ?
  4966. res->cfg_entry.unique_flags1 :
  4967. res->cfg_entry.array_id & 0xFF),
  4968. le32_to_cpu(res->cfg_entry.resource_address));
  4969. }
  4970. }
  4971. /* Detect any deleted entries, mark them for deletion from mid-layer */
  4972. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4973. if (res->scsi_dev) {
  4974. res->change_detected = RES_CHANGE_DEL;
  4975. res->cfg_entry.resource_handle =
  4976. PMCRAID_INVALID_RES_HANDLE;
  4977. list_move_tail(&res->queue, &pinstance->used_res_q);
  4978. } else {
  4979. list_move_tail(&res->queue, &pinstance->free_res_q);
  4980. }
  4981. }
  4982. /* release the resource list lock */
  4983. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  4984. pmcraid_set_timestamp(cmd);
  4985. }
  4986. /**
  4987. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  4988. * @cmd: pointer pmcraid_cmd struct
  4989. *
  4990. * This function sends a Query IOA Configuration command to the adapter to
  4991. * retrieve the IOA configuration table.
  4992. *
  4993. * Return value:
  4994. * none
  4995. */
  4996. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  4997. {
  4998. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4999. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  5000. struct pmcraid_instance *pinstance = cmd->drv_inst;
  5001. int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  5002. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  5003. PMCRAID_FW_VERSION_1)
  5004. pinstance->config_table_entry_size =
  5005. sizeof(struct pmcraid_config_table_entry);
  5006. else
  5007. pinstance->config_table_entry_size =
  5008. sizeof(struct pmcraid_config_table_entry_ext);
  5009. ioarcb->request_type = REQ_TYPE_IOACMD;
  5010. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  5011. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  5012. /* firmware requires 4-byte length field, specified in B.E format */
  5013. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  5014. /* Since entire config table can be described by single IOADL, it can
  5015. * be part of IOARCB itself
  5016. */
  5017. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  5018. offsetof(struct pmcraid_ioarcb,
  5019. add_data.u.ioadl[0]));
  5020. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  5021. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  5022. ioarcb->request_flags0 |= NO_LINK_DESCS;
  5023. ioarcb->data_transfer_length =
  5024. cpu_to_le32(sizeof(struct pmcraid_config_table));
  5025. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  5026. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  5027. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  5028. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  5029. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  5030. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  5031. }
  5032. /**
  5033. * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
  5034. * @pdev: pointer to pci device structure
  5035. * @dev_id: pointer to device ids structure
  5036. *
  5037. * Return Value
  5038. * returns 0 if the device is claimed and successfully configured.
  5039. * returns non-zero error code in case of any failure
  5040. */
  5041. static int pmcraid_probe(struct pci_dev *pdev,
  5042. const struct pci_device_id *dev_id)
  5043. {
  5044. struct pmcraid_instance *pinstance;
  5045. struct Scsi_Host *host;
  5046. void __iomem *mapped_pci_addr;
  5047. int rc = PCIBIOS_SUCCESSFUL;
  5048. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  5049. pmcraid_err
  5050. ("maximum number(%d) of supported adapters reached\n",
  5051. atomic_read(&pmcraid_adapter_count));
  5052. return -ENOMEM;
  5053. }
  5054. atomic_inc(&pmcraid_adapter_count);
  5055. rc = pci_enable_device(pdev);
  5056. if (rc) {
  5057. dev_err(&pdev->dev, "Cannot enable adapter\n");
  5058. atomic_dec(&pmcraid_adapter_count);
  5059. return rc;
  5060. }
  5061. dev_info(&pdev->dev,
  5062. "Found new IOA(%x:%x), Total IOA count: %d\n",
  5063. pdev->vendor, pdev->device,
  5064. atomic_read(&pmcraid_adapter_count));
  5065. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  5066. if (rc < 0) {
  5067. dev_err(&pdev->dev,
  5068. "Couldn't register memory range of registers\n");
  5069. goto out_disable_device;
  5070. }
  5071. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  5072. if (!mapped_pci_addr) {
  5073. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  5074. rc = -ENOMEM;
  5075. goto out_release_regions;
  5076. }
  5077. pci_set_master(pdev);
  5078. /* Firmware requires the system bus address of IOARCB to be within
  5079. * 32-bit addressable range though it has 64-bit IOARRIN register.
  5080. * However, firmware supports 64-bit streaming DMA buffers, whereas
  5081. * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
  5082. * returns memory within 4GB (if not, change this logic), coherent
  5083. * buffers are within firmware acceptable address ranges.
  5084. */
  5085. if ((sizeof(dma_addr_t) == 4) ||
  5086. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  5087. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  5088. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  5089. * bit mask for pci_alloc_consistent to return addresses within 4GB
  5090. */
  5091. if (rc == 0)
  5092. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5093. if (rc != 0) {
  5094. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  5095. goto cleanup_nomem;
  5096. }
  5097. host = scsi_host_alloc(&pmcraid_host_template,
  5098. sizeof(struct pmcraid_instance));
  5099. if (!host) {
  5100. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  5101. rc = -ENOMEM;
  5102. goto cleanup_nomem;
  5103. }
  5104. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  5105. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  5106. host->unique_id = host->host_no;
  5107. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  5108. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  5109. /* zero out entire instance structure */
  5110. pinstance = (struct pmcraid_instance *)host->hostdata;
  5111. memset(pinstance, 0, sizeof(*pinstance));
  5112. pinstance->chip_cfg =
  5113. (struct pmcraid_chip_details *)(dev_id->driver_data);
  5114. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  5115. if (rc < 0) {
  5116. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  5117. goto out_scsi_host_put;
  5118. }
  5119. pci_set_drvdata(pdev, pinstance);
  5120. /* Save PCI config-space for use following the reset */
  5121. rc = pci_save_state(pinstance->pdev);
  5122. if (rc != 0) {
  5123. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  5124. goto out_scsi_host_put;
  5125. }
  5126. pmcraid_disable_interrupts(pinstance, ~0);
  5127. rc = pmcraid_register_interrupt_handler(pinstance);
  5128. if (rc) {
  5129. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  5130. goto out_scsi_host_put;
  5131. }
  5132. pmcraid_init_tasklets(pinstance);
  5133. /* allocate verious buffers used by LLD.*/
  5134. rc = pmcraid_init_buffers(pinstance);
  5135. if (rc) {
  5136. pmcraid_err("couldn't allocate memory blocks\n");
  5137. goto out_unregister_isr;
  5138. }
  5139. /* check the reset type required */
  5140. pmcraid_reset_type(pinstance);
  5141. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  5142. /* Start IOA firmware initialization and bring card to Operational
  5143. * state.
  5144. */
  5145. pmcraid_info("starting IOA initialization sequence\n");
  5146. if (pmcraid_reset_bringup(pinstance)) {
  5147. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  5148. rc = 1;
  5149. goto out_release_bufs;
  5150. }
  5151. /* Add adapter instance into mid-layer list */
  5152. rc = scsi_add_host(pinstance->host, &pdev->dev);
  5153. if (rc != 0) {
  5154. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  5155. goto out_release_bufs;
  5156. }
  5157. scsi_scan_host(pinstance->host);
  5158. rc = pmcraid_setup_chrdev(pinstance);
  5159. if (rc != 0) {
  5160. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  5161. rc);
  5162. goto out_remove_host;
  5163. }
  5164. /* Schedule worker thread to handle CCN and take care of adding and
  5165. * removing devices to OS
  5166. */
  5167. atomic_set(&pinstance->expose_resources, 1);
  5168. schedule_work(&pinstance->worker_q);
  5169. return rc;
  5170. out_remove_host:
  5171. scsi_remove_host(host);
  5172. out_release_bufs:
  5173. pmcraid_release_buffers(pinstance);
  5174. out_unregister_isr:
  5175. pmcraid_kill_tasklets(pinstance);
  5176. pmcraid_unregister_interrupt_handler(pinstance);
  5177. out_scsi_host_put:
  5178. scsi_host_put(host);
  5179. cleanup_nomem:
  5180. iounmap(mapped_pci_addr);
  5181. out_release_regions:
  5182. pci_release_regions(pdev);
  5183. out_disable_device:
  5184. atomic_dec(&pmcraid_adapter_count);
  5185. pci_disable_device(pdev);
  5186. return -ENODEV;
  5187. }
  5188. /*
  5189. * PCI driver structure of pcmraid driver
  5190. */
  5191. static struct pci_driver pmcraid_driver = {
  5192. .name = PMCRAID_DRIVER_NAME,
  5193. .id_table = pmcraid_pci_table,
  5194. .probe = pmcraid_probe,
  5195. .remove = pmcraid_remove,
  5196. .suspend = pmcraid_suspend,
  5197. .resume = pmcraid_resume,
  5198. .shutdown = pmcraid_shutdown
  5199. };
  5200. /**
  5201. * pmcraid_init - module load entry point
  5202. */
  5203. static int __init pmcraid_init(void)
  5204. {
  5205. dev_t dev;
  5206. int error;
  5207. pmcraid_info("%s Device Driver version: %s\n",
  5208. PMCRAID_DRIVER_NAME, PMCRAID_DRIVER_VERSION);
  5209. error = alloc_chrdev_region(&dev, 0,
  5210. PMCRAID_MAX_ADAPTERS,
  5211. PMCRAID_DEVFILE);
  5212. if (error) {
  5213. pmcraid_err("failed to get a major number for adapters\n");
  5214. goto out_init;
  5215. }
  5216. pmcraid_major = MAJOR(dev);
  5217. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  5218. if (IS_ERR(pmcraid_class)) {
  5219. error = PTR_ERR(pmcraid_class);
  5220. pmcraid_err("failed to register with sysfs, error = %x\n",
  5221. error);
  5222. goto out_unreg_chrdev;
  5223. }
  5224. error = pmcraid_netlink_init();
  5225. if (error)
  5226. goto out_unreg_chrdev;
  5227. error = pci_register_driver(&pmcraid_driver);
  5228. if (error == 0)
  5229. goto out_init;
  5230. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  5231. error);
  5232. class_destroy(pmcraid_class);
  5233. pmcraid_netlink_release();
  5234. out_unreg_chrdev:
  5235. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  5236. out_init:
  5237. return error;
  5238. }
  5239. /**
  5240. * pmcraid_exit - module unload entry point
  5241. */
  5242. static void __exit pmcraid_exit(void)
  5243. {
  5244. pmcraid_netlink_release();
  5245. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  5246. PMCRAID_MAX_ADAPTERS);
  5247. pci_unregister_driver(&pmcraid_driver);
  5248. class_destroy(pmcraid_class);
  5249. }
  5250. module_init(pmcraid_init);
  5251. module_exit(pmcraid_exit);