mpt2sas_scsih.c 237 KB

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