mpt3sas_scsih.c 231 KB

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