mpt2sas_scsih.c 237 KB

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