mpt2sas_scsih.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931
  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. if (ioc->manu_pg10.OEMIdentifier &&
  1363. (ioc->manu_pg10.GenericFlags0 &
  1364. MFG10_GF0_R10_DISPLAY) &&
  1365. !(raid_device->num_pds % 2))
  1366. r_level = "RAID10";
  1367. else
  1368. r_level = "RAID1E";
  1369. break;
  1370. case MPI2_RAID_VOL_TYPE_RAID1:
  1371. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1372. r_level = "RAID1";
  1373. break;
  1374. case MPI2_RAID_VOL_TYPE_RAID10:
  1375. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1376. r_level = "RAID10";
  1377. break;
  1378. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  1379. default:
  1380. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1381. r_level = "RAIDX";
  1382. break;
  1383. }
  1384. sdev_printk(KERN_INFO, sdev, "%s: "
  1385. "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
  1386. r_level, raid_device->handle,
  1387. (unsigned long long)raid_device->wwid,
  1388. raid_device->num_pds, ds);
  1389. _scsih_change_queue_depth(sdev, qdepth);
  1390. return 0;
  1391. }
  1392. /* non-raid handling */
  1393. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1394. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1395. sas_device_priv_data->sas_target->sas_address);
  1396. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1397. if (sas_device) {
  1398. if (sas_target_priv_data->flags &
  1399. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1400. mpt2sas_config_get_volume_handle(ioc,
  1401. sas_device->handle, &sas_device->volume_handle);
  1402. mpt2sas_config_get_volume_wwid(ioc,
  1403. sas_device->volume_handle,
  1404. &sas_device->volume_wwid);
  1405. }
  1406. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1407. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1408. ssp_target = 1;
  1409. ds = "SSP";
  1410. } else {
  1411. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1412. if (sas_device->device_info &
  1413. MPI2_SAS_DEVICE_INFO_STP_TARGET)
  1414. ds = "STP";
  1415. else if (sas_device->device_info &
  1416. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1417. ds = "SATA";
  1418. }
  1419. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
  1420. "sas_addr(0x%016llx), device_name(0x%016llx)\n",
  1421. ds, sas_device->handle,
  1422. (unsigned long long)sas_device->sas_address,
  1423. (unsigned long long)sas_device->device_name);
  1424. sdev_printk(KERN_INFO, sdev, "%s: "
  1425. "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
  1426. (unsigned long long) sas_device->enclosure_logical_id,
  1427. sas_device->slot);
  1428. if (!ssp_target)
  1429. _scsih_display_sata_capabilities(ioc, sas_device, sdev);
  1430. }
  1431. _scsih_change_queue_depth(sdev, qdepth);
  1432. if (ssp_target)
  1433. sas_read_port_mode_page(sdev);
  1434. return 0;
  1435. }
  1436. /**
  1437. * _scsih_bios_param - fetch head, sector, cylinder info for a disk
  1438. * @sdev: scsi device struct
  1439. * @bdev: pointer to block device context
  1440. * @capacity: device size (in 512 byte sectors)
  1441. * @params: three element array to place output:
  1442. * params[0] number of heads (max 255)
  1443. * params[1] number of sectors (max 63)
  1444. * params[2] number of cylinders
  1445. *
  1446. * Return nothing.
  1447. */
  1448. static int
  1449. _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1450. sector_t capacity, int params[])
  1451. {
  1452. int heads;
  1453. int sectors;
  1454. sector_t cylinders;
  1455. ulong dummy;
  1456. heads = 64;
  1457. sectors = 32;
  1458. dummy = heads * sectors;
  1459. cylinders = capacity;
  1460. sector_div(cylinders, dummy);
  1461. /*
  1462. * Handle extended translation size for logical drives
  1463. * > 1Gb
  1464. */
  1465. if ((ulong)capacity >= 0x200000) {
  1466. heads = 255;
  1467. sectors = 63;
  1468. dummy = heads * sectors;
  1469. cylinders = capacity;
  1470. sector_div(cylinders, dummy);
  1471. }
  1472. /* return result */
  1473. params[0] = heads;
  1474. params[1] = sectors;
  1475. params[2] = cylinders;
  1476. return 0;
  1477. }
  1478. /**
  1479. * _scsih_response_code - translation of device response code
  1480. * @ioc: per adapter object
  1481. * @response_code: response code returned by the device
  1482. *
  1483. * Return nothing.
  1484. */
  1485. static void
  1486. _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
  1487. {
  1488. char *desc;
  1489. switch (response_code) {
  1490. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  1491. desc = "task management request completed";
  1492. break;
  1493. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  1494. desc = "invalid frame";
  1495. break;
  1496. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1497. desc = "task management request not supported";
  1498. break;
  1499. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  1500. desc = "task management request failed";
  1501. break;
  1502. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1503. desc = "task management request succeeded";
  1504. break;
  1505. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1506. desc = "invalid lun";
  1507. break;
  1508. case 0xA:
  1509. desc = "overlapped tag attempted";
  1510. break;
  1511. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1512. desc = "task queued, however not sent to target";
  1513. break;
  1514. default:
  1515. desc = "unknown";
  1516. break;
  1517. }
  1518. printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
  1519. ioc->name, response_code, desc);
  1520. }
  1521. /**
  1522. * _scsih_tm_done - tm completion routine
  1523. * @ioc: per adapter object
  1524. * @smid: system request message index
  1525. * @VF_ID: virtual function id
  1526. * @reply: reply message frame(lower 32bit addr)
  1527. * Context: none.
  1528. *
  1529. * The callback handler when using scsih_issue_tm.
  1530. *
  1531. * Return nothing.
  1532. */
  1533. static void
  1534. _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  1535. {
  1536. MPI2DefaultReply_t *mpi_reply;
  1537. if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
  1538. return;
  1539. if (ioc->tm_cmds.smid != smid)
  1540. return;
  1541. ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
  1542. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  1543. if (mpi_reply) {
  1544. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1545. ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
  1546. }
  1547. ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
  1548. complete(&ioc->tm_cmds.done);
  1549. }
  1550. /**
  1551. * mpt2sas_scsih_set_tm_flag - set per target tm_busy
  1552. * @ioc: per adapter object
  1553. * @handle: device handle
  1554. *
  1555. * During taskmangement request, we need to freeze the device queue.
  1556. */
  1557. void
  1558. mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1559. {
  1560. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1561. struct scsi_device *sdev;
  1562. u8 skip = 0;
  1563. shost_for_each_device(sdev, ioc->shost) {
  1564. if (skip)
  1565. continue;
  1566. sas_device_priv_data = sdev->hostdata;
  1567. if (!sas_device_priv_data)
  1568. continue;
  1569. if (sas_device_priv_data->sas_target->handle == handle) {
  1570. sas_device_priv_data->sas_target->tm_busy = 1;
  1571. skip = 1;
  1572. ioc->ignore_loginfos = 1;
  1573. }
  1574. }
  1575. }
  1576. /**
  1577. * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
  1578. * @ioc: per adapter object
  1579. * @handle: device handle
  1580. *
  1581. * During taskmangement request, we need to freeze the device queue.
  1582. */
  1583. void
  1584. mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1585. {
  1586. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1587. struct scsi_device *sdev;
  1588. u8 skip = 0;
  1589. shost_for_each_device(sdev, ioc->shost) {
  1590. if (skip)
  1591. continue;
  1592. sas_device_priv_data = sdev->hostdata;
  1593. if (!sas_device_priv_data)
  1594. continue;
  1595. if (sas_device_priv_data->sas_target->handle == handle) {
  1596. sas_device_priv_data->sas_target->tm_busy = 0;
  1597. skip = 1;
  1598. ioc->ignore_loginfos = 0;
  1599. }
  1600. }
  1601. }
  1602. /**
  1603. * mpt2sas_scsih_issue_tm - main routine for sending tm requests
  1604. * @ioc: per adapter struct
  1605. * @device_handle: device handle
  1606. * @lun: lun number
  1607. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  1608. * @smid_task: smid assigned to the task
  1609. * @timeout: timeout in seconds
  1610. * Context: The calling function needs to acquire the tm_cmds.mutex
  1611. *
  1612. * A generic API for sending task management requests to firmware.
  1613. *
  1614. * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
  1615. * this API.
  1616. *
  1617. * The callback index is set inside `ioc->tm_cb_idx`.
  1618. *
  1619. * Return nothing.
  1620. */
  1621. void
  1622. mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
  1623. u8 type, u16 smid_task, ulong timeout)
  1624. {
  1625. Mpi2SCSITaskManagementRequest_t *mpi_request;
  1626. Mpi2SCSITaskManagementReply_t *mpi_reply;
  1627. u16 smid = 0;
  1628. u32 ioc_state;
  1629. unsigned long timeleft;
  1630. u8 VF_ID = 0;
  1631. if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
  1632. printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
  1633. __func__, ioc->name);
  1634. return;
  1635. }
  1636. if (ioc->shost_recovery) {
  1637. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1638. __func__, ioc->name);
  1639. return;
  1640. }
  1641. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  1642. if (ioc_state & MPI2_DOORBELL_USED) {
  1643. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
  1644. "active!\n", ioc->name));
  1645. goto issue_host_reset;
  1646. }
  1647. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  1648. mpt2sas_base_fault_info(ioc, ioc_state &
  1649. MPI2_DOORBELL_DATA_MASK);
  1650. goto issue_host_reset;
  1651. }
  1652. smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
  1653. if (!smid) {
  1654. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1655. ioc->name, __func__);
  1656. return;
  1657. }
  1658. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
  1659. " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
  1660. ioc->tm_cmds.status = MPT2_CMD_PENDING;
  1661. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1662. ioc->tm_cmds.smid = smid;
  1663. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  1664. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  1665. mpi_request->DevHandle = cpu_to_le16(handle);
  1666. mpi_request->TaskType = type;
  1667. mpi_request->TaskMID = cpu_to_le16(smid_task);
  1668. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  1669. mpt2sas_scsih_set_tm_flag(ioc, handle);
  1670. init_completion(&ioc->tm_cmds.done);
  1671. mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
  1672. timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  1673. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  1674. if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
  1675. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1676. ioc->name, __func__);
  1677. _debug_dump_mf(mpi_request,
  1678. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  1679. if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
  1680. goto issue_host_reset;
  1681. }
  1682. if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
  1683. mpi_reply = ioc->tm_cmds.reply;
  1684. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
  1685. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  1686. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  1687. le32_to_cpu(mpi_reply->IOCLogInfo),
  1688. le32_to_cpu(mpi_reply->TerminationCount)));
  1689. if (ioc->logging_level & MPT_DEBUG_TM)
  1690. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  1691. }
  1692. return;
  1693. issue_host_reset:
  1694. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
  1695. }
  1696. /**
  1697. * _scsih_abort - eh threads main abort routine
  1698. * @sdev: scsi device struct
  1699. *
  1700. * Returns SUCCESS if command aborted else FAILED
  1701. */
  1702. static int
  1703. _scsih_abort(struct scsi_cmnd *scmd)
  1704. {
  1705. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1706. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1707. u16 smid;
  1708. u16 handle;
  1709. int r;
  1710. struct scsi_cmnd *scmd_lookup;
  1711. printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
  1712. ioc->name, scmd);
  1713. scsi_print_command(scmd);
  1714. sas_device_priv_data = scmd->device->hostdata;
  1715. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1716. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1717. ioc->name, scmd);
  1718. scmd->result = DID_NO_CONNECT << 16;
  1719. scmd->scsi_done(scmd);
  1720. r = SUCCESS;
  1721. goto out;
  1722. }
  1723. /* search for the command */
  1724. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  1725. if (!smid) {
  1726. scmd->result = DID_RESET << 16;
  1727. r = SUCCESS;
  1728. goto out;
  1729. }
  1730. /* for hidden raid components and volumes this is not supported */
  1731. if (sas_device_priv_data->sas_target->flags &
  1732. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  1733. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  1734. scmd->result = DID_RESET << 16;
  1735. r = FAILED;
  1736. goto out;
  1737. }
  1738. mutex_lock(&ioc->tm_cmds.mutex);
  1739. handle = sas_device_priv_data->sas_target->handle;
  1740. mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
  1741. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
  1742. /* sanity check - see whether command actually completed */
  1743. scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
  1744. if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
  1745. r = FAILED;
  1746. else
  1747. r = SUCCESS;
  1748. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1749. mutex_unlock(&ioc->tm_cmds.mutex);
  1750. out:
  1751. printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
  1752. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1753. return r;
  1754. }
  1755. /**
  1756. * _scsih_dev_reset - eh threads main device reset routine
  1757. * @sdev: scsi device struct
  1758. *
  1759. * Returns SUCCESS if command aborted else FAILED
  1760. */
  1761. static int
  1762. _scsih_dev_reset(struct scsi_cmnd *scmd)
  1763. {
  1764. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1765. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1766. struct _sas_device *sas_device;
  1767. unsigned long flags;
  1768. u16 handle;
  1769. int r;
  1770. printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
  1771. ioc->name, scmd);
  1772. scsi_print_command(scmd);
  1773. sas_device_priv_data = scmd->device->hostdata;
  1774. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1775. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1776. ioc->name, scmd);
  1777. scmd->result = DID_NO_CONNECT << 16;
  1778. scmd->scsi_done(scmd);
  1779. r = SUCCESS;
  1780. goto out;
  1781. }
  1782. /* for hidden raid components obtain the volume_handle */
  1783. handle = 0;
  1784. if (sas_device_priv_data->sas_target->flags &
  1785. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1786. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1787. sas_device = _scsih_sas_device_find_by_handle(ioc,
  1788. sas_device_priv_data->sas_target->handle);
  1789. if (sas_device)
  1790. handle = sas_device->volume_handle;
  1791. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1792. } else
  1793. handle = sas_device_priv_data->sas_target->handle;
  1794. if (!handle) {
  1795. scmd->result = DID_RESET << 16;
  1796. r = FAILED;
  1797. goto out;
  1798. }
  1799. mutex_lock(&ioc->tm_cmds.mutex);
  1800. mpt2sas_scsih_issue_tm(ioc, handle, 0,
  1801. MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
  1802. 30);
  1803. /*
  1804. * sanity check see whether all commands to this device been
  1805. * completed
  1806. */
  1807. if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
  1808. scmd->device->lun, scmd->device->channel))
  1809. r = FAILED;
  1810. else
  1811. r = SUCCESS;
  1812. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1813. mutex_unlock(&ioc->tm_cmds.mutex);
  1814. out:
  1815. printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
  1816. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1817. return r;
  1818. }
  1819. /**
  1820. * _scsih_target_reset - eh threads main target reset routine
  1821. * @sdev: scsi device struct
  1822. *
  1823. * Returns SUCCESS if command aborted else FAILED
  1824. */
  1825. static int
  1826. _scsih_target_reset(struct scsi_cmnd *scmd)
  1827. {
  1828. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1829. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1830. struct _sas_device *sas_device;
  1831. unsigned long flags;
  1832. u16 handle;
  1833. int r;
  1834. printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
  1835. ioc->name, scmd);
  1836. scsi_print_command(scmd);
  1837. sas_device_priv_data = scmd->device->hostdata;
  1838. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1839. printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
  1840. ioc->name, scmd);
  1841. scmd->result = DID_NO_CONNECT << 16;
  1842. scmd->scsi_done(scmd);
  1843. r = SUCCESS;
  1844. goto out;
  1845. }
  1846. /* for hidden raid components obtain the volume_handle */
  1847. handle = 0;
  1848. if (sas_device_priv_data->sas_target->flags &
  1849. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1850. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1851. sas_device = _scsih_sas_device_find_by_handle(ioc,
  1852. sas_device_priv_data->sas_target->handle);
  1853. if (sas_device)
  1854. handle = sas_device->volume_handle;
  1855. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1856. } else
  1857. handle = sas_device_priv_data->sas_target->handle;
  1858. if (!handle) {
  1859. scmd->result = DID_RESET << 16;
  1860. r = FAILED;
  1861. goto out;
  1862. }
  1863. mutex_lock(&ioc->tm_cmds.mutex);
  1864. mpt2sas_scsih_issue_tm(ioc, handle, 0,
  1865. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
  1866. /*
  1867. * sanity check see whether all commands to this target been
  1868. * completed
  1869. */
  1870. if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
  1871. scmd->device->channel))
  1872. r = FAILED;
  1873. else
  1874. r = SUCCESS;
  1875. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1876. mutex_unlock(&ioc->tm_cmds.mutex);
  1877. out:
  1878. printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
  1879. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1880. return r;
  1881. }
  1882. /**
  1883. * _scsih_abort - eh threads main host reset routine
  1884. * @sdev: scsi device struct
  1885. *
  1886. * Returns SUCCESS if command aborted else FAILED
  1887. */
  1888. static int
  1889. _scsih_host_reset(struct scsi_cmnd *scmd)
  1890. {
  1891. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1892. int r, retval;
  1893. printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
  1894. ioc->name, scmd);
  1895. scsi_print_command(scmd);
  1896. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1897. FORCE_BIG_HAMMER);
  1898. r = (retval < 0) ? FAILED : SUCCESS;
  1899. printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
  1900. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1901. return r;
  1902. }
  1903. /**
  1904. * _scsih_fw_event_add - insert and queue up fw_event
  1905. * @ioc: per adapter object
  1906. * @fw_event: object describing the event
  1907. * Context: This function will acquire ioc->fw_event_lock.
  1908. *
  1909. * This adds the firmware event object into link list, then queues it up to
  1910. * be processed from user context.
  1911. *
  1912. * Return nothing.
  1913. */
  1914. static void
  1915. _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  1916. {
  1917. unsigned long flags;
  1918. if (ioc->firmware_event_thread == NULL)
  1919. return;
  1920. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1921. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  1922. INIT_WORK(&fw_event->work, _firmware_event_work);
  1923. queue_work(ioc->firmware_event_thread, &fw_event->work);
  1924. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1925. }
  1926. /**
  1927. * _scsih_fw_event_free - delete fw_event
  1928. * @ioc: per adapter object
  1929. * @fw_event: object describing the event
  1930. * Context: This function will acquire ioc->fw_event_lock.
  1931. *
  1932. * This removes firmware event object from link list, frees associated memory.
  1933. *
  1934. * Return nothing.
  1935. */
  1936. static void
  1937. _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  1938. *fw_event)
  1939. {
  1940. unsigned long flags;
  1941. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1942. list_del(&fw_event->list);
  1943. kfree(fw_event->event_data);
  1944. kfree(fw_event);
  1945. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1946. }
  1947. /**
  1948. * _scsih_fw_event_add - requeue an event
  1949. * @ioc: per adapter object
  1950. * @fw_event: object describing the event
  1951. * Context: This function will acquire ioc->fw_event_lock.
  1952. *
  1953. * Return nothing.
  1954. */
  1955. static void
  1956. _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  1957. *fw_event, unsigned long delay)
  1958. {
  1959. unsigned long flags;
  1960. if (ioc->firmware_event_thread == NULL)
  1961. return;
  1962. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1963. queue_work(ioc->firmware_event_thread, &fw_event->work);
  1964. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1965. }
  1966. /**
  1967. * _scsih_fw_event_off - turn flag off preventing event handling
  1968. * @ioc: per adapter object
  1969. *
  1970. * Used to prevent handling of firmware events during adapter reset
  1971. * driver unload.
  1972. *
  1973. * Return nothing.
  1974. */
  1975. static void
  1976. _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
  1977. {
  1978. unsigned long flags;
  1979. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1980. ioc->fw_events_off = 1;
  1981. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1982. }
  1983. /**
  1984. * _scsih_fw_event_on - turn flag on allowing firmware event handling
  1985. * @ioc: per adapter object
  1986. *
  1987. * Returns nothing.
  1988. */
  1989. static void
  1990. _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
  1991. {
  1992. unsigned long flags;
  1993. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  1994. ioc->fw_events_off = 0;
  1995. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  1996. }
  1997. /**
  1998. * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
  1999. * @ioc: per adapter object
  2000. * @handle: device handle
  2001. *
  2002. * During device pull we need to appropiately set the sdev state.
  2003. */
  2004. static void
  2005. _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2006. {
  2007. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2008. struct scsi_device *sdev;
  2009. shost_for_each_device(sdev, ioc->shost) {
  2010. sas_device_priv_data = sdev->hostdata;
  2011. if (!sas_device_priv_data)
  2012. continue;
  2013. if (!sas_device_priv_data->block)
  2014. continue;
  2015. if (sas_device_priv_data->sas_target->handle == handle) {
  2016. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2017. MPT2SAS_INFO_FMT "SDEV_RUNNING: "
  2018. "handle(0x%04x)\n", ioc->name, handle));
  2019. sas_device_priv_data->block = 0;
  2020. scsi_internal_device_unblock(sdev);
  2021. }
  2022. }
  2023. }
  2024. /**
  2025. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  2026. * @ioc: per adapter object
  2027. * @handle: device handle
  2028. *
  2029. * During device pull we need to appropiately set the sdev state.
  2030. */
  2031. static void
  2032. _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2033. {
  2034. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2035. struct scsi_device *sdev;
  2036. shost_for_each_device(sdev, ioc->shost) {
  2037. sas_device_priv_data = sdev->hostdata;
  2038. if (!sas_device_priv_data)
  2039. continue;
  2040. if (sas_device_priv_data->block)
  2041. continue;
  2042. if (sas_device_priv_data->sas_target->handle == handle) {
  2043. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2044. MPT2SAS_INFO_FMT "SDEV_BLOCK: "
  2045. "handle(0x%04x)\n", ioc->name, handle));
  2046. sas_device_priv_data->block = 1;
  2047. scsi_internal_device_block(sdev);
  2048. }
  2049. }
  2050. }
  2051. /**
  2052. * _scsih_block_io_to_children_attached_to_ex
  2053. * @ioc: per adapter object
  2054. * @sas_expander: the sas_device object
  2055. *
  2056. * This routine set sdev state to SDEV_BLOCK for all devices
  2057. * attached to this expander. This function called when expander is
  2058. * pulled.
  2059. */
  2060. static void
  2061. _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
  2062. struct _sas_node *sas_expander)
  2063. {
  2064. struct _sas_port *mpt2sas_port;
  2065. struct _sas_device *sas_device;
  2066. struct _sas_node *expander_sibling;
  2067. unsigned long flags;
  2068. if (!sas_expander)
  2069. return;
  2070. list_for_each_entry(mpt2sas_port,
  2071. &sas_expander->sas_port_list, port_list) {
  2072. if (mpt2sas_port->remote_identify.device_type ==
  2073. SAS_END_DEVICE) {
  2074. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2075. sas_device =
  2076. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  2077. mpt2sas_port->remote_identify.sas_address);
  2078. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2079. if (!sas_device)
  2080. continue;
  2081. _scsih_block_io_device(ioc, sas_device->handle);
  2082. }
  2083. }
  2084. list_for_each_entry(mpt2sas_port,
  2085. &sas_expander->sas_port_list, port_list) {
  2086. if (mpt2sas_port->remote_identify.device_type ==
  2087. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  2088. mpt2sas_port->remote_identify.device_type ==
  2089. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  2090. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2091. expander_sibling =
  2092. mpt2sas_scsih_expander_find_by_sas_address(
  2093. ioc, mpt2sas_port->remote_identify.sas_address);
  2094. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2095. _scsih_block_io_to_children_attached_to_ex(ioc,
  2096. expander_sibling);
  2097. }
  2098. }
  2099. }
  2100. /**
  2101. * _scsih_block_io_to_children_attached_directly
  2102. * @ioc: per adapter object
  2103. * @event_data: topology change event data
  2104. *
  2105. * This routine set sdev state to SDEV_BLOCK for all devices
  2106. * direct attached during device pull.
  2107. */
  2108. static void
  2109. _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
  2110. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2111. {
  2112. int i;
  2113. u16 handle;
  2114. u16 reason_code;
  2115. u8 phy_number;
  2116. u8 link_rate;
  2117. for (i = 0; i < event_data->NumEntries; i++) {
  2118. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2119. if (!handle)
  2120. continue;
  2121. phy_number = event_data->StartPhyNum + i;
  2122. reason_code = event_data->PHY[i].PhyStatus &
  2123. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2124. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  2125. _scsih_block_io_device(ioc, handle);
  2126. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
  2127. link_rate = event_data->PHY[i].LinkRate >> 4;
  2128. if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
  2129. _scsih_ublock_io_device(ioc, handle);
  2130. }
  2131. }
  2132. }
  2133. /**
  2134. * _scsih_check_topo_delete_events - sanity check on topo events
  2135. * @ioc: per adapter object
  2136. * @event_data: the event data payload
  2137. *
  2138. * This routine added to better handle cable breaker.
  2139. *
  2140. * This handles the case where driver recieves multiple expander
  2141. * add and delete events in a single shot. When there is a delete event
  2142. * the routine will void any pending add events waiting in the event queue.
  2143. *
  2144. * Return nothing.
  2145. */
  2146. static void
  2147. _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
  2148. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2149. {
  2150. struct fw_event_work *fw_event;
  2151. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  2152. u16 expander_handle;
  2153. struct _sas_node *sas_expander;
  2154. unsigned long flags;
  2155. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  2156. if (expander_handle < ioc->sas_hba.num_phys) {
  2157. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2158. return;
  2159. }
  2160. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
  2161. || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
  2162. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2163. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  2164. expander_handle);
  2165. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2166. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  2167. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  2168. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2169. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  2170. return;
  2171. /* mark ignore flag for pending events */
  2172. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2173. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  2174. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  2175. fw_event->ignore)
  2176. continue;
  2177. local_event_data = fw_event->event_data;
  2178. if (local_event_data->ExpStatus ==
  2179. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  2180. local_event_data->ExpStatus ==
  2181. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  2182. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  2183. expander_handle) {
  2184. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  2185. "setting ignoring flag\n", ioc->name));
  2186. fw_event->ignore = 1;
  2187. }
  2188. }
  2189. }
  2190. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2191. }
  2192. /**
  2193. * _scsih_flush_running_cmds - completing outstanding commands.
  2194. * @ioc: per adapter object
  2195. *
  2196. * The flushing out of all pending scmd commands following host reset,
  2197. * where all IO is dropped to the floor.
  2198. *
  2199. * Return nothing.
  2200. */
  2201. static void
  2202. _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
  2203. {
  2204. struct scsi_cmnd *scmd;
  2205. u16 smid;
  2206. u16 count = 0;
  2207. for (smid = 1; smid <= ioc->request_depth; smid++) {
  2208. scmd = _scsih_scsi_lookup_getclear(ioc, smid);
  2209. if (!scmd)
  2210. continue;
  2211. count++;
  2212. mpt2sas_base_free_smid(ioc, smid);
  2213. scsi_dma_unmap(scmd);
  2214. scmd->result = DID_RESET << 16;
  2215. scmd->scsi_done(scmd);
  2216. }
  2217. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
  2218. ioc->name, count));
  2219. }
  2220. /**
  2221. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  2222. * @scmd: pointer to scsi command object
  2223. * @mpi_request: pointer to the SCSI_IO reqest message frame
  2224. *
  2225. * Supporting protection 1 and 3.
  2226. *
  2227. * Returns nothing
  2228. */
  2229. static void
  2230. _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
  2231. {
  2232. u16 eedp_flags;
  2233. unsigned char prot_op = scsi_get_prot_op(scmd);
  2234. unsigned char prot_type = scsi_get_prot_type(scmd);
  2235. if (prot_type == SCSI_PROT_DIF_TYPE0 ||
  2236. prot_type == SCSI_PROT_DIF_TYPE2 ||
  2237. prot_op == SCSI_PROT_NORMAL)
  2238. return;
  2239. if (prot_op == SCSI_PROT_READ_STRIP)
  2240. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  2241. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  2242. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  2243. else
  2244. return;
  2245. mpi_request->EEDPBlockSize = scmd->device->sector_size;
  2246. switch (prot_type) {
  2247. case SCSI_PROT_DIF_TYPE1:
  2248. /*
  2249. * enable ref/guard checking
  2250. * auto increment ref tag
  2251. */
  2252. mpi_request->EEDPFlags = eedp_flags |
  2253. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2254. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2255. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2256. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  2257. cpu_to_be32(scsi_get_lba(scmd));
  2258. break;
  2259. case SCSI_PROT_DIF_TYPE3:
  2260. /*
  2261. * enable guard checking
  2262. */
  2263. mpi_request->EEDPFlags = eedp_flags |
  2264. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2265. break;
  2266. }
  2267. }
  2268. /**
  2269. * _scsih_eedp_error_handling - return sense code for EEDP errors
  2270. * @scmd: pointer to scsi command object
  2271. * @ioc_status: ioc status
  2272. *
  2273. * Returns nothing
  2274. */
  2275. static void
  2276. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  2277. {
  2278. u8 ascq;
  2279. u8 sk;
  2280. u8 host_byte;
  2281. switch (ioc_status) {
  2282. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2283. ascq = 0x01;
  2284. break;
  2285. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2286. ascq = 0x02;
  2287. break;
  2288. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2289. ascq = 0x03;
  2290. break;
  2291. default:
  2292. ascq = 0x00;
  2293. break;
  2294. }
  2295. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2296. sk = ILLEGAL_REQUEST;
  2297. host_byte = DID_ABORT;
  2298. } else {
  2299. sk = ABORTED_COMMAND;
  2300. host_byte = DID_OK;
  2301. }
  2302. scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
  2303. scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
  2304. SAM_STAT_CHECK_CONDITION;
  2305. }
  2306. /**
  2307. * _scsih_qcmd - main scsi request entry point
  2308. * @scmd: pointer to scsi command object
  2309. * @done: function pointer to be invoked on completion
  2310. *
  2311. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  2312. *
  2313. * Returns 0 on success. If there's a failure, return either:
  2314. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  2315. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  2316. */
  2317. static int
  2318. _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
  2319. {
  2320. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2321. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2322. struct MPT2SAS_TARGET *sas_target_priv_data;
  2323. Mpi2SCSIIORequest_t *mpi_request;
  2324. u32 mpi_control;
  2325. u16 smid;
  2326. scmd->scsi_done = done;
  2327. sas_device_priv_data = scmd->device->hostdata;
  2328. if (!sas_device_priv_data) {
  2329. scmd->result = DID_NO_CONNECT << 16;
  2330. scmd->scsi_done(scmd);
  2331. return 0;
  2332. }
  2333. sas_target_priv_data = sas_device_priv_data->sas_target;
  2334. if (!sas_target_priv_data || sas_target_priv_data->handle ==
  2335. MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
  2336. scmd->result = DID_NO_CONNECT << 16;
  2337. scmd->scsi_done(scmd);
  2338. return 0;
  2339. }
  2340. /* see if we are busy with task managment stuff */
  2341. if (sas_target_priv_data->tm_busy)
  2342. return SCSI_MLQUEUE_DEVICE_BUSY;
  2343. else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  2344. return SCSI_MLQUEUE_HOST_BUSY;
  2345. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  2346. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  2347. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2348. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  2349. else
  2350. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  2351. /* set tags */
  2352. if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
  2353. if (scmd->device->tagged_supported) {
  2354. if (scmd->device->ordered_tags)
  2355. mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
  2356. else
  2357. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2358. } else
  2359. /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
  2360. /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
  2361. */
  2362. mpi_control |= (0x500);
  2363. } else
  2364. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2365. if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
  2366. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  2367. smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
  2368. if (!smid) {
  2369. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2370. ioc->name, __func__);
  2371. goto out;
  2372. }
  2373. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2374. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  2375. _scsih_setup_eedp(scmd, mpi_request);
  2376. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2377. if (sas_device_priv_data->sas_target->flags &
  2378. MPT_TARGET_FLAGS_RAID_COMPONENT)
  2379. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  2380. else
  2381. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2382. mpi_request->DevHandle =
  2383. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  2384. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  2385. mpi_request->Control = cpu_to_le32(mpi_control);
  2386. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  2387. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  2388. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  2389. mpi_request->SenseBufferLowAddress =
  2390. (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  2391. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  2392. mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
  2393. MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
  2394. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  2395. mpi_request->LUN);
  2396. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  2397. if (!mpi_request->DataLength) {
  2398. mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
  2399. } else {
  2400. if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
  2401. mpt2sas_base_free_smid(ioc, smid);
  2402. goto out;
  2403. }
  2404. }
  2405. _scsih_scsi_lookup_set(ioc, smid, scmd);
  2406. mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
  2407. sas_device_priv_data->sas_target->handle);
  2408. return 0;
  2409. out:
  2410. return SCSI_MLQUEUE_HOST_BUSY;
  2411. }
  2412. /**
  2413. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  2414. * @sense_buffer: sense data returned by target
  2415. * @data: normalized skey/asc/ascq
  2416. *
  2417. * Return nothing.
  2418. */
  2419. static void
  2420. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  2421. {
  2422. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  2423. /* descriptor format */
  2424. data->skey = sense_buffer[1] & 0x0F;
  2425. data->asc = sense_buffer[2];
  2426. data->ascq = sense_buffer[3];
  2427. } else {
  2428. /* fixed format */
  2429. data->skey = sense_buffer[2] & 0x0F;
  2430. data->asc = sense_buffer[12];
  2431. data->ascq = sense_buffer[13];
  2432. }
  2433. }
  2434. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2435. /**
  2436. * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
  2437. * @ioc: per adapter object
  2438. * @scmd: pointer to scsi command object
  2439. * @mpi_reply: reply mf payload returned from firmware
  2440. *
  2441. * scsi_status - SCSI Status code returned from target device
  2442. * scsi_state - state info associated with SCSI_IO determined by ioc
  2443. * ioc_status - ioc supplied status info
  2444. *
  2445. * Return nothing.
  2446. */
  2447. static void
  2448. _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  2449. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  2450. {
  2451. u32 response_info;
  2452. u8 *response_bytes;
  2453. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  2454. MPI2_IOCSTATUS_MASK;
  2455. u8 scsi_state = mpi_reply->SCSIState;
  2456. u8 scsi_status = mpi_reply->SCSIStatus;
  2457. char *desc_ioc_state = NULL;
  2458. char *desc_scsi_status = NULL;
  2459. char *desc_scsi_state = ioc->tmp_string;
  2460. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  2461. if (log_info == 0x31170000)
  2462. return;
  2463. switch (ioc_status) {
  2464. case MPI2_IOCSTATUS_SUCCESS:
  2465. desc_ioc_state = "success";
  2466. break;
  2467. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2468. desc_ioc_state = "invalid function";
  2469. break;
  2470. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2471. desc_ioc_state = "scsi recovered error";
  2472. break;
  2473. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  2474. desc_ioc_state = "scsi invalid dev handle";
  2475. break;
  2476. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2477. desc_ioc_state = "scsi device not there";
  2478. break;
  2479. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2480. desc_ioc_state = "scsi data overrun";
  2481. break;
  2482. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2483. desc_ioc_state = "scsi data underrun";
  2484. break;
  2485. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2486. desc_ioc_state = "scsi io data error";
  2487. break;
  2488. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2489. desc_ioc_state = "scsi protocol error";
  2490. break;
  2491. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2492. desc_ioc_state = "scsi task terminated";
  2493. break;
  2494. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2495. desc_ioc_state = "scsi residual mismatch";
  2496. break;
  2497. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2498. desc_ioc_state = "scsi task mgmt failed";
  2499. break;
  2500. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2501. desc_ioc_state = "scsi ioc terminated";
  2502. break;
  2503. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2504. desc_ioc_state = "scsi ext terminated";
  2505. break;
  2506. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2507. desc_ioc_state = "eedp guard error";
  2508. break;
  2509. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2510. desc_ioc_state = "eedp ref tag error";
  2511. break;
  2512. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2513. desc_ioc_state = "eedp app tag error";
  2514. break;
  2515. default:
  2516. desc_ioc_state = "unknown";
  2517. break;
  2518. }
  2519. switch (scsi_status) {
  2520. case MPI2_SCSI_STATUS_GOOD:
  2521. desc_scsi_status = "good";
  2522. break;
  2523. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  2524. desc_scsi_status = "check condition";
  2525. break;
  2526. case MPI2_SCSI_STATUS_CONDITION_MET:
  2527. desc_scsi_status = "condition met";
  2528. break;
  2529. case MPI2_SCSI_STATUS_BUSY:
  2530. desc_scsi_status = "busy";
  2531. break;
  2532. case MPI2_SCSI_STATUS_INTERMEDIATE:
  2533. desc_scsi_status = "intermediate";
  2534. break;
  2535. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  2536. desc_scsi_status = "intermediate condmet";
  2537. break;
  2538. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  2539. desc_scsi_status = "reservation conflict";
  2540. break;
  2541. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  2542. desc_scsi_status = "command terminated";
  2543. break;
  2544. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  2545. desc_scsi_status = "task set full";
  2546. break;
  2547. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  2548. desc_scsi_status = "aca active";
  2549. break;
  2550. case MPI2_SCSI_STATUS_TASK_ABORTED:
  2551. desc_scsi_status = "task aborted";
  2552. break;
  2553. default:
  2554. desc_scsi_status = "unknown";
  2555. break;
  2556. }
  2557. desc_scsi_state[0] = '\0';
  2558. if (!scsi_state)
  2559. desc_scsi_state = " ";
  2560. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  2561. strcat(desc_scsi_state, "response info ");
  2562. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2563. strcat(desc_scsi_state, "state terminated ");
  2564. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  2565. strcat(desc_scsi_state, "no status ");
  2566. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  2567. strcat(desc_scsi_state, "autosense failed ");
  2568. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  2569. strcat(desc_scsi_state, "autosense valid ");
  2570. scsi_print_command(scmd);
  2571. printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
  2572. "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
  2573. le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
  2574. ioc_status, smid);
  2575. printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
  2576. "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
  2577. scsi_get_resid(scmd));
  2578. printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
  2579. "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  2580. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  2581. printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
  2582. "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
  2583. scsi_status, desc_scsi_state, scsi_state);
  2584. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2585. struct sense_info data;
  2586. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2587. printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
  2588. "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
  2589. data.asc, data.ascq);
  2590. }
  2591. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  2592. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  2593. response_bytes = (u8 *)&response_info;
  2594. _scsih_response_code(ioc, response_bytes[3]);
  2595. }
  2596. }
  2597. #endif
  2598. /**
  2599. * _scsih_smart_predicted_fault - illuminate Fault LED
  2600. * @ioc: per adapter object
  2601. * @handle: device handle
  2602. *
  2603. * Return nothing.
  2604. */
  2605. static void
  2606. _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2607. {
  2608. Mpi2SepReply_t mpi_reply;
  2609. Mpi2SepRequest_t mpi_request;
  2610. struct scsi_target *starget;
  2611. struct MPT2SAS_TARGET *sas_target_priv_data;
  2612. Mpi2EventNotificationReply_t *event_reply;
  2613. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  2614. struct _sas_device *sas_device;
  2615. ssize_t sz;
  2616. unsigned long flags;
  2617. /* only handle non-raid devices */
  2618. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2619. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  2620. if (!sas_device) {
  2621. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2622. return;
  2623. }
  2624. starget = sas_device->starget;
  2625. sas_target_priv_data = starget->hostdata;
  2626. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  2627. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
  2628. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2629. return;
  2630. }
  2631. starget_printk(KERN_WARNING, starget, "predicted fault\n");
  2632. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2633. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
  2634. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  2635. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  2636. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  2637. mpi_request.SlotStatus =
  2638. MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
  2639. mpi_request.DevHandle = cpu_to_le16(handle);
  2640. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  2641. if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  2642. &mpi_request)) != 0) {
  2643. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2644. ioc->name, __FILE__, __LINE__, __func__);
  2645. return;
  2646. }
  2647. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  2648. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2649. "enclosure_processor: ioc_status (0x%04x), "
  2650. "loginfo(0x%08x)\n", ioc->name,
  2651. le16_to_cpu(mpi_reply.IOCStatus),
  2652. le32_to_cpu(mpi_reply.IOCLogInfo)));
  2653. return;
  2654. }
  2655. }
  2656. /* insert into event log */
  2657. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  2658. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  2659. event_reply = kzalloc(sz, GFP_KERNEL);
  2660. if (!event_reply) {
  2661. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2662. ioc->name, __FILE__, __LINE__, __func__);
  2663. return;
  2664. }
  2665. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  2666. event_reply->Event =
  2667. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  2668. event_reply->MsgLength = sz/4;
  2669. event_reply->EventDataLength =
  2670. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  2671. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  2672. event_reply->EventData;
  2673. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  2674. event_data->ASC = 0x5D;
  2675. event_data->DevHandle = cpu_to_le16(handle);
  2676. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  2677. mpt2sas_ctl_add_to_event_log(ioc, event_reply);
  2678. kfree(event_reply);
  2679. }
  2680. /**
  2681. * _scsih_io_done - scsi request callback
  2682. * @ioc: per adapter object
  2683. * @smid: system request message index
  2684. * @VF_ID: virtual function id
  2685. * @reply: reply message frame(lower 32bit addr)
  2686. *
  2687. * Callback handler when using scsih_qcmd.
  2688. *
  2689. * Return nothing.
  2690. */
  2691. static void
  2692. _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  2693. {
  2694. Mpi2SCSIIORequest_t *mpi_request;
  2695. Mpi2SCSIIOReply_t *mpi_reply;
  2696. struct scsi_cmnd *scmd;
  2697. u16 ioc_status;
  2698. u32 xfer_cnt;
  2699. u8 scsi_state;
  2700. u8 scsi_status;
  2701. u32 log_info;
  2702. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2703. u32 response_code;
  2704. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2705. scmd = _scsih_scsi_lookup_getclear(ioc, smid);
  2706. if (scmd == NULL)
  2707. return;
  2708. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2709. if (mpi_reply == NULL) {
  2710. scmd->result = DID_OK << 16;
  2711. goto out;
  2712. }
  2713. sas_device_priv_data = scmd->device->hostdata;
  2714. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  2715. sas_device_priv_data->sas_target->deleted) {
  2716. scmd->result = DID_NO_CONNECT << 16;
  2717. goto out;
  2718. }
  2719. /* turning off TLR */
  2720. if (!sas_device_priv_data->tlr_snoop_check) {
  2721. sas_device_priv_data->tlr_snoop_check++;
  2722. if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
  2723. response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
  2724. >> 24);
  2725. if (response_code ==
  2726. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
  2727. sas_device_priv_data->flags &=
  2728. ~MPT_DEVICE_TLR_ON;
  2729. }
  2730. }
  2731. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  2732. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  2733. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  2734. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  2735. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  2736. else
  2737. log_info = 0;
  2738. ioc_status &= MPI2_IOCSTATUS_MASK;
  2739. scsi_state = mpi_reply->SCSIState;
  2740. scsi_status = mpi_reply->SCSIStatus;
  2741. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  2742. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  2743. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  2744. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  2745. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  2746. }
  2747. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2748. struct sense_info data;
  2749. const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
  2750. smid);
  2751. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  2752. le32_to_cpu(mpi_reply->SenseCount));
  2753. memcpy(scmd->sense_buffer, sense_data, sz);
  2754. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2755. /* failure prediction threshold exceeded */
  2756. if (data.asc == 0x5D)
  2757. _scsih_smart_predicted_fault(ioc,
  2758. le16_to_cpu(mpi_reply->DevHandle));
  2759. }
  2760. switch (ioc_status) {
  2761. case MPI2_IOCSTATUS_BUSY:
  2762. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  2763. scmd->result = SAM_STAT_BUSY;
  2764. break;
  2765. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2766. scmd->result = DID_NO_CONNECT << 16;
  2767. break;
  2768. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2769. if (sas_device_priv_data->block) {
  2770. scmd->result = (DID_BUS_BUSY << 16);
  2771. break;
  2772. }
  2773. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2774. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2775. scmd->result = DID_RESET << 16;
  2776. break;
  2777. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2778. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  2779. scmd->result = DID_SOFT_ERROR << 16;
  2780. else
  2781. scmd->result = (DID_OK << 16) | scsi_status;
  2782. break;
  2783. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2784. scmd->result = (DID_OK << 16) | scsi_status;
  2785. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  2786. break;
  2787. if (xfer_cnt < scmd->underflow) {
  2788. if (scsi_status == SAM_STAT_BUSY)
  2789. scmd->result = SAM_STAT_BUSY;
  2790. else
  2791. scmd->result = DID_SOFT_ERROR << 16;
  2792. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  2793. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  2794. scmd->result = DID_SOFT_ERROR << 16;
  2795. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2796. scmd->result = DID_RESET << 16;
  2797. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  2798. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  2799. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  2800. scmd->result = (DRIVER_SENSE << 24) |
  2801. SAM_STAT_CHECK_CONDITION;
  2802. scmd->sense_buffer[0] = 0x70;
  2803. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  2804. scmd->sense_buffer[12] = 0x20;
  2805. scmd->sense_buffer[13] = 0;
  2806. }
  2807. break;
  2808. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2809. scsi_set_resid(scmd, 0);
  2810. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2811. case MPI2_IOCSTATUS_SUCCESS:
  2812. scmd->result = (DID_OK << 16) | scsi_status;
  2813. if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  2814. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  2815. scmd->result = DID_SOFT_ERROR << 16;
  2816. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2817. scmd->result = DID_RESET << 16;
  2818. break;
  2819. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2820. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2821. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2822. _scsih_eedp_error_handling(scmd, ioc_status);
  2823. break;
  2824. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2825. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2826. case MPI2_IOCSTATUS_INVALID_SGL:
  2827. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  2828. case MPI2_IOCSTATUS_INVALID_FIELD:
  2829. case MPI2_IOCSTATUS_INVALID_STATE:
  2830. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2831. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2832. default:
  2833. scmd->result = DID_SOFT_ERROR << 16;
  2834. break;
  2835. }
  2836. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2837. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  2838. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  2839. #endif
  2840. out:
  2841. scsi_dma_unmap(scmd);
  2842. scmd->scsi_done(scmd);
  2843. }
  2844. /**
  2845. * _scsih_sas_host_refresh - refreshing sas host object contents
  2846. * @ioc: per adapter object
  2847. * @update: update link information
  2848. * Context: user
  2849. *
  2850. * During port enable, fw will send topology events for every device. Its
  2851. * possible that the handles may change from the previous setting, so this
  2852. * code keeping handles updating if changed.
  2853. *
  2854. * Return nothing.
  2855. */
  2856. static void
  2857. _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
  2858. {
  2859. u16 sz;
  2860. u16 ioc_status;
  2861. int i;
  2862. Mpi2ConfigReply_t mpi_reply;
  2863. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  2864. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  2865. "updating handles for sas_host(0x%016llx)\n",
  2866. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  2867. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  2868. * sizeof(Mpi2SasIOUnit0PhyData_t));
  2869. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  2870. if (!sas_iounit_pg0) {
  2871. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2872. ioc->name, __FILE__, __LINE__, __func__);
  2873. return;
  2874. }
  2875. if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  2876. sas_iounit_pg0, sz))) {
  2877. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2878. MPI2_IOCSTATUS_MASK;
  2879. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  2880. goto out;
  2881. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  2882. ioc->sas_hba.phy[i].handle =
  2883. le16_to_cpu(sas_iounit_pg0->PhyData[i].
  2884. ControllerDevHandle);
  2885. if (update)
  2886. mpt2sas_transport_update_links(
  2887. ioc,
  2888. ioc->sas_hba.phy[i].handle,
  2889. le16_to_cpu(sas_iounit_pg0->PhyData[i].
  2890. AttachedDevHandle), i,
  2891. sas_iounit_pg0->PhyData[i].
  2892. NegotiatedLinkRate >> 4);
  2893. }
  2894. }
  2895. out:
  2896. kfree(sas_iounit_pg0);
  2897. }
  2898. /**
  2899. * _scsih_sas_host_add - create sas host object
  2900. * @ioc: per adapter object
  2901. *
  2902. * Creating host side data object, stored in ioc->sas_hba
  2903. *
  2904. * Return nothing.
  2905. */
  2906. static void
  2907. _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
  2908. {
  2909. int i;
  2910. Mpi2ConfigReply_t mpi_reply;
  2911. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  2912. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  2913. Mpi2SasPhyPage0_t phy_pg0;
  2914. Mpi2SasDevicePage0_t sas_device_pg0;
  2915. Mpi2SasEnclosurePage0_t enclosure_pg0;
  2916. u16 ioc_status;
  2917. u16 sz;
  2918. u16 device_missing_delay;
  2919. mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
  2920. if (!ioc->sas_hba.num_phys) {
  2921. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2922. ioc->name, __FILE__, __LINE__, __func__);
  2923. return;
  2924. }
  2925. /* sas_iounit page 0 */
  2926. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  2927. sizeof(Mpi2SasIOUnit0PhyData_t));
  2928. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  2929. if (!sas_iounit_pg0) {
  2930. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2931. ioc->name, __FILE__, __LINE__, __func__);
  2932. return;
  2933. }
  2934. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  2935. sas_iounit_pg0, sz))) {
  2936. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2937. ioc->name, __FILE__, __LINE__, __func__);
  2938. goto out;
  2939. }
  2940. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2941. MPI2_IOCSTATUS_MASK;
  2942. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2943. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2944. ioc->name, __FILE__, __LINE__, __func__);
  2945. goto out;
  2946. }
  2947. /* sas_iounit page 1 */
  2948. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  2949. sizeof(Mpi2SasIOUnit1PhyData_t));
  2950. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  2951. if (!sas_iounit_pg1) {
  2952. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2953. ioc->name, __FILE__, __LINE__, __func__);
  2954. goto out;
  2955. }
  2956. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  2957. sas_iounit_pg1, sz))) {
  2958. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2959. ioc->name, __FILE__, __LINE__, __func__);
  2960. goto out;
  2961. }
  2962. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2963. MPI2_IOCSTATUS_MASK;
  2964. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2965. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2966. ioc->name, __FILE__, __LINE__, __func__);
  2967. goto out;
  2968. }
  2969. ioc->io_missing_delay =
  2970. le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
  2971. device_missing_delay =
  2972. le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
  2973. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2974. ioc->device_missing_delay = (device_missing_delay &
  2975. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2976. else
  2977. ioc->device_missing_delay = device_missing_delay &
  2978. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2979. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  2980. ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
  2981. sizeof(struct _sas_phy), GFP_KERNEL);
  2982. if (!ioc->sas_hba.phy) {
  2983. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2984. ioc->name, __FILE__, __LINE__, __func__);
  2985. goto out;
  2986. }
  2987. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  2988. if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  2989. i))) {
  2990. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2991. ioc->name, __FILE__, __LINE__, __func__);
  2992. goto out;
  2993. }
  2994. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2995. MPI2_IOCSTATUS_MASK;
  2996. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2997. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  2998. ioc->name, __FILE__, __LINE__, __func__);
  2999. goto out;
  3000. }
  3001. ioc->sas_hba.phy[i].handle =
  3002. le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
  3003. ioc->sas_hba.phy[i].phy_id = i;
  3004. mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  3005. phy_pg0, ioc->sas_hba.parent_dev);
  3006. }
  3007. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3008. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
  3009. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3010. ioc->name, __FILE__, __LINE__, __func__);
  3011. goto out;
  3012. }
  3013. ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
  3014. ioc->sas_hba.enclosure_handle =
  3015. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3016. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3017. printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
  3018. "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
  3019. (unsigned long long) ioc->sas_hba.sas_address,
  3020. ioc->sas_hba.num_phys) ;
  3021. if (ioc->sas_hba.enclosure_handle) {
  3022. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3023. &enclosure_pg0,
  3024. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3025. ioc->sas_hba.enclosure_handle))) {
  3026. ioc->sas_hba.enclosure_logical_id =
  3027. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3028. }
  3029. }
  3030. out:
  3031. kfree(sas_iounit_pg1);
  3032. kfree(sas_iounit_pg0);
  3033. }
  3034. /**
  3035. * _scsih_expander_add - creating expander object
  3036. * @ioc: per adapter object
  3037. * @handle: expander handle
  3038. *
  3039. * Creating expander object, stored in ioc->sas_expander_list.
  3040. *
  3041. * Return 0 for success, else error.
  3042. */
  3043. static int
  3044. _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3045. {
  3046. struct _sas_node *sas_expander;
  3047. Mpi2ConfigReply_t mpi_reply;
  3048. Mpi2ExpanderPage0_t expander_pg0;
  3049. Mpi2ExpanderPage1_t expander_pg1;
  3050. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3051. u32 ioc_status;
  3052. u16 parent_handle;
  3053. __le64 sas_address;
  3054. int i;
  3055. unsigned long flags;
  3056. struct _sas_port *mpt2sas_port = NULL;
  3057. int rc = 0;
  3058. if (!handle)
  3059. return -1;
  3060. if (ioc->shost_recovery)
  3061. return -1;
  3062. if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  3063. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  3064. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3065. ioc->name, __FILE__, __LINE__, __func__);
  3066. return -1;
  3067. }
  3068. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3069. MPI2_IOCSTATUS_MASK;
  3070. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3071. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3072. ioc->name, __FILE__, __LINE__, __func__);
  3073. return -1;
  3074. }
  3075. /* handle out of order topology events */
  3076. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  3077. if (parent_handle >= ioc->sas_hba.num_phys) {
  3078. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3079. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  3080. parent_handle);
  3081. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3082. if (!sas_expander) {
  3083. rc = _scsih_expander_add(ioc, parent_handle);
  3084. if (rc != 0)
  3085. return rc;
  3086. }
  3087. }
  3088. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  3089. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3090. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3091. sas_address);
  3092. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3093. if (sas_expander)
  3094. return 0;
  3095. sas_expander = kzalloc(sizeof(struct _sas_node),
  3096. GFP_KERNEL);
  3097. if (!sas_expander) {
  3098. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3099. ioc->name, __FILE__, __LINE__, __func__);
  3100. return -1;
  3101. }
  3102. sas_expander->handle = handle;
  3103. sas_expander->num_phys = expander_pg0.NumPhys;
  3104. sas_expander->parent_handle = parent_handle;
  3105. sas_expander->enclosure_handle =
  3106. le16_to_cpu(expander_pg0.EnclosureHandle);
  3107. sas_expander->sas_address = sas_address;
  3108. printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
  3109. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  3110. handle, sas_expander->parent_handle, (unsigned long long)
  3111. sas_expander->sas_address, sas_expander->num_phys);
  3112. if (!sas_expander->num_phys)
  3113. goto out_fail;
  3114. sas_expander->phy = kcalloc(sas_expander->num_phys,
  3115. sizeof(struct _sas_phy), GFP_KERNEL);
  3116. if (!sas_expander->phy) {
  3117. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3118. ioc->name, __FILE__, __LINE__, __func__);
  3119. rc = -1;
  3120. goto out_fail;
  3121. }
  3122. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  3123. mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
  3124. sas_expander->parent_handle);
  3125. if (!mpt2sas_port) {
  3126. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3127. ioc->name, __FILE__, __LINE__, __func__);
  3128. rc = -1;
  3129. goto out_fail;
  3130. }
  3131. sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
  3132. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  3133. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  3134. &expander_pg1, i, handle))) {
  3135. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3136. ioc->name, __FILE__, __LINE__, __func__);
  3137. rc = -1;
  3138. goto out_fail;
  3139. }
  3140. sas_expander->phy[i].handle = handle;
  3141. sas_expander->phy[i].phy_id = i;
  3142. if ((mpt2sas_transport_add_expander_phy(ioc,
  3143. &sas_expander->phy[i], expander_pg1,
  3144. sas_expander->parent_dev))) {
  3145. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3146. ioc->name, __FILE__, __LINE__, __func__);
  3147. rc = -1;
  3148. goto out_fail;
  3149. }
  3150. }
  3151. if (sas_expander->enclosure_handle) {
  3152. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3153. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3154. sas_expander->enclosure_handle))) {
  3155. sas_expander->enclosure_logical_id =
  3156. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3157. }
  3158. }
  3159. _scsih_expander_node_add(ioc, sas_expander);
  3160. return 0;
  3161. out_fail:
  3162. if (mpt2sas_port)
  3163. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  3164. sas_expander->parent_handle);
  3165. kfree(sas_expander);
  3166. return rc;
  3167. }
  3168. /**
  3169. * _scsih_expander_remove - removing expander object
  3170. * @ioc: per adapter object
  3171. * @handle: expander handle
  3172. *
  3173. * Return nothing.
  3174. */
  3175. static void
  3176. _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3177. {
  3178. struct _sas_node *sas_expander;
  3179. unsigned long flags;
  3180. if (ioc->shost_recovery)
  3181. return;
  3182. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3183. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
  3184. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3185. _scsih_expander_node_remove(ioc, sas_expander);
  3186. }
  3187. /**
  3188. * _scsih_add_device - creating sas device object
  3189. * @ioc: per adapter object
  3190. * @handle: sas device handle
  3191. * @phy_num: phy number end device attached to
  3192. * @is_pd: is this hidden raid component
  3193. *
  3194. * Creating end device object, stored in ioc->sas_device_list.
  3195. *
  3196. * Returns 0 for success, non-zero for failure.
  3197. */
  3198. static int
  3199. _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
  3200. {
  3201. Mpi2ConfigReply_t mpi_reply;
  3202. Mpi2SasDevicePage0_t sas_device_pg0;
  3203. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3204. struct _sas_device *sas_device;
  3205. u32 ioc_status;
  3206. __le64 sas_address;
  3207. u32 device_info;
  3208. unsigned long flags;
  3209. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3210. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  3211. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3212. ioc->name, __FILE__, __LINE__, __func__);
  3213. return -1;
  3214. }
  3215. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3216. MPI2_IOCSTATUS_MASK;
  3217. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3218. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3219. ioc->name, __FILE__, __LINE__, __func__);
  3220. return -1;
  3221. }
  3222. /* check if device is present */
  3223. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  3224. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  3225. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3226. ioc->name, __FILE__, __LINE__, __func__);
  3227. printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
  3228. ioc->name, le16_to_cpu(sas_device_pg0.Flags));
  3229. return -1;
  3230. }
  3231. /* check if there were any issus with discovery */
  3232. if (sas_device_pg0.AccessStatus ==
  3233. MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
  3234. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3235. ioc->name, __FILE__, __LINE__, __func__);
  3236. printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
  3237. ioc->name, sas_device_pg0.AccessStatus);
  3238. return -1;
  3239. }
  3240. /* check if this is end device */
  3241. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  3242. if (!(_scsih_is_end_device(device_info))) {
  3243. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3244. ioc->name, __FILE__, __LINE__, __func__);
  3245. return -1;
  3246. }
  3247. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3248. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3249. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3250. sas_address);
  3251. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3252. if (sas_device) {
  3253. _scsih_ublock_io_device(ioc, handle);
  3254. return 0;
  3255. }
  3256. sas_device = kzalloc(sizeof(struct _sas_device),
  3257. GFP_KERNEL);
  3258. if (!sas_device) {
  3259. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3260. ioc->name, __FILE__, __LINE__, __func__);
  3261. return -1;
  3262. }
  3263. sas_device->handle = handle;
  3264. sas_device->parent_handle =
  3265. le16_to_cpu(sas_device_pg0.ParentDevHandle);
  3266. sas_device->enclosure_handle =
  3267. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3268. sas_device->slot =
  3269. le16_to_cpu(sas_device_pg0.Slot);
  3270. sas_device->device_info = device_info;
  3271. sas_device->sas_address = sas_address;
  3272. sas_device->hidden_raid_component = is_pd;
  3273. /* get enclosure_logical_id */
  3274. if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
  3275. ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3276. sas_device->enclosure_handle)))
  3277. sas_device->enclosure_logical_id =
  3278. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3279. /* get device name */
  3280. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  3281. if (ioc->wait_for_port_enable_to_complete)
  3282. _scsih_sas_device_init_add(ioc, sas_device);
  3283. else
  3284. _scsih_sas_device_add(ioc, sas_device);
  3285. return 0;
  3286. }
  3287. /**
  3288. * _scsih_remove_device - removing sas device object
  3289. * @ioc: per adapter object
  3290. * @handle: sas device handle
  3291. *
  3292. * Return nothing.
  3293. */
  3294. static void
  3295. _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3296. {
  3297. struct MPT2SAS_TARGET *sas_target_priv_data;
  3298. struct _sas_device *sas_device;
  3299. unsigned long flags;
  3300. Mpi2SasIoUnitControlReply_t mpi_reply;
  3301. Mpi2SasIoUnitControlRequest_t mpi_request;
  3302. u16 device_handle;
  3303. /* lookup sas_device */
  3304. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3305. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3306. if (!sas_device) {
  3307. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3308. return;
  3309. }
  3310. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
  3311. "(0x%04x)\n", ioc->name, __func__, handle));
  3312. if (sas_device->starget && sas_device->starget->hostdata) {
  3313. sas_target_priv_data = sas_device->starget->hostdata;
  3314. sas_target_priv_data->deleted = 1;
  3315. }
  3316. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3317. if (ioc->remove_host)
  3318. goto out;
  3319. /* Target Reset to flush out all the outstanding IO */
  3320. device_handle = (sas_device->hidden_raid_component) ?
  3321. sas_device->volume_handle : handle;
  3322. if (device_handle) {
  3323. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
  3324. "handle(0x%04x)\n", ioc->name, device_handle));
  3325. mutex_lock(&ioc->tm_cmds.mutex);
  3326. mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
  3327. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
  3328. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3329. mutex_unlock(&ioc->tm_cmds.mutex);
  3330. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
  3331. "done: handle(0x%04x)\n", ioc->name, device_handle));
  3332. if (ioc->shost_recovery)
  3333. goto out;
  3334. }
  3335. /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
  3336. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
  3337. "(0x%04x)\n", ioc->name, handle));
  3338. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3339. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3340. mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3341. mpi_request.DevHandle = handle;
  3342. mpi_request.VF_ID = 0;
  3343. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
  3344. &mpi_request)) != 0) {
  3345. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3346. ioc->name, __FILE__, __LINE__, __func__);
  3347. }
  3348. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
  3349. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  3350. le16_to_cpu(mpi_reply.IOCStatus),
  3351. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3352. out:
  3353. _scsih_ublock_io_device(ioc, handle);
  3354. mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
  3355. sas_device->parent_handle);
  3356. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
  3357. "(0x%016llx)\n", ioc->name, sas_device->handle,
  3358. (unsigned long long) sas_device->sas_address);
  3359. _scsih_sas_device_remove(ioc, sas_device);
  3360. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
  3361. "(0x%04x)\n", ioc->name, __func__, handle));
  3362. }
  3363. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3364. /**
  3365. * _scsih_sas_topology_change_event_debug - debug for topology event
  3366. * @ioc: per adapter object
  3367. * @event_data: event data payload
  3368. * Context: user.
  3369. */
  3370. static void
  3371. _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3372. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3373. {
  3374. int i;
  3375. u16 handle;
  3376. u16 reason_code;
  3377. u8 phy_number;
  3378. char *status_str = NULL;
  3379. char link_rate[25];
  3380. switch (event_data->ExpStatus) {
  3381. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  3382. status_str = "add";
  3383. break;
  3384. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  3385. status_str = "remove";
  3386. break;
  3387. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  3388. status_str = "responding";
  3389. break;
  3390. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  3391. status_str = "remove delay";
  3392. break;
  3393. default:
  3394. status_str = "unknown status";
  3395. break;
  3396. }
  3397. printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
  3398. ioc->name, status_str);
  3399. printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
  3400. "start_phy(%02d), count(%d)\n",
  3401. le16_to_cpu(event_data->ExpanderDevHandle),
  3402. le16_to_cpu(event_data->EnclosureHandle),
  3403. event_data->StartPhyNum, event_data->NumEntries);
  3404. for (i = 0; i < event_data->NumEntries; i++) {
  3405. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3406. if (!handle)
  3407. continue;
  3408. phy_number = event_data->StartPhyNum + i;
  3409. reason_code = event_data->PHY[i].PhyStatus &
  3410. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3411. switch (reason_code) {
  3412. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3413. snprintf(link_rate, 25, ": add, link(0x%02x)",
  3414. (event_data->PHY[i].LinkRate >> 4));
  3415. status_str = link_rate;
  3416. break;
  3417. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3418. status_str = ": remove";
  3419. break;
  3420. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  3421. status_str = ": remove_delay";
  3422. break;
  3423. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3424. snprintf(link_rate, 25, ": link(0x%02x)",
  3425. (event_data->PHY[i].LinkRate >> 4));
  3426. status_str = link_rate;
  3427. break;
  3428. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  3429. status_str = ": responding";
  3430. break;
  3431. default:
  3432. status_str = ": unknown";
  3433. break;
  3434. }
  3435. printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
  3436. phy_number, handle, status_str);
  3437. }
  3438. }
  3439. #endif
  3440. /**
  3441. * _scsih_sas_topology_change_event - handle topology changes
  3442. * @ioc: per adapter object
  3443. * @VF_ID:
  3444. * @event_data: event data payload
  3445. * fw_event:
  3446. * Context: user.
  3447. *
  3448. */
  3449. static void
  3450. _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3451. Mpi2EventDataSasTopologyChangeList_t *event_data,
  3452. struct fw_event_work *fw_event)
  3453. {
  3454. int i;
  3455. u16 parent_handle, handle;
  3456. u16 reason_code;
  3457. u8 phy_number;
  3458. struct _sas_node *sas_expander;
  3459. unsigned long flags;
  3460. u8 link_rate_;
  3461. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3462. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3463. _scsih_sas_topology_change_event_debug(ioc, event_data);
  3464. #endif
  3465. if (!ioc->sas_hba.num_phys)
  3466. _scsih_sas_host_add(ioc);
  3467. else
  3468. _scsih_sas_host_refresh(ioc, 0);
  3469. if (fw_event->ignore) {
  3470. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
  3471. "event\n", ioc->name));
  3472. return;
  3473. }
  3474. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3475. /* handle expander add */
  3476. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  3477. if (_scsih_expander_add(ioc, parent_handle) != 0)
  3478. return;
  3479. /* handle siblings events */
  3480. for (i = 0; i < event_data->NumEntries; i++) {
  3481. if (fw_event->ignore) {
  3482. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
  3483. "expander event\n", ioc->name));
  3484. return;
  3485. }
  3486. if (ioc->shost_recovery)
  3487. return;
  3488. if (event_data->PHY[i].PhyStatus &
  3489. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
  3490. continue;
  3491. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3492. if (!handle)
  3493. continue;
  3494. phy_number = event_data->StartPhyNum + i;
  3495. reason_code = event_data->PHY[i].PhyStatus &
  3496. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3497. link_rate_ = event_data->PHY[i].LinkRate >> 4;
  3498. switch (reason_code) {
  3499. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3500. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3501. if (!parent_handle) {
  3502. if (phy_number < ioc->sas_hba.num_phys)
  3503. mpt2sas_transport_update_links(
  3504. ioc,
  3505. ioc->sas_hba.phy[phy_number].handle,
  3506. handle, phy_number, link_rate_);
  3507. } else {
  3508. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3509. sas_expander =
  3510. mpt2sas_scsih_expander_find_by_handle(ioc,
  3511. parent_handle);
  3512. spin_unlock_irqrestore(&ioc->sas_node_lock,
  3513. flags);
  3514. if (sas_expander) {
  3515. if (phy_number < sas_expander->num_phys)
  3516. mpt2sas_transport_update_links(
  3517. ioc,
  3518. sas_expander->
  3519. phy[phy_number].handle,
  3520. handle, phy_number,
  3521. link_rate_);
  3522. }
  3523. }
  3524. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
  3525. if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
  3526. break;
  3527. _scsih_add_device(ioc, handle, phy_number, 0);
  3528. }
  3529. break;
  3530. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3531. _scsih_remove_device(ioc, handle);
  3532. break;
  3533. }
  3534. }
  3535. /* handle expander removal */
  3536. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3537. _scsih_expander_remove(ioc, parent_handle);
  3538. }
  3539. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3540. /**
  3541. * _scsih_sas_device_status_change_event_debug - debug for device event
  3542. * @event_data: event data payload
  3543. * Context: user.
  3544. *
  3545. * Return nothing.
  3546. */
  3547. static void
  3548. _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3549. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  3550. {
  3551. char *reason_str = NULL;
  3552. switch (event_data->ReasonCode) {
  3553. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  3554. reason_str = "smart data";
  3555. break;
  3556. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  3557. reason_str = "unsupported device discovered";
  3558. break;
  3559. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  3560. reason_str = "internal device reset";
  3561. break;
  3562. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  3563. reason_str = "internal task abort";
  3564. break;
  3565. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  3566. reason_str = "internal task abort set";
  3567. break;
  3568. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  3569. reason_str = "internal clear task set";
  3570. break;
  3571. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  3572. reason_str = "internal query task";
  3573. break;
  3574. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  3575. reason_str = "sata init failure";
  3576. break;
  3577. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  3578. reason_str = "internal device reset complete";
  3579. break;
  3580. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  3581. reason_str = "internal task abort complete";
  3582. break;
  3583. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  3584. reason_str = "internal async notification";
  3585. break;
  3586. default:
  3587. reason_str = "unknown reason";
  3588. break;
  3589. }
  3590. printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
  3591. "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
  3592. reason_str, le16_to_cpu(event_data->DevHandle),
  3593. (unsigned long long)le64_to_cpu(event_data->SASAddress));
  3594. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  3595. printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  3596. event_data->ASC, event_data->ASCQ);
  3597. printk(KERN_INFO "\n");
  3598. }
  3599. #endif
  3600. /**
  3601. * _scsih_sas_device_status_change_event - handle device status change
  3602. * @ioc: per adapter object
  3603. * @VF_ID:
  3604. * @event_data: event data payload
  3605. * Context: user.
  3606. *
  3607. * Return nothing.
  3608. */
  3609. static void
  3610. _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3611. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  3612. {
  3613. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3614. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3615. _scsih_sas_device_status_change_event_debug(ioc, event_data);
  3616. #endif
  3617. }
  3618. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3619. /**
  3620. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
  3621. * @ioc: per adapter object
  3622. * @event_data: event data payload
  3623. * Context: user.
  3624. *
  3625. * Return nothing.
  3626. */
  3627. static void
  3628. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3629. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  3630. {
  3631. char *reason_str = NULL;
  3632. switch (event_data->ReasonCode) {
  3633. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  3634. reason_str = "enclosure add";
  3635. break;
  3636. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  3637. reason_str = "enclosure remove";
  3638. break;
  3639. default:
  3640. reason_str = "unknown reason";
  3641. break;
  3642. }
  3643. printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
  3644. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  3645. " number slots(%d)\n", ioc->name, reason_str,
  3646. le16_to_cpu(event_data->EnclosureHandle),
  3647. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  3648. le16_to_cpu(event_data->StartSlot));
  3649. }
  3650. #endif
  3651. /**
  3652. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  3653. * @ioc: per adapter object
  3654. * @VF_ID:
  3655. * @event_data: event data payload
  3656. * Context: user.
  3657. *
  3658. * Return nothing.
  3659. */
  3660. static void
  3661. _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  3662. u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  3663. {
  3664. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3665. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3666. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  3667. event_data);
  3668. #endif
  3669. }
  3670. /**
  3671. * _scsih_sas_broadcast_primative_event - handle broadcast events
  3672. * @ioc: per adapter object
  3673. * @event_data: event data payload
  3674. * Context: user.
  3675. *
  3676. * Return nothing.
  3677. */
  3678. static void
  3679. _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3680. Mpi2EventDataSasBroadcastPrimitive_t *event_data)
  3681. {
  3682. struct scsi_cmnd *scmd;
  3683. u16 smid, handle;
  3684. u32 lun;
  3685. struct MPT2SAS_DEVICE *sas_device_priv_data;
  3686. u32 termination_count;
  3687. u32 query_count;
  3688. Mpi2SCSITaskManagementReply_t *mpi_reply;
  3689. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
  3690. "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
  3691. event_data->PortWidth));
  3692. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
  3693. __func__));
  3694. mutex_lock(&ioc->tm_cmds.mutex);
  3695. termination_count = 0;
  3696. query_count = 0;
  3697. mpi_reply = ioc->tm_cmds.reply;
  3698. for (smid = 1; smid <= ioc->request_depth; smid++) {
  3699. scmd = _scsih_scsi_lookup_get(ioc, smid);
  3700. if (!scmd)
  3701. continue;
  3702. sas_device_priv_data = scmd->device->hostdata;
  3703. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  3704. continue;
  3705. /* skip hidden raid components */
  3706. if (sas_device_priv_data->sas_target->flags &
  3707. MPT_TARGET_FLAGS_RAID_COMPONENT)
  3708. continue;
  3709. /* skip volumes */
  3710. if (sas_device_priv_data->sas_target->flags &
  3711. MPT_TARGET_FLAGS_VOLUME)
  3712. continue;
  3713. handle = sas_device_priv_data->sas_target->handle;
  3714. lun = sas_device_priv_data->lun;
  3715. query_count++;
  3716. mpt2sas_scsih_issue_tm(ioc, handle, lun,
  3717. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
  3718. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3719. if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
  3720. (mpi_reply->ResponseCode ==
  3721. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  3722. mpi_reply->ResponseCode ==
  3723. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  3724. continue;
  3725. mpt2sas_scsih_issue_tm(ioc, handle, lun,
  3726. MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
  3727. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3728. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  3729. }
  3730. ioc->broadcast_aen_busy = 0;
  3731. mutex_unlock(&ioc->tm_cmds.mutex);
  3732. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  3733. "%s - exit, query_count = %d termination_count = %d\n",
  3734. ioc->name, __func__, query_count, termination_count));
  3735. }
  3736. /**
  3737. * _scsih_sas_discovery_event - handle discovery events
  3738. * @ioc: per adapter object
  3739. * @event_data: event data payload
  3740. * Context: user.
  3741. *
  3742. * Return nothing.
  3743. */
  3744. static void
  3745. _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  3746. Mpi2EventDataSasDiscovery_t *event_data)
  3747. {
  3748. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3749. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  3750. printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
  3751. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  3752. "start" : "stop");
  3753. if (event_data->DiscoveryStatus)
  3754. printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
  3755. ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
  3756. printk("\n");
  3757. }
  3758. #endif
  3759. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  3760. !ioc->sas_hba.num_phys)
  3761. _scsih_sas_host_add(ioc);
  3762. }
  3763. /**
  3764. * _scsih_reprobe_lun - reprobing lun
  3765. * @sdev: scsi device struct
  3766. * @no_uld_attach: sdev->no_uld_attach flag setting
  3767. *
  3768. **/
  3769. static void
  3770. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  3771. {
  3772. int rc;
  3773. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  3774. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  3775. sdev->no_uld_attach ? "hidding" : "exposing");
  3776. rc = scsi_device_reprobe(sdev);
  3777. }
  3778. /**
  3779. * _scsih_reprobe_target - reprobing target
  3780. * @starget: scsi target struct
  3781. * @no_uld_attach: sdev->no_uld_attach flag setting
  3782. *
  3783. * Note: no_uld_attach flag determines whether the disk device is attached
  3784. * to block layer. A value of `1` means to not attach.
  3785. **/
  3786. static void
  3787. _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
  3788. {
  3789. struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
  3790. if (no_uld_attach)
  3791. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  3792. else
  3793. sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  3794. starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
  3795. _scsih_reprobe_lun);
  3796. }
  3797. /**
  3798. * _scsih_sas_volume_add - add new volume
  3799. * @ioc: per adapter object
  3800. * @element: IR config element data
  3801. * Context: user.
  3802. *
  3803. * Return nothing.
  3804. */
  3805. static void
  3806. _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
  3807. Mpi2EventIrConfigElement_t *element)
  3808. {
  3809. struct _raid_device *raid_device;
  3810. unsigned long flags;
  3811. u64 wwid;
  3812. u16 handle = le16_to_cpu(element->VolDevHandle);
  3813. int rc;
  3814. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  3815. if (!wwid) {
  3816. printk(MPT2SAS_ERR_FMT
  3817. "failure at %s:%d/%s()!\n", ioc->name,
  3818. __FILE__, __LINE__, __func__);
  3819. return;
  3820. }
  3821. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3822. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  3823. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3824. if (raid_device)
  3825. return;
  3826. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  3827. if (!raid_device) {
  3828. printk(MPT2SAS_ERR_FMT
  3829. "failure at %s:%d/%s()!\n", ioc->name,
  3830. __FILE__, __LINE__, __func__);
  3831. return;
  3832. }
  3833. raid_device->id = ioc->sas_id++;
  3834. raid_device->channel = RAID_CHANNEL;
  3835. raid_device->handle = handle;
  3836. raid_device->wwid = wwid;
  3837. _scsih_raid_device_add(ioc, raid_device);
  3838. if (!ioc->wait_for_port_enable_to_complete) {
  3839. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  3840. raid_device->id, 0);
  3841. if (rc)
  3842. _scsih_raid_device_remove(ioc, raid_device);
  3843. } else
  3844. _scsih_determine_boot_device(ioc, raid_device, 1);
  3845. }
  3846. /**
  3847. * _scsih_sas_volume_delete - delete volume
  3848. * @ioc: per adapter object
  3849. * @element: IR config element data
  3850. * Context: user.
  3851. *
  3852. * Return nothing.
  3853. */
  3854. static void
  3855. _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
  3856. Mpi2EventIrConfigElement_t *element)
  3857. {
  3858. struct _raid_device *raid_device;
  3859. u16 handle = le16_to_cpu(element->VolDevHandle);
  3860. unsigned long flags;
  3861. struct MPT2SAS_TARGET *sas_target_priv_data;
  3862. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3863. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  3864. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3865. if (!raid_device)
  3866. return;
  3867. if (raid_device->starget) {
  3868. sas_target_priv_data = raid_device->starget->hostdata;
  3869. sas_target_priv_data->deleted = 1;
  3870. scsi_remove_target(&raid_device->starget->dev);
  3871. }
  3872. _scsih_raid_device_remove(ioc, raid_device);
  3873. }
  3874. /**
  3875. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  3876. * @ioc: per adapter object
  3877. * @element: IR config element data
  3878. * Context: user.
  3879. *
  3880. * Return nothing.
  3881. */
  3882. static void
  3883. _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
  3884. Mpi2EventIrConfigElement_t *element)
  3885. {
  3886. struct _sas_device *sas_device;
  3887. unsigned long flags;
  3888. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3889. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3890. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3891. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3892. if (!sas_device)
  3893. return;
  3894. /* exposing raid component */
  3895. sas_device->volume_handle = 0;
  3896. sas_device->volume_wwid = 0;
  3897. sas_device->hidden_raid_component = 0;
  3898. _scsih_reprobe_target(sas_device->starget, 0);
  3899. }
  3900. /**
  3901. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  3902. * @ioc: per adapter object
  3903. * @element: IR config element data
  3904. * Context: user.
  3905. *
  3906. * Return nothing.
  3907. */
  3908. static void
  3909. _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
  3910. Mpi2EventIrConfigElement_t *element)
  3911. {
  3912. struct _sas_device *sas_device;
  3913. unsigned long flags;
  3914. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3915. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3916. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3917. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3918. if (!sas_device)
  3919. return;
  3920. /* hiding raid component */
  3921. mpt2sas_config_get_volume_handle(ioc, handle,
  3922. &sas_device->volume_handle);
  3923. mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
  3924. &sas_device->volume_wwid);
  3925. sas_device->hidden_raid_component = 1;
  3926. _scsih_reprobe_target(sas_device->starget, 1);
  3927. }
  3928. /**
  3929. * _scsih_sas_pd_delete - delete pd component
  3930. * @ioc: per adapter object
  3931. * @element: IR config element data
  3932. * Context: user.
  3933. *
  3934. * Return nothing.
  3935. */
  3936. static void
  3937. _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
  3938. Mpi2EventIrConfigElement_t *element)
  3939. {
  3940. struct _sas_device *sas_device;
  3941. unsigned long flags;
  3942. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3943. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3944. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3945. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3946. if (!sas_device)
  3947. return;
  3948. _scsih_remove_device(ioc, handle);
  3949. }
  3950. /**
  3951. * _scsih_sas_pd_add - remove pd component
  3952. * @ioc: per adapter object
  3953. * @element: IR config element data
  3954. * Context: user.
  3955. *
  3956. * Return nothing.
  3957. */
  3958. static void
  3959. _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
  3960. Mpi2EventIrConfigElement_t *element)
  3961. {
  3962. struct _sas_device *sas_device;
  3963. unsigned long flags;
  3964. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  3965. Mpi2ConfigReply_t mpi_reply;
  3966. Mpi2SasDevicePage0_t sas_device_pg0;
  3967. u32 ioc_status;
  3968. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3969. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3970. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3971. if (sas_device) {
  3972. sas_device->hidden_raid_component = 1;
  3973. return;
  3974. }
  3975. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3976. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  3977. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3978. ioc->name, __FILE__, __LINE__, __func__);
  3979. return;
  3980. }
  3981. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3982. MPI2_IOCSTATUS_MASK;
  3983. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3984. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3985. ioc->name, __FILE__, __LINE__, __func__);
  3986. return;
  3987. }
  3988. mpt2sas_transport_update_links(ioc,
  3989. le16_to_cpu(sas_device_pg0.ParentDevHandle),
  3990. handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  3991. _scsih_add_device(ioc, handle, 0, 1);
  3992. }
  3993. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3994. /**
  3995. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  3996. * @ioc: per adapter object
  3997. * @event_data: event data payload
  3998. * Context: user.
  3999. *
  4000. * Return nothing.
  4001. */
  4002. static void
  4003. _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4004. Mpi2EventDataIrConfigChangeList_t *event_data)
  4005. {
  4006. Mpi2EventIrConfigElement_t *element;
  4007. u8 element_type;
  4008. int i;
  4009. char *reason_str = NULL, *element_str = NULL;
  4010. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4011. printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
  4012. ioc->name, (le32_to_cpu(event_data->Flags) &
  4013. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  4014. "foreign" : "native", event_data->NumElements);
  4015. for (i = 0; i < event_data->NumElements; i++, element++) {
  4016. switch (element->ReasonCode) {
  4017. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4018. reason_str = "add";
  4019. break;
  4020. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4021. reason_str = "remove";
  4022. break;
  4023. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  4024. reason_str = "no change";
  4025. break;
  4026. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4027. reason_str = "hide";
  4028. break;
  4029. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4030. reason_str = "unhide";
  4031. break;
  4032. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4033. reason_str = "volume_created";
  4034. break;
  4035. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4036. reason_str = "volume_deleted";
  4037. break;
  4038. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4039. reason_str = "pd_created";
  4040. break;
  4041. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4042. reason_str = "pd_deleted";
  4043. break;
  4044. default:
  4045. reason_str = "unknown reason";
  4046. break;
  4047. }
  4048. element_type = le16_to_cpu(element->ElementFlags) &
  4049. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  4050. switch (element_type) {
  4051. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  4052. element_str = "volume";
  4053. break;
  4054. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  4055. element_str = "phys disk";
  4056. break;
  4057. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  4058. element_str = "hot spare";
  4059. break;
  4060. default:
  4061. element_str = "unknown element";
  4062. break;
  4063. }
  4064. printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
  4065. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  4066. reason_str, le16_to_cpu(element->VolDevHandle),
  4067. le16_to_cpu(element->PhysDiskDevHandle),
  4068. element->PhysDiskNum);
  4069. }
  4070. }
  4071. #endif
  4072. /**
  4073. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  4074. * @ioc: per adapter object
  4075. * @VF_ID:
  4076. * @event_data: event data payload
  4077. * Context: user.
  4078. *
  4079. * Return nothing.
  4080. */
  4081. static void
  4082. _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4083. Mpi2EventDataIrConfigChangeList_t *event_data)
  4084. {
  4085. Mpi2EventIrConfigElement_t *element;
  4086. int i;
  4087. u8 foreign_config;
  4088. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4089. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4090. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  4091. #endif
  4092. foreign_config = (le32_to_cpu(event_data->Flags) &
  4093. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  4094. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4095. for (i = 0; i < event_data->NumElements; i++, element++) {
  4096. switch (element->ReasonCode) {
  4097. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4098. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4099. if (!foreign_config)
  4100. _scsih_sas_volume_add(ioc, element);
  4101. break;
  4102. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4103. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4104. if (!foreign_config)
  4105. _scsih_sas_volume_delete(ioc, element);
  4106. break;
  4107. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4108. _scsih_sas_pd_hide(ioc, element);
  4109. break;
  4110. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4111. _scsih_sas_pd_expose(ioc, element);
  4112. break;
  4113. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4114. _scsih_sas_pd_add(ioc, element);
  4115. break;
  4116. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4117. _scsih_sas_pd_delete(ioc, element);
  4118. break;
  4119. }
  4120. }
  4121. }
  4122. /**
  4123. * _scsih_sas_ir_volume_event - IR volume event
  4124. * @ioc: per adapter object
  4125. * @event_data: event data payload
  4126. * Context: user.
  4127. *
  4128. * Return nothing.
  4129. */
  4130. static void
  4131. _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4132. Mpi2EventDataIrVolume_t *event_data)
  4133. {
  4134. u64 wwid;
  4135. unsigned long flags;
  4136. struct _raid_device *raid_device;
  4137. u16 handle;
  4138. u32 state;
  4139. int rc;
  4140. struct MPT2SAS_TARGET *sas_target_priv_data;
  4141. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  4142. return;
  4143. handle = le16_to_cpu(event_data->VolDevHandle);
  4144. state = le32_to_cpu(event_data->NewValue);
  4145. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4146. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4147. le32_to_cpu(event_data->PreviousValue), state));
  4148. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4149. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4150. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4151. switch (state) {
  4152. case MPI2_RAID_VOL_STATE_MISSING:
  4153. case MPI2_RAID_VOL_STATE_FAILED:
  4154. if (!raid_device)
  4155. break;
  4156. if (raid_device->starget) {
  4157. sas_target_priv_data = raid_device->starget->hostdata;
  4158. sas_target_priv_data->deleted = 1;
  4159. scsi_remove_target(&raid_device->starget->dev);
  4160. }
  4161. _scsih_raid_device_remove(ioc, raid_device);
  4162. break;
  4163. case MPI2_RAID_VOL_STATE_ONLINE:
  4164. case MPI2_RAID_VOL_STATE_DEGRADED:
  4165. case MPI2_RAID_VOL_STATE_OPTIMAL:
  4166. if (raid_device)
  4167. break;
  4168. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  4169. if (!wwid) {
  4170. printk(MPT2SAS_ERR_FMT
  4171. "failure at %s:%d/%s()!\n", ioc->name,
  4172. __FILE__, __LINE__, __func__);
  4173. break;
  4174. }
  4175. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  4176. if (!raid_device) {
  4177. printk(MPT2SAS_ERR_FMT
  4178. "failure at %s:%d/%s()!\n", ioc->name,
  4179. __FILE__, __LINE__, __func__);
  4180. break;
  4181. }
  4182. raid_device->id = ioc->sas_id++;
  4183. raid_device->channel = RAID_CHANNEL;
  4184. raid_device->handle = handle;
  4185. raid_device->wwid = wwid;
  4186. _scsih_raid_device_add(ioc, raid_device);
  4187. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4188. raid_device->id, 0);
  4189. if (rc)
  4190. _scsih_raid_device_remove(ioc, raid_device);
  4191. break;
  4192. case MPI2_RAID_VOL_STATE_INITIALIZING:
  4193. default:
  4194. break;
  4195. }
  4196. }
  4197. /**
  4198. * _scsih_sas_ir_physical_disk_event - PD event
  4199. * @ioc: per adapter object
  4200. * @event_data: event data payload
  4201. * Context: user.
  4202. *
  4203. * Return nothing.
  4204. */
  4205. static void
  4206. _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4207. Mpi2EventDataIrPhysicalDisk_t *event_data)
  4208. {
  4209. u16 handle;
  4210. u32 state;
  4211. struct _sas_device *sas_device;
  4212. unsigned long flags;
  4213. Mpi2ConfigReply_t mpi_reply;
  4214. Mpi2SasDevicePage0_t sas_device_pg0;
  4215. u32 ioc_status;
  4216. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  4217. return;
  4218. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  4219. state = le32_to_cpu(event_data->NewValue);
  4220. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4221. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4222. le32_to_cpu(event_data->PreviousValue), state));
  4223. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4224. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4225. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4226. switch (state) {
  4227. case MPI2_RAID_PD_STATE_ONLINE:
  4228. case MPI2_RAID_PD_STATE_DEGRADED:
  4229. case MPI2_RAID_PD_STATE_REBUILDING:
  4230. case MPI2_RAID_PD_STATE_OPTIMAL:
  4231. if (sas_device) {
  4232. sas_device->hidden_raid_component = 1;
  4233. return;
  4234. }
  4235. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  4236. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  4237. handle))) {
  4238. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4239. ioc->name, __FILE__, __LINE__, __func__);
  4240. return;
  4241. }
  4242. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4243. MPI2_IOCSTATUS_MASK;
  4244. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4245. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4246. ioc->name, __FILE__, __LINE__, __func__);
  4247. return;
  4248. }
  4249. mpt2sas_transport_update_links(ioc,
  4250. le16_to_cpu(sas_device_pg0.ParentDevHandle),
  4251. handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  4252. _scsih_add_device(ioc, handle, 0, 1);
  4253. break;
  4254. case MPI2_RAID_PD_STATE_OFFLINE:
  4255. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  4256. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  4257. case MPI2_RAID_PD_STATE_HOT_SPARE:
  4258. default:
  4259. break;
  4260. }
  4261. }
  4262. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4263. /**
  4264. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  4265. * @ioc: per adapter object
  4266. * @event_data: event data payload
  4267. * Context: user.
  4268. *
  4269. * Return nothing.
  4270. */
  4271. static void
  4272. _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4273. Mpi2EventDataIrOperationStatus_t *event_data)
  4274. {
  4275. char *reason_str = NULL;
  4276. switch (event_data->RAIDOperation) {
  4277. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  4278. reason_str = "resync";
  4279. break;
  4280. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  4281. reason_str = "online capacity expansion";
  4282. break;
  4283. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  4284. reason_str = "consistency check";
  4285. break;
  4286. default:
  4287. reason_str = "unknown reason";
  4288. break;
  4289. }
  4290. printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
  4291. "\thandle(0x%04x), percent complete(%d)\n",
  4292. ioc->name, reason_str,
  4293. le16_to_cpu(event_data->VolDevHandle),
  4294. event_data->PercentComplete);
  4295. }
  4296. #endif
  4297. /**
  4298. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  4299. * @ioc: per adapter object
  4300. * @VF_ID:
  4301. * @event_data: event data payload
  4302. * Context: user.
  4303. *
  4304. * Return nothing.
  4305. */
  4306. static void
  4307. _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4308. Mpi2EventDataIrOperationStatus_t *event_data)
  4309. {
  4310. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4311. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4312. _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
  4313. #endif
  4314. }
  4315. /**
  4316. * _scsih_task_set_full - handle task set full
  4317. * @ioc: per adapter object
  4318. * @event_data: event data payload
  4319. * Context: user.
  4320. *
  4321. * Throttle back qdepth.
  4322. */
  4323. static void
  4324. _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
  4325. Mpi2EventDataTaskSetFull_t *event_data)
  4326. {
  4327. unsigned long flags;
  4328. struct _sas_device *sas_device;
  4329. static struct _raid_device *raid_device;
  4330. struct scsi_device *sdev;
  4331. int depth;
  4332. u16 current_depth;
  4333. u16 handle;
  4334. int id, channel;
  4335. u64 sas_address;
  4336. current_depth = le16_to_cpu(event_data->CurrentDepth);
  4337. handle = le16_to_cpu(event_data->DevHandle);
  4338. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4339. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4340. if (!sas_device) {
  4341. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4342. return;
  4343. }
  4344. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4345. id = sas_device->id;
  4346. channel = sas_device->channel;
  4347. sas_address = sas_device->sas_address;
  4348. /* if hidden raid component, then change to volume characteristics */
  4349. if (sas_device->hidden_raid_component && sas_device->volume_handle) {
  4350. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4351. raid_device = _scsih_raid_device_find_by_handle(
  4352. ioc, sas_device->volume_handle);
  4353. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4354. if (raid_device) {
  4355. id = raid_device->id;
  4356. channel = raid_device->channel;
  4357. handle = raid_device->handle;
  4358. sas_address = raid_device->wwid;
  4359. }
  4360. }
  4361. if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
  4362. starget_printk(KERN_DEBUG, sas_device->starget, "task set "
  4363. "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
  4364. handle, (unsigned long long)sas_address, current_depth);
  4365. shost_for_each_device(sdev, ioc->shost) {
  4366. if (sdev->id == id && sdev->channel == channel) {
  4367. if (current_depth > sdev->queue_depth) {
  4368. if (ioc->logging_level &
  4369. MPT_DEBUG_TASK_SET_FULL)
  4370. sdev_printk(KERN_INFO, sdev, "strange "
  4371. "observation, the queue depth is"
  4372. " (%d) meanwhile fw queue depth "
  4373. "is (%d)\n", sdev->queue_depth,
  4374. current_depth);
  4375. continue;
  4376. }
  4377. depth = scsi_track_queue_full(sdev,
  4378. current_depth - 1);
  4379. if (depth > 0)
  4380. sdev_printk(KERN_INFO, sdev, "Queue depth "
  4381. "reduced to (%d)\n", depth);
  4382. else if (depth < 0)
  4383. sdev_printk(KERN_INFO, sdev, "Tagged Command "
  4384. "Queueing is being disabled\n");
  4385. else if (depth == 0)
  4386. if (ioc->logging_level &
  4387. MPT_DEBUG_TASK_SET_FULL)
  4388. sdev_printk(KERN_INFO, sdev,
  4389. "Queue depth not changed yet\n");
  4390. }
  4391. }
  4392. }
  4393. /**
  4394. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  4395. * @ioc: per adapter object
  4396. * @sas_address: sas address
  4397. * @slot: enclosure slot id
  4398. * @handle: device handle
  4399. *
  4400. * After host reset, find out whether devices are still responding.
  4401. * Used in _scsi_remove_unresponsive_sas_devices.
  4402. *
  4403. * Return nothing.
  4404. */
  4405. static void
  4406. _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4407. u16 slot, u16 handle)
  4408. {
  4409. struct MPT2SAS_TARGET *sas_target_priv_data;
  4410. struct scsi_target *starget;
  4411. struct _sas_device *sas_device;
  4412. unsigned long flags;
  4413. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4414. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  4415. if (sas_device->sas_address == sas_address &&
  4416. sas_device->slot == slot && sas_device->starget) {
  4417. sas_device->responding = 1;
  4418. starget_printk(KERN_INFO, sas_device->starget,
  4419. "handle(0x%04x), sas_addr(0x%016llx), enclosure "
  4420. "logical id(0x%016llx), slot(%d)\n", handle,
  4421. (unsigned long long)sas_device->sas_address,
  4422. (unsigned long long)
  4423. sas_device->enclosure_logical_id,
  4424. sas_device->slot);
  4425. if (sas_device->handle == handle)
  4426. goto out;
  4427. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  4428. sas_device->handle);
  4429. sas_device->handle = handle;
  4430. starget = sas_device->starget;
  4431. sas_target_priv_data = starget->hostdata;
  4432. sas_target_priv_data->handle = handle;
  4433. goto out;
  4434. }
  4435. }
  4436. out:
  4437. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4438. }
  4439. /**
  4440. * _scsih_search_responding_sas_devices -
  4441. * @ioc: per adapter object
  4442. *
  4443. * After host reset, find out whether devices are still responding.
  4444. * If not remove.
  4445. *
  4446. * Return nothing.
  4447. */
  4448. static void
  4449. _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  4450. {
  4451. Mpi2SasDevicePage0_t sas_device_pg0;
  4452. Mpi2ConfigReply_t mpi_reply;
  4453. u16 ioc_status;
  4454. __le64 sas_address;
  4455. u16 handle;
  4456. u32 device_info;
  4457. u16 slot;
  4458. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4459. if (list_empty(&ioc->sas_device_list))
  4460. return;
  4461. handle = 0xFFFF;
  4462. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  4463. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  4464. handle))) {
  4465. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4466. MPI2_IOCSTATUS_MASK;
  4467. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4468. break;
  4469. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  4470. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4471. if (!(_scsih_is_end_device(device_info)))
  4472. continue;
  4473. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4474. slot = le16_to_cpu(sas_device_pg0.Slot);
  4475. _scsih_mark_responding_sas_device(ioc, sas_address, slot,
  4476. handle);
  4477. }
  4478. }
  4479. /**
  4480. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  4481. * @ioc: per adapter object
  4482. * @wwid: world wide identifier for raid volume
  4483. * @handle: device handle
  4484. *
  4485. * After host reset, find out whether devices are still responding.
  4486. * Used in _scsi_remove_unresponsive_raid_devices.
  4487. *
  4488. * Return nothing.
  4489. */
  4490. static void
  4491. _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
  4492. u16 handle)
  4493. {
  4494. struct MPT2SAS_TARGET *sas_target_priv_data;
  4495. struct scsi_target *starget;
  4496. struct _raid_device *raid_device;
  4497. unsigned long flags;
  4498. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4499. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  4500. if (raid_device->wwid == wwid && raid_device->starget) {
  4501. raid_device->responding = 1;
  4502. starget_printk(KERN_INFO, raid_device->starget,
  4503. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  4504. (unsigned long long)raid_device->wwid);
  4505. if (raid_device->handle == handle)
  4506. goto out;
  4507. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  4508. raid_device->handle);
  4509. raid_device->handle = handle;
  4510. starget = raid_device->starget;
  4511. sas_target_priv_data = starget->hostdata;
  4512. sas_target_priv_data->handle = handle;
  4513. goto out;
  4514. }
  4515. }
  4516. out:
  4517. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4518. }
  4519. /**
  4520. * _scsih_search_responding_raid_devices -
  4521. * @ioc: per adapter object
  4522. *
  4523. * After host reset, find out whether devices are still responding.
  4524. * If not remove.
  4525. *
  4526. * Return nothing.
  4527. */
  4528. static void
  4529. _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
  4530. {
  4531. Mpi2RaidVolPage1_t volume_pg1;
  4532. Mpi2ConfigReply_t mpi_reply;
  4533. u16 ioc_status;
  4534. u16 handle;
  4535. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4536. if (list_empty(&ioc->raid_device_list))
  4537. return;
  4538. handle = 0xFFFF;
  4539. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  4540. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  4541. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4542. MPI2_IOCSTATUS_MASK;
  4543. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4544. break;
  4545. handle = le16_to_cpu(volume_pg1.DevHandle);
  4546. _scsih_mark_responding_raid_device(ioc,
  4547. le64_to_cpu(volume_pg1.WWID), handle);
  4548. }
  4549. }
  4550. /**
  4551. * _scsih_mark_responding_expander - mark a expander as responding
  4552. * @ioc: per adapter object
  4553. * @sas_address: sas address
  4554. * @handle:
  4555. *
  4556. * After host reset, find out whether devices are still responding.
  4557. * Used in _scsi_remove_unresponsive_expanders.
  4558. *
  4559. * Return nothing.
  4560. */
  4561. static void
  4562. _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4563. u16 handle)
  4564. {
  4565. struct _sas_node *sas_expander;
  4566. unsigned long flags;
  4567. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4568. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  4569. if (sas_expander->sas_address == sas_address) {
  4570. sas_expander->responding = 1;
  4571. if (sas_expander->handle != handle) {
  4572. printk(KERN_INFO "old handle(0x%04x)\n",
  4573. sas_expander->handle);
  4574. sas_expander->handle = handle;
  4575. }
  4576. goto out;
  4577. }
  4578. }
  4579. out:
  4580. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4581. }
  4582. /**
  4583. * _scsih_search_responding_expanders -
  4584. * @ioc: per adapter object
  4585. *
  4586. * After host reset, find out whether devices are still responding.
  4587. * If not remove.
  4588. *
  4589. * Return nothing.
  4590. */
  4591. static void
  4592. _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
  4593. {
  4594. Mpi2ExpanderPage0_t expander_pg0;
  4595. Mpi2ConfigReply_t mpi_reply;
  4596. u16 ioc_status;
  4597. __le64 sas_address;
  4598. u16 handle;
  4599. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  4600. if (list_empty(&ioc->sas_expander_list))
  4601. return;
  4602. handle = 0xFFFF;
  4603. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  4604. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  4605. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4606. MPI2_IOCSTATUS_MASK;
  4607. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  4608. break;
  4609. handle = le16_to_cpu(expander_pg0.DevHandle);
  4610. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  4611. printk(KERN_INFO "\texpander present: handle(0x%04x), "
  4612. "sas_addr(0x%016llx)\n", handle,
  4613. (unsigned long long)sas_address);
  4614. _scsih_mark_responding_expander(ioc, sas_address, handle);
  4615. }
  4616. }
  4617. /**
  4618. * _scsih_remove_unresponding_devices - removing unresponding devices
  4619. * @ioc: per adapter object
  4620. *
  4621. * Return nothing.
  4622. */
  4623. static void
  4624. _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
  4625. {
  4626. struct _sas_device *sas_device, *sas_device_next;
  4627. struct _sas_node *sas_expander;
  4628. struct _raid_device *raid_device, *raid_device_next;
  4629. list_for_each_entry_safe(sas_device, sas_device_next,
  4630. &ioc->sas_device_list, list) {
  4631. if (sas_device->responding) {
  4632. sas_device->responding = 0;
  4633. continue;
  4634. }
  4635. if (sas_device->starget)
  4636. starget_printk(KERN_INFO, sas_device->starget,
  4637. "removing: handle(0x%04x), sas_addr(0x%016llx), "
  4638. "enclosure logical id(0x%016llx), slot(%d)\n",
  4639. sas_device->handle,
  4640. (unsigned long long)sas_device->sas_address,
  4641. (unsigned long long)
  4642. sas_device->enclosure_logical_id,
  4643. sas_device->slot);
  4644. _scsih_remove_device(ioc, sas_device->handle);
  4645. }
  4646. list_for_each_entry_safe(raid_device, raid_device_next,
  4647. &ioc->raid_device_list, list) {
  4648. if (raid_device->responding) {
  4649. raid_device->responding = 0;
  4650. continue;
  4651. }
  4652. if (raid_device->starget) {
  4653. starget_printk(KERN_INFO, raid_device->starget,
  4654. "removing: handle(0x%04x), wwid(0x%016llx)\n",
  4655. raid_device->handle,
  4656. (unsigned long long)raid_device->wwid);
  4657. scsi_remove_target(&raid_device->starget->dev);
  4658. }
  4659. _scsih_raid_device_remove(ioc, raid_device);
  4660. }
  4661. retry_expander_search:
  4662. sas_expander = NULL;
  4663. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  4664. if (sas_expander->responding) {
  4665. sas_expander->responding = 0;
  4666. continue;
  4667. }
  4668. _scsih_expander_remove(ioc, sas_expander->handle);
  4669. goto retry_expander_search;
  4670. }
  4671. }
  4672. /**
  4673. * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
  4674. * @ioc: per adapter object
  4675. * @reset_phase: phase
  4676. *
  4677. * The handler for doing any required cleanup or initialization.
  4678. *
  4679. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  4680. * MPT2_IOC_DONE_RESET
  4681. *
  4682. * Return nothing.
  4683. */
  4684. void
  4685. mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  4686. {
  4687. switch (reset_phase) {
  4688. case MPT2_IOC_PRE_RESET:
  4689. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4690. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  4691. _scsih_fw_event_off(ioc);
  4692. break;
  4693. case MPT2_IOC_AFTER_RESET:
  4694. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4695. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  4696. if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
  4697. ioc->tm_cmds.status |= MPT2_CMD_RESET;
  4698. mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  4699. complete(&ioc->tm_cmds.done);
  4700. }
  4701. _scsih_fw_event_on(ioc);
  4702. _scsih_flush_running_cmds(ioc);
  4703. break;
  4704. case MPT2_IOC_DONE_RESET:
  4705. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4706. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  4707. _scsih_sas_host_refresh(ioc, 0);
  4708. _scsih_search_responding_sas_devices(ioc);
  4709. _scsih_search_responding_raid_devices(ioc);
  4710. _scsih_search_responding_expanders(ioc);
  4711. break;
  4712. case MPT2_IOC_RUNNING:
  4713. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  4714. "MPT2_IOC_RUNNING\n", ioc->name, __func__));
  4715. _scsih_remove_unresponding_devices(ioc);
  4716. break;
  4717. }
  4718. }
  4719. /**
  4720. * _firmware_event_work - delayed task for processing firmware events
  4721. * @ioc: per adapter object
  4722. * @work: equal to the fw_event_work object
  4723. * Context: user.
  4724. *
  4725. * Return nothing.
  4726. */
  4727. static void
  4728. _firmware_event_work(struct work_struct *work)
  4729. {
  4730. struct fw_event_work *fw_event = container_of(work,
  4731. struct fw_event_work, work);
  4732. unsigned long flags;
  4733. struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
  4734. /* the queue is being flushed so ignore this event */
  4735. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4736. if (ioc->fw_events_off || ioc->remove_host) {
  4737. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4738. _scsih_fw_event_free(ioc, fw_event);
  4739. return;
  4740. }
  4741. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4742. if (ioc->shost_recovery) {
  4743. _scsih_fw_event_requeue(ioc, fw_event, 1000);
  4744. return;
  4745. }
  4746. switch (fw_event->event) {
  4747. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  4748. _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
  4749. fw_event->event_data, fw_event);
  4750. break;
  4751. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4752. _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
  4753. fw_event->event_data);
  4754. break;
  4755. case MPI2_EVENT_SAS_DISCOVERY:
  4756. _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
  4757. fw_event->event_data);
  4758. break;
  4759. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  4760. _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
  4761. fw_event->event_data);
  4762. break;
  4763. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  4764. _scsih_sas_enclosure_dev_status_change_event(ioc,
  4765. fw_event->VF_ID, fw_event->event_data);
  4766. break;
  4767. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  4768. _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
  4769. fw_event->event_data);
  4770. break;
  4771. case MPI2_EVENT_IR_VOLUME:
  4772. _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
  4773. fw_event->event_data);
  4774. break;
  4775. case MPI2_EVENT_IR_PHYSICAL_DISK:
  4776. _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
  4777. fw_event->event_data);
  4778. break;
  4779. case MPI2_EVENT_IR_OPERATION_STATUS:
  4780. _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
  4781. fw_event->event_data);
  4782. break;
  4783. case MPI2_EVENT_TASK_SET_FULL:
  4784. _scsih_task_set_full(ioc, fw_event->VF_ID,
  4785. fw_event->event_data);
  4786. break;
  4787. }
  4788. _scsih_fw_event_free(ioc, fw_event);
  4789. }
  4790. /**
  4791. * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  4792. * @ioc: per adapter object
  4793. * @VF_ID: virtual function id
  4794. * @reply: reply message frame(lower 32bit addr)
  4795. * Context: interrupt.
  4796. *
  4797. * This function merely adds a new work task into ioc->firmware_event_thread.
  4798. * The tasks are worked from _firmware_event_work in user context.
  4799. *
  4800. * Return nothing.
  4801. */
  4802. void
  4803. mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
  4804. {
  4805. struct fw_event_work *fw_event;
  4806. Mpi2EventNotificationReply_t *mpi_reply;
  4807. unsigned long flags;
  4808. u16 event;
  4809. /* events turned off due to host reset or driver unloading */
  4810. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4811. if (ioc->fw_events_off || ioc->remove_host) {
  4812. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4813. return;
  4814. }
  4815. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4816. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  4817. event = le16_to_cpu(mpi_reply->Event);
  4818. switch (event) {
  4819. /* handle these */
  4820. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  4821. {
  4822. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  4823. (Mpi2EventDataSasBroadcastPrimitive_t *)
  4824. mpi_reply->EventData;
  4825. if (baen_data->Primitive !=
  4826. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
  4827. ioc->broadcast_aen_busy)
  4828. return;
  4829. ioc->broadcast_aen_busy = 1;
  4830. break;
  4831. }
  4832. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  4833. _scsih_check_topo_delete_events(ioc,
  4834. (Mpi2EventDataSasTopologyChangeList_t *)
  4835. mpi_reply->EventData);
  4836. break;
  4837. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4838. case MPI2_EVENT_IR_OPERATION_STATUS:
  4839. case MPI2_EVENT_SAS_DISCOVERY:
  4840. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  4841. case MPI2_EVENT_IR_VOLUME:
  4842. case MPI2_EVENT_IR_PHYSICAL_DISK:
  4843. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  4844. case MPI2_EVENT_TASK_SET_FULL:
  4845. break;
  4846. default: /* ignore the rest */
  4847. return;
  4848. }
  4849. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  4850. if (!fw_event) {
  4851. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4852. ioc->name, __FILE__, __LINE__, __func__);
  4853. return;
  4854. }
  4855. fw_event->event_data =
  4856. kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
  4857. if (!fw_event->event_data) {
  4858. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4859. ioc->name, __FILE__, __LINE__, __func__);
  4860. kfree(fw_event);
  4861. return;
  4862. }
  4863. memcpy(fw_event->event_data, mpi_reply->EventData,
  4864. mpi_reply->EventDataLength*4);
  4865. fw_event->ioc = ioc;
  4866. fw_event->VF_ID = VF_ID;
  4867. fw_event->event = event;
  4868. _scsih_fw_event_add(ioc, fw_event);
  4869. }
  4870. /* shost template */
  4871. static struct scsi_host_template scsih_driver_template = {
  4872. .module = THIS_MODULE,
  4873. .name = "Fusion MPT SAS Host",
  4874. .proc_name = MPT2SAS_DRIVER_NAME,
  4875. .queuecommand = _scsih_qcmd,
  4876. .target_alloc = _scsih_target_alloc,
  4877. .slave_alloc = _scsih_slave_alloc,
  4878. .slave_configure = _scsih_slave_configure,
  4879. .target_destroy = _scsih_target_destroy,
  4880. .slave_destroy = _scsih_slave_destroy,
  4881. .change_queue_depth = _scsih_change_queue_depth,
  4882. .change_queue_type = _scsih_change_queue_type,
  4883. .eh_abort_handler = _scsih_abort,
  4884. .eh_device_reset_handler = _scsih_dev_reset,
  4885. .eh_target_reset_handler = _scsih_target_reset,
  4886. .eh_host_reset_handler = _scsih_host_reset,
  4887. .bios_param = _scsih_bios_param,
  4888. .can_queue = 1,
  4889. .this_id = -1,
  4890. .sg_tablesize = MPT2SAS_SG_DEPTH,
  4891. .max_sectors = 8192,
  4892. .cmd_per_lun = 7,
  4893. .use_clustering = ENABLE_CLUSTERING,
  4894. .shost_attrs = mpt2sas_host_attrs,
  4895. .sdev_attrs = mpt2sas_dev_attrs,
  4896. };
  4897. /**
  4898. * _scsih_expander_node_remove - removing expander device from list.
  4899. * @ioc: per adapter object
  4900. * @sas_expander: the sas_device object
  4901. * Context: Calling function should acquire ioc->sas_node_lock.
  4902. *
  4903. * Removing object and freeing associated memory from the
  4904. * ioc->sas_expander_list.
  4905. *
  4906. * Return nothing.
  4907. */
  4908. static void
  4909. _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  4910. struct _sas_node *sas_expander)
  4911. {
  4912. struct _sas_port *mpt2sas_port;
  4913. struct _sas_device *sas_device;
  4914. struct _sas_node *expander_sibling;
  4915. unsigned long flags;
  4916. if (!sas_expander)
  4917. return;
  4918. /* remove sibling ports attached to this expander */
  4919. retry_device_search:
  4920. list_for_each_entry(mpt2sas_port,
  4921. &sas_expander->sas_port_list, port_list) {
  4922. if (mpt2sas_port->remote_identify.device_type ==
  4923. SAS_END_DEVICE) {
  4924. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4925. sas_device =
  4926. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4927. mpt2sas_port->remote_identify.sas_address);
  4928. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4929. if (!sas_device)
  4930. continue;
  4931. _scsih_remove_device(ioc, sas_device->handle);
  4932. if (ioc->shost_recovery)
  4933. return;
  4934. goto retry_device_search;
  4935. }
  4936. }
  4937. retry_expander_search:
  4938. list_for_each_entry(mpt2sas_port,
  4939. &sas_expander->sas_port_list, port_list) {
  4940. if (mpt2sas_port->remote_identify.device_type ==
  4941. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  4942. mpt2sas_port->remote_identify.device_type ==
  4943. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  4944. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4945. expander_sibling =
  4946. mpt2sas_scsih_expander_find_by_sas_address(
  4947. ioc, mpt2sas_port->remote_identify.sas_address);
  4948. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4949. if (!expander_sibling)
  4950. continue;
  4951. _scsih_expander_remove(ioc, expander_sibling->handle);
  4952. if (ioc->shost_recovery)
  4953. return;
  4954. goto retry_expander_search;
  4955. }
  4956. }
  4957. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  4958. sas_expander->parent_handle);
  4959. printk(MPT2SAS_INFO_FMT "expander_remove: handle"
  4960. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  4961. sas_expander->handle, (unsigned long long)
  4962. sas_expander->sas_address);
  4963. list_del(&sas_expander->list);
  4964. kfree(sas_expander->phy);
  4965. kfree(sas_expander);
  4966. }
  4967. /**
  4968. * _scsih_remove - detach and remove add host
  4969. * @pdev: PCI device struct
  4970. *
  4971. * Return nothing.
  4972. */
  4973. static void __devexit
  4974. _scsih_remove(struct pci_dev *pdev)
  4975. {
  4976. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  4977. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  4978. struct _sas_port *mpt2sas_port;
  4979. struct _sas_device *sas_device;
  4980. struct _sas_node *expander_sibling;
  4981. struct workqueue_struct *wq;
  4982. unsigned long flags;
  4983. ioc->remove_host = 1;
  4984. _scsih_fw_event_off(ioc);
  4985. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  4986. wq = ioc->firmware_event_thread;
  4987. ioc->firmware_event_thread = NULL;
  4988. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  4989. if (wq)
  4990. destroy_workqueue(wq);
  4991. /* free ports attached to the sas_host */
  4992. retry_again:
  4993. list_for_each_entry(mpt2sas_port,
  4994. &ioc->sas_hba.sas_port_list, port_list) {
  4995. if (mpt2sas_port->remote_identify.device_type ==
  4996. SAS_END_DEVICE) {
  4997. sas_device =
  4998. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4999. mpt2sas_port->remote_identify.sas_address);
  5000. if (sas_device) {
  5001. _scsih_remove_device(ioc, sas_device->handle);
  5002. goto retry_again;
  5003. }
  5004. } else {
  5005. expander_sibling =
  5006. mpt2sas_scsih_expander_find_by_sas_address(ioc,
  5007. mpt2sas_port->remote_identify.sas_address);
  5008. if (expander_sibling) {
  5009. _scsih_expander_remove(ioc,
  5010. expander_sibling->handle);
  5011. goto retry_again;
  5012. }
  5013. }
  5014. }
  5015. /* free phys attached to the sas_host */
  5016. if (ioc->sas_hba.num_phys) {
  5017. kfree(ioc->sas_hba.phy);
  5018. ioc->sas_hba.phy = NULL;
  5019. ioc->sas_hba.num_phys = 0;
  5020. }
  5021. sas_remove_host(shost);
  5022. mpt2sas_base_detach(ioc);
  5023. list_del(&ioc->list);
  5024. scsi_remove_host(shost);
  5025. scsi_host_put(shost);
  5026. }
  5027. /**
  5028. * _scsih_probe_boot_devices - reports 1st device
  5029. * @ioc: per adapter object
  5030. *
  5031. * If specified in bios page 2, this routine reports the 1st
  5032. * device scsi-ml or sas transport for persistent boot device
  5033. * purposes. Please refer to function _scsih_determine_boot_device()
  5034. */
  5035. static void
  5036. _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
  5037. {
  5038. u8 is_raid;
  5039. void *device;
  5040. struct _sas_device *sas_device;
  5041. struct _raid_device *raid_device;
  5042. u16 handle, parent_handle;
  5043. u64 sas_address;
  5044. unsigned long flags;
  5045. int rc;
  5046. device = NULL;
  5047. if (ioc->req_boot_device.device) {
  5048. device = ioc->req_boot_device.device;
  5049. is_raid = ioc->req_boot_device.is_raid;
  5050. } else if (ioc->req_alt_boot_device.device) {
  5051. device = ioc->req_alt_boot_device.device;
  5052. is_raid = ioc->req_alt_boot_device.is_raid;
  5053. } else if (ioc->current_boot_device.device) {
  5054. device = ioc->current_boot_device.device;
  5055. is_raid = ioc->current_boot_device.is_raid;
  5056. }
  5057. if (!device)
  5058. return;
  5059. if (is_raid) {
  5060. raid_device = device;
  5061. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5062. raid_device->id, 0);
  5063. if (rc)
  5064. _scsih_raid_device_remove(ioc, raid_device);
  5065. } else {
  5066. sas_device = device;
  5067. handle = sas_device->handle;
  5068. parent_handle = sas_device->parent_handle;
  5069. sas_address = sas_device->sas_address;
  5070. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5071. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5072. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5073. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  5074. sas_device->parent_handle)) {
  5075. _scsih_sas_device_remove(ioc, sas_device);
  5076. } else if (!sas_device->starget) {
  5077. mpt2sas_transport_port_remove(ioc, sas_address,
  5078. parent_handle);
  5079. _scsih_sas_device_remove(ioc, sas_device);
  5080. }
  5081. }
  5082. }
  5083. /**
  5084. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  5085. * @ioc: per adapter object
  5086. *
  5087. * Called during initial loading of the driver.
  5088. */
  5089. static void
  5090. _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
  5091. {
  5092. struct _raid_device *raid_device, *raid_next;
  5093. int rc;
  5094. list_for_each_entry_safe(raid_device, raid_next,
  5095. &ioc->raid_device_list, list) {
  5096. if (raid_device->starget)
  5097. continue;
  5098. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5099. raid_device->id, 0);
  5100. if (rc)
  5101. _scsih_raid_device_remove(ioc, raid_device);
  5102. }
  5103. }
  5104. /**
  5105. * _scsih_probe_sas - reporting raid volumes to sas transport
  5106. * @ioc: per adapter object
  5107. *
  5108. * Called during initial loading of the driver.
  5109. */
  5110. static void
  5111. _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
  5112. {
  5113. struct _sas_device *sas_device, *next;
  5114. unsigned long flags;
  5115. u16 handle, parent_handle;
  5116. u64 sas_address;
  5117. /* SAS Device List */
  5118. list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
  5119. list) {
  5120. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5121. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5122. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5123. handle = sas_device->handle;
  5124. parent_handle = sas_device->parent_handle;
  5125. sas_address = sas_device->sas_address;
  5126. if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
  5127. _scsih_sas_device_remove(ioc, sas_device);
  5128. } else if (!sas_device->starget) {
  5129. mpt2sas_transport_port_remove(ioc, sas_address,
  5130. parent_handle);
  5131. _scsih_sas_device_remove(ioc, sas_device);
  5132. }
  5133. }
  5134. }
  5135. /**
  5136. * _scsih_probe_devices - probing for devices
  5137. * @ioc: per adapter object
  5138. *
  5139. * Called during initial loading of the driver.
  5140. */
  5141. static void
  5142. _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
  5143. {
  5144. u16 volume_mapping_flags =
  5145. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  5146. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  5147. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  5148. return; /* return when IOC doesn't support initiator mode */
  5149. _scsih_probe_boot_devices(ioc);
  5150. if (ioc->ir_firmware) {
  5151. if ((volume_mapping_flags &
  5152. MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
  5153. _scsih_probe_sas(ioc);
  5154. _scsih_probe_raid(ioc);
  5155. } else {
  5156. _scsih_probe_raid(ioc);
  5157. _scsih_probe_sas(ioc);
  5158. }
  5159. } else
  5160. _scsih_probe_sas(ioc);
  5161. }
  5162. /**
  5163. * _scsih_probe - attach and add scsi host
  5164. * @pdev: PCI device struct
  5165. * @id: pci device id
  5166. *
  5167. * Returns 0 success, anything else error.
  5168. */
  5169. static int
  5170. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  5171. {
  5172. struct MPT2SAS_ADAPTER *ioc;
  5173. struct Scsi_Host *shost;
  5174. shost = scsi_host_alloc(&scsih_driver_template,
  5175. sizeof(struct MPT2SAS_ADAPTER));
  5176. if (!shost)
  5177. return -ENODEV;
  5178. /* init local params */
  5179. ioc = shost_priv(shost);
  5180. memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
  5181. INIT_LIST_HEAD(&ioc->list);
  5182. list_add_tail(&ioc->list, &mpt2sas_ioc_list);
  5183. ioc->shost = shost;
  5184. ioc->id = mpt_ids++;
  5185. sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
  5186. ioc->pdev = pdev;
  5187. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  5188. ioc->tm_cb_idx = tm_cb_idx;
  5189. ioc->ctl_cb_idx = ctl_cb_idx;
  5190. ioc->base_cb_idx = base_cb_idx;
  5191. ioc->transport_cb_idx = transport_cb_idx;
  5192. ioc->config_cb_idx = config_cb_idx;
  5193. ioc->logging_level = logging_level;
  5194. /* misc semaphores and spin locks */
  5195. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  5196. spin_lock_init(&ioc->scsi_lookup_lock);
  5197. spin_lock_init(&ioc->sas_device_lock);
  5198. spin_lock_init(&ioc->sas_node_lock);
  5199. spin_lock_init(&ioc->fw_event_lock);
  5200. spin_lock_init(&ioc->raid_device_lock);
  5201. INIT_LIST_HEAD(&ioc->sas_device_list);
  5202. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  5203. INIT_LIST_HEAD(&ioc->sas_expander_list);
  5204. INIT_LIST_HEAD(&ioc->fw_event_list);
  5205. INIT_LIST_HEAD(&ioc->raid_device_list);
  5206. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  5207. /* init shost parameters */
  5208. shost->max_cmd_len = 16;
  5209. shost->max_lun = max_lun;
  5210. shost->transportt = mpt2sas_transport_template;
  5211. shost->unique_id = ioc->id;
  5212. if ((scsi_add_host(shost, &pdev->dev))) {
  5213. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5214. ioc->name, __FILE__, __LINE__, __func__);
  5215. list_del(&ioc->list);
  5216. goto out_add_shost_fail;
  5217. }
  5218. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  5219. | SHOST_DIF_TYPE3_PROTECTION);
  5220. /* event thread */
  5221. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  5222. "fw_event%d", ioc->id);
  5223. ioc->firmware_event_thread = create_singlethread_workqueue(
  5224. ioc->firmware_event_name);
  5225. if (!ioc->firmware_event_thread) {
  5226. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5227. ioc->name, __FILE__, __LINE__, __func__);
  5228. goto out_thread_fail;
  5229. }
  5230. ioc->wait_for_port_enable_to_complete = 1;
  5231. if ((mpt2sas_base_attach(ioc))) {
  5232. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5233. ioc->name, __FILE__, __LINE__, __func__);
  5234. goto out_attach_fail;
  5235. }
  5236. ioc->wait_for_port_enable_to_complete = 0;
  5237. _scsih_probe_devices(ioc);
  5238. return 0;
  5239. out_attach_fail:
  5240. destroy_workqueue(ioc->firmware_event_thread);
  5241. out_thread_fail:
  5242. list_del(&ioc->list);
  5243. scsi_remove_host(shost);
  5244. out_add_shost_fail:
  5245. return -ENODEV;
  5246. }
  5247. #ifdef CONFIG_PM
  5248. /**
  5249. * _scsih_suspend - power management suspend main entry point
  5250. * @pdev: PCI device struct
  5251. * @state: PM state change to (usually PCI_D3)
  5252. *
  5253. * Returns 0 success, anything else error.
  5254. */
  5255. static int
  5256. _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  5257. {
  5258. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5259. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5260. u32 device_state;
  5261. mpt2sas_base_stop_watchdog(ioc);
  5262. flush_scheduled_work();
  5263. scsi_block_requests(shost);
  5264. device_state = pci_choose_state(pdev, state);
  5265. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
  5266. "operating state [D%d]\n", ioc->name, pdev,
  5267. pci_name(pdev), device_state);
  5268. mpt2sas_base_free_resources(ioc);
  5269. pci_save_state(pdev);
  5270. pci_disable_device(pdev);
  5271. pci_set_power_state(pdev, device_state);
  5272. return 0;
  5273. }
  5274. /**
  5275. * _scsih_resume - power management resume main entry point
  5276. * @pdev: PCI device struct
  5277. *
  5278. * Returns 0 success, anything else error.
  5279. */
  5280. static int
  5281. _scsih_resume(struct pci_dev *pdev)
  5282. {
  5283. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5284. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5285. u32 device_state = pdev->current_state;
  5286. int r;
  5287. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
  5288. "operating state [D%d]\n", ioc->name, pdev,
  5289. pci_name(pdev), device_state);
  5290. pci_set_power_state(pdev, PCI_D0);
  5291. pci_enable_wake(pdev, PCI_D0, 0);
  5292. pci_restore_state(pdev);
  5293. ioc->pdev = pdev;
  5294. r = mpt2sas_base_map_resources(ioc);
  5295. if (r)
  5296. return r;
  5297. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
  5298. scsi_unblock_requests(shost);
  5299. mpt2sas_base_start_watchdog(ioc);
  5300. return 0;
  5301. }
  5302. #endif /* CONFIG_PM */
  5303. static struct pci_driver scsih_driver = {
  5304. .name = MPT2SAS_DRIVER_NAME,
  5305. .id_table = scsih_pci_table,
  5306. .probe = _scsih_probe,
  5307. .remove = __devexit_p(_scsih_remove),
  5308. #ifdef CONFIG_PM
  5309. .suspend = _scsih_suspend,
  5310. .resume = _scsih_resume,
  5311. #endif
  5312. };
  5313. /**
  5314. * _scsih_init - main entry point for this driver.
  5315. *
  5316. * Returns 0 success, anything else error.
  5317. */
  5318. static int __init
  5319. _scsih_init(void)
  5320. {
  5321. int error;
  5322. mpt_ids = 0;
  5323. printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
  5324. MPT2SAS_DRIVER_VERSION);
  5325. mpt2sas_transport_template =
  5326. sas_attach_transport(&mpt2sas_transport_functions);
  5327. if (!mpt2sas_transport_template)
  5328. return -ENODEV;
  5329. mpt2sas_base_initialize_callback_handler();
  5330. /* queuecommand callback hander */
  5331. scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
  5332. /* task managment callback handler */
  5333. tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
  5334. /* base internal commands callback handler */
  5335. base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
  5336. /* transport internal commands callback handler */
  5337. transport_cb_idx = mpt2sas_base_register_callback_handler(
  5338. mpt2sas_transport_done);
  5339. /* configuration page API internal commands callback handler */
  5340. config_cb_idx = mpt2sas_base_register_callback_handler(
  5341. mpt2sas_config_done);
  5342. /* ctl module callback handler */
  5343. ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
  5344. mpt2sas_ctl_init();
  5345. error = pci_register_driver(&scsih_driver);
  5346. if (error)
  5347. sas_release_transport(mpt2sas_transport_template);
  5348. return error;
  5349. }
  5350. /**
  5351. * _scsih_exit - exit point for this driver (when it is a module).
  5352. *
  5353. * Returns 0 success, anything else error.
  5354. */
  5355. static void __exit
  5356. _scsih_exit(void)
  5357. {
  5358. printk(KERN_INFO "mpt2sas version %s unloading\n",
  5359. MPT2SAS_DRIVER_VERSION);
  5360. pci_unregister_driver(&scsih_driver);
  5361. sas_release_transport(mpt2sas_transport_template);
  5362. mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
  5363. mpt2sas_base_release_callback_handler(tm_cb_idx);
  5364. mpt2sas_base_release_callback_handler(base_cb_idx);
  5365. mpt2sas_base_release_callback_handler(transport_cb_idx);
  5366. mpt2sas_base_release_callback_handler(config_cb_idx);
  5367. mpt2sas_base_release_callback_handler(ctl_cb_idx);
  5368. mpt2sas_ctl_exit();
  5369. }
  5370. module_init(_scsih_init);
  5371. module_exit(_scsih_exit);