mpt2sas_scsih.c 245 KB

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