mpt2sas_scsih.c 245 KB

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