mpt3sas_scsih.c 230 KB

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