mpt2sas_scsih.c 188 KB

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