mpt2sas_scsih.c 207 KB

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