mpt2sas_scsih.c 160 KB

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