mpt2sas_scsih.c 245 KB

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