pmcraid.c 152 KB

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