mpt2sas_scsih.c 188 KB

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