mpt2sas_scsih.c 241 KB

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