mpt2sas_scsih.c 239 KB

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