mpt2sas_scsih.c 241 KB

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