pmcraid.c 169 KB

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