mpt2sas_scsih.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919
  1. /*
  2. * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
  5. * Copyright (C) 2007-2008 LSI Corporation
  6. * (mailto:DL-MPTFusionLinux@lsi.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (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. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. * DISCLAIMER OF LIABILITY
  29. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  35. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  39. * USA.
  40. */
  41. #include <linux/version.h>
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/sched.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/delay.h>
  50. #include <linux/pci.h>
  51. #include <linux/interrupt.h>
  52. #include "mpt2sas_base.h"
  53. MODULE_AUTHOR(MPT2SAS_AUTHOR);
  54. MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
  55. MODULE_LICENSE("GPL");
  56. MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
  57. #define RAID_CHANNEL 1
  58. /* forward proto's */
  59. static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  60. struct _sas_node *sas_expander);
  61. static void _firmware_event_work(struct work_struct *work);
  62. /* global parameters */
  63. LIST_HEAD(mpt2sas_ioc_list);
  64. /* local parameters */
  65. static u8 scsi_io_cb_idx = -1;
  66. static u8 tm_cb_idx = -1;
  67. static u8 ctl_cb_idx = -1;
  68. static u8 base_cb_idx = -1;
  69. static u8 transport_cb_idx = -1;
  70. static u8 config_cb_idx = -1;
  71. static int mpt_ids;
  72. /* command line options */
  73. static u32 logging_level;
  74. MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
  75. "(default=0)");
  76. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  77. #define MPT2SAS_MAX_LUN (16895)
  78. static int max_lun = MPT2SAS_MAX_LUN;
  79. module_param(max_lun, int, 0);
  80. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  81. /**
  82. * struct sense_info - common structure for obtaining sense keys
  83. * @skey: sense key
  84. * @asc: additional sense code
  85. * @ascq: additional sense code qualifier
  86. */
  87. struct sense_info {
  88. u8 skey;
  89. u8 asc;
  90. u8 ascq;
  91. };
  92. /**
  93. * struct fw_event_work - firmware event struct
  94. * @list: link list framework
  95. * @work: work object (ioc->fault_reset_work_q)
  96. * @ioc: per adapter object
  97. * @VF_ID: virtual function id
  98. * @host_reset_handling: handling events during host reset
  99. * @ignore: flag meaning this event has been marked to ignore
  100. * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
  101. * @event_data: reply event data payload follows
  102. *
  103. * This object stored on ioc->fw_event_list.
  104. */
  105. struct fw_event_work {
  106. struct list_head list;
  107. struct work_struct work;
  108. struct MPT2SAS_ADAPTER *ioc;
  109. u8 VF_ID;
  110. u8 host_reset_handling;
  111. u8 ignore;
  112. u16 event;
  113. void *event_data;
  114. };
  115. /**
  116. * struct _scsi_io_transfer - scsi io transfer
  117. * @handle: sas device handle (assigned by firmware)
  118. * @is_raid: flag set for hidden raid components
  119. * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
  120. * @data_length: data transfer length
  121. * @data_dma: dma pointer to data
  122. * @sense: sense data
  123. * @lun: lun number
  124. * @cdb_length: cdb length
  125. * @cdb: cdb contents
  126. * @valid_reply: flag set for reply message
  127. * @timeout: timeout for this command
  128. * @sense_length: sense length
  129. * @ioc_status: ioc status
  130. * @scsi_state: scsi state
  131. * @scsi_status: scsi staus
  132. * @log_info: log information
  133. * @transfer_length: data length transfer when there is a reply message
  134. *
  135. * Used for sending internal scsi commands to devices within this module.
  136. * Refer to _scsi_send_scsi_io().
  137. */
  138. struct _scsi_io_transfer {
  139. u16 handle;
  140. u8 is_raid;
  141. enum dma_data_direction dir;
  142. u32 data_length;
  143. dma_addr_t data_dma;
  144. u8 sense[SCSI_SENSE_BUFFERSIZE];
  145. u32 lun;
  146. u8 cdb_length;
  147. u8 cdb[32];
  148. u8 timeout;
  149. u8 valid_reply;
  150. /* the following bits are only valid when 'valid_reply = 1' */
  151. u32 sense_length;
  152. u16 ioc_status;
  153. u8 scsi_state;
  154. u8 scsi_status;
  155. u32 log_info;
  156. u32 transfer_length;
  157. };
  158. /*
  159. * The pci device ids are defined in mpi/mpi2_cnfg.h.
  160. */
  161. static struct pci_device_id scsih_pci_table[] = {
  162. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
  163. PCI_ANY_ID, PCI_ANY_ID },
  164. /* Falcon ~ 2008*/
  165. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
  166. PCI_ANY_ID, PCI_ANY_ID },
  167. /* Liberator ~ 2108 */
  168. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
  169. PCI_ANY_ID, PCI_ANY_ID },
  170. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
  171. PCI_ANY_ID, PCI_ANY_ID },
  172. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
  173. PCI_ANY_ID, PCI_ANY_ID },
  174. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
  175. PCI_ANY_ID, PCI_ANY_ID },
  176. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
  177. PCI_ANY_ID, PCI_ANY_ID },
  178. {0} /* Terminating entry */
  179. };
  180. MODULE_DEVICE_TABLE(pci, scsih_pci_table);
  181. /**
  182. * _scsih_set_debug_level - global setting of ioc->logging_level.
  183. *
  184. * Note: The logging levels are defined in mpt2sas_debug.h.
  185. */
  186. static int
  187. _scsih_set_debug_level(const char *val, struct kernel_param *kp)
  188. {
  189. int ret = param_set_int(val, kp);
  190. struct MPT2SAS_ADAPTER *ioc;
  191. if (ret)
  192. return ret;
  193. printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
  194. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  195. ioc->logging_level = logging_level;
  196. return 0;
  197. }
  198. module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
  199. &logging_level, 0644);
  200. /**
  201. * _scsih_srch_boot_sas_address - search based on sas_address
  202. * @sas_address: sas address
  203. * @boot_device: boot device object from bios page 2
  204. *
  205. * Returns 1 when there's a match, 0 means no match.
  206. */
  207. static inline int
  208. _scsih_srch_boot_sas_address(u64 sas_address,
  209. Mpi2BootDeviceSasWwid_t *boot_device)
  210. {
  211. return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
  212. }
  213. /**
  214. * _scsih_srch_boot_device_name - search based on device name
  215. * @device_name: device name specified in INDENTIFY fram
  216. * @boot_device: boot device object from bios page 2
  217. *
  218. * Returns 1 when there's a match, 0 means no match.
  219. */
  220. static inline int
  221. _scsih_srch_boot_device_name(u64 device_name,
  222. Mpi2BootDeviceDeviceName_t *boot_device)
  223. {
  224. return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
  225. }
  226. /**
  227. * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
  228. * @enclosure_logical_id: enclosure logical id
  229. * @slot_number: slot number
  230. * @boot_device: boot device object from bios page 2
  231. *
  232. * Returns 1 when there's a match, 0 means no match.
  233. */
  234. static inline int
  235. _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
  236. Mpi2BootDeviceEnclosureSlot_t *boot_device)
  237. {
  238. return (enclosure_logical_id == le64_to_cpu(boot_device->
  239. EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
  240. SlotNumber)) ? 1 : 0;
  241. }
  242. /**
  243. * _scsih_is_boot_device - search for matching boot device.
  244. * @sas_address: sas address
  245. * @device_name: device name specified in INDENTIFY fram
  246. * @enclosure_logical_id: enclosure logical id
  247. * @slot_number: slot number
  248. * @form: specifies boot device form
  249. * @boot_device: boot device object from bios page 2
  250. *
  251. * Returns 1 when there's a match, 0 means no match.
  252. */
  253. static int
  254. _scsih_is_boot_device(u64 sas_address, u64 device_name,
  255. u64 enclosure_logical_id, u16 slot, u8 form,
  256. Mpi2BiosPage2BootDevice_t *boot_device)
  257. {
  258. int rc = 0;
  259. switch (form) {
  260. case MPI2_BIOSPAGE2_FORM_SAS_WWID:
  261. if (!sas_address)
  262. break;
  263. rc = _scsih_srch_boot_sas_address(
  264. sas_address, &boot_device->SasWwid);
  265. break;
  266. case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
  267. if (!enclosure_logical_id)
  268. break;
  269. rc = _scsih_srch_boot_encl_slot(
  270. enclosure_logical_id,
  271. slot, &boot_device->EnclosureSlot);
  272. break;
  273. case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
  274. if (!device_name)
  275. break;
  276. rc = _scsih_srch_boot_device_name(
  277. device_name, &boot_device->DeviceName);
  278. break;
  279. case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
  280. break;
  281. }
  282. return rc;
  283. }
  284. /**
  285. * _scsih_determine_boot_device - determine boot device.
  286. * @ioc: per adapter object
  287. * @device: either sas_device or raid_device object
  288. * @is_raid: [flag] 1 = raid object, 0 = sas object
  289. *
  290. * Determines whether this device should be first reported device to
  291. * to scsi-ml or sas transport, this purpose is for persistant boot device.
  292. * There are primary, alternate, and current entries in bios page 2. The order
  293. * priority is primary, alternate, then current. This routine saves
  294. * the corresponding device object and is_raid flag in the ioc object.
  295. * The saved data to be used later in _scsih_probe_boot_devices().
  296. */
  297. static void
  298. _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
  299. void *device, u8 is_raid)
  300. {
  301. struct _sas_device *sas_device;
  302. struct _raid_device *raid_device;
  303. u64 sas_address;
  304. u64 device_name;
  305. u64 enclosure_logical_id;
  306. u16 slot;
  307. /* only process this function when driver loads */
  308. if (!ioc->wait_for_port_enable_to_complete)
  309. return;
  310. if (!is_raid) {
  311. sas_device = device;
  312. sas_address = sas_device->sas_address;
  313. device_name = sas_device->device_name;
  314. enclosure_logical_id = sas_device->enclosure_logical_id;
  315. slot = sas_device->slot;
  316. } else {
  317. raid_device = device;
  318. sas_address = raid_device->wwid;
  319. device_name = 0;
  320. enclosure_logical_id = 0;
  321. slot = 0;
  322. }
  323. if (!ioc->req_boot_device.device) {
  324. if (_scsih_is_boot_device(sas_address, device_name,
  325. enclosure_logical_id, slot,
  326. (ioc->bios_pg2.ReqBootDeviceForm &
  327. MPI2_BIOSPAGE2_FORM_MASK),
  328. &ioc->bios_pg2.RequestedBootDevice)) {
  329. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  330. "%s: req_boot_device(0x%016llx)\n",
  331. ioc->name, __func__,
  332. (unsigned long long)sas_address));
  333. ioc->req_boot_device.device = device;
  334. ioc->req_boot_device.is_raid = is_raid;
  335. }
  336. }
  337. if (!ioc->req_alt_boot_device.device) {
  338. if (_scsih_is_boot_device(sas_address, device_name,
  339. enclosure_logical_id, slot,
  340. (ioc->bios_pg2.ReqAltBootDeviceForm &
  341. MPI2_BIOSPAGE2_FORM_MASK),
  342. &ioc->bios_pg2.RequestedAltBootDevice)) {
  343. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  344. "%s: req_alt_boot_device(0x%016llx)\n",
  345. ioc->name, __func__,
  346. (unsigned long long)sas_address));
  347. ioc->req_alt_boot_device.device = device;
  348. ioc->req_alt_boot_device.is_raid = is_raid;
  349. }
  350. }
  351. if (!ioc->current_boot_device.device) {
  352. if (_scsih_is_boot_device(sas_address, device_name,
  353. enclosure_logical_id, slot,
  354. (ioc->bios_pg2.CurrentBootDeviceForm &
  355. MPI2_BIOSPAGE2_FORM_MASK),
  356. &ioc->bios_pg2.CurrentBootDevice)) {
  357. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  358. "%s: current_boot_device(0x%016llx)\n",
  359. ioc->name, __func__,
  360. (unsigned long long)sas_address));
  361. ioc->current_boot_device.device = device;
  362. ioc->current_boot_device.is_raid = is_raid;
  363. }
  364. }
  365. }
  366. /**
  367. * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
  368. * @ioc: per adapter object
  369. * @sas_address: sas address
  370. * Context: Calling function should acquire ioc->sas_device_lock
  371. *
  372. * This searches for sas_device based on sas_address, then return sas_device
  373. * object.
  374. */
  375. struct _sas_device *
  376. mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  377. u64 sas_address)
  378. {
  379. struct _sas_device *sas_device, *r;
  380. r = NULL;
  381. /* check the sas_device_init_list */
  382. list_for_each_entry(sas_device, &ioc->sas_device_init_list,
  383. list) {
  384. if (sas_device->sas_address != sas_address)
  385. continue;
  386. r = sas_device;
  387. goto out;
  388. }
  389. /* then check the sas_device_list */
  390. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  391. if (sas_device->sas_address != sas_address)
  392. continue;
  393. r = sas_device;
  394. goto out;
  395. }
  396. out:
  397. return r;
  398. }
  399. /**
  400. * _scsih_sas_device_find_by_handle - sas device search
  401. * @ioc: per adapter object
  402. * @handle: sas device handle (assigned by firmware)
  403. * Context: Calling function should acquire ioc->sas_device_lock
  404. *
  405. * This searches for sas_device based on sas_address, then return sas_device
  406. * object.
  407. */
  408. static struct _sas_device *
  409. _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  410. {
  411. struct _sas_device *sas_device, *r;
  412. r = NULL;
  413. if (ioc->wait_for_port_enable_to_complete) {
  414. list_for_each_entry(sas_device, &ioc->sas_device_init_list,
  415. list) {
  416. if (sas_device->handle != handle)
  417. continue;
  418. r = sas_device;
  419. goto out;
  420. }
  421. } else {
  422. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  423. if (sas_device->handle != handle)
  424. continue;
  425. r = sas_device;
  426. goto out;
  427. }
  428. }
  429. out:
  430. return r;
  431. }
  432. /**
  433. * _scsih_sas_device_remove - remove sas_device from list.
  434. * @ioc: per adapter object
  435. * @sas_device: the sas_device object
  436. * Context: This function will acquire ioc->sas_device_lock.
  437. *
  438. * Removing object and freeing associated memory from the ioc->sas_device_list.
  439. */
  440. static void
  441. _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
  442. struct _sas_device *sas_device)
  443. {
  444. unsigned long flags;
  445. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  446. list_del(&sas_device->list);
  447. memset(sas_device, 0, sizeof(struct _sas_device));
  448. kfree(sas_device);
  449. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  450. }
  451. /**
  452. * _scsih_sas_device_add - insert sas_device to the list.
  453. * @ioc: per adapter object
  454. * @sas_device: the sas_device object
  455. * Context: This function will acquire ioc->sas_device_lock.
  456. *
  457. * Adding new object to the ioc->sas_device_list.
  458. */
  459. static void
  460. _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
  461. struct _sas_device *sas_device)
  462. {
  463. unsigned long flags;
  464. u16 handle, parent_handle;
  465. u64 sas_address;
  466. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  467. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  468. sas_device->handle, (unsigned long long)sas_device->sas_address));
  469. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  470. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  471. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  472. handle = sas_device->handle;
  473. parent_handle = sas_device->parent_handle;
  474. sas_address = sas_device->sas_address;
  475. if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
  476. _scsih_sas_device_remove(ioc, sas_device);
  477. }
  478. /**
  479. * _scsih_sas_device_init_add - insert sas_device to the list.
  480. * @ioc: per adapter object
  481. * @sas_device: the sas_device object
  482. * Context: This function will acquire ioc->sas_device_lock.
  483. *
  484. * Adding new object at driver load time to the ioc->sas_device_init_list.
  485. */
  486. static void
  487. _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
  488. struct _sas_device *sas_device)
  489. {
  490. unsigned long flags;
  491. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  492. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  493. sas_device->handle, (unsigned long long)sas_device->sas_address));
  494. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  495. list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
  496. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  497. _scsih_determine_boot_device(ioc, sas_device, 0);
  498. }
  499. /**
  500. * mpt2sas_scsih_expander_find_by_handle - expander device search
  501. * @ioc: per adapter object
  502. * @handle: expander handle (assigned by firmware)
  503. * Context: Calling function should acquire ioc->sas_device_lock
  504. *
  505. * This searches for expander device based on handle, then returns the
  506. * sas_node object.
  507. */
  508. struct _sas_node *
  509. mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  510. {
  511. struct _sas_node *sas_expander, *r;
  512. r = NULL;
  513. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  514. if (sas_expander->handle != handle)
  515. continue;
  516. r = sas_expander;
  517. goto out;
  518. }
  519. out:
  520. return r;
  521. }
  522. /**
  523. * _scsih_raid_device_find_by_id - raid device search
  524. * @ioc: per adapter object
  525. * @id: sas device target id
  526. * @channel: sas device channel
  527. * Context: Calling function should acquire ioc->raid_device_lock
  528. *
  529. * This searches for raid_device based on target id, then return raid_device
  530. * object.
  531. */
  532. static struct _raid_device *
  533. _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
  534. {
  535. struct _raid_device *raid_device, *r;
  536. r = NULL;
  537. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  538. if (raid_device->id == id && raid_device->channel == channel) {
  539. r = raid_device;
  540. goto out;
  541. }
  542. }
  543. out:
  544. return r;
  545. }
  546. /**
  547. * _scsih_raid_device_find_by_handle - raid device search
  548. * @ioc: per adapter object
  549. * @handle: sas device handle (assigned by firmware)
  550. * Context: Calling function should acquire ioc->raid_device_lock
  551. *
  552. * This searches for raid_device based on handle, then return raid_device
  553. * object.
  554. */
  555. static struct _raid_device *
  556. _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  557. {
  558. struct _raid_device *raid_device, *r;
  559. r = NULL;
  560. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  561. if (raid_device->handle != handle)
  562. continue;
  563. r = raid_device;
  564. goto out;
  565. }
  566. out:
  567. return r;
  568. }
  569. /**
  570. * _scsih_raid_device_find_by_wwid - raid device search
  571. * @ioc: per adapter object
  572. * @handle: sas device handle (assigned by firmware)
  573. * Context: Calling function should acquire ioc->raid_device_lock
  574. *
  575. * This searches for raid_device based on wwid, then return raid_device
  576. * object.
  577. */
  578. static struct _raid_device *
  579. _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
  580. {
  581. struct _raid_device *raid_device, *r;
  582. r = NULL;
  583. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  584. if (raid_device->wwid != wwid)
  585. continue;
  586. r = raid_device;
  587. goto out;
  588. }
  589. out:
  590. return r;
  591. }
  592. /**
  593. * _scsih_raid_device_add - add raid_device object
  594. * @ioc: per adapter object
  595. * @raid_device: raid_device object
  596. *
  597. * This is added to the raid_device_list link list.
  598. */
  599. static void
  600. _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
  601. struct _raid_device *raid_device)
  602. {
  603. unsigned long flags;
  604. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  605. "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  606. raid_device->handle, (unsigned long long)raid_device->wwid));
  607. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  608. list_add_tail(&raid_device->list, &ioc->raid_device_list);
  609. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  610. }
  611. /**
  612. * _scsih_raid_device_remove - delete raid_device object
  613. * @ioc: per adapter object
  614. * @raid_device: raid_device object
  615. *
  616. * This is removed from the raid_device_list link list.
  617. */
  618. static void
  619. _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
  620. struct _raid_device *raid_device)
  621. {
  622. unsigned long flags;
  623. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  624. list_del(&raid_device->list);
  625. memset(raid_device, 0, sizeof(struct _raid_device));
  626. kfree(raid_device);
  627. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  628. }
  629. /**
  630. * mpt2sas_scsih_expander_find_by_sas_address - expander device search
  631. * @ioc: per adapter object
  632. * @sas_address: sas address
  633. * Context: Calling function should acquire ioc->sas_node_lock.
  634. *
  635. * This searches for expander device based on sas_address, then returns the
  636. * sas_node object.
  637. */
  638. struct _sas_node *
  639. mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  640. u64 sas_address)
  641. {
  642. struct _sas_node *sas_expander, *r;
  643. r = NULL;
  644. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  645. if (sas_expander->sas_address != sas_address)
  646. continue;
  647. r = sas_expander;
  648. goto out;
  649. }
  650. out:
  651. return r;
  652. }
  653. /**
  654. * _scsih_expander_node_add - insert expander device to the list.
  655. * @ioc: per adapter object
  656. * @sas_expander: the sas_device object
  657. * Context: This function will acquire ioc->sas_node_lock.
  658. *
  659. * Adding new object to the ioc->sas_expander_list.
  660. *
  661. * Return nothing.
  662. */
  663. static void
  664. _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
  665. struct _sas_node *sas_expander)
  666. {
  667. unsigned long flags;
  668. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  669. list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
  670. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  671. }
  672. /**
  673. * _scsih_is_end_device - determines if device is an end device
  674. * @device_info: bitfield providing information about the device.
  675. * Context: none
  676. *
  677. * Returns 1 if end device.
  678. */
  679. static int
  680. _scsih_is_end_device(u32 device_info)
  681. {
  682. if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
  683. ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
  684. (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
  685. (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
  686. return 1;
  687. else
  688. return 0;
  689. }
  690. /**
  691. * _scsih_scsi_lookup_get - returns scmd entry
  692. * @ioc: per adapter object
  693. * @smid: system request message index
  694. * Context: This function will acquire ioc->scsi_lookup_lock.
  695. *
  696. * Returns the smid stored scmd pointer.
  697. */
  698. static struct scsi_cmnd *
  699. _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  700. {
  701. unsigned long flags;
  702. struct scsi_cmnd *scmd;
  703. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  704. scmd = ioc->scsi_lookup[smid - 1].scmd;
  705. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  706. return scmd;
  707. }
  708. /**
  709. * mptscsih_getclear_scsi_lookup - returns scmd entry
  710. * @ioc: per adapter object
  711. * @smid: system request message index
  712. * Context: This function will acquire ioc->scsi_lookup_lock.
  713. *
  714. * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
  715. */
  716. static struct scsi_cmnd *
  717. _scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  718. {
  719. unsigned long flags;
  720. struct scsi_cmnd *scmd;
  721. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  722. scmd = ioc->scsi_lookup[smid - 1].scmd;
  723. ioc->scsi_lookup[smid - 1].scmd = NULL;
  724. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  725. return scmd;
  726. }
  727. /**
  728. * _scsih_scsi_lookup_set - updates scmd entry in lookup
  729. * @ioc: per adapter object
  730. * @smid: system request message index
  731. * @scmd: pointer to scsi command object
  732. * Context: This function will acquire ioc->scsi_lookup_lock.
  733. *
  734. * This will save scmd pointer in the scsi_lookup array.
  735. *
  736. * Return nothing.
  737. */
  738. static void
  739. _scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  740. struct scsi_cmnd *scmd)
  741. {
  742. unsigned long flags;
  743. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  744. ioc->scsi_lookup[smid - 1].scmd = scmd;
  745. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  746. }
  747. /**
  748. * _scsih_scsi_lookup_find_by_scmd - scmd lookup
  749. * @ioc: per adapter object
  750. * @smid: system request message index
  751. * @scmd: pointer to scsi command object
  752. * Context: This function will acquire ioc->scsi_lookup_lock.
  753. *
  754. * This will search for a scmd pointer in the scsi_lookup array,
  755. * returning the revelent smid. A returned value of zero means invalid.
  756. */
  757. static u16
  758. _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
  759. *scmd)
  760. {
  761. u16 smid;
  762. unsigned long flags;
  763. int i;
  764. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  765. smid = 0;
  766. for (i = 0; i < ioc->request_depth; i++) {
  767. if (ioc->scsi_lookup[i].scmd == scmd) {
  768. smid = i + 1;
  769. goto out;
  770. }
  771. }
  772. out:
  773. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  774. return smid;
  775. }
  776. /**
  777. * _scsih_scsi_lookup_find_by_target - search for matching channel:id
  778. * @ioc: per adapter object
  779. * @id: target id
  780. * @channel: channel
  781. * Context: This function will acquire ioc->scsi_lookup_lock.
  782. *
  783. * This will search for a matching channel:id in the scsi_lookup array,
  784. * returning 1 if found.
  785. */
  786. static u8
  787. _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
  788. int channel)
  789. {
  790. u8 found;
  791. unsigned long flags;
  792. int i;
  793. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  794. found = 0;
  795. for (i = 0 ; i < ioc->request_depth; i++) {
  796. if (ioc->scsi_lookup[i].scmd &&
  797. (ioc->scsi_lookup[i].scmd->device->id == id &&
  798. ioc->scsi_lookup[i].scmd->device->channel == channel)) {
  799. found = 1;
  800. goto out;
  801. }
  802. }
  803. out:
  804. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  805. return found;
  806. }
  807. /**
  808. * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
  809. * @ioc: per adapter object
  810. * @id: target id
  811. * @lun: lun number
  812. * @channel: channel
  813. * Context: This function will acquire ioc->scsi_lookup_lock.
  814. *
  815. * This will search for a matching channel:id:lun in the scsi_lookup array,
  816. * returning 1 if found.
  817. */
  818. static u8
  819. _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
  820. unsigned int lun, int channel)
  821. {
  822. u8 found;
  823. unsigned long flags;
  824. int i;
  825. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  826. found = 0;
  827. for (i = 0 ; i < ioc->request_depth; i++) {
  828. if (ioc->scsi_lookup[i].scmd &&
  829. (ioc->scsi_lookup[i].scmd->device->id == id &&
  830. ioc->scsi_lookup[i].scmd->device->channel == channel &&
  831. ioc->scsi_lookup[i].scmd->device->lun == lun)) {
  832. found = 1;
  833. goto out;
  834. }
  835. }
  836. out:
  837. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  838. return found;
  839. }
  840. /**
  841. * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
  842. * @ioc: per adapter object
  843. * @smid: system request message index
  844. *
  845. * Returns phys pointer to chain buffer.
  846. */
  847. static dma_addr_t
  848. _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  849. {
  850. return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
  851. ioc->chains_needed_per_io));
  852. }
  853. /**
  854. * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
  855. * @ioc: per adapter object
  856. * @smid: system request message index
  857. *
  858. * Returns virt pointer to chain buffer.
  859. */
  860. static void *
  861. _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  862. {
  863. return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
  864. ioc->chains_needed_per_io)));
  865. }
  866. /**
  867. * _scsih_build_scatter_gather - main sg creation routine
  868. * @ioc: per adapter object
  869. * @scmd: scsi command
  870. * @smid: system request message index
  871. * Context: none.
  872. *
  873. * The main routine that builds scatter gather table from a given
  874. * scsi request sent via the .queuecommand main handler.
  875. *
  876. * Returns 0 success, anything else error
  877. */
  878. static int
  879. _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
  880. struct scsi_cmnd *scmd, u16 smid)
  881. {
  882. Mpi2SCSIIORequest_t *mpi_request;
  883. dma_addr_t chain_dma;
  884. struct scatterlist *sg_scmd;
  885. void *sg_local, *chain;
  886. u32 chain_offset;
  887. u32 chain_length;
  888. u32 chain_flags;
  889. u32 sges_left;
  890. u32 sges_in_segment;
  891. u32 sgl_flags;
  892. u32 sgl_flags_last_element;
  893. u32 sgl_flags_end_buffer;
  894. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  895. /* init scatter gather flags */
  896. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  897. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  898. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  899. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  900. << MPI2_SGE_FLAGS_SHIFT;
  901. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  902. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  903. << MPI2_SGE_FLAGS_SHIFT;
  904. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  905. sg_scmd = scsi_sglist(scmd);
  906. sges_left = scsi_dma_map(scmd);
  907. if (!sges_left) {
  908. sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
  909. " failed: request for %d bytes!\n", scsi_bufflen(scmd));
  910. return -ENOMEM;
  911. }
  912. sg_local = &mpi_request->SGL;
  913. sges_in_segment = ioc->max_sges_in_main_message;
  914. if (sges_left <= sges_in_segment)
  915. goto fill_in_last_segment;
  916. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  917. (sges_in_segment * ioc->sge_size))/4;
  918. /* fill in main message segment when there is a chain following */
  919. while (sges_in_segment) {
  920. if (sges_in_segment == 1)
  921. ioc->base_add_sg_single(sg_local,
  922. sgl_flags_last_element | sg_dma_len(sg_scmd),
  923. sg_dma_address(sg_scmd));
  924. else
  925. ioc->base_add_sg_single(sg_local, sgl_flags |
  926. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  927. sg_scmd = sg_next(sg_scmd);
  928. sg_local += ioc->sge_size;
  929. sges_left--;
  930. sges_in_segment--;
  931. }
  932. /* initializing the chain flags and pointers */
  933. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  934. chain = _scsih_get_chain_buffer(ioc, smid);
  935. chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
  936. do {
  937. sges_in_segment = (sges_left <=
  938. ioc->max_sges_in_chain_message) ? sges_left :
  939. ioc->max_sges_in_chain_message;
  940. chain_offset = (sges_left == sges_in_segment) ?
  941. 0 : (sges_in_segment * ioc->sge_size)/4;
  942. chain_length = sges_in_segment * ioc->sge_size;
  943. if (chain_offset) {
  944. chain_offset = chain_offset <<
  945. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  946. chain_length += ioc->sge_size;
  947. }
  948. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  949. chain_length, chain_dma);
  950. sg_local = chain;
  951. if (!chain_offset)
  952. goto fill_in_last_segment;
  953. /* fill in chain segments */
  954. while (sges_in_segment) {
  955. if (sges_in_segment == 1)
  956. ioc->base_add_sg_single(sg_local,
  957. sgl_flags_last_element |
  958. sg_dma_len(sg_scmd),
  959. sg_dma_address(sg_scmd));
  960. else
  961. ioc->base_add_sg_single(sg_local, sgl_flags |
  962. sg_dma_len(sg_scmd),
  963. sg_dma_address(sg_scmd));
  964. sg_scmd = sg_next(sg_scmd);
  965. sg_local += ioc->sge_size;
  966. sges_left--;
  967. sges_in_segment--;
  968. }
  969. chain_dma += ioc->request_sz;
  970. chain += ioc->request_sz;
  971. } while (1);
  972. fill_in_last_segment:
  973. /* fill the last segment */
  974. while (sges_left) {
  975. if (sges_left == 1)
  976. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  977. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  978. else
  979. ioc->base_add_sg_single(sg_local, sgl_flags |
  980. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  981. sg_scmd = sg_next(sg_scmd);
  982. sg_local += ioc->sge_size;
  983. sges_left--;
  984. }
  985. return 0;
  986. }
  987. /**
  988. * _scsih_change_queue_depth - setting device queue depth
  989. * @sdev: scsi device struct
  990. * @qdepth: requested queue depth
  991. *
  992. * Returns queue depth.
  993. */
  994. static int
  995. _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
  996. {
  997. struct Scsi_Host *shost = sdev->host;
  998. int max_depth;
  999. int tag_type;
  1000. max_depth = shost->can_queue;
  1001. if (!sdev->tagged_supported)
  1002. max_depth = 1;
  1003. if (qdepth > max_depth)
  1004. qdepth = max_depth;
  1005. tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
  1006. scsi_adjust_queue_depth(sdev, tag_type, qdepth);
  1007. if (sdev->inquiry_len > 7)
  1008. sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
  1009. "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
  1010. sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
  1011. sdev->ordered_tags, sdev->scsi_level,
  1012. (sdev->inquiry[7] & 2) >> 1);
  1013. return sdev->queue_depth;
  1014. }
  1015. /**
  1016. * _scsih_change_queue_depth - changing device queue tag type
  1017. * @sdev: scsi device struct
  1018. * @tag_type: requested tag type
  1019. *
  1020. * Returns queue tag type.
  1021. */
  1022. static int
  1023. _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
  1024. {
  1025. if (sdev->tagged_supported) {
  1026. scsi_set_tag_type(sdev, tag_type);
  1027. if (tag_type)
  1028. scsi_activate_tcq(sdev, sdev->queue_depth);
  1029. else
  1030. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1031. } else
  1032. tag_type = 0;
  1033. return tag_type;
  1034. }
  1035. /**
  1036. * _scsih_target_alloc - target add routine
  1037. * @starget: scsi target struct
  1038. *
  1039. * Returns 0 if ok. Any other return is assumed to be an error and
  1040. * the device is ignored.
  1041. */
  1042. static int
  1043. _scsih_target_alloc(struct scsi_target *starget)
  1044. {
  1045. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1046. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1047. struct MPT2SAS_TARGET *sas_target_priv_data;
  1048. struct _sas_device *sas_device;
  1049. struct _raid_device *raid_device;
  1050. unsigned long flags;
  1051. struct sas_rphy *rphy;
  1052. sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
  1053. if (!sas_target_priv_data)
  1054. return -ENOMEM;
  1055. starget->hostdata = sas_target_priv_data;
  1056. sas_target_priv_data->starget = starget;
  1057. sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
  1058. /* RAID volumes */
  1059. if (starget->channel == RAID_CHANNEL) {
  1060. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1061. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1062. starget->channel);
  1063. if (raid_device) {
  1064. sas_target_priv_data->handle = raid_device->handle;
  1065. sas_target_priv_data->sas_address = raid_device->wwid;
  1066. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
  1067. raid_device->starget = starget;
  1068. }
  1069. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1070. return 0;
  1071. }
  1072. /* sas/sata devices */
  1073. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1074. rphy = dev_to_rphy(starget->dev.parent);
  1075. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1076. rphy->identify.sas_address);
  1077. if (sas_device) {
  1078. sas_target_priv_data->handle = sas_device->handle;
  1079. sas_target_priv_data->sas_address = sas_device->sas_address;
  1080. sas_device->starget = starget;
  1081. sas_device->id = starget->id;
  1082. sas_device->channel = starget->channel;
  1083. if (sas_device->hidden_raid_component)
  1084. sas_target_priv_data->flags |=
  1085. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1086. }
  1087. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1088. return 0;
  1089. }
  1090. /**
  1091. * _scsih_target_destroy - target destroy routine
  1092. * @starget: scsi target struct
  1093. *
  1094. * Returns nothing.
  1095. */
  1096. static void
  1097. _scsih_target_destroy(struct scsi_target *starget)
  1098. {
  1099. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1100. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1101. struct MPT2SAS_TARGET *sas_target_priv_data;
  1102. struct _sas_device *sas_device;
  1103. struct _raid_device *raid_device;
  1104. unsigned long flags;
  1105. struct sas_rphy *rphy;
  1106. sas_target_priv_data = starget->hostdata;
  1107. if (!sas_target_priv_data)
  1108. return;
  1109. if (starget->channel == RAID_CHANNEL) {
  1110. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1111. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1112. starget->channel);
  1113. if (raid_device) {
  1114. raid_device->starget = NULL;
  1115. raid_device->sdev = NULL;
  1116. }
  1117. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1118. goto out;
  1119. }
  1120. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1121. rphy = dev_to_rphy(starget->dev.parent);
  1122. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1123. rphy->identify.sas_address);
  1124. if (sas_device && (sas_device->starget == starget) &&
  1125. (sas_device->id == starget->id) &&
  1126. (sas_device->channel == starget->channel))
  1127. sas_device->starget = NULL;
  1128. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1129. out:
  1130. kfree(sas_target_priv_data);
  1131. starget->hostdata = NULL;
  1132. }
  1133. /**
  1134. * _scsih_slave_alloc - device add routine
  1135. * @sdev: scsi device struct
  1136. *
  1137. * Returns 0 if ok. Any other return is assumed to be an error and
  1138. * the device is ignored.
  1139. */
  1140. static int
  1141. _scsih_slave_alloc(struct scsi_device *sdev)
  1142. {
  1143. struct Scsi_Host *shost;
  1144. struct MPT2SAS_ADAPTER *ioc;
  1145. struct MPT2SAS_TARGET *sas_target_priv_data;
  1146. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1147. struct scsi_target *starget;
  1148. struct _raid_device *raid_device;
  1149. struct _sas_device *sas_device;
  1150. unsigned long flags;
  1151. sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
  1152. if (!sas_device_priv_data)
  1153. return -ENOMEM;
  1154. sas_device_priv_data->lun = sdev->lun;
  1155. sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
  1156. starget = scsi_target(sdev);
  1157. sas_target_priv_data = starget->hostdata;
  1158. sas_target_priv_data->num_luns++;
  1159. sas_device_priv_data->sas_target = sas_target_priv_data;
  1160. sdev->hostdata = sas_device_priv_data;
  1161. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
  1162. sdev->no_uld_attach = 1;
  1163. shost = dev_to_shost(&starget->dev);
  1164. ioc = shost_priv(shost);
  1165. if (starget->channel == RAID_CHANNEL) {
  1166. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1167. raid_device = _scsih_raid_device_find_by_id(ioc,
  1168. starget->id, starget->channel);
  1169. if (raid_device)
  1170. raid_device->sdev = sdev; /* raid is single lun */
  1171. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1172. } else {
  1173. /* set TLR bit for SSP devices */
  1174. if (!(ioc->facts.IOCCapabilities &
  1175. MPI2_IOCFACTS_CAPABILITY_TLR))
  1176. goto out;
  1177. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1178. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1179. sas_device_priv_data->sas_target->sas_address);
  1180. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1181. if (sas_device && sas_device->device_info &
  1182. MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  1183. sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
  1184. }
  1185. out:
  1186. return 0;
  1187. }
  1188. /**
  1189. * _scsih_slave_destroy - device destroy routine
  1190. * @sdev: scsi device struct
  1191. *
  1192. * Returns nothing.
  1193. */
  1194. static void
  1195. _scsih_slave_destroy(struct scsi_device *sdev)
  1196. {
  1197. struct MPT2SAS_TARGET *sas_target_priv_data;
  1198. struct scsi_target *starget;
  1199. if (!sdev->hostdata)
  1200. return;
  1201. starget = scsi_target(sdev);
  1202. sas_target_priv_data = starget->hostdata;
  1203. sas_target_priv_data->num_luns--;
  1204. kfree(sdev->hostdata);
  1205. sdev->hostdata = NULL;
  1206. }
  1207. /**
  1208. * _scsih_display_sata_capabilities - sata capabilities
  1209. * @ioc: per adapter object
  1210. * @sas_device: the sas_device object
  1211. * @sdev: scsi device struct
  1212. */
  1213. static void
  1214. _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1215. struct _sas_device *sas_device, struct scsi_device *sdev)
  1216. {
  1217. Mpi2ConfigReply_t mpi_reply;
  1218. Mpi2SasDevicePage0_t sas_device_pg0;
  1219. u32 ioc_status;
  1220. u16 flags;
  1221. u32 device_info;
  1222. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  1223. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
  1224. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1225. ioc->name, __FILE__, __LINE__, __func__);
  1226. return;
  1227. }
  1228. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1229. MPI2_IOCSTATUS_MASK;
  1230. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1231. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1232. ioc->name, __FILE__, __LINE__, __func__);
  1233. return;
  1234. }
  1235. flags = le16_to_cpu(sas_device_pg0.Flags);
  1236. device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
  1237. sdev_printk(KERN_INFO, sdev,
  1238. "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
  1239. "sw_preserve(%s)\n",
  1240. (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
  1241. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
  1242. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
  1243. "n",
  1244. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
  1245. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
  1246. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
  1247. }
  1248. /**
  1249. * _scsih_get_volume_capabilities - volume capabilities
  1250. * @ioc: per adapter object
  1251. * @sas_device: the raid_device object
  1252. */
  1253. static void
  1254. _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1255. struct _raid_device *raid_device)
  1256. {
  1257. Mpi2RaidVolPage0_t *vol_pg0;
  1258. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1259. Mpi2SasDevicePage0_t sas_device_pg0;
  1260. Mpi2ConfigReply_t mpi_reply;
  1261. u16 sz;
  1262. u8 num_pds;
  1263. if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
  1264. &num_pds)) || !num_pds) {
  1265. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1266. ioc->name, __FILE__, __LINE__, __func__);
  1267. return;
  1268. }
  1269. raid_device->num_pds = num_pds;
  1270. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1271. sizeof(Mpi2RaidVol0PhysDisk_t));
  1272. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1273. if (!vol_pg0) {
  1274. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1275. ioc->name, __FILE__, __LINE__, __func__);
  1276. return;
  1277. }
  1278. if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1279. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1280. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1281. ioc->name, __FILE__, __LINE__, __func__);
  1282. kfree(vol_pg0);
  1283. return;
  1284. }
  1285. raid_device->volume_type = vol_pg0->VolumeType;
  1286. /* figure out what the underlying devices are by
  1287. * obtaining the device_info bits for the 1st device
  1288. */
  1289. if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1290. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1291. vol_pg0->PhysDisk[0].PhysDiskNum))) {
  1292. if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  1293. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  1294. le16_to_cpu(pd_pg0.DevHandle)))) {
  1295. raid_device->device_info =
  1296. le32_to_cpu(sas_device_pg0.DeviceInfo);
  1297. }
  1298. }
  1299. kfree(vol_pg0);
  1300. }
  1301. /**
  1302. * _scsih_slave_configure - device configure routine.
  1303. * @sdev: scsi device struct
  1304. *
  1305. * Returns 0 if ok. Any other return is assumed to be an error and
  1306. * the device is ignored.
  1307. */
  1308. static int
  1309. _scsih_slave_configure(struct scsi_device *sdev)
  1310. {
  1311. struct Scsi_Host *shost = sdev->host;
  1312. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1313. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1314. struct MPT2SAS_TARGET *sas_target_priv_data;
  1315. struct _sas_device *sas_device;
  1316. struct _raid_device *raid_device;
  1317. unsigned long flags;
  1318. int qdepth;
  1319. u8 ssp_target = 0;
  1320. char *ds = "";
  1321. char *r_level = "";
  1322. qdepth = 1;
  1323. sas_device_priv_data = sdev->hostdata;
  1324. sas_device_priv_data->configured_lun = 1;
  1325. sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
  1326. sas_target_priv_data = sas_device_priv_data->sas_target;
  1327. /* raid volume handling */
  1328. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
  1329. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1330. raid_device = _scsih_raid_device_find_by_handle(ioc,
  1331. sas_target_priv_data->handle);
  1332. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1333. if (!raid_device) {
  1334. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1335. ioc->name, __FILE__, __LINE__, __func__);
  1336. return 0;
  1337. }
  1338. _scsih_get_volume_capabilities(ioc, raid_device);
  1339. /* RAID Queue Depth Support
  1340. * IS volume = underlying qdepth of drive type, either
  1341. * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
  1342. * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
  1343. */
  1344. if (raid_device->device_info &
  1345. MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1346. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1347. ds = "SSP";
  1348. } else {
  1349. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1350. if (raid_device->device_info &
  1351. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1352. ds = "SATA";
  1353. else
  1354. ds = "STP";
  1355. }
  1356. switch (raid_device->volume_type) {
  1357. case MPI2_RAID_VOL_TYPE_RAID0:
  1358. r_level = "RAID0";
  1359. break;
  1360. case MPI2_RAID_VOL_TYPE_RAID1E:
  1361. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1362. r_level = "RAID1E";
  1363. break;
  1364. case MPI2_RAID_VOL_TYPE_RAID1:
  1365. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1366. r_level = "RAID1";
  1367. break;
  1368. case MPI2_RAID_VOL_TYPE_RAID10:
  1369. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1370. r_level = "RAID10";
  1371. break;
  1372. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  1373. default:
  1374. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1375. r_level = "RAIDX";
  1376. break;
  1377. }
  1378. sdev_printk(KERN_INFO, sdev, "%s: "
  1379. "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
  1380. r_level, raid_device->handle,
  1381. (unsigned long long)raid_device->wwid,
  1382. raid_device->num_pds, ds);
  1383. _scsih_change_queue_depth(sdev, qdepth);
  1384. return 0;
  1385. }
  1386. /* non-raid handling */
  1387. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1388. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1389. sas_device_priv_data->sas_target->sas_address);
  1390. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1391. if (sas_device) {
  1392. if (sas_target_priv_data->flags &
  1393. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1394. mpt2sas_config_get_volume_handle(ioc,
  1395. sas_device->handle, &sas_device->volume_handle);
  1396. mpt2sas_config_get_volume_wwid(ioc,
  1397. sas_device->volume_handle,
  1398. &sas_device->volume_wwid);
  1399. }
  1400. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1401. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1402. ssp_target = 1;
  1403. ds = "SSP";
  1404. } else {
  1405. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1406. if (sas_device->device_info &
  1407. MPI2_SAS_DEVICE_INFO_STP_TARGET)
  1408. ds = "STP";
  1409. else if (sas_device->device_info &
  1410. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1411. ds = "SATA";
  1412. }
  1413. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
  1414. "sas_addr(0x%016llx), device_name(0x%016llx)\n",
  1415. ds, sas_device->handle,
  1416. (unsigned long long)sas_device->sas_address,
  1417. (unsigned long long)sas_device->device_name);
  1418. sdev_printk(KERN_INFO, sdev, "%s: "
  1419. "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
  1420. (unsigned long long) sas_device->enclosure_logical_id,
  1421. sas_device->slot);
  1422. if (!ssp_target)
  1423. _scsih_display_sata_capabilities(ioc, sas_device, sdev);
  1424. }
  1425. _scsih_change_queue_depth(sdev, qdepth);
  1426. if (ssp_target)
  1427. sas_read_port_mode_page(sdev);
  1428. return 0;
  1429. }
  1430. /**
  1431. * _scsih_bios_param - fetch head, sector, cylinder info for a disk
  1432. * @sdev: scsi device struct
  1433. * @bdev: pointer to block device context
  1434. * @capacity: device size (in 512 byte sectors)
  1435. * @params: three element array to place output:
  1436. * params[0] number of heads (max 255)
  1437. * params[1] number of sectors (max 63)
  1438. * params[2] number of cylinders
  1439. *
  1440. * Return nothing.
  1441. */
  1442. static int
  1443. _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1444. sector_t capacity, int params[])
  1445. {
  1446. int heads;
  1447. int sectors;
  1448. sector_t cylinders;
  1449. ulong dummy;
  1450. heads = 64;
  1451. sectors = 32;
  1452. dummy = heads * sectors;
  1453. cylinders = capacity;
  1454. sector_div(cylinders, dummy);
  1455. /*
  1456. * Handle extended translation size for logical drives
  1457. * > 1Gb
  1458. */
  1459. if ((ulong)capacity >= 0x200000) {
  1460. heads = 255;
  1461. sectors = 63;
  1462. dummy = heads * sectors;
  1463. cylinders = capacity;
  1464. sector_div(cylinders, dummy);
  1465. }
  1466. /* return result */
  1467. params[0] = heads;
  1468. params[1] = sectors;
  1469. params[2] = cylinders;
  1470. return 0;
  1471. }
  1472. /**
  1473. * _scsih_response_code - translation of device response code
  1474. * @ioc: per adapter object
  1475. * @response_code: response code returned by the device
  1476. *
  1477. * Return nothing.
  1478. */
  1479. static void
  1480. _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
  1481. {
  1482. char *desc;
  1483. switch (response_code) {
  1484. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  1485. desc = "task management request completed";
  1486. break;
  1487. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  1488. desc = "invalid frame";
  1489. break;
  1490. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1491. desc = "task management request not supported";
  1492. break;
  1493. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  1494. desc = "task management request failed";
  1495. break;
  1496. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1497. desc = "task management request succeeded";
  1498. break;
  1499. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1500. desc = "invalid lun";
  1501. break;
  1502. case 0xA:
  1503. desc = "overlapped tag attempted";
  1504. break;
  1505. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1506. desc = "task queued, however not sent to target";
  1507. break;
  1508. default:
  1509. desc = "unknown";
  1510. break;
  1511. }
  1512. printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
  1513. ioc->name, response_code, desc);
  1514. }
  1515. /**
  1516. * _scsih_tm_done - tm completion routine
  1517. * @ioc: per adapter object
  1518. * @smid: system request message index
  1519. * @VF_ID: virtual function id
  1520. * @reply: reply message frame(lower 32bit addr)
  1521. * Context: none.
  1522. *
  1523. * The callback handler when using scsih_issue_tm.
  1524. *
  1525. * Return nothing.
  1526. */
  1527. static void
  1528. _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  1529. {
  1530. MPI2DefaultReply_t *mpi_reply;
  1531. if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
  1532. return;
  1533. if (ioc->tm_cmds.smid != smid)
  1534. return;
  1535. ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
  1536. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  1537. if (mpi_reply) {
  1538. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1539. ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
  1540. }
  1541. ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
  1542. complete(&ioc->tm_cmds.done);
  1543. }
  1544. /**
  1545. * mpt2sas_scsih_set_tm_flag - set per target tm_busy
  1546. * @ioc: per adapter object
  1547. * @handle: device handle
  1548. *
  1549. * During taskmangement request, we need to freeze the device queue.
  1550. */
  1551. void
  1552. mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1553. {
  1554. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1555. struct scsi_device *sdev;
  1556. u8 skip = 0;
  1557. shost_for_each_device(sdev, ioc->shost) {
  1558. if (skip)
  1559. continue;
  1560. sas_device_priv_data = sdev->hostdata;
  1561. if (!sas_device_priv_data)
  1562. continue;
  1563. if (sas_device_priv_data->sas_target->handle == handle) {
  1564. sas_device_priv_data->sas_target->tm_busy = 1;
  1565. skip = 1;
  1566. ioc->ignore_loginfos = 1;
  1567. }
  1568. }
  1569. }
  1570. /**
  1571. * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
  1572. * @ioc: per adapter object
  1573. * @handle: device handle
  1574. *
  1575. * During taskmangement request, we need to freeze the device queue.
  1576. */
  1577. void
  1578. mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1579. {
  1580. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1581. struct scsi_device *sdev;
  1582. u8 skip = 0;
  1583. shost_for_each_device(sdev, ioc->shost) {
  1584. if (skip)
  1585. continue;
  1586. sas_device_priv_data = sdev->hostdata;
  1587. if (!sas_device_priv_data)
  1588. continue;
  1589. if (sas_device_priv_data->sas_target->handle == handle) {
  1590. sas_device_priv_data->sas_target->tm_busy = 0;
  1591. skip = 1;
  1592. ioc->ignore_loginfos = 0;
  1593. }
  1594. }
  1595. }
  1596. /**
  1597. * mpt2sas_scsih_issue_tm - main routine for sending tm requests
  1598. * @ioc: per adapter struct
  1599. * @device_handle: device handle
  1600. * @lun: lun number
  1601. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  1602. * @smid_task: smid assigned to the task
  1603. * @timeout: timeout in seconds
  1604. * Context: The calling function needs to acquire the tm_cmds.mutex
  1605. *
  1606. * A generic API for sending task management requests to firmware.
  1607. *
  1608. * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
  1609. * this API.
  1610. *
  1611. * The callback index is set inside `ioc->tm_cb_idx`.
  1612. *
  1613. * Return nothing.
  1614. */
  1615. void
  1616. mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
  1617. u8 type, u16 smid_task, ulong timeout)
  1618. {
  1619. Mpi2SCSITaskManagementRequest_t *mpi_request;
  1620. Mpi2SCSITaskManagementReply_t *mpi_reply;
  1621. u16 smid = 0;
  1622. u32 ioc_state;
  1623. unsigned long timeleft;
  1624. u8 VF_ID = 0;
  1625. if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
  1626. printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
  1627. __func__, ioc->name);
  1628. return;
  1629. }
  1630. if (ioc->shost_recovery) {
  1631. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1632. __func__, ioc->name);
  1633. return;
  1634. }
  1635. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  1636. if (ioc_state & MPI2_DOORBELL_USED) {
  1637. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
  1638. "active!\n", ioc->name));
  1639. goto issue_host_reset;
  1640. }
  1641. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  1642. mpt2sas_base_fault_info(ioc, ioc_state &
  1643. MPI2_DOORBELL_DATA_MASK);
  1644. goto issue_host_reset;
  1645. }
  1646. smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
  1647. if (!smid) {
  1648. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1649. ioc->name, __func__);
  1650. return;
  1651. }
  1652. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
  1653. " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
  1654. ioc->tm_cmds.status = MPT2_CMD_PENDING;
  1655. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1656. ioc->tm_cmds.smid = smid;
  1657. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  1658. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  1659. mpi_request->DevHandle = cpu_to_le16(handle);
  1660. mpi_request->TaskType = type;
  1661. mpi_request->TaskMID = cpu_to_le16(smid_task);
  1662. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  1663. mpt2sas_scsih_set_tm_flag(ioc, handle);
  1664. mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
  1665. timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  1666. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  1667. if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
  1668. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1669. ioc->name, __func__);
  1670. _debug_dump_mf(mpi_request,
  1671. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  1672. if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
  1673. goto issue_host_reset;
  1674. }
  1675. if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
  1676. mpi_reply = ioc->tm_cmds.reply;
  1677. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
  1678. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  1679. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  1680. le32_to_cpu(mpi_reply->IOCLogInfo),
  1681. le32_to_cpu(mpi_reply->TerminationCount)));
  1682. if (ioc->logging_level & MPT_DEBUG_TM)
  1683. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  1684. }
  1685. return;
  1686. issue_host_reset:
  1687. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
  1688. }
  1689. /**
  1690. * _scsih_abort - eh threads main abort routine
  1691. * @sdev: scsi device struct
  1692. *
  1693. * Returns SUCCESS if command aborted else FAILED
  1694. */
  1695. static int
  1696. _scsih_abort(struct scsi_cmnd *scmd)
  1697. {
  1698. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1699. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1700. u16 smid;
  1701. u16 handle;
  1702. int r;
  1703. struct scsi_cmnd *scmd_lookup;
  1704. printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
  1705. ioc->name, scmd);
  1706. scsi_print_command(scmd);
  1707. sas_device_priv_data = scmd->device->hostdata;
  1708. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1709. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1710. ioc->name, scmd);
  1711. scmd->result = DID_NO_CONNECT << 16;
  1712. scmd->scsi_done(scmd);
  1713. r = SUCCESS;
  1714. goto out;
  1715. }
  1716. /* search for the command */
  1717. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  1718. if (!smid) {
  1719. scmd->result = DID_RESET << 16;
  1720. r = SUCCESS;
  1721. goto out;
  1722. }
  1723. /* for hidden raid components and volumes this is not supported */
  1724. if (sas_device_priv_data->sas_target->flags &
  1725. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  1726. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  1727. scmd->result = DID_RESET << 16;
  1728. r = FAILED;
  1729. goto out;
  1730. }
  1731. mutex_lock(&ioc->tm_cmds.mutex);
  1732. handle = sas_device_priv_data->sas_target->handle;
  1733. mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
  1734. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
  1735. /* sanity check - see whether command actually completed */
  1736. scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
  1737. if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
  1738. r = FAILED;
  1739. else
  1740. r = SUCCESS;
  1741. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1742. mutex_unlock(&ioc->tm_cmds.mutex);
  1743. out:
  1744. printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
  1745. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1746. return r;
  1747. }
  1748. /**
  1749. * _scsih_dev_reset - eh threads main device reset routine
  1750. * @sdev: scsi device struct
  1751. *
  1752. * Returns SUCCESS if command aborted else FAILED
  1753. */
  1754. static int
  1755. _scsih_dev_reset(struct scsi_cmnd *scmd)
  1756. {
  1757. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1758. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1759. struct _sas_device *sas_device;
  1760. unsigned long flags;
  1761. u16 handle;
  1762. int r;
  1763. printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
  1764. ioc->name, scmd);
  1765. scsi_print_command(scmd);
  1766. sas_device_priv_data = scmd->device->hostdata;
  1767. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1768. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1769. ioc->name, scmd);
  1770. scmd->result = DID_NO_CONNECT << 16;
  1771. scmd->scsi_done(scmd);
  1772. r = SUCCESS;
  1773. goto out;
  1774. }
  1775. /* for hidden raid components obtain the volume_handle */
  1776. handle = 0;
  1777. if (sas_device_priv_data->sas_target->flags &
  1778. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1779. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1780. sas_device = _scsih_sas_device_find_by_handle(ioc,
  1781. sas_device_priv_data->sas_target->handle);
  1782. if (sas_device)
  1783. handle = sas_device->volume_handle;
  1784. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1785. } else
  1786. handle = sas_device_priv_data->sas_target->handle;
  1787. if (!handle) {
  1788. scmd->result = DID_RESET << 16;
  1789. r = FAILED;
  1790. goto out;
  1791. }
  1792. mutex_lock(&ioc->tm_cmds.mutex);
  1793. mpt2sas_scsih_issue_tm(ioc, handle, 0,
  1794. MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
  1795. 30);
  1796. /*
  1797. * sanity check see whether all commands to this device been
  1798. * completed
  1799. */
  1800. if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
  1801. scmd->device->lun, scmd->device->channel))
  1802. r = FAILED;
  1803. else
  1804. r = SUCCESS;
  1805. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1806. mutex_unlock(&ioc->tm_cmds.mutex);
  1807. out:
  1808. printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
  1809. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1810. return r;
  1811. }
  1812. /**
  1813. * _scsih_target_reset - eh threads main target reset routine
  1814. * @sdev: scsi device struct
  1815. *
  1816. * Returns SUCCESS if command aborted else FAILED
  1817. */
  1818. static int
  1819. _scsih_target_reset(struct scsi_cmnd *scmd)
  1820. {
  1821. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1822. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1823. struct _sas_device *sas_device;
  1824. unsigned long flags;
  1825. u16 handle;
  1826. int r;
  1827. printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
  1828. ioc->name, scmd);
  1829. scsi_print_command(scmd);
  1830. sas_device_priv_data = scmd->device->hostdata;
  1831. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1832. printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
  1833. ioc->name, scmd);
  1834. scmd->result = DID_NO_CONNECT << 16;
  1835. scmd->scsi_done(scmd);
  1836. r = SUCCESS;
  1837. goto out;
  1838. }
  1839. /* for hidden raid components obtain the volume_handle */
  1840. handle = 0;
  1841. if (sas_device_priv_data->sas_target->flags &
  1842. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1843. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1844. sas_device = _scsih_sas_device_find_by_handle(ioc,
  1845. sas_device_priv_data->sas_target->handle);
  1846. if (sas_device)
  1847. handle = sas_device->volume_handle;
  1848. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1849. } else
  1850. handle = sas_device_priv_data->sas_target->handle;
  1851. if (!handle) {
  1852. scmd->result = DID_RESET << 16;
  1853. r = FAILED;
  1854. goto out;
  1855. }
  1856. mutex_lock(&ioc->tm_cmds.mutex);
  1857. mpt2sas_scsih_issue_tm(ioc, handle, 0,
  1858. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
  1859. /*
  1860. * sanity check see whether all commands to this target been
  1861. * completed
  1862. */
  1863. if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
  1864. scmd->device->channel))
  1865. r = FAILED;
  1866. else
  1867. r = SUCCESS;
  1868. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1869. mutex_unlock(&ioc->tm_cmds.mutex);
  1870. out:
  1871. printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
  1872. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1873. return r;
  1874. }
  1875. /**
  1876. * _scsih_abort - eh threads main host reset routine
  1877. * @sdev: scsi device struct
  1878. *
  1879. * Returns SUCCESS if command aborted else FAILED
  1880. */
  1881. static int
  1882. _scsih_host_reset(struct scsi_cmnd *scmd)
  1883. {
  1884. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1885. int r, retval;
  1886. printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
  1887. ioc->name, scmd);
  1888. scsi_print_command(scmd);
  1889. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1890. FORCE_BIG_HAMMER);
  1891. r = (retval < 0) ? FAILED : SUCCESS;
  1892. printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
  1893. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1894. return r;
  1895. }
  1896. /**
  1897. * _scsih_fw_event_add - insert and queue up fw_event
  1898. * @ioc: per adapter object
  1899. * @fw_event: object describing the event
  1900. * Context: This function will acquire ioc->fw_event_lock.
  1901. *
  1902. * This adds the firmware event object into link list, then queues it up to
  1903. * be processed from user context.
  1904. *
  1905. * Return nothing.
  1906. */
  1907. static void
  1908. _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  1909. {
  1910. unsigned long flags;
  1911. if (ioc->firmware_event_thread == NULL)
  1912. return;
  1913. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1914. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  1915. INIT_WORK(&fw_event->work, _firmware_event_work);
  1916. queue_work(ioc->firmware_event_thread, &fw_event->work);
  1917. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1918. }
  1919. /**
  1920. * _scsih_fw_event_free - delete fw_event
  1921. * @ioc: per adapter object
  1922. * @fw_event: object describing the event
  1923. * Context: This function will acquire ioc->fw_event_lock.
  1924. *
  1925. * This removes firmware event object from link list, frees associated memory.
  1926. *
  1927. * Return nothing.
  1928. */
  1929. static void
  1930. _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  1931. *fw_event)
  1932. {
  1933. unsigned long flags;
  1934. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1935. list_del(&fw_event->list);
  1936. kfree(fw_event->event_data);
  1937. kfree(fw_event);
  1938. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1939. }
  1940. /**
  1941. * _scsih_fw_event_add - requeue an event
  1942. * @ioc: per adapter object
  1943. * @fw_event: object describing the event
  1944. * Context: This function will acquire ioc->fw_event_lock.
  1945. *
  1946. * Return nothing.
  1947. */
  1948. static void
  1949. _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  1950. *fw_event, unsigned long delay)
  1951. {
  1952. unsigned long flags;
  1953. if (ioc->firmware_event_thread == NULL)
  1954. return;
  1955. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1956. queue_work(ioc->firmware_event_thread, &fw_event->work);
  1957. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1958. }
  1959. /**
  1960. * _scsih_fw_event_off - turn flag off preventing event handling
  1961. * @ioc: per adapter object
  1962. *
  1963. * Used to prevent handling of firmware events during adapter reset
  1964. * driver unload.
  1965. *
  1966. * Return nothing.
  1967. */
  1968. static void
  1969. _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
  1970. {
  1971. unsigned long flags;
  1972. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1973. ioc->fw_events_off = 1;
  1974. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1975. }
  1976. /**
  1977. * _scsih_fw_event_on - turn flag on allowing firmware event handling
  1978. * @ioc: per adapter object
  1979. *
  1980. * Returns nothing.
  1981. */
  1982. static void
  1983. _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
  1984. {
  1985. unsigned long flags;
  1986. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1987. ioc->fw_events_off = 0;
  1988. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1989. }
  1990. /**
  1991. * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
  1992. * @ioc: per adapter object
  1993. * @handle: device handle
  1994. *
  1995. * During device pull we need to appropiately set the sdev state.
  1996. */
  1997. static void
  1998. _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1999. {
  2000. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2001. struct scsi_device *sdev;
  2002. shost_for_each_device(sdev, ioc->shost) {
  2003. sas_device_priv_data = sdev->hostdata;
  2004. if (!sas_device_priv_data)
  2005. continue;
  2006. if (!sas_device_priv_data->block)
  2007. continue;
  2008. if (sas_device_priv_data->sas_target->handle == handle) {
  2009. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2010. MPT2SAS_INFO_FMT "SDEV_RUNNING: "
  2011. "handle(0x%04x)\n", ioc->name, handle));
  2012. sas_device_priv_data->block = 0;
  2013. scsi_device_set_state(sdev, SDEV_RUNNING);
  2014. }
  2015. }
  2016. }
  2017. /**
  2018. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  2019. * @ioc: per adapter object
  2020. * @handle: device handle
  2021. *
  2022. * During device pull we need to appropiately set the sdev state.
  2023. */
  2024. static void
  2025. _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2026. {
  2027. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2028. struct scsi_device *sdev;
  2029. shost_for_each_device(sdev, ioc->shost) {
  2030. sas_device_priv_data = sdev->hostdata;
  2031. if (!sas_device_priv_data)
  2032. continue;
  2033. if (sas_device_priv_data->block)
  2034. continue;
  2035. if (sas_device_priv_data->sas_target->handle == handle) {
  2036. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2037. MPT2SAS_INFO_FMT "SDEV_BLOCK: "
  2038. "handle(0x%04x)\n", ioc->name, handle));
  2039. sas_device_priv_data->block = 1;
  2040. scsi_device_set_state(sdev, SDEV_BLOCK);
  2041. }
  2042. }
  2043. }
  2044. /**
  2045. * _scsih_block_io_to_children_attached_to_ex
  2046. * @ioc: per adapter object
  2047. * @sas_expander: the sas_device object
  2048. *
  2049. * This routine set sdev state to SDEV_BLOCK for all devices
  2050. * attached to this expander. This function called when expander is
  2051. * pulled.
  2052. */
  2053. static void
  2054. _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
  2055. struct _sas_node *sas_expander)
  2056. {
  2057. struct _sas_port *mpt2sas_port;
  2058. struct _sas_device *sas_device;
  2059. struct _sas_node *expander_sibling;
  2060. unsigned long flags;
  2061. if (!sas_expander)
  2062. return;
  2063. list_for_each_entry(mpt2sas_port,
  2064. &sas_expander->sas_port_list, port_list) {
  2065. if (mpt2sas_port->remote_identify.device_type ==
  2066. SAS_END_DEVICE) {
  2067. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2068. sas_device =
  2069. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  2070. mpt2sas_port->remote_identify.sas_address);
  2071. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2072. if (!sas_device)
  2073. continue;
  2074. _scsih_block_io_device(ioc, sas_device->handle);
  2075. }
  2076. }
  2077. list_for_each_entry(mpt2sas_port,
  2078. &sas_expander->sas_port_list, port_list) {
  2079. if (mpt2sas_port->remote_identify.device_type ==
  2080. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  2081. mpt2sas_port->remote_identify.device_type ==
  2082. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  2083. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2084. expander_sibling =
  2085. mpt2sas_scsih_expander_find_by_sas_address(
  2086. ioc, mpt2sas_port->remote_identify.sas_address);
  2087. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2088. _scsih_block_io_to_children_attached_to_ex(ioc,
  2089. expander_sibling);
  2090. }
  2091. }
  2092. }
  2093. /**
  2094. * _scsih_block_io_to_children_attached_directly
  2095. * @ioc: per adapter object
  2096. * @event_data: topology change event data
  2097. *
  2098. * This routine set sdev state to SDEV_BLOCK for all devices
  2099. * direct attached during device pull.
  2100. */
  2101. static void
  2102. _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
  2103. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2104. {
  2105. int i;
  2106. u16 handle;
  2107. u16 reason_code;
  2108. u8 phy_number;
  2109. for (i = 0; i < event_data->NumEntries; i++) {
  2110. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2111. if (!handle)
  2112. continue;
  2113. phy_number = event_data->StartPhyNum + i;
  2114. reason_code = event_data->PHY[i].PhyStatus &
  2115. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2116. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  2117. _scsih_block_io_device(ioc, handle);
  2118. }
  2119. }
  2120. /**
  2121. * _scsih_check_topo_delete_events - sanity check on topo events
  2122. * @ioc: per adapter object
  2123. * @event_data: the event data payload
  2124. *
  2125. * This routine added to better handle cable breaker.
  2126. *
  2127. * This handles the case where driver recieves multiple expander
  2128. * add and delete events in a single shot. When there is a delete event
  2129. * the routine will void any pending add events waiting in the event queue.
  2130. *
  2131. * Return nothing.
  2132. */
  2133. static void
  2134. _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
  2135. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2136. {
  2137. struct fw_event_work *fw_event;
  2138. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  2139. u16 expander_handle;
  2140. struct _sas_node *sas_expander;
  2141. unsigned long flags;
  2142. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  2143. if (expander_handle < ioc->sas_hba.num_phys) {
  2144. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2145. return;
  2146. }
  2147. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
  2148. || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
  2149. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2150. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  2151. expander_handle);
  2152. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2153. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  2154. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  2155. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2156. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  2157. return;
  2158. /* mark ignore flag for pending events */
  2159. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2160. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  2161. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  2162. fw_event->ignore)
  2163. continue;
  2164. local_event_data = fw_event->event_data;
  2165. if (local_event_data->ExpStatus ==
  2166. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  2167. local_event_data->ExpStatus ==
  2168. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  2169. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  2170. expander_handle) {
  2171. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  2172. "setting ignoring flag\n", ioc->name));
  2173. fw_event->ignore = 1;
  2174. }
  2175. }
  2176. }
  2177. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2178. }
  2179. /**
  2180. * _scsih_flush_running_cmds - completing outstanding commands.
  2181. * @ioc: per adapter object
  2182. *
  2183. * The flushing out of all pending scmd commands following host reset,
  2184. * where all IO is dropped to the floor.
  2185. *
  2186. * Return nothing.
  2187. */
  2188. static void
  2189. _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
  2190. {
  2191. struct scsi_cmnd *scmd;
  2192. u16 smid;
  2193. u16 count = 0;
  2194. for (smid = 1; smid <= ioc->request_depth; smid++) {
  2195. scmd = _scsih_scsi_lookup_getclear(ioc, smid);
  2196. if (!scmd)
  2197. continue;
  2198. count++;
  2199. mpt2sas_base_free_smid(ioc, smid);
  2200. scsi_dma_unmap(scmd);
  2201. scmd->result = DID_RESET << 16;
  2202. scmd->scsi_done(scmd);
  2203. }
  2204. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
  2205. ioc->name, count));
  2206. }
  2207. /**
  2208. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  2209. * @scmd: pointer to scsi command object
  2210. * @mpi_request: pointer to the SCSI_IO reqest message frame
  2211. *
  2212. * Supporting protection 1 and 3.
  2213. *
  2214. * Returns nothing
  2215. */
  2216. static void
  2217. _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
  2218. {
  2219. u16 eedp_flags;
  2220. unsigned char prot_op = scsi_get_prot_op(scmd);
  2221. unsigned char prot_type = scsi_get_prot_type(scmd);
  2222. if (prot_type == SCSI_PROT_DIF_TYPE0 ||
  2223. prot_type == SCSI_PROT_DIF_TYPE2 ||
  2224. prot_op == SCSI_PROT_NORMAL)
  2225. return;
  2226. if (prot_op == SCSI_PROT_READ_STRIP)
  2227. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  2228. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  2229. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  2230. else
  2231. return;
  2232. mpi_request->EEDPBlockSize = scmd->device->sector_size;
  2233. switch (prot_type) {
  2234. case SCSI_PROT_DIF_TYPE1:
  2235. /*
  2236. * enable ref/guard checking
  2237. * auto increment ref tag
  2238. */
  2239. mpi_request->EEDPFlags = eedp_flags |
  2240. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2241. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2242. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2243. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  2244. cpu_to_be32(scsi_get_lba(scmd));
  2245. break;
  2246. case SCSI_PROT_DIF_TYPE3:
  2247. /*
  2248. * enable guard checking
  2249. */
  2250. mpi_request->EEDPFlags = eedp_flags |
  2251. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2252. break;
  2253. }
  2254. }
  2255. /**
  2256. * _scsih_eedp_error_handling - return sense code for EEDP errors
  2257. * @scmd: pointer to scsi command object
  2258. * @ioc_status: ioc status
  2259. *
  2260. * Returns nothing
  2261. */
  2262. static void
  2263. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  2264. {
  2265. u8 ascq;
  2266. u8 sk;
  2267. u8 host_byte;
  2268. switch (ioc_status) {
  2269. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2270. ascq = 0x01;
  2271. break;
  2272. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2273. ascq = 0x02;
  2274. break;
  2275. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2276. ascq = 0x03;
  2277. break;
  2278. default:
  2279. ascq = 0x00;
  2280. break;
  2281. }
  2282. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2283. sk = ILLEGAL_REQUEST;
  2284. host_byte = DID_ABORT;
  2285. } else {
  2286. sk = ABORTED_COMMAND;
  2287. host_byte = DID_OK;
  2288. }
  2289. scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
  2290. scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
  2291. SAM_STAT_CHECK_CONDITION;
  2292. }
  2293. /**
  2294. * _scsih_qcmd - main scsi request entry point
  2295. * @scmd: pointer to scsi command object
  2296. * @done: function pointer to be invoked on completion
  2297. *
  2298. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  2299. *
  2300. * Returns 0 on success. If there's a failure, return either:
  2301. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  2302. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  2303. */
  2304. static int
  2305. _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
  2306. {
  2307. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2308. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2309. struct MPT2SAS_TARGET *sas_target_priv_data;
  2310. Mpi2SCSIIORequest_t *mpi_request;
  2311. u32 mpi_control;
  2312. u16 smid;
  2313. scmd->scsi_done = done;
  2314. sas_device_priv_data = scmd->device->hostdata;
  2315. if (!sas_device_priv_data) {
  2316. scmd->result = DID_NO_CONNECT << 16;
  2317. scmd->scsi_done(scmd);
  2318. return 0;
  2319. }
  2320. sas_target_priv_data = sas_device_priv_data->sas_target;
  2321. if (!sas_target_priv_data || sas_target_priv_data->handle ==
  2322. MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
  2323. scmd->result = DID_NO_CONNECT << 16;
  2324. scmd->scsi_done(scmd);
  2325. return 0;
  2326. }
  2327. /* see if we are busy with task managment stuff */
  2328. if (sas_target_priv_data->tm_busy)
  2329. return SCSI_MLQUEUE_DEVICE_BUSY;
  2330. else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  2331. return SCSI_MLQUEUE_HOST_BUSY;
  2332. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  2333. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  2334. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2335. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  2336. else
  2337. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  2338. /* set tags */
  2339. if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
  2340. if (scmd->device->tagged_supported) {
  2341. if (scmd->device->ordered_tags)
  2342. mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
  2343. else
  2344. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2345. } else
  2346. /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
  2347. /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
  2348. */
  2349. mpi_control |= (0x500);
  2350. } else
  2351. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2352. if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
  2353. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  2354. smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
  2355. if (!smid) {
  2356. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2357. ioc->name, __func__);
  2358. goto out;
  2359. }
  2360. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2361. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  2362. _scsih_setup_eedp(scmd, mpi_request);
  2363. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2364. if (sas_device_priv_data->sas_target->flags &
  2365. MPT_TARGET_FLAGS_RAID_COMPONENT)
  2366. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  2367. else
  2368. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2369. mpi_request->DevHandle =
  2370. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  2371. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  2372. mpi_request->Control = cpu_to_le32(mpi_control);
  2373. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  2374. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  2375. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  2376. mpi_request->SenseBufferLowAddress =
  2377. (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  2378. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  2379. mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
  2380. MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
  2381. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  2382. mpi_request->LUN);
  2383. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  2384. if (!mpi_request->DataLength) {
  2385. mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
  2386. } else {
  2387. if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
  2388. mpt2sas_base_free_smid(ioc, smid);
  2389. goto out;
  2390. }
  2391. }
  2392. _scsih_scsi_lookup_set(ioc, smid, scmd);
  2393. mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
  2394. sas_device_priv_data->sas_target->handle);
  2395. return 0;
  2396. out:
  2397. return SCSI_MLQUEUE_HOST_BUSY;
  2398. }
  2399. /**
  2400. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  2401. * @sense_buffer: sense data returned by target
  2402. * @data: normalized skey/asc/ascq
  2403. *
  2404. * Return nothing.
  2405. */
  2406. static void
  2407. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  2408. {
  2409. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  2410. /* descriptor format */
  2411. data->skey = sense_buffer[1] & 0x0F;
  2412. data->asc = sense_buffer[2];
  2413. data->ascq = sense_buffer[3];
  2414. } else {
  2415. /* fixed format */
  2416. data->skey = sense_buffer[2] & 0x0F;
  2417. data->asc = sense_buffer[12];
  2418. data->ascq = sense_buffer[13];
  2419. }
  2420. }
  2421. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2422. /**
  2423. * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
  2424. * @ioc: per adapter object
  2425. * @scmd: pointer to scsi command object
  2426. * @mpi_reply: reply mf payload returned from firmware
  2427. *
  2428. * scsi_status - SCSI Status code returned from target device
  2429. * scsi_state - state info associated with SCSI_IO determined by ioc
  2430. * ioc_status - ioc supplied status info
  2431. *
  2432. * Return nothing.
  2433. */
  2434. static void
  2435. _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  2436. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  2437. {
  2438. u32 response_info;
  2439. u8 *response_bytes;
  2440. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  2441. MPI2_IOCSTATUS_MASK;
  2442. u8 scsi_state = mpi_reply->SCSIState;
  2443. u8 scsi_status = mpi_reply->SCSIStatus;
  2444. char *desc_ioc_state = NULL;
  2445. char *desc_scsi_status = NULL;
  2446. char *desc_scsi_state = ioc->tmp_string;
  2447. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  2448. if (log_info == 0x31170000)
  2449. return;
  2450. switch (ioc_status) {
  2451. case MPI2_IOCSTATUS_SUCCESS:
  2452. desc_ioc_state = "success";
  2453. break;
  2454. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2455. desc_ioc_state = "invalid function";
  2456. break;
  2457. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2458. desc_ioc_state = "scsi recovered error";
  2459. break;
  2460. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  2461. desc_ioc_state = "scsi invalid dev handle";
  2462. break;
  2463. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2464. desc_ioc_state = "scsi device not there";
  2465. break;
  2466. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2467. desc_ioc_state = "scsi data overrun";
  2468. break;
  2469. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2470. desc_ioc_state = "scsi data underrun";
  2471. break;
  2472. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2473. desc_ioc_state = "scsi io data error";
  2474. break;
  2475. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2476. desc_ioc_state = "scsi protocol error";
  2477. break;
  2478. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2479. desc_ioc_state = "scsi task terminated";
  2480. break;
  2481. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2482. desc_ioc_state = "scsi residual mismatch";
  2483. break;
  2484. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2485. desc_ioc_state = "scsi task mgmt failed";
  2486. break;
  2487. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2488. desc_ioc_state = "scsi ioc terminated";
  2489. break;
  2490. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2491. desc_ioc_state = "scsi ext terminated";
  2492. break;
  2493. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2494. desc_ioc_state = "eedp guard error";
  2495. break;
  2496. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2497. desc_ioc_state = "eedp ref tag error";
  2498. break;
  2499. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2500. desc_ioc_state = "eedp app tag error";
  2501. break;
  2502. default:
  2503. desc_ioc_state = "unknown";
  2504. break;
  2505. }
  2506. switch (scsi_status) {
  2507. case MPI2_SCSI_STATUS_GOOD:
  2508. desc_scsi_status = "good";
  2509. break;
  2510. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  2511. desc_scsi_status = "check condition";
  2512. break;
  2513. case MPI2_SCSI_STATUS_CONDITION_MET:
  2514. desc_scsi_status = "condition met";
  2515. break;
  2516. case MPI2_SCSI_STATUS_BUSY:
  2517. desc_scsi_status = "busy";
  2518. break;
  2519. case MPI2_SCSI_STATUS_INTERMEDIATE:
  2520. desc_scsi_status = "intermediate";
  2521. break;
  2522. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  2523. desc_scsi_status = "intermediate condmet";
  2524. break;
  2525. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  2526. desc_scsi_status = "reservation conflict";
  2527. break;
  2528. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  2529. desc_scsi_status = "command terminated";
  2530. break;
  2531. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  2532. desc_scsi_status = "task set full";
  2533. break;
  2534. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  2535. desc_scsi_status = "aca active";
  2536. break;
  2537. case MPI2_SCSI_STATUS_TASK_ABORTED:
  2538. desc_scsi_status = "task aborted";
  2539. break;
  2540. default:
  2541. desc_scsi_status = "unknown";
  2542. break;
  2543. }
  2544. desc_scsi_state[0] = '\0';
  2545. if (!scsi_state)
  2546. desc_scsi_state = " ";
  2547. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  2548. strcat(desc_scsi_state, "response info ");
  2549. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2550. strcat(desc_scsi_state, "state terminated ");
  2551. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  2552. strcat(desc_scsi_state, "no status ");
  2553. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  2554. strcat(desc_scsi_state, "autosense failed ");
  2555. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  2556. strcat(desc_scsi_state, "autosense valid ");
  2557. scsi_print_command(scmd);
  2558. printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
  2559. "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
  2560. le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
  2561. ioc_status, smid);
  2562. printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
  2563. "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
  2564. scsi_get_resid(scmd));
  2565. printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
  2566. "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  2567. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  2568. printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
  2569. "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
  2570. scsi_status, desc_scsi_state, scsi_state);
  2571. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2572. struct sense_info data;
  2573. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2574. printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
  2575. "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
  2576. data.asc, data.ascq);
  2577. }
  2578. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  2579. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  2580. response_bytes = (u8 *)&response_info;
  2581. _scsih_response_code(ioc, response_bytes[3]);
  2582. }
  2583. }
  2584. #endif
  2585. /**
  2586. * _scsih_smart_predicted_fault - illuminate Fault LED
  2587. * @ioc: per adapter object
  2588. * @handle: device handle
  2589. *
  2590. * Return nothing.
  2591. */
  2592. static void
  2593. _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2594. {
  2595. Mpi2SepReply_t mpi_reply;
  2596. Mpi2SepRequest_t mpi_request;
  2597. struct scsi_target *starget;
  2598. struct MPT2SAS_TARGET *sas_target_priv_data;
  2599. Mpi2EventNotificationReply_t *event_reply;
  2600. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  2601. struct _sas_device *sas_device;
  2602. ssize_t sz;
  2603. unsigned long flags;
  2604. /* only handle non-raid devices */
  2605. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2606. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  2607. if (!sas_device) {
  2608. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2609. return;
  2610. }
  2611. starget = sas_device->starget;
  2612. sas_target_priv_data = starget->hostdata;
  2613. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  2614. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
  2615. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2616. return;
  2617. }
  2618. starget_printk(KERN_WARNING, starget, "predicted fault\n");
  2619. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2620. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
  2621. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  2622. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  2623. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  2624. mpi_request.SlotStatus =
  2625. MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
  2626. mpi_request.DevHandle = cpu_to_le16(handle);
  2627. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  2628. if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  2629. &mpi_request)) != 0) {
  2630. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2631. ioc->name, __FILE__, __LINE__, __func__);
  2632. return;
  2633. }
  2634. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  2635. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2636. "enclosure_processor: ioc_status (0x%04x), "
  2637. "loginfo(0x%08x)\n", ioc->name,
  2638. le16_to_cpu(mpi_reply.IOCStatus),
  2639. le32_to_cpu(mpi_reply.IOCLogInfo)));
  2640. return;
  2641. }
  2642. }
  2643. /* insert into event log */
  2644. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  2645. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  2646. event_reply = kzalloc(sz, GFP_KERNEL);
  2647. if (!event_reply) {
  2648. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2649. ioc->name, __FILE__, __LINE__, __func__);
  2650. return;
  2651. }
  2652. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  2653. event_reply->Event =
  2654. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  2655. event_reply->MsgLength = sz/4;
  2656. event_reply->EventDataLength =
  2657. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  2658. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  2659. event_reply->EventData;
  2660. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  2661. event_data->ASC = 0x5D;
  2662. event_data->DevHandle = cpu_to_le16(handle);
  2663. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  2664. mpt2sas_ctl_add_to_event_log(ioc, event_reply);
  2665. kfree(event_reply);
  2666. }
  2667. /**
  2668. * _scsih_io_done - scsi request callback
  2669. * @ioc: per adapter object
  2670. * @smid: system request message index
  2671. * @VF_ID: virtual function id
  2672. * @reply: reply message frame(lower 32bit addr)
  2673. *
  2674. * Callback handler when using scsih_qcmd.
  2675. *
  2676. * Return nothing.
  2677. */
  2678. static void
  2679. _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  2680. {
  2681. Mpi2SCSIIORequest_t *mpi_request;
  2682. Mpi2SCSIIOReply_t *mpi_reply;
  2683. struct scsi_cmnd *scmd;
  2684. u16 ioc_status;
  2685. u32 xfer_cnt;
  2686. u8 scsi_state;
  2687. u8 scsi_status;
  2688. u32 log_info;
  2689. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2690. u32 response_code;
  2691. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2692. scmd = _scsih_scsi_lookup_getclear(ioc, smid);
  2693. if (scmd == NULL)
  2694. return;
  2695. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2696. if (mpi_reply == NULL) {
  2697. scmd->result = DID_OK << 16;
  2698. goto out;
  2699. }
  2700. sas_device_priv_data = scmd->device->hostdata;
  2701. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  2702. sas_device_priv_data->sas_target->deleted) {
  2703. scmd->result = DID_NO_CONNECT << 16;
  2704. goto out;
  2705. }
  2706. /* turning off TLR */
  2707. if (!sas_device_priv_data->tlr_snoop_check) {
  2708. sas_device_priv_data->tlr_snoop_check++;
  2709. if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
  2710. response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
  2711. >> 24);
  2712. if (response_code ==
  2713. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
  2714. sas_device_priv_data->flags &=
  2715. ~MPT_DEVICE_TLR_ON;
  2716. }
  2717. }
  2718. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  2719. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  2720. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  2721. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  2722. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  2723. else
  2724. log_info = 0;
  2725. ioc_status &= MPI2_IOCSTATUS_MASK;
  2726. scsi_state = mpi_reply->SCSIState;
  2727. scsi_status = mpi_reply->SCSIStatus;
  2728. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  2729. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  2730. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  2731. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  2732. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  2733. }
  2734. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2735. struct sense_info data;
  2736. const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
  2737. smid);
  2738. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  2739. le32_to_cpu(mpi_reply->SenseCount));
  2740. memcpy(scmd->sense_buffer, sense_data, sz);
  2741. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2742. /* failure prediction threshold exceeded */
  2743. if (data.asc == 0x5D)
  2744. _scsih_smart_predicted_fault(ioc,
  2745. le16_to_cpu(mpi_reply->DevHandle));
  2746. }
  2747. switch (ioc_status) {
  2748. case MPI2_IOCSTATUS_BUSY:
  2749. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  2750. scmd->result = SAM_STAT_BUSY;
  2751. break;
  2752. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2753. scmd->result = DID_NO_CONNECT << 16;
  2754. break;
  2755. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2756. if (sas_device_priv_data->block) {
  2757. scmd->result = (DID_BUS_BUSY << 16);
  2758. break;
  2759. }
  2760. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2761. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2762. scmd->result = DID_RESET << 16;
  2763. break;
  2764. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2765. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  2766. scmd->result = DID_SOFT_ERROR << 16;
  2767. else
  2768. scmd->result = (DID_OK << 16) | scsi_status;
  2769. break;
  2770. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2771. scmd->result = (DID_OK << 16) | scsi_status;
  2772. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  2773. break;
  2774. if (xfer_cnt < scmd->underflow) {
  2775. if (scsi_status == SAM_STAT_BUSY)
  2776. scmd->result = SAM_STAT_BUSY;
  2777. else
  2778. scmd->result = DID_SOFT_ERROR << 16;
  2779. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  2780. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  2781. scmd->result = DID_SOFT_ERROR << 16;
  2782. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2783. scmd->result = DID_RESET << 16;
  2784. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  2785. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  2786. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  2787. scmd->result = (DRIVER_SENSE << 24) |
  2788. SAM_STAT_CHECK_CONDITION;
  2789. scmd->sense_buffer[0] = 0x70;
  2790. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  2791. scmd->sense_buffer[12] = 0x20;
  2792. scmd->sense_buffer[13] = 0;
  2793. }
  2794. break;
  2795. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2796. scsi_set_resid(scmd, 0);
  2797. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2798. case MPI2_IOCSTATUS_SUCCESS:
  2799. scmd->result = (DID_OK << 16) | scsi_status;
  2800. if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  2801. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  2802. scmd->result = DID_SOFT_ERROR << 16;
  2803. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2804. scmd->result = DID_RESET << 16;
  2805. break;
  2806. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2807. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2808. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2809. _scsih_eedp_error_handling(scmd, ioc_status);
  2810. break;
  2811. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2812. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2813. case MPI2_IOCSTATUS_INVALID_SGL:
  2814. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  2815. case MPI2_IOCSTATUS_INVALID_FIELD:
  2816. case MPI2_IOCSTATUS_INVALID_STATE:
  2817. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2818. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2819. default:
  2820. scmd->result = DID_SOFT_ERROR << 16;
  2821. break;
  2822. }
  2823. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2824. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  2825. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  2826. #endif
  2827. out:
  2828. scsi_dma_unmap(scmd);
  2829. scmd->scsi_done(scmd);
  2830. }
  2831. /**
  2832. * _scsih_sas_host_refresh - refreshing sas host object contents
  2833. * @ioc: per adapter object
  2834. * @update: update link information
  2835. * Context: user
  2836. *
  2837. * During port enable, fw will send topology events for every device. Its
  2838. * possible that the handles may change from the previous setting, so this
  2839. * code keeping handles updating if changed.
  2840. *
  2841. * Return nothing.
  2842. */
  2843. static void
  2844. _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
  2845. {
  2846. u16 sz;
  2847. u16 ioc_status;
  2848. int i;
  2849. Mpi2ConfigReply_t mpi_reply;
  2850. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  2851. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  2852. "updating handles for sas_host(0x%016llx)\n",
  2853. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  2854. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  2855. * sizeof(Mpi2SasIOUnit0PhyData_t));
  2856. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  2857. if (!sas_iounit_pg0) {
  2858. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2859. ioc->name, __FILE__, __LINE__, __func__);
  2860. return;
  2861. }
  2862. if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  2863. sas_iounit_pg0, sz))) {
  2864. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2865. MPI2_IOCSTATUS_MASK;
  2866. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  2867. goto out;
  2868. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  2869. ioc->sas_hba.phy[i].handle =
  2870. le16_to_cpu(sas_iounit_pg0->PhyData[i].
  2871. ControllerDevHandle);
  2872. if (update)
  2873. mpt2sas_transport_update_links(
  2874. ioc,
  2875. ioc->sas_hba.phy[i].handle,
  2876. le16_to_cpu(sas_iounit_pg0->PhyData[i].
  2877. AttachedDevHandle), i,
  2878. sas_iounit_pg0->PhyData[i].
  2879. NegotiatedLinkRate >> 4);
  2880. }
  2881. }
  2882. out:
  2883. kfree(sas_iounit_pg0);
  2884. }
  2885. /**
  2886. * _scsih_sas_host_add - create sas host object
  2887. * @ioc: per adapter object
  2888. *
  2889. * Creating host side data object, stored in ioc->sas_hba
  2890. *
  2891. * Return nothing.
  2892. */
  2893. static void
  2894. _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
  2895. {
  2896. int i;
  2897. Mpi2ConfigReply_t mpi_reply;
  2898. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  2899. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  2900. Mpi2SasPhyPage0_t phy_pg0;
  2901. Mpi2SasDevicePage0_t sas_device_pg0;
  2902. Mpi2SasEnclosurePage0_t enclosure_pg0;
  2903. u16 ioc_status;
  2904. u16 sz;
  2905. u16 device_missing_delay;
  2906. mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
  2907. if (!ioc->sas_hba.num_phys) {
  2908. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2909. ioc->name, __FILE__, __LINE__, __func__);
  2910. return;
  2911. }
  2912. /* sas_iounit page 0 */
  2913. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  2914. sizeof(Mpi2SasIOUnit0PhyData_t));
  2915. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  2916. if (!sas_iounit_pg0) {
  2917. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2918. ioc->name, __FILE__, __LINE__, __func__);
  2919. return;
  2920. }
  2921. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  2922. sas_iounit_pg0, sz))) {
  2923. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2924. ioc->name, __FILE__, __LINE__, __func__);
  2925. goto out;
  2926. }
  2927. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2928. MPI2_IOCSTATUS_MASK;
  2929. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2930. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2931. ioc->name, __FILE__, __LINE__, __func__);
  2932. goto out;
  2933. }
  2934. /* sas_iounit page 1 */
  2935. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  2936. sizeof(Mpi2SasIOUnit1PhyData_t));
  2937. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  2938. if (!sas_iounit_pg1) {
  2939. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2940. ioc->name, __FILE__, __LINE__, __func__);
  2941. goto out;
  2942. }
  2943. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  2944. sas_iounit_pg1, sz))) {
  2945. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2946. ioc->name, __FILE__, __LINE__, __func__);
  2947. goto out;
  2948. }
  2949. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2950. MPI2_IOCSTATUS_MASK;
  2951. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2952. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2953. ioc->name, __FILE__, __LINE__, __func__);
  2954. goto out;
  2955. }
  2956. ioc->io_missing_delay =
  2957. le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
  2958. device_missing_delay =
  2959. le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
  2960. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2961. ioc->device_missing_delay = (device_missing_delay &
  2962. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2963. else
  2964. ioc->device_missing_delay = device_missing_delay &
  2965. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2966. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  2967. ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
  2968. sizeof(struct _sas_phy), GFP_KERNEL);
  2969. if (!ioc->sas_hba.phy) {
  2970. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2971. ioc->name, __FILE__, __LINE__, __func__);
  2972. goto out;
  2973. }
  2974. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  2975. if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  2976. i))) {
  2977. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2978. ioc->name, __FILE__, __LINE__, __func__);
  2979. goto out;
  2980. }
  2981. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2982. MPI2_IOCSTATUS_MASK;
  2983. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2984. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2985. ioc->name, __FILE__, __LINE__, __func__);
  2986. goto out;
  2987. }
  2988. ioc->sas_hba.phy[i].handle =
  2989. le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
  2990. ioc->sas_hba.phy[i].phy_id = i;
  2991. mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  2992. phy_pg0, ioc->sas_hba.parent_dev);
  2993. }
  2994. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  2995. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
  2996. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2997. ioc->name, __FILE__, __LINE__, __func__);
  2998. goto out;
  2999. }
  3000. ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
  3001. ioc->sas_hba.enclosure_handle =
  3002. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3003. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3004. printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
  3005. "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
  3006. (unsigned long long) ioc->sas_hba.sas_address,
  3007. ioc->sas_hba.num_phys) ;
  3008. if (ioc->sas_hba.enclosure_handle) {
  3009. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3010. &enclosure_pg0,
  3011. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3012. ioc->sas_hba.enclosure_handle))) {
  3013. ioc->sas_hba.enclosure_logical_id =
  3014. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3015. }
  3016. }
  3017. out:
  3018. kfree(sas_iounit_pg1);
  3019. kfree(sas_iounit_pg0);
  3020. }
  3021. /**
  3022. * _scsih_expander_add - creating expander object
  3023. * @ioc: per adapter object
  3024. * @handle: expander handle
  3025. *
  3026. * Creating expander object, stored in ioc->sas_expander_list.
  3027. *
  3028. * Return 0 for success, else error.
  3029. */
  3030. static int
  3031. _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3032. {
  3033. struct _sas_node *sas_expander;
  3034. Mpi2ConfigReply_t mpi_reply;
  3035. Mpi2ExpanderPage0_t expander_pg0;
  3036. Mpi2ExpanderPage1_t expander_pg1;
  3037. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3038. u32 ioc_status;
  3039. u16 parent_handle;
  3040. __le64 sas_address;
  3041. int i;
  3042. unsigned long flags;
  3043. struct _sas_port *mpt2sas_port = NULL;
  3044. int rc = 0;
  3045. if (!handle)
  3046. return -1;
  3047. if (ioc->shost_recovery)
  3048. return -1;
  3049. if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  3050. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  3051. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3052. ioc->name, __FILE__, __LINE__, __func__);
  3053. return -1;
  3054. }
  3055. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3056. MPI2_IOCSTATUS_MASK;
  3057. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3058. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3059. ioc->name, __FILE__, __LINE__, __func__);
  3060. return -1;
  3061. }
  3062. /* handle out of order topology events */
  3063. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  3064. if (parent_handle >= ioc->sas_hba.num_phys) {
  3065. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3066. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  3067. parent_handle);
  3068. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3069. if (!sas_expander) {
  3070. rc = _scsih_expander_add(ioc, parent_handle);
  3071. if (rc != 0)
  3072. return rc;
  3073. }
  3074. }
  3075. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  3076. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3077. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3078. sas_address);
  3079. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3080. if (sas_expander)
  3081. return 0;
  3082. sas_expander = kzalloc(sizeof(struct _sas_node),
  3083. GFP_KERNEL);
  3084. if (!sas_expander) {
  3085. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3086. ioc->name, __FILE__, __LINE__, __func__);
  3087. return -1;
  3088. }
  3089. sas_expander->handle = handle;
  3090. sas_expander->num_phys = expander_pg0.NumPhys;
  3091. sas_expander->parent_handle = parent_handle;
  3092. sas_expander->enclosure_handle =
  3093. le16_to_cpu(expander_pg0.EnclosureHandle);
  3094. sas_expander->sas_address = sas_address;
  3095. printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
  3096. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  3097. handle, sas_expander->parent_handle, (unsigned long long)
  3098. sas_expander->sas_address, sas_expander->num_phys);
  3099. if (!sas_expander->num_phys)
  3100. goto out_fail;
  3101. sas_expander->phy = kcalloc(sas_expander->num_phys,
  3102. sizeof(struct _sas_phy), GFP_KERNEL);
  3103. if (!sas_expander->phy) {
  3104. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3105. ioc->name, __FILE__, __LINE__, __func__);
  3106. rc = -1;
  3107. goto out_fail;
  3108. }
  3109. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  3110. mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
  3111. sas_expander->parent_handle);
  3112. if (!mpt2sas_port) {
  3113. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3114. ioc->name, __FILE__, __LINE__, __func__);
  3115. rc = -1;
  3116. goto out_fail;
  3117. }
  3118. sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
  3119. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  3120. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  3121. &expander_pg1, i, handle))) {
  3122. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3123. ioc->name, __FILE__, __LINE__, __func__);
  3124. rc = -1;
  3125. goto out_fail;
  3126. }
  3127. sas_expander->phy[i].handle = handle;
  3128. sas_expander->phy[i].phy_id = i;
  3129. if ((mpt2sas_transport_add_expander_phy(ioc,
  3130. &sas_expander->phy[i], expander_pg1,
  3131. sas_expander->parent_dev))) {
  3132. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3133. ioc->name, __FILE__, __LINE__, __func__);
  3134. rc = -1;
  3135. goto out_fail;
  3136. }
  3137. }
  3138. if (sas_expander->enclosure_handle) {
  3139. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3140. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3141. sas_expander->enclosure_handle))) {
  3142. sas_expander->enclosure_logical_id =
  3143. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3144. }
  3145. }
  3146. _scsih_expander_node_add(ioc, sas_expander);
  3147. return 0;
  3148. out_fail:
  3149. if (mpt2sas_port)
  3150. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  3151. sas_expander->parent_handle);
  3152. kfree(sas_expander);
  3153. return rc;
  3154. }
  3155. /**
  3156. * _scsih_expander_remove - removing expander object
  3157. * @ioc: per adapter object
  3158. * @handle: expander handle
  3159. *
  3160. * Return nothing.
  3161. */
  3162. static void
  3163. _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3164. {
  3165. struct _sas_node *sas_expander;
  3166. unsigned long flags;
  3167. if (ioc->shost_recovery)
  3168. return;
  3169. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3170. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
  3171. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3172. _scsih_expander_node_remove(ioc, sas_expander);
  3173. }
  3174. /**
  3175. * _scsih_add_device - creating sas device object
  3176. * @ioc: per adapter object
  3177. * @handle: sas device handle
  3178. * @phy_num: phy number end device attached to
  3179. * @is_pd: is this hidden raid component
  3180. *
  3181. * Creating end device object, stored in ioc->sas_device_list.
  3182. *
  3183. * Returns 0 for success, non-zero for failure.
  3184. */
  3185. static int
  3186. _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
  3187. {
  3188. Mpi2ConfigReply_t mpi_reply;
  3189. Mpi2SasDevicePage0_t sas_device_pg0;
  3190. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3191. struct _sas_device *sas_device;
  3192. u32 ioc_status;
  3193. __le64 sas_address;
  3194. u32 device_info;
  3195. unsigned long flags;
  3196. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3197. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  3198. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3199. ioc->name, __FILE__, __LINE__, __func__);
  3200. return -1;
  3201. }
  3202. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3203. MPI2_IOCSTATUS_MASK;
  3204. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3205. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3206. ioc->name, __FILE__, __LINE__, __func__);
  3207. return -1;
  3208. }
  3209. /* check if device is present */
  3210. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  3211. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  3212. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3213. ioc->name, __FILE__, __LINE__, __func__);
  3214. printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
  3215. ioc->name, le16_to_cpu(sas_device_pg0.Flags));
  3216. return -1;
  3217. }
  3218. /* check if there were any issus with discovery */
  3219. if (sas_device_pg0.AccessStatus ==
  3220. MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
  3221. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3222. ioc->name, __FILE__, __LINE__, __func__);
  3223. printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
  3224. ioc->name, sas_device_pg0.AccessStatus);
  3225. return -1;
  3226. }
  3227. /* check if this is end device */
  3228. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  3229. if (!(_scsih_is_end_device(device_info))) {
  3230. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3231. ioc->name, __FILE__, __LINE__, __func__);
  3232. return -1;
  3233. }
  3234. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3235. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3236. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3237. sas_address);
  3238. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3239. if (sas_device) {
  3240. _scsih_ublock_io_device(ioc, handle);
  3241. return 0;
  3242. }
  3243. sas_device = kzalloc(sizeof(struct _sas_device),
  3244. GFP_KERNEL);
  3245. if (!sas_device) {
  3246. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3247. ioc->name, __FILE__, __LINE__, __func__);
  3248. return -1;
  3249. }
  3250. sas_device->handle = handle;
  3251. sas_device->parent_handle =
  3252. le16_to_cpu(sas_device_pg0.ParentDevHandle);
  3253. sas_device->enclosure_handle =
  3254. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3255. sas_device->slot =
  3256. le16_to_cpu(sas_device_pg0.Slot);
  3257. sas_device->device_info = device_info;
  3258. sas_device->sas_address = sas_address;
  3259. sas_device->hidden_raid_component = is_pd;
  3260. /* get enclosure_logical_id */
  3261. if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
  3262. ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3263. sas_device->enclosure_handle)))
  3264. sas_device->enclosure_logical_id =
  3265. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3266. /* get device name */
  3267. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  3268. if (ioc->wait_for_port_enable_to_complete)
  3269. _scsih_sas_device_init_add(ioc, sas_device);
  3270. else
  3271. _scsih_sas_device_add(ioc, sas_device);
  3272. return 0;
  3273. }
  3274. /**
  3275. * _scsih_remove_device - removing sas device object
  3276. * @ioc: per adapter object
  3277. * @handle: sas device handle
  3278. *
  3279. * Return nothing.
  3280. */
  3281. static void
  3282. _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3283. {
  3284. struct MPT2SAS_TARGET *sas_target_priv_data;
  3285. struct _sas_device *sas_device;
  3286. unsigned long flags;
  3287. Mpi2SasIoUnitControlReply_t mpi_reply;
  3288. Mpi2SasIoUnitControlRequest_t mpi_request;
  3289. u16 device_handle;
  3290. /* lookup sas_device */
  3291. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3292. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3293. if (!sas_device) {
  3294. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3295. return;
  3296. }
  3297. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
  3298. "(0x%04x)\n", ioc->name, __func__, handle));
  3299. if (sas_device->starget && sas_device->starget->hostdata) {
  3300. sas_target_priv_data = sas_device->starget->hostdata;
  3301. sas_target_priv_data->deleted = 1;
  3302. }
  3303. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3304. if (ioc->remove_host)
  3305. goto out;
  3306. /* Target Reset to flush out all the outstanding IO */
  3307. device_handle = (sas_device->hidden_raid_component) ?
  3308. sas_device->volume_handle : handle;
  3309. if (device_handle) {
  3310. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
  3311. "handle(0x%04x)\n", ioc->name, device_handle));
  3312. mutex_lock(&ioc->tm_cmds.mutex);
  3313. mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
  3314. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
  3315. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3316. mutex_unlock(&ioc->tm_cmds.mutex);
  3317. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
  3318. "done: handle(0x%04x)\n", ioc->name, device_handle));
  3319. if (ioc->shost_recovery)
  3320. goto out;
  3321. }
  3322. /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
  3323. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
  3324. "(0x%04x)\n", ioc->name, handle));
  3325. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3326. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3327. mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3328. mpi_request.DevHandle = handle;
  3329. mpi_request.VF_ID = 0;
  3330. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
  3331. &mpi_request)) != 0) {
  3332. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3333. ioc->name, __FILE__, __LINE__, __func__);
  3334. }
  3335. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
  3336. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  3337. le16_to_cpu(mpi_reply.IOCStatus),
  3338. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3339. out:
  3340. mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
  3341. sas_device->parent_handle);
  3342. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
  3343. "(0x%016llx)\n", ioc->name, sas_device->handle,
  3344. (unsigned long long) sas_device->sas_address);
  3345. _scsih_sas_device_remove(ioc, sas_device);
  3346. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
  3347. "(0x%04x)\n", ioc->name, __func__, handle));
  3348. }
  3349. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3350. /**
  3351. * _scsih_sas_topology_change_event_debug - debug for topology event
  3352. * @ioc: per adapter object
  3353. * @event_data: event data payload
  3354. * Context: user.
  3355. */
  3356. static void
  3357. _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3358. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3359. {
  3360. int i;
  3361. u16 handle;
  3362. u16 reason_code;
  3363. u8 phy_number;
  3364. char *status_str = NULL;
  3365. char link_rate[25];
  3366. switch (event_data->ExpStatus) {
  3367. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  3368. status_str = "add";
  3369. break;
  3370. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  3371. status_str = "remove";
  3372. break;
  3373. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  3374. status_str = "responding";
  3375. break;
  3376. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  3377. status_str = "remove delay";
  3378. break;
  3379. default:
  3380. status_str = "unknown status";
  3381. break;
  3382. }
  3383. printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
  3384. ioc->name, status_str);
  3385. printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
  3386. "start_phy(%02d), count(%d)\n",
  3387. le16_to_cpu(event_data->ExpanderDevHandle),
  3388. le16_to_cpu(event_data->EnclosureHandle),
  3389. event_data->StartPhyNum, event_data->NumEntries);
  3390. for (i = 0; i < event_data->NumEntries; i++) {
  3391. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3392. if (!handle)
  3393. continue;
  3394. phy_number = event_data->StartPhyNum + i;
  3395. reason_code = event_data->PHY[i].PhyStatus &
  3396. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3397. switch (reason_code) {
  3398. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3399. snprintf(link_rate, 25, ": add, link(0x%02x)",
  3400. (event_data->PHY[i].LinkRate >> 4));
  3401. status_str = link_rate;
  3402. break;
  3403. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3404. status_str = ": remove";
  3405. break;
  3406. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  3407. status_str = ": remove_delay";
  3408. break;
  3409. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3410. snprintf(link_rate, 25, ": link(0x%02x)",
  3411. (event_data->PHY[i].LinkRate >> 4));
  3412. status_str = link_rate;
  3413. break;
  3414. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  3415. status_str = ": responding";
  3416. break;
  3417. default:
  3418. status_str = ": unknown";
  3419. break;
  3420. }
  3421. printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
  3422. phy_number, handle, status_str);
  3423. }
  3424. }
  3425. #endif
  3426. /**
  3427. * _scsih_sas_topology_change_event - handle topology changes
  3428. * @ioc: per adapter object
  3429. * @VF_ID:
  3430. * @event_data: event data payload
  3431. * fw_event:
  3432. * Context: user.
  3433. *
  3434. */
  3435. static void
  3436. _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3437. Mpi2EventDataSasTopologyChangeList_t *event_data,
  3438. struct fw_event_work *fw_event)
  3439. {
  3440. int i;
  3441. u16 parent_handle, handle;
  3442. u16 reason_code;
  3443. u8 phy_number;
  3444. struct _sas_node *sas_expander;
  3445. unsigned long flags;
  3446. u8 link_rate_;
  3447. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3448. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3449. _scsih_sas_topology_change_event_debug(ioc, event_data);
  3450. #endif
  3451. if (!ioc->sas_hba.num_phys)
  3452. _scsih_sas_host_add(ioc);
  3453. else
  3454. _scsih_sas_host_refresh(ioc, 0);
  3455. if (fw_event->ignore) {
  3456. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
  3457. "event\n", ioc->name));
  3458. return;
  3459. }
  3460. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3461. /* handle expander add */
  3462. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  3463. if (_scsih_expander_add(ioc, parent_handle) != 0)
  3464. return;
  3465. /* handle siblings events */
  3466. for (i = 0; i < event_data->NumEntries; i++) {
  3467. if (fw_event->ignore) {
  3468. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
  3469. "expander event\n", ioc->name));
  3470. return;
  3471. }
  3472. if (ioc->shost_recovery)
  3473. return;
  3474. if (event_data->PHY[i].PhyStatus &
  3475. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
  3476. continue;
  3477. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3478. if (!handle)
  3479. continue;
  3480. phy_number = event_data->StartPhyNum + i;
  3481. reason_code = event_data->PHY[i].PhyStatus &
  3482. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3483. link_rate_ = event_data->PHY[i].LinkRate >> 4;
  3484. switch (reason_code) {
  3485. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3486. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3487. if (!parent_handle) {
  3488. if (phy_number < ioc->sas_hba.num_phys)
  3489. mpt2sas_transport_update_links(
  3490. ioc,
  3491. ioc->sas_hba.phy[phy_number].handle,
  3492. handle, phy_number, link_rate_);
  3493. } else {
  3494. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3495. sas_expander =
  3496. mpt2sas_scsih_expander_find_by_handle(ioc,
  3497. parent_handle);
  3498. spin_unlock_irqrestore(&ioc->sas_node_lock,
  3499. flags);
  3500. if (sas_expander) {
  3501. if (phy_number < sas_expander->num_phys)
  3502. mpt2sas_transport_update_links(
  3503. ioc,
  3504. sas_expander->
  3505. phy[phy_number].handle,
  3506. handle, phy_number,
  3507. link_rate_);
  3508. }
  3509. }
  3510. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
  3511. if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
  3512. _scsih_ublock_io_device(ioc, handle);
  3513. }
  3514. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
  3515. if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
  3516. break;
  3517. _scsih_add_device(ioc, handle, phy_number, 0);
  3518. }
  3519. break;
  3520. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3521. _scsih_remove_device(ioc, handle);
  3522. break;
  3523. }
  3524. }
  3525. /* handle expander removal */
  3526. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3527. _scsih_expander_remove(ioc, parent_handle);
  3528. }
  3529. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3530. /**
  3531. * _scsih_sas_device_status_change_event_debug - debug for device event
  3532. * @event_data: event data payload
  3533. * Context: user.
  3534. *
  3535. * Return nothing.
  3536. */
  3537. static void
  3538. _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3539. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  3540. {
  3541. char *reason_str = NULL;
  3542. switch (event_data->ReasonCode) {
  3543. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  3544. reason_str = "smart data";
  3545. break;
  3546. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  3547. reason_str = "unsupported device discovered";
  3548. break;
  3549. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  3550. reason_str = "internal device reset";
  3551. break;
  3552. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  3553. reason_str = "internal task abort";
  3554. break;
  3555. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  3556. reason_str = "internal task abort set";
  3557. break;
  3558. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  3559. reason_str = "internal clear task set";
  3560. break;
  3561. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  3562. reason_str = "internal query task";
  3563. break;
  3564. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  3565. reason_str = "sata init failure";
  3566. break;
  3567. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  3568. reason_str = "internal device reset complete";
  3569. break;
  3570. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  3571. reason_str = "internal task abort complete";
  3572. break;
  3573. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  3574. reason_str = "internal async notification";
  3575. break;
  3576. default:
  3577. reason_str = "unknown reason";
  3578. break;
  3579. }
  3580. printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
  3581. "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
  3582. reason_str, le16_to_cpu(event_data->DevHandle),
  3583. (unsigned long long)le64_to_cpu(event_data->SASAddress));
  3584. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  3585. printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  3586. event_data->ASC, event_data->ASCQ);
  3587. printk(KERN_INFO "\n");
  3588. }
  3589. #endif
  3590. /**
  3591. * _scsih_sas_device_status_change_event - handle device status change
  3592. * @ioc: per adapter object
  3593. * @VF_ID:
  3594. * @event_data: event data payload
  3595. * Context: user.
  3596. *
  3597. * Return nothing.
  3598. */
  3599. static void
  3600. _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3601. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  3602. {
  3603. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3604. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3605. _scsih_sas_device_status_change_event_debug(ioc, event_data);
  3606. #endif
  3607. }
  3608. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3609. /**
  3610. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
  3611. * @ioc: per adapter object
  3612. * @event_data: event data payload
  3613. * Context: user.
  3614. *
  3615. * Return nothing.
  3616. */
  3617. static void
  3618. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3619. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  3620. {
  3621. char *reason_str = NULL;
  3622. switch (event_data->ReasonCode) {
  3623. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  3624. reason_str = "enclosure add";
  3625. break;
  3626. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  3627. reason_str = "enclosure remove";
  3628. break;
  3629. default:
  3630. reason_str = "unknown reason";
  3631. break;
  3632. }
  3633. printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
  3634. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  3635. " number slots(%d)\n", ioc->name, reason_str,
  3636. le16_to_cpu(event_data->EnclosureHandle),
  3637. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  3638. le16_to_cpu(event_data->StartSlot));
  3639. }
  3640. #endif
  3641. /**
  3642. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  3643. * @ioc: per adapter object
  3644. * @VF_ID:
  3645. * @event_data: event data payload
  3646. * Context: user.
  3647. *
  3648. * Return nothing.
  3649. */
  3650. static void
  3651. _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  3652. u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  3653. {
  3654. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3655. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3656. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  3657. event_data);
  3658. #endif
  3659. }
  3660. /**
  3661. * _scsih_sas_broadcast_primative_event - handle broadcast events
  3662. * @ioc: per adapter object
  3663. * @event_data: event data payload
  3664. * Context: user.
  3665. *
  3666. * Return nothing.
  3667. */
  3668. static void
  3669. _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3670. Mpi2EventDataSasBroadcastPrimitive_t *event_data)
  3671. {
  3672. struct scsi_cmnd *scmd;
  3673. u16 smid, handle;
  3674. u32 lun;
  3675. struct MPT2SAS_DEVICE *sas_device_priv_data;
  3676. u32 termination_count;
  3677. u32 query_count;
  3678. Mpi2SCSITaskManagementReply_t *mpi_reply;
  3679. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
  3680. "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
  3681. event_data->PortWidth));
  3682. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
  3683. __func__));
  3684. mutex_lock(&ioc->tm_cmds.mutex);
  3685. termination_count = 0;
  3686. query_count = 0;
  3687. mpi_reply = ioc->tm_cmds.reply;
  3688. for (smid = 1; smid <= ioc->request_depth; smid++) {
  3689. scmd = _scsih_scsi_lookup_get(ioc, smid);
  3690. if (!scmd)
  3691. continue;
  3692. sas_device_priv_data = scmd->device->hostdata;
  3693. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  3694. continue;
  3695. /* skip hidden raid components */
  3696. if (sas_device_priv_data->sas_target->flags &
  3697. MPT_TARGET_FLAGS_RAID_COMPONENT)
  3698. continue;
  3699. /* skip volumes */
  3700. if (sas_device_priv_data->sas_target->flags &
  3701. MPT_TARGET_FLAGS_VOLUME)
  3702. continue;
  3703. handle = sas_device_priv_data->sas_target->handle;
  3704. lun = sas_device_priv_data->lun;
  3705. query_count++;
  3706. mpt2sas_scsih_issue_tm(ioc, handle, lun,
  3707. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
  3708. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3709. if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
  3710. (mpi_reply->ResponseCode ==
  3711. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  3712. mpi_reply->ResponseCode ==
  3713. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  3714. continue;
  3715. mpt2sas_scsih_issue_tm(ioc, handle, lun,
  3716. MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
  3717. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3718. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  3719. }
  3720. ioc->broadcast_aen_busy = 0;
  3721. mutex_unlock(&ioc->tm_cmds.mutex);
  3722. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  3723. "%s - exit, query_count = %d termination_count = %d\n",
  3724. ioc->name, __func__, query_count, termination_count));
  3725. }
  3726. /**
  3727. * _scsih_sas_discovery_event - handle discovery events
  3728. * @ioc: per adapter object
  3729. * @event_data: event data payload
  3730. * Context: user.
  3731. *
  3732. * Return nothing.
  3733. */
  3734. static void
  3735. _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3736. Mpi2EventDataSasDiscovery_t *event_data)
  3737. {
  3738. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3739. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  3740. printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
  3741. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  3742. "start" : "stop");
  3743. if (event_data->DiscoveryStatus)
  3744. printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
  3745. ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
  3746. printk("\n");
  3747. }
  3748. #endif
  3749. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  3750. !ioc->sas_hba.num_phys)
  3751. _scsih_sas_host_add(ioc);
  3752. }
  3753. /**
  3754. * _scsih_reprobe_lun - reprobing lun
  3755. * @sdev: scsi device struct
  3756. * @no_uld_attach: sdev->no_uld_attach flag setting
  3757. *
  3758. **/
  3759. static void
  3760. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  3761. {
  3762. int rc;
  3763. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  3764. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  3765. sdev->no_uld_attach ? "hidding" : "exposing");
  3766. rc = scsi_device_reprobe(sdev);
  3767. }
  3768. /**
  3769. * _scsih_reprobe_target - reprobing target
  3770. * @starget: scsi target struct
  3771. * @no_uld_attach: sdev->no_uld_attach flag setting
  3772. *
  3773. * Note: no_uld_attach flag determines whether the disk device is attached
  3774. * to block layer. A value of `1` means to not attach.
  3775. **/
  3776. static void
  3777. _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
  3778. {
  3779. struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
  3780. if (no_uld_attach)
  3781. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  3782. else
  3783. sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  3784. starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
  3785. _scsih_reprobe_lun);
  3786. }
  3787. /**
  3788. * _scsih_sas_volume_add - add new volume
  3789. * @ioc: per adapter object
  3790. * @element: IR config element data
  3791. * Context: user.
  3792. *
  3793. * Return nothing.
  3794. */
  3795. static void
  3796. _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
  3797. Mpi2EventIrConfigElement_t *element)
  3798. {
  3799. struct _raid_device *raid_device;
  3800. unsigned long flags;
  3801. u64 wwid;
  3802. u16 handle = le16_to_cpu(element->VolDevHandle);
  3803. int rc;
  3804. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  3805. if (!wwid) {
  3806. printk(MPT2SAS_ERR_FMT
  3807. "failure at %s:%d/%s()!\n", ioc->name,
  3808. __FILE__, __LINE__, __func__);
  3809. return;
  3810. }
  3811. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3812. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  3813. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3814. if (raid_device)
  3815. return;
  3816. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  3817. if (!raid_device) {
  3818. printk(MPT2SAS_ERR_FMT
  3819. "failure at %s:%d/%s()!\n", ioc->name,
  3820. __FILE__, __LINE__, __func__);
  3821. return;
  3822. }
  3823. raid_device->id = ioc->sas_id++;
  3824. raid_device->channel = RAID_CHANNEL;
  3825. raid_device->handle = handle;
  3826. raid_device->wwid = wwid;
  3827. _scsih_raid_device_add(ioc, raid_device);
  3828. if (!ioc->wait_for_port_enable_to_complete) {
  3829. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  3830. raid_device->id, 0);
  3831. if (rc)
  3832. _scsih_raid_device_remove(ioc, raid_device);
  3833. } else
  3834. _scsih_determine_boot_device(ioc, raid_device, 1);
  3835. }
  3836. /**
  3837. * _scsih_sas_volume_delete - delete volume
  3838. * @ioc: per adapter object
  3839. * @element: IR config element data
  3840. * Context: user.
  3841. *
  3842. * Return nothing.
  3843. */
  3844. static void
  3845. _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
  3846. Mpi2EventIrConfigElement_t *element)
  3847. {
  3848. struct _raid_device *raid_device;
  3849. u16 handle = le16_to_cpu(element->VolDevHandle);
  3850. unsigned long flags;
  3851. struct MPT2SAS_TARGET *sas_target_priv_data;
  3852. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3853. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  3854. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3855. if (!raid_device)
  3856. return;
  3857. if (raid_device->starget) {
  3858. sas_target_priv_data = raid_device->starget->hostdata;
  3859. sas_target_priv_data->deleted = 1;
  3860. scsi_remove_target(&raid_device->starget->dev);
  3861. }
  3862. _scsih_raid_device_remove(ioc, raid_device);
  3863. }
  3864. /**
  3865. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  3866. * @ioc: per adapter object
  3867. * @element: IR config element data
  3868. * Context: user.
  3869. *
  3870. * Return nothing.
  3871. */
  3872. static void
  3873. _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
  3874. Mpi2EventIrConfigElement_t *element)
  3875. {
  3876. struct _sas_device *sas_device;
  3877. unsigned long flags;
  3878. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3879. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3880. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3881. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3882. if (!sas_device)
  3883. return;
  3884. /* exposing raid component */
  3885. sas_device->volume_handle = 0;
  3886. sas_device->volume_wwid = 0;
  3887. sas_device->hidden_raid_component = 0;
  3888. _scsih_reprobe_target(sas_device->starget, 0);
  3889. }
  3890. /**
  3891. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  3892. * @ioc: per adapter object
  3893. * @element: IR config element data
  3894. * Context: user.
  3895. *
  3896. * Return nothing.
  3897. */
  3898. static void
  3899. _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
  3900. Mpi2EventIrConfigElement_t *element)
  3901. {
  3902. struct _sas_device *sas_device;
  3903. unsigned long flags;
  3904. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3905. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3906. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3907. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3908. if (!sas_device)
  3909. return;
  3910. /* hiding raid component */
  3911. mpt2sas_config_get_volume_handle(ioc, handle,
  3912. &sas_device->volume_handle);
  3913. mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
  3914. &sas_device->volume_wwid);
  3915. sas_device->hidden_raid_component = 1;
  3916. _scsih_reprobe_target(sas_device->starget, 1);
  3917. }
  3918. /**
  3919. * _scsih_sas_pd_delete - delete pd component
  3920. * @ioc: per adapter object
  3921. * @element: IR config element data
  3922. * Context: user.
  3923. *
  3924. * Return nothing.
  3925. */
  3926. static void
  3927. _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
  3928. Mpi2EventIrConfigElement_t *element)
  3929. {
  3930. struct _sas_device *sas_device;
  3931. unsigned long flags;
  3932. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3933. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3934. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3935. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3936. if (!sas_device)
  3937. return;
  3938. _scsih_remove_device(ioc, handle);
  3939. }
  3940. /**
  3941. * _scsih_sas_pd_add - remove pd component
  3942. * @ioc: per adapter object
  3943. * @element: IR config element data
  3944. * Context: user.
  3945. *
  3946. * Return nothing.
  3947. */
  3948. static void
  3949. _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
  3950. Mpi2EventIrConfigElement_t *element)
  3951. {
  3952. struct _sas_device *sas_device;
  3953. unsigned long flags;
  3954. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3955. Mpi2ConfigReply_t mpi_reply;
  3956. Mpi2SasDevicePage0_t sas_device_pg0;
  3957. u32 ioc_status;
  3958. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3959. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3960. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3961. if (sas_device) {
  3962. sas_device->hidden_raid_component = 1;
  3963. return;
  3964. }
  3965. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3966. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  3967. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3968. ioc->name, __FILE__, __LINE__, __func__);
  3969. return;
  3970. }
  3971. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3972. MPI2_IOCSTATUS_MASK;
  3973. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3974. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3975. ioc->name, __FILE__, __LINE__, __func__);
  3976. return;
  3977. }
  3978. mpt2sas_transport_update_links(ioc,
  3979. le16_to_cpu(sas_device_pg0.ParentDevHandle),
  3980. handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  3981. _scsih_add_device(ioc, handle, 0, 1);
  3982. }
  3983. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3984. /**
  3985. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  3986. * @ioc: per adapter object
  3987. * @event_data: event data payload
  3988. * Context: user.
  3989. *
  3990. * Return nothing.
  3991. */
  3992. static void
  3993. _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3994. Mpi2EventDataIrConfigChangeList_t *event_data)
  3995. {
  3996. Mpi2EventIrConfigElement_t *element;
  3997. u8 element_type;
  3998. int i;
  3999. char *reason_str = NULL, *element_str = NULL;
  4000. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4001. printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
  4002. ioc->name, (le32_to_cpu(event_data->Flags) &
  4003. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  4004. "foreign" : "native", event_data->NumElements);
  4005. for (i = 0; i < event_data->NumElements; i++, element++) {
  4006. switch (element->ReasonCode) {
  4007. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4008. reason_str = "add";
  4009. break;
  4010. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4011. reason_str = "remove";
  4012. break;
  4013. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  4014. reason_str = "no change";
  4015. break;
  4016. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4017. reason_str = "hide";
  4018. break;
  4019. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4020. reason_str = "unhide";
  4021. break;
  4022. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4023. reason_str = "volume_created";
  4024. break;
  4025. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4026. reason_str = "volume_deleted";
  4027. break;
  4028. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4029. reason_str = "pd_created";
  4030. break;
  4031. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4032. reason_str = "pd_deleted";
  4033. break;
  4034. default:
  4035. reason_str = "unknown reason";
  4036. break;
  4037. }
  4038. element_type = le16_to_cpu(element->ElementFlags) &
  4039. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  4040. switch (element_type) {
  4041. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  4042. element_str = "volume";
  4043. break;
  4044. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  4045. element_str = "phys disk";
  4046. break;
  4047. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  4048. element_str = "hot spare";
  4049. break;
  4050. default:
  4051. element_str = "unknown element";
  4052. break;
  4053. }
  4054. printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
  4055. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  4056. reason_str, le16_to_cpu(element->VolDevHandle),
  4057. le16_to_cpu(element->PhysDiskDevHandle),
  4058. element->PhysDiskNum);
  4059. }
  4060. }
  4061. #endif
  4062. /**
  4063. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  4064. * @ioc: per adapter object
  4065. * @VF_ID:
  4066. * @event_data: event data payload
  4067. * Context: user.
  4068. *
  4069. * Return nothing.
  4070. */
  4071. static void
  4072. _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4073. Mpi2EventDataIrConfigChangeList_t *event_data)
  4074. {
  4075. Mpi2EventIrConfigElement_t *element;
  4076. int i;
  4077. u8 foreign_config;
  4078. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4079. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4080. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  4081. #endif
  4082. foreign_config = (le32_to_cpu(event_data->Flags) &
  4083. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  4084. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4085. for (i = 0; i < event_data->NumElements; i++, element++) {
  4086. switch (element->ReasonCode) {
  4087. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4088. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4089. if (!foreign_config)
  4090. _scsih_sas_volume_add(ioc, element);
  4091. break;
  4092. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4093. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4094. if (!foreign_config)
  4095. _scsih_sas_volume_delete(ioc, element);
  4096. break;
  4097. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4098. _scsih_sas_pd_hide(ioc, element);
  4099. break;
  4100. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4101. _scsih_sas_pd_expose(ioc, element);
  4102. break;
  4103. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4104. _scsih_sas_pd_add(ioc, element);
  4105. break;
  4106. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4107. _scsih_sas_pd_delete(ioc, element);
  4108. break;
  4109. }
  4110. }
  4111. }
  4112. /**
  4113. * _scsih_sas_ir_volume_event - IR volume event
  4114. * @ioc: per adapter object
  4115. * @event_data: event data payload
  4116. * Context: user.
  4117. *
  4118. * Return nothing.
  4119. */
  4120. static void
  4121. _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4122. Mpi2EventDataIrVolume_t *event_data)
  4123. {
  4124. u64 wwid;
  4125. unsigned long flags;
  4126. struct _raid_device *raid_device;
  4127. u16 handle;
  4128. u32 state;
  4129. int rc;
  4130. struct MPT2SAS_TARGET *sas_target_priv_data;
  4131. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  4132. return;
  4133. handle = le16_to_cpu(event_data->VolDevHandle);
  4134. state = le32_to_cpu(event_data->NewValue);
  4135. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4136. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4137. le32_to_cpu(event_data->PreviousValue), state));
  4138. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4139. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4140. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4141. switch (state) {
  4142. case MPI2_RAID_VOL_STATE_MISSING:
  4143. case MPI2_RAID_VOL_STATE_FAILED:
  4144. if (!raid_device)
  4145. break;
  4146. if (raid_device->starget) {
  4147. sas_target_priv_data = raid_device->starget->hostdata;
  4148. sas_target_priv_data->deleted = 1;
  4149. scsi_remove_target(&raid_device->starget->dev);
  4150. }
  4151. _scsih_raid_device_remove(ioc, raid_device);
  4152. break;
  4153. case MPI2_RAID_VOL_STATE_ONLINE:
  4154. case MPI2_RAID_VOL_STATE_DEGRADED:
  4155. case MPI2_RAID_VOL_STATE_OPTIMAL:
  4156. if (raid_device)
  4157. break;
  4158. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  4159. if (!wwid) {
  4160. printk(MPT2SAS_ERR_FMT
  4161. "failure at %s:%d/%s()!\n", ioc->name,
  4162. __FILE__, __LINE__, __func__);
  4163. break;
  4164. }
  4165. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  4166. if (!raid_device) {
  4167. printk(MPT2SAS_ERR_FMT
  4168. "failure at %s:%d/%s()!\n", ioc->name,
  4169. __FILE__, __LINE__, __func__);
  4170. break;
  4171. }
  4172. raid_device->id = ioc->sas_id++;
  4173. raid_device->channel = RAID_CHANNEL;
  4174. raid_device->handle = handle;
  4175. raid_device->wwid = wwid;
  4176. _scsih_raid_device_add(ioc, raid_device);
  4177. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4178. raid_device->id, 0);
  4179. if (rc)
  4180. _scsih_raid_device_remove(ioc, raid_device);
  4181. break;
  4182. case MPI2_RAID_VOL_STATE_INITIALIZING:
  4183. default:
  4184. break;
  4185. }
  4186. }
  4187. /**
  4188. * _scsih_sas_ir_physical_disk_event - PD event
  4189. * @ioc: per adapter object
  4190. * @event_data: event data payload
  4191. * Context: user.
  4192. *
  4193. * Return nothing.
  4194. */
  4195. static void
  4196. _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4197. Mpi2EventDataIrPhysicalDisk_t *event_data)
  4198. {
  4199. u16 handle;
  4200. u32 state;
  4201. struct _sas_device *sas_device;
  4202. unsigned long flags;
  4203. Mpi2ConfigReply_t mpi_reply;
  4204. Mpi2SasDevicePage0_t sas_device_pg0;
  4205. u32 ioc_status;
  4206. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  4207. return;
  4208. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  4209. state = le32_to_cpu(event_data->NewValue);
  4210. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4211. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4212. le32_to_cpu(event_data->PreviousValue), state));
  4213. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4214. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4215. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4216. switch (state) {
  4217. case MPI2_RAID_PD_STATE_ONLINE:
  4218. case MPI2_RAID_PD_STATE_DEGRADED:
  4219. case MPI2_RAID_PD_STATE_REBUILDING:
  4220. case MPI2_RAID_PD_STATE_OPTIMAL:
  4221. if (sas_device) {
  4222. sas_device->hidden_raid_component = 1;
  4223. return;
  4224. }
  4225. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  4226. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  4227. handle))) {
  4228. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4229. ioc->name, __FILE__, __LINE__, __func__);
  4230. return;
  4231. }
  4232. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4233. MPI2_IOCSTATUS_MASK;
  4234. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4235. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4236. ioc->name, __FILE__, __LINE__, __func__);
  4237. return;
  4238. }
  4239. mpt2sas_transport_update_links(ioc,
  4240. le16_to_cpu(sas_device_pg0.ParentDevHandle),
  4241. handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  4242. _scsih_add_device(ioc, handle, 0, 1);
  4243. break;
  4244. case MPI2_RAID_PD_STATE_OFFLINE:
  4245. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  4246. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  4247. case MPI2_RAID_PD_STATE_HOT_SPARE:
  4248. default:
  4249. break;
  4250. }
  4251. }
  4252. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4253. /**
  4254. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  4255. * @ioc: per adapter object
  4256. * @event_data: event data payload
  4257. * Context: user.
  4258. *
  4259. * Return nothing.
  4260. */
  4261. static void
  4262. _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4263. Mpi2EventDataIrOperationStatus_t *event_data)
  4264. {
  4265. char *reason_str = NULL;
  4266. switch (event_data->RAIDOperation) {
  4267. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  4268. reason_str = "resync";
  4269. break;
  4270. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  4271. reason_str = "online capacity expansion";
  4272. break;
  4273. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  4274. reason_str = "consistency check";
  4275. break;
  4276. default:
  4277. reason_str = "unknown reason";
  4278. break;
  4279. }
  4280. printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
  4281. "\thandle(0x%04x), percent complete(%d)\n",
  4282. ioc->name, reason_str,
  4283. le16_to_cpu(event_data->VolDevHandle),
  4284. event_data->PercentComplete);
  4285. }
  4286. #endif
  4287. /**
  4288. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  4289. * @ioc: per adapter object
  4290. * @VF_ID:
  4291. * @event_data: event data payload
  4292. * Context: user.
  4293. *
  4294. * Return nothing.
  4295. */
  4296. static void
  4297. _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4298. Mpi2EventDataIrOperationStatus_t *event_data)
  4299. {
  4300. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4301. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4302. _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
  4303. #endif
  4304. }
  4305. /**
  4306. * _scsih_task_set_full - handle task set full
  4307. * @ioc: per adapter object
  4308. * @event_data: event data payload
  4309. * Context: user.
  4310. *
  4311. * Throttle back qdepth.
  4312. */
  4313. static void
  4314. _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4315. Mpi2EventDataTaskSetFull_t *event_data)
  4316. {
  4317. unsigned long flags;
  4318. struct _sas_device *sas_device;
  4319. static struct _raid_device *raid_device;
  4320. struct scsi_device *sdev;
  4321. int depth;
  4322. u16 current_depth;
  4323. u16 handle;
  4324. int id, channel;
  4325. u64 sas_address;
  4326. current_depth = le16_to_cpu(event_data->CurrentDepth);
  4327. handle = le16_to_cpu(event_data->DevHandle);
  4328. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4329. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4330. if (!sas_device) {
  4331. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4332. return;
  4333. }
  4334. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4335. id = sas_device->id;
  4336. channel = sas_device->channel;
  4337. sas_address = sas_device->sas_address;
  4338. /* if hidden raid component, then change to volume characteristics */
  4339. if (sas_device->hidden_raid_component && sas_device->volume_handle) {
  4340. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4341. raid_device = _scsih_raid_device_find_by_handle(
  4342. ioc, sas_device->volume_handle);
  4343. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4344. if (raid_device) {
  4345. id = raid_device->id;
  4346. channel = raid_device->channel;
  4347. handle = raid_device->handle;
  4348. sas_address = raid_device->wwid;
  4349. }
  4350. }
  4351. if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
  4352. starget_printk(KERN_DEBUG, sas_device->starget, "task set "
  4353. "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
  4354. handle, (unsigned long long)sas_address, current_depth);
  4355. shost_for_each_device(sdev, ioc->shost) {
  4356. if (sdev->id == id && sdev->channel == channel) {
  4357. if (current_depth > sdev->queue_depth) {
  4358. if (ioc->logging_level &
  4359. MPT_DEBUG_TASK_SET_FULL)
  4360. sdev_printk(KERN_INFO, sdev, "strange "
  4361. "observation, the queue depth is"
  4362. " (%d) meanwhile fw queue depth "
  4363. "is (%d)\n", sdev->queue_depth,
  4364. current_depth);
  4365. continue;
  4366. }
  4367. depth = scsi_track_queue_full(sdev,
  4368. current_depth - 1);
  4369. if (depth > 0)
  4370. sdev_printk(KERN_INFO, sdev, "Queue depth "
  4371. "reduced to (%d)\n", depth);
  4372. else if (depth < 0)
  4373. sdev_printk(KERN_INFO, sdev, "Tagged Command "
  4374. "Queueing is being disabled\n");
  4375. else if (depth == 0)
  4376. if (ioc->logging_level &
  4377. MPT_DEBUG_TASK_SET_FULL)
  4378. sdev_printk(KERN_INFO, sdev,
  4379. "Queue depth not changed yet\n");
  4380. }
  4381. }
  4382. }
  4383. /**
  4384. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  4385. * @ioc: per adapter object
  4386. * @sas_address: sas address
  4387. * @slot: enclosure slot id
  4388. * @handle: device handle
  4389. *
  4390. * After host reset, find out whether devices are still responding.
  4391. * Used in _scsi_remove_unresponsive_sas_devices.
  4392. *
  4393. * Return nothing.
  4394. */
  4395. static void
  4396. _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4397. u16 slot, u16 handle)
  4398. {
  4399. struct MPT2SAS_TARGET *sas_target_priv_data;
  4400. struct scsi_target *starget;
  4401. struct _sas_device *sas_device;
  4402. unsigned long flags;
  4403. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4404. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  4405. if (sas_device->sas_address == sas_address &&
  4406. sas_device->slot == slot && sas_device->starget) {
  4407. sas_device->responding = 1;
  4408. starget_printk(KERN_INFO, sas_device->starget,
  4409. "handle(0x%04x), sas_addr(0x%016llx), enclosure "
  4410. "logical id(0x%016llx), slot(%d)\n", handle,
  4411. (unsigned long long)sas_device->sas_address,
  4412. (unsigned long long)
  4413. sas_device->enclosure_logical_id,
  4414. sas_device->slot);
  4415. if (sas_device->handle == handle)
  4416. goto out;
  4417. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  4418. sas_device->handle);
  4419. sas_device->handle = handle;
  4420. starget = sas_device->starget;
  4421. sas_target_priv_data = starget->hostdata;
  4422. sas_target_priv_data->handle = handle;
  4423. goto out;
  4424. }
  4425. }
  4426. out:
  4427. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4428. }
  4429. /**
  4430. * _scsih_search_responding_sas_devices -
  4431. * @ioc: per adapter object
  4432. *
  4433. * After host reset, find out whether devices are still responding.
  4434. * If not remove.
  4435. *
  4436. * Return nothing.
  4437. */
  4438. static void
  4439. _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  4440. {
  4441. Mpi2SasDevicePage0_t sas_device_pg0;
  4442. Mpi2ConfigReply_t mpi_reply;
  4443. u16 ioc_status;
  4444. __le64 sas_address;
  4445. u16 handle;
  4446. u32 device_info;
  4447. u16 slot;
  4448. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4449. if (list_empty(&ioc->sas_device_list))
  4450. return;
  4451. handle = 0xFFFF;
  4452. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  4453. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  4454. handle))) {
  4455. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4456. MPI2_IOCSTATUS_MASK;
  4457. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4458. break;
  4459. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  4460. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4461. if (!(_scsih_is_end_device(device_info)))
  4462. continue;
  4463. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4464. slot = le16_to_cpu(sas_device_pg0.Slot);
  4465. _scsih_mark_responding_sas_device(ioc, sas_address, slot,
  4466. handle);
  4467. }
  4468. }
  4469. /**
  4470. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  4471. * @ioc: per adapter object
  4472. * @wwid: world wide identifier for raid volume
  4473. * @handle: device handle
  4474. *
  4475. * After host reset, find out whether devices are still responding.
  4476. * Used in _scsi_remove_unresponsive_raid_devices.
  4477. *
  4478. * Return nothing.
  4479. */
  4480. static void
  4481. _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
  4482. u16 handle)
  4483. {
  4484. struct MPT2SAS_TARGET *sas_target_priv_data;
  4485. struct scsi_target *starget;
  4486. struct _raid_device *raid_device;
  4487. unsigned long flags;
  4488. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4489. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  4490. if (raid_device->wwid == wwid && raid_device->starget) {
  4491. raid_device->responding = 1;
  4492. starget_printk(KERN_INFO, raid_device->starget,
  4493. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  4494. (unsigned long long)raid_device->wwid);
  4495. if (raid_device->handle == handle)
  4496. goto out;
  4497. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  4498. raid_device->handle);
  4499. raid_device->handle = handle;
  4500. starget = raid_device->starget;
  4501. sas_target_priv_data = starget->hostdata;
  4502. sas_target_priv_data->handle = handle;
  4503. goto out;
  4504. }
  4505. }
  4506. out:
  4507. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4508. }
  4509. /**
  4510. * _scsih_search_responding_raid_devices -
  4511. * @ioc: per adapter object
  4512. *
  4513. * After host reset, find out whether devices are still responding.
  4514. * If not remove.
  4515. *
  4516. * Return nothing.
  4517. */
  4518. static void
  4519. _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
  4520. {
  4521. Mpi2RaidVolPage1_t volume_pg1;
  4522. Mpi2ConfigReply_t mpi_reply;
  4523. u16 ioc_status;
  4524. u16 handle;
  4525. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4526. if (list_empty(&ioc->raid_device_list))
  4527. return;
  4528. handle = 0xFFFF;
  4529. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  4530. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  4531. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4532. MPI2_IOCSTATUS_MASK;
  4533. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4534. break;
  4535. handle = le16_to_cpu(volume_pg1.DevHandle);
  4536. _scsih_mark_responding_raid_device(ioc,
  4537. le64_to_cpu(volume_pg1.WWID), handle);
  4538. }
  4539. }
  4540. /**
  4541. * _scsih_mark_responding_expander - mark a expander as responding
  4542. * @ioc: per adapter object
  4543. * @sas_address: sas address
  4544. * @handle:
  4545. *
  4546. * After host reset, find out whether devices are still responding.
  4547. * Used in _scsi_remove_unresponsive_expanders.
  4548. *
  4549. * Return nothing.
  4550. */
  4551. static void
  4552. _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4553. u16 handle)
  4554. {
  4555. struct _sas_node *sas_expander;
  4556. unsigned long flags;
  4557. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4558. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  4559. if (sas_expander->sas_address == sas_address) {
  4560. sas_expander->responding = 1;
  4561. if (sas_expander->handle != handle) {
  4562. printk(KERN_INFO "old handle(0x%04x)\n",
  4563. sas_expander->handle);
  4564. sas_expander->handle = handle;
  4565. }
  4566. goto out;
  4567. }
  4568. }
  4569. out:
  4570. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4571. }
  4572. /**
  4573. * _scsih_search_responding_expanders -
  4574. * @ioc: per adapter object
  4575. *
  4576. * After host reset, find out whether devices are still responding.
  4577. * If not remove.
  4578. *
  4579. * Return nothing.
  4580. */
  4581. static void
  4582. _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
  4583. {
  4584. Mpi2ExpanderPage0_t expander_pg0;
  4585. Mpi2ConfigReply_t mpi_reply;
  4586. u16 ioc_status;
  4587. __le64 sas_address;
  4588. u16 handle;
  4589. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4590. if (list_empty(&ioc->sas_expander_list))
  4591. return;
  4592. handle = 0xFFFF;
  4593. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  4594. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  4595. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4596. MPI2_IOCSTATUS_MASK;
  4597. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4598. break;
  4599. handle = le16_to_cpu(expander_pg0.DevHandle);
  4600. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  4601. printk(KERN_INFO "\texpander present: handle(0x%04x), "
  4602. "sas_addr(0x%016llx)\n", handle,
  4603. (unsigned long long)sas_address);
  4604. _scsih_mark_responding_expander(ioc, sas_address, handle);
  4605. }
  4606. }
  4607. /**
  4608. * _scsih_remove_unresponding_devices - removing unresponding devices
  4609. * @ioc: per adapter object
  4610. *
  4611. * Return nothing.
  4612. */
  4613. static void
  4614. _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
  4615. {
  4616. struct _sas_device *sas_device, *sas_device_next;
  4617. struct _sas_node *sas_expander;
  4618. struct _raid_device *raid_device, *raid_device_next;
  4619. list_for_each_entry_safe(sas_device, sas_device_next,
  4620. &ioc->sas_device_list, list) {
  4621. if (sas_device->responding) {
  4622. sas_device->responding = 0;
  4623. continue;
  4624. }
  4625. if (sas_device->starget)
  4626. starget_printk(KERN_INFO, sas_device->starget,
  4627. "removing: handle(0x%04x), sas_addr(0x%016llx), "
  4628. "enclosure logical id(0x%016llx), slot(%d)\n",
  4629. sas_device->handle,
  4630. (unsigned long long)sas_device->sas_address,
  4631. (unsigned long long)
  4632. sas_device->enclosure_logical_id,
  4633. sas_device->slot);
  4634. _scsih_remove_device(ioc, sas_device->handle);
  4635. }
  4636. list_for_each_entry_safe(raid_device, raid_device_next,
  4637. &ioc->raid_device_list, list) {
  4638. if (raid_device->responding) {
  4639. raid_device->responding = 0;
  4640. continue;
  4641. }
  4642. if (raid_device->starget) {
  4643. starget_printk(KERN_INFO, raid_device->starget,
  4644. "removing: handle(0x%04x), wwid(0x%016llx)\n",
  4645. raid_device->handle,
  4646. (unsigned long long)raid_device->wwid);
  4647. scsi_remove_target(&raid_device->starget->dev);
  4648. }
  4649. _scsih_raid_device_remove(ioc, raid_device);
  4650. }
  4651. retry_expander_search:
  4652. sas_expander = NULL;
  4653. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  4654. if (sas_expander->responding) {
  4655. sas_expander->responding = 0;
  4656. continue;
  4657. }
  4658. _scsih_expander_remove(ioc, sas_expander->handle);
  4659. goto retry_expander_search;
  4660. }
  4661. }
  4662. /**
  4663. * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
  4664. * @ioc: per adapter object
  4665. * @reset_phase: phase
  4666. *
  4667. * The handler for doing any required cleanup or initialization.
  4668. *
  4669. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  4670. * MPT2_IOC_DONE_RESET
  4671. *
  4672. * Return nothing.
  4673. */
  4674. void
  4675. mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  4676. {
  4677. switch (reset_phase) {
  4678. case MPT2_IOC_PRE_RESET:
  4679. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4680. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  4681. _scsih_fw_event_off(ioc);
  4682. break;
  4683. case MPT2_IOC_AFTER_RESET:
  4684. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4685. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  4686. if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
  4687. ioc->tm_cmds.status |= MPT2_CMD_RESET;
  4688. mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  4689. complete(&ioc->tm_cmds.done);
  4690. }
  4691. _scsih_fw_event_on(ioc);
  4692. _scsih_flush_running_cmds(ioc);
  4693. break;
  4694. case MPT2_IOC_DONE_RESET:
  4695. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4696. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  4697. _scsih_sas_host_refresh(ioc, 0);
  4698. _scsih_search_responding_sas_devices(ioc);
  4699. _scsih_search_responding_raid_devices(ioc);
  4700. _scsih_search_responding_expanders(ioc);
  4701. break;
  4702. case MPT2_IOC_RUNNING:
  4703. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4704. "MPT2_IOC_RUNNING\n", ioc->name, __func__));
  4705. _scsih_remove_unresponding_devices(ioc);
  4706. break;
  4707. }
  4708. }
  4709. /**
  4710. * _firmware_event_work - delayed task for processing firmware events
  4711. * @ioc: per adapter object
  4712. * @work: equal to the fw_event_work object
  4713. * Context: user.
  4714. *
  4715. * Return nothing.
  4716. */
  4717. static void
  4718. _firmware_event_work(struct work_struct *work)
  4719. {
  4720. struct fw_event_work *fw_event = container_of(work,
  4721. struct fw_event_work, work);
  4722. unsigned long flags;
  4723. struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
  4724. /* the queue is being flushed so ignore this event */
  4725. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4726. if (ioc->fw_events_off || ioc->remove_host) {
  4727. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4728. _scsih_fw_event_free(ioc, fw_event);
  4729. return;
  4730. }
  4731. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4732. if (ioc->shost_recovery) {
  4733. _scsih_fw_event_requeue(ioc, fw_event, 1000);
  4734. return;
  4735. }
  4736. switch (fw_event->event) {
  4737. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  4738. _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
  4739. fw_event->event_data, fw_event);
  4740. break;
  4741. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4742. _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
  4743. fw_event->event_data);
  4744. break;
  4745. case MPI2_EVENT_SAS_DISCOVERY:
  4746. _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
  4747. fw_event->event_data);
  4748. break;
  4749. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  4750. _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
  4751. fw_event->event_data);
  4752. break;
  4753. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  4754. _scsih_sas_enclosure_dev_status_change_event(ioc,
  4755. fw_event->VF_ID, fw_event->event_data);
  4756. break;
  4757. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  4758. _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
  4759. fw_event->event_data);
  4760. break;
  4761. case MPI2_EVENT_IR_VOLUME:
  4762. _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
  4763. fw_event->event_data);
  4764. break;
  4765. case MPI2_EVENT_IR_PHYSICAL_DISK:
  4766. _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
  4767. fw_event->event_data);
  4768. break;
  4769. case MPI2_EVENT_IR_OPERATION_STATUS:
  4770. _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
  4771. fw_event->event_data);
  4772. break;
  4773. case MPI2_EVENT_TASK_SET_FULL:
  4774. _scsih_task_set_full(ioc, fw_event->VF_ID,
  4775. fw_event->event_data);
  4776. break;
  4777. }
  4778. _scsih_fw_event_free(ioc, fw_event);
  4779. }
  4780. /**
  4781. * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  4782. * @ioc: per adapter object
  4783. * @VF_ID: virtual function id
  4784. * @reply: reply message frame(lower 32bit addr)
  4785. * Context: interrupt.
  4786. *
  4787. * This function merely adds a new work task into ioc->firmware_event_thread.
  4788. * The tasks are worked from _firmware_event_work in user context.
  4789. *
  4790. * Return nothing.
  4791. */
  4792. void
  4793. mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
  4794. {
  4795. struct fw_event_work *fw_event;
  4796. Mpi2EventNotificationReply_t *mpi_reply;
  4797. unsigned long flags;
  4798. u16 event;
  4799. /* events turned off due to host reset or driver unloading */
  4800. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4801. if (ioc->fw_events_off || ioc->remove_host) {
  4802. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4803. return;
  4804. }
  4805. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4806. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  4807. event = le16_to_cpu(mpi_reply->Event);
  4808. switch (event) {
  4809. /* handle these */
  4810. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  4811. {
  4812. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  4813. (Mpi2EventDataSasBroadcastPrimitive_t *)
  4814. mpi_reply->EventData;
  4815. if (baen_data->Primitive !=
  4816. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
  4817. ioc->broadcast_aen_busy)
  4818. return;
  4819. ioc->broadcast_aen_busy = 1;
  4820. break;
  4821. }
  4822. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  4823. _scsih_check_topo_delete_events(ioc,
  4824. (Mpi2EventDataSasTopologyChangeList_t *)
  4825. mpi_reply->EventData);
  4826. break;
  4827. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4828. case MPI2_EVENT_IR_OPERATION_STATUS:
  4829. case MPI2_EVENT_SAS_DISCOVERY:
  4830. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  4831. case MPI2_EVENT_IR_VOLUME:
  4832. case MPI2_EVENT_IR_PHYSICAL_DISK:
  4833. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  4834. case MPI2_EVENT_TASK_SET_FULL:
  4835. break;
  4836. default: /* ignore the rest */
  4837. return;
  4838. }
  4839. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  4840. if (!fw_event) {
  4841. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4842. ioc->name, __FILE__, __LINE__, __func__);
  4843. return;
  4844. }
  4845. fw_event->event_data =
  4846. kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
  4847. if (!fw_event->event_data) {
  4848. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4849. ioc->name, __FILE__, __LINE__, __func__);
  4850. kfree(fw_event);
  4851. return;
  4852. }
  4853. memcpy(fw_event->event_data, mpi_reply->EventData,
  4854. mpi_reply->EventDataLength*4);
  4855. fw_event->ioc = ioc;
  4856. fw_event->VF_ID = VF_ID;
  4857. fw_event->event = event;
  4858. _scsih_fw_event_add(ioc, fw_event);
  4859. }
  4860. /* shost template */
  4861. static struct scsi_host_template scsih_driver_template = {
  4862. .module = THIS_MODULE,
  4863. .name = "Fusion MPT SAS Host",
  4864. .proc_name = MPT2SAS_DRIVER_NAME,
  4865. .queuecommand = _scsih_qcmd,
  4866. .target_alloc = _scsih_target_alloc,
  4867. .slave_alloc = _scsih_slave_alloc,
  4868. .slave_configure = _scsih_slave_configure,
  4869. .target_destroy = _scsih_target_destroy,
  4870. .slave_destroy = _scsih_slave_destroy,
  4871. .change_queue_depth = _scsih_change_queue_depth,
  4872. .change_queue_type = _scsih_change_queue_type,
  4873. .eh_abort_handler = _scsih_abort,
  4874. .eh_device_reset_handler = _scsih_dev_reset,
  4875. .eh_target_reset_handler = _scsih_target_reset,
  4876. .eh_host_reset_handler = _scsih_host_reset,
  4877. .bios_param = _scsih_bios_param,
  4878. .can_queue = 1,
  4879. .this_id = -1,
  4880. .sg_tablesize = MPT2SAS_SG_DEPTH,
  4881. .max_sectors = 8192,
  4882. .cmd_per_lun = 7,
  4883. .use_clustering = ENABLE_CLUSTERING,
  4884. .shost_attrs = mpt2sas_host_attrs,
  4885. .sdev_attrs = mpt2sas_dev_attrs,
  4886. };
  4887. /**
  4888. * _scsih_expander_node_remove - removing expander device from list.
  4889. * @ioc: per adapter object
  4890. * @sas_expander: the sas_device object
  4891. * Context: Calling function should acquire ioc->sas_node_lock.
  4892. *
  4893. * Removing object and freeing associated memory from the
  4894. * ioc->sas_expander_list.
  4895. *
  4896. * Return nothing.
  4897. */
  4898. static void
  4899. _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  4900. struct _sas_node *sas_expander)
  4901. {
  4902. struct _sas_port *mpt2sas_port;
  4903. struct _sas_device *sas_device;
  4904. struct _sas_node *expander_sibling;
  4905. unsigned long flags;
  4906. if (!sas_expander)
  4907. return;
  4908. /* remove sibling ports attached to this expander */
  4909. retry_device_search:
  4910. list_for_each_entry(mpt2sas_port,
  4911. &sas_expander->sas_port_list, port_list) {
  4912. if (mpt2sas_port->remote_identify.device_type ==
  4913. SAS_END_DEVICE) {
  4914. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4915. sas_device =
  4916. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4917. mpt2sas_port->remote_identify.sas_address);
  4918. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4919. if (!sas_device)
  4920. continue;
  4921. _scsih_remove_device(ioc, sas_device->handle);
  4922. if (ioc->shost_recovery)
  4923. return;
  4924. goto retry_device_search;
  4925. }
  4926. }
  4927. retry_expander_search:
  4928. list_for_each_entry(mpt2sas_port,
  4929. &sas_expander->sas_port_list, port_list) {
  4930. if (mpt2sas_port->remote_identify.device_type ==
  4931. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  4932. mpt2sas_port->remote_identify.device_type ==
  4933. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  4934. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4935. expander_sibling =
  4936. mpt2sas_scsih_expander_find_by_sas_address(
  4937. ioc, mpt2sas_port->remote_identify.sas_address);
  4938. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4939. if (!expander_sibling)
  4940. continue;
  4941. _scsih_expander_remove(ioc, expander_sibling->handle);
  4942. if (ioc->shost_recovery)
  4943. return;
  4944. goto retry_expander_search;
  4945. }
  4946. }
  4947. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  4948. sas_expander->parent_handle);
  4949. printk(MPT2SAS_INFO_FMT "expander_remove: handle"
  4950. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  4951. sas_expander->handle, (unsigned long long)
  4952. sas_expander->sas_address);
  4953. list_del(&sas_expander->list);
  4954. kfree(sas_expander->phy);
  4955. kfree(sas_expander);
  4956. }
  4957. /**
  4958. * _scsih_remove - detach and remove add host
  4959. * @pdev: PCI device struct
  4960. *
  4961. * Return nothing.
  4962. */
  4963. static void __devexit
  4964. _scsih_remove(struct pci_dev *pdev)
  4965. {
  4966. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  4967. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  4968. struct _sas_port *mpt2sas_port;
  4969. struct _sas_device *sas_device;
  4970. struct _sas_node *expander_sibling;
  4971. struct workqueue_struct *wq;
  4972. unsigned long flags;
  4973. ioc->remove_host = 1;
  4974. _scsih_fw_event_off(ioc);
  4975. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4976. wq = ioc->firmware_event_thread;
  4977. ioc->firmware_event_thread = NULL;
  4978. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4979. if (wq)
  4980. destroy_workqueue(wq);
  4981. /* free ports attached to the sas_host */
  4982. retry_again:
  4983. list_for_each_entry(mpt2sas_port,
  4984. &ioc->sas_hba.sas_port_list, port_list) {
  4985. if (mpt2sas_port->remote_identify.device_type ==
  4986. SAS_END_DEVICE) {
  4987. sas_device =
  4988. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4989. mpt2sas_port->remote_identify.sas_address);
  4990. if (sas_device) {
  4991. _scsih_remove_device(ioc, sas_device->handle);
  4992. goto retry_again;
  4993. }
  4994. } else {
  4995. expander_sibling =
  4996. mpt2sas_scsih_expander_find_by_sas_address(ioc,
  4997. mpt2sas_port->remote_identify.sas_address);
  4998. if (expander_sibling) {
  4999. _scsih_expander_remove(ioc,
  5000. expander_sibling->handle);
  5001. goto retry_again;
  5002. }
  5003. }
  5004. }
  5005. /* free phys attached to the sas_host */
  5006. if (ioc->sas_hba.num_phys) {
  5007. kfree(ioc->sas_hba.phy);
  5008. ioc->sas_hba.phy = NULL;
  5009. ioc->sas_hba.num_phys = 0;
  5010. }
  5011. sas_remove_host(shost);
  5012. mpt2sas_base_detach(ioc);
  5013. list_del(&ioc->list);
  5014. scsi_remove_host(shost);
  5015. scsi_host_put(shost);
  5016. }
  5017. /**
  5018. * _scsih_probe_boot_devices - reports 1st device
  5019. * @ioc: per adapter object
  5020. *
  5021. * If specified in bios page 2, this routine reports the 1st
  5022. * device scsi-ml or sas transport for persistent boot device
  5023. * purposes. Please refer to function _scsih_determine_boot_device()
  5024. */
  5025. static void
  5026. _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
  5027. {
  5028. u8 is_raid;
  5029. void *device;
  5030. struct _sas_device *sas_device;
  5031. struct _raid_device *raid_device;
  5032. u16 handle, parent_handle;
  5033. u64 sas_address;
  5034. unsigned long flags;
  5035. int rc;
  5036. device = NULL;
  5037. if (ioc->req_boot_device.device) {
  5038. device = ioc->req_boot_device.device;
  5039. is_raid = ioc->req_boot_device.is_raid;
  5040. } else if (ioc->req_alt_boot_device.device) {
  5041. device = ioc->req_alt_boot_device.device;
  5042. is_raid = ioc->req_alt_boot_device.is_raid;
  5043. } else if (ioc->current_boot_device.device) {
  5044. device = ioc->current_boot_device.device;
  5045. is_raid = ioc->current_boot_device.is_raid;
  5046. }
  5047. if (!device)
  5048. return;
  5049. if (is_raid) {
  5050. raid_device = device;
  5051. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5052. raid_device->id, 0);
  5053. if (rc)
  5054. _scsih_raid_device_remove(ioc, raid_device);
  5055. } else {
  5056. sas_device = device;
  5057. handle = sas_device->handle;
  5058. parent_handle = sas_device->parent_handle;
  5059. sas_address = sas_device->sas_address;
  5060. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5061. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5062. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5063. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  5064. sas_device->parent_handle)) {
  5065. _scsih_sas_device_remove(ioc, sas_device);
  5066. } else if (!sas_device->starget) {
  5067. mpt2sas_transport_port_remove(ioc, sas_address,
  5068. parent_handle);
  5069. _scsih_sas_device_remove(ioc, sas_device);
  5070. }
  5071. }
  5072. }
  5073. /**
  5074. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  5075. * @ioc: per adapter object
  5076. *
  5077. * Called during initial loading of the driver.
  5078. */
  5079. static void
  5080. _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
  5081. {
  5082. struct _raid_device *raid_device, *raid_next;
  5083. int rc;
  5084. list_for_each_entry_safe(raid_device, raid_next,
  5085. &ioc->raid_device_list, list) {
  5086. if (raid_device->starget)
  5087. continue;
  5088. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5089. raid_device->id, 0);
  5090. if (rc)
  5091. _scsih_raid_device_remove(ioc, raid_device);
  5092. }
  5093. }
  5094. /**
  5095. * _scsih_probe_sas - reporting raid volumes to sas transport
  5096. * @ioc: per adapter object
  5097. *
  5098. * Called during initial loading of the driver.
  5099. */
  5100. static void
  5101. _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
  5102. {
  5103. struct _sas_device *sas_device, *next;
  5104. unsigned long flags;
  5105. u16 handle, parent_handle;
  5106. u64 sas_address;
  5107. /* SAS Device List */
  5108. list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
  5109. list) {
  5110. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5111. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5112. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5113. handle = sas_device->handle;
  5114. parent_handle = sas_device->parent_handle;
  5115. sas_address = sas_device->sas_address;
  5116. if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
  5117. _scsih_sas_device_remove(ioc, sas_device);
  5118. } else if (!sas_device->starget) {
  5119. mpt2sas_transport_port_remove(ioc, sas_address,
  5120. parent_handle);
  5121. _scsih_sas_device_remove(ioc, sas_device);
  5122. }
  5123. }
  5124. }
  5125. /**
  5126. * _scsih_probe_devices - probing for devices
  5127. * @ioc: per adapter object
  5128. *
  5129. * Called during initial loading of the driver.
  5130. */
  5131. static void
  5132. _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
  5133. {
  5134. u16 volume_mapping_flags =
  5135. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  5136. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  5137. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  5138. return; /* return when IOC doesn't support initiator mode */
  5139. _scsih_probe_boot_devices(ioc);
  5140. if (ioc->ir_firmware) {
  5141. if ((volume_mapping_flags &
  5142. MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
  5143. _scsih_probe_sas(ioc);
  5144. _scsih_probe_raid(ioc);
  5145. } else {
  5146. _scsih_probe_raid(ioc);
  5147. _scsih_probe_sas(ioc);
  5148. }
  5149. } else
  5150. _scsih_probe_sas(ioc);
  5151. }
  5152. /**
  5153. * _scsih_probe - attach and add scsi host
  5154. * @pdev: PCI device struct
  5155. * @id: pci device id
  5156. *
  5157. * Returns 0 success, anything else error.
  5158. */
  5159. static int
  5160. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  5161. {
  5162. struct MPT2SAS_ADAPTER *ioc;
  5163. struct Scsi_Host *shost;
  5164. shost = scsi_host_alloc(&scsih_driver_template,
  5165. sizeof(struct MPT2SAS_ADAPTER));
  5166. if (!shost)
  5167. return -ENODEV;
  5168. /* init local params */
  5169. ioc = shost_priv(shost);
  5170. memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
  5171. INIT_LIST_HEAD(&ioc->list);
  5172. list_add_tail(&ioc->list, &mpt2sas_ioc_list);
  5173. ioc->shost = shost;
  5174. ioc->id = mpt_ids++;
  5175. sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
  5176. ioc->pdev = pdev;
  5177. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  5178. ioc->tm_cb_idx = tm_cb_idx;
  5179. ioc->ctl_cb_idx = ctl_cb_idx;
  5180. ioc->base_cb_idx = base_cb_idx;
  5181. ioc->transport_cb_idx = transport_cb_idx;
  5182. ioc->config_cb_idx = config_cb_idx;
  5183. ioc->logging_level = logging_level;
  5184. /* misc semaphores and spin locks */
  5185. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  5186. spin_lock_init(&ioc->scsi_lookup_lock);
  5187. spin_lock_init(&ioc->sas_device_lock);
  5188. spin_lock_init(&ioc->sas_node_lock);
  5189. spin_lock_init(&ioc->fw_event_lock);
  5190. spin_lock_init(&ioc->raid_device_lock);
  5191. INIT_LIST_HEAD(&ioc->sas_device_list);
  5192. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  5193. INIT_LIST_HEAD(&ioc->sas_expander_list);
  5194. INIT_LIST_HEAD(&ioc->fw_event_list);
  5195. INIT_LIST_HEAD(&ioc->raid_device_list);
  5196. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  5197. /* init shost parameters */
  5198. shost->max_cmd_len = 16;
  5199. shost->max_lun = max_lun;
  5200. shost->transportt = mpt2sas_transport_template;
  5201. shost->unique_id = ioc->id;
  5202. if ((scsi_add_host(shost, &pdev->dev))) {
  5203. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5204. ioc->name, __FILE__, __LINE__, __func__);
  5205. list_del(&ioc->list);
  5206. goto out_add_shost_fail;
  5207. }
  5208. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  5209. | SHOST_DIF_TYPE3_PROTECTION);
  5210. /* event thread */
  5211. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  5212. "fw_event%d", ioc->id);
  5213. ioc->firmware_event_thread = create_singlethread_workqueue(
  5214. ioc->firmware_event_name);
  5215. if (!ioc->firmware_event_thread) {
  5216. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5217. ioc->name, __FILE__, __LINE__, __func__);
  5218. goto out_thread_fail;
  5219. }
  5220. ioc->wait_for_port_enable_to_complete = 1;
  5221. if ((mpt2sas_base_attach(ioc))) {
  5222. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5223. ioc->name, __FILE__, __LINE__, __func__);
  5224. goto out_attach_fail;
  5225. }
  5226. ioc->wait_for_port_enable_to_complete = 0;
  5227. _scsih_probe_devices(ioc);
  5228. return 0;
  5229. out_attach_fail:
  5230. destroy_workqueue(ioc->firmware_event_thread);
  5231. out_thread_fail:
  5232. list_del(&ioc->list);
  5233. scsi_remove_host(shost);
  5234. out_add_shost_fail:
  5235. return -ENODEV;
  5236. }
  5237. #ifdef CONFIG_PM
  5238. /**
  5239. * _scsih_suspend - power management suspend main entry point
  5240. * @pdev: PCI device struct
  5241. * @state: PM state change to (usually PCI_D3)
  5242. *
  5243. * Returns 0 success, anything else error.
  5244. */
  5245. static int
  5246. _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  5247. {
  5248. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5249. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5250. u32 device_state;
  5251. mpt2sas_base_stop_watchdog(ioc);
  5252. flush_scheduled_work();
  5253. scsi_block_requests(shost);
  5254. device_state = pci_choose_state(pdev, state);
  5255. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
  5256. "operating state [D%d]\n", ioc->name, pdev,
  5257. pci_name(pdev), device_state);
  5258. mpt2sas_base_free_resources(ioc);
  5259. pci_save_state(pdev);
  5260. pci_disable_device(pdev);
  5261. pci_set_power_state(pdev, device_state);
  5262. return 0;
  5263. }
  5264. /**
  5265. * _scsih_resume - power management resume main entry point
  5266. * @pdev: PCI device struct
  5267. *
  5268. * Returns 0 success, anything else error.
  5269. */
  5270. static int
  5271. _scsih_resume(struct pci_dev *pdev)
  5272. {
  5273. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5274. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5275. u32 device_state = pdev->current_state;
  5276. int r;
  5277. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
  5278. "operating state [D%d]\n", ioc->name, pdev,
  5279. pci_name(pdev), device_state);
  5280. pci_set_power_state(pdev, PCI_D0);
  5281. pci_enable_wake(pdev, PCI_D0, 0);
  5282. pci_restore_state(pdev);
  5283. ioc->pdev = pdev;
  5284. r = mpt2sas_base_map_resources(ioc);
  5285. if (r)
  5286. return r;
  5287. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
  5288. scsi_unblock_requests(shost);
  5289. mpt2sas_base_start_watchdog(ioc);
  5290. return 0;
  5291. }
  5292. #endif /* CONFIG_PM */
  5293. static struct pci_driver scsih_driver = {
  5294. .name = MPT2SAS_DRIVER_NAME,
  5295. .id_table = scsih_pci_table,
  5296. .probe = _scsih_probe,
  5297. .remove = __devexit_p(_scsih_remove),
  5298. #ifdef CONFIG_PM
  5299. .suspend = _scsih_suspend,
  5300. .resume = _scsih_resume,
  5301. #endif
  5302. };
  5303. /**
  5304. * _scsih_init - main entry point for this driver.
  5305. *
  5306. * Returns 0 success, anything else error.
  5307. */
  5308. static int __init
  5309. _scsih_init(void)
  5310. {
  5311. int error;
  5312. mpt_ids = 0;
  5313. printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
  5314. MPT2SAS_DRIVER_VERSION);
  5315. mpt2sas_transport_template =
  5316. sas_attach_transport(&mpt2sas_transport_functions);
  5317. if (!mpt2sas_transport_template)
  5318. return -ENODEV;
  5319. mpt2sas_base_initialize_callback_handler();
  5320. /* queuecommand callback hander */
  5321. scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
  5322. /* task managment callback handler */
  5323. tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
  5324. /* base internal commands callback handler */
  5325. base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
  5326. /* transport internal commands callback handler */
  5327. transport_cb_idx = mpt2sas_base_register_callback_handler(
  5328. mpt2sas_transport_done);
  5329. /* configuration page API internal commands callback handler */
  5330. config_cb_idx = mpt2sas_base_register_callback_handler(
  5331. mpt2sas_config_done);
  5332. /* ctl module callback handler */
  5333. ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
  5334. mpt2sas_ctl_init();
  5335. error = pci_register_driver(&scsih_driver);
  5336. if (error)
  5337. sas_release_transport(mpt2sas_transport_template);
  5338. return error;
  5339. }
  5340. /**
  5341. * _scsih_exit - exit point for this driver (when it is a module).
  5342. *
  5343. * Returns 0 success, anything else error.
  5344. */
  5345. static void __exit
  5346. _scsih_exit(void)
  5347. {
  5348. printk(KERN_INFO "mpt2sas version %s unloading\n",
  5349. MPT2SAS_DRIVER_VERSION);
  5350. pci_unregister_driver(&scsih_driver);
  5351. sas_release_transport(mpt2sas_transport_template);
  5352. mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
  5353. mpt2sas_base_release_callback_handler(tm_cb_idx);
  5354. mpt2sas_base_release_callback_handler(base_cb_idx);
  5355. mpt2sas_base_release_callback_handler(transport_cb_idx);
  5356. mpt2sas_base_release_callback_handler(config_cb_idx);
  5357. mpt2sas_base_release_callback_handler(ctl_cb_idx);
  5358. mpt2sas_ctl_exit();
  5359. }
  5360. module_init(_scsih_init);
  5361. module_exit(_scsih_exit);