pmcraid.c 166 KB

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