mpt2sas_scsih.c 195 KB

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