mpt2sas_scsih.c 196 KB

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