mpt2sas_scsih.c 226 KB

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