mpt2sas_scsih.c 239 KB

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