pmcraid.c 152 KB

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