mpt2sas_scsih.c 226 KB

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