mpt2sas_scsih.c 242 KB

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