mpt2sas_scsih.c 174 KB

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