mpt2sas_scsih.c 188 KB

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