mpt2sas_scsih.c 197 KB

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