mpt3sas_scsih.c 319 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242
  1. /*
  2. * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
  5. * Copyright (C) 2012-2014 LSI Corporation
  6. * Copyright (C) 2013-2014 Avago Technologies
  7. * (mailto: MPT-FusionLinux.pdl@avagotech.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/sched.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/delay.h>
  50. #include <linux/pci.h>
  51. #include <linux/pci-aspm.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/aer.h>
  54. #include <linux/raid_class.h>
  55. #include <asm/unaligned.h>
  56. #include "mpt3sas_base.h"
  57. #define RAID_CHANNEL 1
  58. #define PCIE_CHANNEL 2
  59. /* forward proto's */
  60. static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
  61. struct _sas_node *sas_expander);
  62. static void _firmware_event_work(struct work_struct *work);
  63. static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
  64. struct _sas_device *sas_device);
  65. static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  66. u8 retry_count, u8 is_pd);
  67. static int _scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
  68. static void _scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
  69. struct _pcie_device *pcie_device);
  70. static void
  71. _scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
  72. static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
  73. /* global parameters */
  74. LIST_HEAD(mpt3sas_ioc_list);
  75. /* global ioc lock for list operations */
  76. DEFINE_SPINLOCK(gioc_lock);
  77. MODULE_AUTHOR(MPT3SAS_AUTHOR);
  78. MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
  79. MODULE_LICENSE("GPL");
  80. MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
  81. MODULE_ALIAS("mpt2sas");
  82. /* local parameters */
  83. static u8 scsi_io_cb_idx = -1;
  84. static u8 tm_cb_idx = -1;
  85. static u8 ctl_cb_idx = -1;
  86. static u8 base_cb_idx = -1;
  87. static u8 port_enable_cb_idx = -1;
  88. static u8 transport_cb_idx = -1;
  89. static u8 scsih_cb_idx = -1;
  90. static u8 config_cb_idx = -1;
  91. static int mpt2_ids;
  92. static int mpt3_ids;
  93. static u8 tm_tr_cb_idx = -1 ;
  94. static u8 tm_tr_volume_cb_idx = -1 ;
  95. static u8 tm_sas_control_cb_idx = -1;
  96. /* command line options */
  97. static u32 logging_level;
  98. MODULE_PARM_DESC(logging_level,
  99. " bits for enabling additional logging info (default=0)");
  100. static ushort max_sectors = 0xFFFF;
  101. module_param(max_sectors, ushort, 0);
  102. MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
  103. static int missing_delay[2] = {-1, -1};
  104. module_param_array(missing_delay, int, NULL, 0);
  105. MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
  106. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  107. #define MPT3SAS_MAX_LUN (16895)
  108. static u64 max_lun = MPT3SAS_MAX_LUN;
  109. module_param(max_lun, ullong, 0);
  110. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  111. static ushort hbas_to_enumerate;
  112. module_param(hbas_to_enumerate, ushort, 0);
  113. MODULE_PARM_DESC(hbas_to_enumerate,
  114. " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
  115. 1 - enumerates only SAS 2.0 generation HBAs\n \
  116. 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
  117. /* diag_buffer_enable is bitwise
  118. * bit 0 set = TRACE
  119. * bit 1 set = SNAPSHOT
  120. * bit 2 set = EXTENDED
  121. *
  122. * Either bit can be set, or both
  123. */
  124. static int diag_buffer_enable = -1;
  125. module_param(diag_buffer_enable, int, 0);
  126. MODULE_PARM_DESC(diag_buffer_enable,
  127. " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
  128. static int disable_discovery = -1;
  129. module_param(disable_discovery, int, 0);
  130. MODULE_PARM_DESC(disable_discovery, " disable discovery ");
  131. /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
  132. static int prot_mask = -1;
  133. module_param(prot_mask, int, 0);
  134. MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
  135. /* raid transport support */
  136. struct raid_template *mpt3sas_raid_template;
  137. struct raid_template *mpt2sas_raid_template;
  138. /**
  139. * struct sense_info - common structure for obtaining sense keys
  140. * @skey: sense key
  141. * @asc: additional sense code
  142. * @ascq: additional sense code qualifier
  143. */
  144. struct sense_info {
  145. u8 skey;
  146. u8 asc;
  147. u8 ascq;
  148. };
  149. #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
  150. #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
  151. #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
  152. #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
  153. #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
  154. /**
  155. * struct fw_event_work - firmware event struct
  156. * @list: link list framework
  157. * @work: work object (ioc->fault_reset_work_q)
  158. * @ioc: per adapter object
  159. * @device_handle: device handle
  160. * @VF_ID: virtual function id
  161. * @VP_ID: virtual port id
  162. * @ignore: flag meaning this event has been marked to ignore
  163. * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
  164. * @refcount: kref for this event
  165. * @event_data: reply event data payload follows
  166. *
  167. * This object stored on ioc->fw_event_list.
  168. */
  169. struct fw_event_work {
  170. struct list_head list;
  171. struct work_struct work;
  172. struct MPT3SAS_ADAPTER *ioc;
  173. u16 device_handle;
  174. u8 VF_ID;
  175. u8 VP_ID;
  176. u8 ignore;
  177. u16 event;
  178. struct kref refcount;
  179. char event_data[0] __aligned(4);
  180. };
  181. static void fw_event_work_free(struct kref *r)
  182. {
  183. kfree(container_of(r, struct fw_event_work, refcount));
  184. }
  185. static void fw_event_work_get(struct fw_event_work *fw_work)
  186. {
  187. kref_get(&fw_work->refcount);
  188. }
  189. static void fw_event_work_put(struct fw_event_work *fw_work)
  190. {
  191. kref_put(&fw_work->refcount, fw_event_work_free);
  192. }
  193. static struct fw_event_work *alloc_fw_event_work(int len)
  194. {
  195. struct fw_event_work *fw_event;
  196. fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
  197. if (!fw_event)
  198. return NULL;
  199. kref_init(&fw_event->refcount);
  200. return fw_event;
  201. }
  202. /**
  203. * struct _scsi_io_transfer - scsi io transfer
  204. * @handle: sas device handle (assigned by firmware)
  205. * @is_raid: flag set for hidden raid components
  206. * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
  207. * @data_length: data transfer length
  208. * @data_dma: dma pointer to data
  209. * @sense: sense data
  210. * @lun: lun number
  211. * @cdb_length: cdb length
  212. * @cdb: cdb contents
  213. * @timeout: timeout for this command
  214. * @VF_ID: virtual function id
  215. * @VP_ID: virtual port id
  216. * @valid_reply: flag set for reply message
  217. * @sense_length: sense length
  218. * @ioc_status: ioc status
  219. * @scsi_state: scsi state
  220. * @scsi_status: scsi staus
  221. * @log_info: log information
  222. * @transfer_length: data length transfer when there is a reply message
  223. *
  224. * Used for sending internal scsi commands to devices within this module.
  225. * Refer to _scsi_send_scsi_io().
  226. */
  227. struct _scsi_io_transfer {
  228. u16 handle;
  229. u8 is_raid;
  230. enum dma_data_direction dir;
  231. u32 data_length;
  232. dma_addr_t data_dma;
  233. u8 sense[SCSI_SENSE_BUFFERSIZE];
  234. u32 lun;
  235. u8 cdb_length;
  236. u8 cdb[32];
  237. u8 timeout;
  238. u8 VF_ID;
  239. u8 VP_ID;
  240. u8 valid_reply;
  241. /* the following bits are only valid when 'valid_reply = 1' */
  242. u32 sense_length;
  243. u16 ioc_status;
  244. u8 scsi_state;
  245. u8 scsi_status;
  246. u32 log_info;
  247. u32 transfer_length;
  248. };
  249. /**
  250. * _scsih_set_debug_level - global setting of ioc->logging_level.
  251. *
  252. * Note: The logging levels are defined in mpt3sas_debug.h.
  253. */
  254. static int
  255. _scsih_set_debug_level(const char *val, struct kernel_param *kp)
  256. {
  257. int ret = param_set_int(val, kp);
  258. struct MPT3SAS_ADAPTER *ioc;
  259. if (ret)
  260. return ret;
  261. pr_info("setting logging_level(0x%08x)\n", logging_level);
  262. spin_lock(&gioc_lock);
  263. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  264. ioc->logging_level = logging_level;
  265. spin_unlock(&gioc_lock);
  266. return 0;
  267. }
  268. module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
  269. &logging_level, 0644);
  270. /**
  271. * _scsih_srch_boot_sas_address - search based on sas_address
  272. * @sas_address: sas address
  273. * @boot_device: boot device object from bios page 2
  274. *
  275. * Returns 1 when there's a match, 0 means no match.
  276. */
  277. static inline int
  278. _scsih_srch_boot_sas_address(u64 sas_address,
  279. Mpi2BootDeviceSasWwid_t *boot_device)
  280. {
  281. return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
  282. }
  283. /**
  284. * _scsih_srch_boot_device_name - search based on device name
  285. * @device_name: device name specified in INDENTIFY fram
  286. * @boot_device: boot device object from bios page 2
  287. *
  288. * Returns 1 when there's a match, 0 means no match.
  289. */
  290. static inline int
  291. _scsih_srch_boot_device_name(u64 device_name,
  292. Mpi2BootDeviceDeviceName_t *boot_device)
  293. {
  294. return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
  295. }
  296. /**
  297. * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
  298. * @enclosure_logical_id: enclosure logical id
  299. * @slot_number: slot number
  300. * @boot_device: boot device object from bios page 2
  301. *
  302. * Returns 1 when there's a match, 0 means no match.
  303. */
  304. static inline int
  305. _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
  306. Mpi2BootDeviceEnclosureSlot_t *boot_device)
  307. {
  308. return (enclosure_logical_id == le64_to_cpu(boot_device->
  309. EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
  310. SlotNumber)) ? 1 : 0;
  311. }
  312. /**
  313. * _scsih_is_boot_device - search for matching boot device.
  314. * @sas_address: sas address
  315. * @device_name: device name specified in INDENTIFY fram
  316. * @enclosure_logical_id: enclosure logical id
  317. * @slot_number: slot number
  318. * @form: specifies boot device form
  319. * @boot_device: boot device object from bios page 2
  320. *
  321. * Returns 1 when there's a match, 0 means no match.
  322. */
  323. static int
  324. _scsih_is_boot_device(u64 sas_address, u64 device_name,
  325. u64 enclosure_logical_id, u16 slot, u8 form,
  326. Mpi2BiosPage2BootDevice_t *boot_device)
  327. {
  328. int rc = 0;
  329. switch (form) {
  330. case MPI2_BIOSPAGE2_FORM_SAS_WWID:
  331. if (!sas_address)
  332. break;
  333. rc = _scsih_srch_boot_sas_address(
  334. sas_address, &boot_device->SasWwid);
  335. break;
  336. case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
  337. if (!enclosure_logical_id)
  338. break;
  339. rc = _scsih_srch_boot_encl_slot(
  340. enclosure_logical_id,
  341. slot, &boot_device->EnclosureSlot);
  342. break;
  343. case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
  344. if (!device_name)
  345. break;
  346. rc = _scsih_srch_boot_device_name(
  347. device_name, &boot_device->DeviceName);
  348. break;
  349. case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
  350. break;
  351. }
  352. return rc;
  353. }
  354. /**
  355. * _scsih_get_sas_address - set the sas_address for given device handle
  356. * @handle: device handle
  357. * @sas_address: sas address
  358. *
  359. * Returns 0 success, non-zero when failure
  360. */
  361. static int
  362. _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  363. u64 *sas_address)
  364. {
  365. Mpi2SasDevicePage0_t sas_device_pg0;
  366. Mpi2ConfigReply_t mpi_reply;
  367. u32 ioc_status;
  368. *sas_address = 0;
  369. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  370. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  371. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
  372. __FILE__, __LINE__, __func__);
  373. return -ENXIO;
  374. }
  375. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  376. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  377. /* For HBA, vSES doesn't return HBA SAS address. Instead return
  378. * vSES's sas address.
  379. */
  380. if ((handle <= ioc->sas_hba.num_phys) &&
  381. (!(le32_to_cpu(sas_device_pg0.DeviceInfo) &
  382. MPI2_SAS_DEVICE_INFO_SEP)))
  383. *sas_address = ioc->sas_hba.sas_address;
  384. else
  385. *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  386. return 0;
  387. }
  388. /* we hit this because the given parent handle doesn't exist */
  389. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  390. return -ENXIO;
  391. /* else error case */
  392. pr_err(MPT3SAS_FMT
  393. "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
  394. ioc->name, handle, ioc_status,
  395. __FILE__, __LINE__, __func__);
  396. return -EIO;
  397. }
  398. /**
  399. * _scsih_determine_boot_device - determine boot device.
  400. * @ioc: per adapter object
  401. * @device: sas_device or pcie_device object
  402. * @channel: SAS or PCIe channel
  403. *
  404. * Determines whether this device should be first reported device to
  405. * to scsi-ml or sas transport, this purpose is for persistent boot device.
  406. * There are primary, alternate, and current entries in bios page 2. The order
  407. * priority is primary, alternate, then current. This routine saves
  408. * the corresponding device object.
  409. * The saved data to be used later in _scsih_probe_boot_devices().
  410. */
  411. static void
  412. _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc, void *device,
  413. u32 channel)
  414. {
  415. struct _sas_device *sas_device;
  416. struct _pcie_device *pcie_device;
  417. struct _raid_device *raid_device;
  418. u64 sas_address;
  419. u64 device_name;
  420. u64 enclosure_logical_id;
  421. u16 slot;
  422. /* only process this function when driver loads */
  423. if (!ioc->is_driver_loading)
  424. return;
  425. /* no Bios, return immediately */
  426. if (!ioc->bios_pg3.BiosVersion)
  427. return;
  428. if (channel == RAID_CHANNEL) {
  429. raid_device = device;
  430. sas_address = raid_device->wwid;
  431. device_name = 0;
  432. enclosure_logical_id = 0;
  433. slot = 0;
  434. } else if (channel == PCIE_CHANNEL) {
  435. pcie_device = device;
  436. sas_address = pcie_device->wwid;
  437. device_name = 0;
  438. enclosure_logical_id = 0;
  439. slot = 0;
  440. } else {
  441. sas_device = device;
  442. sas_address = sas_device->sas_address;
  443. device_name = sas_device->device_name;
  444. enclosure_logical_id = sas_device->enclosure_logical_id;
  445. slot = sas_device->slot;
  446. }
  447. if (!ioc->req_boot_device.device) {
  448. if (_scsih_is_boot_device(sas_address, device_name,
  449. enclosure_logical_id, slot,
  450. (ioc->bios_pg2.ReqBootDeviceForm &
  451. MPI2_BIOSPAGE2_FORM_MASK),
  452. &ioc->bios_pg2.RequestedBootDevice)) {
  453. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  454. "%s: req_boot_device(0x%016llx)\n",
  455. ioc->name, __func__,
  456. (unsigned long long)sas_address));
  457. ioc->req_boot_device.device = device;
  458. ioc->req_boot_device.channel = channel;
  459. }
  460. }
  461. if (!ioc->req_alt_boot_device.device) {
  462. if (_scsih_is_boot_device(sas_address, device_name,
  463. enclosure_logical_id, slot,
  464. (ioc->bios_pg2.ReqAltBootDeviceForm &
  465. MPI2_BIOSPAGE2_FORM_MASK),
  466. &ioc->bios_pg2.RequestedAltBootDevice)) {
  467. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  468. "%s: req_alt_boot_device(0x%016llx)\n",
  469. ioc->name, __func__,
  470. (unsigned long long)sas_address));
  471. ioc->req_alt_boot_device.device = device;
  472. ioc->req_alt_boot_device.channel = channel;
  473. }
  474. }
  475. if (!ioc->current_boot_device.device) {
  476. if (_scsih_is_boot_device(sas_address, device_name,
  477. enclosure_logical_id, slot,
  478. (ioc->bios_pg2.CurrentBootDeviceForm &
  479. MPI2_BIOSPAGE2_FORM_MASK),
  480. &ioc->bios_pg2.CurrentBootDevice)) {
  481. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  482. "%s: current_boot_device(0x%016llx)\n",
  483. ioc->name, __func__,
  484. (unsigned long long)sas_address));
  485. ioc->current_boot_device.device = device;
  486. ioc->current_boot_device.channel = channel;
  487. }
  488. }
  489. }
  490. static struct _sas_device *
  491. __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
  492. struct MPT3SAS_TARGET *tgt_priv)
  493. {
  494. struct _sas_device *ret;
  495. assert_spin_locked(&ioc->sas_device_lock);
  496. ret = tgt_priv->sas_dev;
  497. if (ret)
  498. sas_device_get(ret);
  499. return ret;
  500. }
  501. static struct _sas_device *
  502. mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
  503. struct MPT3SAS_TARGET *tgt_priv)
  504. {
  505. struct _sas_device *ret;
  506. unsigned long flags;
  507. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  508. ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
  509. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  510. return ret;
  511. }
  512. static struct _pcie_device *
  513. __mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
  514. struct MPT3SAS_TARGET *tgt_priv)
  515. {
  516. struct _pcie_device *ret;
  517. assert_spin_locked(&ioc->pcie_device_lock);
  518. ret = tgt_priv->pcie_dev;
  519. if (ret)
  520. pcie_device_get(ret);
  521. return ret;
  522. }
  523. /**
  524. * mpt3sas_get_pdev_from_target - pcie device search
  525. * @ioc: per adapter object
  526. * @tgt_priv: starget private object
  527. *
  528. * Context: This function will acquire ioc->pcie_device_lock and will release
  529. * before returning the pcie_device object.
  530. *
  531. * This searches for pcie_device from target, then return pcie_device object.
  532. */
  533. static struct _pcie_device *
  534. mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
  535. struct MPT3SAS_TARGET *tgt_priv)
  536. {
  537. struct _pcie_device *ret;
  538. unsigned long flags;
  539. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  540. ret = __mpt3sas_get_pdev_from_target(ioc, tgt_priv);
  541. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  542. return ret;
  543. }
  544. struct _sas_device *
  545. __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
  546. u64 sas_address)
  547. {
  548. struct _sas_device *sas_device;
  549. assert_spin_locked(&ioc->sas_device_lock);
  550. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  551. if (sas_device->sas_address == sas_address)
  552. goto found_device;
  553. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  554. if (sas_device->sas_address == sas_address)
  555. goto found_device;
  556. return NULL;
  557. found_device:
  558. sas_device_get(sas_device);
  559. return sas_device;
  560. }
  561. /**
  562. * mpt3sas_get_sdev_by_addr - sas device search
  563. * @ioc: per adapter object
  564. * @sas_address: sas address
  565. * Context: Calling function should acquire ioc->sas_device_lock
  566. *
  567. * This searches for sas_device based on sas_address, then return sas_device
  568. * object.
  569. */
  570. struct _sas_device *
  571. mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
  572. u64 sas_address)
  573. {
  574. struct _sas_device *sas_device;
  575. unsigned long flags;
  576. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  577. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  578. sas_address);
  579. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  580. return sas_device;
  581. }
  582. static struct _sas_device *
  583. __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  584. {
  585. struct _sas_device *sas_device;
  586. assert_spin_locked(&ioc->sas_device_lock);
  587. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  588. if (sas_device->handle == handle)
  589. goto found_device;
  590. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  591. if (sas_device->handle == handle)
  592. goto found_device;
  593. return NULL;
  594. found_device:
  595. sas_device_get(sas_device);
  596. return sas_device;
  597. }
  598. /**
  599. * mpt3sas_get_sdev_by_handle - sas device search
  600. * @ioc: per adapter object
  601. * @handle: sas device handle (assigned by firmware)
  602. * Context: Calling function should acquire ioc->sas_device_lock
  603. *
  604. * This searches for sas_device based on sas_address, then return sas_device
  605. * object.
  606. */
  607. struct _sas_device *
  608. mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  609. {
  610. struct _sas_device *sas_device;
  611. unsigned long flags;
  612. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  613. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  614. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  615. return sas_device;
  616. }
  617. /**
  618. * _scsih_display_enclosure_chassis_info - display device location info
  619. * @ioc: per adapter object
  620. * @sas_device: per sas device object
  621. * @sdev: scsi device struct
  622. * @starget: scsi target struct
  623. *
  624. * Returns nothing.
  625. */
  626. static void
  627. _scsih_display_enclosure_chassis_info(struct MPT3SAS_ADAPTER *ioc,
  628. struct _sas_device *sas_device, struct scsi_device *sdev,
  629. struct scsi_target *starget)
  630. {
  631. if (sdev) {
  632. if (sas_device->enclosure_handle != 0)
  633. sdev_printk(KERN_INFO, sdev,
  634. "enclosure logical id (0x%016llx), slot(%d) \n",
  635. (unsigned long long)
  636. sas_device->enclosure_logical_id,
  637. sas_device->slot);
  638. if (sas_device->connector_name[0] != '\0')
  639. sdev_printk(KERN_INFO, sdev,
  640. "enclosure level(0x%04x), connector name( %s)\n",
  641. sas_device->enclosure_level,
  642. sas_device->connector_name);
  643. if (sas_device->is_chassis_slot_valid)
  644. sdev_printk(KERN_INFO, sdev, "chassis slot(0x%04x)\n",
  645. sas_device->chassis_slot);
  646. } else if (starget) {
  647. if (sas_device->enclosure_handle != 0)
  648. starget_printk(KERN_INFO, starget,
  649. "enclosure logical id(0x%016llx), slot(%d) \n",
  650. (unsigned long long)
  651. sas_device->enclosure_logical_id,
  652. sas_device->slot);
  653. if (sas_device->connector_name[0] != '\0')
  654. starget_printk(KERN_INFO, starget,
  655. "enclosure level(0x%04x), connector name( %s)\n",
  656. sas_device->enclosure_level,
  657. sas_device->connector_name);
  658. if (sas_device->is_chassis_slot_valid)
  659. starget_printk(KERN_INFO, starget,
  660. "chassis slot(0x%04x)\n",
  661. sas_device->chassis_slot);
  662. } else {
  663. if (sas_device->enclosure_handle != 0)
  664. pr_info(MPT3SAS_FMT
  665. "enclosure logical id(0x%016llx), slot(%d) \n",
  666. ioc->name, (unsigned long long)
  667. sas_device->enclosure_logical_id,
  668. sas_device->slot);
  669. if (sas_device->connector_name[0] != '\0')
  670. pr_info(MPT3SAS_FMT
  671. "enclosure level(0x%04x), connector name( %s)\n",
  672. ioc->name, sas_device->enclosure_level,
  673. sas_device->connector_name);
  674. if (sas_device->is_chassis_slot_valid)
  675. pr_info(MPT3SAS_FMT "chassis slot(0x%04x)\n",
  676. ioc->name, sas_device->chassis_slot);
  677. }
  678. }
  679. /**
  680. * _scsih_sas_device_remove - remove sas_device from list.
  681. * @ioc: per adapter object
  682. * @sas_device: the sas_device object
  683. * Context: This function will acquire ioc->sas_device_lock.
  684. *
  685. * If sas_device is on the list, remove it and decrement its reference count.
  686. */
  687. static void
  688. _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
  689. struct _sas_device *sas_device)
  690. {
  691. unsigned long flags;
  692. if (!sas_device)
  693. return;
  694. pr_info(MPT3SAS_FMT
  695. "removing handle(0x%04x), sas_addr(0x%016llx)\n",
  696. ioc->name, sas_device->handle,
  697. (unsigned long long) sas_device->sas_address);
  698. _scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
  699. /*
  700. * The lock serializes access to the list, but we still need to verify
  701. * that nobody removed the entry while we were waiting on the lock.
  702. */
  703. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  704. if (!list_empty(&sas_device->list)) {
  705. list_del_init(&sas_device->list);
  706. sas_device_put(sas_device);
  707. }
  708. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  709. }
  710. /**
  711. * _scsih_device_remove_by_handle - removing device object by handle
  712. * @ioc: per adapter object
  713. * @handle: device handle
  714. *
  715. * Return nothing.
  716. */
  717. static void
  718. _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  719. {
  720. struct _sas_device *sas_device;
  721. unsigned long flags;
  722. if (ioc->shost_recovery)
  723. return;
  724. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  725. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  726. if (sas_device) {
  727. list_del_init(&sas_device->list);
  728. sas_device_put(sas_device);
  729. }
  730. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  731. if (sas_device) {
  732. _scsih_remove_device(ioc, sas_device);
  733. sas_device_put(sas_device);
  734. }
  735. }
  736. /**
  737. * mpt3sas_device_remove_by_sas_address - removing device object by sas address
  738. * @ioc: per adapter object
  739. * @sas_address: device sas_address
  740. *
  741. * Return nothing.
  742. */
  743. void
  744. mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  745. u64 sas_address)
  746. {
  747. struct _sas_device *sas_device;
  748. unsigned long flags;
  749. if (ioc->shost_recovery)
  750. return;
  751. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  752. sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
  753. if (sas_device) {
  754. list_del_init(&sas_device->list);
  755. sas_device_put(sas_device);
  756. }
  757. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  758. if (sas_device) {
  759. _scsih_remove_device(ioc, sas_device);
  760. sas_device_put(sas_device);
  761. }
  762. }
  763. /**
  764. * _scsih_sas_device_add - insert sas_device to the list.
  765. * @ioc: per adapter object
  766. * @sas_device: the sas_device object
  767. * Context: This function will acquire ioc->sas_device_lock.
  768. *
  769. * Adding new object to the ioc->sas_device_list.
  770. */
  771. static void
  772. _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
  773. struct _sas_device *sas_device)
  774. {
  775. unsigned long flags;
  776. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  777. "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
  778. ioc->name, __func__, sas_device->handle,
  779. (unsigned long long)sas_device->sas_address));
  780. dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
  781. NULL, NULL));
  782. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  783. sas_device_get(sas_device);
  784. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  785. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  786. if (ioc->hide_drives) {
  787. clear_bit(sas_device->handle, ioc->pend_os_device_add);
  788. return;
  789. }
  790. if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
  791. sas_device->sas_address_parent)) {
  792. _scsih_sas_device_remove(ioc, sas_device);
  793. } else if (!sas_device->starget) {
  794. /*
  795. * When asyn scanning is enabled, its not possible to remove
  796. * devices while scanning is turned on due to an oops in
  797. * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
  798. */
  799. if (!ioc->is_driver_loading) {
  800. mpt3sas_transport_port_remove(ioc,
  801. sas_device->sas_address,
  802. sas_device->sas_address_parent);
  803. _scsih_sas_device_remove(ioc, sas_device);
  804. }
  805. } else
  806. clear_bit(sas_device->handle, ioc->pend_os_device_add);
  807. }
  808. /**
  809. * _scsih_sas_device_init_add - insert sas_device to the list.
  810. * @ioc: per adapter object
  811. * @sas_device: the sas_device object
  812. * Context: This function will acquire ioc->sas_device_lock.
  813. *
  814. * Adding new object at driver load time to the ioc->sas_device_init_list.
  815. */
  816. static void
  817. _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
  818. struct _sas_device *sas_device)
  819. {
  820. unsigned long flags;
  821. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  822. "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  823. __func__, sas_device->handle,
  824. (unsigned long long)sas_device->sas_address));
  825. dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
  826. NULL, NULL));
  827. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  828. sas_device_get(sas_device);
  829. list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
  830. _scsih_determine_boot_device(ioc, sas_device, 0);
  831. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  832. }
  833. static struct _pcie_device *
  834. __mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
  835. {
  836. struct _pcie_device *pcie_device;
  837. assert_spin_locked(&ioc->pcie_device_lock);
  838. list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
  839. if (pcie_device->wwid == wwid)
  840. goto found_device;
  841. list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
  842. if (pcie_device->wwid == wwid)
  843. goto found_device;
  844. return NULL;
  845. found_device:
  846. pcie_device_get(pcie_device);
  847. return pcie_device;
  848. }
  849. /**
  850. * mpt3sas_get_pdev_by_wwid - pcie device search
  851. * @ioc: per adapter object
  852. * @wwid: wwid
  853. *
  854. * Context: This function will acquire ioc->pcie_device_lock and will release
  855. * before returning the pcie_device object.
  856. *
  857. * This searches for pcie_device based on wwid, then return pcie_device object.
  858. */
  859. static struct _pcie_device *
  860. mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
  861. {
  862. struct _pcie_device *pcie_device;
  863. unsigned long flags;
  864. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  865. pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
  866. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  867. return pcie_device;
  868. }
  869. static struct _pcie_device *
  870. __mpt3sas_get_pdev_by_idchannel(struct MPT3SAS_ADAPTER *ioc, int id,
  871. int channel)
  872. {
  873. struct _pcie_device *pcie_device;
  874. assert_spin_locked(&ioc->pcie_device_lock);
  875. list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
  876. if (pcie_device->id == id && pcie_device->channel == channel)
  877. goto found_device;
  878. list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
  879. if (pcie_device->id == id && pcie_device->channel == channel)
  880. goto found_device;
  881. return NULL;
  882. found_device:
  883. pcie_device_get(pcie_device);
  884. return pcie_device;
  885. }
  886. static struct _pcie_device *
  887. __mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  888. {
  889. struct _pcie_device *pcie_device;
  890. assert_spin_locked(&ioc->pcie_device_lock);
  891. list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
  892. if (pcie_device->handle == handle)
  893. goto found_device;
  894. list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
  895. if (pcie_device->handle == handle)
  896. goto found_device;
  897. return NULL;
  898. found_device:
  899. pcie_device_get(pcie_device);
  900. return pcie_device;
  901. }
  902. /**
  903. * mpt3sas_get_pdev_by_handle - pcie device search
  904. * @ioc: per adapter object
  905. * @handle: Firmware device handle
  906. *
  907. * Context: This function will acquire ioc->pcie_device_lock and will release
  908. * before returning the pcie_device object.
  909. *
  910. * This searches for pcie_device based on handle, then return pcie_device
  911. * object.
  912. */
  913. struct _pcie_device *
  914. mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  915. {
  916. struct _pcie_device *pcie_device;
  917. unsigned long flags;
  918. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  919. pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
  920. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  921. return pcie_device;
  922. }
  923. /**
  924. * _scsih_pcie_device_remove - remove pcie_device from list.
  925. * @ioc: per adapter object
  926. * @pcie_device: the pcie_device object
  927. * Context: This function will acquire ioc->pcie_device_lock.
  928. *
  929. * If pcie_device is on the list, remove it and decrement its reference count.
  930. */
  931. static void
  932. _scsih_pcie_device_remove(struct MPT3SAS_ADAPTER *ioc,
  933. struct _pcie_device *pcie_device)
  934. {
  935. unsigned long flags;
  936. int was_on_pcie_device_list = 0;
  937. if (!pcie_device)
  938. return;
  939. pr_info(MPT3SAS_FMT
  940. "removing handle(0x%04x), wwid(0x%016llx)\n",
  941. ioc->name, pcie_device->handle,
  942. (unsigned long long) pcie_device->wwid);
  943. if (pcie_device->enclosure_handle != 0)
  944. pr_info(MPT3SAS_FMT
  945. "removing enclosure logical id(0x%016llx), slot(%d)\n",
  946. ioc->name,
  947. (unsigned long long)pcie_device->enclosure_logical_id,
  948. pcie_device->slot);
  949. if (pcie_device->connector_name[0] != '\0')
  950. pr_info(MPT3SAS_FMT
  951. "removing enclosure level(0x%04x), connector name( %s)\n",
  952. ioc->name, pcie_device->enclosure_level,
  953. pcie_device->connector_name);
  954. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  955. if (!list_empty(&pcie_device->list)) {
  956. list_del_init(&pcie_device->list);
  957. was_on_pcie_device_list = 1;
  958. }
  959. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  960. if (was_on_pcie_device_list) {
  961. kfree(pcie_device->serial_number);
  962. pcie_device_put(pcie_device);
  963. }
  964. }
  965. /**
  966. * _scsih_pcie_device_remove_by_handle - removing pcie device object by handle
  967. * @ioc: per adapter object
  968. * @handle: device handle
  969. *
  970. * Return nothing.
  971. */
  972. static void
  973. _scsih_pcie_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  974. {
  975. struct _pcie_device *pcie_device;
  976. unsigned long flags;
  977. int was_on_pcie_device_list = 0;
  978. if (ioc->shost_recovery)
  979. return;
  980. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  981. pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
  982. if (pcie_device) {
  983. if (!list_empty(&pcie_device->list)) {
  984. list_del_init(&pcie_device->list);
  985. was_on_pcie_device_list = 1;
  986. pcie_device_put(pcie_device);
  987. }
  988. }
  989. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  990. if (was_on_pcie_device_list) {
  991. _scsih_pcie_device_remove_from_sml(ioc, pcie_device);
  992. pcie_device_put(pcie_device);
  993. }
  994. }
  995. /**
  996. * _scsih_pcie_device_add - add pcie_device object
  997. * @ioc: per adapter object
  998. * @pcie_device: pcie_device object
  999. *
  1000. * This is added to the pcie_device_list link list.
  1001. */
  1002. static void
  1003. _scsih_pcie_device_add(struct MPT3SAS_ADAPTER *ioc,
  1004. struct _pcie_device *pcie_device)
  1005. {
  1006. unsigned long flags;
  1007. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1008. "%s: handle (0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  1009. pcie_device->handle, (unsigned long long)pcie_device->wwid));
  1010. if (pcie_device->enclosure_handle != 0)
  1011. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1012. "%s: enclosure logical id(0x%016llx), slot( %d)\n",
  1013. ioc->name, __func__,
  1014. (unsigned long long)pcie_device->enclosure_logical_id,
  1015. pcie_device->slot));
  1016. if (pcie_device->connector_name[0] != '\0')
  1017. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1018. "%s: enclosure level(0x%04x), connector name( %s)\n",
  1019. ioc->name, __func__, pcie_device->enclosure_level,
  1020. pcie_device->connector_name));
  1021. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1022. pcie_device_get(pcie_device);
  1023. list_add_tail(&pcie_device->list, &ioc->pcie_device_list);
  1024. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1025. if (scsi_add_device(ioc->shost, PCIE_CHANNEL, pcie_device->id, 0)) {
  1026. _scsih_pcie_device_remove(ioc, pcie_device);
  1027. } else if (!pcie_device->starget) {
  1028. if (!ioc->is_driver_loading) {
  1029. /*TODO-- Need to find out whether this condition will occur or not*/
  1030. clear_bit(pcie_device->handle, ioc->pend_os_device_add);
  1031. }
  1032. } else
  1033. clear_bit(pcie_device->handle, ioc->pend_os_device_add);
  1034. }
  1035. /*
  1036. * _scsih_pcie_device_init_add - insert pcie_device to the init list.
  1037. * @ioc: per adapter object
  1038. * @pcie_device: the pcie_device object
  1039. * Context: This function will acquire ioc->pcie_device_lock.
  1040. *
  1041. * Adding new object at driver load time to the ioc->pcie_device_init_list.
  1042. */
  1043. static void
  1044. _scsih_pcie_device_init_add(struct MPT3SAS_ADAPTER *ioc,
  1045. struct _pcie_device *pcie_device)
  1046. {
  1047. unsigned long flags;
  1048. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1049. "%s: handle (0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  1050. pcie_device->handle, (unsigned long long)pcie_device->wwid));
  1051. if (pcie_device->enclosure_handle != 0)
  1052. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1053. "%s: enclosure logical id(0x%016llx), slot( %d)\n",
  1054. ioc->name, __func__,
  1055. (unsigned long long)pcie_device->enclosure_logical_id,
  1056. pcie_device->slot));
  1057. if (pcie_device->connector_name[0] != '\0')
  1058. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1059. "%s: enclosure level(0x%04x), connector name( %s)\n",
  1060. ioc->name, __func__, pcie_device->enclosure_level,
  1061. pcie_device->connector_name));
  1062. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1063. pcie_device_get(pcie_device);
  1064. list_add_tail(&pcie_device->list, &ioc->pcie_device_init_list);
  1065. _scsih_determine_boot_device(ioc, pcie_device, PCIE_CHANNEL);
  1066. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1067. }
  1068. /**
  1069. * _scsih_raid_device_find_by_id - raid device search
  1070. * @ioc: per adapter object
  1071. * @id: sas device target id
  1072. * @channel: sas device channel
  1073. * Context: Calling function should acquire ioc->raid_device_lock
  1074. *
  1075. * This searches for raid_device based on target id, then return raid_device
  1076. * object.
  1077. */
  1078. static struct _raid_device *
  1079. _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
  1080. {
  1081. struct _raid_device *raid_device, *r;
  1082. r = NULL;
  1083. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1084. if (raid_device->id == id && raid_device->channel == channel) {
  1085. r = raid_device;
  1086. goto out;
  1087. }
  1088. }
  1089. out:
  1090. return r;
  1091. }
  1092. /**
  1093. * mpt3sas_raid_device_find_by_handle - raid device search
  1094. * @ioc: per adapter object
  1095. * @handle: sas device handle (assigned by firmware)
  1096. * Context: Calling function should acquire ioc->raid_device_lock
  1097. *
  1098. * This searches for raid_device based on handle, then return raid_device
  1099. * object.
  1100. */
  1101. struct _raid_device *
  1102. mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  1103. {
  1104. struct _raid_device *raid_device, *r;
  1105. r = NULL;
  1106. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1107. if (raid_device->handle != handle)
  1108. continue;
  1109. r = raid_device;
  1110. goto out;
  1111. }
  1112. out:
  1113. return r;
  1114. }
  1115. /**
  1116. * _scsih_raid_device_find_by_wwid - raid device search
  1117. * @ioc: per adapter object
  1118. * @handle: sas device handle (assigned by firmware)
  1119. * Context: Calling function should acquire ioc->raid_device_lock
  1120. *
  1121. * This searches for raid_device based on wwid, then return raid_device
  1122. * object.
  1123. */
  1124. static struct _raid_device *
  1125. _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
  1126. {
  1127. struct _raid_device *raid_device, *r;
  1128. r = NULL;
  1129. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1130. if (raid_device->wwid != wwid)
  1131. continue;
  1132. r = raid_device;
  1133. goto out;
  1134. }
  1135. out:
  1136. return r;
  1137. }
  1138. /**
  1139. * _scsih_raid_device_add - add raid_device object
  1140. * @ioc: per adapter object
  1141. * @raid_device: raid_device object
  1142. *
  1143. * This is added to the raid_device_list link list.
  1144. */
  1145. static void
  1146. _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
  1147. struct _raid_device *raid_device)
  1148. {
  1149. unsigned long flags;
  1150. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  1151. "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  1152. raid_device->handle, (unsigned long long)raid_device->wwid));
  1153. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1154. list_add_tail(&raid_device->list, &ioc->raid_device_list);
  1155. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1156. }
  1157. /**
  1158. * _scsih_raid_device_remove - delete raid_device object
  1159. * @ioc: per adapter object
  1160. * @raid_device: raid_device object
  1161. *
  1162. */
  1163. static void
  1164. _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
  1165. struct _raid_device *raid_device)
  1166. {
  1167. unsigned long flags;
  1168. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1169. list_del(&raid_device->list);
  1170. kfree(raid_device);
  1171. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1172. }
  1173. /**
  1174. * mpt3sas_scsih_expander_find_by_handle - expander device search
  1175. * @ioc: per adapter object
  1176. * @handle: expander handle (assigned by firmware)
  1177. * Context: Calling function should acquire ioc->sas_device_lock
  1178. *
  1179. * This searches for expander device based on handle, then returns the
  1180. * sas_node object.
  1181. */
  1182. struct _sas_node *
  1183. mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  1184. {
  1185. struct _sas_node *sas_expander, *r;
  1186. r = NULL;
  1187. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  1188. if (sas_expander->handle != handle)
  1189. continue;
  1190. r = sas_expander;
  1191. goto out;
  1192. }
  1193. out:
  1194. return r;
  1195. }
  1196. /**
  1197. * mpt3sas_scsih_expander_find_by_sas_address - expander device search
  1198. * @ioc: per adapter object
  1199. * @sas_address: sas address
  1200. * Context: Calling function should acquire ioc->sas_node_lock.
  1201. *
  1202. * This searches for expander device based on sas_address, then returns the
  1203. * sas_node object.
  1204. */
  1205. struct _sas_node *
  1206. mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  1207. u64 sas_address)
  1208. {
  1209. struct _sas_node *sas_expander, *r;
  1210. r = NULL;
  1211. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  1212. if (sas_expander->sas_address != sas_address)
  1213. continue;
  1214. r = sas_expander;
  1215. goto out;
  1216. }
  1217. out:
  1218. return r;
  1219. }
  1220. /**
  1221. * _scsih_expander_node_add - insert expander device to the list.
  1222. * @ioc: per adapter object
  1223. * @sas_expander: the sas_device object
  1224. * Context: This function will acquire ioc->sas_node_lock.
  1225. *
  1226. * Adding new object to the ioc->sas_expander_list.
  1227. *
  1228. * Return nothing.
  1229. */
  1230. static void
  1231. _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
  1232. struct _sas_node *sas_expander)
  1233. {
  1234. unsigned long flags;
  1235. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1236. list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
  1237. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1238. }
  1239. /**
  1240. * _scsih_is_end_device - determines if device is an end device
  1241. * @device_info: bitfield providing information about the device.
  1242. * Context: none
  1243. *
  1244. * Returns 1 if end device.
  1245. */
  1246. static int
  1247. _scsih_is_end_device(u32 device_info)
  1248. {
  1249. if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
  1250. ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
  1251. (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
  1252. (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
  1253. return 1;
  1254. else
  1255. return 0;
  1256. }
  1257. /**
  1258. * _scsih_is_nvme_device - determines if device is an nvme device
  1259. * @device_info: bitfield providing information about the device.
  1260. * Context: none
  1261. *
  1262. * Returns 1 if nvme device.
  1263. */
  1264. static int
  1265. _scsih_is_nvme_device(u32 device_info)
  1266. {
  1267. if ((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
  1268. == MPI26_PCIE_DEVINFO_NVME)
  1269. return 1;
  1270. else
  1271. return 0;
  1272. }
  1273. /**
  1274. * _scsih_scsi_lookup_get - returns scmd entry
  1275. * @ioc: per adapter object
  1276. * @smid: system request message index
  1277. *
  1278. * Returns the smid stored scmd pointer.
  1279. */
  1280. static struct scsi_cmnd *
  1281. _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1282. {
  1283. return ioc->scsi_lookup[smid - 1].scmd;
  1284. }
  1285. /**
  1286. * __scsih_scsi_lookup_get_clear - returns scmd entry without
  1287. * holding any lock.
  1288. * @ioc: per adapter object
  1289. * @smid: system request message index
  1290. *
  1291. * Returns the smid stored scmd pointer.
  1292. * Then will dereference the stored scmd pointer.
  1293. */
  1294. static inline struct scsi_cmnd *
  1295. __scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc,
  1296. u16 smid)
  1297. {
  1298. struct scsi_cmnd *scmd = NULL;
  1299. swap(scmd, ioc->scsi_lookup[smid - 1].scmd);
  1300. return scmd;
  1301. }
  1302. /**
  1303. * _scsih_scsi_lookup_get_clear - returns scmd entry
  1304. * @ioc: per adapter object
  1305. * @smid: system request message index
  1306. *
  1307. * Returns the smid stored scmd pointer.
  1308. * Then will derefrence the stored scmd pointer.
  1309. */
  1310. static inline struct scsi_cmnd *
  1311. _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1312. {
  1313. unsigned long flags;
  1314. struct scsi_cmnd *scmd;
  1315. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1316. scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
  1317. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1318. return scmd;
  1319. }
  1320. /**
  1321. * _scsih_scsi_lookup_find_by_scmd - scmd lookup
  1322. * @ioc: per adapter object
  1323. * @smid: system request message index
  1324. * @scmd: pointer to scsi command object
  1325. * Context: This function will acquire ioc->scsi_lookup_lock.
  1326. *
  1327. * This will search for a scmd pointer in the scsi_lookup array,
  1328. * returning the revelent smid. A returned value of zero means invalid.
  1329. */
  1330. static u16
  1331. _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
  1332. *scmd)
  1333. {
  1334. u16 smid;
  1335. unsigned long flags;
  1336. int i;
  1337. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1338. smid = 0;
  1339. for (i = 0; i < ioc->scsiio_depth; i++) {
  1340. if (ioc->scsi_lookup[i].scmd == scmd) {
  1341. smid = ioc->scsi_lookup[i].smid;
  1342. goto out;
  1343. }
  1344. }
  1345. out:
  1346. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1347. return smid;
  1348. }
  1349. /**
  1350. * _scsih_scsi_lookup_find_by_target - search for matching channel:id
  1351. * @ioc: per adapter object
  1352. * @id: target id
  1353. * @channel: channel
  1354. * Context: This function will acquire ioc->scsi_lookup_lock.
  1355. *
  1356. * This will search for a matching channel:id in the scsi_lookup array,
  1357. * returning 1 if found.
  1358. */
  1359. static u8
  1360. _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
  1361. int channel)
  1362. {
  1363. u8 found;
  1364. unsigned long flags;
  1365. int i;
  1366. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1367. found = 0;
  1368. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  1369. if (ioc->scsi_lookup[i].scmd &&
  1370. (ioc->scsi_lookup[i].scmd->device->id == id &&
  1371. ioc->scsi_lookup[i].scmd->device->channel == channel)) {
  1372. found = 1;
  1373. goto out;
  1374. }
  1375. }
  1376. out:
  1377. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1378. return found;
  1379. }
  1380. /**
  1381. * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
  1382. * @ioc: per adapter object
  1383. * @id: target id
  1384. * @lun: lun number
  1385. * @channel: channel
  1386. * Context: This function will acquire ioc->scsi_lookup_lock.
  1387. *
  1388. * This will search for a matching channel:id:lun in the scsi_lookup array,
  1389. * returning 1 if found.
  1390. */
  1391. static u8
  1392. _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
  1393. unsigned int lun, int channel)
  1394. {
  1395. u8 found;
  1396. unsigned long flags;
  1397. int i;
  1398. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1399. found = 0;
  1400. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  1401. if (ioc->scsi_lookup[i].scmd &&
  1402. (ioc->scsi_lookup[i].scmd->device->id == id &&
  1403. ioc->scsi_lookup[i].scmd->device->channel == channel &&
  1404. ioc->scsi_lookup[i].scmd->device->lun == lun)) {
  1405. found = 1;
  1406. goto out;
  1407. }
  1408. }
  1409. out:
  1410. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1411. return found;
  1412. }
  1413. /**
  1414. * scsih_change_queue_depth - setting device queue depth
  1415. * @sdev: scsi device struct
  1416. * @qdepth: requested queue depth
  1417. *
  1418. * Returns queue depth.
  1419. */
  1420. static int
  1421. scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1422. {
  1423. struct Scsi_Host *shost = sdev->host;
  1424. int max_depth;
  1425. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1426. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1427. struct MPT3SAS_TARGET *sas_target_priv_data;
  1428. struct _sas_device *sas_device;
  1429. unsigned long flags;
  1430. max_depth = shost->can_queue;
  1431. /* limit max device queue for SATA to 32 */
  1432. sas_device_priv_data = sdev->hostdata;
  1433. if (!sas_device_priv_data)
  1434. goto not_sata;
  1435. sas_target_priv_data = sas_device_priv_data->sas_target;
  1436. if (!sas_target_priv_data)
  1437. goto not_sata;
  1438. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
  1439. goto not_sata;
  1440. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1441. sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
  1442. if (sas_device) {
  1443. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1444. max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
  1445. sas_device_put(sas_device);
  1446. }
  1447. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1448. not_sata:
  1449. if (!sdev->tagged_supported)
  1450. max_depth = 1;
  1451. if (qdepth > max_depth)
  1452. qdepth = max_depth;
  1453. return scsi_change_queue_depth(sdev, qdepth);
  1454. }
  1455. /**
  1456. * scsih_target_alloc - target add routine
  1457. * @starget: scsi target struct
  1458. *
  1459. * Returns 0 if ok. Any other return is assumed to be an error and
  1460. * the device is ignored.
  1461. */
  1462. static int
  1463. scsih_target_alloc(struct scsi_target *starget)
  1464. {
  1465. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1466. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1467. struct MPT3SAS_TARGET *sas_target_priv_data;
  1468. struct _sas_device *sas_device;
  1469. struct _raid_device *raid_device;
  1470. struct _pcie_device *pcie_device;
  1471. unsigned long flags;
  1472. struct sas_rphy *rphy;
  1473. sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
  1474. GFP_KERNEL);
  1475. if (!sas_target_priv_data)
  1476. return -ENOMEM;
  1477. starget->hostdata = sas_target_priv_data;
  1478. sas_target_priv_data->starget = starget;
  1479. sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  1480. /* RAID volumes */
  1481. if (starget->channel == RAID_CHANNEL) {
  1482. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1483. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1484. starget->channel);
  1485. if (raid_device) {
  1486. sas_target_priv_data->handle = raid_device->handle;
  1487. sas_target_priv_data->sas_address = raid_device->wwid;
  1488. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
  1489. if (ioc->is_warpdrive)
  1490. sas_target_priv_data->raid_device = raid_device;
  1491. raid_device->starget = starget;
  1492. }
  1493. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1494. return 0;
  1495. }
  1496. /* PCIe devices */
  1497. if (starget->channel == PCIE_CHANNEL) {
  1498. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1499. pcie_device = __mpt3sas_get_pdev_by_idchannel(ioc, starget->id,
  1500. starget->channel);
  1501. if (pcie_device) {
  1502. sas_target_priv_data->handle = pcie_device->handle;
  1503. sas_target_priv_data->sas_address = pcie_device->wwid;
  1504. sas_target_priv_data->pcie_dev = pcie_device;
  1505. pcie_device->starget = starget;
  1506. pcie_device->id = starget->id;
  1507. pcie_device->channel = starget->channel;
  1508. sas_target_priv_data->flags |=
  1509. MPT_TARGET_FLAGS_PCIE_DEVICE;
  1510. if (pcie_device->fast_path)
  1511. sas_target_priv_data->flags |=
  1512. MPT_TARGET_FASTPATH_IO;
  1513. }
  1514. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1515. return 0;
  1516. }
  1517. /* sas/sata devices */
  1518. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1519. rphy = dev_to_rphy(starget->dev.parent);
  1520. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1521. rphy->identify.sas_address);
  1522. if (sas_device) {
  1523. sas_target_priv_data->handle = sas_device->handle;
  1524. sas_target_priv_data->sas_address = sas_device->sas_address;
  1525. sas_target_priv_data->sas_dev = sas_device;
  1526. sas_device->starget = starget;
  1527. sas_device->id = starget->id;
  1528. sas_device->channel = starget->channel;
  1529. if (test_bit(sas_device->handle, ioc->pd_handles))
  1530. sas_target_priv_data->flags |=
  1531. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1532. if (sas_device->fast_path)
  1533. sas_target_priv_data->flags |=
  1534. MPT_TARGET_FASTPATH_IO;
  1535. }
  1536. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1537. return 0;
  1538. }
  1539. /**
  1540. * scsih_target_destroy - target destroy routine
  1541. * @starget: scsi target struct
  1542. *
  1543. * Returns nothing.
  1544. */
  1545. static void
  1546. scsih_target_destroy(struct scsi_target *starget)
  1547. {
  1548. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1549. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1550. struct MPT3SAS_TARGET *sas_target_priv_data;
  1551. struct _sas_device *sas_device;
  1552. struct _raid_device *raid_device;
  1553. struct _pcie_device *pcie_device;
  1554. unsigned long flags;
  1555. struct sas_rphy *rphy;
  1556. sas_target_priv_data = starget->hostdata;
  1557. if (!sas_target_priv_data)
  1558. return;
  1559. if (starget->channel == RAID_CHANNEL) {
  1560. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1561. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1562. starget->channel);
  1563. if (raid_device) {
  1564. raid_device->starget = NULL;
  1565. raid_device->sdev = NULL;
  1566. }
  1567. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1568. goto out;
  1569. }
  1570. if (starget->channel == PCIE_CHANNEL) {
  1571. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1572. pcie_device = __mpt3sas_get_pdev_from_target(ioc,
  1573. sas_target_priv_data);
  1574. if (pcie_device && (pcie_device->starget == starget) &&
  1575. (pcie_device->id == starget->id) &&
  1576. (pcie_device->channel == starget->channel))
  1577. pcie_device->starget = NULL;
  1578. if (pcie_device) {
  1579. /*
  1580. * Corresponding get() is in _scsih_target_alloc()
  1581. */
  1582. sas_target_priv_data->pcie_dev = NULL;
  1583. pcie_device_put(pcie_device);
  1584. pcie_device_put(pcie_device);
  1585. }
  1586. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1587. goto out;
  1588. }
  1589. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1590. rphy = dev_to_rphy(starget->dev.parent);
  1591. sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
  1592. if (sas_device && (sas_device->starget == starget) &&
  1593. (sas_device->id == starget->id) &&
  1594. (sas_device->channel == starget->channel))
  1595. sas_device->starget = NULL;
  1596. if (sas_device) {
  1597. /*
  1598. * Corresponding get() is in _scsih_target_alloc()
  1599. */
  1600. sas_target_priv_data->sas_dev = NULL;
  1601. sas_device_put(sas_device);
  1602. sas_device_put(sas_device);
  1603. }
  1604. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1605. out:
  1606. kfree(sas_target_priv_data);
  1607. starget->hostdata = NULL;
  1608. }
  1609. /**
  1610. * scsih_slave_alloc - device add routine
  1611. * @sdev: scsi device struct
  1612. *
  1613. * Returns 0 if ok. Any other return is assumed to be an error and
  1614. * the device is ignored.
  1615. */
  1616. static int
  1617. scsih_slave_alloc(struct scsi_device *sdev)
  1618. {
  1619. struct Scsi_Host *shost;
  1620. struct MPT3SAS_ADAPTER *ioc;
  1621. struct MPT3SAS_TARGET *sas_target_priv_data;
  1622. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1623. struct scsi_target *starget;
  1624. struct _raid_device *raid_device;
  1625. struct _sas_device *sas_device;
  1626. struct _pcie_device *pcie_device;
  1627. unsigned long flags;
  1628. sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
  1629. GFP_KERNEL);
  1630. if (!sas_device_priv_data)
  1631. return -ENOMEM;
  1632. sas_device_priv_data->lun = sdev->lun;
  1633. sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
  1634. starget = scsi_target(sdev);
  1635. sas_target_priv_data = starget->hostdata;
  1636. sas_target_priv_data->num_luns++;
  1637. sas_device_priv_data->sas_target = sas_target_priv_data;
  1638. sdev->hostdata = sas_device_priv_data;
  1639. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
  1640. sdev->no_uld_attach = 1;
  1641. shost = dev_to_shost(&starget->dev);
  1642. ioc = shost_priv(shost);
  1643. if (starget->channel == RAID_CHANNEL) {
  1644. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1645. raid_device = _scsih_raid_device_find_by_id(ioc,
  1646. starget->id, starget->channel);
  1647. if (raid_device)
  1648. raid_device->sdev = sdev; /* raid is single lun */
  1649. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1650. }
  1651. if (starget->channel == PCIE_CHANNEL) {
  1652. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1653. pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
  1654. sas_target_priv_data->sas_address);
  1655. if (pcie_device && (pcie_device->starget == NULL)) {
  1656. sdev_printk(KERN_INFO, sdev,
  1657. "%s : pcie_device->starget set to starget @ %d\n",
  1658. __func__, __LINE__);
  1659. pcie_device->starget = starget;
  1660. }
  1661. if (pcie_device)
  1662. pcie_device_put(pcie_device);
  1663. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1664. } else if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
  1665. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1666. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1667. sas_target_priv_data->sas_address);
  1668. if (sas_device && (sas_device->starget == NULL)) {
  1669. sdev_printk(KERN_INFO, sdev,
  1670. "%s : sas_device->starget set to starget @ %d\n",
  1671. __func__, __LINE__);
  1672. sas_device->starget = starget;
  1673. }
  1674. if (sas_device)
  1675. sas_device_put(sas_device);
  1676. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1677. }
  1678. return 0;
  1679. }
  1680. /**
  1681. * scsih_slave_destroy - device destroy routine
  1682. * @sdev: scsi device struct
  1683. *
  1684. * Returns nothing.
  1685. */
  1686. static void
  1687. scsih_slave_destroy(struct scsi_device *sdev)
  1688. {
  1689. struct MPT3SAS_TARGET *sas_target_priv_data;
  1690. struct scsi_target *starget;
  1691. struct Scsi_Host *shost;
  1692. struct MPT3SAS_ADAPTER *ioc;
  1693. struct _sas_device *sas_device;
  1694. struct _pcie_device *pcie_device;
  1695. unsigned long flags;
  1696. if (!sdev->hostdata)
  1697. return;
  1698. starget = scsi_target(sdev);
  1699. sas_target_priv_data = starget->hostdata;
  1700. sas_target_priv_data->num_luns--;
  1701. shost = dev_to_shost(&starget->dev);
  1702. ioc = shost_priv(shost);
  1703. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
  1704. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  1705. pcie_device = __mpt3sas_get_pdev_from_target(ioc,
  1706. sas_target_priv_data);
  1707. if (pcie_device && !sas_target_priv_data->num_luns)
  1708. pcie_device->starget = NULL;
  1709. if (pcie_device)
  1710. pcie_device_put(pcie_device);
  1711. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  1712. } else if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
  1713. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1714. sas_device = __mpt3sas_get_sdev_from_target(ioc,
  1715. sas_target_priv_data);
  1716. if (sas_device && !sas_target_priv_data->num_luns)
  1717. sas_device->starget = NULL;
  1718. if (sas_device)
  1719. sas_device_put(sas_device);
  1720. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1721. }
  1722. kfree(sdev->hostdata);
  1723. sdev->hostdata = NULL;
  1724. }
  1725. /**
  1726. * _scsih_display_sata_capabilities - sata capabilities
  1727. * @ioc: per adapter object
  1728. * @handle: device handle
  1729. * @sdev: scsi device struct
  1730. */
  1731. static void
  1732. _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
  1733. u16 handle, struct scsi_device *sdev)
  1734. {
  1735. Mpi2ConfigReply_t mpi_reply;
  1736. Mpi2SasDevicePage0_t sas_device_pg0;
  1737. u32 ioc_status;
  1738. u16 flags;
  1739. u32 device_info;
  1740. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  1741. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  1742. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1743. ioc->name, __FILE__, __LINE__, __func__);
  1744. return;
  1745. }
  1746. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1747. MPI2_IOCSTATUS_MASK;
  1748. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1749. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1750. ioc->name, __FILE__, __LINE__, __func__);
  1751. return;
  1752. }
  1753. flags = le16_to_cpu(sas_device_pg0.Flags);
  1754. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  1755. sdev_printk(KERN_INFO, sdev,
  1756. "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
  1757. "sw_preserve(%s)\n",
  1758. (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
  1759. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
  1760. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
  1761. "n",
  1762. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
  1763. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
  1764. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
  1765. }
  1766. /*
  1767. * raid transport support -
  1768. * Enabled for SLES11 and newer, in older kernels the driver will panic when
  1769. * unloading the driver followed by a load - I believe that the subroutine
  1770. * raid_class_release() is not cleaning up properly.
  1771. */
  1772. /**
  1773. * scsih_is_raid - return boolean indicating device is raid volume
  1774. * @dev the device struct object
  1775. */
  1776. static int
  1777. scsih_is_raid(struct device *dev)
  1778. {
  1779. struct scsi_device *sdev = to_scsi_device(dev);
  1780. struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1781. if (ioc->is_warpdrive)
  1782. return 0;
  1783. return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
  1784. }
  1785. static int
  1786. scsih_is_nvme(struct device *dev)
  1787. {
  1788. struct scsi_device *sdev = to_scsi_device(dev);
  1789. return (sdev->channel == PCIE_CHANNEL) ? 1 : 0;
  1790. }
  1791. /**
  1792. * scsih_get_resync - get raid volume resync percent complete
  1793. * @dev the device struct object
  1794. */
  1795. static void
  1796. scsih_get_resync(struct device *dev)
  1797. {
  1798. struct scsi_device *sdev = to_scsi_device(dev);
  1799. struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1800. static struct _raid_device *raid_device;
  1801. unsigned long flags;
  1802. Mpi2RaidVolPage0_t vol_pg0;
  1803. Mpi2ConfigReply_t mpi_reply;
  1804. u32 volume_status_flags;
  1805. u8 percent_complete;
  1806. u16 handle;
  1807. percent_complete = 0;
  1808. handle = 0;
  1809. if (ioc->is_warpdrive)
  1810. goto out;
  1811. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1812. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1813. sdev->channel);
  1814. if (raid_device) {
  1815. handle = raid_device->handle;
  1816. percent_complete = raid_device->percent_complete;
  1817. }
  1818. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1819. if (!handle)
  1820. goto out;
  1821. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1822. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  1823. sizeof(Mpi2RaidVolPage0_t))) {
  1824. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1825. ioc->name, __FILE__, __LINE__, __func__);
  1826. percent_complete = 0;
  1827. goto out;
  1828. }
  1829. volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1830. if (!(volume_status_flags &
  1831. MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
  1832. percent_complete = 0;
  1833. out:
  1834. switch (ioc->hba_mpi_version_belonged) {
  1835. case MPI2_VERSION:
  1836. raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
  1837. break;
  1838. case MPI25_VERSION:
  1839. case MPI26_VERSION:
  1840. raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
  1841. break;
  1842. }
  1843. }
  1844. /**
  1845. * scsih_get_state - get raid volume level
  1846. * @dev the device struct object
  1847. */
  1848. static void
  1849. scsih_get_state(struct device *dev)
  1850. {
  1851. struct scsi_device *sdev = to_scsi_device(dev);
  1852. struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1853. static struct _raid_device *raid_device;
  1854. unsigned long flags;
  1855. Mpi2RaidVolPage0_t vol_pg0;
  1856. Mpi2ConfigReply_t mpi_reply;
  1857. u32 volstate;
  1858. enum raid_state state = RAID_STATE_UNKNOWN;
  1859. u16 handle = 0;
  1860. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1861. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1862. sdev->channel);
  1863. if (raid_device)
  1864. handle = raid_device->handle;
  1865. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1866. if (!raid_device)
  1867. goto out;
  1868. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1869. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  1870. sizeof(Mpi2RaidVolPage0_t))) {
  1871. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1872. ioc->name, __FILE__, __LINE__, __func__);
  1873. goto out;
  1874. }
  1875. volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1876. if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
  1877. state = RAID_STATE_RESYNCING;
  1878. goto out;
  1879. }
  1880. switch (vol_pg0.VolumeState) {
  1881. case MPI2_RAID_VOL_STATE_OPTIMAL:
  1882. case MPI2_RAID_VOL_STATE_ONLINE:
  1883. state = RAID_STATE_ACTIVE;
  1884. break;
  1885. case MPI2_RAID_VOL_STATE_DEGRADED:
  1886. state = RAID_STATE_DEGRADED;
  1887. break;
  1888. case MPI2_RAID_VOL_STATE_FAILED:
  1889. case MPI2_RAID_VOL_STATE_MISSING:
  1890. state = RAID_STATE_OFFLINE;
  1891. break;
  1892. }
  1893. out:
  1894. switch (ioc->hba_mpi_version_belonged) {
  1895. case MPI2_VERSION:
  1896. raid_set_state(mpt2sas_raid_template, dev, state);
  1897. break;
  1898. case MPI25_VERSION:
  1899. case MPI26_VERSION:
  1900. raid_set_state(mpt3sas_raid_template, dev, state);
  1901. break;
  1902. }
  1903. }
  1904. /**
  1905. * _scsih_set_level - set raid level
  1906. * @sdev: scsi device struct
  1907. * @volume_type: volume type
  1908. */
  1909. static void
  1910. _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
  1911. struct scsi_device *sdev, u8 volume_type)
  1912. {
  1913. enum raid_level level = RAID_LEVEL_UNKNOWN;
  1914. switch (volume_type) {
  1915. case MPI2_RAID_VOL_TYPE_RAID0:
  1916. level = RAID_LEVEL_0;
  1917. break;
  1918. case MPI2_RAID_VOL_TYPE_RAID10:
  1919. level = RAID_LEVEL_10;
  1920. break;
  1921. case MPI2_RAID_VOL_TYPE_RAID1E:
  1922. level = RAID_LEVEL_1E;
  1923. break;
  1924. case MPI2_RAID_VOL_TYPE_RAID1:
  1925. level = RAID_LEVEL_1;
  1926. break;
  1927. }
  1928. switch (ioc->hba_mpi_version_belonged) {
  1929. case MPI2_VERSION:
  1930. raid_set_level(mpt2sas_raid_template,
  1931. &sdev->sdev_gendev, level);
  1932. break;
  1933. case MPI25_VERSION:
  1934. case MPI26_VERSION:
  1935. raid_set_level(mpt3sas_raid_template,
  1936. &sdev->sdev_gendev, level);
  1937. break;
  1938. }
  1939. }
  1940. /**
  1941. * _scsih_get_volume_capabilities - volume capabilities
  1942. * @ioc: per adapter object
  1943. * @sas_device: the raid_device object
  1944. *
  1945. * Returns 0 for success, else 1
  1946. */
  1947. static int
  1948. _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
  1949. struct _raid_device *raid_device)
  1950. {
  1951. Mpi2RaidVolPage0_t *vol_pg0;
  1952. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1953. Mpi2SasDevicePage0_t sas_device_pg0;
  1954. Mpi2ConfigReply_t mpi_reply;
  1955. u16 sz;
  1956. u8 num_pds;
  1957. if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
  1958. &num_pds)) || !num_pds) {
  1959. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1960. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1961. __func__));
  1962. return 1;
  1963. }
  1964. raid_device->num_pds = num_pds;
  1965. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1966. sizeof(Mpi2RaidVol0PhysDisk_t));
  1967. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1968. if (!vol_pg0) {
  1969. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1970. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1971. __func__));
  1972. return 1;
  1973. }
  1974. if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1975. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1976. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1977. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1978. __func__));
  1979. kfree(vol_pg0);
  1980. return 1;
  1981. }
  1982. raid_device->volume_type = vol_pg0->VolumeType;
  1983. /* figure out what the underlying devices are by
  1984. * obtaining the device_info bits for the 1st device
  1985. */
  1986. if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1987. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1988. vol_pg0->PhysDisk[0].PhysDiskNum))) {
  1989. if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  1990. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  1991. le16_to_cpu(pd_pg0.DevHandle)))) {
  1992. raid_device->device_info =
  1993. le32_to_cpu(sas_device_pg0.DeviceInfo);
  1994. }
  1995. }
  1996. kfree(vol_pg0);
  1997. return 0;
  1998. }
  1999. /**
  2000. * _scsih_enable_tlr - setting TLR flags
  2001. * @ioc: per adapter object
  2002. * @sdev: scsi device struct
  2003. *
  2004. * Enabling Transaction Layer Retries for tape devices when
  2005. * vpd page 0x90 is present
  2006. *
  2007. */
  2008. static void
  2009. _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
  2010. {
  2011. /* only for TAPE */
  2012. if (sdev->type != TYPE_TAPE)
  2013. return;
  2014. if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
  2015. return;
  2016. sas_enable_tlr(sdev);
  2017. sdev_printk(KERN_INFO, sdev, "TLR %s\n",
  2018. sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
  2019. return;
  2020. }
  2021. /**
  2022. * scsih_slave_configure - device configure routine.
  2023. * @sdev: scsi device struct
  2024. *
  2025. * Returns 0 if ok. Any other return is assumed to be an error and
  2026. * the device is ignored.
  2027. */
  2028. static int
  2029. scsih_slave_configure(struct scsi_device *sdev)
  2030. {
  2031. struct Scsi_Host *shost = sdev->host;
  2032. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  2033. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2034. struct MPT3SAS_TARGET *sas_target_priv_data;
  2035. struct _sas_device *sas_device;
  2036. struct _pcie_device *pcie_device;
  2037. struct _raid_device *raid_device;
  2038. unsigned long flags;
  2039. int qdepth;
  2040. u8 ssp_target = 0;
  2041. char *ds = "";
  2042. char *r_level = "";
  2043. u16 handle, volume_handle = 0;
  2044. u64 volume_wwid = 0;
  2045. qdepth = 1;
  2046. sas_device_priv_data = sdev->hostdata;
  2047. sas_device_priv_data->configured_lun = 1;
  2048. sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
  2049. sas_target_priv_data = sas_device_priv_data->sas_target;
  2050. handle = sas_target_priv_data->handle;
  2051. /* raid volume handling */
  2052. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
  2053. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  2054. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  2055. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  2056. if (!raid_device) {
  2057. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  2058. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  2059. __LINE__, __func__));
  2060. return 1;
  2061. }
  2062. if (_scsih_get_volume_capabilities(ioc, raid_device)) {
  2063. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  2064. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  2065. __LINE__, __func__));
  2066. return 1;
  2067. }
  2068. /*
  2069. * WARPDRIVE: Initialize the required data for Direct IO
  2070. */
  2071. mpt3sas_init_warpdrive_properties(ioc, raid_device);
  2072. /* RAID Queue Depth Support
  2073. * IS volume = underlying qdepth of drive type, either
  2074. * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
  2075. * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
  2076. */
  2077. if (raid_device->device_info &
  2078. MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  2079. qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
  2080. ds = "SSP";
  2081. } else {
  2082. qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
  2083. if (raid_device->device_info &
  2084. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  2085. ds = "SATA";
  2086. else
  2087. ds = "STP";
  2088. }
  2089. switch (raid_device->volume_type) {
  2090. case MPI2_RAID_VOL_TYPE_RAID0:
  2091. r_level = "RAID0";
  2092. break;
  2093. case MPI2_RAID_VOL_TYPE_RAID1E:
  2094. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  2095. if (ioc->manu_pg10.OEMIdentifier &&
  2096. (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
  2097. MFG10_GF0_R10_DISPLAY) &&
  2098. !(raid_device->num_pds % 2))
  2099. r_level = "RAID10";
  2100. else
  2101. r_level = "RAID1E";
  2102. break;
  2103. case MPI2_RAID_VOL_TYPE_RAID1:
  2104. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  2105. r_level = "RAID1";
  2106. break;
  2107. case MPI2_RAID_VOL_TYPE_RAID10:
  2108. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  2109. r_level = "RAID10";
  2110. break;
  2111. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  2112. default:
  2113. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  2114. r_level = "RAIDX";
  2115. break;
  2116. }
  2117. if (!ioc->hide_ir_msg)
  2118. sdev_printk(KERN_INFO, sdev,
  2119. "%s: handle(0x%04x), wwid(0x%016llx),"
  2120. " pd_count(%d), type(%s)\n",
  2121. r_level, raid_device->handle,
  2122. (unsigned long long)raid_device->wwid,
  2123. raid_device->num_pds, ds);
  2124. if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
  2125. blk_queue_max_hw_sectors(sdev->request_queue,
  2126. MPT3SAS_RAID_MAX_SECTORS);
  2127. sdev_printk(KERN_INFO, sdev,
  2128. "Set queue's max_sector to: %u\n",
  2129. MPT3SAS_RAID_MAX_SECTORS);
  2130. }
  2131. scsih_change_queue_depth(sdev, qdepth);
  2132. /* raid transport support */
  2133. if (!ioc->is_warpdrive)
  2134. _scsih_set_level(ioc, sdev, raid_device->volume_type);
  2135. return 0;
  2136. }
  2137. /* non-raid handling */
  2138. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2139. if (mpt3sas_config_get_volume_handle(ioc, handle,
  2140. &volume_handle)) {
  2141. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  2142. "failure at %s:%d/%s()!\n", ioc->name,
  2143. __FILE__, __LINE__, __func__));
  2144. return 1;
  2145. }
  2146. if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
  2147. volume_handle, &volume_wwid)) {
  2148. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  2149. "failure at %s:%d/%s()!\n", ioc->name,
  2150. __FILE__, __LINE__, __func__));
  2151. return 1;
  2152. }
  2153. }
  2154. /* PCIe handling */
  2155. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
  2156. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  2157. pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
  2158. sas_device_priv_data->sas_target->sas_address);
  2159. if (!pcie_device) {
  2160. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  2161. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  2162. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  2163. __LINE__, __func__));
  2164. return 1;
  2165. }
  2166. qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
  2167. ds = "NVMe";
  2168. sdev_printk(KERN_INFO, sdev,
  2169. "%s: handle(0x%04x), wwid(0x%016llx), port(%d)\n",
  2170. ds, handle, (unsigned long long)pcie_device->wwid,
  2171. pcie_device->port_num);
  2172. if (pcie_device->enclosure_handle != 0)
  2173. sdev_printk(KERN_INFO, sdev,
  2174. "%s: enclosure logical id(0x%016llx), slot(%d)\n",
  2175. ds,
  2176. (unsigned long long)pcie_device->enclosure_logical_id,
  2177. pcie_device->slot);
  2178. if (pcie_device->connector_name[0] != '\0')
  2179. sdev_printk(KERN_INFO, sdev,
  2180. "%s: enclosure level(0x%04x),"
  2181. "connector name( %s)\n", ds,
  2182. pcie_device->enclosure_level,
  2183. pcie_device->connector_name);
  2184. pcie_device_put(pcie_device);
  2185. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  2186. scsih_change_queue_depth(sdev, qdepth);
  2187. if (pcie_device->nvme_mdts)
  2188. blk_queue_max_hw_sectors(sdev->request_queue,
  2189. pcie_device->nvme_mdts/512);
  2190. /* Enable QUEUE_FLAG_NOMERGES flag, so that IOs won't be
  2191. ** merged and can eliminate holes created during merging
  2192. ** operation.
  2193. **/
  2194. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES,
  2195. sdev->request_queue);
  2196. blk_queue_virt_boundary(sdev->request_queue,
  2197. ioc->page_size - 1);
  2198. return 0;
  2199. }
  2200. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2201. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  2202. sas_device_priv_data->sas_target->sas_address);
  2203. if (!sas_device) {
  2204. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2205. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  2206. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  2207. __func__));
  2208. return 1;
  2209. }
  2210. sas_device->volume_handle = volume_handle;
  2211. sas_device->volume_wwid = volume_wwid;
  2212. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  2213. qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
  2214. ssp_target = 1;
  2215. if (sas_device->device_info &
  2216. MPI2_SAS_DEVICE_INFO_SEP) {
  2217. sdev_printk(KERN_WARNING, sdev,
  2218. "set ignore_delay_remove for handle(0x%04x)\n",
  2219. sas_device_priv_data->sas_target->handle);
  2220. sas_device_priv_data->ignore_delay_remove = 1;
  2221. ds = "SES";
  2222. } else
  2223. ds = "SSP";
  2224. } else {
  2225. qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
  2226. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  2227. ds = "STP";
  2228. else if (sas_device->device_info &
  2229. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  2230. ds = "SATA";
  2231. }
  2232. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
  2233. "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
  2234. ds, handle, (unsigned long long)sas_device->sas_address,
  2235. sas_device->phy, (unsigned long long)sas_device->device_name);
  2236. _scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
  2237. sas_device_put(sas_device);
  2238. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2239. if (!ssp_target)
  2240. _scsih_display_sata_capabilities(ioc, handle, sdev);
  2241. scsih_change_queue_depth(sdev, qdepth);
  2242. if (ssp_target) {
  2243. sas_read_port_mode_page(sdev);
  2244. _scsih_enable_tlr(ioc, sdev);
  2245. }
  2246. return 0;
  2247. }
  2248. /**
  2249. * scsih_bios_param - fetch head, sector, cylinder info for a disk
  2250. * @sdev: scsi device struct
  2251. * @bdev: pointer to block device context
  2252. * @capacity: device size (in 512 byte sectors)
  2253. * @params: three element array to place output:
  2254. * params[0] number of heads (max 255)
  2255. * params[1] number of sectors (max 63)
  2256. * params[2] number of cylinders
  2257. *
  2258. * Return nothing.
  2259. */
  2260. static int
  2261. scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  2262. sector_t capacity, int params[])
  2263. {
  2264. int heads;
  2265. int sectors;
  2266. sector_t cylinders;
  2267. ulong dummy;
  2268. heads = 64;
  2269. sectors = 32;
  2270. dummy = heads * sectors;
  2271. cylinders = capacity;
  2272. sector_div(cylinders, dummy);
  2273. /*
  2274. * Handle extended translation size for logical drives
  2275. * > 1Gb
  2276. */
  2277. if ((ulong)capacity >= 0x200000) {
  2278. heads = 255;
  2279. sectors = 63;
  2280. dummy = heads * sectors;
  2281. cylinders = capacity;
  2282. sector_div(cylinders, dummy);
  2283. }
  2284. /* return result */
  2285. params[0] = heads;
  2286. params[1] = sectors;
  2287. params[2] = cylinders;
  2288. return 0;
  2289. }
  2290. /**
  2291. * _scsih_response_code - translation of device response code
  2292. * @ioc: per adapter object
  2293. * @response_code: response code returned by the device
  2294. *
  2295. * Return nothing.
  2296. */
  2297. static void
  2298. _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
  2299. {
  2300. char *desc;
  2301. switch (response_code) {
  2302. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  2303. desc = "task management request completed";
  2304. break;
  2305. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  2306. desc = "invalid frame";
  2307. break;
  2308. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  2309. desc = "task management request not supported";
  2310. break;
  2311. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  2312. desc = "task management request failed";
  2313. break;
  2314. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  2315. desc = "task management request succeeded";
  2316. break;
  2317. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  2318. desc = "invalid lun";
  2319. break;
  2320. case 0xA:
  2321. desc = "overlapped tag attempted";
  2322. break;
  2323. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  2324. desc = "task queued, however not sent to target";
  2325. break;
  2326. default:
  2327. desc = "unknown";
  2328. break;
  2329. }
  2330. pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
  2331. ioc->name, response_code, desc);
  2332. }
  2333. /**
  2334. * _scsih_tm_done - tm completion routine
  2335. * @ioc: per adapter object
  2336. * @smid: system request message index
  2337. * @msix_index: MSIX table index supplied by the OS
  2338. * @reply: reply message frame(lower 32bit addr)
  2339. * Context: none.
  2340. *
  2341. * The callback handler when using scsih_issue_tm.
  2342. *
  2343. * Return 1 meaning mf should be freed from _base_interrupt
  2344. * 0 means the mf is freed from this function.
  2345. */
  2346. static u8
  2347. _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  2348. {
  2349. MPI2DefaultReply_t *mpi_reply;
  2350. if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
  2351. return 1;
  2352. if (ioc->tm_cmds.smid != smid)
  2353. return 1;
  2354. ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
  2355. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  2356. if (mpi_reply) {
  2357. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  2358. ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
  2359. }
  2360. ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
  2361. complete(&ioc->tm_cmds.done);
  2362. return 1;
  2363. }
  2364. /**
  2365. * mpt3sas_scsih_set_tm_flag - set per target tm_busy
  2366. * @ioc: per adapter object
  2367. * @handle: device handle
  2368. *
  2369. * During taskmangement request, we need to freeze the device queue.
  2370. */
  2371. void
  2372. mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  2373. {
  2374. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2375. struct scsi_device *sdev;
  2376. u8 skip = 0;
  2377. shost_for_each_device(sdev, ioc->shost) {
  2378. if (skip)
  2379. continue;
  2380. sas_device_priv_data = sdev->hostdata;
  2381. if (!sas_device_priv_data)
  2382. continue;
  2383. if (sas_device_priv_data->sas_target->handle == handle) {
  2384. sas_device_priv_data->sas_target->tm_busy = 1;
  2385. skip = 1;
  2386. ioc->ignore_loginfos = 1;
  2387. }
  2388. }
  2389. }
  2390. /**
  2391. * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
  2392. * @ioc: per adapter object
  2393. * @handle: device handle
  2394. *
  2395. * During taskmangement request, we need to freeze the device queue.
  2396. */
  2397. void
  2398. mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  2399. {
  2400. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2401. struct scsi_device *sdev;
  2402. u8 skip = 0;
  2403. shost_for_each_device(sdev, ioc->shost) {
  2404. if (skip)
  2405. continue;
  2406. sas_device_priv_data = sdev->hostdata;
  2407. if (!sas_device_priv_data)
  2408. continue;
  2409. if (sas_device_priv_data->sas_target->handle == handle) {
  2410. sas_device_priv_data->sas_target->tm_busy = 0;
  2411. skip = 1;
  2412. ioc->ignore_loginfos = 0;
  2413. }
  2414. }
  2415. }
  2416. /**
  2417. * mpt3sas_scsih_issue_tm - main routine for sending tm requests
  2418. * @ioc: per adapter struct
  2419. * @device_handle: device handle
  2420. * @channel: the channel assigned by the OS
  2421. * @id: the id assigned by the OS
  2422. * @lun: lun number
  2423. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  2424. * @smid_task: smid assigned to the task
  2425. * @timeout: timeout in seconds
  2426. * Context: user
  2427. *
  2428. * A generic API for sending task management requests to firmware.
  2429. *
  2430. * The callback index is set inside `ioc->tm_cb_idx`.
  2431. *
  2432. * Return SUCCESS or FAILED.
  2433. */
  2434. int
  2435. mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
  2436. uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
  2437. {
  2438. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2439. Mpi2SCSITaskManagementReply_t *mpi_reply;
  2440. u16 smid = 0;
  2441. u32 ioc_state;
  2442. struct scsiio_tracker *scsi_lookup = NULL;
  2443. int rc;
  2444. u16 msix_task = 0;
  2445. lockdep_assert_held(&ioc->tm_cmds.mutex);
  2446. if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
  2447. pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
  2448. __func__, ioc->name);
  2449. return FAILED;
  2450. }
  2451. if (ioc->shost_recovery || ioc->remove_host ||
  2452. ioc->pci_error_recovery) {
  2453. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  2454. __func__, ioc->name);
  2455. return FAILED;
  2456. }
  2457. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  2458. if (ioc_state & MPI2_DOORBELL_USED) {
  2459. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2460. "unexpected doorbell active!\n", ioc->name));
  2461. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  2462. return (!rc) ? SUCCESS : FAILED;
  2463. }
  2464. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  2465. mpt3sas_base_fault_info(ioc, ioc_state &
  2466. MPI2_DOORBELL_DATA_MASK);
  2467. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  2468. return (!rc) ? SUCCESS : FAILED;
  2469. }
  2470. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
  2471. if (!smid) {
  2472. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  2473. ioc->name, __func__);
  2474. return FAILED;
  2475. }
  2476. if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  2477. scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
  2478. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  2479. "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
  2480. ioc->name, handle, type, smid_task));
  2481. ioc->tm_cmds.status = MPT3_CMD_PENDING;
  2482. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2483. ioc->tm_cmds.smid = smid;
  2484. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2485. memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
  2486. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2487. mpi_request->DevHandle = cpu_to_le16(handle);
  2488. mpi_request->TaskType = type;
  2489. mpi_request->TaskMID = cpu_to_le16(smid_task);
  2490. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  2491. mpt3sas_scsih_set_tm_flag(ioc, handle);
  2492. init_completion(&ioc->tm_cmds.done);
  2493. if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
  2494. (scsi_lookup->msix_io < ioc->reply_queue_count))
  2495. msix_task = scsi_lookup->msix_io;
  2496. else
  2497. msix_task = 0;
  2498. ioc->put_smid_hi_priority(ioc, smid, msix_task);
  2499. wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  2500. if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
  2501. pr_err(MPT3SAS_FMT "%s: timeout\n",
  2502. ioc->name, __func__);
  2503. _debug_dump_mf(mpi_request,
  2504. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  2505. if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
  2506. rc = mpt3sas_base_hard_reset_handler(ioc,
  2507. FORCE_BIG_HAMMER);
  2508. rc = (!rc) ? SUCCESS : FAILED;
  2509. goto out;
  2510. }
  2511. }
  2512. /* sync IRQs in case those were busy during flush. */
  2513. mpt3sas_base_sync_reply_irqs(ioc);
  2514. if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
  2515. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
  2516. mpi_reply = ioc->tm_cmds.reply;
  2517. dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
  2518. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  2519. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  2520. le32_to_cpu(mpi_reply->IOCLogInfo),
  2521. le32_to_cpu(mpi_reply->TerminationCount)));
  2522. if (ioc->logging_level & MPT_DEBUG_TM) {
  2523. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  2524. if (mpi_reply->IOCStatus)
  2525. _debug_dump_mf(mpi_request,
  2526. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  2527. }
  2528. }
  2529. switch (type) {
  2530. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  2531. rc = SUCCESS;
  2532. if (scsi_lookup->scmd == NULL)
  2533. break;
  2534. rc = FAILED;
  2535. break;
  2536. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  2537. if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
  2538. rc = FAILED;
  2539. else
  2540. rc = SUCCESS;
  2541. break;
  2542. case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
  2543. case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
  2544. if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
  2545. rc = FAILED;
  2546. else
  2547. rc = SUCCESS;
  2548. break;
  2549. case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
  2550. rc = SUCCESS;
  2551. break;
  2552. default:
  2553. rc = FAILED;
  2554. break;
  2555. }
  2556. out:
  2557. mpt3sas_scsih_clear_tm_flag(ioc, handle);
  2558. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  2559. return rc;
  2560. }
  2561. int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  2562. uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
  2563. {
  2564. int ret;
  2565. mutex_lock(&ioc->tm_cmds.mutex);
  2566. ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
  2567. smid_task, timeout);
  2568. mutex_unlock(&ioc->tm_cmds.mutex);
  2569. return ret;
  2570. }
  2571. /**
  2572. * _scsih_tm_display_info - displays info about the device
  2573. * @ioc: per adapter struct
  2574. * @scmd: pointer to scsi command object
  2575. *
  2576. * Called by task management callback handlers.
  2577. */
  2578. static void
  2579. _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
  2580. {
  2581. struct scsi_target *starget = scmd->device->sdev_target;
  2582. struct MPT3SAS_TARGET *priv_target = starget->hostdata;
  2583. struct _sas_device *sas_device = NULL;
  2584. struct _pcie_device *pcie_device = NULL;
  2585. unsigned long flags;
  2586. char *device_str = NULL;
  2587. if (!priv_target)
  2588. return;
  2589. if (ioc->hide_ir_msg)
  2590. device_str = "WarpDrive";
  2591. else
  2592. device_str = "volume";
  2593. scsi_print_command(scmd);
  2594. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  2595. starget_printk(KERN_INFO, starget,
  2596. "%s handle(0x%04x), %s wwid(0x%016llx)\n",
  2597. device_str, priv_target->handle,
  2598. device_str, (unsigned long long)priv_target->sas_address);
  2599. } else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
  2600. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  2601. pcie_device = __mpt3sas_get_pdev_from_target(ioc, priv_target);
  2602. if (pcie_device) {
  2603. starget_printk(KERN_INFO, starget,
  2604. "handle(0x%04x), wwid(0x%016llx), port(%d)\n",
  2605. pcie_device->handle,
  2606. (unsigned long long)pcie_device->wwid,
  2607. pcie_device->port_num);
  2608. if (pcie_device->enclosure_handle != 0)
  2609. starget_printk(KERN_INFO, starget,
  2610. "enclosure logical id(0x%016llx), slot(%d)\n",
  2611. (unsigned long long)
  2612. pcie_device->enclosure_logical_id,
  2613. pcie_device->slot);
  2614. if (pcie_device->connector_name[0] != '\0')
  2615. starget_printk(KERN_INFO, starget,
  2616. "enclosure level(0x%04x), connector name( %s)\n",
  2617. pcie_device->enclosure_level,
  2618. pcie_device->connector_name);
  2619. pcie_device_put(pcie_device);
  2620. }
  2621. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  2622. } else {
  2623. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2624. sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
  2625. if (sas_device) {
  2626. if (priv_target->flags &
  2627. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2628. starget_printk(KERN_INFO, starget,
  2629. "volume handle(0x%04x), "
  2630. "volume wwid(0x%016llx)\n",
  2631. sas_device->volume_handle,
  2632. (unsigned long long)sas_device->volume_wwid);
  2633. }
  2634. starget_printk(KERN_INFO, starget,
  2635. "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
  2636. sas_device->handle,
  2637. (unsigned long long)sas_device->sas_address,
  2638. sas_device->phy);
  2639. _scsih_display_enclosure_chassis_info(NULL, sas_device,
  2640. NULL, starget);
  2641. sas_device_put(sas_device);
  2642. }
  2643. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2644. }
  2645. }
  2646. /**
  2647. * scsih_abort - eh threads main abort routine
  2648. * @scmd: pointer to scsi command object
  2649. *
  2650. * Returns SUCCESS if command aborted else FAILED
  2651. */
  2652. static int
  2653. scsih_abort(struct scsi_cmnd *scmd)
  2654. {
  2655. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2656. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2657. u16 smid;
  2658. u16 handle;
  2659. int r;
  2660. sdev_printk(KERN_INFO, scmd->device,
  2661. "attempting task abort! scmd(%p)\n", scmd);
  2662. _scsih_tm_display_info(ioc, scmd);
  2663. sas_device_priv_data = scmd->device->hostdata;
  2664. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2665. sdev_printk(KERN_INFO, scmd->device,
  2666. "device been deleted! scmd(%p)\n", scmd);
  2667. scmd->result = DID_NO_CONNECT << 16;
  2668. scmd->scsi_done(scmd);
  2669. r = SUCCESS;
  2670. goto out;
  2671. }
  2672. /* search for the command */
  2673. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  2674. if (!smid) {
  2675. scmd->result = DID_RESET << 16;
  2676. r = SUCCESS;
  2677. goto out;
  2678. }
  2679. /* for hidden raid components and volumes this is not supported */
  2680. if (sas_device_priv_data->sas_target->flags &
  2681. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  2682. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  2683. scmd->result = DID_RESET << 16;
  2684. r = FAILED;
  2685. goto out;
  2686. }
  2687. mpt3sas_halt_firmware(ioc);
  2688. handle = sas_device_priv_data->sas_target->handle;
  2689. r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
  2690. scmd->device->id, scmd->device->lun,
  2691. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
  2692. out:
  2693. sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
  2694. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2695. return r;
  2696. }
  2697. /**
  2698. * scsih_dev_reset - eh threads main device reset routine
  2699. * @scmd: pointer to scsi command object
  2700. *
  2701. * Returns SUCCESS if command aborted else FAILED
  2702. */
  2703. static int
  2704. scsih_dev_reset(struct scsi_cmnd *scmd)
  2705. {
  2706. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2707. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2708. struct _sas_device *sas_device = NULL;
  2709. u16 handle;
  2710. int r;
  2711. struct scsi_target *starget = scmd->device->sdev_target;
  2712. struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
  2713. sdev_printk(KERN_INFO, scmd->device,
  2714. "attempting device reset! scmd(%p)\n", scmd);
  2715. _scsih_tm_display_info(ioc, scmd);
  2716. sas_device_priv_data = scmd->device->hostdata;
  2717. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2718. sdev_printk(KERN_INFO, scmd->device,
  2719. "device been deleted! scmd(%p)\n", scmd);
  2720. scmd->result = DID_NO_CONNECT << 16;
  2721. scmd->scsi_done(scmd);
  2722. r = SUCCESS;
  2723. goto out;
  2724. }
  2725. /* for hidden raid components obtain the volume_handle */
  2726. handle = 0;
  2727. if (sas_device_priv_data->sas_target->flags &
  2728. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2729. sas_device = mpt3sas_get_sdev_from_target(ioc,
  2730. target_priv_data);
  2731. if (sas_device)
  2732. handle = sas_device->volume_handle;
  2733. } else
  2734. handle = sas_device_priv_data->sas_target->handle;
  2735. if (!handle) {
  2736. scmd->result = DID_RESET << 16;
  2737. r = FAILED;
  2738. goto out;
  2739. }
  2740. r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
  2741. scmd->device->id, scmd->device->lun,
  2742. MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
  2743. out:
  2744. sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
  2745. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2746. if (sas_device)
  2747. sas_device_put(sas_device);
  2748. return r;
  2749. }
  2750. /**
  2751. * scsih_target_reset - eh threads main target reset routine
  2752. * @scmd: pointer to scsi command object
  2753. *
  2754. * Returns SUCCESS if command aborted else FAILED
  2755. */
  2756. static int
  2757. scsih_target_reset(struct scsi_cmnd *scmd)
  2758. {
  2759. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2760. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2761. struct _sas_device *sas_device = NULL;
  2762. u16 handle;
  2763. int r;
  2764. struct scsi_target *starget = scmd->device->sdev_target;
  2765. struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
  2766. starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
  2767. scmd);
  2768. _scsih_tm_display_info(ioc, scmd);
  2769. sas_device_priv_data = scmd->device->hostdata;
  2770. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2771. starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
  2772. scmd);
  2773. scmd->result = DID_NO_CONNECT << 16;
  2774. scmd->scsi_done(scmd);
  2775. r = SUCCESS;
  2776. goto out;
  2777. }
  2778. /* for hidden raid components obtain the volume_handle */
  2779. handle = 0;
  2780. if (sas_device_priv_data->sas_target->flags &
  2781. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2782. sas_device = mpt3sas_get_sdev_from_target(ioc,
  2783. target_priv_data);
  2784. if (sas_device)
  2785. handle = sas_device->volume_handle;
  2786. } else
  2787. handle = sas_device_priv_data->sas_target->handle;
  2788. if (!handle) {
  2789. scmd->result = DID_RESET << 16;
  2790. r = FAILED;
  2791. goto out;
  2792. }
  2793. r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
  2794. scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  2795. 30);
  2796. out:
  2797. starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
  2798. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2799. if (sas_device)
  2800. sas_device_put(sas_device);
  2801. return r;
  2802. }
  2803. /**
  2804. * scsih_host_reset - eh threads main host reset routine
  2805. * @scmd: pointer to scsi command object
  2806. *
  2807. * Returns SUCCESS if command aborted else FAILED
  2808. */
  2809. static int
  2810. scsih_host_reset(struct scsi_cmnd *scmd)
  2811. {
  2812. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2813. int r, retval;
  2814. pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
  2815. ioc->name, scmd);
  2816. scsi_print_command(scmd);
  2817. if (ioc->is_driver_loading) {
  2818. pr_info(MPT3SAS_FMT "Blocking the host reset\n",
  2819. ioc->name);
  2820. r = FAILED;
  2821. goto out;
  2822. }
  2823. retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  2824. r = (retval < 0) ? FAILED : SUCCESS;
  2825. out:
  2826. pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
  2827. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2828. return r;
  2829. }
  2830. /**
  2831. * _scsih_fw_event_add - insert and queue up fw_event
  2832. * @ioc: per adapter object
  2833. * @fw_event: object describing the event
  2834. * Context: This function will acquire ioc->fw_event_lock.
  2835. *
  2836. * This adds the firmware event object into link list, then queues it up to
  2837. * be processed from user context.
  2838. *
  2839. * Return nothing.
  2840. */
  2841. static void
  2842. _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  2843. {
  2844. unsigned long flags;
  2845. if (ioc->firmware_event_thread == NULL)
  2846. return;
  2847. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2848. fw_event_work_get(fw_event);
  2849. INIT_LIST_HEAD(&fw_event->list);
  2850. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  2851. INIT_WORK(&fw_event->work, _firmware_event_work);
  2852. fw_event_work_get(fw_event);
  2853. queue_work(ioc->firmware_event_thread, &fw_event->work);
  2854. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2855. }
  2856. /**
  2857. * _scsih_fw_event_del_from_list - delete fw_event from the list
  2858. * @ioc: per adapter object
  2859. * @fw_event: object describing the event
  2860. * Context: This function will acquire ioc->fw_event_lock.
  2861. *
  2862. * If the fw_event is on the fw_event_list, remove it and do a put.
  2863. *
  2864. * Return nothing.
  2865. */
  2866. static void
  2867. _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
  2868. *fw_event)
  2869. {
  2870. unsigned long flags;
  2871. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2872. if (!list_empty(&fw_event->list)) {
  2873. list_del_init(&fw_event->list);
  2874. fw_event_work_put(fw_event);
  2875. }
  2876. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2877. }
  2878. /**
  2879. * mpt3sas_send_trigger_data_event - send event for processing trigger data
  2880. * @ioc: per adapter object
  2881. * @event_data: trigger event data
  2882. *
  2883. * Return nothing.
  2884. */
  2885. void
  2886. mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
  2887. struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
  2888. {
  2889. struct fw_event_work *fw_event;
  2890. u16 sz;
  2891. if (ioc->is_driver_loading)
  2892. return;
  2893. sz = sizeof(*event_data);
  2894. fw_event = alloc_fw_event_work(sz);
  2895. if (!fw_event)
  2896. return;
  2897. fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
  2898. fw_event->ioc = ioc;
  2899. memcpy(fw_event->event_data, event_data, sizeof(*event_data));
  2900. _scsih_fw_event_add(ioc, fw_event);
  2901. fw_event_work_put(fw_event);
  2902. }
  2903. /**
  2904. * _scsih_error_recovery_delete_devices - remove devices not responding
  2905. * @ioc: per adapter object
  2906. *
  2907. * Return nothing.
  2908. */
  2909. static void
  2910. _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
  2911. {
  2912. struct fw_event_work *fw_event;
  2913. if (ioc->is_driver_loading)
  2914. return;
  2915. fw_event = alloc_fw_event_work(0);
  2916. if (!fw_event)
  2917. return;
  2918. fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
  2919. fw_event->ioc = ioc;
  2920. _scsih_fw_event_add(ioc, fw_event);
  2921. fw_event_work_put(fw_event);
  2922. }
  2923. /**
  2924. * mpt3sas_port_enable_complete - port enable completed (fake event)
  2925. * @ioc: per adapter object
  2926. *
  2927. * Return nothing.
  2928. */
  2929. void
  2930. mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
  2931. {
  2932. struct fw_event_work *fw_event;
  2933. fw_event = alloc_fw_event_work(0);
  2934. if (!fw_event)
  2935. return;
  2936. fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
  2937. fw_event->ioc = ioc;
  2938. _scsih_fw_event_add(ioc, fw_event);
  2939. fw_event_work_put(fw_event);
  2940. }
  2941. static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
  2942. {
  2943. unsigned long flags;
  2944. struct fw_event_work *fw_event = NULL;
  2945. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2946. if (!list_empty(&ioc->fw_event_list)) {
  2947. fw_event = list_first_entry(&ioc->fw_event_list,
  2948. struct fw_event_work, list);
  2949. list_del_init(&fw_event->list);
  2950. }
  2951. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2952. return fw_event;
  2953. }
  2954. /**
  2955. * _scsih_fw_event_cleanup_queue - cleanup event queue
  2956. * @ioc: per adapter object
  2957. *
  2958. * Walk the firmware event queue, either killing timers, or waiting
  2959. * for outstanding events to complete
  2960. *
  2961. * Return nothing.
  2962. */
  2963. static void
  2964. _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
  2965. {
  2966. struct fw_event_work *fw_event;
  2967. if (list_empty(&ioc->fw_event_list) ||
  2968. !ioc->firmware_event_thread || in_interrupt())
  2969. return;
  2970. while ((fw_event = dequeue_next_fw_event(ioc))) {
  2971. /*
  2972. * Wait on the fw_event to complete. If this returns 1, then
  2973. * the event was never executed, and we need a put for the
  2974. * reference the work had on the fw_event.
  2975. *
  2976. * If it did execute, we wait for it to finish, and the put will
  2977. * happen from _firmware_event_work()
  2978. */
  2979. if (cancel_work_sync(&fw_event->work))
  2980. fw_event_work_put(fw_event);
  2981. fw_event_work_put(fw_event);
  2982. }
  2983. }
  2984. /**
  2985. * _scsih_internal_device_block - block the sdev device
  2986. * @sdev: per device object
  2987. * @sas_device_priv_data : per device driver private data
  2988. *
  2989. * make sure device is blocked without error, if not
  2990. * print an error
  2991. */
  2992. static void
  2993. _scsih_internal_device_block(struct scsi_device *sdev,
  2994. struct MPT3SAS_DEVICE *sas_device_priv_data)
  2995. {
  2996. int r = 0;
  2997. sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
  2998. sas_device_priv_data->sas_target->handle);
  2999. sas_device_priv_data->block = 1;
  3000. r = scsi_internal_device_block_nowait(sdev);
  3001. if (r == -EINVAL)
  3002. sdev_printk(KERN_WARNING, sdev,
  3003. "device_block failed with return(%d) for handle(0x%04x)\n",
  3004. r, sas_device_priv_data->sas_target->handle);
  3005. }
  3006. /**
  3007. * _scsih_internal_device_unblock - unblock the sdev device
  3008. * @sdev: per device object
  3009. * @sas_device_priv_data : per device driver private data
  3010. * make sure device is unblocked without error, if not retry
  3011. * by blocking and then unblocking
  3012. */
  3013. static void
  3014. _scsih_internal_device_unblock(struct scsi_device *sdev,
  3015. struct MPT3SAS_DEVICE *sas_device_priv_data)
  3016. {
  3017. int r = 0;
  3018. sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
  3019. "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
  3020. sas_device_priv_data->block = 0;
  3021. r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
  3022. if (r == -EINVAL) {
  3023. /* The device has been set to SDEV_RUNNING by SD layer during
  3024. * device addition but the request queue is still stopped by
  3025. * our earlier block call. We need to perform a block again
  3026. * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
  3027. sdev_printk(KERN_WARNING, sdev,
  3028. "device_unblock failed with return(%d) for handle(0x%04x) "
  3029. "performing a block followed by an unblock\n",
  3030. r, sas_device_priv_data->sas_target->handle);
  3031. sas_device_priv_data->block = 1;
  3032. r = scsi_internal_device_block_nowait(sdev);
  3033. if (r)
  3034. sdev_printk(KERN_WARNING, sdev, "retried device_block "
  3035. "failed with return(%d) for handle(0x%04x)\n",
  3036. r, sas_device_priv_data->sas_target->handle);
  3037. sas_device_priv_data->block = 0;
  3038. r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
  3039. if (r)
  3040. sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
  3041. " failed with return(%d) for handle(0x%04x)\n",
  3042. r, sas_device_priv_data->sas_target->handle);
  3043. }
  3044. }
  3045. /**
  3046. * _scsih_ublock_io_all_device - unblock every device
  3047. * @ioc: per adapter object
  3048. *
  3049. * change the device state from block to running
  3050. */
  3051. static void
  3052. _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
  3053. {
  3054. struct MPT3SAS_DEVICE *sas_device_priv_data;
  3055. struct scsi_device *sdev;
  3056. shost_for_each_device(sdev, ioc->shost) {
  3057. sas_device_priv_data = sdev->hostdata;
  3058. if (!sas_device_priv_data)
  3059. continue;
  3060. if (!sas_device_priv_data->block)
  3061. continue;
  3062. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  3063. "device_running, handle(0x%04x)\n",
  3064. sas_device_priv_data->sas_target->handle));
  3065. _scsih_internal_device_unblock(sdev, sas_device_priv_data);
  3066. }
  3067. }
  3068. /**
  3069. * _scsih_ublock_io_device - prepare device to be deleted
  3070. * @ioc: per adapter object
  3071. * @sas_addr: sas address
  3072. *
  3073. * unblock then put device in offline state
  3074. */
  3075. static void
  3076. _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
  3077. {
  3078. struct MPT3SAS_DEVICE *sas_device_priv_data;
  3079. struct scsi_device *sdev;
  3080. shost_for_each_device(sdev, ioc->shost) {
  3081. sas_device_priv_data = sdev->hostdata;
  3082. if (!sas_device_priv_data)
  3083. continue;
  3084. if (sas_device_priv_data->sas_target->sas_address
  3085. != sas_address)
  3086. continue;
  3087. if (sas_device_priv_data->block)
  3088. _scsih_internal_device_unblock(sdev,
  3089. sas_device_priv_data);
  3090. }
  3091. }
  3092. /**
  3093. * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
  3094. * @ioc: per adapter object
  3095. * @handle: device handle
  3096. *
  3097. * During device pull we need to appropriately set the sdev state.
  3098. */
  3099. static void
  3100. _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
  3101. {
  3102. struct MPT3SAS_DEVICE *sas_device_priv_data;
  3103. struct scsi_device *sdev;
  3104. shost_for_each_device(sdev, ioc->shost) {
  3105. sas_device_priv_data = sdev->hostdata;
  3106. if (!sas_device_priv_data)
  3107. continue;
  3108. if (sas_device_priv_data->block)
  3109. continue;
  3110. if (sas_device_priv_data->ignore_delay_remove) {
  3111. sdev_printk(KERN_INFO, sdev,
  3112. "%s skip device_block for SES handle(0x%04x)\n",
  3113. __func__, sas_device_priv_data->sas_target->handle);
  3114. continue;
  3115. }
  3116. _scsih_internal_device_block(sdev, sas_device_priv_data);
  3117. }
  3118. }
  3119. /**
  3120. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  3121. * @ioc: per adapter object
  3122. * @handle: device handle
  3123. *
  3124. * During device pull we need to appropriately set the sdev state.
  3125. */
  3126. static void
  3127. _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  3128. {
  3129. struct MPT3SAS_DEVICE *sas_device_priv_data;
  3130. struct scsi_device *sdev;
  3131. struct _sas_device *sas_device;
  3132. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  3133. shost_for_each_device(sdev, ioc->shost) {
  3134. sas_device_priv_data = sdev->hostdata;
  3135. if (!sas_device_priv_data)
  3136. continue;
  3137. if (sas_device_priv_data->sas_target->handle != handle)
  3138. continue;
  3139. if (sas_device_priv_data->block)
  3140. continue;
  3141. if (sas_device && sas_device->pend_sas_rphy_add)
  3142. continue;
  3143. if (sas_device_priv_data->ignore_delay_remove) {
  3144. sdev_printk(KERN_INFO, sdev,
  3145. "%s skip device_block for SES handle(0x%04x)\n",
  3146. __func__, sas_device_priv_data->sas_target->handle);
  3147. continue;
  3148. }
  3149. _scsih_internal_device_block(sdev, sas_device_priv_data);
  3150. }
  3151. if (sas_device)
  3152. sas_device_put(sas_device);
  3153. }
  3154. /**
  3155. * _scsih_block_io_to_children_attached_to_ex
  3156. * @ioc: per adapter object
  3157. * @sas_expander: the sas_device object
  3158. *
  3159. * This routine set sdev state to SDEV_BLOCK for all devices
  3160. * attached to this expander. This function called when expander is
  3161. * pulled.
  3162. */
  3163. static void
  3164. _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
  3165. struct _sas_node *sas_expander)
  3166. {
  3167. struct _sas_port *mpt3sas_port;
  3168. struct _sas_device *sas_device;
  3169. struct _sas_node *expander_sibling;
  3170. unsigned long flags;
  3171. if (!sas_expander)
  3172. return;
  3173. list_for_each_entry(mpt3sas_port,
  3174. &sas_expander->sas_port_list, port_list) {
  3175. if (mpt3sas_port->remote_identify.device_type ==
  3176. SAS_END_DEVICE) {
  3177. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3178. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  3179. mpt3sas_port->remote_identify.sas_address);
  3180. if (sas_device) {
  3181. set_bit(sas_device->handle,
  3182. ioc->blocking_handles);
  3183. sas_device_put(sas_device);
  3184. }
  3185. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3186. }
  3187. }
  3188. list_for_each_entry(mpt3sas_port,
  3189. &sas_expander->sas_port_list, port_list) {
  3190. if (mpt3sas_port->remote_identify.device_type ==
  3191. SAS_EDGE_EXPANDER_DEVICE ||
  3192. mpt3sas_port->remote_identify.device_type ==
  3193. SAS_FANOUT_EXPANDER_DEVICE) {
  3194. expander_sibling =
  3195. mpt3sas_scsih_expander_find_by_sas_address(
  3196. ioc, mpt3sas_port->remote_identify.sas_address);
  3197. _scsih_block_io_to_children_attached_to_ex(ioc,
  3198. expander_sibling);
  3199. }
  3200. }
  3201. }
  3202. /**
  3203. * _scsih_block_io_to_children_attached_directly
  3204. * @ioc: per adapter object
  3205. * @event_data: topology change event data
  3206. *
  3207. * This routine set sdev state to SDEV_BLOCK for all devices
  3208. * direct attached during device pull.
  3209. */
  3210. static void
  3211. _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
  3212. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3213. {
  3214. int i;
  3215. u16 handle;
  3216. u16 reason_code;
  3217. for (i = 0; i < event_data->NumEntries; i++) {
  3218. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3219. if (!handle)
  3220. continue;
  3221. reason_code = event_data->PHY[i].PhyStatus &
  3222. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3223. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  3224. _scsih_block_io_device(ioc, handle);
  3225. }
  3226. }
  3227. /**
  3228. * _scsih_block_io_to_pcie_children_attached_directly
  3229. * @ioc: per adapter object
  3230. * @event_data: topology change event data
  3231. *
  3232. * This routine set sdev state to SDEV_BLOCK for all devices
  3233. * direct attached during device pull/reconnect.
  3234. */
  3235. static void
  3236. _scsih_block_io_to_pcie_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
  3237. Mpi26EventDataPCIeTopologyChangeList_t *event_data)
  3238. {
  3239. int i;
  3240. u16 handle;
  3241. u16 reason_code;
  3242. for (i = 0; i < event_data->NumEntries; i++) {
  3243. handle =
  3244. le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
  3245. if (!handle)
  3246. continue;
  3247. reason_code = event_data->PortEntry[i].PortStatus;
  3248. if (reason_code ==
  3249. MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING)
  3250. _scsih_block_io_device(ioc, handle);
  3251. }
  3252. }
  3253. /**
  3254. * _scsih_tm_tr_send - send task management request
  3255. * @ioc: per adapter object
  3256. * @handle: device handle
  3257. * Context: interrupt time.
  3258. *
  3259. * This code is to initiate the device removal handshake protocol
  3260. * with controller firmware. This function will issue target reset
  3261. * using high priority request queue. It will send a sas iounit
  3262. * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
  3263. *
  3264. * This is designed to send muliple task management request at the same
  3265. * time to the fifo. If the fifo is full, we will append the request,
  3266. * and process it in a future completion.
  3267. */
  3268. static void
  3269. _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  3270. {
  3271. Mpi2SCSITaskManagementRequest_t *mpi_request;
  3272. u16 smid;
  3273. struct _sas_device *sas_device = NULL;
  3274. struct _pcie_device *pcie_device = NULL;
  3275. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  3276. u64 sas_address = 0;
  3277. unsigned long flags;
  3278. struct _tr_list *delayed_tr;
  3279. u32 ioc_state;
  3280. if (ioc->pci_error_recovery) {
  3281. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3282. "%s: host in pci error recovery: handle(0x%04x)\n",
  3283. __func__, ioc->name,
  3284. handle));
  3285. return;
  3286. }
  3287. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3288. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3289. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3290. "%s: host is not operational: handle(0x%04x)\n",
  3291. __func__, ioc->name,
  3292. handle));
  3293. return;
  3294. }
  3295. /* if PD, then return */
  3296. if (test_bit(handle, ioc->pd_handles))
  3297. return;
  3298. clear_bit(handle, ioc->pend_os_device_add);
  3299. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3300. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  3301. if (sas_device && sas_device->starget &&
  3302. sas_device->starget->hostdata) {
  3303. sas_target_priv_data = sas_device->starget->hostdata;
  3304. sas_target_priv_data->deleted = 1;
  3305. sas_address = sas_device->sas_address;
  3306. }
  3307. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3308. if (!sas_device) {
  3309. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  3310. pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
  3311. if (pcie_device && pcie_device->starget &&
  3312. pcie_device->starget->hostdata) {
  3313. sas_target_priv_data = pcie_device->starget->hostdata;
  3314. sas_target_priv_data->deleted = 1;
  3315. sas_address = pcie_device->wwid;
  3316. }
  3317. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  3318. }
  3319. if (sas_target_priv_data) {
  3320. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3321. "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
  3322. ioc->name, handle,
  3323. (unsigned long long)sas_address));
  3324. if (sas_device) {
  3325. if (sas_device->enclosure_handle != 0)
  3326. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3327. "setting delete flag:enclosure logical "
  3328. "id(0x%016llx), slot(%d)\n", ioc->name,
  3329. (unsigned long long)
  3330. sas_device->enclosure_logical_id,
  3331. sas_device->slot));
  3332. if (sas_device->connector_name[0] != '\0')
  3333. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3334. "setting delete flag: enclosure "
  3335. "level(0x%04x), connector name( %s)\n",
  3336. ioc->name, sas_device->enclosure_level,
  3337. sas_device->connector_name));
  3338. } else if (pcie_device) {
  3339. if (pcie_device->enclosure_handle != 0)
  3340. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3341. "setting delete flag: logical "
  3342. "id(0x%016llx), slot(%d)\n", ioc->name,
  3343. (unsigned long long)
  3344. pcie_device->enclosure_logical_id,
  3345. pcie_device->slot));
  3346. if (pcie_device->connector_name[0] != '\0')
  3347. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3348. "setting delete flag:, enclosure "
  3349. "level(0x%04x), "
  3350. "connector name( %s)\n", ioc->name,
  3351. pcie_device->enclosure_level,
  3352. pcie_device->connector_name));
  3353. }
  3354. _scsih_ublock_io_device(ioc, sas_address);
  3355. sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  3356. }
  3357. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
  3358. if (!smid) {
  3359. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  3360. if (!delayed_tr)
  3361. goto out;
  3362. INIT_LIST_HEAD(&delayed_tr->list);
  3363. delayed_tr->handle = handle;
  3364. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  3365. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3366. "DELAYED:tr:handle(0x%04x), (open)\n",
  3367. ioc->name, handle));
  3368. goto out;
  3369. }
  3370. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3371. "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  3372. ioc->name, handle, smid,
  3373. ioc->tm_tr_cb_idx));
  3374. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3375. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  3376. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  3377. mpi_request->DevHandle = cpu_to_le16(handle);
  3378. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  3379. set_bit(handle, ioc->device_remove_in_progress);
  3380. ioc->put_smid_hi_priority(ioc, smid, 0);
  3381. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
  3382. out:
  3383. if (sas_device)
  3384. sas_device_put(sas_device);
  3385. if (pcie_device)
  3386. pcie_device_put(pcie_device);
  3387. }
  3388. /**
  3389. * _scsih_tm_tr_complete -
  3390. * @ioc: per adapter object
  3391. * @smid: system request message index
  3392. * @msix_index: MSIX table index supplied by the OS
  3393. * @reply: reply message frame(lower 32bit addr)
  3394. * Context: interrupt time.
  3395. *
  3396. * This is the target reset completion routine.
  3397. * This code is part of the code to initiate the device removal
  3398. * handshake protocol with controller firmware.
  3399. * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
  3400. *
  3401. * Return 1 meaning mf should be freed from _base_interrupt
  3402. * 0 means the mf is freed from this function.
  3403. */
  3404. static u8
  3405. _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  3406. u32 reply)
  3407. {
  3408. u16 handle;
  3409. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  3410. Mpi2SCSITaskManagementReply_t *mpi_reply =
  3411. mpt3sas_base_get_reply_virt_addr(ioc, reply);
  3412. Mpi2SasIoUnitControlRequest_t *mpi_request;
  3413. u16 smid_sas_ctrl;
  3414. u32 ioc_state;
  3415. struct _sc_list *delayed_sc;
  3416. if (ioc->remove_host) {
  3417. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3418. "%s: host has been removed\n", __func__, ioc->name));
  3419. return 1;
  3420. } else if (ioc->pci_error_recovery) {
  3421. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3422. "%s: host in pci error recovery\n", __func__,
  3423. ioc->name));
  3424. return 1;
  3425. }
  3426. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3427. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3428. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3429. "%s: host is not operational\n", __func__, ioc->name));
  3430. return 1;
  3431. }
  3432. if (unlikely(!mpi_reply)) {
  3433. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  3434. ioc->name, __FILE__, __LINE__, __func__);
  3435. return 1;
  3436. }
  3437. mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
  3438. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  3439. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  3440. dewtprintk(ioc, pr_err(MPT3SAS_FMT
  3441. "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
  3442. ioc->name, handle,
  3443. le16_to_cpu(mpi_reply->DevHandle), smid));
  3444. return 0;
  3445. }
  3446. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
  3447. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3448. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  3449. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  3450. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  3451. le32_to_cpu(mpi_reply->IOCLogInfo),
  3452. le32_to_cpu(mpi_reply->TerminationCount)));
  3453. smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
  3454. if (!smid_sas_ctrl) {
  3455. delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
  3456. if (!delayed_sc)
  3457. return _scsih_check_for_pending_tm(ioc, smid);
  3458. INIT_LIST_HEAD(&delayed_sc->list);
  3459. delayed_sc->handle = mpi_request_tm->DevHandle;
  3460. list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
  3461. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3462. "DELAYED:sc:handle(0x%04x), (open)\n",
  3463. ioc->name, handle));
  3464. return _scsih_check_for_pending_tm(ioc, smid);
  3465. }
  3466. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3467. "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  3468. ioc->name, handle, smid_sas_ctrl,
  3469. ioc->tm_sas_control_cb_idx));
  3470. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
  3471. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3472. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3473. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3474. mpi_request->DevHandle = mpi_request_tm->DevHandle;
  3475. ioc->put_smid_default(ioc, smid_sas_ctrl);
  3476. return _scsih_check_for_pending_tm(ioc, smid);
  3477. }
  3478. /**
  3479. * _scsih_sas_control_complete - completion routine
  3480. * @ioc: per adapter object
  3481. * @smid: system request message index
  3482. * @msix_index: MSIX table index supplied by the OS
  3483. * @reply: reply message frame(lower 32bit addr)
  3484. * Context: interrupt time.
  3485. *
  3486. * This is the sas iounit control completion routine.
  3487. * This code is part of the code to initiate the device removal
  3488. * handshake protocol with controller firmware.
  3489. *
  3490. * Return 1 meaning mf should be freed from _base_interrupt
  3491. * 0 means the mf is freed from this function.
  3492. */
  3493. static u8
  3494. _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3495. u8 msix_index, u32 reply)
  3496. {
  3497. Mpi2SasIoUnitControlReply_t *mpi_reply =
  3498. mpt3sas_base_get_reply_virt_addr(ioc, reply);
  3499. if (likely(mpi_reply)) {
  3500. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3501. "sc_complete:handle(0x%04x), (open) "
  3502. "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  3503. ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
  3504. le16_to_cpu(mpi_reply->IOCStatus),
  3505. le32_to_cpu(mpi_reply->IOCLogInfo)));
  3506. if (le16_to_cpu(mpi_reply->IOCStatus) ==
  3507. MPI2_IOCSTATUS_SUCCESS) {
  3508. clear_bit(le16_to_cpu(mpi_reply->DevHandle),
  3509. ioc->device_remove_in_progress);
  3510. }
  3511. } else {
  3512. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  3513. ioc->name, __FILE__, __LINE__, __func__);
  3514. }
  3515. return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
  3516. }
  3517. /**
  3518. * _scsih_tm_tr_volume_send - send target reset request for volumes
  3519. * @ioc: per adapter object
  3520. * @handle: device handle
  3521. * Context: interrupt time.
  3522. *
  3523. * This is designed to send muliple task management request at the same
  3524. * time to the fifo. If the fifo is full, we will append the request,
  3525. * and process it in a future completion.
  3526. */
  3527. static void
  3528. _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  3529. {
  3530. Mpi2SCSITaskManagementRequest_t *mpi_request;
  3531. u16 smid;
  3532. struct _tr_list *delayed_tr;
  3533. if (ioc->shost_recovery || ioc->remove_host ||
  3534. ioc->pci_error_recovery) {
  3535. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3536. "%s: host reset in progress!\n",
  3537. __func__, ioc->name));
  3538. return;
  3539. }
  3540. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
  3541. if (!smid) {
  3542. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  3543. if (!delayed_tr)
  3544. return;
  3545. INIT_LIST_HEAD(&delayed_tr->list);
  3546. delayed_tr->handle = handle;
  3547. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
  3548. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3549. "DELAYED:tr:handle(0x%04x), (open)\n",
  3550. ioc->name, handle));
  3551. return;
  3552. }
  3553. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3554. "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  3555. ioc->name, handle, smid,
  3556. ioc->tm_tr_volume_cb_idx));
  3557. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3558. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  3559. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  3560. mpi_request->DevHandle = cpu_to_le16(handle);
  3561. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  3562. ioc->put_smid_hi_priority(ioc, smid, 0);
  3563. }
  3564. /**
  3565. * _scsih_tm_volume_tr_complete - target reset completion
  3566. * @ioc: per adapter object
  3567. * @smid: system request message index
  3568. * @msix_index: MSIX table index supplied by the OS
  3569. * @reply: reply message frame(lower 32bit addr)
  3570. * Context: interrupt time.
  3571. *
  3572. * Return 1 meaning mf should be freed from _base_interrupt
  3573. * 0 means the mf is freed from this function.
  3574. */
  3575. static u8
  3576. _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3577. u8 msix_index, u32 reply)
  3578. {
  3579. u16 handle;
  3580. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  3581. Mpi2SCSITaskManagementReply_t *mpi_reply =
  3582. mpt3sas_base_get_reply_virt_addr(ioc, reply);
  3583. if (ioc->shost_recovery || ioc->remove_host ||
  3584. ioc->pci_error_recovery) {
  3585. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3586. "%s: host reset in progress!\n",
  3587. __func__, ioc->name));
  3588. return 1;
  3589. }
  3590. if (unlikely(!mpi_reply)) {
  3591. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  3592. ioc->name, __FILE__, __LINE__, __func__);
  3593. return 1;
  3594. }
  3595. mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
  3596. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  3597. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  3598. dewtprintk(ioc, pr_err(MPT3SAS_FMT
  3599. "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
  3600. ioc->name, handle,
  3601. le16_to_cpu(mpi_reply->DevHandle), smid));
  3602. return 0;
  3603. }
  3604. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3605. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  3606. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  3607. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  3608. le32_to_cpu(mpi_reply->IOCLogInfo),
  3609. le32_to_cpu(mpi_reply->TerminationCount)));
  3610. return _scsih_check_for_pending_tm(ioc, smid);
  3611. }
  3612. /**
  3613. * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
  3614. * @ioc: per adapter object
  3615. * @smid: system request message index
  3616. * @event: Event ID
  3617. * @event_context: used to track events uniquely
  3618. *
  3619. * Context - processed in interrupt context.
  3620. */
  3621. static void
  3622. _scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
  3623. u32 event_context)
  3624. {
  3625. Mpi2EventAckRequest_t *ack_request;
  3626. int i = smid - ioc->internal_smid;
  3627. unsigned long flags;
  3628. /* Without releasing the smid just update the
  3629. * call back index and reuse the same smid for
  3630. * processing this delayed request
  3631. */
  3632. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3633. ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
  3634. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3635. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3636. "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
  3637. ioc->name, le16_to_cpu(event), smid,
  3638. ioc->base_cb_idx));
  3639. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3640. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  3641. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  3642. ack_request->Event = event;
  3643. ack_request->EventContext = event_context;
  3644. ack_request->VF_ID = 0; /* TODO */
  3645. ack_request->VP_ID = 0;
  3646. ioc->put_smid_default(ioc, smid);
  3647. }
  3648. /**
  3649. * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
  3650. * sas_io_unit_ctrl messages
  3651. * @ioc: per adapter object
  3652. * @smid: system request message index
  3653. * @handle: device handle
  3654. *
  3655. * Context - processed in interrupt context.
  3656. */
  3657. static void
  3658. _scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
  3659. u16 smid, u16 handle)
  3660. {
  3661. Mpi2SasIoUnitControlRequest_t *mpi_request;
  3662. u32 ioc_state;
  3663. int i = smid - ioc->internal_smid;
  3664. unsigned long flags;
  3665. if (ioc->remove_host) {
  3666. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3667. "%s: host has been removed\n",
  3668. __func__, ioc->name));
  3669. return;
  3670. } else if (ioc->pci_error_recovery) {
  3671. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3672. "%s: host in pci error recovery\n",
  3673. __func__, ioc->name));
  3674. return;
  3675. }
  3676. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3677. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3678. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3679. "%s: host is not operational\n",
  3680. __func__, ioc->name));
  3681. return;
  3682. }
  3683. /* Without releasing the smid just update the
  3684. * call back index and reuse the same smid for
  3685. * processing this delayed request
  3686. */
  3687. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3688. ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
  3689. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3690. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3691. "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  3692. ioc->name, le16_to_cpu(handle), smid,
  3693. ioc->tm_sas_control_cb_idx));
  3694. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3695. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3696. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3697. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3698. mpi_request->DevHandle = handle;
  3699. ioc->put_smid_default(ioc, smid);
  3700. }
  3701. /**
  3702. * _scsih_check_for_pending_internal_cmds - check for pending internal messages
  3703. * @ioc: per adapter object
  3704. * @smid: system request message index
  3705. *
  3706. * Context: Executed in interrupt context
  3707. *
  3708. * This will check delayed internal messages list, and process the
  3709. * next request.
  3710. *
  3711. * Return 1 meaning mf should be freed from _base_interrupt
  3712. * 0 means the mf is freed from this function.
  3713. */
  3714. u8
  3715. mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3716. {
  3717. struct _sc_list *delayed_sc;
  3718. struct _event_ack_list *delayed_event_ack;
  3719. if (!list_empty(&ioc->delayed_event_ack_list)) {
  3720. delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
  3721. struct _event_ack_list, list);
  3722. _scsih_issue_delayed_event_ack(ioc, smid,
  3723. delayed_event_ack->Event, delayed_event_ack->EventContext);
  3724. list_del(&delayed_event_ack->list);
  3725. kfree(delayed_event_ack);
  3726. return 0;
  3727. }
  3728. if (!list_empty(&ioc->delayed_sc_list)) {
  3729. delayed_sc = list_entry(ioc->delayed_sc_list.next,
  3730. struct _sc_list, list);
  3731. _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
  3732. delayed_sc->handle);
  3733. list_del(&delayed_sc->list);
  3734. kfree(delayed_sc);
  3735. return 0;
  3736. }
  3737. return 1;
  3738. }
  3739. /**
  3740. * _scsih_check_for_pending_tm - check for pending task management
  3741. * @ioc: per adapter object
  3742. * @smid: system request message index
  3743. *
  3744. * This will check delayed target reset list, and feed the
  3745. * next reqeust.
  3746. *
  3747. * Return 1 meaning mf should be freed from _base_interrupt
  3748. * 0 means the mf is freed from this function.
  3749. */
  3750. static u8
  3751. _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3752. {
  3753. struct _tr_list *delayed_tr;
  3754. if (!list_empty(&ioc->delayed_tr_volume_list)) {
  3755. delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
  3756. struct _tr_list, list);
  3757. mpt3sas_base_free_smid(ioc, smid);
  3758. _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
  3759. list_del(&delayed_tr->list);
  3760. kfree(delayed_tr);
  3761. return 0;
  3762. }
  3763. if (!list_empty(&ioc->delayed_tr_list)) {
  3764. delayed_tr = list_entry(ioc->delayed_tr_list.next,
  3765. struct _tr_list, list);
  3766. mpt3sas_base_free_smid(ioc, smid);
  3767. _scsih_tm_tr_send(ioc, delayed_tr->handle);
  3768. list_del(&delayed_tr->list);
  3769. kfree(delayed_tr);
  3770. return 0;
  3771. }
  3772. return 1;
  3773. }
  3774. /**
  3775. * _scsih_check_topo_delete_events - sanity check on topo events
  3776. * @ioc: per adapter object
  3777. * @event_data: the event data payload
  3778. *
  3779. * This routine added to better handle cable breaker.
  3780. *
  3781. * This handles the case where driver receives multiple expander
  3782. * add and delete events in a single shot. When there is a delete event
  3783. * the routine will void any pending add events waiting in the event queue.
  3784. *
  3785. * Return nothing.
  3786. */
  3787. static void
  3788. _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
  3789. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3790. {
  3791. struct fw_event_work *fw_event;
  3792. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  3793. u16 expander_handle;
  3794. struct _sas_node *sas_expander;
  3795. unsigned long flags;
  3796. int i, reason_code;
  3797. u16 handle;
  3798. for (i = 0 ; i < event_data->NumEntries; i++) {
  3799. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3800. if (!handle)
  3801. continue;
  3802. reason_code = event_data->PHY[i].PhyStatus &
  3803. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3804. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
  3805. _scsih_tm_tr_send(ioc, handle);
  3806. }
  3807. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3808. if (expander_handle < ioc->sas_hba.num_phys) {
  3809. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  3810. return;
  3811. }
  3812. if (event_data->ExpStatus ==
  3813. MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
  3814. /* put expander attached devices into blocking state */
  3815. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3816. sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
  3817. expander_handle);
  3818. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  3819. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3820. do {
  3821. handle = find_first_bit(ioc->blocking_handles,
  3822. ioc->facts.MaxDevHandle);
  3823. if (handle < ioc->facts.MaxDevHandle)
  3824. _scsih_block_io_device(ioc, handle);
  3825. } while (test_and_clear_bit(handle, ioc->blocking_handles));
  3826. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  3827. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  3828. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3829. return;
  3830. /* mark ignore flag for pending events */
  3831. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  3832. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  3833. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  3834. fw_event->ignore)
  3835. continue;
  3836. local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
  3837. fw_event->event_data;
  3838. if (local_event_data->ExpStatus ==
  3839. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  3840. local_event_data->ExpStatus ==
  3841. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  3842. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  3843. expander_handle) {
  3844. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3845. "setting ignoring flag\n", ioc->name));
  3846. fw_event->ignore = 1;
  3847. }
  3848. }
  3849. }
  3850. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  3851. }
  3852. /**
  3853. * _scsih_check_pcie_topo_remove_events - sanity check on topo
  3854. * events
  3855. * @ioc: per adapter object
  3856. * @event_data: the event data payload
  3857. *
  3858. * This handles the case where driver receives multiple switch
  3859. * or device add and delete events in a single shot. When there
  3860. * is a delete event the routine will void any pending add
  3861. * events waiting in the event queue.
  3862. *
  3863. * Return nothing.
  3864. */
  3865. static void
  3866. _scsih_check_pcie_topo_remove_events(struct MPT3SAS_ADAPTER *ioc,
  3867. Mpi26EventDataPCIeTopologyChangeList_t *event_data)
  3868. {
  3869. struct fw_event_work *fw_event;
  3870. Mpi26EventDataPCIeTopologyChangeList_t *local_event_data;
  3871. unsigned long flags;
  3872. int i, reason_code;
  3873. u16 handle, switch_handle;
  3874. for (i = 0; i < event_data->NumEntries; i++) {
  3875. handle =
  3876. le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
  3877. if (!handle)
  3878. continue;
  3879. reason_code = event_data->PortEntry[i].PortStatus;
  3880. if (reason_code == MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING)
  3881. _scsih_tm_tr_send(ioc, handle);
  3882. }
  3883. switch_handle = le16_to_cpu(event_data->SwitchDevHandle);
  3884. if (!switch_handle) {
  3885. _scsih_block_io_to_pcie_children_attached_directly(
  3886. ioc, event_data);
  3887. return;
  3888. }
  3889. /* TODO We are not supporting cascaded PCIe Switch removal yet*/
  3890. if ((event_data->SwitchStatus
  3891. == MPI26_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING) ||
  3892. (event_data->SwitchStatus ==
  3893. MPI26_EVENT_PCIE_TOPO_SS_RESPONDING))
  3894. _scsih_block_io_to_pcie_children_attached_directly(
  3895. ioc, event_data);
  3896. if (event_data->SwitchStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3897. return;
  3898. /* mark ignore flag for pending events */
  3899. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  3900. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  3901. if (fw_event->event != MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
  3902. fw_event->ignore)
  3903. continue;
  3904. local_event_data =
  3905. (Mpi26EventDataPCIeTopologyChangeList_t *)
  3906. fw_event->event_data;
  3907. if (local_event_data->SwitchStatus ==
  3908. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  3909. local_event_data->SwitchStatus ==
  3910. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  3911. if (le16_to_cpu(local_event_data->SwitchDevHandle) ==
  3912. switch_handle) {
  3913. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3914. "setting ignoring flag for switch event\n",
  3915. ioc->name));
  3916. fw_event->ignore = 1;
  3917. }
  3918. }
  3919. }
  3920. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  3921. }
  3922. /**
  3923. * _scsih_set_volume_delete_flag - setting volume delete flag
  3924. * @ioc: per adapter object
  3925. * @handle: device handle
  3926. *
  3927. * This returns nothing.
  3928. */
  3929. static void
  3930. _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  3931. {
  3932. struct _raid_device *raid_device;
  3933. struct MPT3SAS_TARGET *sas_target_priv_data;
  3934. unsigned long flags;
  3935. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3936. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  3937. if (raid_device && raid_device->starget &&
  3938. raid_device->starget->hostdata) {
  3939. sas_target_priv_data =
  3940. raid_device->starget->hostdata;
  3941. sas_target_priv_data->deleted = 1;
  3942. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3943. "setting delete flag: handle(0x%04x), "
  3944. "wwid(0x%016llx)\n", ioc->name, handle,
  3945. (unsigned long long) raid_device->wwid));
  3946. }
  3947. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3948. }
  3949. /**
  3950. * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
  3951. * @handle: input handle
  3952. * @a: handle for volume a
  3953. * @b: handle for volume b
  3954. *
  3955. * IR firmware only supports two raid volumes. The purpose of this
  3956. * routine is to set the volume handle in either a or b. When the given
  3957. * input handle is non-zero, or when a and b have not been set before.
  3958. */
  3959. static void
  3960. _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
  3961. {
  3962. if (!handle || handle == *a || handle == *b)
  3963. return;
  3964. if (!*a)
  3965. *a = handle;
  3966. else if (!*b)
  3967. *b = handle;
  3968. }
  3969. /**
  3970. * _scsih_check_ir_config_unhide_events - check for UNHIDE events
  3971. * @ioc: per adapter object
  3972. * @event_data: the event data payload
  3973. * Context: interrupt time.
  3974. *
  3975. * This routine will send target reset to volume, followed by target
  3976. * resets to the PDs. This is called when a PD has been removed, or
  3977. * volume has been deleted or removed. When the target reset is sent
  3978. * to volume, the PD target resets need to be queued to start upon
  3979. * completion of the volume target reset.
  3980. *
  3981. * Return nothing.
  3982. */
  3983. static void
  3984. _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
  3985. Mpi2EventDataIrConfigChangeList_t *event_data)
  3986. {
  3987. Mpi2EventIrConfigElement_t *element;
  3988. int i;
  3989. u16 handle, volume_handle, a, b;
  3990. struct _tr_list *delayed_tr;
  3991. a = 0;
  3992. b = 0;
  3993. if (ioc->is_warpdrive)
  3994. return;
  3995. /* Volume Resets for Deleted or Removed */
  3996. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3997. for (i = 0; i < event_data->NumElements; i++, element++) {
  3998. if (le32_to_cpu(event_data->Flags) &
  3999. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  4000. continue;
  4001. if (element->ReasonCode ==
  4002. MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
  4003. element->ReasonCode ==
  4004. MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
  4005. volume_handle = le16_to_cpu(element->VolDevHandle);
  4006. _scsih_set_volume_delete_flag(ioc, volume_handle);
  4007. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  4008. }
  4009. }
  4010. /* Volume Resets for UNHIDE events */
  4011. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4012. for (i = 0; i < event_data->NumElements; i++, element++) {
  4013. if (le32_to_cpu(event_data->Flags) &
  4014. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  4015. continue;
  4016. if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
  4017. volume_handle = le16_to_cpu(element->VolDevHandle);
  4018. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  4019. }
  4020. }
  4021. if (a)
  4022. _scsih_tm_tr_volume_send(ioc, a);
  4023. if (b)
  4024. _scsih_tm_tr_volume_send(ioc, b);
  4025. /* PD target resets */
  4026. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4027. for (i = 0; i < event_data->NumElements; i++, element++) {
  4028. if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
  4029. continue;
  4030. handle = le16_to_cpu(element->PhysDiskDevHandle);
  4031. volume_handle = le16_to_cpu(element->VolDevHandle);
  4032. clear_bit(handle, ioc->pd_handles);
  4033. if (!volume_handle)
  4034. _scsih_tm_tr_send(ioc, handle);
  4035. else if (volume_handle == a || volume_handle == b) {
  4036. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  4037. BUG_ON(!delayed_tr);
  4038. INIT_LIST_HEAD(&delayed_tr->list);
  4039. delayed_tr->handle = handle;
  4040. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  4041. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  4042. "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
  4043. handle));
  4044. } else
  4045. _scsih_tm_tr_send(ioc, handle);
  4046. }
  4047. }
  4048. /**
  4049. * _scsih_check_volume_delete_events - set delete flag for volumes
  4050. * @ioc: per adapter object
  4051. * @event_data: the event data payload
  4052. * Context: interrupt time.
  4053. *
  4054. * This will handle the case when the cable connected to entire volume is
  4055. * pulled. We will take care of setting the deleted flag so normal IO will
  4056. * not be sent.
  4057. *
  4058. * Return nothing.
  4059. */
  4060. static void
  4061. _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
  4062. Mpi2EventDataIrVolume_t *event_data)
  4063. {
  4064. u32 state;
  4065. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  4066. return;
  4067. state = le32_to_cpu(event_data->NewValue);
  4068. if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
  4069. MPI2_RAID_VOL_STATE_FAILED)
  4070. _scsih_set_volume_delete_flag(ioc,
  4071. le16_to_cpu(event_data->VolDevHandle));
  4072. }
  4073. /**
  4074. * _scsih_temp_threshold_events - display temperature threshold exceeded events
  4075. * @ioc: per adapter object
  4076. * @event_data: the temp threshold event data
  4077. * Context: interrupt time.
  4078. *
  4079. * Return nothing.
  4080. */
  4081. static void
  4082. _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
  4083. Mpi2EventDataTemperature_t *event_data)
  4084. {
  4085. if (ioc->temp_sensors_count >= event_data->SensorNum) {
  4086. pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
  4087. " exceeded for Sensor: %d !!!\n", ioc->name,
  4088. ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
  4089. ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
  4090. ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
  4091. ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
  4092. event_data->SensorNum);
  4093. pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
  4094. ioc->name, event_data->CurrentTemperature);
  4095. }
  4096. }
  4097. static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
  4098. {
  4099. struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;
  4100. if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
  4101. return 0;
  4102. if (pending)
  4103. return test_and_set_bit(0, &priv->ata_command_pending);
  4104. clear_bit(0, &priv->ata_command_pending);
  4105. return 0;
  4106. }
  4107. /**
  4108. * _scsih_flush_running_cmds - completing outstanding commands.
  4109. * @ioc: per adapter object
  4110. *
  4111. * The flushing out of all pending scmd commands following host reset,
  4112. * where all IO is dropped to the floor.
  4113. *
  4114. * Return nothing.
  4115. */
  4116. static void
  4117. _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
  4118. {
  4119. struct scsi_cmnd *scmd;
  4120. u16 smid;
  4121. u16 count = 0;
  4122. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  4123. scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
  4124. if (!scmd)
  4125. continue;
  4126. count++;
  4127. _scsih_set_satl_pending(scmd, false);
  4128. mpt3sas_base_free_smid(ioc, smid);
  4129. scsi_dma_unmap(scmd);
  4130. if (ioc->pci_error_recovery)
  4131. scmd->result = DID_NO_CONNECT << 16;
  4132. else
  4133. scmd->result = DID_RESET << 16;
  4134. scmd->scsi_done(scmd);
  4135. }
  4136. dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
  4137. ioc->name, count));
  4138. }
  4139. /**
  4140. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  4141. * @ioc: per adapter object
  4142. * @scmd: pointer to scsi command object
  4143. * @mpi_request: pointer to the SCSI_IO request message frame
  4144. *
  4145. * Supporting protection 1 and 3.
  4146. *
  4147. * Returns nothing
  4148. */
  4149. static void
  4150. _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  4151. Mpi25SCSIIORequest_t *mpi_request)
  4152. {
  4153. u16 eedp_flags;
  4154. unsigned char prot_op = scsi_get_prot_op(scmd);
  4155. unsigned char prot_type = scsi_get_prot_type(scmd);
  4156. Mpi25SCSIIORequest_t *mpi_request_3v =
  4157. (Mpi25SCSIIORequest_t *)mpi_request;
  4158. if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
  4159. return;
  4160. if (prot_op == SCSI_PROT_READ_STRIP)
  4161. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  4162. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  4163. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  4164. else
  4165. return;
  4166. switch (prot_type) {
  4167. case SCSI_PROT_DIF_TYPE1:
  4168. case SCSI_PROT_DIF_TYPE2:
  4169. /*
  4170. * enable ref/guard checking
  4171. * auto increment ref tag
  4172. */
  4173. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  4174. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  4175. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  4176. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  4177. cpu_to_be32(scsi_prot_ref_tag(scmd));
  4178. break;
  4179. case SCSI_PROT_DIF_TYPE3:
  4180. /*
  4181. * enable guard checking
  4182. */
  4183. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  4184. break;
  4185. }
  4186. mpi_request_3v->EEDPBlockSize =
  4187. cpu_to_le16(scmd->device->sector_size);
  4188. if (ioc->is_gen35_ioc)
  4189. eedp_flags |= MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
  4190. mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
  4191. }
  4192. /**
  4193. * _scsih_eedp_error_handling - return sense code for EEDP errors
  4194. * @scmd: pointer to scsi command object
  4195. * @ioc_status: ioc status
  4196. *
  4197. * Returns nothing
  4198. */
  4199. static void
  4200. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  4201. {
  4202. u8 ascq;
  4203. switch (ioc_status) {
  4204. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  4205. ascq = 0x01;
  4206. break;
  4207. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  4208. ascq = 0x02;
  4209. break;
  4210. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  4211. ascq = 0x03;
  4212. break;
  4213. default:
  4214. ascq = 0x00;
  4215. break;
  4216. }
  4217. scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
  4218. ascq);
  4219. scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
  4220. SAM_STAT_CHECK_CONDITION;
  4221. }
  4222. /**
  4223. * scsih_qcmd - main scsi request entry point
  4224. * @scmd: pointer to scsi command object
  4225. * @done: function pointer to be invoked on completion
  4226. *
  4227. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  4228. *
  4229. * Returns 0 on success. If there's a failure, return either:
  4230. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  4231. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  4232. */
  4233. static int
  4234. scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
  4235. {
  4236. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  4237. struct MPT3SAS_DEVICE *sas_device_priv_data;
  4238. struct MPT3SAS_TARGET *sas_target_priv_data;
  4239. struct _raid_device *raid_device;
  4240. struct request *rq = scmd->request;
  4241. int class;
  4242. Mpi25SCSIIORequest_t *mpi_request;
  4243. struct _pcie_device *pcie_device = NULL;
  4244. u32 mpi_control;
  4245. u16 smid;
  4246. u16 handle;
  4247. if (ioc->logging_level & MPT_DEBUG_SCSI)
  4248. scsi_print_command(scmd);
  4249. sas_device_priv_data = scmd->device->hostdata;
  4250. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  4251. scmd->result = DID_NO_CONNECT << 16;
  4252. scmd->scsi_done(scmd);
  4253. return 0;
  4254. }
  4255. if (ioc->pci_error_recovery || ioc->remove_host) {
  4256. scmd->result = DID_NO_CONNECT << 16;
  4257. scmd->scsi_done(scmd);
  4258. return 0;
  4259. }
  4260. /*
  4261. * Bug work around for firmware SATL handling. The loop
  4262. * is based on atomic operations and ensures consistency
  4263. * since we're lockless at this point
  4264. */
  4265. do {
  4266. if (test_bit(0, &sas_device_priv_data->ata_command_pending)) {
  4267. scmd->result = SAM_STAT_BUSY;
  4268. scmd->scsi_done(scmd);
  4269. return 0;
  4270. }
  4271. } while (_scsih_set_satl_pending(scmd, true));
  4272. sas_target_priv_data = sas_device_priv_data->sas_target;
  4273. /* invalid device handle */
  4274. handle = sas_target_priv_data->handle;
  4275. if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
  4276. scmd->result = DID_NO_CONNECT << 16;
  4277. scmd->scsi_done(scmd);
  4278. return 0;
  4279. }
  4280. /* host recovery or link resets sent via IOCTLs */
  4281. if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  4282. return SCSI_MLQUEUE_HOST_BUSY;
  4283. /* device has been deleted */
  4284. else if (sas_target_priv_data->deleted) {
  4285. scmd->result = DID_NO_CONNECT << 16;
  4286. scmd->scsi_done(scmd);
  4287. return 0;
  4288. /* device busy with task management */
  4289. } else if (sas_target_priv_data->tm_busy ||
  4290. sas_device_priv_data->block)
  4291. return SCSI_MLQUEUE_DEVICE_BUSY;
  4292. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  4293. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  4294. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  4295. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  4296. else
  4297. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  4298. /* set tags */
  4299. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  4300. /* NCQ Prio supported, make sure control indicated high priority */
  4301. if (sas_device_priv_data->ncq_prio_enable) {
  4302. class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
  4303. if (class == IOPRIO_CLASS_RT)
  4304. mpi_control |= 1 << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT;
  4305. }
  4306. /* Make sure Device is not raid volume.
  4307. * We do not expose raid functionality to upper layer for warpdrive.
  4308. */
  4309. if (((!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev))
  4310. && !scsih_is_nvme(&scmd->device->sdev_gendev))
  4311. && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
  4312. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  4313. smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
  4314. if (!smid) {
  4315. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  4316. ioc->name, __func__);
  4317. goto out;
  4318. }
  4319. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4320. memset(mpi_request, 0, ioc->request_sz);
  4321. _scsih_setup_eedp(ioc, scmd, mpi_request);
  4322. if (scmd->cmd_len == 32)
  4323. mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
  4324. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  4325. if (sas_device_priv_data->sas_target->flags &
  4326. MPT_TARGET_FLAGS_RAID_COMPONENT)
  4327. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  4328. else
  4329. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  4330. mpi_request->DevHandle = cpu_to_le16(handle);
  4331. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  4332. mpi_request->Control = cpu_to_le32(mpi_control);
  4333. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  4334. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  4335. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  4336. mpi_request->SenseBufferLowAddress =
  4337. mpt3sas_base_get_sense_buffer_dma(ioc, smid);
  4338. mpi_request->SGLOffset0 = offsetof(Mpi25SCSIIORequest_t, SGL) / 4;
  4339. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  4340. mpi_request->LUN);
  4341. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  4342. if (mpi_request->DataLength) {
  4343. pcie_device = sas_target_priv_data->pcie_dev;
  4344. if (ioc->build_sg_scmd(ioc, scmd, smid, pcie_device)) {
  4345. mpt3sas_base_free_smid(ioc, smid);
  4346. goto out;
  4347. }
  4348. } else
  4349. ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
  4350. raid_device = sas_target_priv_data->raid_device;
  4351. if (raid_device && raid_device->direct_io_enabled)
  4352. mpt3sas_setup_direct_io(ioc, scmd,
  4353. raid_device, mpi_request, smid);
  4354. if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
  4355. if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
  4356. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
  4357. MPI25_SCSIIO_IOFLAGS_FAST_PATH);
  4358. ioc->put_smid_fast_path(ioc, smid, handle);
  4359. } else
  4360. ioc->put_smid_scsi_io(ioc, smid,
  4361. le16_to_cpu(mpi_request->DevHandle));
  4362. } else
  4363. ioc->put_smid_default(ioc, smid);
  4364. return 0;
  4365. out:
  4366. return SCSI_MLQUEUE_HOST_BUSY;
  4367. }
  4368. /**
  4369. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  4370. * @sense_buffer: sense data returned by target
  4371. * @data: normalized skey/asc/ascq
  4372. *
  4373. * Return nothing.
  4374. */
  4375. static void
  4376. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  4377. {
  4378. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  4379. /* descriptor format */
  4380. data->skey = sense_buffer[1] & 0x0F;
  4381. data->asc = sense_buffer[2];
  4382. data->ascq = sense_buffer[3];
  4383. } else {
  4384. /* fixed format */
  4385. data->skey = sense_buffer[2] & 0x0F;
  4386. data->asc = sense_buffer[12];
  4387. data->ascq = sense_buffer[13];
  4388. }
  4389. }
  4390. /**
  4391. * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
  4392. * @ioc: per adapter object
  4393. * @scmd: pointer to scsi command object
  4394. * @mpi_reply: reply mf payload returned from firmware
  4395. *
  4396. * scsi_status - SCSI Status code returned from target device
  4397. * scsi_state - state info associated with SCSI_IO determined by ioc
  4398. * ioc_status - ioc supplied status info
  4399. *
  4400. * Return nothing.
  4401. */
  4402. static void
  4403. _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  4404. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  4405. {
  4406. u32 response_info;
  4407. u8 *response_bytes;
  4408. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  4409. MPI2_IOCSTATUS_MASK;
  4410. u8 scsi_state = mpi_reply->SCSIState;
  4411. u8 scsi_status = mpi_reply->SCSIStatus;
  4412. char *desc_ioc_state = NULL;
  4413. char *desc_scsi_status = NULL;
  4414. char *desc_scsi_state = ioc->tmp_string;
  4415. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  4416. struct _sas_device *sas_device = NULL;
  4417. struct _pcie_device *pcie_device = NULL;
  4418. struct scsi_target *starget = scmd->device->sdev_target;
  4419. struct MPT3SAS_TARGET *priv_target = starget->hostdata;
  4420. char *device_str = NULL;
  4421. if (!priv_target)
  4422. return;
  4423. if (ioc->hide_ir_msg)
  4424. device_str = "WarpDrive";
  4425. else
  4426. device_str = "volume";
  4427. if (log_info == 0x31170000)
  4428. return;
  4429. switch (ioc_status) {
  4430. case MPI2_IOCSTATUS_SUCCESS:
  4431. desc_ioc_state = "success";
  4432. break;
  4433. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  4434. desc_ioc_state = "invalid function";
  4435. break;
  4436. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  4437. desc_ioc_state = "scsi recovered error";
  4438. break;
  4439. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  4440. desc_ioc_state = "scsi invalid dev handle";
  4441. break;
  4442. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  4443. desc_ioc_state = "scsi device not there";
  4444. break;
  4445. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  4446. desc_ioc_state = "scsi data overrun";
  4447. break;
  4448. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  4449. desc_ioc_state = "scsi data underrun";
  4450. break;
  4451. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  4452. desc_ioc_state = "scsi io data error";
  4453. break;
  4454. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  4455. desc_ioc_state = "scsi protocol error";
  4456. break;
  4457. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  4458. desc_ioc_state = "scsi task terminated";
  4459. break;
  4460. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  4461. desc_ioc_state = "scsi residual mismatch";
  4462. break;
  4463. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  4464. desc_ioc_state = "scsi task mgmt failed";
  4465. break;
  4466. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  4467. desc_ioc_state = "scsi ioc terminated";
  4468. break;
  4469. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  4470. desc_ioc_state = "scsi ext terminated";
  4471. break;
  4472. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  4473. desc_ioc_state = "eedp guard error";
  4474. break;
  4475. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  4476. desc_ioc_state = "eedp ref tag error";
  4477. break;
  4478. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  4479. desc_ioc_state = "eedp app tag error";
  4480. break;
  4481. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  4482. desc_ioc_state = "insufficient power";
  4483. break;
  4484. default:
  4485. desc_ioc_state = "unknown";
  4486. break;
  4487. }
  4488. switch (scsi_status) {
  4489. case MPI2_SCSI_STATUS_GOOD:
  4490. desc_scsi_status = "good";
  4491. break;
  4492. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  4493. desc_scsi_status = "check condition";
  4494. break;
  4495. case MPI2_SCSI_STATUS_CONDITION_MET:
  4496. desc_scsi_status = "condition met";
  4497. break;
  4498. case MPI2_SCSI_STATUS_BUSY:
  4499. desc_scsi_status = "busy";
  4500. break;
  4501. case MPI2_SCSI_STATUS_INTERMEDIATE:
  4502. desc_scsi_status = "intermediate";
  4503. break;
  4504. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  4505. desc_scsi_status = "intermediate condmet";
  4506. break;
  4507. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  4508. desc_scsi_status = "reservation conflict";
  4509. break;
  4510. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  4511. desc_scsi_status = "command terminated";
  4512. break;
  4513. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  4514. desc_scsi_status = "task set full";
  4515. break;
  4516. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  4517. desc_scsi_status = "aca active";
  4518. break;
  4519. case MPI2_SCSI_STATUS_TASK_ABORTED:
  4520. desc_scsi_status = "task aborted";
  4521. break;
  4522. default:
  4523. desc_scsi_status = "unknown";
  4524. break;
  4525. }
  4526. desc_scsi_state[0] = '\0';
  4527. if (!scsi_state)
  4528. desc_scsi_state = " ";
  4529. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  4530. strcat(desc_scsi_state, "response info ");
  4531. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4532. strcat(desc_scsi_state, "state terminated ");
  4533. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  4534. strcat(desc_scsi_state, "no status ");
  4535. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  4536. strcat(desc_scsi_state, "autosense failed ");
  4537. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  4538. strcat(desc_scsi_state, "autosense valid ");
  4539. scsi_print_command(scmd);
  4540. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  4541. pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
  4542. device_str, (unsigned long long)priv_target->sas_address);
  4543. } else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
  4544. pcie_device = mpt3sas_get_pdev_from_target(ioc, priv_target);
  4545. if (pcie_device) {
  4546. pr_info(MPT3SAS_FMT "\twwid(0x%016llx), port(%d)\n",
  4547. ioc->name,
  4548. (unsigned long long)pcie_device->wwid,
  4549. pcie_device->port_num);
  4550. if (pcie_device->enclosure_handle != 0)
  4551. pr_info(MPT3SAS_FMT
  4552. "\tenclosure logical id(0x%016llx), "
  4553. "slot(%d)\n", ioc->name,
  4554. (unsigned long long)
  4555. pcie_device->enclosure_logical_id,
  4556. pcie_device->slot);
  4557. if (pcie_device->connector_name[0])
  4558. pr_info(MPT3SAS_FMT
  4559. "\tenclosure level(0x%04x),"
  4560. "connector name( %s)\n",
  4561. ioc->name, pcie_device->enclosure_level,
  4562. pcie_device->connector_name);
  4563. pcie_device_put(pcie_device);
  4564. }
  4565. } else {
  4566. sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
  4567. if (sas_device) {
  4568. pr_warn(MPT3SAS_FMT
  4569. "\tsas_address(0x%016llx), phy(%d)\n",
  4570. ioc->name, (unsigned long long)
  4571. sas_device->sas_address, sas_device->phy);
  4572. _scsih_display_enclosure_chassis_info(ioc, sas_device,
  4573. NULL, NULL);
  4574. sas_device_put(sas_device);
  4575. }
  4576. }
  4577. pr_warn(MPT3SAS_FMT
  4578. "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
  4579. ioc->name, le16_to_cpu(mpi_reply->DevHandle),
  4580. desc_ioc_state, ioc_status, smid);
  4581. pr_warn(MPT3SAS_FMT
  4582. "\trequest_len(%d), underflow(%d), resid(%d)\n",
  4583. ioc->name, scsi_bufflen(scmd), scmd->underflow,
  4584. scsi_get_resid(scmd));
  4585. pr_warn(MPT3SAS_FMT
  4586. "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
  4587. ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  4588. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  4589. pr_warn(MPT3SAS_FMT
  4590. "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
  4591. ioc->name, desc_scsi_status,
  4592. scsi_status, desc_scsi_state, scsi_state);
  4593. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  4594. struct sense_info data;
  4595. _scsih_normalize_sense(scmd->sense_buffer, &data);
  4596. pr_warn(MPT3SAS_FMT
  4597. "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
  4598. ioc->name, data.skey,
  4599. data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
  4600. }
  4601. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  4602. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  4603. response_bytes = (u8 *)&response_info;
  4604. _scsih_response_code(ioc, response_bytes[0]);
  4605. }
  4606. }
  4607. /**
  4608. * _scsih_turn_on_pfa_led - illuminate PFA LED
  4609. * @ioc: per adapter object
  4610. * @handle: device handle
  4611. * Context: process
  4612. *
  4613. * Return nothing.
  4614. */
  4615. static void
  4616. _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  4617. {
  4618. Mpi2SepReply_t mpi_reply;
  4619. Mpi2SepRequest_t mpi_request;
  4620. struct _sas_device *sas_device;
  4621. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  4622. if (!sas_device)
  4623. return;
  4624. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  4625. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  4626. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  4627. mpi_request.SlotStatus =
  4628. cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  4629. mpi_request.DevHandle = cpu_to_le16(handle);
  4630. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  4631. if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  4632. &mpi_request)) != 0) {
  4633. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
  4634. __FILE__, __LINE__, __func__);
  4635. goto out;
  4636. }
  4637. sas_device->pfa_led_on = 1;
  4638. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  4639. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  4640. "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
  4641. ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
  4642. le32_to_cpu(mpi_reply.IOCLogInfo)));
  4643. goto out;
  4644. }
  4645. out:
  4646. sas_device_put(sas_device);
  4647. }
  4648. /**
  4649. * _scsih_turn_off_pfa_led - turn off Fault LED
  4650. * @ioc: per adapter object
  4651. * @sas_device: sas device whose PFA LED has to turned off
  4652. * Context: process
  4653. *
  4654. * Return nothing.
  4655. */
  4656. static void
  4657. _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
  4658. struct _sas_device *sas_device)
  4659. {
  4660. Mpi2SepReply_t mpi_reply;
  4661. Mpi2SepRequest_t mpi_request;
  4662. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  4663. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  4664. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  4665. mpi_request.SlotStatus = 0;
  4666. mpi_request.Slot = cpu_to_le16(sas_device->slot);
  4667. mpi_request.DevHandle = 0;
  4668. mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
  4669. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
  4670. if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  4671. &mpi_request)) != 0) {
  4672. printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
  4673. __FILE__, __LINE__, __func__);
  4674. return;
  4675. }
  4676. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  4677. dewtprintk(ioc, printk(MPT3SAS_FMT
  4678. "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
  4679. ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
  4680. le32_to_cpu(mpi_reply.IOCLogInfo)));
  4681. return;
  4682. }
  4683. }
  4684. /**
  4685. * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
  4686. * @ioc: per adapter object
  4687. * @handle: device handle
  4688. * Context: interrupt.
  4689. *
  4690. * Return nothing.
  4691. */
  4692. static void
  4693. _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  4694. {
  4695. struct fw_event_work *fw_event;
  4696. fw_event = alloc_fw_event_work(0);
  4697. if (!fw_event)
  4698. return;
  4699. fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
  4700. fw_event->device_handle = handle;
  4701. fw_event->ioc = ioc;
  4702. _scsih_fw_event_add(ioc, fw_event);
  4703. fw_event_work_put(fw_event);
  4704. }
  4705. /**
  4706. * _scsih_smart_predicted_fault - process smart errors
  4707. * @ioc: per adapter object
  4708. * @handle: device handle
  4709. * Context: interrupt.
  4710. *
  4711. * Return nothing.
  4712. */
  4713. static void
  4714. _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  4715. {
  4716. struct scsi_target *starget;
  4717. struct MPT3SAS_TARGET *sas_target_priv_data;
  4718. Mpi2EventNotificationReply_t *event_reply;
  4719. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  4720. struct _sas_device *sas_device;
  4721. ssize_t sz;
  4722. unsigned long flags;
  4723. /* only handle non-raid devices */
  4724. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4725. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  4726. if (!sas_device)
  4727. goto out_unlock;
  4728. starget = sas_device->starget;
  4729. sas_target_priv_data = starget->hostdata;
  4730. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  4731. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
  4732. goto out_unlock;
  4733. _scsih_display_enclosure_chassis_info(NULL, sas_device, NULL, starget);
  4734. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4735. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
  4736. _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
  4737. /* insert into event log */
  4738. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  4739. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  4740. event_reply = kzalloc(sz, GFP_KERNEL);
  4741. if (!event_reply) {
  4742. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4743. ioc->name, __FILE__, __LINE__, __func__);
  4744. goto out;
  4745. }
  4746. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  4747. event_reply->Event =
  4748. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  4749. event_reply->MsgLength = sz/4;
  4750. event_reply->EventDataLength =
  4751. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  4752. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  4753. event_reply->EventData;
  4754. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  4755. event_data->ASC = 0x5D;
  4756. event_data->DevHandle = cpu_to_le16(handle);
  4757. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  4758. mpt3sas_ctl_add_to_event_log(ioc, event_reply);
  4759. kfree(event_reply);
  4760. out:
  4761. if (sas_device)
  4762. sas_device_put(sas_device);
  4763. return;
  4764. out_unlock:
  4765. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4766. goto out;
  4767. }
  4768. /**
  4769. * _scsih_io_done - scsi request callback
  4770. * @ioc: per adapter object
  4771. * @smid: system request message index
  4772. * @msix_index: MSIX table index supplied by the OS
  4773. * @reply: reply message frame(lower 32bit addr)
  4774. *
  4775. * Callback handler when using _scsih_qcmd.
  4776. *
  4777. * Return 1 meaning mf should be freed from _base_interrupt
  4778. * 0 means the mf is freed from this function.
  4779. */
  4780. static u8
  4781. _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  4782. {
  4783. Mpi25SCSIIORequest_t *mpi_request;
  4784. Mpi2SCSIIOReply_t *mpi_reply;
  4785. struct scsi_cmnd *scmd;
  4786. u16 ioc_status;
  4787. u32 xfer_cnt;
  4788. u8 scsi_state;
  4789. u8 scsi_status;
  4790. u32 log_info;
  4791. struct MPT3SAS_DEVICE *sas_device_priv_data;
  4792. u32 response_code = 0;
  4793. unsigned long flags;
  4794. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  4795. if (ioc->broadcast_aen_busy || ioc->pci_error_recovery ||
  4796. ioc->got_task_abort_from_ioctl)
  4797. scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
  4798. else
  4799. scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
  4800. if (scmd == NULL)
  4801. return 1;
  4802. _scsih_set_satl_pending(scmd, false);
  4803. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4804. if (mpi_reply == NULL) {
  4805. scmd->result = DID_OK << 16;
  4806. goto out;
  4807. }
  4808. sas_device_priv_data = scmd->device->hostdata;
  4809. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  4810. sas_device_priv_data->sas_target->deleted) {
  4811. scmd->result = DID_NO_CONNECT << 16;
  4812. goto out;
  4813. }
  4814. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  4815. /*
  4816. * WARPDRIVE: If direct_io is set then it is directIO,
  4817. * the failed direct I/O should be redirected to volume
  4818. */
  4819. if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
  4820. ((ioc_status & MPI2_IOCSTATUS_MASK)
  4821. != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
  4822. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4823. ioc->scsi_lookup[smid - 1].scmd = scmd;
  4824. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4825. mpt3sas_scsi_direct_io_set(ioc, smid, 0);
  4826. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  4827. mpi_request->DevHandle =
  4828. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  4829. ioc->put_smid_scsi_io(ioc, smid,
  4830. sas_device_priv_data->sas_target->handle);
  4831. return 0;
  4832. }
  4833. /* turning off TLR */
  4834. scsi_state = mpi_reply->SCSIState;
  4835. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  4836. response_code =
  4837. le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
  4838. if (!sas_device_priv_data->tlr_snoop_check) {
  4839. sas_device_priv_data->tlr_snoop_check++;
  4840. if ((!ioc->is_warpdrive &&
  4841. !scsih_is_raid(&scmd->device->sdev_gendev) &&
  4842. !scsih_is_nvme(&scmd->device->sdev_gendev))
  4843. && sas_is_tlr_enabled(scmd->device) &&
  4844. response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
  4845. sas_disable_tlr(scmd->device);
  4846. sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
  4847. }
  4848. }
  4849. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  4850. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  4851. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  4852. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  4853. else
  4854. log_info = 0;
  4855. ioc_status &= MPI2_IOCSTATUS_MASK;
  4856. scsi_status = mpi_reply->SCSIStatus;
  4857. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  4858. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  4859. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  4860. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  4861. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  4862. }
  4863. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  4864. struct sense_info data;
  4865. const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
  4866. smid);
  4867. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  4868. le32_to_cpu(mpi_reply->SenseCount));
  4869. memcpy(scmd->sense_buffer, sense_data, sz);
  4870. _scsih_normalize_sense(scmd->sense_buffer, &data);
  4871. /* failure prediction threshold exceeded */
  4872. if (data.asc == 0x5D)
  4873. _scsih_smart_predicted_fault(ioc,
  4874. le16_to_cpu(mpi_reply->DevHandle));
  4875. mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
  4876. if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
  4877. ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
  4878. (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
  4879. (scmd->sense_buffer[2] == HARDWARE_ERROR)))
  4880. _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
  4881. }
  4882. switch (ioc_status) {
  4883. case MPI2_IOCSTATUS_BUSY:
  4884. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  4885. scmd->result = SAM_STAT_BUSY;
  4886. break;
  4887. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  4888. scmd->result = DID_NO_CONNECT << 16;
  4889. break;
  4890. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  4891. if (sas_device_priv_data->block) {
  4892. scmd->result = DID_TRANSPORT_DISRUPTED << 16;
  4893. goto out;
  4894. }
  4895. if (log_info == 0x31110630) {
  4896. if (scmd->retries > 2) {
  4897. scmd->result = DID_NO_CONNECT << 16;
  4898. scsi_device_set_state(scmd->device,
  4899. SDEV_OFFLINE);
  4900. } else {
  4901. scmd->result = DID_SOFT_ERROR << 16;
  4902. scmd->device->expecting_cc_ua = 1;
  4903. }
  4904. break;
  4905. } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
  4906. scmd->result = DID_RESET << 16;
  4907. break;
  4908. } else if ((scmd->device->channel == RAID_CHANNEL) &&
  4909. (scsi_state == (MPI2_SCSI_STATE_TERMINATED |
  4910. MPI2_SCSI_STATE_NO_SCSI_STATUS))) {
  4911. scmd->result = DID_RESET << 16;
  4912. break;
  4913. }
  4914. scmd->result = DID_SOFT_ERROR << 16;
  4915. break;
  4916. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  4917. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  4918. scmd->result = DID_RESET << 16;
  4919. break;
  4920. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  4921. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  4922. scmd->result = DID_SOFT_ERROR << 16;
  4923. else
  4924. scmd->result = (DID_OK << 16) | scsi_status;
  4925. break;
  4926. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  4927. scmd->result = (DID_OK << 16) | scsi_status;
  4928. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  4929. break;
  4930. if (xfer_cnt < scmd->underflow) {
  4931. if (scsi_status == SAM_STAT_BUSY)
  4932. scmd->result = SAM_STAT_BUSY;
  4933. else
  4934. scmd->result = DID_SOFT_ERROR << 16;
  4935. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  4936. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  4937. scmd->result = DID_SOFT_ERROR << 16;
  4938. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4939. scmd->result = DID_RESET << 16;
  4940. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  4941. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  4942. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  4943. scmd->result = (DRIVER_SENSE << 24) |
  4944. SAM_STAT_CHECK_CONDITION;
  4945. scmd->sense_buffer[0] = 0x70;
  4946. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  4947. scmd->sense_buffer[12] = 0x20;
  4948. scmd->sense_buffer[13] = 0;
  4949. }
  4950. break;
  4951. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  4952. scsi_set_resid(scmd, 0);
  4953. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  4954. case MPI2_IOCSTATUS_SUCCESS:
  4955. scmd->result = (DID_OK << 16) | scsi_status;
  4956. if (response_code ==
  4957. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
  4958. (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  4959. MPI2_SCSI_STATE_NO_SCSI_STATUS)))
  4960. scmd->result = DID_SOFT_ERROR << 16;
  4961. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4962. scmd->result = DID_RESET << 16;
  4963. break;
  4964. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  4965. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  4966. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  4967. _scsih_eedp_error_handling(scmd, ioc_status);
  4968. break;
  4969. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  4970. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  4971. case MPI2_IOCSTATUS_INVALID_SGL:
  4972. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  4973. case MPI2_IOCSTATUS_INVALID_FIELD:
  4974. case MPI2_IOCSTATUS_INVALID_STATE:
  4975. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  4976. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  4977. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  4978. default:
  4979. scmd->result = DID_SOFT_ERROR << 16;
  4980. break;
  4981. }
  4982. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  4983. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  4984. out:
  4985. scsi_dma_unmap(scmd);
  4986. scmd->scsi_done(scmd);
  4987. return 1;
  4988. }
  4989. /**
  4990. * _scsih_sas_host_refresh - refreshing sas host object contents
  4991. * @ioc: per adapter object
  4992. * Context: user
  4993. *
  4994. * During port enable, fw will send topology events for every device. Its
  4995. * possible that the handles may change from the previous setting, so this
  4996. * code keeping handles updating if changed.
  4997. *
  4998. * Return nothing.
  4999. */
  5000. static void
  5001. _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
  5002. {
  5003. u16 sz;
  5004. u16 ioc_status;
  5005. int i;
  5006. Mpi2ConfigReply_t mpi_reply;
  5007. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  5008. u16 attached_handle;
  5009. u8 link_rate;
  5010. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  5011. "updating handles for sas_host(0x%016llx)\n",
  5012. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  5013. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  5014. * sizeof(Mpi2SasIOUnit0PhyData_t));
  5015. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  5016. if (!sas_iounit_pg0) {
  5017. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5018. ioc->name, __FILE__, __LINE__, __func__);
  5019. return;
  5020. }
  5021. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  5022. sas_iounit_pg0, sz)) != 0)
  5023. goto out;
  5024. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  5025. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  5026. goto out;
  5027. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  5028. link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
  5029. if (i == 0)
  5030. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  5031. PhyData[0].ControllerDevHandle);
  5032. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  5033. attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
  5034. AttachedDevHandle);
  5035. if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  5036. link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
  5037. mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
  5038. attached_handle, i, link_rate);
  5039. }
  5040. out:
  5041. kfree(sas_iounit_pg0);
  5042. }
  5043. /**
  5044. * _scsih_sas_host_add - create sas host object
  5045. * @ioc: per adapter object
  5046. *
  5047. * Creating host side data object, stored in ioc->sas_hba
  5048. *
  5049. * Return nothing.
  5050. */
  5051. static void
  5052. _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
  5053. {
  5054. int i;
  5055. Mpi2ConfigReply_t mpi_reply;
  5056. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  5057. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  5058. Mpi2SasPhyPage0_t phy_pg0;
  5059. Mpi2SasDevicePage0_t sas_device_pg0;
  5060. Mpi2SasEnclosurePage0_t enclosure_pg0;
  5061. u16 ioc_status;
  5062. u16 sz;
  5063. u8 device_missing_delay;
  5064. u8 num_phys;
  5065. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  5066. if (!num_phys) {
  5067. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5068. ioc->name, __FILE__, __LINE__, __func__);
  5069. return;
  5070. }
  5071. ioc->sas_hba.phy = kcalloc(num_phys,
  5072. sizeof(struct _sas_phy), GFP_KERNEL);
  5073. if (!ioc->sas_hba.phy) {
  5074. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5075. ioc->name, __FILE__, __LINE__, __func__);
  5076. goto out;
  5077. }
  5078. ioc->sas_hba.num_phys = num_phys;
  5079. /* sas_iounit page 0 */
  5080. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  5081. sizeof(Mpi2SasIOUnit0PhyData_t));
  5082. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  5083. if (!sas_iounit_pg0) {
  5084. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5085. ioc->name, __FILE__, __LINE__, __func__);
  5086. return;
  5087. }
  5088. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  5089. sas_iounit_pg0, sz))) {
  5090. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5091. ioc->name, __FILE__, __LINE__, __func__);
  5092. goto out;
  5093. }
  5094. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5095. MPI2_IOCSTATUS_MASK;
  5096. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5097. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5098. ioc->name, __FILE__, __LINE__, __func__);
  5099. goto out;
  5100. }
  5101. /* sas_iounit page 1 */
  5102. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  5103. sizeof(Mpi2SasIOUnit1PhyData_t));
  5104. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  5105. if (!sas_iounit_pg1) {
  5106. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5107. ioc->name, __FILE__, __LINE__, __func__);
  5108. goto out;
  5109. }
  5110. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  5111. sas_iounit_pg1, sz))) {
  5112. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5113. ioc->name, __FILE__, __LINE__, __func__);
  5114. goto out;
  5115. }
  5116. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5117. MPI2_IOCSTATUS_MASK;
  5118. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5119. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5120. ioc->name, __FILE__, __LINE__, __func__);
  5121. goto out;
  5122. }
  5123. ioc->io_missing_delay =
  5124. sas_iounit_pg1->IODeviceMissingDelay;
  5125. device_missing_delay =
  5126. sas_iounit_pg1->ReportDeviceMissingDelay;
  5127. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  5128. ioc->device_missing_delay = (device_missing_delay &
  5129. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  5130. else
  5131. ioc->device_missing_delay = device_missing_delay &
  5132. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  5133. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  5134. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  5135. if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  5136. i))) {
  5137. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5138. ioc->name, __FILE__, __LINE__, __func__);
  5139. goto out;
  5140. }
  5141. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5142. MPI2_IOCSTATUS_MASK;
  5143. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5144. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5145. ioc->name, __FILE__, __LINE__, __func__);
  5146. goto out;
  5147. }
  5148. if (i == 0)
  5149. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  5150. PhyData[0].ControllerDevHandle);
  5151. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  5152. ioc->sas_hba.phy[i].phy_id = i;
  5153. mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  5154. phy_pg0, ioc->sas_hba.parent_dev);
  5155. }
  5156. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  5157. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
  5158. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5159. ioc->name, __FILE__, __LINE__, __func__);
  5160. goto out;
  5161. }
  5162. ioc->sas_hba.enclosure_handle =
  5163. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  5164. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  5165. pr_info(MPT3SAS_FMT
  5166. "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
  5167. ioc->name, ioc->sas_hba.handle,
  5168. (unsigned long long) ioc->sas_hba.sas_address,
  5169. ioc->sas_hba.num_phys) ;
  5170. if (ioc->sas_hba.enclosure_handle) {
  5171. if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  5172. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  5173. ioc->sas_hba.enclosure_handle)))
  5174. ioc->sas_hba.enclosure_logical_id =
  5175. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  5176. }
  5177. out:
  5178. kfree(sas_iounit_pg1);
  5179. kfree(sas_iounit_pg0);
  5180. }
  5181. /**
  5182. * _scsih_expander_add - creating expander object
  5183. * @ioc: per adapter object
  5184. * @handle: expander handle
  5185. *
  5186. * Creating expander object, stored in ioc->sas_expander_list.
  5187. *
  5188. * Return 0 for success, else error.
  5189. */
  5190. static int
  5191. _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  5192. {
  5193. struct _sas_node *sas_expander;
  5194. Mpi2ConfigReply_t mpi_reply;
  5195. Mpi2ExpanderPage0_t expander_pg0;
  5196. Mpi2ExpanderPage1_t expander_pg1;
  5197. Mpi2SasEnclosurePage0_t enclosure_pg0;
  5198. u32 ioc_status;
  5199. u16 parent_handle;
  5200. u64 sas_address, sas_address_parent = 0;
  5201. int i;
  5202. unsigned long flags;
  5203. struct _sas_port *mpt3sas_port = NULL;
  5204. int rc = 0;
  5205. if (!handle)
  5206. return -1;
  5207. if (ioc->shost_recovery || ioc->pci_error_recovery)
  5208. return -1;
  5209. if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  5210. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  5211. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5212. ioc->name, __FILE__, __LINE__, __func__);
  5213. return -1;
  5214. }
  5215. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5216. MPI2_IOCSTATUS_MASK;
  5217. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5218. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5219. ioc->name, __FILE__, __LINE__, __func__);
  5220. return -1;
  5221. }
  5222. /* handle out of order topology events */
  5223. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  5224. if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
  5225. != 0) {
  5226. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5227. ioc->name, __FILE__, __LINE__, __func__);
  5228. return -1;
  5229. }
  5230. if (sas_address_parent != ioc->sas_hba.sas_address) {
  5231. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5232. sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
  5233. sas_address_parent);
  5234. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5235. if (!sas_expander) {
  5236. rc = _scsih_expander_add(ioc, parent_handle);
  5237. if (rc != 0)
  5238. return rc;
  5239. }
  5240. }
  5241. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5242. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  5243. sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
  5244. sas_address);
  5245. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5246. if (sas_expander)
  5247. return 0;
  5248. sas_expander = kzalloc(sizeof(struct _sas_node),
  5249. GFP_KERNEL);
  5250. if (!sas_expander) {
  5251. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5252. ioc->name, __FILE__, __LINE__, __func__);
  5253. return -1;
  5254. }
  5255. sas_expander->handle = handle;
  5256. sas_expander->num_phys = expander_pg0.NumPhys;
  5257. sas_expander->sas_address_parent = sas_address_parent;
  5258. sas_expander->sas_address = sas_address;
  5259. pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
  5260. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  5261. handle, parent_handle, (unsigned long long)
  5262. sas_expander->sas_address, sas_expander->num_phys);
  5263. if (!sas_expander->num_phys)
  5264. goto out_fail;
  5265. sas_expander->phy = kcalloc(sas_expander->num_phys,
  5266. sizeof(struct _sas_phy), GFP_KERNEL);
  5267. if (!sas_expander->phy) {
  5268. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5269. ioc->name, __FILE__, __LINE__, __func__);
  5270. rc = -1;
  5271. goto out_fail;
  5272. }
  5273. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  5274. mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
  5275. sas_address_parent);
  5276. if (!mpt3sas_port) {
  5277. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5278. ioc->name, __FILE__, __LINE__, __func__);
  5279. rc = -1;
  5280. goto out_fail;
  5281. }
  5282. sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
  5283. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  5284. if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
  5285. &expander_pg1, i, handle))) {
  5286. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5287. ioc->name, __FILE__, __LINE__, __func__);
  5288. rc = -1;
  5289. goto out_fail;
  5290. }
  5291. sas_expander->phy[i].handle = handle;
  5292. sas_expander->phy[i].phy_id = i;
  5293. if ((mpt3sas_transport_add_expander_phy(ioc,
  5294. &sas_expander->phy[i], expander_pg1,
  5295. sas_expander->parent_dev))) {
  5296. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5297. ioc->name, __FILE__, __LINE__, __func__);
  5298. rc = -1;
  5299. goto out_fail;
  5300. }
  5301. }
  5302. if (sas_expander->enclosure_handle) {
  5303. if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  5304. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  5305. sas_expander->enclosure_handle)))
  5306. sas_expander->enclosure_logical_id =
  5307. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  5308. }
  5309. _scsih_expander_node_add(ioc, sas_expander);
  5310. return 0;
  5311. out_fail:
  5312. if (mpt3sas_port)
  5313. mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
  5314. sas_address_parent);
  5315. kfree(sas_expander);
  5316. return rc;
  5317. }
  5318. /**
  5319. * mpt3sas_expander_remove - removing expander object
  5320. * @ioc: per adapter object
  5321. * @sas_address: expander sas_address
  5322. *
  5323. * Return nothing.
  5324. */
  5325. void
  5326. mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
  5327. {
  5328. struct _sas_node *sas_expander;
  5329. unsigned long flags;
  5330. if (ioc->shost_recovery)
  5331. return;
  5332. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5333. sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
  5334. sas_address);
  5335. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5336. if (sas_expander)
  5337. _scsih_expander_node_remove(ioc, sas_expander);
  5338. }
  5339. /**
  5340. * _scsih_done - internal SCSI_IO callback handler.
  5341. * @ioc: per adapter object
  5342. * @smid: system request message index
  5343. * @msix_index: MSIX table index supplied by the OS
  5344. * @reply: reply message frame(lower 32bit addr)
  5345. *
  5346. * Callback handler when sending internal generated SCSI_IO.
  5347. * The callback index passed is `ioc->scsih_cb_idx`
  5348. *
  5349. * Return 1 meaning mf should be freed from _base_interrupt
  5350. * 0 means the mf is freed from this function.
  5351. */
  5352. static u8
  5353. _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  5354. {
  5355. MPI2DefaultReply_t *mpi_reply;
  5356. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  5357. if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
  5358. return 1;
  5359. if (ioc->scsih_cmds.smid != smid)
  5360. return 1;
  5361. ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
  5362. if (mpi_reply) {
  5363. memcpy(ioc->scsih_cmds.reply, mpi_reply,
  5364. mpi_reply->MsgLength*4);
  5365. ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
  5366. }
  5367. ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
  5368. complete(&ioc->scsih_cmds.done);
  5369. return 1;
  5370. }
  5371. #define MPT3_MAX_LUNS (255)
  5372. /**
  5373. * _scsih_check_access_status - check access flags
  5374. * @ioc: per adapter object
  5375. * @sas_address: sas address
  5376. * @handle: sas device handle
  5377. * @access_flags: errors returned during discovery of the device
  5378. *
  5379. * Return 0 for success, else failure
  5380. */
  5381. static u8
  5382. _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
  5383. u16 handle, u8 access_status)
  5384. {
  5385. u8 rc = 1;
  5386. char *desc = NULL;
  5387. switch (access_status) {
  5388. case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
  5389. case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
  5390. rc = 0;
  5391. break;
  5392. case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
  5393. desc = "sata capability failed";
  5394. break;
  5395. case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
  5396. desc = "sata affiliation conflict";
  5397. break;
  5398. case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
  5399. desc = "route not addressable";
  5400. break;
  5401. case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
  5402. desc = "smp error not addressable";
  5403. break;
  5404. case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
  5405. desc = "device blocked";
  5406. break;
  5407. case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
  5408. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
  5409. case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
  5410. case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
  5411. case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
  5412. case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
  5413. case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
  5414. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
  5415. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
  5416. case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
  5417. case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
  5418. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
  5419. desc = "sata initialization failed";
  5420. break;
  5421. default:
  5422. desc = "unknown";
  5423. break;
  5424. }
  5425. if (!rc)
  5426. return 0;
  5427. pr_err(MPT3SAS_FMT
  5428. "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
  5429. ioc->name, desc, (unsigned long long)sas_address, handle);
  5430. return rc;
  5431. }
  5432. /**
  5433. * _scsih_get_enclosure_logicalid_chassis_slot - get device's
  5434. * EnclosureLogicalID and ChassisSlot information.
  5435. * @ioc: per adapter object
  5436. * @sas_device_pg0: SAS device page0
  5437. * @sas_device: per sas device object
  5438. *
  5439. * Returns nothing.
  5440. */
  5441. static void
  5442. _scsih_get_enclosure_logicalid_chassis_slot(struct MPT3SAS_ADAPTER *ioc,
  5443. Mpi2SasDevicePage0_t *sas_device_pg0, struct _sas_device *sas_device)
  5444. {
  5445. Mpi2ConfigReply_t mpi_reply;
  5446. Mpi2SasEnclosurePage0_t enclosure_pg0;
  5447. if (!sas_device_pg0 || !sas_device)
  5448. return;
  5449. sas_device->enclosure_handle =
  5450. le16_to_cpu(sas_device_pg0->EnclosureHandle);
  5451. sas_device->is_chassis_slot_valid = 0;
  5452. if (!le16_to_cpu(sas_device_pg0->EnclosureHandle))
  5453. return;
  5454. if (mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  5455. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  5456. le16_to_cpu(sas_device_pg0->EnclosureHandle))) {
  5457. pr_err(MPT3SAS_FMT
  5458. "Enclosure Pg0 read failed for handle(0x%04x)\n",
  5459. ioc->name, le16_to_cpu(sas_device_pg0->EnclosureHandle));
  5460. return;
  5461. }
  5462. sas_device->enclosure_logical_id =
  5463. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  5464. if (le16_to_cpu(enclosure_pg0.Flags) &
  5465. MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
  5466. sas_device->is_chassis_slot_valid = 1;
  5467. sas_device->chassis_slot = enclosure_pg0.ChassisSlot;
  5468. }
  5469. }
  5470. /**
  5471. * _scsih_check_device - checking device responsiveness
  5472. * @ioc: per adapter object
  5473. * @parent_sas_address: sas address of parent expander or sas host
  5474. * @handle: attached device handle
  5475. * @phy_numberv: phy number
  5476. * @link_rate: new link rate
  5477. *
  5478. * Returns nothing.
  5479. */
  5480. static void
  5481. _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
  5482. u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
  5483. {
  5484. Mpi2ConfigReply_t mpi_reply;
  5485. Mpi2SasDevicePage0_t sas_device_pg0;
  5486. struct _sas_device *sas_device;
  5487. u32 ioc_status;
  5488. unsigned long flags;
  5489. u64 sas_address;
  5490. struct scsi_target *starget;
  5491. struct MPT3SAS_TARGET *sas_target_priv_data;
  5492. u32 device_info;
  5493. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  5494. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
  5495. return;
  5496. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  5497. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  5498. return;
  5499. /* wide port handling ~ we need only handle device once for the phy that
  5500. * is matched in sas device page zero
  5501. */
  5502. if (phy_number != sas_device_pg0.PhyNum)
  5503. return;
  5504. /* check if this is end device */
  5505. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  5506. if (!(_scsih_is_end_device(device_info)))
  5507. return;
  5508. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5509. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  5510. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  5511. sas_address);
  5512. if (!sas_device)
  5513. goto out_unlock;
  5514. if (unlikely(sas_device->handle != handle)) {
  5515. starget = sas_device->starget;
  5516. sas_target_priv_data = starget->hostdata;
  5517. starget_printk(KERN_INFO, starget,
  5518. "handle changed from(0x%04x) to (0x%04x)!!!\n",
  5519. sas_device->handle, handle);
  5520. sas_target_priv_data->handle = handle;
  5521. sas_device->handle = handle;
  5522. if (le16_to_cpu(sas_device_pg0.Flags) &
  5523. MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
  5524. sas_device->enclosure_level =
  5525. sas_device_pg0.EnclosureLevel;
  5526. memcpy(sas_device->connector_name,
  5527. sas_device_pg0.ConnectorName, 4);
  5528. sas_device->connector_name[4] = '\0';
  5529. } else {
  5530. sas_device->enclosure_level = 0;
  5531. sas_device->connector_name[0] = '\0';
  5532. }
  5533. _scsih_get_enclosure_logicalid_chassis_slot(ioc,
  5534. &sas_device_pg0, sas_device);
  5535. }
  5536. /* check if device is present */
  5537. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  5538. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  5539. pr_err(MPT3SAS_FMT
  5540. "device is not present handle(0x%04x), flags!!!\n",
  5541. ioc->name, handle);
  5542. goto out_unlock;
  5543. }
  5544. /* check if there were any issues with discovery */
  5545. if (_scsih_check_access_status(ioc, sas_address, handle,
  5546. sas_device_pg0.AccessStatus))
  5547. goto out_unlock;
  5548. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5549. _scsih_ublock_io_device(ioc, sas_address);
  5550. if (sas_device)
  5551. sas_device_put(sas_device);
  5552. return;
  5553. out_unlock:
  5554. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5555. if (sas_device)
  5556. sas_device_put(sas_device);
  5557. }
  5558. /**
  5559. * _scsih_add_device - creating sas device object
  5560. * @ioc: per adapter object
  5561. * @handle: sas device handle
  5562. * @phy_num: phy number end device attached to
  5563. * @is_pd: is this hidden raid component
  5564. *
  5565. * Creating end device object, stored in ioc->sas_device_list.
  5566. *
  5567. * Returns 0 for success, non-zero for failure.
  5568. */
  5569. static int
  5570. _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
  5571. u8 is_pd)
  5572. {
  5573. Mpi2ConfigReply_t mpi_reply;
  5574. Mpi2SasDevicePage0_t sas_device_pg0;
  5575. Mpi2SasEnclosurePage0_t enclosure_pg0;
  5576. struct _sas_device *sas_device;
  5577. u32 ioc_status;
  5578. u64 sas_address;
  5579. u32 device_info;
  5580. int encl_pg0_rc = -1;
  5581. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  5582. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  5583. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5584. ioc->name, __FILE__, __LINE__, __func__);
  5585. return -1;
  5586. }
  5587. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5588. MPI2_IOCSTATUS_MASK;
  5589. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5590. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5591. ioc->name, __FILE__, __LINE__, __func__);
  5592. return -1;
  5593. }
  5594. /* check if this is end device */
  5595. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  5596. if (!(_scsih_is_end_device(device_info)))
  5597. return -1;
  5598. set_bit(handle, ioc->pend_os_device_add);
  5599. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  5600. /* check if device is present */
  5601. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  5602. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  5603. pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
  5604. ioc->name, handle);
  5605. return -1;
  5606. }
  5607. /* check if there were any issues with discovery */
  5608. if (_scsih_check_access_status(ioc, sas_address, handle,
  5609. sas_device_pg0.AccessStatus))
  5610. return -1;
  5611. sas_device = mpt3sas_get_sdev_by_addr(ioc,
  5612. sas_address);
  5613. if (sas_device) {
  5614. clear_bit(handle, ioc->pend_os_device_add);
  5615. sas_device_put(sas_device);
  5616. return -1;
  5617. }
  5618. if (sas_device_pg0.EnclosureHandle) {
  5619. encl_pg0_rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  5620. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  5621. sas_device_pg0.EnclosureHandle);
  5622. if (encl_pg0_rc)
  5623. pr_info(MPT3SAS_FMT
  5624. "Enclosure Pg0 read failed for handle(0x%04x)\n",
  5625. ioc->name, sas_device_pg0.EnclosureHandle);
  5626. }
  5627. sas_device = kzalloc(sizeof(struct _sas_device),
  5628. GFP_KERNEL);
  5629. if (!sas_device) {
  5630. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5631. ioc->name, __FILE__, __LINE__, __func__);
  5632. return 0;
  5633. }
  5634. kref_init(&sas_device->refcount);
  5635. sas_device->handle = handle;
  5636. if (_scsih_get_sas_address(ioc,
  5637. le16_to_cpu(sas_device_pg0.ParentDevHandle),
  5638. &sas_device->sas_address_parent) != 0)
  5639. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5640. ioc->name, __FILE__, __LINE__, __func__);
  5641. sas_device->enclosure_handle =
  5642. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  5643. if (sas_device->enclosure_handle != 0)
  5644. sas_device->slot =
  5645. le16_to_cpu(sas_device_pg0.Slot);
  5646. sas_device->device_info = device_info;
  5647. sas_device->sas_address = sas_address;
  5648. sas_device->phy = sas_device_pg0.PhyNum;
  5649. sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
  5650. MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
  5651. if (le16_to_cpu(sas_device_pg0.Flags)
  5652. & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
  5653. sas_device->enclosure_level =
  5654. sas_device_pg0.EnclosureLevel;
  5655. memcpy(sas_device->connector_name,
  5656. sas_device_pg0.ConnectorName, 4);
  5657. sas_device->connector_name[4] = '\0';
  5658. } else {
  5659. sas_device->enclosure_level = 0;
  5660. sas_device->connector_name[0] = '\0';
  5661. }
  5662. /* get enclosure_logical_id & chassis_slot */
  5663. sas_device->is_chassis_slot_valid = 0;
  5664. if (encl_pg0_rc == 0) {
  5665. sas_device->enclosure_logical_id =
  5666. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  5667. if (le16_to_cpu(enclosure_pg0.Flags) &
  5668. MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
  5669. sas_device->is_chassis_slot_valid = 1;
  5670. sas_device->chassis_slot =
  5671. enclosure_pg0.ChassisSlot;
  5672. }
  5673. }
  5674. /* get device name */
  5675. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  5676. if (ioc->wait_for_discovery_to_complete)
  5677. _scsih_sas_device_init_add(ioc, sas_device);
  5678. else
  5679. _scsih_sas_device_add(ioc, sas_device);
  5680. sas_device_put(sas_device);
  5681. return 0;
  5682. }
  5683. /**
  5684. * _scsih_remove_device - removing sas device object
  5685. * @ioc: per adapter object
  5686. * @sas_device_delete: the sas_device object
  5687. *
  5688. * Return nothing.
  5689. */
  5690. static void
  5691. _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
  5692. struct _sas_device *sas_device)
  5693. {
  5694. struct MPT3SAS_TARGET *sas_target_priv_data;
  5695. if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
  5696. (sas_device->pfa_led_on)) {
  5697. _scsih_turn_off_pfa_led(ioc, sas_device);
  5698. sas_device->pfa_led_on = 0;
  5699. }
  5700. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5701. "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
  5702. ioc->name, __func__,
  5703. sas_device->handle, (unsigned long long)
  5704. sas_device->sas_address));
  5705. dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
  5706. NULL, NULL));
  5707. if (sas_device->starget && sas_device->starget->hostdata) {
  5708. sas_target_priv_data = sas_device->starget->hostdata;
  5709. sas_target_priv_data->deleted = 1;
  5710. _scsih_ublock_io_device(ioc, sas_device->sas_address);
  5711. sas_target_priv_data->handle =
  5712. MPT3SAS_INVALID_DEVICE_HANDLE;
  5713. }
  5714. if (!ioc->hide_drives)
  5715. mpt3sas_transport_port_remove(ioc,
  5716. sas_device->sas_address,
  5717. sas_device->sas_address_parent);
  5718. pr_info(MPT3SAS_FMT
  5719. "removing handle(0x%04x), sas_addr(0x%016llx)\n",
  5720. ioc->name, sas_device->handle,
  5721. (unsigned long long) sas_device->sas_address);
  5722. _scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
  5723. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5724. "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
  5725. ioc->name, __func__,
  5726. sas_device->handle, (unsigned long long)
  5727. sas_device->sas_address));
  5728. dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
  5729. NULL, NULL));
  5730. }
  5731. /**
  5732. * _scsih_sas_topology_change_event_debug - debug for topology event
  5733. * @ioc: per adapter object
  5734. * @event_data: event data payload
  5735. * Context: user.
  5736. */
  5737. static void
  5738. _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  5739. Mpi2EventDataSasTopologyChangeList_t *event_data)
  5740. {
  5741. int i;
  5742. u16 handle;
  5743. u16 reason_code;
  5744. u8 phy_number;
  5745. char *status_str = NULL;
  5746. u8 link_rate, prev_link_rate;
  5747. switch (event_data->ExpStatus) {
  5748. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  5749. status_str = "add";
  5750. break;
  5751. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  5752. status_str = "remove";
  5753. break;
  5754. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  5755. case 0:
  5756. status_str = "responding";
  5757. break;
  5758. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  5759. status_str = "remove delay";
  5760. break;
  5761. default:
  5762. status_str = "unknown status";
  5763. break;
  5764. }
  5765. pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
  5766. ioc->name, status_str);
  5767. pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
  5768. "start_phy(%02d), count(%d)\n",
  5769. le16_to_cpu(event_data->ExpanderDevHandle),
  5770. le16_to_cpu(event_data->EnclosureHandle),
  5771. event_data->StartPhyNum, event_data->NumEntries);
  5772. for (i = 0; i < event_data->NumEntries; i++) {
  5773. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  5774. if (!handle)
  5775. continue;
  5776. phy_number = event_data->StartPhyNum + i;
  5777. reason_code = event_data->PHY[i].PhyStatus &
  5778. MPI2_EVENT_SAS_TOPO_RC_MASK;
  5779. switch (reason_code) {
  5780. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  5781. status_str = "target add";
  5782. break;
  5783. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  5784. status_str = "target remove";
  5785. break;
  5786. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  5787. status_str = "delay target remove";
  5788. break;
  5789. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  5790. status_str = "link rate change";
  5791. break;
  5792. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  5793. status_str = "target responding";
  5794. break;
  5795. default:
  5796. status_str = "unknown";
  5797. break;
  5798. }
  5799. link_rate = event_data->PHY[i].LinkRate >> 4;
  5800. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  5801. pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
  5802. " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
  5803. handle, status_str, link_rate, prev_link_rate);
  5804. }
  5805. }
  5806. /**
  5807. * _scsih_sas_topology_change_event - handle topology changes
  5808. * @ioc: per adapter object
  5809. * @fw_event: The fw_event_work object
  5810. * Context: user.
  5811. *
  5812. */
  5813. static int
  5814. _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
  5815. struct fw_event_work *fw_event)
  5816. {
  5817. int i;
  5818. u16 parent_handle, handle;
  5819. u16 reason_code;
  5820. u8 phy_number, max_phys;
  5821. struct _sas_node *sas_expander;
  5822. u64 sas_address;
  5823. unsigned long flags;
  5824. u8 link_rate, prev_link_rate;
  5825. Mpi2EventDataSasTopologyChangeList_t *event_data =
  5826. (Mpi2EventDataSasTopologyChangeList_t *)
  5827. fw_event->event_data;
  5828. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5829. _scsih_sas_topology_change_event_debug(ioc, event_data);
  5830. if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
  5831. return 0;
  5832. if (!ioc->sas_hba.num_phys)
  5833. _scsih_sas_host_add(ioc);
  5834. else
  5835. _scsih_sas_host_refresh(ioc);
  5836. if (fw_event->ignore) {
  5837. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5838. "ignoring expander event\n", ioc->name));
  5839. return 0;
  5840. }
  5841. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  5842. /* handle expander add */
  5843. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  5844. if (_scsih_expander_add(ioc, parent_handle) != 0)
  5845. return 0;
  5846. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5847. sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
  5848. parent_handle);
  5849. if (sas_expander) {
  5850. sas_address = sas_expander->sas_address;
  5851. max_phys = sas_expander->num_phys;
  5852. } else if (parent_handle < ioc->sas_hba.num_phys) {
  5853. sas_address = ioc->sas_hba.sas_address;
  5854. max_phys = ioc->sas_hba.num_phys;
  5855. } else {
  5856. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5857. return 0;
  5858. }
  5859. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5860. /* handle siblings events */
  5861. for (i = 0; i < event_data->NumEntries; i++) {
  5862. if (fw_event->ignore) {
  5863. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5864. "ignoring expander event\n", ioc->name));
  5865. return 0;
  5866. }
  5867. if (ioc->remove_host || ioc->pci_error_recovery)
  5868. return 0;
  5869. phy_number = event_data->StartPhyNum + i;
  5870. if (phy_number >= max_phys)
  5871. continue;
  5872. reason_code = event_data->PHY[i].PhyStatus &
  5873. MPI2_EVENT_SAS_TOPO_RC_MASK;
  5874. if ((event_data->PHY[i].PhyStatus &
  5875. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
  5876. MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
  5877. continue;
  5878. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  5879. if (!handle)
  5880. continue;
  5881. link_rate = event_data->PHY[i].LinkRate >> 4;
  5882. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  5883. switch (reason_code) {
  5884. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  5885. if (ioc->shost_recovery)
  5886. break;
  5887. if (link_rate == prev_link_rate)
  5888. break;
  5889. mpt3sas_transport_update_links(ioc, sas_address,
  5890. handle, phy_number, link_rate);
  5891. if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  5892. break;
  5893. _scsih_check_device(ioc, sas_address, handle,
  5894. phy_number, link_rate);
  5895. if (!test_bit(handle, ioc->pend_os_device_add))
  5896. break;
  5897. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  5898. if (ioc->shost_recovery)
  5899. break;
  5900. mpt3sas_transport_update_links(ioc, sas_address,
  5901. handle, phy_number, link_rate);
  5902. _scsih_add_device(ioc, handle, phy_number, 0);
  5903. break;
  5904. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  5905. _scsih_device_remove_by_handle(ioc, handle);
  5906. break;
  5907. }
  5908. }
  5909. /* handle expander removal */
  5910. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
  5911. sas_expander)
  5912. mpt3sas_expander_remove(ioc, sas_address);
  5913. return 0;
  5914. }
  5915. /**
  5916. * _scsih_sas_device_status_change_event_debug - debug for device event
  5917. * @event_data: event data payload
  5918. * Context: user.
  5919. *
  5920. * Return nothing.
  5921. */
  5922. static void
  5923. _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  5924. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  5925. {
  5926. char *reason_str = NULL;
  5927. switch (event_data->ReasonCode) {
  5928. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  5929. reason_str = "smart data";
  5930. break;
  5931. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  5932. reason_str = "unsupported device discovered";
  5933. break;
  5934. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  5935. reason_str = "internal device reset";
  5936. break;
  5937. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  5938. reason_str = "internal task abort";
  5939. break;
  5940. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  5941. reason_str = "internal task abort set";
  5942. break;
  5943. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  5944. reason_str = "internal clear task set";
  5945. break;
  5946. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  5947. reason_str = "internal query task";
  5948. break;
  5949. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  5950. reason_str = "sata init failure";
  5951. break;
  5952. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  5953. reason_str = "internal device reset complete";
  5954. break;
  5955. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  5956. reason_str = "internal task abort complete";
  5957. break;
  5958. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  5959. reason_str = "internal async notification";
  5960. break;
  5961. case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
  5962. reason_str = "expander reduced functionality";
  5963. break;
  5964. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
  5965. reason_str = "expander reduced functionality complete";
  5966. break;
  5967. default:
  5968. reason_str = "unknown reason";
  5969. break;
  5970. }
  5971. pr_info(MPT3SAS_FMT "device status change: (%s)\n"
  5972. "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
  5973. ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
  5974. (unsigned long long)le64_to_cpu(event_data->SASAddress),
  5975. le16_to_cpu(event_data->TaskTag));
  5976. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  5977. pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  5978. event_data->ASC, event_data->ASCQ);
  5979. pr_info("\n");
  5980. }
  5981. /**
  5982. * _scsih_sas_device_status_change_event - handle device status change
  5983. * @ioc: per adapter object
  5984. * @fw_event: The fw_event_work object
  5985. * Context: user.
  5986. *
  5987. * Return nothing.
  5988. */
  5989. static void
  5990. _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
  5991. struct fw_event_work *fw_event)
  5992. {
  5993. struct MPT3SAS_TARGET *target_priv_data;
  5994. struct _sas_device *sas_device;
  5995. u64 sas_address;
  5996. unsigned long flags;
  5997. Mpi2EventDataSasDeviceStatusChange_t *event_data =
  5998. (Mpi2EventDataSasDeviceStatusChange_t *)
  5999. fw_event->event_data;
  6000. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  6001. _scsih_sas_device_status_change_event_debug(ioc,
  6002. event_data);
  6003. /* In MPI Revision K (0xC), the internal device reset complete was
  6004. * implemented, so avoid setting tm_busy flag for older firmware.
  6005. */
  6006. if ((ioc->facts.HeaderVersion >> 8) < 0xC)
  6007. return;
  6008. if (event_data->ReasonCode !=
  6009. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  6010. event_data->ReasonCode !=
  6011. MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
  6012. return;
  6013. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6014. sas_address = le64_to_cpu(event_data->SASAddress);
  6015. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  6016. sas_address);
  6017. if (!sas_device || !sas_device->starget)
  6018. goto out;
  6019. target_priv_data = sas_device->starget->hostdata;
  6020. if (!target_priv_data)
  6021. goto out;
  6022. if (event_data->ReasonCode ==
  6023. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
  6024. target_priv_data->tm_busy = 1;
  6025. else
  6026. target_priv_data->tm_busy = 0;
  6027. out:
  6028. if (sas_device)
  6029. sas_device_put(sas_device);
  6030. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6031. }
  6032. /**
  6033. * _scsih_check_pcie_access_status - check access flags
  6034. * @ioc: per adapter object
  6035. * @wwid: wwid
  6036. * @handle: sas device handle
  6037. * @access_flags: errors returned during discovery of the device
  6038. *
  6039. * Return 0 for success, else failure
  6040. */
  6041. static u8
  6042. _scsih_check_pcie_access_status(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
  6043. u16 handle, u8 access_status)
  6044. {
  6045. u8 rc = 1;
  6046. char *desc = NULL;
  6047. switch (access_status) {
  6048. case MPI26_PCIEDEV0_ASTATUS_NO_ERRORS:
  6049. case MPI26_PCIEDEV0_ASTATUS_NEEDS_INITIALIZATION:
  6050. rc = 0;
  6051. break;
  6052. case MPI26_PCIEDEV0_ASTATUS_CAPABILITY_FAILED:
  6053. desc = "PCIe device capability failed";
  6054. break;
  6055. case MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED:
  6056. desc = "PCIe device blocked";
  6057. break;
  6058. case MPI26_PCIEDEV0_ASTATUS_MEMORY_SPACE_ACCESS_FAILED:
  6059. desc = "PCIe device mem space access failed";
  6060. break;
  6061. case MPI26_PCIEDEV0_ASTATUS_UNSUPPORTED_DEVICE:
  6062. desc = "PCIe device unsupported";
  6063. break;
  6064. case MPI26_PCIEDEV0_ASTATUS_MSIX_REQUIRED:
  6065. desc = "PCIe device MSIx Required";
  6066. break;
  6067. case MPI26_PCIEDEV0_ASTATUS_INIT_FAIL_MAX:
  6068. desc = "PCIe device init fail max";
  6069. break;
  6070. case MPI26_PCIEDEV0_ASTATUS_UNKNOWN:
  6071. desc = "PCIe device status unknown";
  6072. break;
  6073. case MPI26_PCIEDEV0_ASTATUS_NVME_READY_TIMEOUT:
  6074. desc = "nvme ready timeout";
  6075. break;
  6076. case MPI26_PCIEDEV0_ASTATUS_NVME_DEVCFG_UNSUPPORTED:
  6077. desc = "nvme device configuration unsupported";
  6078. break;
  6079. case MPI26_PCIEDEV0_ASTATUS_NVME_IDENTIFY_FAILED:
  6080. desc = "nvme identify failed";
  6081. break;
  6082. case MPI26_PCIEDEV0_ASTATUS_NVME_QCONFIG_FAILED:
  6083. desc = "nvme qconfig failed";
  6084. break;
  6085. case MPI26_PCIEDEV0_ASTATUS_NVME_QCREATION_FAILED:
  6086. desc = "nvme qcreation failed";
  6087. break;
  6088. case MPI26_PCIEDEV0_ASTATUS_NVME_EVENTCFG_FAILED:
  6089. desc = "nvme eventcfg failed";
  6090. break;
  6091. case MPI26_PCIEDEV0_ASTATUS_NVME_GET_FEATURE_STAT_FAILED:
  6092. desc = "nvme get feature stat failed";
  6093. break;
  6094. case MPI26_PCIEDEV0_ASTATUS_NVME_IDLE_TIMEOUT:
  6095. desc = "nvme idle timeout";
  6096. break;
  6097. case MPI26_PCIEDEV0_ASTATUS_NVME_FAILURE_STATUS:
  6098. desc = "nvme failure status";
  6099. break;
  6100. default:
  6101. pr_err(MPT3SAS_FMT
  6102. " NVMe discovery error(0x%02x): wwid(0x%016llx),"
  6103. "handle(0x%04x)\n", ioc->name, access_status,
  6104. (unsigned long long)wwid, handle);
  6105. return rc;
  6106. }
  6107. if (!rc)
  6108. return rc;
  6109. pr_info(MPT3SAS_FMT
  6110. "NVMe discovery error(%s): wwid(0x%016llx), handle(0x%04x)\n",
  6111. ioc->name, desc,
  6112. (unsigned long long)wwid, handle);
  6113. return rc;
  6114. }
  6115. /**
  6116. * _scsih_pcie_device_remove_from_sml - removing pcie device
  6117. * from SML and free up associated memory
  6118. * @ioc: per adapter object
  6119. * @pcie_device: the pcie_device object
  6120. *
  6121. * Return nothing.
  6122. */
  6123. static void
  6124. _scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
  6125. struct _pcie_device *pcie_device)
  6126. {
  6127. struct MPT3SAS_TARGET *sas_target_priv_data;
  6128. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6129. "%s: enter: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  6130. pcie_device->handle, (unsigned long long)
  6131. pcie_device->wwid));
  6132. if (pcie_device->enclosure_handle != 0)
  6133. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6134. "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
  6135. ioc->name, __func__,
  6136. (unsigned long long)pcie_device->enclosure_logical_id,
  6137. pcie_device->slot));
  6138. if (pcie_device->connector_name[0] != '\0')
  6139. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6140. "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
  6141. ioc->name, __func__,
  6142. pcie_device->enclosure_level,
  6143. pcie_device->connector_name));
  6144. if (pcie_device->starget && pcie_device->starget->hostdata) {
  6145. sas_target_priv_data = pcie_device->starget->hostdata;
  6146. sas_target_priv_data->deleted = 1;
  6147. _scsih_ublock_io_device(ioc, pcie_device->wwid);
  6148. sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  6149. }
  6150. pr_info(MPT3SAS_FMT
  6151. "removing handle(0x%04x), wwid (0x%016llx)\n",
  6152. ioc->name, pcie_device->handle,
  6153. (unsigned long long) pcie_device->wwid);
  6154. if (pcie_device->enclosure_handle != 0)
  6155. pr_info(MPT3SAS_FMT
  6156. "removing : enclosure logical id(0x%016llx), slot(%d)\n",
  6157. ioc->name,
  6158. (unsigned long long)pcie_device->enclosure_logical_id,
  6159. pcie_device->slot);
  6160. if (pcie_device->connector_name[0] != '\0')
  6161. pr_info(MPT3SAS_FMT
  6162. "removing: enclosure level(0x%04x), connector name( %s)\n",
  6163. ioc->name, pcie_device->enclosure_level,
  6164. pcie_device->connector_name);
  6165. if (pcie_device->starget)
  6166. scsi_remove_target(&pcie_device->starget->dev);
  6167. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6168. "%s: exit: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  6169. pcie_device->handle, (unsigned long long)
  6170. pcie_device->wwid));
  6171. if (pcie_device->enclosure_handle != 0)
  6172. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6173. "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
  6174. ioc->name, __func__,
  6175. (unsigned long long)pcie_device->enclosure_logical_id,
  6176. pcie_device->slot));
  6177. if (pcie_device->connector_name[0] != '\0')
  6178. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6179. "%s: exit: enclosure level(0x%04x), connector name( %s)\n",
  6180. ioc->name, __func__, pcie_device->enclosure_level,
  6181. pcie_device->connector_name));
  6182. kfree(pcie_device->serial_number);
  6183. }
  6184. /**
  6185. * _scsih_pcie_check_device - checking device responsiveness
  6186. * @ioc: per adapter object
  6187. * @handle: attached device handle
  6188. *
  6189. * Returns nothing.
  6190. */
  6191. static void
  6192. _scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  6193. {
  6194. Mpi2ConfigReply_t mpi_reply;
  6195. Mpi26PCIeDevicePage0_t pcie_device_pg0;
  6196. u32 ioc_status;
  6197. struct _pcie_device *pcie_device;
  6198. u64 wwid;
  6199. unsigned long flags;
  6200. struct scsi_target *starget;
  6201. struct MPT3SAS_TARGET *sas_target_priv_data;
  6202. u32 device_info;
  6203. if ((mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
  6204. &pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle)))
  6205. return;
  6206. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  6207. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  6208. return;
  6209. /* check if this is end device */
  6210. device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
  6211. if (!(_scsih_is_nvme_device(device_info)))
  6212. return;
  6213. wwid = le64_to_cpu(pcie_device_pg0.WWID);
  6214. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  6215. pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
  6216. if (!pcie_device) {
  6217. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  6218. return;
  6219. }
  6220. if (unlikely(pcie_device->handle != handle)) {
  6221. starget = pcie_device->starget;
  6222. sas_target_priv_data = starget->hostdata;
  6223. starget_printk(KERN_INFO, starget,
  6224. "handle changed from(0x%04x) to (0x%04x)!!!\n",
  6225. pcie_device->handle, handle);
  6226. sas_target_priv_data->handle = handle;
  6227. pcie_device->handle = handle;
  6228. if (le32_to_cpu(pcie_device_pg0.Flags) &
  6229. MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
  6230. pcie_device->enclosure_level =
  6231. pcie_device_pg0.EnclosureLevel;
  6232. memcpy(&pcie_device->connector_name[0],
  6233. &pcie_device_pg0.ConnectorName[0], 4);
  6234. } else {
  6235. pcie_device->enclosure_level = 0;
  6236. pcie_device->connector_name[0] = '\0';
  6237. }
  6238. }
  6239. /* check if device is present */
  6240. if (!(le32_to_cpu(pcie_device_pg0.Flags) &
  6241. MPI26_PCIEDEV0_FLAGS_DEVICE_PRESENT)) {
  6242. pr_info(MPT3SAS_FMT
  6243. "device is not present handle(0x%04x), flags!!!\n",
  6244. ioc->name, handle);
  6245. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  6246. pcie_device_put(pcie_device);
  6247. return;
  6248. }
  6249. /* check if there were any issues with discovery */
  6250. if (_scsih_check_pcie_access_status(ioc, wwid, handle,
  6251. pcie_device_pg0.AccessStatus)) {
  6252. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  6253. pcie_device_put(pcie_device);
  6254. return;
  6255. }
  6256. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  6257. pcie_device_put(pcie_device);
  6258. _scsih_ublock_io_device(ioc, wwid);
  6259. return;
  6260. }
  6261. /**
  6262. * _scsih_pcie_add_device - creating pcie device object
  6263. * @ioc: per adapter object
  6264. * @handle: pcie device handle
  6265. *
  6266. * Creating end device object, stored in ioc->pcie_device_list.
  6267. *
  6268. * Return 1 means queue the event later, 0 means complete the event
  6269. */
  6270. static int
  6271. _scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  6272. {
  6273. Mpi26PCIeDevicePage0_t pcie_device_pg0;
  6274. Mpi26PCIeDevicePage2_t pcie_device_pg2;
  6275. Mpi2ConfigReply_t mpi_reply;
  6276. Mpi2SasEnclosurePage0_t enclosure_pg0;
  6277. struct _pcie_device *pcie_device;
  6278. u32 pcie_device_type;
  6279. u32 ioc_status;
  6280. u64 wwid;
  6281. if ((mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
  6282. &pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
  6283. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6284. ioc->name, __FILE__, __LINE__, __func__);
  6285. return 0;
  6286. }
  6287. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6288. MPI2_IOCSTATUS_MASK;
  6289. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6290. pr_err(MPT3SAS_FMT
  6291. "failure at %s:%d/%s()!\n",
  6292. ioc->name, __FILE__, __LINE__, __func__);
  6293. return 0;
  6294. }
  6295. set_bit(handle, ioc->pend_os_device_add);
  6296. wwid = le64_to_cpu(pcie_device_pg0.WWID);
  6297. /* check if device is present */
  6298. if (!(le32_to_cpu(pcie_device_pg0.Flags) &
  6299. MPI26_PCIEDEV0_FLAGS_DEVICE_PRESENT)) {
  6300. pr_err(MPT3SAS_FMT
  6301. "device is not present handle(0x04%x)!!!\n",
  6302. ioc->name, handle);
  6303. return 0;
  6304. }
  6305. /* check if there were any issues with discovery */
  6306. if (_scsih_check_pcie_access_status(ioc, wwid, handle,
  6307. pcie_device_pg0.AccessStatus))
  6308. return 0;
  6309. if (!(_scsih_is_nvme_device(le32_to_cpu(pcie_device_pg0.DeviceInfo))))
  6310. return 0;
  6311. pcie_device = mpt3sas_get_pdev_by_wwid(ioc, wwid);
  6312. if (pcie_device) {
  6313. clear_bit(handle, ioc->pend_os_device_add);
  6314. pcie_device_put(pcie_device);
  6315. return 0;
  6316. }
  6317. pcie_device = kzalloc(sizeof(struct _pcie_device), GFP_KERNEL);
  6318. if (!pcie_device) {
  6319. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6320. ioc->name, __FILE__, __LINE__, __func__);
  6321. return 0;
  6322. }
  6323. kref_init(&pcie_device->refcount);
  6324. pcie_device->id = ioc->pcie_target_id++;
  6325. pcie_device->channel = PCIE_CHANNEL;
  6326. pcie_device->handle = handle;
  6327. pcie_device->device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
  6328. pcie_device->wwid = wwid;
  6329. pcie_device->port_num = pcie_device_pg0.PortNum;
  6330. pcie_device->fast_path = (le32_to_cpu(pcie_device_pg0.Flags) &
  6331. MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
  6332. pcie_device_type = pcie_device->device_info &
  6333. MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE;
  6334. pcie_device->enclosure_handle =
  6335. le16_to_cpu(pcie_device_pg0.EnclosureHandle);
  6336. if (pcie_device->enclosure_handle != 0)
  6337. pcie_device->slot = le16_to_cpu(pcie_device_pg0.Slot);
  6338. if (le16_to_cpu(pcie_device_pg0.Flags) &
  6339. MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
  6340. pcie_device->enclosure_level = pcie_device_pg0.EnclosureLevel;
  6341. memcpy(&pcie_device->connector_name[0],
  6342. &pcie_device_pg0.ConnectorName[0], 4);
  6343. } else {
  6344. pcie_device->enclosure_level = 0;
  6345. pcie_device->connector_name[0] = '\0';
  6346. }
  6347. /* get enclosure_logical_id */
  6348. if (pcie_device->enclosure_handle &&
  6349. !(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  6350. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  6351. pcie_device->enclosure_handle)))
  6352. pcie_device->enclosure_logical_id =
  6353. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  6354. /* TODO -- Add device name once FW supports it */
  6355. if (mpt3sas_config_get_pcie_device_pg2(ioc, &mpi_reply,
  6356. &pcie_device_pg2, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)) {
  6357. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6358. ioc->name, __FILE__, __LINE__, __func__);
  6359. kfree(pcie_device);
  6360. return 0;
  6361. }
  6362. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  6363. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6364. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6365. ioc->name, __FILE__, __LINE__, __func__);
  6366. kfree(pcie_device);
  6367. return 0;
  6368. }
  6369. pcie_device->nvme_mdts =
  6370. le32_to_cpu(pcie_device_pg2.MaximumDataTransferSize);
  6371. if (ioc->wait_for_discovery_to_complete)
  6372. _scsih_pcie_device_init_add(ioc, pcie_device);
  6373. else
  6374. _scsih_pcie_device_add(ioc, pcie_device);
  6375. pcie_device_put(pcie_device);
  6376. return 0;
  6377. }
  6378. /**
  6379. * _scsih_pcie_topology_change_event_debug - debug for topology
  6380. * event
  6381. * @ioc: per adapter object
  6382. * @event_data: event data payload
  6383. * Context: user.
  6384. */
  6385. static void
  6386. _scsih_pcie_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  6387. Mpi26EventDataPCIeTopologyChangeList_t *event_data)
  6388. {
  6389. int i;
  6390. u16 handle;
  6391. u16 reason_code;
  6392. u8 port_number;
  6393. char *status_str = NULL;
  6394. u8 link_rate, prev_link_rate;
  6395. switch (event_data->SwitchStatus) {
  6396. case MPI26_EVENT_PCIE_TOPO_SS_ADDED:
  6397. status_str = "add";
  6398. break;
  6399. case MPI26_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
  6400. status_str = "remove";
  6401. break;
  6402. case MPI26_EVENT_PCIE_TOPO_SS_RESPONDING:
  6403. case 0:
  6404. status_str = "responding";
  6405. break;
  6406. case MPI26_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
  6407. status_str = "remove delay";
  6408. break;
  6409. default:
  6410. status_str = "unknown status";
  6411. break;
  6412. }
  6413. pr_info(MPT3SAS_FMT "pcie topology change: (%s)\n",
  6414. ioc->name, status_str);
  6415. pr_info("\tswitch_handle(0x%04x), enclosure_handle(0x%04x)"
  6416. "start_port(%02d), count(%d)\n",
  6417. le16_to_cpu(event_data->SwitchDevHandle),
  6418. le16_to_cpu(event_data->EnclosureHandle),
  6419. event_data->StartPortNum, event_data->NumEntries);
  6420. for (i = 0; i < event_data->NumEntries; i++) {
  6421. handle =
  6422. le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
  6423. if (!handle)
  6424. continue;
  6425. port_number = event_data->StartPortNum + i;
  6426. reason_code = event_data->PortEntry[i].PortStatus;
  6427. switch (reason_code) {
  6428. case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
  6429. status_str = "target add";
  6430. break;
  6431. case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
  6432. status_str = "target remove";
  6433. break;
  6434. case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
  6435. status_str = "delay target remove";
  6436. break;
  6437. case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
  6438. status_str = "link rate change";
  6439. break;
  6440. case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
  6441. status_str = "target responding";
  6442. break;
  6443. default:
  6444. status_str = "unknown";
  6445. break;
  6446. }
  6447. link_rate = event_data->PortEntry[i].CurrentPortInfo &
  6448. MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
  6449. prev_link_rate = event_data->PortEntry[i].PreviousPortInfo &
  6450. MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
  6451. pr_info("\tport(%02d), attached_handle(0x%04x): %s:"
  6452. " link rate: new(0x%02x), old(0x%02x)\n", port_number,
  6453. handle, status_str, link_rate, prev_link_rate);
  6454. }
  6455. }
  6456. /**
  6457. * _scsih_pcie_topology_change_event - handle PCIe topology
  6458. * changes
  6459. * @ioc: per adapter object
  6460. * @fw_event: The fw_event_work object
  6461. * Context: user.
  6462. *
  6463. */
  6464. static int
  6465. _scsih_pcie_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
  6466. struct fw_event_work *fw_event)
  6467. {
  6468. int i;
  6469. u16 handle;
  6470. u16 reason_code;
  6471. u8 link_rate, prev_link_rate;
  6472. unsigned long flags;
  6473. int rc;
  6474. int requeue_event;
  6475. Mpi26EventDataPCIeTopologyChangeList_t *event_data =
  6476. (Mpi26EventDataPCIeTopologyChangeList_t *) fw_event->event_data;
  6477. struct _pcie_device *pcie_device;
  6478. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  6479. _scsih_pcie_topology_change_event_debug(ioc, event_data);
  6480. if (ioc->shost_recovery || ioc->remove_host ||
  6481. ioc->pci_error_recovery)
  6482. return 0;
  6483. if (fw_event->ignore) {
  6484. dewtprintk(ioc, pr_info(MPT3SAS_FMT "ignoring switch event\n",
  6485. ioc->name));
  6486. return 0;
  6487. }
  6488. /* handle siblings events */
  6489. for (i = 0; i < event_data->NumEntries; i++) {
  6490. if (fw_event->ignore) {
  6491. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6492. "ignoring switch event\n", ioc->name));
  6493. return 0;
  6494. }
  6495. if (ioc->remove_host || ioc->pci_error_recovery)
  6496. return 0;
  6497. reason_code = event_data->PortEntry[i].PortStatus;
  6498. handle =
  6499. le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
  6500. if (!handle)
  6501. continue;
  6502. link_rate = event_data->PortEntry[i].CurrentPortInfo
  6503. & MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
  6504. prev_link_rate = event_data->PortEntry[i].PreviousPortInfo
  6505. & MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
  6506. switch (reason_code) {
  6507. case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
  6508. if (ioc->shost_recovery)
  6509. break;
  6510. if (link_rate == prev_link_rate)
  6511. break;
  6512. if (link_rate < MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5)
  6513. break;
  6514. _scsih_pcie_check_device(ioc, handle);
  6515. /* This code after this point handles the test case
  6516. * where a device has been added, however its returning
  6517. * BUSY for sometime. Then before the Device Missing
  6518. * Delay expires and the device becomes READY, the
  6519. * device is removed and added back.
  6520. */
  6521. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  6522. pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
  6523. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  6524. if (pcie_device) {
  6525. pcie_device_put(pcie_device);
  6526. break;
  6527. }
  6528. if (!test_bit(handle, ioc->pend_os_device_add))
  6529. break;
  6530. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6531. "handle(0x%04x) device not found: convert "
  6532. "event to a device add\n", ioc->name, handle));
  6533. event_data->PortEntry[i].PortStatus &= 0xF0;
  6534. event_data->PortEntry[i].PortStatus |=
  6535. MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED;
  6536. case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
  6537. if (ioc->shost_recovery)
  6538. break;
  6539. if (link_rate < MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5)
  6540. break;
  6541. rc = _scsih_pcie_add_device(ioc, handle);
  6542. if (!rc) {
  6543. /* mark entry vacant */
  6544. /* TODO This needs to be reviewed and fixed,
  6545. * we dont have an entry
  6546. * to make an event void like vacant
  6547. */
  6548. event_data->PortEntry[i].PortStatus |=
  6549. MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE;
  6550. }
  6551. break;
  6552. case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
  6553. _scsih_pcie_device_remove_by_handle(ioc, handle);
  6554. break;
  6555. }
  6556. }
  6557. return requeue_event;
  6558. }
  6559. /**
  6560. * _scsih_pcie_device_status_change_event_debug - debug for
  6561. * device event
  6562. * @event_data: event data payload
  6563. * Context: user.
  6564. *
  6565. * Return nothing.
  6566. */
  6567. static void
  6568. _scsih_pcie_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  6569. Mpi26EventDataPCIeDeviceStatusChange_t *event_data)
  6570. {
  6571. char *reason_str = NULL;
  6572. switch (event_data->ReasonCode) {
  6573. case MPI26_EVENT_PCIDEV_STAT_RC_SMART_DATA:
  6574. reason_str = "smart data";
  6575. break;
  6576. case MPI26_EVENT_PCIDEV_STAT_RC_UNSUPPORTED:
  6577. reason_str = "unsupported device discovered";
  6578. break;
  6579. case MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET:
  6580. reason_str = "internal device reset";
  6581. break;
  6582. case MPI26_EVENT_PCIDEV_STAT_RC_TASK_ABORT_INTERNAL:
  6583. reason_str = "internal task abort";
  6584. break;
  6585. case MPI26_EVENT_PCIDEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  6586. reason_str = "internal task abort set";
  6587. break;
  6588. case MPI26_EVENT_PCIDEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  6589. reason_str = "internal clear task set";
  6590. break;
  6591. case MPI26_EVENT_PCIDEV_STAT_RC_QUERY_TASK_INTERNAL:
  6592. reason_str = "internal query task";
  6593. break;
  6594. case MPI26_EVENT_PCIDEV_STAT_RC_DEV_INIT_FAILURE:
  6595. reason_str = "device init failure";
  6596. break;
  6597. case MPI26_EVENT_PCIDEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  6598. reason_str = "internal device reset complete";
  6599. break;
  6600. case MPI26_EVENT_PCIDEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  6601. reason_str = "internal task abort complete";
  6602. break;
  6603. case MPI26_EVENT_PCIDEV_STAT_RC_ASYNC_NOTIFICATION:
  6604. reason_str = "internal async notification";
  6605. break;
  6606. default:
  6607. reason_str = "unknown reason";
  6608. break;
  6609. }
  6610. pr_info(MPT3SAS_FMT "PCIE device status change: (%s)\n"
  6611. "\thandle(0x%04x), WWID(0x%016llx), tag(%d)",
  6612. ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
  6613. (unsigned long long)le64_to_cpu(event_data->WWID),
  6614. le16_to_cpu(event_data->TaskTag));
  6615. if (event_data->ReasonCode == MPI26_EVENT_PCIDEV_STAT_RC_SMART_DATA)
  6616. pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  6617. event_data->ASC, event_data->ASCQ);
  6618. pr_info("\n");
  6619. }
  6620. /**
  6621. * _scsih_pcie_device_status_change_event - handle device status
  6622. * change
  6623. * @ioc: per adapter object
  6624. * @fw_event: The fw_event_work object
  6625. * Context: user.
  6626. *
  6627. * Return nothing.
  6628. */
  6629. static void
  6630. _scsih_pcie_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
  6631. struct fw_event_work *fw_event)
  6632. {
  6633. struct MPT3SAS_TARGET *target_priv_data;
  6634. struct _pcie_device *pcie_device;
  6635. u64 wwid;
  6636. unsigned long flags;
  6637. Mpi26EventDataPCIeDeviceStatusChange_t *event_data =
  6638. (Mpi26EventDataPCIeDeviceStatusChange_t *)fw_event->event_data;
  6639. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  6640. _scsih_pcie_device_status_change_event_debug(ioc,
  6641. event_data);
  6642. if (event_data->ReasonCode !=
  6643. MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  6644. event_data->ReasonCode !=
  6645. MPI26_EVENT_PCIDEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
  6646. return;
  6647. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  6648. wwid = le64_to_cpu(event_data->WWID);
  6649. pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
  6650. if (!pcie_device || !pcie_device->starget)
  6651. goto out;
  6652. target_priv_data = pcie_device->starget->hostdata;
  6653. if (!target_priv_data)
  6654. goto out;
  6655. if (event_data->ReasonCode ==
  6656. MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET)
  6657. target_priv_data->tm_busy = 1;
  6658. else
  6659. target_priv_data->tm_busy = 0;
  6660. out:
  6661. if (pcie_device)
  6662. pcie_device_put(pcie_device);
  6663. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  6664. }
  6665. /**
  6666. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
  6667. * event
  6668. * @ioc: per adapter object
  6669. * @event_data: event data payload
  6670. * Context: user.
  6671. *
  6672. * Return nothing.
  6673. */
  6674. static void
  6675. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  6676. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  6677. {
  6678. char *reason_str = NULL;
  6679. switch (event_data->ReasonCode) {
  6680. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  6681. reason_str = "enclosure add";
  6682. break;
  6683. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  6684. reason_str = "enclosure remove";
  6685. break;
  6686. default:
  6687. reason_str = "unknown reason";
  6688. break;
  6689. }
  6690. pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
  6691. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  6692. " number slots(%d)\n", ioc->name, reason_str,
  6693. le16_to_cpu(event_data->EnclosureHandle),
  6694. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  6695. le16_to_cpu(event_data->StartSlot));
  6696. }
  6697. /**
  6698. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  6699. * @ioc: per adapter object
  6700. * @fw_event: The fw_event_work object
  6701. * Context: user.
  6702. *
  6703. * Return nothing.
  6704. */
  6705. static void
  6706. _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
  6707. struct fw_event_work *fw_event)
  6708. {
  6709. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  6710. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  6711. (Mpi2EventDataSasEnclDevStatusChange_t *)
  6712. fw_event->event_data);
  6713. }
  6714. /**
  6715. * _scsih_sas_broadcast_primitive_event - handle broadcast events
  6716. * @ioc: per adapter object
  6717. * @fw_event: The fw_event_work object
  6718. * Context: user.
  6719. *
  6720. * Return nothing.
  6721. */
  6722. static void
  6723. _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
  6724. struct fw_event_work *fw_event)
  6725. {
  6726. struct scsi_cmnd *scmd;
  6727. struct scsi_device *sdev;
  6728. u16 smid, handle;
  6729. u32 lun;
  6730. struct MPT3SAS_DEVICE *sas_device_priv_data;
  6731. u32 termination_count;
  6732. u32 query_count;
  6733. Mpi2SCSITaskManagementReply_t *mpi_reply;
  6734. Mpi2EventDataSasBroadcastPrimitive_t *event_data =
  6735. (Mpi2EventDataSasBroadcastPrimitive_t *)
  6736. fw_event->event_data;
  6737. u16 ioc_status;
  6738. unsigned long flags;
  6739. int r;
  6740. u8 max_retries = 0;
  6741. u8 task_abort_retries;
  6742. mutex_lock(&ioc->tm_cmds.mutex);
  6743. pr_info(MPT3SAS_FMT
  6744. "%s: enter: phy number(%d), width(%d)\n",
  6745. ioc->name, __func__, event_data->PhyNum,
  6746. event_data->PortWidth);
  6747. _scsih_block_io_all_device(ioc);
  6748. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  6749. mpi_reply = ioc->tm_cmds.reply;
  6750. broadcast_aen_retry:
  6751. /* sanity checks for retrying this loop */
  6752. if (max_retries++ == 5) {
  6753. dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
  6754. ioc->name, __func__));
  6755. goto out;
  6756. } else if (max_retries > 1)
  6757. dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
  6758. ioc->name, __func__, max_retries - 1));
  6759. termination_count = 0;
  6760. query_count = 0;
  6761. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  6762. if (ioc->shost_recovery)
  6763. goto out;
  6764. scmd = _scsih_scsi_lookup_get(ioc, smid);
  6765. if (!scmd)
  6766. continue;
  6767. sdev = scmd->device;
  6768. sas_device_priv_data = sdev->hostdata;
  6769. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  6770. continue;
  6771. /* skip hidden raid components */
  6772. if (sas_device_priv_data->sas_target->flags &
  6773. MPT_TARGET_FLAGS_RAID_COMPONENT)
  6774. continue;
  6775. /* skip volumes */
  6776. if (sas_device_priv_data->sas_target->flags &
  6777. MPT_TARGET_FLAGS_VOLUME)
  6778. continue;
  6779. handle = sas_device_priv_data->sas_target->handle;
  6780. lun = sas_device_priv_data->lun;
  6781. query_count++;
  6782. if (ioc->shost_recovery)
  6783. goto out;
  6784. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  6785. r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  6786. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
  6787. if (r == FAILED) {
  6788. sdev_printk(KERN_WARNING, sdev,
  6789. "mpt3sas_scsih_issue_tm: FAILED when sending "
  6790. "QUERY_TASK: scmd(%p)\n", scmd);
  6791. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  6792. goto broadcast_aen_retry;
  6793. }
  6794. ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
  6795. & MPI2_IOCSTATUS_MASK;
  6796. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6797. sdev_printk(KERN_WARNING, sdev,
  6798. "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
  6799. ioc_status, scmd);
  6800. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  6801. goto broadcast_aen_retry;
  6802. }
  6803. /* see if IO is still owned by IOC and target */
  6804. if (mpi_reply->ResponseCode ==
  6805. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  6806. mpi_reply->ResponseCode ==
  6807. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
  6808. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  6809. continue;
  6810. }
  6811. task_abort_retries = 0;
  6812. tm_retry:
  6813. if (task_abort_retries++ == 60) {
  6814. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6815. "%s: ABORT_TASK: giving up\n", ioc->name,
  6816. __func__));
  6817. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  6818. goto broadcast_aen_retry;
  6819. }
  6820. if (ioc->shost_recovery)
  6821. goto out_no_lock;
  6822. r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
  6823. sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
  6824. 30);
  6825. if (r == FAILED) {
  6826. sdev_printk(KERN_WARNING, sdev,
  6827. "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
  6828. "scmd(%p)\n", scmd);
  6829. goto tm_retry;
  6830. }
  6831. if (task_abort_retries > 1)
  6832. sdev_printk(KERN_WARNING, sdev,
  6833. "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
  6834. " scmd(%p)\n",
  6835. task_abort_retries - 1, scmd);
  6836. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  6837. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  6838. }
  6839. if (ioc->broadcast_aen_pending) {
  6840. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6841. "%s: loop back due to pending AEN\n",
  6842. ioc->name, __func__));
  6843. ioc->broadcast_aen_pending = 0;
  6844. goto broadcast_aen_retry;
  6845. }
  6846. out:
  6847. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  6848. out_no_lock:
  6849. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6850. "%s - exit, query_count = %d termination_count = %d\n",
  6851. ioc->name, __func__, query_count, termination_count));
  6852. ioc->broadcast_aen_busy = 0;
  6853. if (!ioc->shost_recovery)
  6854. _scsih_ublock_io_all_device(ioc);
  6855. mutex_unlock(&ioc->tm_cmds.mutex);
  6856. }
  6857. /**
  6858. * _scsih_sas_discovery_event - handle discovery events
  6859. * @ioc: per adapter object
  6860. * @fw_event: The fw_event_work object
  6861. * Context: user.
  6862. *
  6863. * Return nothing.
  6864. */
  6865. static void
  6866. _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
  6867. struct fw_event_work *fw_event)
  6868. {
  6869. Mpi2EventDataSasDiscovery_t *event_data =
  6870. (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
  6871. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  6872. pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
  6873. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  6874. "start" : "stop");
  6875. if (event_data->DiscoveryStatus)
  6876. pr_info("discovery_status(0x%08x)",
  6877. le32_to_cpu(event_data->DiscoveryStatus));
  6878. pr_info("\n");
  6879. }
  6880. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  6881. !ioc->sas_hba.num_phys) {
  6882. if (disable_discovery > 0 && ioc->shost_recovery) {
  6883. /* Wait for the reset to complete */
  6884. while (ioc->shost_recovery)
  6885. ssleep(1);
  6886. }
  6887. _scsih_sas_host_add(ioc);
  6888. }
  6889. }
  6890. /**
  6891. * _scsih_pcie_enumeration_event - handle enumeration events
  6892. * @ioc: per adapter object
  6893. * @fw_event: The fw_event_work object
  6894. * Context: user.
  6895. *
  6896. * Return nothing.
  6897. */
  6898. static void
  6899. _scsih_pcie_enumeration_event(struct MPT3SAS_ADAPTER *ioc,
  6900. struct fw_event_work *fw_event)
  6901. {
  6902. Mpi26EventDataPCIeEnumeration_t *event_data =
  6903. (Mpi26EventDataPCIeEnumeration_t *)fw_event->event_data;
  6904. if (!(ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK))
  6905. return;
  6906. pr_info(MPT3SAS_FMT "pcie enumeration event: (%s) Flag 0x%02x",
  6907. ioc->name,
  6908. (event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
  6909. "started" : "completed",
  6910. event_data->Flags);
  6911. if (event_data->EnumerationStatus)
  6912. pr_cont("enumeration_status(0x%08x)",
  6913. le32_to_cpu(event_data->EnumerationStatus));
  6914. pr_cont("\n");
  6915. }
  6916. /**
  6917. * _scsih_ir_fastpath - turn on fastpath for IR physdisk
  6918. * @ioc: per adapter object
  6919. * @handle: device handle for physical disk
  6920. * @phys_disk_num: physical disk number
  6921. *
  6922. * Return 0 for success, else failure.
  6923. */
  6924. static int
  6925. _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
  6926. {
  6927. Mpi2RaidActionRequest_t *mpi_request;
  6928. Mpi2RaidActionReply_t *mpi_reply;
  6929. u16 smid;
  6930. u8 issue_reset = 0;
  6931. int rc = 0;
  6932. u16 ioc_status;
  6933. u32 log_info;
  6934. if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
  6935. return rc;
  6936. mutex_lock(&ioc->scsih_cmds.mutex);
  6937. if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
  6938. pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
  6939. ioc->name, __func__);
  6940. rc = -EAGAIN;
  6941. goto out;
  6942. }
  6943. ioc->scsih_cmds.status = MPT3_CMD_PENDING;
  6944. smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  6945. if (!smid) {
  6946. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  6947. ioc->name, __func__);
  6948. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  6949. rc = -EAGAIN;
  6950. goto out;
  6951. }
  6952. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  6953. ioc->scsih_cmds.smid = smid;
  6954. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  6955. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  6956. mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
  6957. mpi_request->PhysDiskNum = phys_disk_num;
  6958. dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
  6959. "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
  6960. handle, phys_disk_num));
  6961. init_completion(&ioc->scsih_cmds.done);
  6962. ioc->put_smid_default(ioc, smid);
  6963. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  6964. if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
  6965. pr_err(MPT3SAS_FMT "%s: timeout\n",
  6966. ioc->name, __func__);
  6967. if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
  6968. issue_reset = 1;
  6969. rc = -EFAULT;
  6970. goto out;
  6971. }
  6972. if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
  6973. mpi_reply = ioc->scsih_cmds.reply;
  6974. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  6975. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  6976. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  6977. else
  6978. log_info = 0;
  6979. ioc_status &= MPI2_IOCSTATUS_MASK;
  6980. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6981. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6982. "IR RAID_ACTION: failed: ioc_status(0x%04x), "
  6983. "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
  6984. log_info));
  6985. rc = -EFAULT;
  6986. } else
  6987. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6988. "IR RAID_ACTION: completed successfully\n",
  6989. ioc->name));
  6990. }
  6991. out:
  6992. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  6993. mutex_unlock(&ioc->scsih_cmds.mutex);
  6994. if (issue_reset)
  6995. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  6996. return rc;
  6997. }
  6998. /**
  6999. * _scsih_reprobe_lun - reprobing lun
  7000. * @sdev: scsi device struct
  7001. * @no_uld_attach: sdev->no_uld_attach flag setting
  7002. *
  7003. **/
  7004. static void
  7005. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  7006. {
  7007. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  7008. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  7009. sdev->no_uld_attach ? "hiding" : "exposing");
  7010. WARN_ON(scsi_device_reprobe(sdev));
  7011. }
  7012. /**
  7013. * _scsih_sas_volume_add - add new volume
  7014. * @ioc: per adapter object
  7015. * @element: IR config element data
  7016. * Context: user.
  7017. *
  7018. * Return nothing.
  7019. */
  7020. static void
  7021. _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
  7022. Mpi2EventIrConfigElement_t *element)
  7023. {
  7024. struct _raid_device *raid_device;
  7025. unsigned long flags;
  7026. u64 wwid;
  7027. u16 handle = le16_to_cpu(element->VolDevHandle);
  7028. int rc;
  7029. mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
  7030. if (!wwid) {
  7031. pr_err(MPT3SAS_FMT
  7032. "failure at %s:%d/%s()!\n", ioc->name,
  7033. __FILE__, __LINE__, __func__);
  7034. return;
  7035. }
  7036. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7037. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  7038. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7039. if (raid_device)
  7040. return;
  7041. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  7042. if (!raid_device) {
  7043. pr_err(MPT3SAS_FMT
  7044. "failure at %s:%d/%s()!\n", ioc->name,
  7045. __FILE__, __LINE__, __func__);
  7046. return;
  7047. }
  7048. raid_device->id = ioc->sas_id++;
  7049. raid_device->channel = RAID_CHANNEL;
  7050. raid_device->handle = handle;
  7051. raid_device->wwid = wwid;
  7052. _scsih_raid_device_add(ioc, raid_device);
  7053. if (!ioc->wait_for_discovery_to_complete) {
  7054. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  7055. raid_device->id, 0);
  7056. if (rc)
  7057. _scsih_raid_device_remove(ioc, raid_device);
  7058. } else {
  7059. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7060. _scsih_determine_boot_device(ioc, raid_device, 1);
  7061. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7062. }
  7063. }
  7064. /**
  7065. * _scsih_sas_volume_delete - delete volume
  7066. * @ioc: per adapter object
  7067. * @handle: volume device handle
  7068. * Context: user.
  7069. *
  7070. * Return nothing.
  7071. */
  7072. static void
  7073. _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  7074. {
  7075. struct _raid_device *raid_device;
  7076. unsigned long flags;
  7077. struct MPT3SAS_TARGET *sas_target_priv_data;
  7078. struct scsi_target *starget = NULL;
  7079. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7080. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  7081. if (raid_device) {
  7082. if (raid_device->starget) {
  7083. starget = raid_device->starget;
  7084. sas_target_priv_data = starget->hostdata;
  7085. sas_target_priv_data->deleted = 1;
  7086. }
  7087. pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
  7088. ioc->name, raid_device->handle,
  7089. (unsigned long long) raid_device->wwid);
  7090. list_del(&raid_device->list);
  7091. kfree(raid_device);
  7092. }
  7093. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7094. if (starget)
  7095. scsi_remove_target(&starget->dev);
  7096. }
  7097. /**
  7098. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  7099. * @ioc: per adapter object
  7100. * @element: IR config element data
  7101. * Context: user.
  7102. *
  7103. * Return nothing.
  7104. */
  7105. static void
  7106. _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
  7107. Mpi2EventIrConfigElement_t *element)
  7108. {
  7109. struct _sas_device *sas_device;
  7110. struct scsi_target *starget = NULL;
  7111. struct MPT3SAS_TARGET *sas_target_priv_data;
  7112. unsigned long flags;
  7113. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  7114. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7115. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  7116. if (sas_device) {
  7117. sas_device->volume_handle = 0;
  7118. sas_device->volume_wwid = 0;
  7119. clear_bit(handle, ioc->pd_handles);
  7120. if (sas_device->starget && sas_device->starget->hostdata) {
  7121. starget = sas_device->starget;
  7122. sas_target_priv_data = starget->hostdata;
  7123. sas_target_priv_data->flags &=
  7124. ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  7125. }
  7126. }
  7127. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7128. if (!sas_device)
  7129. return;
  7130. /* exposing raid component */
  7131. if (starget)
  7132. starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
  7133. sas_device_put(sas_device);
  7134. }
  7135. /**
  7136. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  7137. * @ioc: per adapter object
  7138. * @element: IR config element data
  7139. * Context: user.
  7140. *
  7141. * Return nothing.
  7142. */
  7143. static void
  7144. _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
  7145. Mpi2EventIrConfigElement_t *element)
  7146. {
  7147. struct _sas_device *sas_device;
  7148. struct scsi_target *starget = NULL;
  7149. struct MPT3SAS_TARGET *sas_target_priv_data;
  7150. unsigned long flags;
  7151. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  7152. u16 volume_handle = 0;
  7153. u64 volume_wwid = 0;
  7154. mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
  7155. if (volume_handle)
  7156. mpt3sas_config_get_volume_wwid(ioc, volume_handle,
  7157. &volume_wwid);
  7158. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7159. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  7160. if (sas_device) {
  7161. set_bit(handle, ioc->pd_handles);
  7162. if (sas_device->starget && sas_device->starget->hostdata) {
  7163. starget = sas_device->starget;
  7164. sas_target_priv_data = starget->hostdata;
  7165. sas_target_priv_data->flags |=
  7166. MPT_TARGET_FLAGS_RAID_COMPONENT;
  7167. sas_device->volume_handle = volume_handle;
  7168. sas_device->volume_wwid = volume_wwid;
  7169. }
  7170. }
  7171. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7172. if (!sas_device)
  7173. return;
  7174. /* hiding raid component */
  7175. _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
  7176. if (starget)
  7177. starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
  7178. sas_device_put(sas_device);
  7179. }
  7180. /**
  7181. * _scsih_sas_pd_delete - delete pd component
  7182. * @ioc: per adapter object
  7183. * @element: IR config element data
  7184. * Context: user.
  7185. *
  7186. * Return nothing.
  7187. */
  7188. static void
  7189. _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
  7190. Mpi2EventIrConfigElement_t *element)
  7191. {
  7192. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  7193. _scsih_device_remove_by_handle(ioc, handle);
  7194. }
  7195. /**
  7196. * _scsih_sas_pd_add - remove pd component
  7197. * @ioc: per adapter object
  7198. * @element: IR config element data
  7199. * Context: user.
  7200. *
  7201. * Return nothing.
  7202. */
  7203. static void
  7204. _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
  7205. Mpi2EventIrConfigElement_t *element)
  7206. {
  7207. struct _sas_device *sas_device;
  7208. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  7209. Mpi2ConfigReply_t mpi_reply;
  7210. Mpi2SasDevicePage0_t sas_device_pg0;
  7211. u32 ioc_status;
  7212. u64 sas_address;
  7213. u16 parent_handle;
  7214. set_bit(handle, ioc->pd_handles);
  7215. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  7216. if (sas_device) {
  7217. _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
  7218. sas_device_put(sas_device);
  7219. return;
  7220. }
  7221. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  7222. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  7223. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7224. ioc->name, __FILE__, __LINE__, __func__);
  7225. return;
  7226. }
  7227. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  7228. MPI2_IOCSTATUS_MASK;
  7229. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  7230. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7231. ioc->name, __FILE__, __LINE__, __func__);
  7232. return;
  7233. }
  7234. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  7235. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  7236. mpt3sas_transport_update_links(ioc, sas_address, handle,
  7237. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  7238. _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
  7239. _scsih_add_device(ioc, handle, 0, 1);
  7240. }
  7241. /**
  7242. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  7243. * @ioc: per adapter object
  7244. * @event_data: event data payload
  7245. * Context: user.
  7246. *
  7247. * Return nothing.
  7248. */
  7249. static void
  7250. _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  7251. Mpi2EventDataIrConfigChangeList_t *event_data)
  7252. {
  7253. Mpi2EventIrConfigElement_t *element;
  7254. u8 element_type;
  7255. int i;
  7256. char *reason_str = NULL, *element_str = NULL;
  7257. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  7258. pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
  7259. ioc->name, (le32_to_cpu(event_data->Flags) &
  7260. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  7261. "foreign" : "native", event_data->NumElements);
  7262. for (i = 0; i < event_data->NumElements; i++, element++) {
  7263. switch (element->ReasonCode) {
  7264. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  7265. reason_str = "add";
  7266. break;
  7267. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  7268. reason_str = "remove";
  7269. break;
  7270. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  7271. reason_str = "no change";
  7272. break;
  7273. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  7274. reason_str = "hide";
  7275. break;
  7276. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  7277. reason_str = "unhide";
  7278. break;
  7279. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  7280. reason_str = "volume_created";
  7281. break;
  7282. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  7283. reason_str = "volume_deleted";
  7284. break;
  7285. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  7286. reason_str = "pd_created";
  7287. break;
  7288. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  7289. reason_str = "pd_deleted";
  7290. break;
  7291. default:
  7292. reason_str = "unknown reason";
  7293. break;
  7294. }
  7295. element_type = le16_to_cpu(element->ElementFlags) &
  7296. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  7297. switch (element_type) {
  7298. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  7299. element_str = "volume";
  7300. break;
  7301. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  7302. element_str = "phys disk";
  7303. break;
  7304. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  7305. element_str = "hot spare";
  7306. break;
  7307. default:
  7308. element_str = "unknown element";
  7309. break;
  7310. }
  7311. pr_info("\t(%s:%s), vol handle(0x%04x), " \
  7312. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  7313. reason_str, le16_to_cpu(element->VolDevHandle),
  7314. le16_to_cpu(element->PhysDiskDevHandle),
  7315. element->PhysDiskNum);
  7316. }
  7317. }
  7318. /**
  7319. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  7320. * @ioc: per adapter object
  7321. * @fw_event: The fw_event_work object
  7322. * Context: user.
  7323. *
  7324. * Return nothing.
  7325. */
  7326. static void
  7327. _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
  7328. struct fw_event_work *fw_event)
  7329. {
  7330. Mpi2EventIrConfigElement_t *element;
  7331. int i;
  7332. u8 foreign_config;
  7333. Mpi2EventDataIrConfigChangeList_t *event_data =
  7334. (Mpi2EventDataIrConfigChangeList_t *)
  7335. fw_event->event_data;
  7336. if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
  7337. (!ioc->hide_ir_msg))
  7338. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  7339. foreign_config = (le32_to_cpu(event_data->Flags) &
  7340. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  7341. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  7342. if (ioc->shost_recovery &&
  7343. ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  7344. for (i = 0; i < event_data->NumElements; i++, element++) {
  7345. if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
  7346. _scsih_ir_fastpath(ioc,
  7347. le16_to_cpu(element->PhysDiskDevHandle),
  7348. element->PhysDiskNum);
  7349. }
  7350. return;
  7351. }
  7352. for (i = 0; i < event_data->NumElements; i++, element++) {
  7353. switch (element->ReasonCode) {
  7354. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  7355. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  7356. if (!foreign_config)
  7357. _scsih_sas_volume_add(ioc, element);
  7358. break;
  7359. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  7360. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  7361. if (!foreign_config)
  7362. _scsih_sas_volume_delete(ioc,
  7363. le16_to_cpu(element->VolDevHandle));
  7364. break;
  7365. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  7366. if (!ioc->is_warpdrive)
  7367. _scsih_sas_pd_hide(ioc, element);
  7368. break;
  7369. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  7370. if (!ioc->is_warpdrive)
  7371. _scsih_sas_pd_expose(ioc, element);
  7372. break;
  7373. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  7374. if (!ioc->is_warpdrive)
  7375. _scsih_sas_pd_add(ioc, element);
  7376. break;
  7377. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  7378. if (!ioc->is_warpdrive)
  7379. _scsih_sas_pd_delete(ioc, element);
  7380. break;
  7381. }
  7382. }
  7383. }
  7384. /**
  7385. * _scsih_sas_ir_volume_event - IR volume event
  7386. * @ioc: per adapter object
  7387. * @fw_event: The fw_event_work object
  7388. * Context: user.
  7389. *
  7390. * Return nothing.
  7391. */
  7392. static void
  7393. _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
  7394. struct fw_event_work *fw_event)
  7395. {
  7396. u64 wwid;
  7397. unsigned long flags;
  7398. struct _raid_device *raid_device;
  7399. u16 handle;
  7400. u32 state;
  7401. int rc;
  7402. Mpi2EventDataIrVolume_t *event_data =
  7403. (Mpi2EventDataIrVolume_t *) fw_event->event_data;
  7404. if (ioc->shost_recovery)
  7405. return;
  7406. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  7407. return;
  7408. handle = le16_to_cpu(event_data->VolDevHandle);
  7409. state = le32_to_cpu(event_data->NewValue);
  7410. if (!ioc->hide_ir_msg)
  7411. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  7412. "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
  7413. ioc->name, __func__, handle,
  7414. le32_to_cpu(event_data->PreviousValue), state));
  7415. switch (state) {
  7416. case MPI2_RAID_VOL_STATE_MISSING:
  7417. case MPI2_RAID_VOL_STATE_FAILED:
  7418. _scsih_sas_volume_delete(ioc, handle);
  7419. break;
  7420. case MPI2_RAID_VOL_STATE_ONLINE:
  7421. case MPI2_RAID_VOL_STATE_DEGRADED:
  7422. case MPI2_RAID_VOL_STATE_OPTIMAL:
  7423. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7424. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  7425. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7426. if (raid_device)
  7427. break;
  7428. mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
  7429. if (!wwid) {
  7430. pr_err(MPT3SAS_FMT
  7431. "failure at %s:%d/%s()!\n", ioc->name,
  7432. __FILE__, __LINE__, __func__);
  7433. break;
  7434. }
  7435. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  7436. if (!raid_device) {
  7437. pr_err(MPT3SAS_FMT
  7438. "failure at %s:%d/%s()!\n", ioc->name,
  7439. __FILE__, __LINE__, __func__);
  7440. break;
  7441. }
  7442. raid_device->id = ioc->sas_id++;
  7443. raid_device->channel = RAID_CHANNEL;
  7444. raid_device->handle = handle;
  7445. raid_device->wwid = wwid;
  7446. _scsih_raid_device_add(ioc, raid_device);
  7447. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  7448. raid_device->id, 0);
  7449. if (rc)
  7450. _scsih_raid_device_remove(ioc, raid_device);
  7451. break;
  7452. case MPI2_RAID_VOL_STATE_INITIALIZING:
  7453. default:
  7454. break;
  7455. }
  7456. }
  7457. /**
  7458. * _scsih_sas_ir_physical_disk_event - PD event
  7459. * @ioc: per adapter object
  7460. * @fw_event: The fw_event_work object
  7461. * Context: user.
  7462. *
  7463. * Return nothing.
  7464. */
  7465. static void
  7466. _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
  7467. struct fw_event_work *fw_event)
  7468. {
  7469. u16 handle, parent_handle;
  7470. u32 state;
  7471. struct _sas_device *sas_device;
  7472. Mpi2ConfigReply_t mpi_reply;
  7473. Mpi2SasDevicePage0_t sas_device_pg0;
  7474. u32 ioc_status;
  7475. Mpi2EventDataIrPhysicalDisk_t *event_data =
  7476. (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
  7477. u64 sas_address;
  7478. if (ioc->shost_recovery)
  7479. return;
  7480. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  7481. return;
  7482. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  7483. state = le32_to_cpu(event_data->NewValue);
  7484. if (!ioc->hide_ir_msg)
  7485. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  7486. "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
  7487. ioc->name, __func__, handle,
  7488. le32_to_cpu(event_data->PreviousValue), state));
  7489. switch (state) {
  7490. case MPI2_RAID_PD_STATE_ONLINE:
  7491. case MPI2_RAID_PD_STATE_DEGRADED:
  7492. case MPI2_RAID_PD_STATE_REBUILDING:
  7493. case MPI2_RAID_PD_STATE_OPTIMAL:
  7494. case MPI2_RAID_PD_STATE_HOT_SPARE:
  7495. if (!ioc->is_warpdrive)
  7496. set_bit(handle, ioc->pd_handles);
  7497. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  7498. if (sas_device) {
  7499. sas_device_put(sas_device);
  7500. return;
  7501. }
  7502. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  7503. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  7504. handle))) {
  7505. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7506. ioc->name, __FILE__, __LINE__, __func__);
  7507. return;
  7508. }
  7509. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  7510. MPI2_IOCSTATUS_MASK;
  7511. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  7512. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7513. ioc->name, __FILE__, __LINE__, __func__);
  7514. return;
  7515. }
  7516. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  7517. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  7518. mpt3sas_transport_update_links(ioc, sas_address, handle,
  7519. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  7520. _scsih_add_device(ioc, handle, 0, 1);
  7521. break;
  7522. case MPI2_RAID_PD_STATE_OFFLINE:
  7523. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  7524. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  7525. default:
  7526. break;
  7527. }
  7528. }
  7529. /**
  7530. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  7531. * @ioc: per adapter object
  7532. * @event_data: event data payload
  7533. * Context: user.
  7534. *
  7535. * Return nothing.
  7536. */
  7537. static void
  7538. _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
  7539. Mpi2EventDataIrOperationStatus_t *event_data)
  7540. {
  7541. char *reason_str = NULL;
  7542. switch (event_data->RAIDOperation) {
  7543. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  7544. reason_str = "resync";
  7545. break;
  7546. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  7547. reason_str = "online capacity expansion";
  7548. break;
  7549. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  7550. reason_str = "consistency check";
  7551. break;
  7552. case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
  7553. reason_str = "background init";
  7554. break;
  7555. case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
  7556. reason_str = "make data consistent";
  7557. break;
  7558. }
  7559. if (!reason_str)
  7560. return;
  7561. pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
  7562. "\thandle(0x%04x), percent complete(%d)\n",
  7563. ioc->name, reason_str,
  7564. le16_to_cpu(event_data->VolDevHandle),
  7565. event_data->PercentComplete);
  7566. }
  7567. /**
  7568. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  7569. * @ioc: per adapter object
  7570. * @fw_event: The fw_event_work object
  7571. * Context: user.
  7572. *
  7573. * Return nothing.
  7574. */
  7575. static void
  7576. _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
  7577. struct fw_event_work *fw_event)
  7578. {
  7579. Mpi2EventDataIrOperationStatus_t *event_data =
  7580. (Mpi2EventDataIrOperationStatus_t *)
  7581. fw_event->event_data;
  7582. static struct _raid_device *raid_device;
  7583. unsigned long flags;
  7584. u16 handle;
  7585. if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
  7586. (!ioc->hide_ir_msg))
  7587. _scsih_sas_ir_operation_status_event_debug(ioc,
  7588. event_data);
  7589. /* code added for raid transport support */
  7590. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
  7591. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7592. handle = le16_to_cpu(event_data->VolDevHandle);
  7593. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  7594. if (raid_device)
  7595. raid_device->percent_complete =
  7596. event_data->PercentComplete;
  7597. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7598. }
  7599. }
  7600. /**
  7601. * _scsih_prep_device_scan - initialize parameters prior to device scan
  7602. * @ioc: per adapter object
  7603. *
  7604. * Set the deleted flag prior to device scan. If the device is found during
  7605. * the scan, then we clear the deleted flag.
  7606. */
  7607. static void
  7608. _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
  7609. {
  7610. struct MPT3SAS_DEVICE *sas_device_priv_data;
  7611. struct scsi_device *sdev;
  7612. shost_for_each_device(sdev, ioc->shost) {
  7613. sas_device_priv_data = sdev->hostdata;
  7614. if (sas_device_priv_data && sas_device_priv_data->sas_target)
  7615. sas_device_priv_data->sas_target->deleted = 1;
  7616. }
  7617. }
  7618. /**
  7619. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  7620. * @ioc: per adapter object
  7621. * @sas_device_pg0: SAS Device page 0
  7622. *
  7623. * After host reset, find out whether devices are still responding.
  7624. * Used in _scsih_remove_unresponsive_sas_devices.
  7625. *
  7626. * Return nothing.
  7627. */
  7628. static void
  7629. _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
  7630. Mpi2SasDevicePage0_t *sas_device_pg0)
  7631. {
  7632. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  7633. struct scsi_target *starget;
  7634. struct _sas_device *sas_device = NULL;
  7635. unsigned long flags;
  7636. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7637. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  7638. if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
  7639. (sas_device->slot == sas_device_pg0->Slot)) {
  7640. sas_device->responding = 1;
  7641. starget = sas_device->starget;
  7642. if (starget && starget->hostdata) {
  7643. sas_target_priv_data = starget->hostdata;
  7644. sas_target_priv_data->tm_busy = 0;
  7645. sas_target_priv_data->deleted = 0;
  7646. } else
  7647. sas_target_priv_data = NULL;
  7648. if (starget) {
  7649. starget_printk(KERN_INFO, starget,
  7650. "handle(0x%04x), sas_addr(0x%016llx)\n",
  7651. sas_device_pg0->DevHandle,
  7652. (unsigned long long)
  7653. sas_device->sas_address);
  7654. if (sas_device->enclosure_handle != 0)
  7655. starget_printk(KERN_INFO, starget,
  7656. "enclosure logical id(0x%016llx),"
  7657. " slot(%d)\n",
  7658. (unsigned long long)
  7659. sas_device->enclosure_logical_id,
  7660. sas_device->slot);
  7661. }
  7662. if (sas_device_pg0->Flags &
  7663. MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
  7664. sas_device->enclosure_level =
  7665. sas_device_pg0->EnclosureLevel;
  7666. memcpy(&sas_device->connector_name[0],
  7667. &sas_device_pg0->ConnectorName[0], 4);
  7668. } else {
  7669. sas_device->enclosure_level = 0;
  7670. sas_device->connector_name[0] = '\0';
  7671. }
  7672. _scsih_get_enclosure_logicalid_chassis_slot(ioc,
  7673. sas_device_pg0, sas_device);
  7674. if (sas_device->handle == sas_device_pg0->DevHandle)
  7675. goto out;
  7676. pr_info("\thandle changed from(0x%04x)!!!\n",
  7677. sas_device->handle);
  7678. sas_device->handle = sas_device_pg0->DevHandle;
  7679. if (sas_target_priv_data)
  7680. sas_target_priv_data->handle =
  7681. sas_device_pg0->DevHandle;
  7682. goto out;
  7683. }
  7684. }
  7685. out:
  7686. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7687. }
  7688. /**
  7689. * _scsih_search_responding_sas_devices -
  7690. * @ioc: per adapter object
  7691. *
  7692. * After host reset, find out whether devices are still responding.
  7693. * If not remove.
  7694. *
  7695. * Return nothing.
  7696. */
  7697. static void
  7698. _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
  7699. {
  7700. Mpi2SasDevicePage0_t sas_device_pg0;
  7701. Mpi2ConfigReply_t mpi_reply;
  7702. u16 ioc_status;
  7703. u16 handle;
  7704. u32 device_info;
  7705. pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
  7706. if (list_empty(&ioc->sas_device_list))
  7707. goto out;
  7708. handle = 0xFFFF;
  7709. while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  7710. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  7711. handle))) {
  7712. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  7713. MPI2_IOCSTATUS_MASK;
  7714. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  7715. break;
  7716. handle = sas_device_pg0.DevHandle =
  7717. le16_to_cpu(sas_device_pg0.DevHandle);
  7718. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  7719. if (!(_scsih_is_end_device(device_info)))
  7720. continue;
  7721. sas_device_pg0.SASAddress =
  7722. le64_to_cpu(sas_device_pg0.SASAddress);
  7723. sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
  7724. sas_device_pg0.Flags = le16_to_cpu(sas_device_pg0.Flags);
  7725. _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
  7726. }
  7727. out:
  7728. pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
  7729. ioc->name);
  7730. }
  7731. /**
  7732. * _scsih_mark_responding_pcie_device - mark a pcie_device as responding
  7733. * @ioc: per adapter object
  7734. * @pcie_device_pg0: PCIe Device page 0
  7735. *
  7736. * After host reset, find out whether devices are still responding.
  7737. * Used in _scsih_remove_unresponding_devices.
  7738. *
  7739. * Return nothing.
  7740. */
  7741. static void
  7742. _scsih_mark_responding_pcie_device(struct MPT3SAS_ADAPTER *ioc,
  7743. Mpi26PCIeDevicePage0_t *pcie_device_pg0)
  7744. {
  7745. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  7746. struct scsi_target *starget;
  7747. struct _pcie_device *pcie_device;
  7748. unsigned long flags;
  7749. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  7750. list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
  7751. if ((pcie_device->wwid == pcie_device_pg0->WWID) &&
  7752. (pcie_device->slot == pcie_device_pg0->Slot)) {
  7753. pcie_device->responding = 1;
  7754. starget = pcie_device->starget;
  7755. if (starget && starget->hostdata) {
  7756. sas_target_priv_data = starget->hostdata;
  7757. sas_target_priv_data->tm_busy = 0;
  7758. sas_target_priv_data->deleted = 0;
  7759. } else
  7760. sas_target_priv_data = NULL;
  7761. if (starget) {
  7762. starget_printk(KERN_INFO, starget,
  7763. "handle(0x%04x), wwid(0x%016llx) ",
  7764. pcie_device->handle,
  7765. (unsigned long long)pcie_device->wwid);
  7766. if (pcie_device->enclosure_handle != 0)
  7767. starget_printk(KERN_INFO, starget,
  7768. "enclosure logical id(0x%016llx), "
  7769. "slot(%d)\n",
  7770. (unsigned long long)
  7771. pcie_device->enclosure_logical_id,
  7772. pcie_device->slot);
  7773. }
  7774. if (((le32_to_cpu(pcie_device_pg0->Flags)) &
  7775. MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) &&
  7776. (ioc->hba_mpi_version_belonged != MPI2_VERSION)) {
  7777. pcie_device->enclosure_level =
  7778. pcie_device_pg0->EnclosureLevel;
  7779. memcpy(&pcie_device->connector_name[0],
  7780. &pcie_device_pg0->ConnectorName[0], 4);
  7781. } else {
  7782. pcie_device->enclosure_level = 0;
  7783. pcie_device->connector_name[0] = '\0';
  7784. }
  7785. if (pcie_device->handle == pcie_device_pg0->DevHandle)
  7786. goto out;
  7787. pr_info("\thandle changed from(0x%04x)!!!\n",
  7788. pcie_device->handle);
  7789. pcie_device->handle = pcie_device_pg0->DevHandle;
  7790. if (sas_target_priv_data)
  7791. sas_target_priv_data->handle =
  7792. pcie_device_pg0->DevHandle;
  7793. goto out;
  7794. }
  7795. }
  7796. out:
  7797. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  7798. }
  7799. /**
  7800. * _scsih_search_responding_pcie_devices -
  7801. * @ioc: per adapter object
  7802. *
  7803. * After host reset, find out whether devices are still responding.
  7804. * If not remove.
  7805. *
  7806. * Return nothing.
  7807. */
  7808. static void
  7809. _scsih_search_responding_pcie_devices(struct MPT3SAS_ADAPTER *ioc)
  7810. {
  7811. Mpi26PCIeDevicePage0_t pcie_device_pg0;
  7812. Mpi2ConfigReply_t mpi_reply;
  7813. u16 ioc_status;
  7814. u16 handle;
  7815. u32 device_info;
  7816. pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
  7817. if (list_empty(&ioc->pcie_device_list))
  7818. goto out;
  7819. handle = 0xFFFF;
  7820. while (!(mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
  7821. &pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  7822. handle))) {
  7823. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  7824. MPI2_IOCSTATUS_MASK;
  7825. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  7826. pr_info(MPT3SAS_FMT "\tbreak from %s: "
  7827. "ioc_status(0x%04x), loginfo(0x%08x)\n", ioc->name,
  7828. __func__, ioc_status,
  7829. le32_to_cpu(mpi_reply.IOCLogInfo));
  7830. break;
  7831. }
  7832. handle = le16_to_cpu(pcie_device_pg0.DevHandle);
  7833. device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
  7834. if (!(_scsih_is_nvme_device(device_info)))
  7835. continue;
  7836. pcie_device_pg0.WWID = le64_to_cpu(pcie_device_pg0.WWID),
  7837. pcie_device_pg0.Slot = le16_to_cpu(pcie_device_pg0.Slot);
  7838. pcie_device_pg0.Flags = le32_to_cpu(pcie_device_pg0.Flags);
  7839. pcie_device_pg0.DevHandle = handle;
  7840. _scsih_mark_responding_pcie_device(ioc, &pcie_device_pg0);
  7841. }
  7842. out:
  7843. pr_info(MPT3SAS_FMT "search for PCIe end-devices: complete\n",
  7844. ioc->name);
  7845. }
  7846. /**
  7847. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  7848. * @ioc: per adapter object
  7849. * @wwid: world wide identifier for raid volume
  7850. * @handle: device handle
  7851. *
  7852. * After host reset, find out whether devices are still responding.
  7853. * Used in _scsih_remove_unresponsive_raid_devices.
  7854. *
  7855. * Return nothing.
  7856. */
  7857. static void
  7858. _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
  7859. u16 handle)
  7860. {
  7861. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  7862. struct scsi_target *starget;
  7863. struct _raid_device *raid_device;
  7864. unsigned long flags;
  7865. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7866. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  7867. if (raid_device->wwid == wwid && raid_device->starget) {
  7868. starget = raid_device->starget;
  7869. if (starget && starget->hostdata) {
  7870. sas_target_priv_data = starget->hostdata;
  7871. sas_target_priv_data->deleted = 0;
  7872. } else
  7873. sas_target_priv_data = NULL;
  7874. raid_device->responding = 1;
  7875. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7876. starget_printk(KERN_INFO, raid_device->starget,
  7877. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  7878. (unsigned long long)raid_device->wwid);
  7879. /*
  7880. * WARPDRIVE: The handles of the PDs might have changed
  7881. * across the host reset so re-initialize the
  7882. * required data for Direct IO
  7883. */
  7884. mpt3sas_init_warpdrive_properties(ioc, raid_device);
  7885. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  7886. if (raid_device->handle == handle) {
  7887. spin_unlock_irqrestore(&ioc->raid_device_lock,
  7888. flags);
  7889. return;
  7890. }
  7891. pr_info("\thandle changed from(0x%04x)!!!\n",
  7892. raid_device->handle);
  7893. raid_device->handle = handle;
  7894. if (sas_target_priv_data)
  7895. sas_target_priv_data->handle = handle;
  7896. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7897. return;
  7898. }
  7899. }
  7900. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  7901. }
  7902. /**
  7903. * _scsih_search_responding_raid_devices -
  7904. * @ioc: per adapter object
  7905. *
  7906. * After host reset, find out whether devices are still responding.
  7907. * If not remove.
  7908. *
  7909. * Return nothing.
  7910. */
  7911. static void
  7912. _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
  7913. {
  7914. Mpi2RaidVolPage1_t volume_pg1;
  7915. Mpi2RaidVolPage0_t volume_pg0;
  7916. Mpi2RaidPhysDiskPage0_t pd_pg0;
  7917. Mpi2ConfigReply_t mpi_reply;
  7918. u16 ioc_status;
  7919. u16 handle;
  7920. u8 phys_disk_num;
  7921. if (!ioc->ir_firmware)
  7922. return;
  7923. pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
  7924. ioc->name);
  7925. if (list_empty(&ioc->raid_device_list))
  7926. goto out;
  7927. handle = 0xFFFF;
  7928. while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  7929. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  7930. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  7931. MPI2_IOCSTATUS_MASK;
  7932. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  7933. break;
  7934. handle = le16_to_cpu(volume_pg1.DevHandle);
  7935. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  7936. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  7937. sizeof(Mpi2RaidVolPage0_t)))
  7938. continue;
  7939. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  7940. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  7941. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
  7942. _scsih_mark_responding_raid_device(ioc,
  7943. le64_to_cpu(volume_pg1.WWID), handle);
  7944. }
  7945. /* refresh the pd_handles */
  7946. if (!ioc->is_warpdrive) {
  7947. phys_disk_num = 0xFF;
  7948. memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
  7949. while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  7950. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  7951. phys_disk_num))) {
  7952. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  7953. MPI2_IOCSTATUS_MASK;
  7954. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  7955. break;
  7956. phys_disk_num = pd_pg0.PhysDiskNum;
  7957. handle = le16_to_cpu(pd_pg0.DevHandle);
  7958. set_bit(handle, ioc->pd_handles);
  7959. }
  7960. }
  7961. out:
  7962. pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
  7963. ioc->name);
  7964. }
  7965. /**
  7966. * _scsih_mark_responding_expander - mark a expander as responding
  7967. * @ioc: per adapter object
  7968. * @expander_pg0:SAS Expander Config Page0
  7969. *
  7970. * After host reset, find out whether devices are still responding.
  7971. * Used in _scsih_remove_unresponsive_expanders.
  7972. *
  7973. * Return nothing.
  7974. */
  7975. static void
  7976. _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc,
  7977. Mpi2ExpanderPage0_t *expander_pg0)
  7978. {
  7979. struct _sas_node *sas_expander = NULL;
  7980. unsigned long flags;
  7981. int i, encl_pg0_rc = -1;
  7982. Mpi2ConfigReply_t mpi_reply;
  7983. Mpi2SasEnclosurePage0_t enclosure_pg0;
  7984. u16 handle = le16_to_cpu(expander_pg0->DevHandle);
  7985. u64 sas_address = le64_to_cpu(expander_pg0->SASAddress);
  7986. if (le16_to_cpu(expander_pg0->EnclosureHandle)) {
  7987. encl_pg0_rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  7988. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  7989. le16_to_cpu(expander_pg0->EnclosureHandle));
  7990. if (encl_pg0_rc)
  7991. pr_info(MPT3SAS_FMT
  7992. "Enclosure Pg0 read failed for handle(0x%04x)\n",
  7993. ioc->name,
  7994. le16_to_cpu(expander_pg0->EnclosureHandle));
  7995. }
  7996. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  7997. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  7998. if (sas_expander->sas_address != sas_address)
  7999. continue;
  8000. sas_expander->responding = 1;
  8001. if (!encl_pg0_rc)
  8002. sas_expander->enclosure_logical_id =
  8003. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  8004. sas_expander->enclosure_handle =
  8005. le16_to_cpu(expander_pg0->EnclosureHandle);
  8006. if (sas_expander->handle == handle)
  8007. goto out;
  8008. pr_info("\texpander(0x%016llx): handle changed" \
  8009. " from(0x%04x) to (0x%04x)!!!\n",
  8010. (unsigned long long)sas_expander->sas_address,
  8011. sas_expander->handle, handle);
  8012. sas_expander->handle = handle;
  8013. for (i = 0 ; i < sas_expander->num_phys ; i++)
  8014. sas_expander->phy[i].handle = handle;
  8015. goto out;
  8016. }
  8017. out:
  8018. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  8019. }
  8020. /**
  8021. * _scsih_search_responding_expanders -
  8022. * @ioc: per adapter object
  8023. *
  8024. * After host reset, find out whether devices are still responding.
  8025. * If not remove.
  8026. *
  8027. * Return nothing.
  8028. */
  8029. static void
  8030. _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
  8031. {
  8032. Mpi2ExpanderPage0_t expander_pg0;
  8033. Mpi2ConfigReply_t mpi_reply;
  8034. u16 ioc_status;
  8035. u64 sas_address;
  8036. u16 handle;
  8037. pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
  8038. if (list_empty(&ioc->sas_expander_list))
  8039. goto out;
  8040. handle = 0xFFFF;
  8041. while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  8042. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  8043. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8044. MPI2_IOCSTATUS_MASK;
  8045. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  8046. break;
  8047. handle = le16_to_cpu(expander_pg0.DevHandle);
  8048. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  8049. pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
  8050. handle,
  8051. (unsigned long long)sas_address);
  8052. _scsih_mark_responding_expander(ioc, &expander_pg0);
  8053. }
  8054. out:
  8055. pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
  8056. }
  8057. /**
  8058. * _scsih_remove_unresponding_devices - removing unresponding devices
  8059. * @ioc: per adapter object
  8060. *
  8061. * Return nothing.
  8062. */
  8063. static void
  8064. _scsih_remove_unresponding_devices(struct MPT3SAS_ADAPTER *ioc)
  8065. {
  8066. struct _sas_device *sas_device, *sas_device_next;
  8067. struct _sas_node *sas_expander, *sas_expander_next;
  8068. struct _raid_device *raid_device, *raid_device_next;
  8069. struct _pcie_device *pcie_device, *pcie_device_next;
  8070. struct list_head tmp_list;
  8071. unsigned long flags;
  8072. LIST_HEAD(head);
  8073. pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
  8074. ioc->name);
  8075. /* removing unresponding end devices */
  8076. pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
  8077. ioc->name);
  8078. /*
  8079. * Iterate, pulling off devices marked as non-responding. We become the
  8080. * owner for the reference the list had on any object we prune.
  8081. */
  8082. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  8083. list_for_each_entry_safe(sas_device, sas_device_next,
  8084. &ioc->sas_device_list, list) {
  8085. if (!sas_device->responding)
  8086. list_move_tail(&sas_device->list, &head);
  8087. else
  8088. sas_device->responding = 0;
  8089. }
  8090. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  8091. /*
  8092. * Now, uninitialize and remove the unresponding devices we pruned.
  8093. */
  8094. list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
  8095. _scsih_remove_device(ioc, sas_device);
  8096. list_del_init(&sas_device->list);
  8097. sas_device_put(sas_device);
  8098. }
  8099. pr_info(MPT3SAS_FMT
  8100. " Removing unresponding devices: pcie end-devices\n"
  8101. , ioc->name);
  8102. INIT_LIST_HEAD(&head);
  8103. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  8104. list_for_each_entry_safe(pcie_device, pcie_device_next,
  8105. &ioc->pcie_device_list, list) {
  8106. if (!pcie_device->responding)
  8107. list_move_tail(&pcie_device->list, &head);
  8108. else
  8109. pcie_device->responding = 0;
  8110. }
  8111. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  8112. list_for_each_entry_safe(pcie_device, pcie_device_next, &head, list) {
  8113. _scsih_pcie_device_remove_from_sml(ioc, pcie_device);
  8114. list_del_init(&pcie_device->list);
  8115. pcie_device_put(pcie_device);
  8116. }
  8117. /* removing unresponding volumes */
  8118. if (ioc->ir_firmware) {
  8119. pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
  8120. ioc->name);
  8121. list_for_each_entry_safe(raid_device, raid_device_next,
  8122. &ioc->raid_device_list, list) {
  8123. if (!raid_device->responding)
  8124. _scsih_sas_volume_delete(ioc,
  8125. raid_device->handle);
  8126. else
  8127. raid_device->responding = 0;
  8128. }
  8129. }
  8130. /* removing unresponding expanders */
  8131. pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
  8132. ioc->name);
  8133. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  8134. INIT_LIST_HEAD(&tmp_list);
  8135. list_for_each_entry_safe(sas_expander, sas_expander_next,
  8136. &ioc->sas_expander_list, list) {
  8137. if (!sas_expander->responding)
  8138. list_move_tail(&sas_expander->list, &tmp_list);
  8139. else
  8140. sas_expander->responding = 0;
  8141. }
  8142. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  8143. list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
  8144. list) {
  8145. _scsih_expander_node_remove(ioc, sas_expander);
  8146. }
  8147. pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
  8148. ioc->name);
  8149. /* unblock devices */
  8150. _scsih_ublock_io_all_device(ioc);
  8151. }
  8152. static void
  8153. _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
  8154. struct _sas_node *sas_expander, u16 handle)
  8155. {
  8156. Mpi2ExpanderPage1_t expander_pg1;
  8157. Mpi2ConfigReply_t mpi_reply;
  8158. int i;
  8159. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  8160. if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
  8161. &expander_pg1, i, handle))) {
  8162. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  8163. ioc->name, __FILE__, __LINE__, __func__);
  8164. return;
  8165. }
  8166. mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
  8167. le16_to_cpu(expander_pg1.AttachedDevHandle), i,
  8168. expander_pg1.NegotiatedLinkRate >> 4);
  8169. }
  8170. }
  8171. /**
  8172. * _scsih_scan_for_devices_after_reset - scan for devices after host reset
  8173. * @ioc: per adapter object
  8174. *
  8175. * Return nothing.
  8176. */
  8177. static void
  8178. _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
  8179. {
  8180. Mpi2ExpanderPage0_t expander_pg0;
  8181. Mpi2SasDevicePage0_t sas_device_pg0;
  8182. Mpi26PCIeDevicePage0_t pcie_device_pg0;
  8183. Mpi2RaidVolPage1_t volume_pg1;
  8184. Mpi2RaidVolPage0_t volume_pg0;
  8185. Mpi2RaidPhysDiskPage0_t pd_pg0;
  8186. Mpi2EventIrConfigElement_t element;
  8187. Mpi2ConfigReply_t mpi_reply;
  8188. u8 phys_disk_num;
  8189. u16 ioc_status;
  8190. u16 handle, parent_handle;
  8191. u64 sas_address;
  8192. struct _sas_device *sas_device;
  8193. struct _pcie_device *pcie_device;
  8194. struct _sas_node *expander_device;
  8195. static struct _raid_device *raid_device;
  8196. u8 retry_count;
  8197. unsigned long flags;
  8198. pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
  8199. _scsih_sas_host_refresh(ioc);
  8200. pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
  8201. /* expanders */
  8202. handle = 0xFFFF;
  8203. while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  8204. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  8205. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8206. MPI2_IOCSTATUS_MASK;
  8207. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8208. pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
  8209. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  8210. ioc->name, ioc_status,
  8211. le32_to_cpu(mpi_reply.IOCLogInfo));
  8212. break;
  8213. }
  8214. handle = le16_to_cpu(expander_pg0.DevHandle);
  8215. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  8216. expander_device = mpt3sas_scsih_expander_find_by_sas_address(
  8217. ioc, le64_to_cpu(expander_pg0.SASAddress));
  8218. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  8219. if (expander_device)
  8220. _scsih_refresh_expander_links(ioc, expander_device,
  8221. handle);
  8222. else {
  8223. pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
  8224. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  8225. handle, (unsigned long long)
  8226. le64_to_cpu(expander_pg0.SASAddress));
  8227. _scsih_expander_add(ioc, handle);
  8228. pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
  8229. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  8230. handle, (unsigned long long)
  8231. le64_to_cpu(expander_pg0.SASAddress));
  8232. }
  8233. }
  8234. pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
  8235. ioc->name);
  8236. if (!ioc->ir_firmware)
  8237. goto skip_to_sas;
  8238. pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
  8239. /* phys disk */
  8240. phys_disk_num = 0xFF;
  8241. while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  8242. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  8243. phys_disk_num))) {
  8244. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8245. MPI2_IOCSTATUS_MASK;
  8246. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8247. pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
  8248. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  8249. ioc->name, ioc_status,
  8250. le32_to_cpu(mpi_reply.IOCLogInfo));
  8251. break;
  8252. }
  8253. phys_disk_num = pd_pg0.PhysDiskNum;
  8254. handle = le16_to_cpu(pd_pg0.DevHandle);
  8255. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  8256. if (sas_device) {
  8257. sas_device_put(sas_device);
  8258. continue;
  8259. }
  8260. if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  8261. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  8262. handle) != 0)
  8263. continue;
  8264. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8265. MPI2_IOCSTATUS_MASK;
  8266. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8267. pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
  8268. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  8269. ioc->name, ioc_status,
  8270. le32_to_cpu(mpi_reply.IOCLogInfo));
  8271. break;
  8272. }
  8273. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  8274. if (!_scsih_get_sas_address(ioc, parent_handle,
  8275. &sas_address)) {
  8276. pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
  8277. " handle (0x%04x), sas_addr(0x%016llx)\n",
  8278. ioc->name, handle, (unsigned long long)
  8279. le64_to_cpu(sas_device_pg0.SASAddress));
  8280. mpt3sas_transport_update_links(ioc, sas_address,
  8281. handle, sas_device_pg0.PhyNum,
  8282. MPI2_SAS_NEG_LINK_RATE_1_5);
  8283. set_bit(handle, ioc->pd_handles);
  8284. retry_count = 0;
  8285. /* This will retry adding the end device.
  8286. * _scsih_add_device() will decide on retries and
  8287. * return "1" when it should be retried
  8288. */
  8289. while (_scsih_add_device(ioc, handle, retry_count++,
  8290. 1)) {
  8291. ssleep(1);
  8292. }
  8293. pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
  8294. " handle (0x%04x), sas_addr(0x%016llx)\n",
  8295. ioc->name, handle, (unsigned long long)
  8296. le64_to_cpu(sas_device_pg0.SASAddress));
  8297. }
  8298. }
  8299. pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
  8300. ioc->name);
  8301. pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
  8302. /* volumes */
  8303. handle = 0xFFFF;
  8304. while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  8305. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  8306. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8307. MPI2_IOCSTATUS_MASK;
  8308. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8309. pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
  8310. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  8311. ioc->name, ioc_status,
  8312. le32_to_cpu(mpi_reply.IOCLogInfo));
  8313. break;
  8314. }
  8315. handle = le16_to_cpu(volume_pg1.DevHandle);
  8316. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  8317. raid_device = _scsih_raid_device_find_by_wwid(ioc,
  8318. le64_to_cpu(volume_pg1.WWID));
  8319. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  8320. if (raid_device)
  8321. continue;
  8322. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  8323. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  8324. sizeof(Mpi2RaidVolPage0_t)))
  8325. continue;
  8326. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8327. MPI2_IOCSTATUS_MASK;
  8328. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8329. pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
  8330. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  8331. ioc->name, ioc_status,
  8332. le32_to_cpu(mpi_reply.IOCLogInfo));
  8333. break;
  8334. }
  8335. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  8336. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  8337. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
  8338. memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
  8339. element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
  8340. element.VolDevHandle = volume_pg1.DevHandle;
  8341. pr_info(MPT3SAS_FMT
  8342. "\tBEFORE adding volume: handle (0x%04x)\n",
  8343. ioc->name, volume_pg1.DevHandle);
  8344. _scsih_sas_volume_add(ioc, &element);
  8345. pr_info(MPT3SAS_FMT
  8346. "\tAFTER adding volume: handle (0x%04x)\n",
  8347. ioc->name, volume_pg1.DevHandle);
  8348. }
  8349. }
  8350. pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
  8351. ioc->name);
  8352. skip_to_sas:
  8353. pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
  8354. ioc->name);
  8355. /* sas devices */
  8356. handle = 0xFFFF;
  8357. while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  8358. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  8359. handle))) {
  8360. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  8361. MPI2_IOCSTATUS_MASK;
  8362. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8363. pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
  8364. " ioc_status(0x%04x), loginfo(0x%08x)\n",
  8365. ioc->name, ioc_status,
  8366. le32_to_cpu(mpi_reply.IOCLogInfo));
  8367. break;
  8368. }
  8369. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  8370. if (!(_scsih_is_end_device(
  8371. le32_to_cpu(sas_device_pg0.DeviceInfo))))
  8372. continue;
  8373. sas_device = mpt3sas_get_sdev_by_addr(ioc,
  8374. le64_to_cpu(sas_device_pg0.SASAddress));
  8375. if (sas_device) {
  8376. sas_device_put(sas_device);
  8377. continue;
  8378. }
  8379. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  8380. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
  8381. pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
  8382. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  8383. handle, (unsigned long long)
  8384. le64_to_cpu(sas_device_pg0.SASAddress));
  8385. mpt3sas_transport_update_links(ioc, sas_address, handle,
  8386. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  8387. retry_count = 0;
  8388. /* This will retry adding the end device.
  8389. * _scsih_add_device() will decide on retries and
  8390. * return "1" when it should be retried
  8391. */
  8392. while (_scsih_add_device(ioc, handle, retry_count++,
  8393. 0)) {
  8394. ssleep(1);
  8395. }
  8396. pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
  8397. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  8398. handle, (unsigned long long)
  8399. le64_to_cpu(sas_device_pg0.SASAddress));
  8400. }
  8401. }
  8402. pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
  8403. ioc->name);
  8404. pr_info(MPT3SAS_FMT "\tscan devices: pcie end devices start\n",
  8405. ioc->name);
  8406. /* pcie devices */
  8407. handle = 0xFFFF;
  8408. while (!(mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
  8409. &pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  8410. handle))) {
  8411. ioc_status = le16_to_cpu(mpi_reply.IOCStatus)
  8412. & MPI2_IOCSTATUS_MASK;
  8413. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  8414. pr_info(MPT3SAS_FMT "\tbreak from pcie end device"
  8415. " scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
  8416. ioc->name, ioc_status,
  8417. le32_to_cpu(mpi_reply.IOCLogInfo));
  8418. break;
  8419. }
  8420. handle = le16_to_cpu(pcie_device_pg0.DevHandle);
  8421. if (!(_scsih_is_nvme_device(
  8422. le32_to_cpu(pcie_device_pg0.DeviceInfo))))
  8423. continue;
  8424. pcie_device = mpt3sas_get_pdev_by_wwid(ioc,
  8425. le64_to_cpu(pcie_device_pg0.WWID));
  8426. if (pcie_device) {
  8427. pcie_device_put(pcie_device);
  8428. continue;
  8429. }
  8430. retry_count = 0;
  8431. parent_handle = le16_to_cpu(pcie_device_pg0.ParentDevHandle);
  8432. _scsih_pcie_add_device(ioc, handle);
  8433. pr_info(MPT3SAS_FMT "\tAFTER adding pcie end device: "
  8434. "handle (0x%04x), wwid(0x%016llx)\n", ioc->name,
  8435. handle,
  8436. (unsigned long long) le64_to_cpu(pcie_device_pg0.WWID));
  8437. }
  8438. pr_info(MPT3SAS_FMT "\tpcie devices: pcie end devices complete\n",
  8439. ioc->name);
  8440. pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
  8441. }
  8442. /**
  8443. * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
  8444. * @ioc: per adapter object
  8445. * @reset_phase: phase
  8446. *
  8447. * The handler for doing any required cleanup or initialization.
  8448. *
  8449. * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
  8450. * MPT3_IOC_DONE_RESET
  8451. *
  8452. * Return nothing.
  8453. */
  8454. void
  8455. mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
  8456. {
  8457. switch (reset_phase) {
  8458. case MPT3_IOC_PRE_RESET:
  8459. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  8460. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  8461. break;
  8462. case MPT3_IOC_AFTER_RESET:
  8463. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  8464. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  8465. if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
  8466. ioc->scsih_cmds.status |= MPT3_CMD_RESET;
  8467. mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
  8468. complete(&ioc->scsih_cmds.done);
  8469. }
  8470. if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
  8471. ioc->tm_cmds.status |= MPT3_CMD_RESET;
  8472. mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  8473. complete(&ioc->tm_cmds.done);
  8474. }
  8475. memset(ioc->pend_os_device_add, 0, ioc->pend_os_device_add_sz);
  8476. memset(ioc->device_remove_in_progress, 0,
  8477. ioc->device_remove_in_progress_sz);
  8478. _scsih_fw_event_cleanup_queue(ioc);
  8479. _scsih_flush_running_cmds(ioc);
  8480. break;
  8481. case MPT3_IOC_DONE_RESET:
  8482. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  8483. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  8484. if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
  8485. !ioc->sas_hba.num_phys)) {
  8486. _scsih_prep_device_scan(ioc);
  8487. _scsih_search_responding_sas_devices(ioc);
  8488. _scsih_search_responding_pcie_devices(ioc);
  8489. _scsih_search_responding_raid_devices(ioc);
  8490. _scsih_search_responding_expanders(ioc);
  8491. _scsih_error_recovery_delete_devices(ioc);
  8492. }
  8493. break;
  8494. }
  8495. }
  8496. /**
  8497. * _mpt3sas_fw_work - delayed task for processing firmware events
  8498. * @ioc: per adapter object
  8499. * @fw_event: The fw_event_work object
  8500. * Context: user.
  8501. *
  8502. * Return nothing.
  8503. */
  8504. static void
  8505. _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  8506. {
  8507. _scsih_fw_event_del_from_list(ioc, fw_event);
  8508. /* the queue is being flushed so ignore this event */
  8509. if (ioc->remove_host || ioc->pci_error_recovery) {
  8510. fw_event_work_put(fw_event);
  8511. return;
  8512. }
  8513. switch (fw_event->event) {
  8514. case MPT3SAS_PROCESS_TRIGGER_DIAG:
  8515. mpt3sas_process_trigger_data(ioc,
  8516. (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
  8517. fw_event->event_data);
  8518. break;
  8519. case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
  8520. while (scsi_host_in_recovery(ioc->shost) ||
  8521. ioc->shost_recovery) {
  8522. /*
  8523. * If we're unloading, bail. Otherwise, this can become
  8524. * an infinite loop.
  8525. */
  8526. if (ioc->remove_host)
  8527. goto out;
  8528. ssleep(1);
  8529. }
  8530. _scsih_remove_unresponding_devices(ioc);
  8531. _scsih_scan_for_devices_after_reset(ioc);
  8532. break;
  8533. case MPT3SAS_PORT_ENABLE_COMPLETE:
  8534. ioc->start_scan = 0;
  8535. if (missing_delay[0] != -1 && missing_delay[1] != -1)
  8536. mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
  8537. missing_delay[1]);
  8538. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  8539. "port enable: complete from worker thread\n",
  8540. ioc->name));
  8541. break;
  8542. case MPT3SAS_TURN_ON_PFA_LED:
  8543. _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
  8544. break;
  8545. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  8546. _scsih_sas_topology_change_event(ioc, fw_event);
  8547. break;
  8548. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  8549. _scsih_sas_device_status_change_event(ioc, fw_event);
  8550. break;
  8551. case MPI2_EVENT_SAS_DISCOVERY:
  8552. _scsih_sas_discovery_event(ioc, fw_event);
  8553. break;
  8554. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  8555. _scsih_sas_broadcast_primitive_event(ioc, fw_event);
  8556. break;
  8557. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  8558. _scsih_sas_enclosure_dev_status_change_event(ioc,
  8559. fw_event);
  8560. break;
  8561. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  8562. _scsih_sas_ir_config_change_event(ioc, fw_event);
  8563. break;
  8564. case MPI2_EVENT_IR_VOLUME:
  8565. _scsih_sas_ir_volume_event(ioc, fw_event);
  8566. break;
  8567. case MPI2_EVENT_IR_PHYSICAL_DISK:
  8568. _scsih_sas_ir_physical_disk_event(ioc, fw_event);
  8569. break;
  8570. case MPI2_EVENT_IR_OPERATION_STATUS:
  8571. _scsih_sas_ir_operation_status_event(ioc, fw_event);
  8572. break;
  8573. case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
  8574. _scsih_pcie_device_status_change_event(ioc, fw_event);
  8575. break;
  8576. case MPI2_EVENT_PCIE_ENUMERATION:
  8577. _scsih_pcie_enumeration_event(ioc, fw_event);
  8578. break;
  8579. case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
  8580. _scsih_pcie_topology_change_event(ioc, fw_event);
  8581. return;
  8582. break;
  8583. }
  8584. out:
  8585. fw_event_work_put(fw_event);
  8586. }
  8587. /**
  8588. * _firmware_event_work
  8589. * @ioc: per adapter object
  8590. * @work: The fw_event_work object
  8591. * Context: user.
  8592. *
  8593. * wrappers for the work thread handling firmware events
  8594. *
  8595. * Return nothing.
  8596. */
  8597. static void
  8598. _firmware_event_work(struct work_struct *work)
  8599. {
  8600. struct fw_event_work *fw_event = container_of(work,
  8601. struct fw_event_work, work);
  8602. _mpt3sas_fw_work(fw_event->ioc, fw_event);
  8603. }
  8604. /**
  8605. * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
  8606. * @ioc: per adapter object
  8607. * @msix_index: MSIX table index supplied by the OS
  8608. * @reply: reply message frame(lower 32bit addr)
  8609. * Context: interrupt.
  8610. *
  8611. * This function merely adds a new work task into ioc->firmware_event_thread.
  8612. * The tasks are worked from _firmware_event_work in user context.
  8613. *
  8614. * Return 1 meaning mf should be freed from _base_interrupt
  8615. * 0 means the mf is freed from this function.
  8616. */
  8617. u8
  8618. mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
  8619. u32 reply)
  8620. {
  8621. struct fw_event_work *fw_event;
  8622. Mpi2EventNotificationReply_t *mpi_reply;
  8623. u16 event;
  8624. u16 sz;
  8625. Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
  8626. /* events turned off due to host reset or driver unloading */
  8627. if (ioc->remove_host || ioc->pci_error_recovery)
  8628. return 1;
  8629. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  8630. if (unlikely(!mpi_reply)) {
  8631. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  8632. ioc->name, __FILE__, __LINE__, __func__);
  8633. return 1;
  8634. }
  8635. event = le16_to_cpu(mpi_reply->Event);
  8636. if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
  8637. mpt3sas_trigger_event(ioc, event, 0);
  8638. switch (event) {
  8639. /* handle these */
  8640. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  8641. {
  8642. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  8643. (Mpi2EventDataSasBroadcastPrimitive_t *)
  8644. mpi_reply->EventData;
  8645. if (baen_data->Primitive !=
  8646. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
  8647. return 1;
  8648. if (ioc->broadcast_aen_busy) {
  8649. ioc->broadcast_aen_pending++;
  8650. return 1;
  8651. } else
  8652. ioc->broadcast_aen_busy = 1;
  8653. break;
  8654. }
  8655. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  8656. _scsih_check_topo_delete_events(ioc,
  8657. (Mpi2EventDataSasTopologyChangeList_t *)
  8658. mpi_reply->EventData);
  8659. break;
  8660. case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
  8661. _scsih_check_pcie_topo_remove_events(ioc,
  8662. (Mpi26EventDataPCIeTopologyChangeList_t *)
  8663. mpi_reply->EventData);
  8664. break;
  8665. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  8666. _scsih_check_ir_config_unhide_events(ioc,
  8667. (Mpi2EventDataIrConfigChangeList_t *)
  8668. mpi_reply->EventData);
  8669. break;
  8670. case MPI2_EVENT_IR_VOLUME:
  8671. _scsih_check_volume_delete_events(ioc,
  8672. (Mpi2EventDataIrVolume_t *)
  8673. mpi_reply->EventData);
  8674. break;
  8675. case MPI2_EVENT_LOG_ENTRY_ADDED:
  8676. {
  8677. Mpi2EventDataLogEntryAdded_t *log_entry;
  8678. u32 *log_code;
  8679. if (!ioc->is_warpdrive)
  8680. break;
  8681. log_entry = (Mpi2EventDataLogEntryAdded_t *)
  8682. mpi_reply->EventData;
  8683. log_code = (u32 *)log_entry->LogData;
  8684. if (le16_to_cpu(log_entry->LogEntryQualifier)
  8685. != MPT2_WARPDRIVE_LOGENTRY)
  8686. break;
  8687. switch (le32_to_cpu(*log_code)) {
  8688. case MPT2_WARPDRIVE_LC_SSDT:
  8689. pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
  8690. "IO Throttling has occurred in the WarpDrive "
  8691. "subsystem. Check WarpDrive documentation for "
  8692. "additional details.\n", ioc->name);
  8693. break;
  8694. case MPT2_WARPDRIVE_LC_SSDLW:
  8695. pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
  8696. "Program/Erase Cycles for the WarpDrive subsystem "
  8697. "in degraded range. Check WarpDrive documentation "
  8698. "for additional details.\n", ioc->name);
  8699. break;
  8700. case MPT2_WARPDRIVE_LC_SSDLF:
  8701. pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
  8702. "There are no Program/Erase Cycles for the "
  8703. "WarpDrive subsystem. The storage device will be "
  8704. "in read-only mode. Check WarpDrive documentation "
  8705. "for additional details.\n", ioc->name);
  8706. break;
  8707. case MPT2_WARPDRIVE_LC_BRMF:
  8708. pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
  8709. "The Backup Rail Monitor has failed on the "
  8710. "WarpDrive subsystem. Check WarpDrive "
  8711. "documentation for additional details.\n",
  8712. ioc->name);
  8713. break;
  8714. }
  8715. break;
  8716. }
  8717. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  8718. case MPI2_EVENT_IR_OPERATION_STATUS:
  8719. case MPI2_EVENT_SAS_DISCOVERY:
  8720. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  8721. case MPI2_EVENT_IR_PHYSICAL_DISK:
  8722. case MPI2_EVENT_PCIE_ENUMERATION:
  8723. case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
  8724. break;
  8725. case MPI2_EVENT_TEMP_THRESHOLD:
  8726. _scsih_temp_threshold_events(ioc,
  8727. (Mpi2EventDataTemperature_t *)
  8728. mpi_reply->EventData);
  8729. break;
  8730. case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
  8731. ActiveCableEventData =
  8732. (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
  8733. switch (ActiveCableEventData->ReasonCode) {
  8734. case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
  8735. pr_notice(MPT3SAS_FMT
  8736. "Currently an active cable with ReceptacleID %d\n",
  8737. ioc->name, ActiveCableEventData->ReceptacleID);
  8738. pr_notice("cannot be powered and devices connected\n");
  8739. pr_notice("to this active cable will not be seen\n");
  8740. pr_notice("This active cable requires %d mW of power\n",
  8741. ActiveCableEventData->ActiveCablePowerRequirement);
  8742. break;
  8743. case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
  8744. pr_notice(MPT3SAS_FMT
  8745. "Currently a cable with ReceptacleID %d\n",
  8746. ioc->name, ActiveCableEventData->ReceptacleID);
  8747. pr_notice(
  8748. "is not running at optimal speed(12 Gb/s rate)\n");
  8749. break;
  8750. }
  8751. break;
  8752. default: /* ignore the rest */
  8753. return 1;
  8754. }
  8755. sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
  8756. fw_event = alloc_fw_event_work(sz);
  8757. if (!fw_event) {
  8758. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  8759. ioc->name, __FILE__, __LINE__, __func__);
  8760. return 1;
  8761. }
  8762. memcpy(fw_event->event_data, mpi_reply->EventData, sz);
  8763. fw_event->ioc = ioc;
  8764. fw_event->VF_ID = mpi_reply->VF_ID;
  8765. fw_event->VP_ID = mpi_reply->VP_ID;
  8766. fw_event->event = event;
  8767. _scsih_fw_event_add(ioc, fw_event);
  8768. fw_event_work_put(fw_event);
  8769. return 1;
  8770. }
  8771. /**
  8772. * _scsih_expander_node_remove - removing expander device from list.
  8773. * @ioc: per adapter object
  8774. * @sas_expander: the sas_device object
  8775. *
  8776. * Removing object and freeing associated memory from the
  8777. * ioc->sas_expander_list.
  8778. *
  8779. * Return nothing.
  8780. */
  8781. static void
  8782. _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
  8783. struct _sas_node *sas_expander)
  8784. {
  8785. struct _sas_port *mpt3sas_port, *next;
  8786. unsigned long flags;
  8787. /* remove sibling ports attached to this expander */
  8788. list_for_each_entry_safe(mpt3sas_port, next,
  8789. &sas_expander->sas_port_list, port_list) {
  8790. if (ioc->shost_recovery)
  8791. return;
  8792. if (mpt3sas_port->remote_identify.device_type ==
  8793. SAS_END_DEVICE)
  8794. mpt3sas_device_remove_by_sas_address(ioc,
  8795. mpt3sas_port->remote_identify.sas_address);
  8796. else if (mpt3sas_port->remote_identify.device_type ==
  8797. SAS_EDGE_EXPANDER_DEVICE ||
  8798. mpt3sas_port->remote_identify.device_type ==
  8799. SAS_FANOUT_EXPANDER_DEVICE)
  8800. mpt3sas_expander_remove(ioc,
  8801. mpt3sas_port->remote_identify.sas_address);
  8802. }
  8803. mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
  8804. sas_expander->sas_address_parent);
  8805. pr_info(MPT3SAS_FMT
  8806. "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
  8807. ioc->name,
  8808. sas_expander->handle, (unsigned long long)
  8809. sas_expander->sas_address);
  8810. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  8811. list_del(&sas_expander->list);
  8812. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  8813. kfree(sas_expander->phy);
  8814. kfree(sas_expander);
  8815. }
  8816. /**
  8817. * _scsih_ir_shutdown - IR shutdown notification
  8818. * @ioc: per adapter object
  8819. *
  8820. * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
  8821. * the host system is shutting down.
  8822. *
  8823. * Return nothing.
  8824. */
  8825. static void
  8826. _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
  8827. {
  8828. Mpi2RaidActionRequest_t *mpi_request;
  8829. Mpi2RaidActionReply_t *mpi_reply;
  8830. u16 smid;
  8831. /* is IR firmware build loaded ? */
  8832. if (!ioc->ir_firmware)
  8833. return;
  8834. /* are there any volumes ? */
  8835. if (list_empty(&ioc->raid_device_list))
  8836. return;
  8837. mutex_lock(&ioc->scsih_cmds.mutex);
  8838. if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
  8839. pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
  8840. ioc->name, __func__);
  8841. goto out;
  8842. }
  8843. ioc->scsih_cmds.status = MPT3_CMD_PENDING;
  8844. smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  8845. if (!smid) {
  8846. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  8847. ioc->name, __func__);
  8848. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  8849. goto out;
  8850. }
  8851. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  8852. ioc->scsih_cmds.smid = smid;
  8853. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  8854. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  8855. mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
  8856. if (!ioc->hide_ir_msg)
  8857. pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
  8858. init_completion(&ioc->scsih_cmds.done);
  8859. ioc->put_smid_default(ioc, smid);
  8860. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  8861. if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
  8862. pr_err(MPT3SAS_FMT "%s: timeout\n",
  8863. ioc->name, __func__);
  8864. goto out;
  8865. }
  8866. if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
  8867. mpi_reply = ioc->scsih_cmds.reply;
  8868. if (!ioc->hide_ir_msg)
  8869. pr_info(MPT3SAS_FMT "IR shutdown "
  8870. "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
  8871. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  8872. le32_to_cpu(mpi_reply->IOCLogInfo));
  8873. }
  8874. out:
  8875. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  8876. mutex_unlock(&ioc->scsih_cmds.mutex);
  8877. }
  8878. /**
  8879. * scsih_remove - detach and remove add host
  8880. * @pdev: PCI device struct
  8881. *
  8882. * Routine called when unloading the driver.
  8883. * Return nothing.
  8884. */
  8885. static void scsih_remove(struct pci_dev *pdev)
  8886. {
  8887. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8888. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8889. struct _sas_port *mpt3sas_port, *next_port;
  8890. struct _raid_device *raid_device, *next;
  8891. struct MPT3SAS_TARGET *sas_target_priv_data;
  8892. struct _pcie_device *pcie_device, *pcienext;
  8893. struct workqueue_struct *wq;
  8894. unsigned long flags;
  8895. ioc->remove_host = 1;
  8896. _scsih_fw_event_cleanup_queue(ioc);
  8897. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  8898. wq = ioc->firmware_event_thread;
  8899. ioc->firmware_event_thread = NULL;
  8900. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  8901. if (wq)
  8902. destroy_workqueue(wq);
  8903. /* release all the volumes */
  8904. _scsih_ir_shutdown(ioc);
  8905. list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
  8906. list) {
  8907. if (raid_device->starget) {
  8908. sas_target_priv_data =
  8909. raid_device->starget->hostdata;
  8910. sas_target_priv_data->deleted = 1;
  8911. scsi_remove_target(&raid_device->starget->dev);
  8912. }
  8913. pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
  8914. ioc->name, raid_device->handle,
  8915. (unsigned long long) raid_device->wwid);
  8916. _scsih_raid_device_remove(ioc, raid_device);
  8917. }
  8918. list_for_each_entry_safe(pcie_device, pcienext, &ioc->pcie_device_list,
  8919. list) {
  8920. _scsih_pcie_device_remove_from_sml(ioc, pcie_device);
  8921. list_del_init(&pcie_device->list);
  8922. pcie_device_put(pcie_device);
  8923. }
  8924. /* free ports attached to the sas_host */
  8925. list_for_each_entry_safe(mpt3sas_port, next_port,
  8926. &ioc->sas_hba.sas_port_list, port_list) {
  8927. if (mpt3sas_port->remote_identify.device_type ==
  8928. SAS_END_DEVICE)
  8929. mpt3sas_device_remove_by_sas_address(ioc,
  8930. mpt3sas_port->remote_identify.sas_address);
  8931. else if (mpt3sas_port->remote_identify.device_type ==
  8932. SAS_EDGE_EXPANDER_DEVICE ||
  8933. mpt3sas_port->remote_identify.device_type ==
  8934. SAS_FANOUT_EXPANDER_DEVICE)
  8935. mpt3sas_expander_remove(ioc,
  8936. mpt3sas_port->remote_identify.sas_address);
  8937. }
  8938. /* free phys attached to the sas_host */
  8939. if (ioc->sas_hba.num_phys) {
  8940. kfree(ioc->sas_hba.phy);
  8941. ioc->sas_hba.phy = NULL;
  8942. ioc->sas_hba.num_phys = 0;
  8943. }
  8944. sas_remove_host(shost);
  8945. mpt3sas_base_detach(ioc);
  8946. spin_lock(&gioc_lock);
  8947. list_del(&ioc->list);
  8948. spin_unlock(&gioc_lock);
  8949. scsi_host_put(shost);
  8950. }
  8951. /**
  8952. * scsih_shutdown - routine call during system shutdown
  8953. * @pdev: PCI device struct
  8954. *
  8955. * Return nothing.
  8956. */
  8957. static void
  8958. scsih_shutdown(struct pci_dev *pdev)
  8959. {
  8960. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8961. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8962. struct workqueue_struct *wq;
  8963. unsigned long flags;
  8964. ioc->remove_host = 1;
  8965. _scsih_fw_event_cleanup_queue(ioc);
  8966. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  8967. wq = ioc->firmware_event_thread;
  8968. ioc->firmware_event_thread = NULL;
  8969. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  8970. if (wq)
  8971. destroy_workqueue(wq);
  8972. _scsih_ir_shutdown(ioc);
  8973. mpt3sas_base_detach(ioc);
  8974. }
  8975. /**
  8976. * _scsih_probe_boot_devices - reports 1st device
  8977. * @ioc: per adapter object
  8978. *
  8979. * If specified in bios page 2, this routine reports the 1st
  8980. * device scsi-ml or sas transport for persistent boot device
  8981. * purposes. Please refer to function _scsih_determine_boot_device()
  8982. */
  8983. static void
  8984. _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
  8985. {
  8986. u32 channel;
  8987. void *device;
  8988. struct _sas_device *sas_device;
  8989. struct _raid_device *raid_device;
  8990. struct _pcie_device *pcie_device;
  8991. u16 handle;
  8992. u64 sas_address_parent;
  8993. u64 sas_address;
  8994. unsigned long flags;
  8995. int rc;
  8996. int tid;
  8997. /* no Bios, return immediately */
  8998. if (!ioc->bios_pg3.BiosVersion)
  8999. return;
  9000. device = NULL;
  9001. if (ioc->req_boot_device.device) {
  9002. device = ioc->req_boot_device.device;
  9003. channel = ioc->req_boot_device.channel;
  9004. } else if (ioc->req_alt_boot_device.device) {
  9005. device = ioc->req_alt_boot_device.device;
  9006. channel = ioc->req_alt_boot_device.channel;
  9007. } else if (ioc->current_boot_device.device) {
  9008. device = ioc->current_boot_device.device;
  9009. channel = ioc->current_boot_device.channel;
  9010. }
  9011. if (!device)
  9012. return;
  9013. if (channel == RAID_CHANNEL) {
  9014. raid_device = device;
  9015. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  9016. raid_device->id, 0);
  9017. if (rc)
  9018. _scsih_raid_device_remove(ioc, raid_device);
  9019. } else if (channel == PCIE_CHANNEL) {
  9020. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  9021. pcie_device = device;
  9022. tid = pcie_device->id;
  9023. list_move_tail(&pcie_device->list, &ioc->pcie_device_list);
  9024. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  9025. rc = scsi_add_device(ioc->shost, PCIE_CHANNEL, tid, 0);
  9026. if (rc)
  9027. _scsih_pcie_device_remove(ioc, pcie_device);
  9028. } else {
  9029. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  9030. sas_device = device;
  9031. handle = sas_device->handle;
  9032. sas_address_parent = sas_device->sas_address_parent;
  9033. sas_address = sas_device->sas_address;
  9034. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  9035. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  9036. if (ioc->hide_drives)
  9037. return;
  9038. if (!mpt3sas_transport_port_add(ioc, handle,
  9039. sas_address_parent)) {
  9040. _scsih_sas_device_remove(ioc, sas_device);
  9041. } else if (!sas_device->starget) {
  9042. if (!ioc->is_driver_loading) {
  9043. mpt3sas_transport_port_remove(ioc,
  9044. sas_address,
  9045. sas_address_parent);
  9046. _scsih_sas_device_remove(ioc, sas_device);
  9047. }
  9048. }
  9049. }
  9050. }
  9051. /**
  9052. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  9053. * @ioc: per adapter object
  9054. *
  9055. * Called during initial loading of the driver.
  9056. */
  9057. static void
  9058. _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
  9059. {
  9060. struct _raid_device *raid_device, *raid_next;
  9061. int rc;
  9062. list_for_each_entry_safe(raid_device, raid_next,
  9063. &ioc->raid_device_list, list) {
  9064. if (raid_device->starget)
  9065. continue;
  9066. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  9067. raid_device->id, 0);
  9068. if (rc)
  9069. _scsih_raid_device_remove(ioc, raid_device);
  9070. }
  9071. }
  9072. static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
  9073. {
  9074. struct _sas_device *sas_device = NULL;
  9075. unsigned long flags;
  9076. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  9077. if (!list_empty(&ioc->sas_device_init_list)) {
  9078. sas_device = list_first_entry(&ioc->sas_device_init_list,
  9079. struct _sas_device, list);
  9080. sas_device_get(sas_device);
  9081. }
  9082. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  9083. return sas_device;
  9084. }
  9085. static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
  9086. struct _sas_device *sas_device)
  9087. {
  9088. unsigned long flags;
  9089. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  9090. /*
  9091. * Since we dropped the lock during the call to port_add(), we need to
  9092. * be careful here that somebody else didn't move or delete this item
  9093. * while we were busy with other things.
  9094. *
  9095. * If it was on the list, we need a put() for the reference the list
  9096. * had. Either way, we need a get() for the destination list.
  9097. */
  9098. if (!list_empty(&sas_device->list)) {
  9099. list_del_init(&sas_device->list);
  9100. sas_device_put(sas_device);
  9101. }
  9102. sas_device_get(sas_device);
  9103. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  9104. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  9105. }
  9106. /**
  9107. * _scsih_probe_sas - reporting sas devices to sas transport
  9108. * @ioc: per adapter object
  9109. *
  9110. * Called during initial loading of the driver.
  9111. */
  9112. static void
  9113. _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
  9114. {
  9115. struct _sas_device *sas_device;
  9116. if (ioc->hide_drives)
  9117. return;
  9118. while ((sas_device = get_next_sas_device(ioc))) {
  9119. if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
  9120. sas_device->sas_address_parent)) {
  9121. _scsih_sas_device_remove(ioc, sas_device);
  9122. sas_device_put(sas_device);
  9123. continue;
  9124. } else if (!sas_device->starget) {
  9125. /*
  9126. * When asyn scanning is enabled, its not possible to
  9127. * remove devices while scanning is turned on due to an
  9128. * oops in scsi_sysfs_add_sdev()->add_device()->
  9129. * sysfs_addrm_start()
  9130. */
  9131. if (!ioc->is_driver_loading) {
  9132. mpt3sas_transport_port_remove(ioc,
  9133. sas_device->sas_address,
  9134. sas_device->sas_address_parent);
  9135. _scsih_sas_device_remove(ioc, sas_device);
  9136. sas_device_put(sas_device);
  9137. continue;
  9138. }
  9139. }
  9140. sas_device_make_active(ioc, sas_device);
  9141. sas_device_put(sas_device);
  9142. }
  9143. }
  9144. /**
  9145. * get_next_pcie_device - Get the next pcie device
  9146. * @ioc: per adapter object
  9147. *
  9148. * Get the next pcie device from pcie_device_init_list list.
  9149. *
  9150. * Returns pcie device structure if pcie_device_init_list list is not empty
  9151. * otherwise returns NULL
  9152. */
  9153. static struct _pcie_device *get_next_pcie_device(struct MPT3SAS_ADAPTER *ioc)
  9154. {
  9155. struct _pcie_device *pcie_device = NULL;
  9156. unsigned long flags;
  9157. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  9158. if (!list_empty(&ioc->pcie_device_init_list)) {
  9159. pcie_device = list_first_entry(&ioc->pcie_device_init_list,
  9160. struct _pcie_device, list);
  9161. pcie_device_get(pcie_device);
  9162. }
  9163. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  9164. return pcie_device;
  9165. }
  9166. /**
  9167. * pcie_device_make_active - Add pcie device to pcie_device_list list
  9168. * @ioc: per adapter object
  9169. * @pcie_device: pcie device object
  9170. *
  9171. * Add the pcie device which has registered with SCSI Transport Later to
  9172. * pcie_device_list list
  9173. */
  9174. static void pcie_device_make_active(struct MPT3SAS_ADAPTER *ioc,
  9175. struct _pcie_device *pcie_device)
  9176. {
  9177. unsigned long flags;
  9178. spin_lock_irqsave(&ioc->pcie_device_lock, flags);
  9179. if (!list_empty(&pcie_device->list)) {
  9180. list_del_init(&pcie_device->list);
  9181. pcie_device_put(pcie_device);
  9182. }
  9183. pcie_device_get(pcie_device);
  9184. list_add_tail(&pcie_device->list, &ioc->pcie_device_list);
  9185. spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
  9186. }
  9187. /**
  9188. * _scsih_probe_pcie - reporting PCIe devices to scsi-ml
  9189. * @ioc: per adapter object
  9190. *
  9191. * Called during initial loading of the driver.
  9192. */
  9193. static void
  9194. _scsih_probe_pcie(struct MPT3SAS_ADAPTER *ioc)
  9195. {
  9196. struct _pcie_device *pcie_device;
  9197. int rc;
  9198. /* PCIe Device List */
  9199. while ((pcie_device = get_next_pcie_device(ioc))) {
  9200. if (pcie_device->starget) {
  9201. pcie_device_put(pcie_device);
  9202. continue;
  9203. }
  9204. rc = scsi_add_device(ioc->shost, PCIE_CHANNEL,
  9205. pcie_device->id, 0);
  9206. if (rc) {
  9207. _scsih_pcie_device_remove(ioc, pcie_device);
  9208. pcie_device_put(pcie_device);
  9209. continue;
  9210. } else if (!pcie_device->starget) {
  9211. /*
  9212. * When async scanning is enabled, its not possible to
  9213. * remove devices while scanning is turned on due to an
  9214. * oops in scsi_sysfs_add_sdev()->add_device()->
  9215. * sysfs_addrm_start()
  9216. */
  9217. if (!ioc->is_driver_loading) {
  9218. /* TODO-- Need to find out whether this condition will
  9219. * occur or not
  9220. */
  9221. _scsih_pcie_device_remove(ioc, pcie_device);
  9222. pcie_device_put(pcie_device);
  9223. continue;
  9224. }
  9225. }
  9226. pcie_device_make_active(ioc, pcie_device);
  9227. pcie_device_put(pcie_device);
  9228. }
  9229. }
  9230. /**
  9231. * _scsih_probe_devices - probing for devices
  9232. * @ioc: per adapter object
  9233. *
  9234. * Called during initial loading of the driver.
  9235. */
  9236. static void
  9237. _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
  9238. {
  9239. u16 volume_mapping_flags;
  9240. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  9241. return; /* return when IOC doesn't support initiator mode */
  9242. _scsih_probe_boot_devices(ioc);
  9243. if (ioc->ir_firmware) {
  9244. volume_mapping_flags =
  9245. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  9246. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  9247. if (volume_mapping_flags ==
  9248. MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
  9249. _scsih_probe_raid(ioc);
  9250. _scsih_probe_sas(ioc);
  9251. } else {
  9252. _scsih_probe_sas(ioc);
  9253. _scsih_probe_raid(ioc);
  9254. }
  9255. } else {
  9256. _scsih_probe_sas(ioc);
  9257. _scsih_probe_pcie(ioc);
  9258. }
  9259. }
  9260. /**
  9261. * scsih_scan_start - scsi lld callback for .scan_start
  9262. * @shost: SCSI host pointer
  9263. *
  9264. * The shost has the ability to discover targets on its own instead
  9265. * of scanning the entire bus. In our implemention, we will kick off
  9266. * firmware discovery.
  9267. */
  9268. static void
  9269. scsih_scan_start(struct Scsi_Host *shost)
  9270. {
  9271. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9272. int rc;
  9273. if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
  9274. mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
  9275. if (disable_discovery > 0)
  9276. return;
  9277. ioc->start_scan = 1;
  9278. rc = mpt3sas_port_enable(ioc);
  9279. if (rc != 0)
  9280. pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
  9281. }
  9282. /**
  9283. * scsih_scan_finished - scsi lld callback for .scan_finished
  9284. * @shost: SCSI host pointer
  9285. * @time: elapsed time of the scan in jiffies
  9286. *
  9287. * This function will be called periodicallyn until it returns 1 with the
  9288. * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
  9289. * we wait for firmware discovery to complete, then return 1.
  9290. */
  9291. static int
  9292. scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
  9293. {
  9294. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9295. if (disable_discovery > 0) {
  9296. ioc->is_driver_loading = 0;
  9297. ioc->wait_for_discovery_to_complete = 0;
  9298. return 1;
  9299. }
  9300. if (time >= (300 * HZ)) {
  9301. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  9302. pr_info(MPT3SAS_FMT
  9303. "port enable: FAILED with timeout (timeout=300s)\n",
  9304. ioc->name);
  9305. ioc->is_driver_loading = 0;
  9306. return 1;
  9307. }
  9308. if (ioc->start_scan)
  9309. return 0;
  9310. if (ioc->start_scan_failed) {
  9311. pr_info(MPT3SAS_FMT
  9312. "port enable: FAILED with (ioc_status=0x%08x)\n",
  9313. ioc->name, ioc->start_scan_failed);
  9314. ioc->is_driver_loading = 0;
  9315. ioc->wait_for_discovery_to_complete = 0;
  9316. ioc->remove_host = 1;
  9317. return 1;
  9318. }
  9319. pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
  9320. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  9321. if (ioc->wait_for_discovery_to_complete) {
  9322. ioc->wait_for_discovery_to_complete = 0;
  9323. _scsih_probe_devices(ioc);
  9324. }
  9325. mpt3sas_base_start_watchdog(ioc);
  9326. ioc->is_driver_loading = 0;
  9327. return 1;
  9328. }
  9329. /* shost template for SAS 2.0 HBA devices */
  9330. static struct scsi_host_template mpt2sas_driver_template = {
  9331. .module = THIS_MODULE,
  9332. .name = "Fusion MPT SAS Host",
  9333. .proc_name = MPT2SAS_DRIVER_NAME,
  9334. .queuecommand = scsih_qcmd,
  9335. .target_alloc = scsih_target_alloc,
  9336. .slave_alloc = scsih_slave_alloc,
  9337. .slave_configure = scsih_slave_configure,
  9338. .target_destroy = scsih_target_destroy,
  9339. .slave_destroy = scsih_slave_destroy,
  9340. .scan_finished = scsih_scan_finished,
  9341. .scan_start = scsih_scan_start,
  9342. .change_queue_depth = scsih_change_queue_depth,
  9343. .eh_abort_handler = scsih_abort,
  9344. .eh_device_reset_handler = scsih_dev_reset,
  9345. .eh_target_reset_handler = scsih_target_reset,
  9346. .eh_host_reset_handler = scsih_host_reset,
  9347. .bios_param = scsih_bios_param,
  9348. .can_queue = 1,
  9349. .this_id = -1,
  9350. .sg_tablesize = MPT2SAS_SG_DEPTH,
  9351. .max_sectors = 32767,
  9352. .cmd_per_lun = 7,
  9353. .use_clustering = ENABLE_CLUSTERING,
  9354. .shost_attrs = mpt3sas_host_attrs,
  9355. .sdev_attrs = mpt3sas_dev_attrs,
  9356. .track_queue_depth = 1,
  9357. };
  9358. /* raid transport support for SAS 2.0 HBA devices */
  9359. static struct raid_function_template mpt2sas_raid_functions = {
  9360. .cookie = &mpt2sas_driver_template,
  9361. .is_raid = scsih_is_raid,
  9362. .get_resync = scsih_get_resync,
  9363. .get_state = scsih_get_state,
  9364. };
  9365. /* shost template for SAS 3.0 HBA devices */
  9366. static struct scsi_host_template mpt3sas_driver_template = {
  9367. .module = THIS_MODULE,
  9368. .name = "Fusion MPT SAS Host",
  9369. .proc_name = MPT3SAS_DRIVER_NAME,
  9370. .queuecommand = scsih_qcmd,
  9371. .target_alloc = scsih_target_alloc,
  9372. .slave_alloc = scsih_slave_alloc,
  9373. .slave_configure = scsih_slave_configure,
  9374. .target_destroy = scsih_target_destroy,
  9375. .slave_destroy = scsih_slave_destroy,
  9376. .scan_finished = scsih_scan_finished,
  9377. .scan_start = scsih_scan_start,
  9378. .change_queue_depth = scsih_change_queue_depth,
  9379. .eh_abort_handler = scsih_abort,
  9380. .eh_device_reset_handler = scsih_dev_reset,
  9381. .eh_target_reset_handler = scsih_target_reset,
  9382. .eh_host_reset_handler = scsih_host_reset,
  9383. .bios_param = scsih_bios_param,
  9384. .can_queue = 1,
  9385. .this_id = -1,
  9386. .sg_tablesize = MPT3SAS_SG_DEPTH,
  9387. .max_sectors = 32767,
  9388. .cmd_per_lun = 7,
  9389. .use_clustering = ENABLE_CLUSTERING,
  9390. .shost_attrs = mpt3sas_host_attrs,
  9391. .sdev_attrs = mpt3sas_dev_attrs,
  9392. .track_queue_depth = 1,
  9393. };
  9394. /* raid transport support for SAS 3.0 HBA devices */
  9395. static struct raid_function_template mpt3sas_raid_functions = {
  9396. .cookie = &mpt3sas_driver_template,
  9397. .is_raid = scsih_is_raid,
  9398. .get_resync = scsih_get_resync,
  9399. .get_state = scsih_get_state,
  9400. };
  9401. /**
  9402. * _scsih_determine_hba_mpi_version - determine in which MPI version class
  9403. * this device belongs to.
  9404. * @pdev: PCI device struct
  9405. *
  9406. * return MPI2_VERSION for SAS 2.0 HBA devices,
  9407. * MPI25_VERSION for SAS 3.0 HBA devices, and
  9408. * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
  9409. */
  9410. static u16
  9411. _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
  9412. {
  9413. switch (pdev->device) {
  9414. case MPI2_MFGPAGE_DEVID_SSS6200:
  9415. case MPI2_MFGPAGE_DEVID_SAS2004:
  9416. case MPI2_MFGPAGE_DEVID_SAS2008:
  9417. case MPI2_MFGPAGE_DEVID_SAS2108_1:
  9418. case MPI2_MFGPAGE_DEVID_SAS2108_2:
  9419. case MPI2_MFGPAGE_DEVID_SAS2108_3:
  9420. case MPI2_MFGPAGE_DEVID_SAS2116_1:
  9421. case MPI2_MFGPAGE_DEVID_SAS2116_2:
  9422. case MPI2_MFGPAGE_DEVID_SAS2208_1:
  9423. case MPI2_MFGPAGE_DEVID_SAS2208_2:
  9424. case MPI2_MFGPAGE_DEVID_SAS2208_3:
  9425. case MPI2_MFGPAGE_DEVID_SAS2208_4:
  9426. case MPI2_MFGPAGE_DEVID_SAS2208_5:
  9427. case MPI2_MFGPAGE_DEVID_SAS2208_6:
  9428. case MPI2_MFGPAGE_DEVID_SAS2308_1:
  9429. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  9430. case MPI2_MFGPAGE_DEVID_SAS2308_3:
  9431. return MPI2_VERSION;
  9432. case MPI25_MFGPAGE_DEVID_SAS3004:
  9433. case MPI25_MFGPAGE_DEVID_SAS3008:
  9434. case MPI25_MFGPAGE_DEVID_SAS3108_1:
  9435. case MPI25_MFGPAGE_DEVID_SAS3108_2:
  9436. case MPI25_MFGPAGE_DEVID_SAS3108_5:
  9437. case MPI25_MFGPAGE_DEVID_SAS3108_6:
  9438. return MPI25_VERSION;
  9439. case MPI26_MFGPAGE_DEVID_SAS3216:
  9440. case MPI26_MFGPAGE_DEVID_SAS3224:
  9441. case MPI26_MFGPAGE_DEVID_SAS3316_1:
  9442. case MPI26_MFGPAGE_DEVID_SAS3316_2:
  9443. case MPI26_MFGPAGE_DEVID_SAS3316_3:
  9444. case MPI26_MFGPAGE_DEVID_SAS3316_4:
  9445. case MPI26_MFGPAGE_DEVID_SAS3324_1:
  9446. case MPI26_MFGPAGE_DEVID_SAS3324_2:
  9447. case MPI26_MFGPAGE_DEVID_SAS3324_3:
  9448. case MPI26_MFGPAGE_DEVID_SAS3324_4:
  9449. case MPI26_MFGPAGE_DEVID_SAS3508:
  9450. case MPI26_MFGPAGE_DEVID_SAS3508_1:
  9451. case MPI26_MFGPAGE_DEVID_SAS3408:
  9452. case MPI26_MFGPAGE_DEVID_SAS3516:
  9453. case MPI26_MFGPAGE_DEVID_SAS3516_1:
  9454. case MPI26_MFGPAGE_DEVID_SAS3416:
  9455. case MPI26_MFGPAGE_DEVID_SAS3616:
  9456. return MPI26_VERSION;
  9457. }
  9458. return 0;
  9459. }
  9460. /**
  9461. * _scsih_probe - attach and add scsi host
  9462. * @pdev: PCI device struct
  9463. * @id: pci device id
  9464. *
  9465. * Returns 0 success, anything else error.
  9466. */
  9467. static int
  9468. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  9469. {
  9470. struct MPT3SAS_ADAPTER *ioc;
  9471. struct Scsi_Host *shost = NULL;
  9472. int rv;
  9473. u16 hba_mpi_version;
  9474. /* Determine in which MPI version class this pci device belongs */
  9475. hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
  9476. if (hba_mpi_version == 0)
  9477. return -ENODEV;
  9478. /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
  9479. * for other generation HBA's return with -ENODEV
  9480. */
  9481. if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
  9482. return -ENODEV;
  9483. /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
  9484. * for other generation HBA's return with -ENODEV
  9485. */
  9486. if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
  9487. || hba_mpi_version == MPI26_VERSION)))
  9488. return -ENODEV;
  9489. switch (hba_mpi_version) {
  9490. case MPI2_VERSION:
  9491. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  9492. PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
  9493. /* Use mpt2sas driver host template for SAS 2.0 HBA's */
  9494. shost = scsi_host_alloc(&mpt2sas_driver_template,
  9495. sizeof(struct MPT3SAS_ADAPTER));
  9496. if (!shost)
  9497. return -ENODEV;
  9498. ioc = shost_priv(shost);
  9499. memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
  9500. ioc->hba_mpi_version_belonged = hba_mpi_version;
  9501. ioc->id = mpt2_ids++;
  9502. sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
  9503. if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
  9504. ioc->is_warpdrive = 1;
  9505. ioc->hide_ir_msg = 1;
  9506. } else
  9507. ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
  9508. break;
  9509. case MPI25_VERSION:
  9510. case MPI26_VERSION:
  9511. /* Use mpt3sas driver host template for SAS 3.0 HBA's */
  9512. shost = scsi_host_alloc(&mpt3sas_driver_template,
  9513. sizeof(struct MPT3SAS_ADAPTER));
  9514. if (!shost)
  9515. return -ENODEV;
  9516. ioc = shost_priv(shost);
  9517. memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
  9518. ioc->hba_mpi_version_belonged = hba_mpi_version;
  9519. ioc->id = mpt3_ids++;
  9520. sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
  9521. switch (pdev->device) {
  9522. case MPI26_MFGPAGE_DEVID_SAS3508:
  9523. case MPI26_MFGPAGE_DEVID_SAS3508_1:
  9524. case MPI26_MFGPAGE_DEVID_SAS3408:
  9525. case MPI26_MFGPAGE_DEVID_SAS3516:
  9526. case MPI26_MFGPAGE_DEVID_SAS3516_1:
  9527. case MPI26_MFGPAGE_DEVID_SAS3416:
  9528. case MPI26_MFGPAGE_DEVID_SAS3616:
  9529. ioc->is_gen35_ioc = 1;
  9530. break;
  9531. default:
  9532. ioc->is_gen35_ioc = 0;
  9533. }
  9534. if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
  9535. pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
  9536. (ioc->hba_mpi_version_belonged == MPI26_VERSION)) {
  9537. ioc->combined_reply_queue = 1;
  9538. if (ioc->is_gen35_ioc)
  9539. ioc->combined_reply_index_count =
  9540. MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35;
  9541. else
  9542. ioc->combined_reply_index_count =
  9543. MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3;
  9544. }
  9545. break;
  9546. default:
  9547. return -ENODEV;
  9548. }
  9549. INIT_LIST_HEAD(&ioc->list);
  9550. spin_lock(&gioc_lock);
  9551. list_add_tail(&ioc->list, &mpt3sas_ioc_list);
  9552. spin_unlock(&gioc_lock);
  9553. ioc->shost = shost;
  9554. ioc->pdev = pdev;
  9555. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  9556. ioc->tm_cb_idx = tm_cb_idx;
  9557. ioc->ctl_cb_idx = ctl_cb_idx;
  9558. ioc->base_cb_idx = base_cb_idx;
  9559. ioc->port_enable_cb_idx = port_enable_cb_idx;
  9560. ioc->transport_cb_idx = transport_cb_idx;
  9561. ioc->scsih_cb_idx = scsih_cb_idx;
  9562. ioc->config_cb_idx = config_cb_idx;
  9563. ioc->tm_tr_cb_idx = tm_tr_cb_idx;
  9564. ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
  9565. ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
  9566. ioc->logging_level = logging_level;
  9567. ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
  9568. /* misc semaphores and spin locks */
  9569. mutex_init(&ioc->reset_in_progress_mutex);
  9570. /* initializing pci_access_mutex lock */
  9571. mutex_init(&ioc->pci_access_mutex);
  9572. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  9573. spin_lock_init(&ioc->scsi_lookup_lock);
  9574. spin_lock_init(&ioc->sas_device_lock);
  9575. spin_lock_init(&ioc->sas_node_lock);
  9576. spin_lock_init(&ioc->fw_event_lock);
  9577. spin_lock_init(&ioc->raid_device_lock);
  9578. spin_lock_init(&ioc->pcie_device_lock);
  9579. spin_lock_init(&ioc->diag_trigger_lock);
  9580. INIT_LIST_HEAD(&ioc->sas_device_list);
  9581. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  9582. INIT_LIST_HEAD(&ioc->sas_expander_list);
  9583. INIT_LIST_HEAD(&ioc->pcie_device_list);
  9584. INIT_LIST_HEAD(&ioc->pcie_device_init_list);
  9585. INIT_LIST_HEAD(&ioc->fw_event_list);
  9586. INIT_LIST_HEAD(&ioc->raid_device_list);
  9587. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  9588. INIT_LIST_HEAD(&ioc->delayed_tr_list);
  9589. INIT_LIST_HEAD(&ioc->delayed_sc_list);
  9590. INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
  9591. INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
  9592. INIT_LIST_HEAD(&ioc->reply_queue_list);
  9593. sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
  9594. /* init shost parameters */
  9595. shost->max_cmd_len = 32;
  9596. shost->max_lun = max_lun;
  9597. shost->transportt = mpt3sas_transport_template;
  9598. shost->unique_id = ioc->id;
  9599. if (max_sectors != 0xFFFF) {
  9600. if (max_sectors < 64) {
  9601. shost->max_sectors = 64;
  9602. pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
  9603. "for max_sectors, range is 64 to 32767. Assigning "
  9604. "value of 64.\n", ioc->name, max_sectors);
  9605. } else if (max_sectors > 32767) {
  9606. shost->max_sectors = 32767;
  9607. pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
  9608. "for max_sectors, range is 64 to 32767. Assigning "
  9609. "default value of 32767.\n", ioc->name,
  9610. max_sectors);
  9611. } else {
  9612. shost->max_sectors = max_sectors & 0xFFFE;
  9613. pr_info(MPT3SAS_FMT
  9614. "The max_sectors value is set to %d\n",
  9615. ioc->name, shost->max_sectors);
  9616. }
  9617. }
  9618. /* register EEDP capabilities with SCSI layer */
  9619. if (prot_mask > 0)
  9620. scsi_host_set_prot(shost, prot_mask);
  9621. else
  9622. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  9623. | SHOST_DIF_TYPE2_PROTECTION
  9624. | SHOST_DIF_TYPE3_PROTECTION);
  9625. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  9626. /* event thread */
  9627. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  9628. "fw_event_%s%d", ioc->driver_name, ioc->id);
  9629. ioc->firmware_event_thread = alloc_ordered_workqueue(
  9630. ioc->firmware_event_name, WQ_MEM_RECLAIM);
  9631. if (!ioc->firmware_event_thread) {
  9632. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  9633. ioc->name, __FILE__, __LINE__, __func__);
  9634. rv = -ENODEV;
  9635. goto out_thread_fail;
  9636. }
  9637. ioc->is_driver_loading = 1;
  9638. if ((mpt3sas_base_attach(ioc))) {
  9639. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  9640. ioc->name, __FILE__, __LINE__, __func__);
  9641. rv = -ENODEV;
  9642. goto out_attach_fail;
  9643. }
  9644. if (ioc->is_warpdrive) {
  9645. if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
  9646. ioc->hide_drives = 0;
  9647. else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
  9648. ioc->hide_drives = 1;
  9649. else {
  9650. if (mpt3sas_get_num_volumes(ioc))
  9651. ioc->hide_drives = 1;
  9652. else
  9653. ioc->hide_drives = 0;
  9654. }
  9655. } else
  9656. ioc->hide_drives = 0;
  9657. rv = scsi_add_host(shost, &pdev->dev);
  9658. if (rv) {
  9659. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  9660. ioc->name, __FILE__, __LINE__, __func__);
  9661. goto out_add_shost_fail;
  9662. }
  9663. scsi_scan_host(shost);
  9664. return 0;
  9665. out_add_shost_fail:
  9666. mpt3sas_base_detach(ioc);
  9667. out_attach_fail:
  9668. destroy_workqueue(ioc->firmware_event_thread);
  9669. out_thread_fail:
  9670. spin_lock(&gioc_lock);
  9671. list_del(&ioc->list);
  9672. spin_unlock(&gioc_lock);
  9673. scsi_host_put(shost);
  9674. return rv;
  9675. }
  9676. #ifdef CONFIG_PM
  9677. /**
  9678. * scsih_suspend - power management suspend main entry point
  9679. * @pdev: PCI device struct
  9680. * @state: PM state change to (usually PCI_D3)
  9681. *
  9682. * Returns 0 success, anything else error.
  9683. */
  9684. static int
  9685. scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  9686. {
  9687. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  9688. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9689. pci_power_t device_state;
  9690. mpt3sas_base_stop_watchdog(ioc);
  9691. flush_scheduled_work();
  9692. scsi_block_requests(shost);
  9693. device_state = pci_choose_state(pdev, state);
  9694. pr_info(MPT3SAS_FMT
  9695. "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
  9696. ioc->name, pdev, pci_name(pdev), device_state);
  9697. pci_save_state(pdev);
  9698. mpt3sas_base_free_resources(ioc);
  9699. pci_set_power_state(pdev, device_state);
  9700. return 0;
  9701. }
  9702. /**
  9703. * scsih_resume - power management resume main entry point
  9704. * @pdev: PCI device struct
  9705. *
  9706. * Returns 0 success, anything else error.
  9707. */
  9708. static int
  9709. scsih_resume(struct pci_dev *pdev)
  9710. {
  9711. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  9712. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9713. pci_power_t device_state = pdev->current_state;
  9714. int r;
  9715. pr_info(MPT3SAS_FMT
  9716. "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
  9717. ioc->name, pdev, pci_name(pdev), device_state);
  9718. pci_set_power_state(pdev, PCI_D0);
  9719. pci_enable_wake(pdev, PCI_D0, 0);
  9720. pci_restore_state(pdev);
  9721. ioc->pdev = pdev;
  9722. r = mpt3sas_base_map_resources(ioc);
  9723. if (r)
  9724. return r;
  9725. mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
  9726. scsi_unblock_requests(shost);
  9727. mpt3sas_base_start_watchdog(ioc);
  9728. return 0;
  9729. }
  9730. #endif /* CONFIG_PM */
  9731. /**
  9732. * scsih_pci_error_detected - Called when a PCI error is detected.
  9733. * @pdev: PCI device struct
  9734. * @state: PCI channel state
  9735. *
  9736. * Description: Called when a PCI error is detected.
  9737. *
  9738. * Return value:
  9739. * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  9740. */
  9741. static pci_ers_result_t
  9742. scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  9743. {
  9744. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  9745. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9746. pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
  9747. ioc->name, state);
  9748. switch (state) {
  9749. case pci_channel_io_normal:
  9750. return PCI_ERS_RESULT_CAN_RECOVER;
  9751. case pci_channel_io_frozen:
  9752. /* Fatal error, prepare for slot reset */
  9753. ioc->pci_error_recovery = 1;
  9754. scsi_block_requests(ioc->shost);
  9755. mpt3sas_base_stop_watchdog(ioc);
  9756. mpt3sas_base_free_resources(ioc);
  9757. return PCI_ERS_RESULT_NEED_RESET;
  9758. case pci_channel_io_perm_failure:
  9759. /* Permanent error, prepare for device removal */
  9760. ioc->pci_error_recovery = 1;
  9761. mpt3sas_base_stop_watchdog(ioc);
  9762. _scsih_flush_running_cmds(ioc);
  9763. return PCI_ERS_RESULT_DISCONNECT;
  9764. }
  9765. return PCI_ERS_RESULT_NEED_RESET;
  9766. }
  9767. /**
  9768. * scsih_pci_slot_reset - Called when PCI slot has been reset.
  9769. * @pdev: PCI device struct
  9770. *
  9771. * Description: This routine is called by the pci error recovery
  9772. * code after the PCI slot has been reset, just before we
  9773. * should resume normal operations.
  9774. */
  9775. static pci_ers_result_t
  9776. scsih_pci_slot_reset(struct pci_dev *pdev)
  9777. {
  9778. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  9779. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9780. int rc;
  9781. pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
  9782. ioc->name);
  9783. ioc->pci_error_recovery = 0;
  9784. ioc->pdev = pdev;
  9785. pci_restore_state(pdev);
  9786. rc = mpt3sas_base_map_resources(ioc);
  9787. if (rc)
  9788. return PCI_ERS_RESULT_DISCONNECT;
  9789. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  9790. pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
  9791. (rc == 0) ? "success" : "failed");
  9792. if (!rc)
  9793. return PCI_ERS_RESULT_RECOVERED;
  9794. else
  9795. return PCI_ERS_RESULT_DISCONNECT;
  9796. }
  9797. /**
  9798. * scsih_pci_resume() - resume normal ops after PCI reset
  9799. * @pdev: pointer to PCI device
  9800. *
  9801. * Called when the error recovery driver tells us that its
  9802. * OK to resume normal operation. Use completion to allow
  9803. * halted scsi ops to resume.
  9804. */
  9805. static void
  9806. scsih_pci_resume(struct pci_dev *pdev)
  9807. {
  9808. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  9809. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9810. pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
  9811. pci_cleanup_aer_uncorrect_error_status(pdev);
  9812. mpt3sas_base_start_watchdog(ioc);
  9813. scsi_unblock_requests(ioc->shost);
  9814. }
  9815. /**
  9816. * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
  9817. * @pdev: pointer to PCI device
  9818. */
  9819. static pci_ers_result_t
  9820. scsih_pci_mmio_enabled(struct pci_dev *pdev)
  9821. {
  9822. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  9823. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  9824. pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
  9825. ioc->name);
  9826. /* TODO - dump whatever for debugging purposes */
  9827. /* This called only if scsih_pci_error_detected returns
  9828. * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
  9829. * works, no need to reset slot.
  9830. */
  9831. return PCI_ERS_RESULT_RECOVERED;
  9832. }
  9833. /**
  9834. * scsih__ncq_prio_supp - Check for NCQ command priority support
  9835. * @sdev: scsi device struct
  9836. *
  9837. * This is called when a user indicates they would like to enable
  9838. * ncq command priorities. This works only on SATA devices.
  9839. */
  9840. bool scsih_ncq_prio_supp(struct scsi_device *sdev)
  9841. {
  9842. unsigned char *buf;
  9843. bool ncq_prio_supp = false;
  9844. if (!scsi_device_supports_vpd(sdev))
  9845. return ncq_prio_supp;
  9846. buf = kmalloc(SCSI_VPD_PG_LEN, GFP_KERNEL);
  9847. if (!buf)
  9848. return ncq_prio_supp;
  9849. if (!scsi_get_vpd_page(sdev, 0x89, buf, SCSI_VPD_PG_LEN))
  9850. ncq_prio_supp = (buf[213] >> 4) & 1;
  9851. kfree(buf);
  9852. return ncq_prio_supp;
  9853. }
  9854. /*
  9855. * The pci device ids are defined in mpi/mpi2_cnfg.h.
  9856. */
  9857. static const struct pci_device_id mpt3sas_pci_table[] = {
  9858. /* Spitfire ~ 2004 */
  9859. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
  9860. PCI_ANY_ID, PCI_ANY_ID },
  9861. /* Falcon ~ 2008 */
  9862. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
  9863. PCI_ANY_ID, PCI_ANY_ID },
  9864. /* Liberator ~ 2108 */
  9865. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
  9866. PCI_ANY_ID, PCI_ANY_ID },
  9867. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
  9868. PCI_ANY_ID, PCI_ANY_ID },
  9869. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
  9870. PCI_ANY_ID, PCI_ANY_ID },
  9871. /* Meteor ~ 2116 */
  9872. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
  9873. PCI_ANY_ID, PCI_ANY_ID },
  9874. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
  9875. PCI_ANY_ID, PCI_ANY_ID },
  9876. /* Thunderbolt ~ 2208 */
  9877. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
  9878. PCI_ANY_ID, PCI_ANY_ID },
  9879. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
  9880. PCI_ANY_ID, PCI_ANY_ID },
  9881. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
  9882. PCI_ANY_ID, PCI_ANY_ID },
  9883. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
  9884. PCI_ANY_ID, PCI_ANY_ID },
  9885. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
  9886. PCI_ANY_ID, PCI_ANY_ID },
  9887. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
  9888. PCI_ANY_ID, PCI_ANY_ID },
  9889. /* Mustang ~ 2308 */
  9890. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
  9891. PCI_ANY_ID, PCI_ANY_ID },
  9892. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
  9893. PCI_ANY_ID, PCI_ANY_ID },
  9894. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
  9895. PCI_ANY_ID, PCI_ANY_ID },
  9896. /* SSS6200 */
  9897. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
  9898. PCI_ANY_ID, PCI_ANY_ID },
  9899. /* Fury ~ 3004 and 3008 */
  9900. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
  9901. PCI_ANY_ID, PCI_ANY_ID },
  9902. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
  9903. PCI_ANY_ID, PCI_ANY_ID },
  9904. /* Invader ~ 3108 */
  9905. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
  9906. PCI_ANY_ID, PCI_ANY_ID },
  9907. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
  9908. PCI_ANY_ID, PCI_ANY_ID },
  9909. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
  9910. PCI_ANY_ID, PCI_ANY_ID },
  9911. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
  9912. PCI_ANY_ID, PCI_ANY_ID },
  9913. /* Cutlass ~ 3216 and 3224 */
  9914. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
  9915. PCI_ANY_ID, PCI_ANY_ID },
  9916. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
  9917. PCI_ANY_ID, PCI_ANY_ID },
  9918. /* Intruder ~ 3316 and 3324 */
  9919. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
  9920. PCI_ANY_ID, PCI_ANY_ID },
  9921. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
  9922. PCI_ANY_ID, PCI_ANY_ID },
  9923. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
  9924. PCI_ANY_ID, PCI_ANY_ID },
  9925. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
  9926. PCI_ANY_ID, PCI_ANY_ID },
  9927. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
  9928. PCI_ANY_ID, PCI_ANY_ID },
  9929. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
  9930. PCI_ANY_ID, PCI_ANY_ID },
  9931. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
  9932. PCI_ANY_ID, PCI_ANY_ID },
  9933. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
  9934. PCI_ANY_ID, PCI_ANY_ID },
  9935. /* Ventura, Crusader, Harpoon & Tomcat ~ 3516, 3416, 3508 & 3408*/
  9936. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
  9937. PCI_ANY_ID, PCI_ANY_ID },
  9938. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
  9939. PCI_ANY_ID, PCI_ANY_ID },
  9940. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
  9941. PCI_ANY_ID, PCI_ANY_ID },
  9942. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
  9943. PCI_ANY_ID, PCI_ANY_ID },
  9944. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
  9945. PCI_ANY_ID, PCI_ANY_ID },
  9946. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
  9947. PCI_ANY_ID, PCI_ANY_ID },
  9948. /* Mercator ~ 3616*/
  9949. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3616,
  9950. PCI_ANY_ID, PCI_ANY_ID },
  9951. {0} /* Terminating entry */
  9952. };
  9953. MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
  9954. static struct pci_error_handlers _mpt3sas_err_handler = {
  9955. .error_detected = scsih_pci_error_detected,
  9956. .mmio_enabled = scsih_pci_mmio_enabled,
  9957. .slot_reset = scsih_pci_slot_reset,
  9958. .resume = scsih_pci_resume,
  9959. };
  9960. static struct pci_driver mpt3sas_driver = {
  9961. .name = MPT3SAS_DRIVER_NAME,
  9962. .id_table = mpt3sas_pci_table,
  9963. .probe = _scsih_probe,
  9964. .remove = scsih_remove,
  9965. .shutdown = scsih_shutdown,
  9966. .err_handler = &_mpt3sas_err_handler,
  9967. #ifdef CONFIG_PM
  9968. .suspend = scsih_suspend,
  9969. .resume = scsih_resume,
  9970. #endif
  9971. };
  9972. /**
  9973. * scsih_init - main entry point for this driver.
  9974. *
  9975. * Returns 0 success, anything else error.
  9976. */
  9977. static int
  9978. scsih_init(void)
  9979. {
  9980. mpt2_ids = 0;
  9981. mpt3_ids = 0;
  9982. mpt3sas_base_initialize_callback_handler();
  9983. /* queuecommand callback hander */
  9984. scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
  9985. /* task management callback handler */
  9986. tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
  9987. /* base internal commands callback handler */
  9988. base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
  9989. port_enable_cb_idx = mpt3sas_base_register_callback_handler(
  9990. mpt3sas_port_enable_done);
  9991. /* transport internal commands callback handler */
  9992. transport_cb_idx = mpt3sas_base_register_callback_handler(
  9993. mpt3sas_transport_done);
  9994. /* scsih internal commands callback handler */
  9995. scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
  9996. /* configuration page API internal commands callback handler */
  9997. config_cb_idx = mpt3sas_base_register_callback_handler(
  9998. mpt3sas_config_done);
  9999. /* ctl module callback handler */
  10000. ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
  10001. tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
  10002. _scsih_tm_tr_complete);
  10003. tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
  10004. _scsih_tm_volume_tr_complete);
  10005. tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
  10006. _scsih_sas_control_complete);
  10007. return 0;
  10008. }
  10009. /**
  10010. * scsih_exit - exit point for this driver (when it is a module).
  10011. *
  10012. * Returns 0 success, anything else error.
  10013. */
  10014. static void
  10015. scsih_exit(void)
  10016. {
  10017. mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
  10018. mpt3sas_base_release_callback_handler(tm_cb_idx);
  10019. mpt3sas_base_release_callback_handler(base_cb_idx);
  10020. mpt3sas_base_release_callback_handler(port_enable_cb_idx);
  10021. mpt3sas_base_release_callback_handler(transport_cb_idx);
  10022. mpt3sas_base_release_callback_handler(scsih_cb_idx);
  10023. mpt3sas_base_release_callback_handler(config_cb_idx);
  10024. mpt3sas_base_release_callback_handler(ctl_cb_idx);
  10025. mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
  10026. mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
  10027. mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
  10028. /* raid transport support */
  10029. if (hbas_to_enumerate != 1)
  10030. raid_class_release(mpt3sas_raid_template);
  10031. if (hbas_to_enumerate != 2)
  10032. raid_class_release(mpt2sas_raid_template);
  10033. sas_release_transport(mpt3sas_transport_template);
  10034. }
  10035. /**
  10036. * _mpt3sas_init - main entry point for this driver.
  10037. *
  10038. * Returns 0 success, anything else error.
  10039. */
  10040. static int __init
  10041. _mpt3sas_init(void)
  10042. {
  10043. int error;
  10044. pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
  10045. MPT3SAS_DRIVER_VERSION);
  10046. mpt3sas_transport_template =
  10047. sas_attach_transport(&mpt3sas_transport_functions);
  10048. if (!mpt3sas_transport_template)
  10049. return -ENODEV;
  10050. /* No need attach mpt3sas raid functions template
  10051. * if hbas_to_enumarate value is one.
  10052. */
  10053. if (hbas_to_enumerate != 1) {
  10054. mpt3sas_raid_template =
  10055. raid_class_attach(&mpt3sas_raid_functions);
  10056. if (!mpt3sas_raid_template) {
  10057. sas_release_transport(mpt3sas_transport_template);
  10058. return -ENODEV;
  10059. }
  10060. }
  10061. /* No need to attach mpt2sas raid functions template
  10062. * if hbas_to_enumarate value is two
  10063. */
  10064. if (hbas_to_enumerate != 2) {
  10065. mpt2sas_raid_template =
  10066. raid_class_attach(&mpt2sas_raid_functions);
  10067. if (!mpt2sas_raid_template) {
  10068. sas_release_transport(mpt3sas_transport_template);
  10069. return -ENODEV;
  10070. }
  10071. }
  10072. error = scsih_init();
  10073. if (error) {
  10074. scsih_exit();
  10075. return error;
  10076. }
  10077. mpt3sas_ctl_init(hbas_to_enumerate);
  10078. error = pci_register_driver(&mpt3sas_driver);
  10079. if (error)
  10080. scsih_exit();
  10081. return error;
  10082. }
  10083. /**
  10084. * _mpt3sas_exit - exit point for this driver (when it is a module).
  10085. *
  10086. */
  10087. static void __exit
  10088. _mpt3sas_exit(void)
  10089. {
  10090. pr_info("mpt3sas version %s unloading\n",
  10091. MPT3SAS_DRIVER_VERSION);
  10092. pci_unregister_driver(&mpt3sas_driver);
  10093. mpt3sas_ctl_exit(hbas_to_enumerate);
  10094. scsih_exit();
  10095. }
  10096. module_init(_mpt3sas_init);
  10097. module_exit(_mpt3sas_exit);